Add support for CJK to/from utf-8 (irc). #400

This commit is contained in:
Wim 2018-05-11 21:55:53 +02:00
parent bf0b9959d1
commit 75381c2c6e

View File

@ -7,6 +7,7 @@ import (
"github.com/42wim/matterbridge/bridge" "github.com/42wim/matterbridge/bridge"
"github.com/42wim/matterbridge/bridge/config" "github.com/42wim/matterbridge/bridge/config"
"github.com/42wim/matterbridge/bridge/helper" "github.com/42wim/matterbridge/bridge/helper"
"github.com/dfordsoft/golib/ic"
"github.com/lrstanley/girc" "github.com/lrstanley/girc"
"github.com/paulrosania/go-charset/charset" "github.com/paulrosania/go-charset/charset"
_ "github.com/paulrosania/go-charset/data" _ "github.com/paulrosania/go-charset/data"
@ -177,6 +178,10 @@ func (b *Birc) Send(msg config.Message) (string, error) {
// convert to specified charset // convert to specified charset
if b.GetString("Charset") != "" { if b.GetString("Charset") != "" {
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) buf := new(bytes.Buffer)
w, err := charset.NewWriter(b.GetString("Charset"), buf) w, err := charset.NewWriter(b.GetString("Charset"), buf)
if err != nil { if err != nil {
@ -187,6 +192,7 @@ func (b *Birc) Send(msg config.Message) (string, error) {
w.Close() w.Close()
msg.Text = buf.String() msg.Text = buf.String()
} }
}
// Handle files // Handle files
if msg.Extra != nil { if msg.Extra != nil {
@ -404,6 +410,10 @@ func (b *Birc) handlePrivMsg(client *girc.Client, event girc.Event) {
mycharset = "ISO-8859-1" mycharset = "ISO-8859-1"
} }
} }
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)) r, err = charset.NewReader(mycharset, strings.NewReader(rmsg.Text))
if err != nil { if err != nil {
b.Log.Errorf("charset to utf-8 conversion failed: %s", err) b.Log.Errorf("charset to utf-8 conversion failed: %s", err)
@ -411,6 +421,7 @@ func (b *Birc) handlePrivMsg(client *girc.Client, event girc.Event) {
} }
output, _ := ioutil.ReadAll(r) output, _ := ioutil.ReadAll(r)
rmsg.Text = string(output) rmsg.Text = string(output)
}
b.Log.Debugf("<= Sending message from %s on %s to gateway", event.Params[0], b.Account) b.Log.Debugf("<= Sending message from %s on %s to gateway", event.Params[0], b.Account)
b.Remote <- rmsg b.Remote <- rmsg