diff options
author | Wim <wim@42.be> | 2018-05-11 21:55:53 +0200 |
---|---|---|
committer | Wim <wim@42.be> | 2018-05-11 21:55:53 +0200 |
commit | 75381c2c6e32b7a7b36c4b6a662feae2b1b0fb5a (patch) | |
tree | 2340b4cc13aa789b378c3ad19c8548f15353d6a9 /bridge | |
parent | bf0b9959d1b81d6362a390baaee70f2102fb2d58 (diff) | |
download | matterbridge-msglm-75381c2c6e32b7a7b36c4b6a662feae2b1b0fb5a.tar.gz matterbridge-msglm-75381c2c6e32b7a7b36c4b6a662feae2b1b0fb5a.tar.bz2 matterbridge-msglm-75381c2c6e32b7a7b36c4b6a662feae2b1b0fb5a.zip |
Add support for CJK to/from utf-8 (irc). #400
Diffstat (limited to 'bridge')
-rw-r--r-- | bridge/irc/irc.go | 39 |
1 files changed, 25 insertions, 14 deletions
diff --git a/bridge/irc/irc.go b/bridge/irc/irc.go index 83184008..1e26b770 100644 --- a/bridge/irc/irc.go +++ b/bridge/irc/irc.go @@ -7,6 +7,7 @@ import ( "github.com/42wim/matterbridge/bridge" "github.com/42wim/matterbridge/bridge/config" "github.com/42wim/matterbridge/bridge/helper" + "github.com/dfordsoft/golib/ic" "github.com/lrstanley/girc" "github.com/paulrosania/go-charset/charset" _ "github.com/paulrosania/go-charset/data" @@ -177,15 +178,20 @@ func (b *Birc) Send(msg config.Message) (string, error) { // convert to specified charset if b.GetString("Charset") != "" { - buf := new(bytes.Buffer) - w, err := charset.NewWriter(b.GetString("Charset"), buf) - if err != nil { - b.Log.Errorf("charset from utf-8 conversion failed: %s", err) - return "", err + switch b.GetString("Charset") { + case "gbk", "gb18030", "gb2312", "big5", "euc-kr", "euc-jp", "shift-jis", "iso-2022-jp": + msg.Text = ic.ConvertString("utf-8", b.GetString("Charset"), msg.Text) + default: + buf := new(bytes.Buffer) + w, err := charset.NewWriter(b.GetString("Charset"), buf) + if err != nil { + b.Log.Errorf("charset from utf-8 conversion failed: %s", err) + return "", err + } + fmt.Fprintf(w, msg.Text) + w.Close() + msg.Text = buf.String() } - fmt.Fprintf(w, msg.Text) - w.Close() - msg.Text = buf.String() } // Handle files @@ -404,13 +410,18 @@ func (b *Birc) handlePrivMsg(client *girc.Client, event girc.Event) { mycharset = "ISO-8859-1" } } - r, err = charset.NewReader(mycharset, strings.NewReader(rmsg.Text)) - if err != nil { - b.Log.Errorf("charset to utf-8 conversion failed: %s", err) - return + switch mycharset { + case "gbk", "gb18030", "gb2312", "big5", "euc-kr", "euc-jp", "shift-jis", "iso-2022-jp": + rmsg.Text = ic.ConvertString("utf-8", b.GetString("Charset"), rmsg.Text) + default: + r, err = charset.NewReader(mycharset, strings.NewReader(rmsg.Text)) + if err != nil { + b.Log.Errorf("charset to utf-8 conversion failed: %s", err) + return + } + output, _ := ioutil.ReadAll(r) + rmsg.Text = string(output) } - output, _ := ioutil.ReadAll(r) - rmsg.Text = string(output) b.Log.Debugf("<= Sending message from %s on %s to gateway", event.Params[0], b.Account) b.Remote <- rmsg |