-
Notifications
You must be signed in to change notification settings - Fork 20
/
irc.go
224 lines (180 loc) · 5.36 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
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
package bruxism
import (
"errors"
"io"
"github.com/fluffle/goirc/client"
)
// IRCServiceName is the service name for the IRC service.
const IRCServiceName string = "IRC"
// IRCMessage is a Message wrapper around client.Line.
type IRCMessage client.Line
// Channel returns the channel id for this message.
func (m *IRCMessage) Channel() string {
i := client.Line(*m)
return i.Target()
}
// UserName returns the user name for this message.
func (m *IRCMessage) UserName() string {
return m.Nick
}
// UserID returns the user id for this message.
func (m *IRCMessage) UserID() string {
return m.Nick
}
// UserAvatar returns the avatar url for this message.
func (m *IRCMessage) UserAvatar() string {
return ""
}
// Message returns the message content for this message.
func (m *IRCMessage) Message() string {
return m.RawMessage()
}
// RawMessage returns the raw message content for this message.
func (m *IRCMessage) RawMessage() string {
i := client.Line(*m)
return i.Text()
}
// MessageID returns the message ID for this message.
func (m *IRCMessage) MessageID() string {
return ""
}
// Type returns the type of message.
func (m *IRCMessage) Type() MessageType {
return MessageTypeCreate
}
func (m *IRCMessage) MatchesCommand(prefix, command string) (bool, bool) {
return false, false
}
func (m *IRCMessage) ParseCommand(prefix string) (string, []string, bool) {
return "", []string{}, false
}
// IRC is a Service provider for IRC.
type IRC struct {
host string
nick string
password string
channels []string
Conn *client.Conn
messageChan chan Message
}
// NewIRC creates a new IRC service.
func NewIRC(host, nick, password string, channels []string) *IRC {
return &IRC{
host: host,
nick: nick,
password: password,
channels: channels,
messageChan: make(chan Message, 200),
}
}
func (i *IRC) onMessage(conn *client.Conn, line *client.Line) {
m := IRCMessage(*line)
i.messageChan <- &m
}
func (i *IRC) onConnect(conn *client.Conn, line *client.Line) {
for _, c := range i.channels {
conn.Join(c)
}
}
func (i *IRC) onDisconnect(conn *client.Conn, line *client.Line) {
conn.ConnectTo(i.host)
}
// Name returns the name of the service.
func (i *IRC) Name() string {
return IRCServiceName
}
// Open opens the service and returns a channel which all messages will be sent on.
func (i *IRC) Open() (<-chan Message, error) {
i.Conn = client.SimpleClient(i.nick, i.nick, i.nick)
i.Conn.Config().Version = i.nick
i.Conn.Config().QuitMessage = ""
i.Conn.HandleFunc("connected", i.onConnect)
i.Conn.HandleFunc("disconnected", i.onDisconnect)
i.Conn.HandleFunc(client.PRIVMSG, i.onMessage)
go i.Conn.ConnectTo(i.host, i.password)
return i.messageChan, nil
}
// IsMe returns whether or not a message was sent by the bot.
func (i *IRC) IsMe(message Message) bool {
return message.UserName() == i.UserName()
}
// SendMessage sends a message.
func (i *IRC) SendMessage(channel, message string) error {
i.Conn.Privmsg(channel, message)
return nil
}
// SendAction sends an action.
func (i *IRC) SendAction(channel, message string) error {
i.Conn.Action(channel, message)
return nil
}
// DeleteMessage deletes a message.
func (i *IRC) DeleteMessage(channel, messageID string) error {
return errors.New("Deleting messages not supported on IRC.")
}
// SendFile sends a file.
func (i *IRC) SendFile(channel, name string, r io.Reader) error {
return errors.New("Send file not supported.")
}
// BanUser bans a user.
func (i *IRC) BanUser(channel, userID string, duration int) error {
i.Conn.Kick(channel, userID)
return nil
}
// UnbanUser unbans a user.
func (i *IRC) UnbanUser(channel, userID string) error {
return errors.New("Unbanning users not supported on IRC.")
}
// UserName returns the bots name.
func (i *IRC) UserName() string {
return i.Conn.Me().Nick
}
// UserID returns the bots user id.
func (i *IRC) UserID() string {
return i.Conn.Me().Nick
}
// Join will join a channel.
func (i *IRC) Join(join string) error {
i.Conn.Join(join)
return nil
}
// Typing sets that the bot is typing.
func (i *IRC) Typing(channel string) error {
return errors.New("Typing not supported on IRC.")
}
// PrivateMessage will send a private message to a user.
func (i *IRC) PrivateMessage(userID, message string) error {
return i.SendMessage(userID, message)
}
// SupportsPrivateMessages returns whether the service supports private messages.
func (i *IRC) SupportsPrivateMessages() bool {
return true
}
// SupportsMultiline returns whether the service supports multiline messages.
func (i *IRC) SupportsMultiline() bool {
return false
}
// CommandPrefix returns the command prefix for the service.
func (i *IRC) CommandPrefix() string {
return "!"
}
// IsBotOwner returns whether or not a message sender was the owner of the bot.
func (i *IRC) IsBotOwner(message Message) bool {
return false
}
// IsPrivate returns whether or not a message was private.
func (i *IRC) IsPrivate(message Message) bool {
return message.UserName() == message.Channel()
}
// IsChannelOwner returns whether or not the sender of a message is the owner.
func (i *IRC) IsChannelOwner(message Message) bool {
return false
}
// IsModerator returns whether or not the sender of a message is a moderator.
func (i *IRC) IsModerator(message Message) bool {
return false
}
// ChannelCount returns the number of channels the bot is in.
func (i *IRC) ChannelCount() int {
return len(i.channels)
}