From e7fcb25107ff6f7676624fb3011e2bc1bede4cd0 Mon Sep 17 00:00:00 2001 From: Wim Date: Tue, 29 Aug 2017 21:30:59 +0200 Subject: Add a charset option (irc). Closes #247 --- bridge/config/config.go | 1 + bridge/irc/irc.go | 29 +++++++++++++++++------------ 2 files changed, 18 insertions(+), 12 deletions(-) (limited to 'bridge') diff --git a/bridge/config/config.go b/bridge/config/config.go index 7e6786a8..c5925a1d 100644 --- a/bridge/config/config.go +++ b/bridge/config/config.go @@ -43,6 +43,7 @@ type Protocol struct { AuthCode string // steam BindAddress string // mattermost, slack // DEPRECATED Buffer int // api + Charset string // irc EditSuffix string // mattermost, slack, discord, telegram, gitter EditDisable bool // mattermost, slack, discord, telegram, gitter IconURL string // mattermost, slack diff --git a/bridge/irc/irc.go b/bridge/irc/irc.go index 36b01b6c..5176389b 100644 --- a/bridge/irc/irc.go +++ b/bridge/irc/irc.go @@ -265,20 +265,25 @@ func (b *Birc) handlePrivMsg(event *irc.Event) { re := regexp.MustCompile(`[[:cntrl:]](\d+,|)\d+`) msg = re.ReplaceAllString(msg, "") - // detect what were sending so that we convert it to utf-8 - detector := chardet.NewTextDetector() - result, err := detector.DetectBest([]byte(msg)) - if err != nil { - flog.Infof("detection failed for msg: %#v", msg) - return - } - flog.Debugf("detected %s confidence %#v", result.Charset, result.Confidence) var r io.Reader - r, err = charset.NewReader(result.Charset, strings.NewReader(msg)) - // if we're not sure, just pick ISO-8859-1 - if result.Confidence < 80 { - r, err = charset.NewReader("ISO-8859-1", strings.NewReader(msg)) + var err error + mycharset := b.Config.Charset + if mycharset == "" { + // detect what were sending so that we convert it to utf-8 + detector := chardet.NewTextDetector() + result, err := detector.DetectBest([]byte(msg)) + if err != nil { + flog.Infof("detection failed for msg: %#v", msg) + return + } + flog.Debugf("detected %s confidence %#v", result.Charset, result.Confidence) + r, err = charset.NewReader(result.Charset, strings.NewReader(msg)) + // if we're not sure, just pick ISO-8859-1 + if result.Confidence < 80 { + mycharset = "ISO-8859-1" + } } + r, err = charset.NewReader(mycharset, strings.NewReader(msg)) if err != nil { flog.Errorf("charset to utf-8 conversion failed: %s", err) return -- cgit v1.2.3