diff --git a/xmpp.go b/xmpp.go index 4dd161a..30673c9 100644 --- a/xmpp.go +++ b/xmpp.go @@ -35,6 +35,7 @@ import ( "net/http" "net/url" "os" + "regexp" "slices" "strconv" "strings" @@ -1153,6 +1154,7 @@ func (c *Client) Send(chat Chat) (n int, err error) { stanza := "" + subtext + "%s" + oobtext + thdtext + "\n" + chat.Text = validUTF8(chat.Text) return fmt.Fprintf(c.stanzaWriter, stanza, xmlEscape(chat.Remote), xmlEscape(chat.Type), cnonce(), xmlEscape(chat.Text)) } @@ -1534,3 +1536,12 @@ func (t tee) Read(p []byte) (n int, err error) { } return } + +func validUTF8(s string) string { + // Remove invalid code points. + s = strings.ToValidUTF8(s, "�") + reg := regexp.MustCompile(`[\x{0000}-\x{0008}\x{000B}\x{000C}\x{000E}-\x{001F}]`) + s = reg.ReplaceAllString(s, "�") + + return s +}