2015-10-23 20:34:37 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/tls"
|
|
|
|
"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
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Bridge struct {
|
|
|
|
i *irc.Connection
|
|
|
|
m *matterhook.Client
|
|
|
|
*Config
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewBridge(name string, config *Config) *Bridge {
|
|
|
|
b := &Bridge{}
|
|
|
|
b.Config = config
|
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}
|
|
|
|
i.Connect(b.Config.IRC.Server + ":" + strconv.Itoa(b.Config.IRC.Port))
|
|
|
|
time.Sleep(time.Second)
|
|
|
|
log.Println("Joining", b.Config.IRC.Channel, "as", b.Config.IRC.Nick)
|
|
|
|
i.Join(b.Config.IRC.Channel)
|
|
|
|
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-24 15:25:18 +00:00
|
|
|
i.AddCallback("353", b.handleOther)
|
2015-10-23 20:34:37 +00:00
|
|
|
return i
|
|
|
|
}
|
|
|
|
|
|
|
|
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()
|
|
|
|
b.Send("irc-"+event.Nick, msg)
|
2015-10-23 20:34:37 +00:00
|
|
|
}
|
|
|
|
|
2015-10-24 14:39:01 +00:00
|
|
|
func (b *Bridge) handleJoinPart(event *irc.Event) {
|
2015-11-28 23:28:10 +00:00
|
|
|
b.SendType(b.Config.IRC.Nick, "irc-"+event.Nick+" "+strings.ToLower(event.Code)+"s "+event.Message(), "join_leave")
|
2015-10-24 14:39:01 +00:00
|
|
|
}
|
|
|
|
|
2015-10-24 15:25:18 +00:00
|
|
|
func (b *Bridge) handleOther(event *irc.Event) {
|
|
|
|
switch event.Code {
|
|
|
|
case "353":
|
2015-10-24 16:44:45 +00:00
|
|
|
b.Send(b.Config.IRC.Nick, event.Message()+" currently on IRC")
|
2015-10-24 15:25:18 +00:00
|
|
|
}
|
2015-10-24 16:44:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Bridge) Send(nick string, message string) error {
|
2015-11-28 23:28:10 +00:00
|
|
|
return b.SendType(nick, message, "")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Bridge) SendType(nick string, message string, mtype string) error {
|
2015-10-24 16:44:45 +00:00
|
|
|
matterMessage := matterhook.OMessage{IconURL: b.Config.Mattermost.IconURL}
|
|
|
|
matterMessage.UserName = nick
|
|
|
|
matterMessage.Text = message
|
2015-11-28 23:28:10 +00:00
|
|
|
matterMessage.Type = mtype
|
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() {
|
|
|
|
for {
|
|
|
|
message := b.m.Receive()
|
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)
|
|
|
|
b.i.SendRaw("NAMES " + b.Config.IRC.Channel)
|
2015-10-27 23:04:57 +00:00
|
|
|
case "!gif":
|
|
|
|
message.Text = b.giphyRandom(strings.Fields(strings.Replace(message.Text, "!gif ", "", 1)))
|
2015-10-27 23:16:42 +00:00
|
|
|
b.Send(b.Config.IRC.Nick, message.Text)
|
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 {
|
|
|
|
b.i.Privmsg(b.Config.IRC.Channel, message.UserName+": "+text)
|
|
|
|
}
|
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-10-23 20:34:37 +00:00
|
|
|
func main() {
|
|
|
|
NewBridge("matterbot", NewConfig("matterbridge.conf"))
|
|
|
|
select {}
|
|
|
|
}
|