Add non UTF-8 encoding support

This commit is contained in:
Hiroaki Mizuguchi 2019-08-07 18:08:15 +09:00
parent 949efec008
commit 511f12d368
4 changed files with 18 additions and 2 deletions

2
go.mod
View File

@ -1,3 +1,5 @@
module github.com/thoj/go-ircevent module github.com/thoj/go-ircevent
go 1.12 go 1.12
require golang.org/x/text v0.3.2

3
go.sum Normal file
View File

@ -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=

12
irc.go
View File

@ -30,6 +30,8 @@ import (
"strconv" "strconv"
"strings" "strings"
"time" "time"
"golang.org/x/text/encoding"
) )
const ( const (
@ -41,7 +43,8 @@ var ErrDisconnected = errors.New("Disconnect Called")
// Read data from a connection. To be used as a goroutine. // Read data from a connection. To be used as a goroutine.
func (irc *Connection) readLoop() { func (irc *Connection) readLoop() {
defer irc.Done() defer irc.Done()
br := bufio.NewReaderSize(irc.socket, 512) r := irc.Encoding.NewDecoder().Reader(irc.socket)
br := bufio.NewReaderSize(r, 512)
errChan := irc.ErrorChan() 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. // Loop to write to a connection. To be used as a goroutine.
func (irc *Connection) writeLoop() { func (irc *Connection) writeLoop() {
defer irc.Done() defer irc.Done()
w := irc.Encoding.NewEncoder().Writer(irc.socket)
errChan := irc.ErrorChan() errChan := irc.ErrorChan()
for { for {
select { select {
@ -172,7 +176,7 @@ func (irc *Connection) writeLoop() {
// Set a write deadline based on the time out // Set a write deadline based on the time out
irc.socket.SetWriteDeadline(time.Now().Add(irc.Timeout)) irc.socket.SetWriteDeadline(time.Now().Add(irc.Timeout))
_, err := irc.socket.Write([]byte(b)) _, err := w.Write([]byte(b))
// Past blocking write, bin timeout // Past blocking write, bin timeout
var zero time.Time var zero time.Time
@ -465,6 +469,10 @@ func (irc *Connection) Connect(server string) error {
return err return err
} }
if irc.Encoding == nil {
irc.Encoding = encoding.Nop
}
irc.stopped = false irc.stopped = false
irc.Log.Printf("Connected to %s (%s)\n", irc.Server, irc.socket.RemoteAddr()) irc.Log.Printf("Connected to %s (%s)\n", irc.Server, irc.socket.RemoteAddr())

View File

@ -12,6 +12,8 @@ import (
"regexp" "regexp"
"sync" "sync"
"time" "time"
"golang.org/x/text/encoding"
) )
type Connection struct { type Connection struct {
@ -35,6 +37,7 @@ type Connection struct {
PingFreq time.Duration PingFreq time.Duration
KeepAlive time.Duration KeepAlive time.Duration
Server string Server string
Encoding encoding.Encoding
RealName string // The real name we want to display. RealName string // The real name we want to display.
// If zero-value defaults to the user. // If zero-value defaults to the user.