-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathendpoint_irc.go
134 lines (115 loc) · 2.71 KB
/
endpoint_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
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
125
126
127
128
129
130
131
132
133
134
package main
import (
"encoding/json"
"log"
"time"
irc "github.com/fluffle/goirc/client"
)
func init() {
AddEndpointDriver("irc", func(options *json.RawMessage) Endpoint {
return newEndpointIRC(options)
})
}
type EndpointIRC struct {
Config EndpointIRCConfig
conn *irc.Conn
handler func(text string, source User, channel string, response MessageTarget)
}
type EndpointIRCConfig struct {
Nick string
Server string
Channels []string
}
type UserIRC struct {
ei *EndpointIRC
nick string
}
type MessageTargetIRC struct {
ei *EndpointIRC
target string
public bool
}
func newEndpointIRC(options *json.RawMessage) *EndpointIRC {
e := &EndpointIRC{}
json.Unmarshal(*options, &e.Config)
c := irc.NewConfig(e.Config.Nick)
c.Server = e.Config.Server
e.conn = irc.Client(c)
e.conn.EnableStateTracking()
e.conn.HandleFunc(irc.CONNECTED, e.connect)
e.conn.HandleFunc(irc.PRIVMSG, e.message)
e.conn.HandleFunc(irc.DISCONNECTED,
func(conn *irc.Conn, line *irc.Line) {
time.AfterFunc(time.Second*30, func() {
e.conn.Connect()
})
})
return e
}
func (ei *EndpointIRC) Run() {
err := ei.conn.Connect()
if err != nil {
log.Printf("Error while connecting IRC endpoint: %s", err)
}
}
func (ei *EndpointIRC) connect(c *irc.Conn, l *irc.Line) {
for _, channel := range ei.Config.Channels {
c.Join(channel)
}
}
func (ei *EndpointIRC) message(c *irc.Conn, l *irc.Line) {
var messageTarget MessageTarget
if l.Public() {
messageTarget = ei.GetChannel(l.Target())
} else {
messageTarget = ei.GetUser(l.Target())
}
ei.handler(l.Text(), ei.GetUser(l.Nick), l.Target(), messageTarget)
}
func (ei *EndpointIRC) GetUser(nick string) User {
return &UserIRC{
ei: ei,
nick: nick,
}
}
func (ei *EndpointIRC) GetChannel(channel string) MessageTarget {
return &MessageTargetIRC{
ei: ei,
target: channel,
public: true,
}
}
func (ei *EndpointIRC) HandleMessage(handler func(text string, source User, channel string, response MessageTarget)) {
ei.handler = handler
}
func (u *UserIRC) SendMessage(format string, args ...interface{}) {
u.ei.conn.Privmsgf(u.nick, format, args...)
}
func (u *UserIRC) HasRights() bool {
user := u.ei.conn.StateTracker().GetNick(u.nick)
for channel, privs := range user.Channels {
validChannel := false
for _, mychannel := range u.ei.Config.Channels {
if mychannel == channel {
validChannel = true
break
}
}
if !validChannel {
continue
}
if privs.Op {
return true
}
}
return false
}
func (u *UserIRC) IsPublic() bool {
return false
}
func (mt *MessageTargetIRC) SendMessage(format string, args ...interface{}) {
mt.ei.conn.Privmsgf(mt.target, format, args...)
}
func (mt *MessageTargetIRC) IsPublic() bool {
return mt.public
}