2016-08-14 21:48:51 +02:00
|
|
|
package birc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/tls"
|
2016-10-29 18:59:12 +02:00
|
|
|
"fmt"
|
2018-05-11 23:02:43 +02:00
|
|
|
"hash/crc32"
|
2017-10-20 22:22:13 +02:00
|
|
|
"net"
|
2016-08-14 21:48:51 +02:00
|
|
|
"sort"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"time"
|
2018-06-09 12:47:40 +02:00
|
|
|
|
|
|
|
"github.com/42wim/matterbridge/bridge"
|
|
|
|
"github.com/42wim/matterbridge/bridge/config"
|
|
|
|
"github.com/42wim/matterbridge/bridge/helper"
|
|
|
|
"github.com/lrstanley/girc"
|
2018-11-14 22:43:52 +01:00
|
|
|
|
|
|
|
// 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"
|
2016-08-14 21:48:51 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type Birc struct {
|
2018-03-04 23:52:14 +01:00
|
|
|
i *girc.Client
|
|
|
|
Nick string
|
|
|
|
names map[string][]string
|
2018-11-23 00:26:50 +01:00
|
|
|
connected chan error
|
2018-03-04 23:52:14 +01:00
|
|
|
Local chan config.Message // local queue for flood control
|
2018-11-23 00:26:50 +01:00
|
|
|
FirstConnection, authDone bool
|
2018-03-04 23:52:14 +01:00
|
|
|
MessageDelay, MessageQueue, MessageLength int
|
|
|
|
|
|
|
|
*bridge.Config
|
2016-08-14 21:48:51 +02:00
|
|
|
}
|
|
|
|
|
2018-03-04 23:52:14 +01:00
|
|
|
func New(cfg *bridge.Config) bridge.Bridger {
|
2016-08-14 21:48:51 +02:00
|
|
|
b := &Birc{}
|
2018-03-04 23:52:14 +01:00
|
|
|
b.Config = cfg
|
|
|
|
b.Nick = b.GetString("Nick")
|
2016-08-14 21:48:51 +02:00
|
|
|
b.names = make(map[string][]string)
|
2018-11-23 00:26:50 +01:00
|
|
|
b.connected = make(chan error)
|
2018-03-04 23:52:14 +01:00
|
|
|
if b.GetInt("MessageDelay") == 0 {
|
|
|
|
b.MessageDelay = 1300
|
2018-03-06 20:51:02 +01:00
|
|
|
} else {
|
|
|
|
b.MessageDelay = b.GetInt("MessageDelay")
|
2016-11-01 22:52:28 +01:00
|
|
|
}
|
2018-03-04 23:52:14 +01:00
|
|
|
if b.GetInt("MessageQueue") == 0 {
|
|
|
|
b.MessageQueue = 30
|
2018-03-06 20:51:02 +01:00
|
|
|
} else {
|
|
|
|
b.MessageQueue = b.GetInt("MessageQueue")
|
2016-11-01 22:52:28 +01:00
|
|
|
}
|
2018-03-04 23:52:14 +01:00
|
|
|
if b.GetInt("MessageLength") == 0 {
|
|
|
|
b.MessageLength = 400
|
2018-03-06 20:51:02 +01:00
|
|
|
} else {
|
|
|
|
b.MessageLength = b.GetInt("MessageLength")
|
2017-05-29 21:54:34 +02:00
|
|
|
}
|
2017-06-18 00:13:10 +02:00
|
|
|
b.FirstConnection = true
|
2016-08-14 21:48:51 +02:00
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Birc) Command(msg *config.Message) string {
|
2018-11-08 00:46:34 +01:00
|
|
|
if msg.Text == "!users" {
|
2017-10-20 22:22:13 +02:00
|
|
|
b.i.Handlers.Add(girc.RPL_NAMREPLY, b.storeNames)
|
|
|
|
b.i.Handlers.Add(girc.RPL_ENDOFNAMES, b.endNames)
|
2018-11-28 10:58:56 +01:00
|
|
|
b.i.Cmd.SendRaw("NAMES " + msg.Channel) //nolint:errcheck
|
2016-08-14 21:48:51 +02:00
|
|
|
}
|
|
|
|
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)
|
2018-03-04 23:52:14 +01:00
|
|
|
b.Log.Infof("Connecting %s", b.GetString("Server"))
|
2018-11-28 10:58:56 +01:00
|
|
|
|
|
|
|
i, err := b.getClient()
|
2016-08-15 23:16:07 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-10-20 22:22:13 +02:00
|
|
|
|
2018-03-04 23:52:14 +01:00
|
|
|
if b.GetBool("UseSASL") {
|
2018-11-07 20:36:50 +01:00
|
|
|
i.Config.SASL = &girc.SASLPlain{
|
|
|
|
User: b.GetString("NickServNick"),
|
|
|
|
Pass: b.GetString("NickServPassword"),
|
|
|
|
}
|
2017-10-20 22:22:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
2018-11-23 00:26:50 +01:00
|
|
|
|
2018-11-28 10:58:56 +01:00
|
|
|
go b.doConnect()
|
|
|
|
|
2016-08-15 23:16:07 +02:00
|
|
|
b.i = i
|
2018-11-23 00:26:50 +01:00
|
|
|
err = <-b.connected
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("connection failed %s", err)
|
2016-10-29 18:59:12 +02:00
|
|
|
}
|
2018-11-23 00:26:50 +01:00
|
|
|
b.Log.Info("Connection succeeded")
|
|
|
|
b.FirstConnection = false
|
2018-03-04 23:52:14 +01:00
|
|
|
if b.GetInt("DebugLevel") == 0 {
|
2018-02-22 18:56:21 +01:00
|
|
|
i.Handlers.Clear(girc.ALL_EVENTS)
|
|
|
|
}
|
2016-11-01 22:52:28 +01:00
|
|
|
go b.doSend()
|
2016-08-15 23:16:07 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-02-14 21:12:02 +01:00
|
|
|
func (b *Birc) Disconnect() error {
|
2018-03-04 23:52:14 +01:00
|
|
|
b.i.Close()
|
2017-02-14 21:12:02 +01:00
|
|
|
close(b.Local)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-08-12 14:51:41 +02:00
|
|
|
func (b *Birc) JoinChannel(channel config.ChannelInfo) error {
|
2018-11-23 00:26:50 +01:00
|
|
|
// need to check if we have nickserv auth done before joining channels
|
|
|
|
for {
|
|
|
|
if b.authDone {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
time.Sleep(time.Second)
|
|
|
|
}
|
2017-08-12 14:51:41 +02:00
|
|
|
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)
|
2017-10-20 22:22:13 +02:00
|
|
|
b.i.Cmd.JoinKey(channel.Name, channel.Options.Key)
|
2017-08-12 14:51:41 +02:00
|
|
|
} else {
|
2017-10-20 22:22:13 +02:00
|
|
|
b.i.Cmd.Join(channel.Name)
|
2017-08-12 14:51:41 +02:00
|
|
|
}
|
2016-09-18 19:21:15 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-08-27 22:59:37 +02:00
|
|
|
func (b *Birc) Send(msg config.Message) (string, error) {
|
2017-09-11 22:45:15 +02:00
|
|
|
// ignore delete messages
|
2018-11-15 20:43:43 +01:00
|
|
|
if msg.Event == config.EventMsgDelete {
|
2017-09-11 22:45:15 +02:00
|
|
|
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
|
|
|
|
2018-03-05 22:50:38 +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
|
2016-08-14 21:48:51 +02:00
|
|
|
if strings.HasPrefix(msg.Text, "!") {
|
|
|
|
b.Command(&msg)
|
|
|
|
}
|
2017-08-30 20:59:54 +02:00
|
|
|
|
2018-02-24 23:56:35 +01:00
|
|
|
// convert to specified charset
|
2018-11-28 10:58:56 +01:00
|
|
|
if err := b.handleCharset(&msg); err != nil {
|
|
|
|
return "", err
|
2017-08-30 20:59:54 +02:00
|
|
|
}
|
|
|
|
|
2018-11-28 10:58:56 +01:00
|
|
|
// handle files, return if we're done here
|
|
|
|
if ok := b.handleFiles(&msg); ok {
|
|
|
|
return "", nil
|
2017-11-24 22:36:19 +01:00
|
|
|
}
|
|
|
|
|
2018-11-14 22:43:52 +01:00
|
|
|
var msgLines []string
|
2018-03-04 23:52:14 +01:00
|
|
|
if b.GetBool("MessageSplit") {
|
2018-11-14 22:43:52 +01:00
|
|
|
msgLines = helper.GetSubLines(msg.Text, b.MessageLength)
|
|
|
|
} else {
|
|
|
|
msgLines = helper.GetSubLines(msg.Text, 0)
|
2017-11-24 23:27:13 +01:00
|
|
|
}
|
2018-11-14 22:43:52 +01:00
|
|
|
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))
|
2018-11-14 22:43:52 +01:00
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
|
|
|
|
b.Local <- config.Message{
|
|
|
|
Text: msgLines[i],
|
|
|
|
Username: msg.Username,
|
|
|
|
Channel: msg.Channel,
|
|
|
|
Event: msg.Event,
|
2016-11-01 22:52:28 +01:00
|
|
|
}
|
2016-09-29 21:21:24 +02:00
|
|
|
}
|
2017-08-27 22:59:37 +02:00
|
|
|
return "", nil
|
2016-08-14 21:48:51 +02:00
|
|
|
}
|
|
|
|
|
2018-11-28 10:58:56 +01:00
|
|
|
func (b *Birc) doConnect() {
|
|
|
|
for {
|
|
|
|
if err := b.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)
|
|
|
|
b.i.Handlers.Clear(girc.RPL_WELCOME)
|
|
|
|
b.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-11-01 22:52:28 +01:00
|
|
|
func (b *Birc) doSend() {
|
2018-03-04 23:52:14 +01:00
|
|
|
rate := time.Millisecond * time.Duration(b.MessageDelay)
|
2017-07-14 00:35:01 +02:00
|
|
|
throttle := time.NewTicker(rate)
|
2016-11-01 22:52:28 +01:00
|
|
|
for msg := range b.Local {
|
2017-07-14 00:35:01 +02:00
|
|
|
<-throttle.C
|
2018-05-11 23:02:43 +02:00
|
|
|
username := msg.Username
|
|
|
|
if b.GetBool("Colornicks") {
|
|
|
|
checksum := crc32.ChecksumIEEE([]byte(msg.Username))
|
2018-05-29 22:52:01 +02:00
|
|
|
colorCode := checksum%14 + 2 // quick fix - prevent white or black color codes
|
|
|
|
username = fmt.Sprintf("\x03%02d%s\x0F", colorCode, msg.Username)
|
2018-05-11 23:02:43 +02:00
|
|
|
}
|
2018-11-15 20:43:43 +01:00
|
|
|
if msg.Event == config.EventUserAction {
|
2018-05-11 23:02:43 +02:00
|
|
|
b.i.Cmd.Action(msg.Channel, username+msg.Text)
|
2017-07-30 17:48:23 +02:00
|
|
|
} else {
|
2018-03-04 23:52:14 +01:00
|
|
|
b.Log.Debugf("Sending to channel %s", msg.Channel)
|
2018-05-11 23:02:43 +02:00
|
|
|
b.i.Cmd.Message(msg.Channel, username+msg.Text)
|
2017-07-30 17:48:23 +02:00
|
|
|
}
|
2016-11-01 22:52:28 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-28 10:58:56 +01:00
|
|
|
// validateInput validates the server/port/nick configuration. Returns a *girc.Client if successful
|
|
|
|
func (b *Birc) getClient() (*girc.Client, error) {
|
|
|
|
server, portstr, err := net.SplitHostPort(b.GetString("Server"))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
port, err := strconv.Atoi(portstr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, 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}, //nolint:gosec
|
|
|
|
PingDelay: time.Minute,
|
|
|
|
})
|
|
|
|
return i, nil
|
|
|
|
}
|
|
|
|
|
2017-10-20 22:22:13 +02:00
|
|
|
func (b *Birc) endNames(client *girc.Client, event girc.Event) {
|
|
|
|
channel := event.Params[1]
|
2016-08-14 21:48:51 +02:00
|
|
|
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}
|
2016-08-14 21:48:51 +02:00
|
|
|
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}
|
2016-08-14 21:48:51 +02:00
|
|
|
b.names[channel] = nil
|
2017-10-20 22:22:13 +02:00
|
|
|
b.i.Handlers.Clear(girc.RPL_NAMREPLY)
|
|
|
|
b.i.Handlers.Clear(girc.RPL_ENDOFNAMES)
|
2016-08-14 21:48:51 +02:00
|
|
|
}
|
|
|
|
|
2018-02-24 23:56:35 +01:00
|
|
|
func (b *Birc) skipPrivMsg(event girc.Event) bool {
|
|
|
|
// Our nick can be changed
|
2017-06-18 00:13:10 +02:00
|
|
|
b.Nick = b.i.GetNick()
|
2018-02-24 23:56:35 +01:00
|
|
|
|
2017-06-18 00:13:10 +02:00
|
|
|
// freenode doesn't send 001 as first reply
|
2017-10-20 22:22:13 +02:00
|
|
|
if event.Command == "NOTICE" {
|
2018-02-24 23:56:35 +01:00
|
|
|
return true
|
2017-06-18 00:13:10 +02:00
|
|
|
}
|
2016-10-29 16:27:07 +02:00
|
|
|
// don't forward queries to the bot
|
2017-10-20 22:22:13 +02:00
|
|
|
if event.Params[0] == b.Nick {
|
2018-02-24 23:56:35 +01:00
|
|
|
return true
|
2016-10-29 16:27:07 +02:00
|
|
|
}
|
2016-10-29 16:35:16 +02:00
|
|
|
// don't forward message from ourself
|
2017-10-20 22:22:13 +02:00
|
|
|
if event.Source.Name == b.Nick {
|
2018-02-24 23:56:35 +01:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2016-08-14 21:48:51 +02:00
|
|
|
func (b *Birc) nicksPerRow() int {
|
2016-09-18 19:21:15 +02:00
|
|
|
return 4
|
2016-08-14 21:48:51 +02:00
|
|
|
}
|
|
|
|
|
2017-10-20 22:22:13 +02:00
|
|
|
func (b *Birc) storeNames(client *girc.Client, event girc.Event) {
|
|
|
|
channel := event.Params[2]
|
2016-08-14 21:48:51 +02:00
|
|
|
b.names[channel] = append(
|
|
|
|
b.names[channel],
|
2017-10-20 22:22:13 +02:00
|
|
|
strings.Split(strings.TrimSpace(event.Trailing), " ")...)
|
2016-08-14 21:48:51 +02:00
|
|
|
}
|
|
|
|
|
2018-11-08 00:29:30 +01:00
|
|
|
func (b *Birc) formatnicks(nicks []string) string {
|
2018-11-14 22:43:52 +01:00
|
|
|
return strings.Join(nicks, ", ") + " currently on IRC"
|
2016-08-14 21:48:51 +02:00
|
|
|
}
|