2015-10-23 20:34:37 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/tls"
|
2015-12-18 19:54:28 +00:00
|
|
|
"flag"
|
2015-10-23 20:34:37 +00:00
|
|
|
"github.com/42wim/matterbridge/matterhook"
|
2015-10-27 23:04:57 +00:00
|
|
|
"github.com/peterhellberg/giphy"
|
2015-10-23 20:34:37 +00:00
|
|
|
"github.com/thoj/go-ircevent"
|
|
|
|
"log"
|
|
|
|
"strconv"
|
2015-10-24 14:39:01 +00:00
|
|
|
"strings"
|
2015-10-23 20:34:37 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Bridge struct {
|
2016-03-21 18:28:16 +00:00
|
|
|
i *irc.Connection
|
|
|
|
m *matterhook.Client
|
|
|
|
cmap map[string]string
|
|
|
|
ircNick string
|
|
|
|
ircNickPass string
|
2015-10-23 20:34:37 +00:00
|
|
|
*Config
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewBridge(name string, config *Config) *Bridge {
|
|
|
|
b := &Bridge{}
|
|
|
|
b.Config = config
|
2015-12-19 15:55:49 +00:00
|
|
|
b.cmap = make(map[string]string)
|
2016-03-21 18:28:16 +00:00
|
|
|
b.ircNick = b.Config.IRC.Nick
|
2015-12-19 15:55:49 +00:00
|
|
|
if len(b.Config.Token) > 0 {
|
|
|
|
for _, val := range b.Config.Token {
|
|
|
|
b.cmap[val.IRCChannel] = val.MMChannel
|
|
|
|
}
|
|
|
|
}
|
2015-10-24 16:01:15 +00:00
|
|
|
b.m = matterhook.New(b.Config.Mattermost.URL,
|
2015-10-24 23:00:19 +00:00
|
|
|
matterhook.Config{Port: b.Config.Mattermost.Port, Token: b.Config.Mattermost.Token,
|
2015-12-12 20:26:53 +00:00
|
|
|
InsecureSkipVerify: b.Config.Mattermost.SkipTLSVerify,
|
|
|
|
BindAddress: b.Config.Mattermost.BindAddress})
|
2015-10-23 20:34:37 +00:00
|
|
|
b.i = b.createIRC(name)
|
|
|
|
go b.handleMatter()
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Bridge) createIRC(name string) *irc.Connection {
|
|
|
|
i := irc.IRC(b.Config.IRC.Nick, b.Config.IRC.Nick)
|
|
|
|
i.UseTLS = b.Config.IRC.UseTLS
|
|
|
|
i.TLSConfig = &tls.Config{InsecureSkipVerify: b.Config.IRC.SkipTLSVerify}
|
2016-01-27 19:09:06 +00:00
|
|
|
if b.Config.IRC.Password != "" {
|
|
|
|
i.Password = b.Config.IRC.Password
|
|
|
|
}
|
2016-03-21 13:24:25 +00:00
|
|
|
i.AddCallback("*", b.handleOther)
|
2015-10-23 20:34:37 +00:00
|
|
|
i.Connect(b.Config.IRC.Server + ":" + strconv.Itoa(b.Config.IRC.Port))
|
2016-03-21 13:24:25 +00:00
|
|
|
return i
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Bridge) handleNewConnection(event *irc.Event) {
|
2016-03-21 18:28:16 +00:00
|
|
|
b.ircNick = event.Arguments[0]
|
2016-03-21 13:24:25 +00:00
|
|
|
b.setupChannels()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Bridge) setupChannels() {
|
|
|
|
i := b.i
|
2016-03-21 18:28:16 +00:00
|
|
|
log.Println("Joining", b.Config.IRC.Channel, "as", b.ircNick)
|
2015-10-23 20:34:37 +00:00
|
|
|
i.Join(b.Config.IRC.Channel)
|
2015-12-19 15:55:49 +00:00
|
|
|
for _, val := range b.Config.Token {
|
2016-03-21 18:28:16 +00:00
|
|
|
log.Println("Joining", val.IRCChannel, "as", b.ircNick)
|
2015-12-19 15:55:49 +00:00
|
|
|
i.Join(val.IRCChannel)
|
|
|
|
}
|
2015-10-23 20:34:37 +00:00
|
|
|
i.AddCallback("PRIVMSG", b.handlePrivMsg)
|
2015-10-24 14:39:01 +00:00
|
|
|
i.AddCallback("CTCP_ACTION", b.handlePrivMsg)
|
|
|
|
if b.Config.Mattermost.ShowJoinPart {
|
|
|
|
i.AddCallback("JOIN", b.handleJoinPart)
|
|
|
|
i.AddCallback("PART", b.handleJoinPart)
|
|
|
|
}
|
2015-10-23 20:34:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Bridge) handlePrivMsg(event *irc.Event) {
|
2015-10-24 16:44:45 +00:00
|
|
|
msg := ""
|
2015-10-24 14:39:01 +00:00
|
|
|
if event.Code == "CTCP_ACTION" {
|
2015-10-24 16:44:45 +00:00
|
|
|
msg = event.Nick + " "
|
2015-10-24 14:39:01 +00:00
|
|
|
}
|
2015-10-24 16:44:45 +00:00
|
|
|
msg += event.Message()
|
2015-12-19 15:55:49 +00:00
|
|
|
b.Send("irc-"+event.Nick, msg, b.getMMChannel(event.Arguments[0]))
|
2015-10-23 20:34:37 +00:00
|
|
|
}
|
|
|
|
|
2015-10-24 14:39:01 +00:00
|
|
|
func (b *Bridge) handleJoinPart(event *irc.Event) {
|
2016-03-21 18:28:16 +00:00
|
|
|
b.Send(b.ircNick, "irc-"+event.Nick+" "+strings.ToLower(event.Code)+"s "+event.Message(), b.getMMChannel(event.Arguments[0]))
|
|
|
|
//b.SendType(b.ircNick, "irc-"+event.Nick+" "+strings.ToLower(event.Code)+"s "+event.Message(), b.getMMChannel(event.Arguments[0]), "join_leave")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Bridge) handleNotice(event *irc.Event) {
|
|
|
|
if (strings.Contains(event.Message(), "This nickname is registered")) {
|
|
|
|
b.i.Privmsg(b.Config.IRC.NickServNick, "IDENTIFY " + b.Config.IRC.NickServPassword)
|
|
|
|
}
|
2015-10-24 14:39:01 +00:00
|
|
|
}
|
|
|
|
|
2016-03-18 19:12:58 +00:00
|
|
|
func tableformatter (nicks_s string, nicksPerRow int) string {
|
|
|
|
nicks := strings.Split(nicks_s, " ")
|
|
|
|
result := "|IRC users"
|
|
|
|
if nicksPerRow < 1 {
|
|
|
|
nicksPerRow = 4
|
|
|
|
}
|
|
|
|
for i := 0; i < 2; i++ {
|
2016-03-18 22:13:17 +00:00
|
|
|
for j := 1; j <= nicksPerRow && j <= len(nicks); j++ {
|
2016-03-18 19:12:58 +00:00
|
|
|
if i == 0 {
|
|
|
|
result += "|"
|
|
|
|
} else {
|
|
|
|
result += ":-|"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
result += "\r\n|"
|
|
|
|
}
|
|
|
|
result += nicks[0] + "|"
|
|
|
|
for i := 1; i < len(nicks); i++ {
|
|
|
|
if i % nicksPerRow == 0 {
|
|
|
|
result += "\r\n|" + nicks[i] + "|"
|
|
|
|
} else {
|
|
|
|
result += nicks[i] + "|"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
func plainformatter (nicks string, nicksPerRow int) string {
|
|
|
|
return nicks + " currently on IRC"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Bridge) formatnicks (nicks string) string {
|
2016-03-18 19:54:14 +00:00
|
|
|
switch (b.Config.Mattermost.NickFormatter) {
|
2016-03-18 19:12:58 +00:00
|
|
|
case "table":
|
2016-03-18 19:54:14 +00:00
|
|
|
return tableformatter(nicks, b.Config.Mattermost.NicksPerRow)
|
2016-03-18 19:12:58 +00:00
|
|
|
default:
|
2016-03-18 19:54:14 +00:00
|
|
|
return plainformatter(nicks, b.Config.Mattermost.NicksPerRow)
|
2016-03-18 19:12:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-24 15:25:18 +00:00
|
|
|
func (b *Bridge) handleOther(event *irc.Event) {
|
|
|
|
switch event.Code {
|
2016-03-21 13:24:25 +00:00
|
|
|
case "001":
|
|
|
|
b.handleNewConnection(event)
|
2015-10-24 15:25:18 +00:00
|
|
|
case "353":
|
2015-12-19 15:55:49 +00:00
|
|
|
log.Println("handleOther", b.getMMChannel(event.Arguments[0]))
|
2016-03-21 18:28:16 +00:00
|
|
|
b.Send(b.ircNick, b.formatnicks(event.Message()), b.getMMChannel(event.Arguments[0]))
|
|
|
|
case "NOTICE":
|
|
|
|
b.handleNotice(event)
|
2016-03-18 19:12:58 +00:00
|
|
|
default:
|
2016-03-21 18:28:16 +00:00
|
|
|
log.Printf("UNKNOWN EVENT: %+v\n", event);
|
|
|
|
return
|
2015-10-24 15:25:18 +00:00
|
|
|
}
|
2016-03-21 18:28:16 +00:00
|
|
|
log.Printf("%+v\n", event);
|
2015-10-24 16:44:45 +00:00
|
|
|
}
|
|
|
|
|
2015-12-19 14:55:07 +00:00
|
|
|
func (b *Bridge) Send(nick string, message string, channel string) error {
|
|
|
|
return b.SendType(nick, message, channel, "")
|
2015-11-28 23:28:10 +00:00
|
|
|
}
|
|
|
|
|
2016-03-18 22:03:15 +00:00
|
|
|
func IsMarkup(message string) bool {
|
|
|
|
switch (message[0]) {
|
|
|
|
case '|': fallthrough
|
|
|
|
case '#': fallthrough
|
|
|
|
case '_': fallthrough
|
|
|
|
case '*': fallthrough
|
|
|
|
case '~': fallthrough
|
|
|
|
case '-': fallthrough
|
|
|
|
case ':': fallthrough
|
|
|
|
case '>': fallthrough
|
|
|
|
case '=': return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2015-12-19 14:55:07 +00:00
|
|
|
func (b *Bridge) SendType(nick string, message string, channel string, mtype string) error {
|
2015-10-24 16:44:45 +00:00
|
|
|
matterMessage := matterhook.OMessage{IconURL: b.Config.Mattermost.IconURL}
|
2015-12-19 14:55:07 +00:00
|
|
|
matterMessage.Channel = channel
|
2015-10-24 16:44:45 +00:00
|
|
|
matterMessage.UserName = nick
|
2015-11-28 23:28:10 +00:00
|
|
|
matterMessage.Type = mtype
|
2016-03-18 22:03:15 +00:00
|
|
|
if b.Config.Mattermost.PrefixMessagesWithNick {
|
|
|
|
if IsMarkup(message) {
|
|
|
|
matterMessage.Text = nick + ":\n\n" + message
|
|
|
|
} else {
|
|
|
|
matterMessage.Text = nick + ": " + message
|
|
|
|
}
|
2016-03-18 10:09:29 +00:00
|
|
|
} else {
|
|
|
|
matterMessage.Text = message
|
|
|
|
}
|
2015-10-24 23:00:19 +00:00
|
|
|
err := b.m.Send(matterMessage)
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
return err
|
|
|
|
}
|
2015-10-24 16:44:45 +00:00
|
|
|
return nil
|
2015-10-24 15:25:18 +00:00
|
|
|
}
|
|
|
|
|
2015-10-23 20:34:37 +00:00
|
|
|
func (b *Bridge) handleMatter() {
|
2016-02-18 20:45:29 +00:00
|
|
|
var username string
|
2015-10-23 20:34:37 +00:00
|
|
|
for {
|
|
|
|
message := b.m.Receive()
|
2016-02-18 20:45:29 +00:00
|
|
|
username = message.UserName + ": "
|
|
|
|
if b.Config.IRC.UseSlackCircumfix {
|
|
|
|
username = "<" + message.UserName + "> "
|
|
|
|
}
|
2015-10-27 23:04:57 +00:00
|
|
|
cmd := strings.Fields(message.Text)[0]
|
|
|
|
switch cmd {
|
2015-10-24 15:25:18 +00:00
|
|
|
case "!users":
|
|
|
|
log.Println("received !users from", message.UserName)
|
2015-12-19 15:55:49 +00:00
|
|
|
b.i.SendRaw("NAMES " + b.getIRCChannel(message.Token))
|
2016-03-18 19:12:58 +00:00
|
|
|
return
|
2015-10-27 23:04:57 +00:00
|
|
|
case "!gif":
|
|
|
|
message.Text = b.giphyRandom(strings.Fields(strings.Replace(message.Text, "!gif ", "", 1)))
|
2016-03-21 18:28:16 +00:00
|
|
|
b.Send(b.ircNick, message.Text, b.getIRCChannel(message.Token))
|
2016-03-18 19:12:58 +00:00
|
|
|
return
|
2015-10-24 15:25:18 +00:00
|
|
|
}
|
2015-10-27 10:25:21 +00:00
|
|
|
texts := strings.Split(message.Text, "\n")
|
|
|
|
for _, text := range texts {
|
2016-02-18 20:45:29 +00:00
|
|
|
b.i.Privmsg(b.getIRCChannel(message.Token), username+text)
|
2015-10-27 10:25:21 +00:00
|
|
|
}
|
2015-10-23 20:34:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-27 23:04:57 +00:00
|
|
|
func (b *Bridge) giphyRandom(query []string) string {
|
|
|
|
g := giphy.DefaultClient
|
|
|
|
if b.Config.General.GiphyAPIKey != "" {
|
|
|
|
g.APIKey = b.Config.General.GiphyAPIKey
|
|
|
|
}
|
|
|
|
res, err := g.Random(query)
|
|
|
|
if err != nil {
|
|
|
|
return "error"
|
|
|
|
}
|
|
|
|
return res.Data.FixedHeightDownsampledURL
|
|
|
|
}
|
|
|
|
|
2015-12-19 15:55:49 +00:00
|
|
|
func (b *Bridge) getMMChannel(ircChannel string) string {
|
|
|
|
mmchannel, ok := b.cmap[ircChannel]
|
|
|
|
if !ok {
|
|
|
|
mmchannel = b.Config.Mattermost.Channel
|
|
|
|
}
|
|
|
|
return mmchannel
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Bridge) getIRCChannel(token string) string {
|
|
|
|
ircchannel := b.Config.IRC.Channel
|
|
|
|
_, ok := b.Config.Token[token]
|
|
|
|
if ok {
|
|
|
|
ircchannel = b.Config.Token[token].IRCChannel
|
|
|
|
}
|
|
|
|
return ircchannel
|
|
|
|
}
|
|
|
|
|
2015-10-23 20:34:37 +00:00
|
|
|
func main() {
|
2015-12-18 19:54:28 +00:00
|
|
|
flagConfig := flag.String("conf", "matterbridge.conf", "config file")
|
|
|
|
flag.Parse()
|
|
|
|
NewBridge("matterbot", NewConfig(*flagConfig))
|
2015-10-23 20:34:37 +00:00
|
|
|
select {}
|
|
|
|
}
|