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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
|
package main
import (
"crypto/tls"
"flag"
"github.com/42wim/matterbridge/matterhook"
"github.com/peterhellberg/giphy"
"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, Token: b.Config.Mattermost.Token,
InsecureSkipVerify: b.Config.Mattermost.SkipTLSVerify,
BindAddress: b.Config.Mattermost.BindAddress})
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)
}
i.AddCallback("353", b.handleOther)
return i
}
func (b *Bridge) handlePrivMsg(event *irc.Event) {
msg := ""
if event.Code == "CTCP_ACTION" {
msg = event.Nick + " "
}
msg += event.Message()
b.Send("irc-"+event.Nick, msg)
}
func (b *Bridge) handleJoinPart(event *irc.Event) {
b.SendType(b.Config.IRC.Nick, "irc-"+event.Nick+" "+strings.ToLower(event.Code)+"s "+event.Message(), "join_leave")
}
func (b *Bridge) handleOther(event *irc.Event) {
switch event.Code {
case "353":
b.Send(b.Config.IRC.Nick, event.Message()+" currently on IRC")
}
}
func (b *Bridge) Send(nick string, message string) error {
return b.SendType(nick, message, "")
}
func (b *Bridge) SendType(nick string, message string, mtype string) error {
matterMessage := matterhook.OMessage{IconURL: b.Config.Mattermost.IconURL}
matterMessage.UserName = nick
matterMessage.Text = message
matterMessage.Type = mtype
err := b.m.Send(matterMessage)
if err != nil {
log.Println(err)
return err
}
return nil
}
func (b *Bridge) handleMatter() {
for {
message := b.m.Receive()
cmd := strings.Fields(message.Text)[0]
switch cmd {
case "!users":
log.Println("received !users from", message.UserName)
b.i.SendRaw("NAMES " + b.Config.IRC.Channel)
case "!gif":
message.Text = b.giphyRandom(strings.Fields(strings.Replace(message.Text, "!gif ", "", 1)))
b.Send(b.Config.IRC.Nick, message.Text)
}
texts := strings.Split(message.Text, "\n")
for _, text := range texts {
b.i.Privmsg(b.Config.IRC.Channel, message.UserName+": "+text)
}
}
}
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
}
func main() {
flagConfig := flag.String("conf", "matterbridge.conf", "config file")
flag.Parse()
NewBridge("matterbot", NewConfig(*flagConfig))
select {}
}
|