From 511f12d368a5a6ae12b0be7217afad9e312a5099 Mon Sep 17 00:00:00 2001 From: Hiroaki Mizuguchi Date: Wed, 7 Aug 2019 18:08:15 +0900 Subject: [PATCH] Add non UTF-8 encoding support --- go.mod | 2 ++ go.sum | 3 +++ irc.go | 12 ++++++++++-- irc_struct.go | 3 +++ 4 files changed, 18 insertions(+), 2 deletions(-) create mode 100644 go.sum diff --git a/go.mod b/go.mod index 9aa24fa..b7c604b 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,5 @@ module github.com/thoj/go-ircevent go 1.12 + +require golang.org/x/text v0.3.2 diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..63c9200 --- /dev/null +++ b/go.sum @@ -0,0 +1,3 @@ +golang.org/x/text v0.3.2 h1:tW2bmiBqwgJj/UpqtC8EpXEZVYOwU0yG4iWbprSVAcs= +golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk= +golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= diff --git a/irc.go b/irc.go index de8f6a5..8a46d44 100644 --- a/irc.go +++ b/irc.go @@ -30,6 +30,8 @@ import ( "strconv" "strings" "time" + + "golang.org/x/text/encoding" ) const ( @@ -41,7 +43,8 @@ var ErrDisconnected = errors.New("Disconnect Called") // Read data from a connection. To be used as a goroutine. func (irc *Connection) readLoop() { defer irc.Done() - br := bufio.NewReaderSize(irc.socket, 512) + r := irc.Encoding.NewDecoder().Reader(irc.socket) + br := bufio.NewReaderSize(r, 512) errChan := irc.ErrorChan() @@ -155,6 +158,7 @@ func parseToEvent(msg string) (*Event, error) { // Loop to write to a connection. To be used as a goroutine. func (irc *Connection) writeLoop() { defer irc.Done() + w := irc.Encoding.NewEncoder().Writer(irc.socket) errChan := irc.ErrorChan() for { select { @@ -172,7 +176,7 @@ func (irc *Connection) writeLoop() { // Set a write deadline based on the time out irc.socket.SetWriteDeadline(time.Now().Add(irc.Timeout)) - _, err := irc.socket.Write([]byte(b)) + _, err := w.Write([]byte(b)) // Past blocking write, bin timeout var zero time.Time @@ -465,6 +469,10 @@ func (irc *Connection) Connect(server string) error { return err } + if irc.Encoding == nil { + irc.Encoding = encoding.Nop + } + irc.stopped = false irc.Log.Printf("Connected to %s (%s)\n", irc.Server, irc.socket.RemoteAddr()) diff --git a/irc_struct.go b/irc_struct.go index 6c696fc..948f49e 100644 --- a/irc_struct.go +++ b/irc_struct.go @@ -12,6 +12,8 @@ import ( "regexp" "sync" "time" + + "golang.org/x/text/encoding" ) type Connection struct { @@ -35,6 +37,7 @@ type Connection struct { PingFreq time.Duration KeepAlive time.Duration Server string + Encoding encoding.Encoding RealName string // The real name we want to display. // If zero-value defaults to the user.