matterbridge/bridge/irc/irc.go

497 lines
14 KiB
Go
Raw Normal View History

package birc
import (
"bytes"
"crypto/tls"
"fmt"
"hash/crc32"
"io"
"io/ioutil"
"net"
2016-09-21 00:33:40 +02:00
"regexp"
"sort"
"strconv"
"strings"
"time"
"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/saintfish/chardet"
// We need to import the 'data' package as an implicit dependency.
// See: https://godoc.org/github.com/paulrosania/go-charset/charset
_ "github.com/paulrosania/go-charset/data"
)
type Birc struct {
i *girc.Client
Nick string
names map[string][]string
connected chan error
Local chan config.Message // local queue for flood control
FirstConnection, authDone bool
MessageDelay, MessageQueue, MessageLength int
*bridge.Config
}
func New(cfg *bridge.Config) bridge.Bridger {
b := &Birc{}
b.Config = cfg
b.Nick = b.GetString("Nick")
b.names = make(map[string][]string)
b.connected = make(chan error)
if b.GetInt("MessageDelay") == 0 {
b.MessageDelay = 1300
2018-03-06 20:51:02 +01:00
} else {
b.MessageDelay = b.GetInt("MessageDelay")
}
if b.GetInt("MessageQueue") == 0 {
b.MessageQueue = 30
2018-03-06 20:51:02 +01:00
} else {
b.MessageQueue = b.GetInt("MessageQueue")
}
if b.GetInt("MessageLength") == 0 {
b.MessageLength = 400
2018-03-06 20:51:02 +01:00
} else {
b.MessageLength = b.GetInt("MessageLength")
}
b.FirstConnection = true
return b
}
func (b *Birc) Command(msg *config.Message) string {
2018-11-08 00:46:34 +01:00
if msg.Text == "!users" {
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)
}
return ""
}
2016-08-15 23:16:07 +02:00
func (b *Birc) Connect() error {
2018-03-06 20:41:34 +01:00
b.Local = make(chan config.Message, b.MessageQueue+10)
b.Log.Infof("Connecting %s", b.GetString("Server"))
server, portstr, err := net.SplitHostPort(b.GetString("Server"))
if err != nil {
return err
2016-08-15 23:16:07 +02:00
}
port, err := strconv.Atoi(portstr)
2016-08-15 23:16:07 +02:00
if err != nil {
return err
}
// fix strict user handling of girc
user := b.GetString("Nick")
for !girc.IsValidUser(user) {
if len(user) == 1 {
user = "matterbridge"
break
}
user = user[1:]
}
i := girc.New(girc.Config{
Server: server,
ServerPass: b.GetString("Password"),
Port: port,
Nick: b.GetString("Nick"),
User: user,
Name: b.GetString("Nick"),
SSL: b.GetBool("UseTLS"),
TLSConfig: &tls.Config{InsecureSkipVerify: b.GetBool("SkipTLSVerify"), ServerName: server},
PingDelay: time.Minute,
})
if b.GetBool("UseSASL") {
i.Config.SASL = &girc.SASLPlain{
User: b.GetString("NickServNick"),
Pass: b.GetString("NickServPassword"),
}
}
i.Handlers.Add(girc.RPL_WELCOME, b.handleNewConnection)
i.Handlers.Add(girc.RPL_ENDOFMOTD, b.handleOtherAuth)
2018-02-22 18:56:21 +01:00
i.Handlers.Add(girc.ALL_EVENTS, b.handleOther)
go func() {
for {
if err := i.Connect(); err != nil {
b.Log.Errorf("disconnect: error: %s", err)
if b.FirstConnection {
b.connected <- err
return
}
} else {
b.Log.Info("disconnect: client requested quit")
}
b.Log.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.EventRejoinChannels}
// set our correct nick on reconnect if necessary
b.Nick = event.Source.Name
})
}
}()
2016-08-15 23:16:07 +02:00
b.i = i
err = <-b.connected
if err != nil {
return fmt.Errorf("connection failed %s", err)
}
b.Log.Info("Connection succeeded")
b.FirstConnection = false
if b.GetInt("DebugLevel") == 0 {
2018-02-22 18:56:21 +01:00
i.Handlers.Clear(girc.ALL_EVENTS)
}
go b.doSend()
2016-08-15 23:16:07 +02:00
return nil
}
func (b *Birc) Disconnect() error {
b.i.Close()
close(b.Local)
return nil
}
func (b *Birc) JoinChannel(channel config.ChannelInfo) error {
// need to check if we have nickserv auth done before joining channels
for {
if b.authDone {
break
}
time.Sleep(time.Second)
}
if channel.Options.Key != "" {
2018-02-27 00:33:21 +01:00
b.Log.Debugf("using key %s for channel %s", channel.Options.Key, channel.Name)
b.i.Cmd.JoinKey(channel.Name, channel.Options.Key)
} else {
b.i.Cmd.Join(channel.Name)
}
b.authDone = false
return nil
}
func (b *Birc) Send(msg config.Message) (string, error) {
// ignore delete messages
if msg.Event == config.EventMsgDelete {
return "", nil
}
2018-02-24 23:56:35 +01:00
2018-02-28 22:23:29 +01:00
b.Log.Debugf("=> Receiving %#v", msg)
2018-02-24 23:56:35 +01:00
// we can be in between reconnects #385
if !b.i.IsConnected() {
b.Log.Error("Not connected to server, dropping message")
}
2018-02-24 23:56:35 +01:00
// Execute a command
if strings.HasPrefix(msg.Text, "!") {
b.Command(&msg)
}
2018-02-24 23:56:35 +01:00
// convert to specified 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)
w, err := charset.NewWriter(b.GetString("Charset"), buf)
if err != nil {
b.Log.Errorf("charset from utf-8 conversion failed: %s", err)
return "", err
}
2018-05-18 21:45:39 +02:00
fmt.Fprint(w, msg.Text)
w.Close()
msg.Text = buf.String()
}
}
2018-02-24 23:56:35 +01:00
// Handle files
if msg.Extra != nil {
for _, rmsg := range helper.HandleExtra(&msg, b.General) {
b.Local <- rmsg
}
if len(msg.Extra["file"]) > 0 {
for _, f := range msg.Extra["file"] {
fi := f.(config.FileInfo)
if fi.Comment != "" {
msg.Text += fi.Comment + ": "
}
if fi.URL != "" {
msg.Text = fi.URL
2018-05-06 16:57:59 +02:00
if fi.Comment != "" {
msg.Text = fi.Comment + ": " + fi.URL
}
}
b.Local <- config.Message{Text: msg.Text, Username: msg.Username, Channel: msg.Channel, Event: msg.Event}
}
return "", nil
}
}
var msgLines []string
if b.GetBool("MessageSplit") {
msgLines = helper.GetSubLines(msg.Text, b.MessageLength)
} else {
msgLines = helper.GetSubLines(msg.Text, 0)
}
for i := range msgLines {
if len(b.Local) >= b.MessageQueue {
2018-02-27 00:33:21 +01:00
b.Log.Debugf("flooding, dropping message (queue at %d)", len(b.Local))
return "", nil
}
b.Local <- config.Message{
Text: msgLines[i],
Username: msg.Username,
Channel: msg.Channel,
Event: msg.Event,
}
2016-09-29 21:21:24 +02:00
}
return "", nil
}
func (b *Birc) doSend() {
rate := time.Millisecond * time.Duration(b.MessageDelay)
2017-07-14 00:35:01 +02:00
throttle := time.NewTicker(rate)
for msg := range b.Local {
2017-07-14 00:35:01 +02:00
<-throttle.C
username := msg.Username
if b.GetBool("Colornicks") {
checksum := crc32.ChecksumIEEE([]byte(msg.Username))
colorCode := checksum%14 + 2 // quick fix - prevent white or black color codes
username = fmt.Sprintf("\x03%02d%s\x0F", colorCode, msg.Username)
}
if msg.Event == config.EventUserAction {
b.i.Cmd.Action(msg.Channel, username+msg.Text)
} else {
b.Log.Debugf("Sending to channel %s", msg.Channel)
b.i.Cmd.Message(msg.Channel, username+msg.Text)
}
}
}
func (b *Birc) endNames(client *girc.Client, event girc.Event) {
channel := event.Params[1]
sort.Strings(b.names[channel])
maxNamesPerPost := (300 / b.nicksPerRow()) * b.nicksPerRow()
for len(b.names[channel]) > maxNamesPerPost {
2018-11-08 00:29:30 +01:00
b.Remote <- config.Message{Username: b.Nick, Text: b.formatnicks(b.names[channel][0:maxNamesPerPost]),
2016-11-13 23:06:37 +01:00
Channel: channel, Account: b.Account}
b.names[channel] = b.names[channel][maxNamesPerPost:]
}
2018-11-08 00:29:30 +01:00
b.Remote <- config.Message{Username: b.Nick, Text: b.formatnicks(b.names[channel]),
2016-11-13 23:06:37 +01:00
Channel: channel, Account: b.Account}
b.names[channel] = nil
b.i.Handlers.Clear(girc.RPL_NAMREPLY)
b.i.Handlers.Clear(girc.RPL_ENDOFNAMES)
}
func (b *Birc) handleNewConnection(client *girc.Client, event girc.Event) {
2018-02-27 00:33:21 +01:00
b.Log.Debug("Registering callbacks")
i := b.i
b.Nick = event.Params[0]
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)
}
func (b *Birc) handleJoinPart(client *girc.Client, event girc.Event) {
2017-10-26 22:07:57 +02:00
if len(event.Params) == 0 {
2018-02-27 00:33:21 +01:00
b.Log.Debugf("handleJoinPart: empty Params? %#v", event)
2017-10-26 22:07:57 +02:00
return
}
channel := strings.ToLower(event.Params[0])
if event.Command == "KICK" && event.Params[1] == b.Nick {
2018-02-27 00:33:21 +01:00
b.Log.Infof("Got kicked from %s by %s", channel, event.Source.Name)
time.Sleep(time.Duration(b.GetInt("RejoinDelay")) * time.Second)
b.Remote <- config.Message{Username: "system", Text: "rejoin", Channel: channel, Account: b.Account, Event: config.EventRejoinChannels}
return
}
if event.Command == "QUIT" {
if event.Source.Name == b.Nick && strings.Contains(event.Trailing, "Ping timeout") {
2018-02-27 00:33:21 +01:00
b.Log.Infof("%s reconnecting ..", b.Account)
b.Remote <- config.Message{Username: "system", Text: "reconnect", Channel: channel, Account: b.Account, Event: config.EventFailure}
return
}
}
if event.Source.Name != b.Nick {
if b.GetBool("nosendjoinpart") {
return
}
2018-02-28 22:23:29 +01:00
b.Log.Debugf("<= Sending JOIN_LEAVE event from %s to gateway", b.Account)
msg := config.Message{Username: "system", Text: event.Source.Name + " " + strings.ToLower(event.Command) + "s", Channel: channel, Account: b.Account, Event: config.EventJoinLeave}
2018-02-28 22:23:29 +01:00
b.Log.Debugf("<= Message is %#v", msg)
2018-02-22 18:23:22 +01:00
b.Remote <- msg
return
}
2018-02-27 00:33:21 +01:00
b.Log.Debugf("handle %#v", event)
}
func (b *Birc) handleNotice(client *girc.Client, event girc.Event) {
if strings.Contains(event.String(), "This nickname is registered") && event.Source.Name == b.GetString("NickServNick") {
2018-11-22 22:46:38 +01:00
b.handleNickServ()
2016-10-29 18:01:16 +02:00
} else {
b.handlePrivMsg(client, event)
}
}
func (b *Birc) handleOther(client *girc.Client, event girc.Event) {
if b.GetInt("DebugLevel") == 1 {
2018-02-22 18:56:21 +01:00
if event.Command != "CLIENT_STATE_UPDATED" &&
event.Command != "CLIENT_GENERAL_UPDATED" {
2018-02-27 00:33:21 +01:00
b.Log.Debugf("%#v", event.String())
2018-02-22 18:56:21 +01:00
}
return
}
switch event.Command {
2016-09-19 23:35:47 +02:00
case "372", "375", "376", "250", "251", "252", "253", "254", "255", "265", "266", "002", "003", "004", "005":
return
}
2018-02-27 00:33:21 +01:00
b.Log.Debugf("%#v", event.String())
}
func (b *Birc) handleOtherAuth(client *girc.Client, event girc.Event) {
2018-11-22 22:46:38 +01:00
b.handleNickServ()
b.handleRunCommands()
2018-11-22 22:46:38 +01:00
// we are now fully connected
b.connected <- nil
}
2018-02-24 23:56:35 +01:00
func (b *Birc) skipPrivMsg(event girc.Event) bool {
// Our nick can be changed
b.Nick = b.i.GetNick()
2018-02-24 23:56:35 +01:00
// freenode doesn't send 001 as first reply
if event.Command == "NOTICE" {
2018-02-24 23:56:35 +01:00
return true
}
2016-10-29 16:27:07 +02:00
// don't forward queries to the bot
if event.Params[0] == b.Nick {
2018-02-24 23:56:35 +01:00
return true
2016-10-29 16:27:07 +02:00
}
// don't forward message from ourself
if event.Source.Name == b.Nick {
2018-02-24 23:56:35 +01:00
return true
}
return false
}
func (b *Birc) handlePrivMsg(client *girc.Client, event girc.Event) {
if b.skipPrivMsg(event) {
return
}
rmsg := config.Message{Username: event.Source.Name, Channel: strings.ToLower(event.Params[0]), Account: b.Account, UserID: event.Source.Ident + "@" + event.Source.Host}
2018-02-28 22:23:29 +01:00
b.Log.Debugf("== Receiving PRIVMSG: %s %s %#v", event.Source.Name, event.Trailing, event)
2018-02-24 23:56:35 +01:00
// set action event
if event.IsAction() {
rmsg.Event = config.EventUserAction
}
2018-02-24 23:56:35 +01:00
// strip action, we made an event if it was an action
rmsg.Text += event.StripAction()
2016-09-21 00:33:40 +02:00
// strip IRC colors
re := regexp.MustCompile(`\x03(?:\d{1,2}(?:,\d{1,2})?)?|[[:cntrl:]]`)
2018-02-24 23:56:35 +01:00
rmsg.Text = re.ReplaceAllString(rmsg.Text, "")
2018-02-24 23:56:35 +01:00
// start detecting the charset
var r io.Reader
var err error
mycharset := b.GetString("Charset")
if mycharset == "" {
// detect what were sending so that we convert it to utf-8
detector := chardet.NewTextDetector()
2018-02-24 23:56:35 +01:00
result, err := detector.DetectBest([]byte(rmsg.Text))
if err != nil {
2018-02-27 00:33:21 +01:00
b.Log.Infof("detection failed for rmsg.Text: %#v", rmsg.Text)
return
}
2018-02-27 00:33:21 +01:00
b.Log.Debugf("detected %s confidence %#v", result.Charset, result.Confidence)
2017-08-29 21:35:36 +02:00
mycharset = result.Charset
// if we're not sure, just pick ISO-8859-1
if result.Confidence < 80 {
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))
if err != nil {
b.Log.Errorf("charset to utf-8 conversion failed: %s", err)
return
}
output, _ := ioutil.ReadAll(r)
rmsg.Text = string(output)
}
2018-02-28 22:23:29 +01:00
b.Log.Debugf("<= Sending message from %s on %s to gateway", event.Params[0], b.Account)
b.Remote <- rmsg
}
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)
if err != nil {
2018-02-27 00:33:21 +01:00
b.Log.Errorf("Invalid time stamp: %s", event.Params[3])
}
user := parts[0]
if len(parts) > 1 {
user += " [" + parts[1] + "]"
}
2018-02-27 00:33:21 +01:00
b.Log.Debugf("%s: Topic set by %s [%s]", event.Command, user, time.Unix(t, 0))
}
func (b *Birc) nicksPerRow() int {
return 4
}
func (b *Birc) storeNames(client *girc.Client, event girc.Event) {
channel := event.Params[2]
b.names[channel] = append(
b.names[channel],
strings.Split(strings.TrimSpace(event.Trailing), " ")...)
}
2018-11-08 00:29:30 +01:00
func (b *Birc) formatnicks(nicks []string) string {
return strings.Join(nicks, ", ") + " currently on IRC"
}
2018-11-22 22:46:38 +01:00
func (b *Birc) handleRunCommands() {
for _, cmd := range b.GetStringSlice("RunCommands") {
if err := b.i.Cmd.SendRaw(cmd); err != nil {
b.Log.Errorf("RunCommands %s failed: %s", cmd, err)
}
time.Sleep(time.Second)
}
}
2018-11-22 22:46:38 +01:00
func (b *Birc) handleNickServ() {
if !b.GetBool("UseSASL") && b.GetString("NickServNick") != "" && b.GetString("NickServPassword") != "" {
b.Log.Debugf("Sending identify to nickserv %s", b.GetString("NickServNick"))
b.i.Cmd.Message(b.GetString("NickServNick"), "IDENTIFY "+b.GetString("NickServPassword"))
}
if strings.EqualFold(b.GetString("NickServNick"), "Q@CServe.quakenet.org") {
b.Log.Debugf("Authenticating %s against %s", b.GetString("NickServUsername"), b.GetString("NickServNick"))
b.i.Cmd.Message(b.GetString("NickServNick"), "AUTH "+b.GetString("NickServUsername")+" "+b.GetString("NickServPassword"))
}
// give nickserv some slack
time.Sleep(time.Second * 5)
b.authDone = true
2018-11-22 22:46:38 +01:00
}