Merge pull request #128 from akihiro/master

Add non UTF-8 encoding support
This commit is contained in:
Thomas Jager
2019-08-07 11:50:34 +00:00
committed by GitHub
4 changed files with 18 additions and 2 deletions

12
irc.go
View File

@@ -30,6 +30,8 @@ import (
"strconv"
"strings"
"time"
"golang.org/x/text/encoding"
)
const (
@@ -43,7 +45,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()
@@ -157,6 +160,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 {
@@ -174,7 +178,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
@@ -467,6 +471,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())