2016-08-14 12:48:51 -07:00
|
|
|
package birc
|
|
|
|
|
|
|
|
import (
|
2017-08-30 11:59:54 -07:00
|
|
|
"bytes"
|
2016-08-14 12:48:51 -07:00
|
|
|
"crypto/tls"
|
2016-10-29 09:59:12 -07:00
|
|
|
"fmt"
|
2016-08-14 12:48:51 -07:00
|
|
|
"github.com/42wim/matterbridge/bridge/config"
|
2017-11-24 14:27:13 -08:00
|
|
|
"github.com/42wim/matterbridge/bridge/helper"
|
2018-02-20 14:41:09 -08:00
|
|
|
log "github.com/sirupsen/logrus"
|
2017-10-20 13:22:13 -07:00
|
|
|
"github.com/lrstanley/girc"
|
2017-07-07 14:39:38 -07:00
|
|
|
"github.com/paulrosania/go-charset/charset"
|
|
|
|
_ "github.com/paulrosania/go-charset/data"
|
|
|
|
"github.com/saintfish/chardet"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
2017-10-20 13:22:13 -07:00
|
|
|
"net"
|
2016-09-20 15:33:40 -07:00
|
|
|
"regexp"
|
2016-08-14 12:48:51 -07:00
|
|
|
"sort"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"time"
|
2018-02-08 14:28:33 -08:00
|
|
|
"unicode/utf8"
|
2016-08-14 12:48:51 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
type Birc struct {
|
2017-10-20 13:22:13 -07:00
|
|
|
i *girc.Client
|
2017-06-17 15:13:10 -07:00
|
|
|
Nick string
|
|
|
|
names map[string][]string
|
|
|
|
connected chan struct{}
|
|
|
|
Local chan config.Message // local queue for flood control
|
|
|
|
FirstConnection bool
|
2017-12-19 14:15:03 -08:00
|
|
|
|
|
|
|
*config.BridgeConfig
|
2016-08-14 12:48:51 -07:00
|
|
|
}
|
|
|
|
|
2016-09-18 10:21:15 -07:00
|
|
|
var flog *log.Entry
|
|
|
|
var protocol = "irc"
|
2016-08-14 12:48:51 -07:00
|
|
|
|
|
|
|
func init() {
|
2016-09-18 10:21:15 -07:00
|
|
|
flog = log.WithFields(log.Fields{"module": protocol})
|
2016-08-14 12:48:51 -07:00
|
|
|
}
|
|
|
|
|
2017-12-19 14:15:03 -08:00
|
|
|
func New(cfg *config.BridgeConfig) *Birc {
|
2016-08-14 12:48:51 -07:00
|
|
|
b := &Birc{}
|
2017-12-19 14:15:03 -08:00
|
|
|
b.BridgeConfig = cfg
|
2016-09-19 14:35:47 -07:00
|
|
|
b.Nick = b.Config.Nick
|
2016-08-14 12:48:51 -07:00
|
|
|
b.names = make(map[string][]string)
|
2016-10-29 09:59:12 -07:00
|
|
|
b.connected = make(chan struct{})
|
2016-11-01 14:52:28 -07:00
|
|
|
if b.Config.MessageDelay == 0 {
|
|
|
|
b.Config.MessageDelay = 1300
|
|
|
|
}
|
|
|
|
if b.Config.MessageQueue == 0 {
|
|
|
|
b.Config.MessageQueue = 30
|
|
|
|
}
|
2017-05-29 12:54:34 -07:00
|
|
|
if b.Config.MessageLength == 0 {
|
|
|
|
b.Config.MessageLength = 400
|
|
|
|
}
|
2017-06-17 15:13:10 -07:00
|
|
|
b.FirstConnection = true
|
2016-08-14 12:48:51 -07:00
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Birc) Command(msg *config.Message) string {
|
|
|
|
switch msg.Text {
|
|
|
|
case "!users":
|
2017-10-20 13:22:13 -07:00
|
|
|
b.i.Handlers.Add(girc.RPL_NAMREPLY, b.storeNames)
|
|
|
|
b.i.Handlers.Add(girc.RPL_ENDOFNAMES, b.endNames)
|
|
|
|
b.i.Cmd.SendRaw("NAMES " + msg.Channel)
|
2016-08-14 12:48:51 -07:00
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2016-08-15 14:16:07 -07:00
|
|
|
func (b *Birc) Connect() error {
|
2017-02-14 12:12:02 -08:00
|
|
|
b.Local = make(chan config.Message, b.Config.MessageQueue+10)
|
2016-09-19 14:35:47 -07:00
|
|
|
flog.Infof("Connecting %s", b.Config.Server)
|
2017-10-20 13:22:13 -07:00
|
|
|
server, portstr, err := net.SplitHostPort(b.Config.Server)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2016-08-15 14:16:07 -07:00
|
|
|
}
|
2017-10-20 13:22:13 -07:00
|
|
|
port, err := strconv.Atoi(portstr)
|
2016-08-15 14:16:07 -07:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-11-11 14:16:58 -08:00
|
|
|
// fix strict user handling of girc
|
|
|
|
user := b.Config.Nick
|
|
|
|
for !girc.IsValidUser(user) {
|
|
|
|
if len(user) == 1 {
|
|
|
|
user = "matterbridge"
|
|
|
|
break
|
|
|
|
}
|
|
|
|
user = user[1:]
|
|
|
|
}
|
|
|
|
|
2017-10-20 13:22:13 -07:00
|
|
|
i := girc.New(girc.Config{
|
|
|
|
Server: server,
|
|
|
|
ServerPass: b.Config.Password,
|
|
|
|
Port: port,
|
|
|
|
Nick: b.Config.Nick,
|
2017-11-11 14:16:58 -08:00
|
|
|
User: user,
|
2017-10-20 13:22:13 -07:00
|
|
|
Name: b.Config.Nick,
|
|
|
|
SSL: b.Config.UseTLS,
|
2017-10-20 15:42:01 -07:00
|
|
|
TLSConfig: &tls.Config{InsecureSkipVerify: b.Config.SkipTLSVerify, ServerName: server},
|
2017-10-20 13:22:13 -07:00
|
|
|
PingDelay: time.Minute,
|
|
|
|
})
|
|
|
|
|
|
|
|
if b.Config.UseSASL {
|
|
|
|
i.Config.SASL = &girc.SASLPlain{b.Config.NickServNick, b.Config.NickServPassword}
|
|
|
|
}
|
|
|
|
|
|
|
|
i.Handlers.Add(girc.RPL_WELCOME, b.handleNewConnection)
|
|
|
|
i.Handlers.Add(girc.RPL_ENDOFMOTD, b.handleOtherAuth)
|
|
|
|
i.Handlers.Add("*", b.handleOther)
|
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
if err := i.Connect(); err != nil {
|
|
|
|
flog.Errorf("error: %s", err)
|
|
|
|
flog.Info("reconnecting in 30 seconds...")
|
|
|
|
time.Sleep(30 * time.Second)
|
|
|
|
i.Handlers.Clear(girc.RPL_WELCOME)
|
|
|
|
i.Handlers.Add(girc.RPL_WELCOME, func(client *girc.Client, event girc.Event) {
|
|
|
|
b.Remote <- config.Message{Username: "system", Text: "rejoin", Channel: "", Account: b.Account, Event: config.EVENT_REJOIN_CHANNELS}
|
|
|
|
// set our correct nick on reconnect if necessary
|
|
|
|
b.Nick = event.Source.Name
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
2016-08-15 14:16:07 -07:00
|
|
|
b.i = i
|
2016-10-29 09:59:12 -07:00
|
|
|
select {
|
|
|
|
case <-b.connected:
|
|
|
|
flog.Info("Connection succeeded")
|
|
|
|
case <-time.After(time.Second * 30):
|
|
|
|
return fmt.Errorf("connection timed out")
|
|
|
|
}
|
2017-10-20 13:22:13 -07:00
|
|
|
//i.Debug = false
|
|
|
|
i.Handlers.Clear("*")
|
2016-11-01 14:52:28 -07:00
|
|
|
go b.doSend()
|
2016-08-15 14:16:07 -07:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-02-14 12:12:02 -08:00
|
|
|
func (b *Birc) Disconnect() error {
|
2017-04-01 08:24:19 -07:00
|
|
|
//b.i.Disconnect()
|
2017-02-14 12:12:02 -08:00
|
|
|
close(b.Local)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-08-12 05:51:41 -07:00
|
|
|
func (b *Birc) JoinChannel(channel config.ChannelInfo) error {
|
|
|
|
if channel.Options.Key != "" {
|
|
|
|
flog.Debugf("using key %s for channel %s", channel.Options.Key, channel.Name)
|
2017-10-20 13:22:13 -07:00
|
|
|
b.i.Cmd.JoinKey(channel.Name, channel.Options.Key)
|
2017-08-12 05:51:41 -07:00
|
|
|
} else {
|
2017-10-20 13:22:13 -07:00
|
|
|
b.i.Cmd.Join(channel.Name)
|
2017-08-12 05:51:41 -07:00
|
|
|
}
|
2016-09-18 10:21:15 -07:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-08-27 13:59:37 -07:00
|
|
|
func (b *Birc) Send(msg config.Message) (string, error) {
|
2017-09-11 13:45:15 -07:00
|
|
|
// ignore delete messages
|
|
|
|
if msg.Event == config.EVENT_MSG_DELETE {
|
|
|
|
return "", nil
|
|
|
|
}
|
2016-09-19 15:21:14 -07:00
|
|
|
flog.Debugf("Receiving %#v", msg)
|
2016-08-14 12:48:51 -07:00
|
|
|
if strings.HasPrefix(msg.Text, "!") {
|
|
|
|
b.Command(&msg)
|
|
|
|
}
|
2017-08-30 11:59:54 -07:00
|
|
|
|
|
|
|
if b.Config.Charset != "" {
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
w, err := charset.NewWriter(b.Config.Charset, buf)
|
|
|
|
if err != nil {
|
|
|
|
flog.Errorf("charset from utf-8 conversion failed: %s", err)
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
fmt.Fprintf(w, msg.Text)
|
|
|
|
w.Close()
|
|
|
|
msg.Text = buf.String()
|
|
|
|
}
|
|
|
|
|
2017-11-24 13:36:19 -08:00
|
|
|
if msg.Extra != nil {
|
2018-02-02 16:11:11 -08:00
|
|
|
for _, rmsg := range helper.HandleExtra(&msg, b.General) {
|
|
|
|
b.Local <- rmsg
|
|
|
|
}
|
2017-11-24 13:36:19 -08:00
|
|
|
if len(msg.Extra["file"]) > 0 {
|
|
|
|
for _, f := range msg.Extra["file"] {
|
|
|
|
fi := f.(config.FileInfo)
|
2018-01-31 15:41:09 -08:00
|
|
|
if fi.Comment != "" {
|
2018-02-01 08:46:10 -08:00
|
|
|
msg.Text += fi.Comment + ": "
|
2018-01-31 15:41:09 -08:00
|
|
|
}
|
2017-11-24 13:36:19 -08:00
|
|
|
if fi.URL != "" {
|
|
|
|
msg.Text = fi.URL
|
|
|
|
}
|
2017-11-24 13:55:24 -08:00
|
|
|
b.Local <- config.Message{Text: msg.Text, Username: msg.Username, Channel: msg.Channel, Event: msg.Event}
|
2017-11-24 13:36:19 -08:00
|
|
|
}
|
2017-11-24 13:55:24 -08:00
|
|
|
return "", nil
|
2017-11-24 13:36:19 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-24 14:27:13 -08:00
|
|
|
// split long messages on messageLength, to avoid clipped messages #281
|
|
|
|
if b.Config.MessageSplit {
|
|
|
|
msg.Text = helper.SplitStringLength(msg.Text, b.Config.MessageLength)
|
|
|
|
}
|
2016-09-29 12:21:24 -07:00
|
|
|
for _, text := range strings.Split(msg.Text, "\n") {
|
2017-05-29 12:54:34 -07:00
|
|
|
if len(text) > b.Config.MessageLength {
|
2018-02-08 14:28:33 -08:00
|
|
|
text = text[:b.Config.MessageLength-len(" <message clipped>")]
|
|
|
|
if r, size := utf8.DecodeLastRuneInString(text); r == utf8.RuneError {
|
|
|
|
text = text[:len(text)-size]
|
|
|
|
}
|
|
|
|
text += " <message clipped>"
|
2017-05-29 12:54:34 -07:00
|
|
|
}
|
2016-11-01 14:52:28 -07:00
|
|
|
if len(b.Local) < b.Config.MessageQueue {
|
|
|
|
if len(b.Local) == b.Config.MessageQueue-1 {
|
|
|
|
text = text + " <message clipped>"
|
|
|
|
}
|
2017-07-30 08:48:23 -07:00
|
|
|
b.Local <- config.Message{Text: text, Username: msg.Username, Channel: msg.Channel, Event: msg.Event}
|
2016-11-01 14:52:28 -07:00
|
|
|
} else {
|
|
|
|
flog.Debugf("flooding, dropping message (queue at %d)", len(b.Local))
|
|
|
|
}
|
2016-09-29 12:21:24 -07:00
|
|
|
}
|
2017-08-27 13:59:37 -07:00
|
|
|
return "", nil
|
2016-08-14 12:48:51 -07:00
|
|
|
}
|
|
|
|
|
2016-11-01 14:52:28 -07:00
|
|
|
func (b *Birc) doSend() {
|
|
|
|
rate := time.Millisecond * time.Duration(b.Config.MessageDelay)
|
2017-07-13 15:35:01 -07:00
|
|
|
throttle := time.NewTicker(rate)
|
2016-11-01 14:52:28 -07:00
|
|
|
for msg := range b.Local {
|
2017-07-13 15:35:01 -07:00
|
|
|
<-throttle.C
|
2017-07-30 08:48:23 -07:00
|
|
|
if msg.Event == config.EVENT_USER_ACTION {
|
2017-10-20 13:22:13 -07:00
|
|
|
b.i.Cmd.Action(msg.Channel, msg.Username+msg.Text)
|
2017-07-30 08:48:23 -07:00
|
|
|
} else {
|
2017-10-20 13:22:13 -07:00
|
|
|
b.i.Cmd.Message(msg.Channel, msg.Username+msg.Text)
|
2017-07-30 08:48:23 -07:00
|
|
|
}
|
2016-11-01 14:52:28 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-20 13:22:13 -07:00
|
|
|
func (b *Birc) endNames(client *girc.Client, event girc.Event) {
|
|
|
|
channel := event.Params[1]
|
2016-08-14 12:48:51 -07:00
|
|
|
sort.Strings(b.names[channel])
|
|
|
|
maxNamesPerPost := (300 / b.nicksPerRow()) * b.nicksPerRow()
|
|
|
|
continued := false
|
|
|
|
for len(b.names[channel]) > maxNamesPerPost {
|
2016-09-19 14:35:47 -07:00
|
|
|
b.Remote <- config.Message{Username: b.Nick, Text: b.formatnicks(b.names[channel][0:maxNamesPerPost], continued),
|
2016-11-13 14:06:37 -08:00
|
|
|
Channel: channel, Account: b.Account}
|
2016-08-14 12:48:51 -07:00
|
|
|
b.names[channel] = b.names[channel][maxNamesPerPost:]
|
|
|
|
continued = true
|
|
|
|
}
|
2016-11-13 14:06:37 -08:00
|
|
|
b.Remote <- config.Message{Username: b.Nick, Text: b.formatnicks(b.names[channel], continued),
|
|
|
|
Channel: channel, Account: b.Account}
|
2016-08-14 12:48:51 -07:00
|
|
|
b.names[channel] = nil
|
2017-10-20 13:22:13 -07:00
|
|
|
b.i.Handlers.Clear(girc.RPL_NAMREPLY)
|
|
|
|
b.i.Handlers.Clear(girc.RPL_ENDOFNAMES)
|
2016-08-14 12:48:51 -07:00
|
|
|
}
|
|
|
|
|
2017-10-20 13:22:13 -07:00
|
|
|
func (b *Birc) handleNewConnection(client *girc.Client, event girc.Event) {
|
2016-09-19 14:35:47 -07:00
|
|
|
flog.Debug("Registering callbacks")
|
2016-08-14 12:48:51 -07:00
|
|
|
i := b.i
|
2017-10-20 13:22:13 -07:00
|
|
|
b.Nick = event.Params[0]
|
|
|
|
|
|
|
|
i.Handlers.Add(girc.RPL_ENDOFMOTD, b.handleOtherAuth)
|
|
|
|
i.Handlers.Add("PRIVMSG", b.handlePrivMsg)
|
|
|
|
i.Handlers.Add("CTCP_ACTION", b.handlePrivMsg)
|
|
|
|
i.Handlers.Add(girc.RPL_TOPICWHOTIME, b.handleTopicWhoTime)
|
|
|
|
i.Handlers.Add(girc.NOTICE, b.handleNotice)
|
|
|
|
i.Handlers.Add("JOIN", b.handleJoinPart)
|
|
|
|
i.Handlers.Add("PART", b.handleJoinPart)
|
|
|
|
i.Handlers.Add("QUIT", b.handleJoinPart)
|
|
|
|
i.Handlers.Add("KICK", b.handleJoinPart)
|
2016-10-29 09:59:12 -07:00
|
|
|
// we are now fully connected
|
|
|
|
b.connected <- struct{}{}
|
2016-08-14 12:48:51 -07:00
|
|
|
}
|
|
|
|
|
2017-10-20 13:22:13 -07:00
|
|
|
func (b *Birc) handleJoinPart(client *girc.Client, event girc.Event) {
|
2017-10-26 13:07:57 -07:00
|
|
|
if len(event.Params) == 0 {
|
|
|
|
flog.Debugf("handleJoinPart: empty Params? %#v", event)
|
|
|
|
return
|
|
|
|
}
|
2017-10-20 13:22:13 -07:00
|
|
|
channel := event.Params[0]
|
|
|
|
if event.Command == "KICK" {
|
|
|
|
flog.Infof("Got kicked from %s by %s", channel, event.Source.Name)
|
2017-12-22 15:11:30 -08:00
|
|
|
time.Sleep(time.Duration(b.Config.RejoinDelay) * time.Second)
|
2017-04-07 15:42:37 -07:00
|
|
|
b.Remote <- config.Message{Username: "system", Text: "rejoin", Channel: channel, Account: b.Account, Event: config.EVENT_REJOIN_CHANNELS}
|
|
|
|
return
|
|
|
|
}
|
2017-10-20 13:22:13 -07:00
|
|
|
if event.Command == "QUIT" {
|
|
|
|
if event.Source.Name == b.Nick && strings.Contains(event.Trailing, "Ping timeout") {
|
2017-02-14 12:12:02 -08:00
|
|
|
flog.Infof("%s reconnecting ..", b.Account)
|
|
|
|
b.Remote <- config.Message{Username: "system", Text: "reconnect", Channel: channel, Account: b.Account, Event: config.EVENT_FAILURE}
|
|
|
|
return
|
|
|
|
}
|
2016-11-14 13:53:06 -08:00
|
|
|
}
|
2017-10-20 13:22:13 -07:00
|
|
|
if event.Source.Name != b.Nick {
|
2017-06-17 08:58:56 -07:00
|
|
|
flog.Debugf("Sending JOIN_LEAVE event from %s to gateway", b.Account)
|
2017-10-20 13:22:13 -07:00
|
|
|
b.Remote <- config.Message{Username: "system", Text: event.Source.Name + " " + strings.ToLower(event.Command) + "s", Channel: channel, Account: b.Account, Event: config.EVENT_JOIN_LEAVE}
|
2017-06-17 08:58:56 -07:00
|
|
|
return
|
|
|
|
}
|
2016-11-14 13:53:06 -08:00
|
|
|
flog.Debugf("handle %#v", event)
|
|
|
|
}
|
|
|
|
|
2017-10-20 13:22:13 -07:00
|
|
|
func (b *Birc) handleNotice(client *girc.Client, event girc.Event) {
|
|
|
|
if strings.Contains(event.String(), "This nickname is registered") && event.Source.Name == b.Config.NickServNick {
|
|
|
|
b.i.Cmd.Message(b.Config.NickServNick, "IDENTIFY "+b.Config.NickServPassword)
|
2016-10-29 09:01:16 -07:00
|
|
|
} else {
|
2017-10-20 13:22:13 -07:00
|
|
|
b.handlePrivMsg(client, event)
|
2016-08-14 12:48:51 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-20 13:22:13 -07:00
|
|
|
func (b *Birc) handleOther(client *girc.Client, event girc.Event) {
|
|
|
|
switch event.Command {
|
2016-09-19 14:35:47 -07:00
|
|
|
case "372", "375", "376", "250", "251", "252", "253", "254", "255", "265", "266", "002", "003", "004", "005":
|
|
|
|
return
|
|
|
|
}
|
2017-10-20 13:22:13 -07:00
|
|
|
flog.Debugf("%#v", event.String())
|
2016-08-14 12:48:51 -07:00
|
|
|
}
|
|
|
|
|
2017-10-20 13:22:13 -07:00
|
|
|
func (b *Birc) handleOtherAuth(client *girc.Client, event girc.Event) {
|
2017-09-20 13:47:26 -07:00
|
|
|
if strings.EqualFold(b.Config.NickServNick, "Q@CServe.quakenet.org") {
|
|
|
|
flog.Debugf("Authenticating %s against %s", b.Config.NickServUsername, b.Config.NickServNick)
|
2017-10-20 13:22:13 -07:00
|
|
|
b.i.Cmd.Message(b.Config.NickServNick, "AUTH "+b.Config.NickServUsername+" "+b.Config.NickServPassword)
|
2017-09-20 13:47:26 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-20 13:22:13 -07:00
|
|
|
func (b *Birc) handlePrivMsg(client *girc.Client, event girc.Event) {
|
2017-06-17 15:13:10 -07:00
|
|
|
b.Nick = b.i.GetNick()
|
|
|
|
// freenode doesn't send 001 as first reply
|
2017-10-20 13:22:13 -07:00
|
|
|
if event.Command == "NOTICE" {
|
2017-06-17 15:13:10 -07:00
|
|
|
return
|
|
|
|
}
|
2016-10-29 07:27:07 -07:00
|
|
|
// don't forward queries to the bot
|
2017-10-20 13:22:13 -07:00
|
|
|
if event.Params[0] == b.Nick {
|
2016-10-29 07:27:07 -07:00
|
|
|
return
|
|
|
|
}
|
2016-10-29 07:35:16 -07:00
|
|
|
// don't forward message from ourself
|
2017-10-20 13:22:13 -07:00
|
|
|
if event.Source.Name == b.Nick {
|
2016-10-29 07:35:16 -07:00
|
|
|
return
|
|
|
|
}
|
2018-01-06 13:55:03 -08:00
|
|
|
rmsg := config.Message{Username: event.Source.Name, Channel: strings.ToLower(event.Params[0]), Account: b.Account, UserID: event.Source.Ident + "@" + event.Source.Host}
|
2017-10-20 13:22:13 -07:00
|
|
|
flog.Debugf("handlePrivMsg() %s %s %#v", event.Source.Name, event.Trailing, event)
|
2016-08-14 12:48:51 -07:00
|
|
|
msg := ""
|
2017-12-07 13:07:45 -08:00
|
|
|
if event.IsAction() {
|
2017-07-30 08:48:23 -07:00
|
|
|
rmsg.Event = config.EVENT_USER_ACTION
|
2016-08-14 12:48:51 -07:00
|
|
|
}
|
2017-12-07 13:07:45 -08:00
|
|
|
msg += event.StripAction()
|
2016-09-20 15:33:40 -07:00
|
|
|
// strip IRC colors
|
2017-10-28 05:28:15 -07:00
|
|
|
re := regexp.MustCompile(`[[:cntrl:]](?:\d{1,2}(?:,\d{1,2})?)?`)
|
2016-09-20 15:33:40 -07:00
|
|
|
msg = re.ReplaceAllString(msg, "")
|
2017-07-07 14:39:38 -07:00
|
|
|
|
|
|
|
var r io.Reader
|
2017-08-29 12:30:59 -07:00
|
|
|
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)
|
2017-08-29 12:35:36 -07:00
|
|
|
mycharset = result.Charset
|
2017-08-29 12:30:59 -07:00
|
|
|
// if we're not sure, just pick ISO-8859-1
|
|
|
|
if result.Confidence < 80 {
|
|
|
|
mycharset = "ISO-8859-1"
|
|
|
|
}
|
2017-07-07 14:39:38 -07:00
|
|
|
}
|
2017-08-29 12:30:59 -07:00
|
|
|
r, err = charset.NewReader(mycharset, strings.NewReader(msg))
|
2017-07-07 14:39:38 -07:00
|
|
|
if err != nil {
|
|
|
|
flog.Errorf("charset to utf-8 conversion failed: %s", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
output, _ := ioutil.ReadAll(r)
|
|
|
|
msg = string(output)
|
|
|
|
|
2017-10-20 13:22:13 -07:00
|
|
|
flog.Debugf("Sending message from %s on %s to gateway", event.Params[0], b.Account)
|
2017-07-30 08:48:23 -07:00
|
|
|
rmsg.Text = msg
|
|
|
|
b.Remote <- rmsg
|
2016-08-14 12:48:51 -07:00
|
|
|
}
|
|
|
|
|
2017-10-20 13:22:13 -07:00
|
|
|
func (b *Birc) handleTopicWhoTime(client *girc.Client, event girc.Event) {
|
|
|
|
parts := strings.Split(event.Params[2], "!")
|
|
|
|
t, err := strconv.ParseInt(event.Params[3], 10, 64)
|
2016-08-14 12:48:51 -07:00
|
|
|
if err != nil {
|
2017-10-20 13:22:13 -07:00
|
|
|
flog.Errorf("Invalid time stamp: %s", event.Params[3])
|
2016-08-14 12:48:51 -07:00
|
|
|
}
|
|
|
|
user := parts[0]
|
|
|
|
if len(parts) > 1 {
|
|
|
|
user += " [" + parts[1] + "]"
|
|
|
|
}
|
2017-10-20 13:22:13 -07:00
|
|
|
flog.Debugf("%s: Topic set by %s [%s]", event.Command, user, time.Unix(t, 0))
|
2016-08-14 12:48:51 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Birc) nicksPerRow() int {
|
2016-09-18 10:21:15 -07:00
|
|
|
return 4
|
2016-08-14 12:48:51 -07:00
|
|
|
}
|
|
|
|
|
2017-10-20 13:22:13 -07:00
|
|
|
func (b *Birc) storeNames(client *girc.Client, event girc.Event) {
|
|
|
|
channel := event.Params[2]
|
2016-08-14 12:48:51 -07:00
|
|
|
b.names[channel] = append(
|
|
|
|
b.names[channel],
|
2017-10-20 13:22:13 -07:00
|
|
|
strings.Split(strings.TrimSpace(event.Trailing), " ")...)
|
2016-08-14 12:48:51 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Birc) formatnicks(nicks []string, continued bool) string {
|
2016-09-18 10:21:15 -07:00
|
|
|
return plainformatter(nicks, b.nicksPerRow())
|
2016-08-14 12:48:51 -07:00
|
|
|
}
|