summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRobert S. Gerus <ar@bash.org.pl>2015-12-23 20:31:05 +0100
committerRobert S. Gerus <ar@bash.org.pl>2015-12-23 20:31:05 +0100
commit95e143e1c646dcfce1c8a9823e38b6683564f656 (patch)
tree8daec5f34fda22e6201bfd62a8f0187144e9762f
parent81886be013f7d1cb1518c34e1eacedbe2afb63b1 (diff)
parenta7eefc7daf05bae8e83ef96f65b288215459f7a6 (diff)
downloadgorepost-95e143e1c646dcfce1c8a9823e38b6683564f656.tar.gz
gorepost-95e143e1c646dcfce1c8a9823e38b6683564f656.tar.bz2
gorepost-95e143e1c646dcfce1c8a9823e38b6683564f656.zip
Merge pull request #53 from arachnist/verbose-truncate
Give explicit notice on message truncation.
-rw-r--r--irc/irc.go26
-rw-r--r--irc/message.go32
2 files changed, 51 insertions, 7 deletions
diff --git a/irc/irc.go b/irc/irc.go
index 24f9d3d..b4505af 100644
--- a/irc/irc.go
+++ b/irc/irc.go
@@ -11,6 +11,7 @@ import (
"net"
"sync"
"time"
+ "unicode/utf8"
"github.com/arachnist/dyncfg"
)
@@ -39,9 +40,28 @@ type Connection struct {
func (c *Connection) Sender(msg Message) {
c.l.Lock()
defer c.l.Unlock()
- c.writer.WriteString(msg.String() + endline)
- log.Println(c.network, "-->", msg.String())
- c.writer.Flush()
+ if msg.WireLen() > maxLength {
+ currLen := 0
+ for i, ch := range msg.String() {
+ currLen += utf8.RuneLen(ch)
+ if currLen > maxLength {
+ c.writer.WriteString(msg.String()[:i] + endline)
+ log.Println(c.network, "-->", msg.String())
+ c.writer.Flush()
+ // eh, it is a bit naive to assume that we won't explode again…
+ if msg.Command == "PRIVMSG" { // we don't care otherwise
+ newMsg := msg
+ newMsg.Trailing = "Message truncated"
+ go c.Sender(newMsg)
+ }
+ return
+ }
+ }
+ } else {
+ c.writer.WriteString(msg.String() + endline)
+ log.Println(c.network, "-->", msg.String())
+ c.writer.Flush()
+ }
}
// Receiver receives IRC messages from server, logs their contents, sets message
diff --git a/irc/message.go b/irc/message.go
index 2f8971a..e1d448b 100644
--- a/irc/message.go
+++ b/irc/message.go
@@ -288,12 +288,36 @@ func (m *Message) Bytes() []byte {
buffer.WriteString(m.Trailing)
}
- // We need the limit the buffer length.
- if buffer.Len() > (maxLength) {
- buffer.Truncate(maxLength)
+ return buffer.Bytes()
+}
+
+func (m *Message) WireLen() int {
+
+ buffer := new(bytes.Buffer)
+
+ // Message prefix
+ if m.Prefix != nil {
+ buffer.WriteByte(prefix)
+ m.Prefix.writeTo(buffer)
+ buffer.WriteByte(space)
}
- return buffer.Bytes()
+ // Command is required
+ buffer.WriteString(m.Command)
+
+ // Space separated list of arguments
+ if len(m.Params) > 0 {
+ buffer.WriteByte(space)
+ buffer.WriteString(strings.Join(m.Params, string(space)))
+ }
+
+ if len(m.Trailing) > 0 || m.EmptyTrailing {
+ buffer.WriteByte(space)
+ buffer.WriteByte(prefix)
+ buffer.WriteString(m.Trailing)
+ }
+
+ return buffer.Len()
}
// String returns a string representation of this message.