/
irc.go
70 lines (58 loc) · 1.48 KB
/
irc.go
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
package slack
import (
"crypto/tls"
"github.com/djosephsen/hal"
"github.com/thoj/go-ircevent"
)
func (a *adapter) startIRCConnection() {
con := irc.IRC(a.botname, a.botname)
con.UseTLS = true
// con.Debug = true
con.Password = a.ircPassword
con.TLSConfig = &tls.Config{ServerName: "*.irc.slack.com"}
err := con.Connect(a.ircServer())
if err != nil {
panic("failed to connect to" + err.Error())
}
con.AddCallback("001", func(e *irc.Event) {
for _, channel := range a.channels {
hal.Logger.Debugf("slack - joined channel %v", channel)
}
})
con.AddCallback("PRIVMSG", func(e *irc.Event) {
hal.Logger.Debug("slack - IRC handler received message")
message := a.newMessageFromIRC(e)
a.Receive(message)
})
a.ircConnection = con
con.Loop()
}
func (a *adapter) stopIRCConnection() {
hal.Logger.Debug("Stopping slack IRC connection")
a.ircConnection.Quit()
hal.Logger.Debug("Stopped slack IRC connection")
}
func (a *adapter) newMessageFromIRC(req *irc.Event) *hal.Message {
return &hal.Message{
User: hal.User{
ID: req.Nick,
Name: req.Nick,
},
Room: req.Arguments[0],
Text: req.Message(),
}
}
func (a *adapter) ircServer() string {
return a.team + `.irc.slack.com:6667`
}
func (a *adapter) sendIRC(res *hal.Response, strings ...string) error {
hal.Logger.Debug("slack - sending IRC response")
for _, str := range strings {
s := &slackPayload{
Channel: res.Message.Room,
Text: str,
}
a.ircConnection.Privmsg(s.Channel, s.Text)
}
return nil
}