Last change was overcomplicated.

master
Robert Gerus 2015-12-23 20:57:31 +01:00
parent 95e143e1c6
commit 4998f0b548
2 changed files with 12 additions and 20 deletions

View File

@ -11,7 +11,6 @@ import (
"net" "net"
"sync" "sync"
"time" "time"
"unicode/utf8"
"github.com/arachnist/dyncfg" "github.com/arachnist/dyncfg"
) )
@ -40,27 +39,15 @@ type Connection struct {
func (c *Connection) Sender(msg Message) { func (c *Connection) Sender(msg Message) {
c.l.Lock() c.l.Lock()
defer c.l.Unlock() defer c.l.Unlock()
c.writer.WriteString(msg.String() + endline)
log.Println(c.network, "-->", msg.String())
c.writer.Flush()
if msg.WireLen() > maxLength { if msg.WireLen() > maxLength {
currLen := 0 if msg.Command == "PRIVMSG" { // we don't care otherwise
for i, ch := range msg.String() { newMsg := msg
currLen += utf8.RuneLen(ch) newMsg.Trailing = "Message truncated"
if currLen > maxLength { go c.Sender(newMsg)
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()
} }
} }

View File

@ -288,6 +288,11 @@ func (m *Message) Bytes() []byte {
buffer.WriteString(m.Trailing) buffer.WriteString(m.Trailing)
} }
// We need the limit the buffer length.
if buffer.Len() > (maxLength) {
buffer.Truncate(maxLength)
}
return buffer.Bytes() return buffer.Bytes()
} }