2016-08-14 19:48:51 +00:00
|
|
|
package birc
|
|
|
|
|
|
|
|
import (
|
2017-08-30 18:59:54 +00:00
|
|
|
"bytes"
|
2016-08-14 19:48:51 +00:00
|
|
|
"crypto/tls"
|
2016-10-29 16:59:12 +00:00
|
|
|
"fmt"
|
2017-07-28 12:26:26 +00:00
|
|
|
"github.com/42wim/go-ircevent"
|
2016-08-14 19:48:51 +00:00
|
|
|
"github.com/42wim/matterbridge/bridge/config"
|
|
|
|
log "github.com/Sirupsen/logrus"
|
2017-07-07 21:39:38 +00:00
|
|
|
"github.com/paulrosania/go-charset/charset"
|
|
|
|
_ "github.com/paulrosania/go-charset/data"
|
|
|
|
"github.com/saintfish/chardet"
|
2016-08-14 19:48:51 +00:00
|
|
|
ircm "github.com/sorcix/irc"
|
2017-07-07 21:39:38 +00:00
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
2016-09-20 22:33:40 +00:00
|
|
|
"regexp"
|
2016-08-14 19:48:51 +00:00
|
|
|
"sort"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Birc struct {
|
2017-06-17 22:13:10 +00:00
|
|
|
i *irc.Connection
|
|
|
|
Nick string
|
|
|
|
names map[string][]string
|
|
|
|
Config *config.Protocol
|
|
|
|
Remote chan config.Message
|
|
|
|
connected chan struct{}
|
|
|
|
Local chan config.Message // local queue for flood control
|
|
|
|
Account string
|
|
|
|
FirstConnection bool
|
2016-08-14 19:48:51 +00:00
|
|
|
}
|
|
|
|
|
2016-09-18 17:21:15 +00:00
|
|
|
var flog *log.Entry
|
|
|
|
var protocol = "irc"
|
2016-08-14 19:48:51 +00:00
|
|
|
|
|
|
|
func init() {
|
2016-09-18 17:21:15 +00:00
|
|
|
flog = log.WithFields(log.Fields{"module": protocol})
|
2016-08-14 19:48:51 +00:00
|
|
|
}
|
|
|
|
|
2016-11-13 22:06:37 +00:00
|
|
|
func New(cfg config.Protocol, account string, c chan config.Message) *Birc {
|
2016-08-14 19:48:51 +00:00
|
|
|
b := &Birc{}
|
2016-11-01 21:52:28 +00:00
|
|
|
b.Config = &cfg
|
2016-09-19 21:35:47 +00:00
|
|
|
b.Nick = b.Config.Nick
|
2016-08-14 19:48:51 +00:00
|
|
|
b.Remote = c
|
|
|
|
b.names = make(map[string][]string)
|
2016-11-13 22:06:37 +00:00
|
|
|
b.Account = account
|
2016-10-29 16:59:12 +00:00
|
|
|
b.connected = make(chan struct{})
|
2016-11-01 21:52:28 +00:00
|
|
|
if b.Config.MessageDelay == 0 {
|
|
|
|
b.Config.MessageDelay = 1300
|
|
|
|
}
|
|
|
|
if b.Config.MessageQueue == 0 {
|
|
|
|
b.Config.MessageQueue = 30
|
|
|
|
}
|
2017-05-29 19:54:34 +00:00
|
|
|
if b.Config.MessageLength == 0 {
|
|
|
|
b.Config.MessageLength = 400
|
|
|
|
}
|
2017-06-17 22:13:10 +00:00
|
|
|
b.FirstConnection = true
|
2016-08-14 19:48:51 +00:00
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Birc) Command(msg *config.Message) string {
|
|
|
|
switch msg.Text {
|
|
|
|
case "!users":
|
2016-11-13 23:10:55 +00:00
|
|
|
b.i.AddCallback(ircm.RPL_NAMREPLY, b.storeNames)
|
2016-10-23 12:11:21 +00:00
|
|
|
b.i.AddCallback(ircm.RPL_ENDOFNAMES, b.endNames)
|
2016-08-14 19:48:51 +00:00
|
|
|
b.i.SendRaw("NAMES " + msg.Channel)
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2016-08-15 21:16:07 +00:00
|
|
|
func (b *Birc) Connect() error {
|
2017-02-14 20:12:02 +00:00
|
|
|
b.Local = make(chan config.Message, b.Config.MessageQueue+10)
|
2016-09-19 21:35:47 +00:00
|
|
|
flog.Infof("Connecting %s", b.Config.Server)
|
2016-09-18 17:21:15 +00:00
|
|
|
i := irc.IRC(b.Config.Nick, b.Config.Nick)
|
2016-10-01 18:07:59 +00:00
|
|
|
if log.GetLevel() == log.DebugLevel {
|
|
|
|
i.Debug = true
|
|
|
|
}
|
2016-09-18 17:21:15 +00:00
|
|
|
i.UseTLS = b.Config.UseTLS
|
|
|
|
i.UseSASL = b.Config.UseSASL
|
|
|
|
i.SASLLogin = b.Config.NickServNick
|
|
|
|
i.SASLPassword = b.Config.NickServPassword
|
|
|
|
i.TLSConfig = &tls.Config{InsecureSkipVerify: b.Config.SkipTLSVerify}
|
2017-06-17 22:13:10 +00:00
|
|
|
i.KeepAlive = time.Minute
|
|
|
|
i.PingFreq = time.Minute
|
2016-09-18 17:21:15 +00:00
|
|
|
if b.Config.Password != "" {
|
|
|
|
i.Password = b.Config.Password
|
2016-08-15 21:16:07 +00:00
|
|
|
}
|
|
|
|
i.AddCallback(ircm.RPL_WELCOME, b.handleNewConnection)
|
2016-09-18 17:21:15 +00:00
|
|
|
err := i.Connect(b.Config.Server)
|
2016-08-15 21:16:07 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
b.i = i
|
2016-10-29 16:59:12 +00:00
|
|
|
select {
|
|
|
|
case <-b.connected:
|
|
|
|
flog.Info("Connection succeeded")
|
|
|
|
case <-time.After(time.Second * 30):
|
|
|
|
return fmt.Errorf("connection timed out")
|
|
|
|
}
|
|
|
|
i.Debug = false
|
2017-06-17 22:13:10 +00:00
|
|
|
// clear on reconnects
|
|
|
|
i.ClearCallback(ircm.RPL_WELCOME)
|
|
|
|
i.AddCallback(ircm.RPL_WELCOME, func(event *irc.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.Nick
|
|
|
|
})
|
|
|
|
go i.Loop()
|
2016-11-01 21:52:28 +00:00
|
|
|
go b.doSend()
|
2016-08-15 21:16:07 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-02-14 20:12:02 +00:00
|
|
|
func (b *Birc) Disconnect() error {
|
2017-04-01 15:24:19 +00:00
|
|
|
//b.i.Disconnect()
|
2017-02-14 20:12:02 +00:00
|
|
|
close(b.Local)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-08-12 12:51:41 +00: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)
|
|
|
|
b.i.Join(channel.Name + " " + channel.Options.Key)
|
|
|
|
} else {
|
|
|
|
b.i.Join(channel.Name)
|
|
|
|
}
|
2016-09-18 17:21:15 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-08-27 20:59:37 +00:00
|
|
|
func (b *Birc) Send(msg config.Message) (string, error) {
|
2016-09-19 22:21:14 +00:00
|
|
|
flog.Debugf("Receiving %#v", msg)
|
2016-08-14 19:48:51 +00:00
|
|
|
if strings.HasPrefix(msg.Text, "!") {
|
|
|
|
b.Command(&msg)
|
|
|
|
}
|
2017-08-30 18:59:54 +00: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()
|
|
|
|
}
|
|
|
|
|
2016-09-29 19:21:24 +00:00
|
|
|
for _, text := range strings.Split(msg.Text, "\n") {
|
2017-05-29 19:54:34 +00:00
|
|
|
if len(text) > b.Config.MessageLength {
|
|
|
|
text = text[:b.Config.MessageLength] + " <message clipped>"
|
|
|
|
}
|
2016-11-01 21:52:28 +00:00
|
|
|
if len(b.Local) < b.Config.MessageQueue {
|
|
|
|
if len(b.Local) == b.Config.MessageQueue-1 {
|
|
|
|
text = text + " <message clipped>"
|
|
|
|
}
|
2017-07-30 15:48:23 +00:00
|
|
|
b.Local <- config.Message{Text: text, Username: msg.Username, Channel: msg.Channel, Event: msg.Event}
|
2016-11-01 21:52:28 +00:00
|
|
|
} else {
|
|
|
|
flog.Debugf("flooding, dropping message (queue at %d)", len(b.Local))
|
|
|
|
}
|
2016-09-29 19:21:24 +00:00
|
|
|
}
|
2017-08-27 20:59:37 +00:00
|
|
|
return "", nil
|
2016-08-14 19:48:51 +00:00
|
|
|
}
|
|
|
|
|
2016-11-01 21:52:28 +00:00
|
|
|
func (b *Birc) doSend() {
|
|
|
|
rate := time.Millisecond * time.Duration(b.Config.MessageDelay)
|
2017-07-13 22:35:01 +00:00
|
|
|
throttle := time.NewTicker(rate)
|
2016-11-01 21:52:28 +00:00
|
|
|
for msg := range b.Local {
|
2017-07-13 22:35:01 +00:00
|
|
|
<-throttle.C
|
2017-07-30 15:48:23 +00:00
|
|
|
if msg.Event == config.EVENT_USER_ACTION {
|
|
|
|
b.i.Action(msg.Channel, msg.Username+msg.Text)
|
|
|
|
} else {
|
|
|
|
b.i.Privmsg(msg.Channel, msg.Username+msg.Text)
|
|
|
|
}
|
2016-11-01 21:52:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-14 19:48:51 +00:00
|
|
|
func (b *Birc) endNames(event *irc.Event) {
|
|
|
|
channel := event.Arguments[1]
|
|
|
|
sort.Strings(b.names[channel])
|
|
|
|
maxNamesPerPost := (300 / b.nicksPerRow()) * b.nicksPerRow()
|
|
|
|
continued := false
|
|
|
|
for len(b.names[channel]) > maxNamesPerPost {
|
2016-09-19 21:35:47 +00:00
|
|
|
b.Remote <- config.Message{Username: b.Nick, Text: b.formatnicks(b.names[channel][0:maxNamesPerPost], continued),
|
2016-11-13 22:06:37 +00:00
|
|
|
Channel: channel, Account: b.Account}
|
2016-08-14 19:48:51 +00:00
|
|
|
b.names[channel] = b.names[channel][maxNamesPerPost:]
|
|
|
|
continued = true
|
|
|
|
}
|
2016-11-13 22:06:37 +00:00
|
|
|
b.Remote <- config.Message{Username: b.Nick, Text: b.formatnicks(b.names[channel], continued),
|
|
|
|
Channel: channel, Account: b.Account}
|
2016-08-14 19:48:51 +00:00
|
|
|
b.names[channel] = nil
|
2016-11-20 16:20:52 +00:00
|
|
|
b.i.ClearCallback(ircm.RPL_NAMREPLY)
|
|
|
|
b.i.ClearCallback(ircm.RPL_ENDOFNAMES)
|
2016-08-14 19:48:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Birc) handleNewConnection(event *irc.Event) {
|
2016-09-19 21:35:47 +00:00
|
|
|
flog.Debug("Registering callbacks")
|
2016-08-14 19:48:51 +00:00
|
|
|
i := b.i
|
2016-09-19 21:35:47 +00:00
|
|
|
b.Nick = event.Arguments[0]
|
2016-08-14 19:48:51 +00:00
|
|
|
i.AddCallback("PRIVMSG", b.handlePrivMsg)
|
|
|
|
i.AddCallback("CTCP_ACTION", b.handlePrivMsg)
|
|
|
|
i.AddCallback(ircm.RPL_TOPICWHOTIME, b.handleTopicWhoTime)
|
|
|
|
i.AddCallback(ircm.NOTICE, b.handleNotice)
|
2016-09-19 21:35:47 +00:00
|
|
|
//i.AddCallback(ircm.RPL_MYINFO, func(e *irc.Event) { flog.Infof("%s: %s", e.Code, strings.Join(e.Arguments[1:], " ")) })
|
2016-08-14 19:48:51 +00:00
|
|
|
i.AddCallback("PING", func(e *irc.Event) {
|
|
|
|
i.SendRaw("PONG :" + e.Message())
|
2016-09-18 17:21:15 +00:00
|
|
|
flog.Debugf("PING/PONG")
|
2016-08-14 19:48:51 +00:00
|
|
|
})
|
2016-11-14 21:53:06 +00:00
|
|
|
i.AddCallback("JOIN", b.handleJoinPart)
|
|
|
|
i.AddCallback("PART", b.handleJoinPart)
|
|
|
|
i.AddCallback("QUIT", b.handleJoinPart)
|
2017-04-07 22:42:37 +00:00
|
|
|
i.AddCallback("KICK", b.handleJoinPart)
|
2016-08-14 19:48:51 +00:00
|
|
|
i.AddCallback("*", b.handleOther)
|
2016-10-29 16:59:12 +00:00
|
|
|
// we are now fully connected
|
|
|
|
b.connected <- struct{}{}
|
2016-08-14 19:48:51 +00:00
|
|
|
}
|
|
|
|
|
2016-11-14 21:53:06 +00:00
|
|
|
func (b *Birc) handleJoinPart(event *irc.Event) {
|
|
|
|
channel := event.Arguments[0]
|
2017-04-07 22:42:37 +00:00
|
|
|
if event.Code == "KICK" {
|
|
|
|
flog.Infof("Got kicked from %s by %s", channel, event.Nick)
|
|
|
|
b.Remote <- config.Message{Username: "system", Text: "rejoin", Channel: channel, Account: b.Account, Event: config.EVENT_REJOIN_CHANNELS}
|
|
|
|
return
|
|
|
|
}
|
2016-11-14 21:53:06 +00:00
|
|
|
if event.Code == "QUIT" {
|
2017-02-14 20:12:02 +00:00
|
|
|
if event.Nick == b.Nick && strings.Contains(event.Raw, "Ping timeout") {
|
|
|
|
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 21:53:06 +00:00
|
|
|
}
|
2017-06-17 15:58:56 +00:00
|
|
|
if event.Nick != b.Nick {
|
|
|
|
flog.Debugf("Sending JOIN_LEAVE event from %s to gateway", b.Account)
|
|
|
|
b.Remote <- config.Message{Username: "system", Text: event.Nick + " " + strings.ToLower(event.Code) + "s", Channel: channel, Account: b.Account, Event: config.EVENT_JOIN_LEAVE}
|
|
|
|
return
|
|
|
|
}
|
2016-11-14 21:53:06 +00:00
|
|
|
flog.Debugf("handle %#v", event)
|
|
|
|
}
|
|
|
|
|
2016-08-14 19:48:51 +00:00
|
|
|
func (b *Birc) handleNotice(event *irc.Event) {
|
2016-10-29 14:09:58 +00:00
|
|
|
if strings.Contains(event.Message(), "This nickname is registered") && event.Nick == b.Config.NickServNick {
|
2016-09-18 17:21:15 +00:00
|
|
|
b.i.Privmsg(b.Config.NickServNick, "IDENTIFY "+b.Config.NickServPassword)
|
2016-10-29 16:01:16 +00:00
|
|
|
} else {
|
|
|
|
b.handlePrivMsg(event)
|
2016-08-14 19:48:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Birc) handleOther(event *irc.Event) {
|
2016-09-19 21:35:47 +00:00
|
|
|
switch event.Code {
|
|
|
|
case "372", "375", "376", "250", "251", "252", "253", "254", "255", "265", "266", "002", "003", "004", "005":
|
|
|
|
return
|
|
|
|
}
|
|
|
|
flog.Debugf("%#v", event.Raw)
|
2016-08-14 19:48:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Birc) handlePrivMsg(event *irc.Event) {
|
2017-06-17 22:13:10 +00:00
|
|
|
b.Nick = b.i.GetNick()
|
|
|
|
// freenode doesn't send 001 as first reply
|
|
|
|
if event.Code == "NOTICE" {
|
|
|
|
return
|
|
|
|
}
|
2016-10-29 14:27:07 +00:00
|
|
|
// don't forward queries to the bot
|
|
|
|
if event.Arguments[0] == b.Nick {
|
|
|
|
return
|
|
|
|
}
|
2016-10-29 14:35:16 +00:00
|
|
|
// don't forward message from ourself
|
|
|
|
if event.Nick == b.Nick {
|
|
|
|
return
|
|
|
|
}
|
2017-07-30 15:48:23 +00:00
|
|
|
rmsg := config.Message{Username: event.Nick, Channel: event.Arguments[0], Account: b.Account, UserID: event.User + "@" + event.Host}
|
2016-10-29 14:27:07 +00:00
|
|
|
flog.Debugf("handlePrivMsg() %s %s %#v", event.Nick, event.Message(), event)
|
2016-08-14 19:48:51 +00:00
|
|
|
msg := ""
|
|
|
|
if event.Code == "CTCP_ACTION" {
|
2017-07-30 15:48:23 +00:00
|
|
|
// msg = event.Nick + " "
|
|
|
|
rmsg.Event = config.EVENT_USER_ACTION
|
2016-08-14 19:48:51 +00:00
|
|
|
}
|
|
|
|
msg += event.Message()
|
2016-09-20 22:33:40 +00:00
|
|
|
// strip IRC colors
|
2016-09-22 21:48:05 +00:00
|
|
|
re := regexp.MustCompile(`[[:cntrl:]](\d+,|)\d+`)
|
2016-09-20 22:33:40 +00:00
|
|
|
msg = re.ReplaceAllString(msg, "")
|
2017-07-07 21:39:38 +00:00
|
|
|
|
|
|
|
var r io.Reader
|
2017-08-29 19:30:59 +00: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 19:35:36 +00:00
|
|
|
mycharset = result.Charset
|
2017-08-29 19:30:59 +00:00
|
|
|
// if we're not sure, just pick ISO-8859-1
|
|
|
|
if result.Confidence < 80 {
|
|
|
|
mycharset = "ISO-8859-1"
|
|
|
|
}
|
2017-07-07 21:39:38 +00:00
|
|
|
}
|
2017-08-29 19:30:59 +00:00
|
|
|
r, err = charset.NewReader(mycharset, strings.NewReader(msg))
|
2017-07-07 21:39:38 +00:00
|
|
|
if err != nil {
|
|
|
|
flog.Errorf("charset to utf-8 conversion failed: %s", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
output, _ := ioutil.ReadAll(r)
|
|
|
|
msg = string(output)
|
|
|
|
|
2016-11-13 22:06:37 +00:00
|
|
|
flog.Debugf("Sending message from %s on %s to gateway", event.Arguments[0], b.Account)
|
2017-07-30 15:48:23 +00:00
|
|
|
rmsg.Text = msg
|
|
|
|
b.Remote <- rmsg
|
2016-08-14 19:48:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Birc) handleTopicWhoTime(event *irc.Event) {
|
|
|
|
parts := strings.Split(event.Arguments[2], "!")
|
|
|
|
t, err := strconv.ParseInt(event.Arguments[3], 10, 64)
|
|
|
|
if err != nil {
|
2016-09-18 17:21:15 +00:00
|
|
|
flog.Errorf("Invalid time stamp: %s", event.Arguments[3])
|
2016-08-14 19:48:51 +00:00
|
|
|
}
|
|
|
|
user := parts[0]
|
|
|
|
if len(parts) > 1 {
|
|
|
|
user += " [" + parts[1] + "]"
|
|
|
|
}
|
2016-09-19 21:35:47 +00:00
|
|
|
flog.Debugf("%s: Topic set by %s [%s]", event.Code, user, time.Unix(t, 0))
|
2016-08-14 19:48:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Birc) nicksPerRow() int {
|
2016-09-18 17:21:15 +00:00
|
|
|
return 4
|
|
|
|
/*
|
|
|
|
if b.Config.Mattermost.NicksPerRow < 1 {
|
|
|
|
return 4
|
|
|
|
}
|
|
|
|
return b.Config.Mattermost.NicksPerRow
|
|
|
|
*/
|
2016-08-14 19:48:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Birc) storeNames(event *irc.Event) {
|
|
|
|
channel := event.Arguments[2]
|
|
|
|
b.names[channel] = append(
|
|
|
|
b.names[channel],
|
|
|
|
strings.Split(strings.TrimSpace(event.Message()), " ")...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Birc) formatnicks(nicks []string, continued bool) string {
|
2016-09-18 17:21:15 +00:00
|
|
|
return plainformatter(nicks, b.nicksPerRow())
|
|
|
|
/*
|
|
|
|
switch b.Config.Mattermost.NickFormatter {
|
|
|
|
case "table":
|
|
|
|
return tableformatter(nicks, b.nicksPerRow(), continued)
|
|
|
|
default:
|
|
|
|
return plainformatter(nicks, b.nicksPerRow())
|
|
|
|
}
|
|
|
|
*/
|
2016-08-14 19:48:51 +00:00
|
|
|
}
|