summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDimitri Sokolyuk <demon@dim13.org>2018-01-04 12:25:12 +0100
committerDimitri Sokolyuk <demon@dim13.org>2018-01-04 12:25:12 +0100
commit72f8954656af3ad362d9a912c90bd154faf438df (patch)
treeaa0210c914f928ca9268b6ebced4bb68fba4c326
parentca84f2f360172f52a57bd67a265509df68a13a50 (diff)
limit user input
-rw-r--r--main.go24
1 files 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)
}
}
}