natsim

NATS ↔ Instant Messaging Bridge
git clone https://git.instinctive.eu/natsim.git
Log | Files | Refs | README | LICENSE

commit c3dd8387263e194915571e62af9ef0d59913a02d
parent 12f1201ca2b3142466bda94d25ac1f38ff0dc332
Author: Natasha Kerensikova <natgh@instinctive.eu>
Date:   Tue, 29 Jul 2025 18:01:22 +0000

Spaces are allowed in hex and base-64 inputs
Diffstat:
Mmain.go | 18++++++++++++++----
1 file changed, 14 insertions(+), 4 deletions(-)

diff --git a/main.go b/main.go @@ -301,7 +301,7 @@ func (natsim *NatsIM) doCommands() { } case "b64data": - if decoded, err := base64.StdEncoding.DecodeString(cmd.arg); err != nil { + if decoded, err := b64Decode(cmd.arg); err != nil { natsim.ircSendError("b64Decode", err) } else { natsim.curMsg.Data = append(natsim.curMsg.Data, []byte(decoded)...) @@ -440,7 +440,7 @@ func (natsim *NatsIM) doCommands() { natsim.ircSend(buf.String()) case "hdata": - if decoded, err := hex.DecodeString(cmd.arg); err != nil { + if decoded, err := hexDecode(cmd.arg); err != nil { natsim.ircSendError("hexDecode", err) } else { natsim.curMsg.Data = append(natsim.curMsg.Data, []byte(decoded)...) @@ -679,14 +679,14 @@ func (natsim *NatsIM) ircReceive(e *irc.Event) { if len(data) >= 2 && data[0] == data[len(data)-1] && (data[0] == '"' || data[0] == '`' || data[0] == '#' || data[0] == '|') { switch data[0] { case '#': - if decoded, err := hex.DecodeString(data[1 : len(data)-1]); err != nil { + if decoded, err := hexDecode(data[1 : len(data)-1]); err != nil { natsim.ircSendError("hexDecode", err) return } else { natsim.curMsg.Data = decoded } case '|': - if decoded, err := base64.StdEncoding.DecodeString(data[1 : len(data)-1]); err != nil { + if decoded, err := b64Decode(data[1 : len(data)-1]); err != nil { natsim.ircSendError("b64Decode", err) return } else { @@ -1199,6 +1199,16 @@ func (af *antiflood) UnmarshalText(text []byte) error { return nil } +func b64Decode(s string) ([]byte, error) { + stripped := strings.ReplaceAll(s, " ", "") + return base64.StdEncoding.DecodeString(stripped) +} + +func hexDecode(s string) ([]byte, error) { + stripped := strings.ReplaceAll(s, " ", "") + return hex.DecodeString(stripped) +} + func humanizeNum(n uint64) string { num, unit, found := strings.Cut(humanize.Bytes(n), " ") if !found || unit == "" || unit[len(unit)-1:] != "B" {