summaryrefslogtreecommitdiffstats
path: root/matterbridge.go
blob: d6ec67e100dddd94ea3433a0f6451fa85a21027b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package main

import (
	"crypto/tls"
	"github.com/42wim/matterbridge/matterhook"
	"github.com/thoj/go-ircevent"
	"log"
	"strconv"
	"strings"
	"time"
)

type Bridge struct {
	i *irc.Connection
	m *matterhook.Client
	*Config
}

func NewBridge(name string, config *Config) *Bridge {
	b := &Bridge{}
	b.Config = config
	b.m = matterhook.New(b.Config.Mattermost.URL, matterhook.Config{Port: b.Config.Mattermost.Port})
	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)
	i.AddCallback("CTCP_ACTION", b.handlePrivMsg)
	if b.Config.Mattermost.ShowJoinPart {
		i.AddCallback("JOIN", b.handleJoinPart)
		i.AddCallback("PART", b.handleJoinPart)
	}
	return i
}

func (b *Bridge) handlePrivMsg(event *irc.Event) {
	matterMessage := matterhook.OMessage{}
	if event.Code == "CTCP_ACTION" {
		matterMessage.Text = event.Nick + " "
	}
	matterMessage.Text += event.Message()
	matterMessage.UserName = "irc-" + event.Nick
	b.m.Send(matterMessage)
}

func (b *Bridge) handleJoinPart(event *irc.Event) {
	matterMessage := matterhook.OMessage{}
	matterMessage.Text = "irc-" + event.Nick + " " + strings.ToLower(event.Code) + "s " + event.Message()
	matterMessage.UserName = b.Config.IRC.Nick
	b.m.Send(matterMessage)
}

func (b *Bridge) handleMatter() {
	for {
		message := b.m.Receive()
		b.i.Privmsg(b.Config.IRC.Channel, message.UserName+": "+message.Text)
	}
}

func main() {
	NewBridge("matterbot", NewConfig("matterbridge.conf"))
	select {}
}