2016-08-14 21:48:51 +02:00
|
|
|
package birc
|
|
|
|
|
|
|
|
import (
|
2017-08-30 20:59:54 +02:00
|
|
|
"bytes"
|
2016-08-14 21:48:51 +02:00
|
|
|
"crypto/tls"
|
2016-10-29 18:59:12 +02:00
|
|
|
"fmt"
|
2018-02-27 00:33:21 +01:00
|
|
|
"github.com/42wim/matterbridge/bridge"
|
2016-08-14 21:48:51 +02:00
|
|
|
"github.com/42wim/matterbridge/bridge/config"
|
2017-11-24 23:27:13 +01:00
|
|
|
"github.com/42wim/matterbridge/bridge/helper"
|
2018-05-11 21:55:53 +02:00
|
|
|
"github.com/dfordsoft/golib/ic"
|
2017-10-20 22:22:13 +02:00
|
|
|
"github.com/lrstanley/girc"
|
2017-07-07 23:39:38 +02:00
|
|
|
"github.com/paulrosania/go-charset/charset"
|
|
|
|
_ "github.com/paulrosania/go-charset/data"
|
|
|
|
"github.com/saintfish/chardet"
|
2018-05-11 23:02:43 +02:00
|
|
|
"hash/crc32"
|
2017-07-07 23:39:38 +02:00
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
2017-10-20 22:22:13 +02:00
|
|
|
"net"
|
2016-09-21 00:33:40 +02:00
|
|
|
"regexp"
|
2016-08-14 21:48:51 +02:00
|
|
|
"sort"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"time"
|
2018-02-08 23:28:33 +01:00
|
|
|
"unicode/utf8"
|
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
|
|
|
|
connected chan struct{}
|
|
|
|
Local chan config.Message // local queue for flood control
|
|
|
|
FirstConnection bool
|
|
|
|
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)
|
2016-10-29 18:59:12 +02:00
|
|
|
b.connected = make(chan struct{})
|
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 {
|
|
|
|
switch msg.Text {
|
|
|
|
case "!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)
|
|
|
|
b.i.Cmd.SendRaw("NAMES " + msg.Channel)
|
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"))
|
|
|
|
server, portstr, err := net.SplitHostPort(b.GetString("Server"))
|
2017-10-20 22:22:13 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2016-08-15 23:16:07 +02:00
|
|
|
}
|
2017-10-20 22:22:13 +02:00
|
|
|
port, err := strconv.Atoi(portstr)
|
2016-08-15 23:16:07 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-11-11 23:16:58 +01:00
|
|
|
// fix strict user handling of girc
|
2018-03-04 23:52:14 +01:00
|
|
|
user := b.GetString("Nick")
|
2017-11-11 23:16:58 +01:00
|
|
|
for !girc.IsValidUser(user) {
|
|
|
|
if len(user) == 1 {
|
|
|
|
user = "matterbridge"
|
|
|
|
break
|
|
|
|
}
|
|
|
|
user = user[1:]
|
|
|
|
}
|
|
|
|
|
2017-10-20 22:22:13 +02:00
|
|
|
i := girc.New(girc.Config{
|
|
|
|
Server: server,
|
2018-03-04 23:52:14 +01:00
|
|
|
ServerPass: b.GetString("Password"),
|
2017-10-20 22:22:13 +02:00
|
|
|
Port: port,
|
2018-03-04 23:52:14 +01:00
|
|
|
Nick: b.GetString("Nick"),
|
2017-11-11 23:16:58 +01:00
|
|
|
User: user,
|
2018-03-04 23:52:14 +01:00
|
|
|
Name: b.GetString("Nick"),
|
|
|
|
SSL: b.GetBool("UseTLS"),
|
|
|
|
TLSConfig: &tls.Config{InsecureSkipVerify: b.GetBool("SkipTLSVerify"), ServerName: server},
|
2017-10-20 22:22:13 +02:00
|
|
|
PingDelay: time.Minute,
|
|
|
|
})
|
|
|
|
|
2018-03-04 23:52:14 +01:00
|
|
|
if b.GetBool("UseSASL") {
|
|
|
|
i.Config.SASL = &girc.SASLPlain{b.GetString("NickServNick"), 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)
|
2017-10-20 22:22:13 +02:00
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
if err := i.Connect(); err != nil {
|
2018-02-27 00:33:21 +01:00
|
|
|
b.Log.Errorf("error: %s", err)
|
|
|
|
b.Log.Info("reconnecting in 30 seconds...")
|
2017-10-20 22:22:13 +02:00
|
|
|
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 23:16:07 +02:00
|
|
|
b.i = i
|
2016-10-29 18:59:12 +02:00
|
|
|
select {
|
|
|
|
case <-b.connected:
|
2018-02-27 00:33:21 +01:00
|
|
|
b.Log.Info("Connection succeeded")
|
2016-10-29 18:59:12 +02:00
|
|
|
case <-time.After(time.Second * 30):
|
|
|
|
return fmt.Errorf("connection timed out")
|
|
|
|
}
|
2017-10-20 22:22:13 +02:00
|
|
|
//i.Debug = 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 {
|
|
|
|
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
|
|
|
|
if msg.Event == config.EVENT_MSG_DELETE {
|
|
|
|
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-03-04 23:52:14 +01:00
|
|
|
if b.GetString("Charset") != "" {
|
2018-05-11 21:55:53 +02:00
|
|
|
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)
|
2018-05-11 21:55:53 +02:00
|
|
|
w.Close()
|
|
|
|
msg.Text = buf.String()
|
2017-08-30 20:59:54 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-24 23:56:35 +01:00
|
|
|
// Handle files
|
2017-11-24 22:36:19 +01:00
|
|
|
if msg.Extra != nil {
|
2018-02-03 01:11:11 +01:00
|
|
|
for _, rmsg := range helper.HandleExtra(&msg, b.General) {
|
|
|
|
b.Local <- rmsg
|
|
|
|
}
|
2017-11-24 22:36:19 +01:00
|
|
|
if len(msg.Extra["file"]) > 0 {
|
|
|
|
for _, f := range msg.Extra["file"] {
|
|
|
|
fi := f.(config.FileInfo)
|
2018-02-01 00:41:09 +01:00
|
|
|
if fi.Comment != "" {
|
2018-02-01 17:46:10 +01:00
|
|
|
msg.Text += fi.Comment + ": "
|
2018-02-01 00:41:09 +01:00
|
|
|
}
|
2017-11-24 22:36:19 +01:00
|
|
|
if fi.URL != "" {
|
|
|
|
msg.Text = fi.URL
|
2018-05-06 16:57:59 +02:00
|
|
|
if fi.Comment != "" {
|
|
|
|
msg.Text = fi.Comment + ": " + fi.URL
|
|
|
|
}
|
2017-11-24 22:36:19 +01:00
|
|
|
}
|
2017-11-24 22:55:24 +01:00
|
|
|
b.Local <- config.Message{Text: msg.Text, Username: msg.Username, Channel: msg.Channel, Event: msg.Event}
|
2017-11-24 22:36:19 +01:00
|
|
|
}
|
2017-11-24 22:55:24 +01:00
|
|
|
return "", nil
|
2017-11-24 22:36:19 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-24 23:27:13 +01:00
|
|
|
// split long messages on messageLength, to avoid clipped messages #281
|
2018-03-04 23:52:14 +01:00
|
|
|
if b.GetBool("MessageSplit") {
|
2018-03-06 20:41:34 +01:00
|
|
|
msg.Text = helper.SplitStringLength(msg.Text, b.MessageLength)
|
2017-11-24 23:27:13 +01:00
|
|
|
}
|
2016-09-29 21:21:24 +02:00
|
|
|
for _, text := range strings.Split(msg.Text, "\n") {
|
2018-03-04 23:52:14 +01:00
|
|
|
if len(text) > b.MessageLength {
|
|
|
|
text = text[:b.MessageLength-len(" <message clipped>")]
|
2018-02-08 23:28:33 +01:00
|
|
|
if r, size := utf8.DecodeLastRuneInString(text); r == utf8.RuneError {
|
|
|
|
text = text[:len(text)-size]
|
|
|
|
}
|
|
|
|
text += " <message clipped>"
|
2017-05-29 21:54:34 +02:00
|
|
|
}
|
2018-03-06 20:41:34 +01:00
|
|
|
if len(b.Local) < b.MessageQueue {
|
|
|
|
if len(b.Local) == b.MessageQueue-1 {
|
2016-11-01 22:52:28 +01:00
|
|
|
text = text + " <message clipped>"
|
|
|
|
}
|
2017-07-30 17:48:23 +02:00
|
|
|
b.Local <- config.Message{Text: text, Username: msg.Username, Channel: msg.Channel, Event: msg.Event}
|
2016-11-01 22:52:28 +01:00
|
|
|
} else {
|
2018-02-27 00:33:21 +01:00
|
|
|
b.Log.Debugf("flooding, dropping message (queue at %d)", len(b.Local))
|
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
|
|
|
}
|
|
|
|
|
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-18 21:33:37 +02:00
|
|
|
username = fmt.Sprintf("\x03%02d%s\x0F", checksum%0x10, msg.Username)
|
2018-05-11 23:02:43 +02:00
|
|
|
}
|
2017-07-30 17:48:23 +02:00
|
|
|
if msg.Event == config.EVENT_USER_ACTION {
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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()
|
|
|
|
continued := false
|
|
|
|
for len(b.names[channel]) > maxNamesPerPost {
|
2016-09-19 23:35:47 +02:00
|
|
|
b.Remote <- config.Message{Username: b.Nick, Text: b.formatnicks(b.names[channel][0:maxNamesPerPost], continued),
|
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:]
|
|
|
|
continued = true
|
|
|
|
}
|
2016-11-13 23:06:37 +01:00
|
|
|
b.Remote <- config.Message{Username: b.Nick, Text: b.formatnicks(b.names[channel], continued),
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2017-10-20 22:22:13 +02:00
|
|
|
func (b *Birc) handleNewConnection(client *girc.Client, event girc.Event) {
|
2018-02-27 00:33:21 +01:00
|
|
|
b.Log.Debug("Registering callbacks")
|
2016-08-14 21:48:51 +02:00
|
|
|
i := b.i
|
2017-10-20 22:22:13 +02: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 18:59:12 +02:00
|
|
|
// we are now fully connected
|
|
|
|
b.connected <- struct{}{}
|
2016-08-14 21:48:51 +02:00
|
|
|
}
|
|
|
|
|
2017-10-20 22:22:13 +02:00
|
|
|
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
|
|
|
|
}
|
2018-02-22 22:51:32 +01:00
|
|
|
channel := strings.ToLower(event.Params[0])
|
2017-10-20 22:22:13 +02:00
|
|
|
if event.Command == "KICK" {
|
2018-02-27 00:33:21 +01:00
|
|
|
b.Log.Infof("Got kicked from %s by %s", channel, event.Source.Name)
|
2018-03-04 23:52:14 +01:00
|
|
|
time.Sleep(time.Duration(b.GetInt("RejoinDelay")) * time.Second)
|
2017-04-08 00:42:37 +02:00
|
|
|
b.Remote <- config.Message{Username: "system", Text: "rejoin", Channel: channel, Account: b.Account, Event: config.EVENT_REJOIN_CHANNELS}
|
|
|
|
return
|
|
|
|
}
|
2017-10-20 22:22:13 +02:00
|
|
|
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)
|
2017-02-14 21:12:02 +01:00
|
|
|
b.Remote <- config.Message{Username: "system", Text: "reconnect", Channel: channel, Account: b.Account, Event: config.EVENT_FAILURE}
|
|
|
|
return
|
|
|
|
}
|
2016-11-14 22:53:06 +01:00
|
|
|
}
|
2017-10-20 22:22:13 +02:00
|
|
|
if event.Source.Name != b.Nick {
|
2018-03-06 21:34:55 +01:00
|
|
|
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)
|
2018-02-22 18:23:22 +01:00
|
|
|
msg := config.Message{Username: "system", Text: event.Source.Name + " " + strings.ToLower(event.Command) + "s", Channel: channel, Account: b.Account, Event: config.EVENT_JOIN_LEAVE}
|
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
|
2017-06-17 17:58:56 +02:00
|
|
|
return
|
|
|
|
}
|
2018-02-27 00:33:21 +01:00
|
|
|
b.Log.Debugf("handle %#v", event)
|
2016-11-14 22:53:06 +01:00
|
|
|
}
|
|
|
|
|
2017-10-20 22:22:13 +02:00
|
|
|
func (b *Birc) handleNotice(client *girc.Client, event girc.Event) {
|
2018-03-04 23:52:14 +01:00
|
|
|
if strings.Contains(event.String(), "This nickname is registered") && event.Source.Name == b.GetString("NickServNick") {
|
|
|
|
b.i.Cmd.Message(b.GetString("NickServNick"), "IDENTIFY "+b.GetString("NickServPassword"))
|
2016-10-29 18:01:16 +02:00
|
|
|
} else {
|
2017-10-20 22:22:13 +02:00
|
|
|
b.handlePrivMsg(client, event)
|
2016-08-14 21:48:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-20 22:22:13 +02:00
|
|
|
func (b *Birc) handleOther(client *girc.Client, event girc.Event) {
|
2018-03-04 23:52:14 +01:00
|
|
|
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
|
|
|
|
}
|
2017-10-20 22:22:13 +02:00
|
|
|
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())
|
2016-08-14 21:48:51 +02:00
|
|
|
}
|
|
|
|
|
2017-10-20 22:22:13 +02:00
|
|
|
func (b *Birc) handleOtherAuth(client *girc.Client, event girc.Event) {
|
2018-03-04 23:52:14 +01:00
|
|
|
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"))
|
2017-09-20 22:47:26 +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
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Birc) handlePrivMsg(client *girc.Client, event girc.Event) {
|
|
|
|
if b.skipPrivMsg(event) {
|
2016-10-29 16:35:16 +02:00
|
|
|
return
|
|
|
|
}
|
2018-01-06 22:55:03 +01:00
|
|
|
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
|
2017-12-07 22:07:45 +01:00
|
|
|
if event.IsAction() {
|
2017-07-30 17:48:23 +02:00
|
|
|
rmsg.Event = config.EVENT_USER_ACTION
|
2016-08-14 21:48:51 +02:00
|
|
|
}
|
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
|
2017-10-28 14:28:15 +02:00
|
|
|
re := regexp.MustCompile(`[[:cntrl:]](?:\d{1,2}(?:,\d{1,2})?)?`)
|
2018-02-24 23:56:35 +01:00
|
|
|
rmsg.Text = re.ReplaceAllString(rmsg.Text, "")
|
2017-07-07 23:39:38 +02:00
|
|
|
|
2018-02-24 23:56:35 +01:00
|
|
|
// start detecting the charset
|
2017-07-07 23:39:38 +02:00
|
|
|
var r io.Reader
|
2017-08-29 21:30:59 +02:00
|
|
|
var err error
|
2018-03-04 23:52:14 +01:00
|
|
|
mycharset := b.GetString("Charset")
|
2017-08-29 21:30:59 +02:00
|
|
|
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))
|
2017-08-29 21:30:59 +02:00
|
|
|
if err != nil {
|
2018-02-27 00:33:21 +01:00
|
|
|
b.Log.Infof("detection failed for rmsg.Text: %#v", rmsg.Text)
|
2017-08-29 21:30:59 +02:00
|
|
|
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
|
2017-08-29 21:30:59 +02:00
|
|
|
// if we're not sure, just pick ISO-8859-1
|
|
|
|
if result.Confidence < 80 {
|
|
|
|
mycharset = "ISO-8859-1"
|
|
|
|
}
|
2017-07-07 23:39:38 +02:00
|
|
|
}
|
2018-05-11 21:55:53 +02:00
|
|
|
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)
|
2017-07-07 23:39:38 +02:00
|
|
|
}
|
|
|
|
|
2018-02-28 22:23:29 +01:00
|
|
|
b.Log.Debugf("<= Sending message from %s on %s to gateway", event.Params[0], b.Account)
|
2017-07-30 17:48:23 +02:00
|
|
|
b.Remote <- rmsg
|
2016-08-14 21:48:51 +02:00
|
|
|
}
|
|
|
|
|
2017-10-20 22:22:13 +02: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 21:48:51 +02:00
|
|
|
if err != nil {
|
2018-02-27 00:33:21 +01:00
|
|
|
b.Log.Errorf("Invalid time stamp: %s", event.Params[3])
|
2016-08-14 21:48:51 +02:00
|
|
|
}
|
|
|
|
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))
|
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
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Birc) formatnicks(nicks []string, continued bool) string {
|
2016-09-18 19:21:15 +02:00
|
|
|
return plainformatter(nicks, b.nicksPerRow())
|
2016-08-14 21:48:51 +02:00
|
|
|
}
|