From 72f8954656af3ad362d9a912c90bd154faf438df Mon Sep 17 00:00:00 2001 From: Dimitri Sokolyuk Date: Thu, 4 Jan 2018 12:25:12 +0100 Subject: limit user input --- main.go | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/main.go b/main.go index 6150db4..105bf55 100644 --- a/main.go +++ b/main.go @@ -9,6 +9,8 @@ import ( irc "github.com/fluffle/goirc/client" ) +const maxLen = 500 + type Notify struct { conn *irc.Conn target string @@ -18,15 +20,19 @@ func NewNotify(conn *irc.Conn, target string) *Notify { return &Notify{conn: conn, target: target} } -func (n *Notify) Write(p []byte) (int, error) { - const maxLen = 500 - sz := len(p) - if sz > maxLen { - p = append(p[:maxLen], []byte("...")...) +func limit(n int, s string) string { + r := []rune(s) + if sz := len(r); sz > n { + r = append(r[:n], []rune("...")...) } - log.Println("Send", string(p)) - n.conn.Notice(n.target, string(p)) - return sz, nil + return string(r) +} + +func (n *Notify) Write(p []byte) (int, error) { + s := limit(maxLen, string(p)) + log.Println("Send", s) + n.conn.Notice(n.target, s) + return len(p), nil } func privmsg(room string) irc.HandlerFunc { @@ -57,7 +63,7 @@ func privmsg(room string) irc.HandlerFunc { fmt.Fprintf(NewNotify(conn, line.Target()), "Title: %v", title) } } - last[line.Nick] = t + last[line.Nick] = limit(maxLen, t) } } } -- cgit v1.2.3