-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.py
362 lines (331 loc) · 11 KB
/
client.py
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
import logging
import pathlib
import socket
import sys
import threading
from channel import Channel, ChannelList, User
from server import Server
from util.nick import nickeq, nicklower
from util.msg import Message
class Client:
def __init__(self, server, nick, username, realname, **conf):
self.sock = None
self.nick = None
self.server = None
self.conf = {
"port": 6667,
"autorejoin": True,
"autoconn": True,
"channels": []
}
self.conf.update(conf)
self.conf.update({
"server": server,
"nick": nick,
"username": username,
"realname": realname,
})
self.channels = ChannelList()
self.listeners = {}
self.nickmod = 0
if self.conf["autoconn"]:
threading.Thread(name="main", target=self.connect).start()
def connect(self):
"""Attempt to connect to the server. Log in if successful."""
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
self.sock.connect((self.conf["server"], self.conf["port"]))
self.server = Server(self.conf["server"], self.conf["port"])
logging.info("Connected to %s", self.server.host)
self.send("NICK", self.conf["nick"])
if "password" in self.conf:
self.send("PASS", self.conf["password"])
self.send("USER", self.conf["username"], 8, "*", \
self.conf["realname"])
threading.Thread(name="listen", target=self.listen).start()
except socket.error as e:
logging.error("Cannot connect to %s: %s", self.conf["server"], e)
self.sock.close()
self.sock = None
def send(self, *msg):
"""Send a raw message to the server."""
if not self.sock:
logging.error("Not connected to server")
return
message = " ".join(map(str, msg))
self.sock.sendall(bytes(message + "\r\n", "utf-8"))
logging.debug("(%s) %s", threading.current_thread().name, message.rstrip())
def say(self, to, msg):
"""Send a message to a user/channel."""
if not msg:
return
self.send("PRIVMSG", to, ":" + msg)
if any([to.startswith(i) for i in self.server.types]):
self.channels[to[1:]].users[self.nick].messages.append(msg)
def notice(self, to, msg):
"""Send a notice to a user/channel."""
self.send("NOTICE", to, ":" + msg)
def ctcp_say(self, to, text):
"""Send a CTCP PRIVMSG message."""
self.say(to, "\x01{0}\x01".format(text))
def ctcp_notice(self, to, text):
"""Send a CTCP NOTICE message."""
self.notice(to, "\x01{0}\x01".format(text))
def action(self, to, msg):
self.ctcp_say(to, "ACTION {0}".format(msg))
def join(self, chan):
"""Join a channel."""
self.send("JOIN", chan)
def part(self, chan, reason=None):
self.send("PART", chan, ":" + (reason or ""))
def listen(self):
"""Listen for incoming messages from the IRC server."""
if not self.sock:
logging.error("Not connected to server")
return
sock = self.sock.makefile('rb')
while True:
try:
msg = sock.readline().decode("utf-8", errors="ignore").rstrip("\r\n")
m = Message.parse(msg)
if not m:
raise socket.error
logging.debug(m.raw)
thread = threading.Thread(target=lambda: self.handle(m))
thread.start()
except socket.error:
logging.info("Disconnected from server")
self.sock.close()
self.sock = None
break
def add_listener(self, event, fn):
"""Add a function to listen for the specified event."""
if event not in self.listeners:
self.listeners[event] = []
self.listeners[event].append(fn)
def remove_listener(self, event, fn):
"""Remove a function as a listener from the specified event."""
if event not in self.listeners:
return
self.listeners[event] = [l for l in self.listeners[event] if l != fn]
def remove_listeners(self, event):
"""Remove all functions listening for the specified event."""
if event not in self.listeners:
return
self.listeners.pop(event)
def emit(self, event, *params):
"""Emit an event, and call all functions listening for it."""
if event != "raw" and self.conf["verbose"]:
logging.debug("[{0}] '{1}'".format(event, "', '".join(map(str, params))))
if event in self.listeners:
for listener in self.listeners[event]:
try:
thread = threading.Thread(target=lambda: listener(*params))
thread.start()
except TypeError as e:
logging.error("(%s) invalid number of parameters [%s]",
threading.current_thread().name, listener.__name__)
except:
type, value, lasttb = sys.exc_info()
fname = pathlib.Path(lasttb.tb_frame.f_code.co_filename).name
self.circa.say(self, channel, "\x02\x034{0}\x03\x02: {1} ({2}:{3})".format( \
type.__name__, value, fname, lasttb.tb_lineno))
def _ctcp(self, fr, to, text, type):
text = text.strip("\x01")
parts = text.split()
self.emit("ctcp", fr, to, text, type)
self.emit("ctcp." + type, fr, to, text)
if type == "privmsg":
if parts[0] == "VERSION":
self.emit("ctcp.version", fr, to)
elif len(parts) > 1 and parts[0] == "ACTION":
self.emit("action", fr, to, " ".join(parts[1:]))
elif len(parts) > 1 and parts[0] == "PING":
self.ctcp_notice(fr, "PING " + " ".join(parts[1:]))
def handle(self, msg):
self.emit("raw", msg)
c = msg.command
if c == "001":
self.nick = self.conf["nick"] + self.nickmod * "_"
self.emit("registered", msg.params[0], msg)
elif c == "004":
self.server.usermodes = set(msg.params[3])
elif c == "005":
for p in msg.params:
if "=" in p:
param, value = p.split("=")
if param == "CHANLIMIT":
for pair in value.split(","):
typ, num = pair.split(":")
self.server.chlimit[typ] = int(num)
elif param == "CHANMODES":
modes = value.split(",")
self.server.chmodes = dict(zip("abcd", map(set, modes)))
elif param == "CHANTYPES":
self.server.types = set(value)
elif param == "CHANNELLEN":
self.server.chlength = int(value)
elif param == "IDCHAN":
for pair in value.split(","):
typ, num = pair.split(":")
self.server.idlength[typ] = int(num)
elif param == "KICKLEN":
self.server.kicklength = int(value)
elif param == "NICKLEN":
self.server.nicklength = int(value)
elif param == "PREFIX":
modes, prefixes = value[1:].split(")")
self.server.prefix_mode = dict(zip(prefixes, modes))
self.server.mode_prefix = dict(zip(modes, prefixes))
self.server.chmodes["b"].update(modes)
elif param == "TARGMAX":
for pair in value.split(","):
typ, num = pair.split(":")
self.server.maxtargets[typ] = int(num) if num else 0
elif param == "TOPICLEN":
self.server.topiclength = int(value)
elif c == "433":
self.nickmod += 1
self.send("NICK", self.conf["nick"] + self.nickmod * "_")
elif c == "PING":
self.send("PONG", msg.params[0])
self.emit("ping", msg.params[0], msg)
elif c == "PONG":
self.emit("pong", msg.params[0], msg)
elif c == "NOTICE":
fr, to = msg.nick, msg.params[0]
text = msg.params[1] or ""
if text[0] == "\x01" and "\x01" in text[1:]:
self._ctcp(fr, to, text, "notice")
else:
self.emit("notice", fr, to, text, msg)
elif c == "MODE":
by = msg.nick
adding = True
if msg.params[0][0] in self.server.types:
chan = msg.params[0].lower()[1:]
params = msg.params[1:]
while len(params):
modes = params.pop(0)
for mode in modes:
if mode == '+':
adding = True
elif mode == '-':
adding = False
elif mode in self.server.mode_prefix:
user = params.pop(0)
op = "+" if adding else "-"
u = self.channels[chan].users[user].mode
try:
(u.add if adding else u.remove)(mode)
except KeyError:
pass
self.emit(op + "mode", chan, by, mode, user, msg)
elif c == "NICK":
nick = msg.params[0]
if nickeq(msg.nick, self.nick):
self.nick = nicklower(nick)
elif nickeq(msg.nick, nick):
return
chans = list(filter(lambda c: msg.nick in c, self.channels.values()))
for chan in chans:
chan.users[nick] = chan.users[msg.nick]
chan.users.pop(msg.nick)
chan.users[nick].nick = nicklower(nick)
self.emit("nick", msg.nick, nick, [c.name for c in chans], msg)
elif c == "375":
self.server.motd = msg.params[1] + "\n"
elif c == "372":
self.server.motd += msg.params[1] + "\n"
elif c == "376" or c == "422":
self.server.motd += msg.params[1] + "\n"
self.emit("motd", self.server.motd, msg)
elif c == "353":
channel = self.channels[msg.params[2][1:]]
if channel:
users = msg.params[3].strip().split()
for user in users:
nick = nicklower(user)
if user[0] in self.server.prefix_mode:
nick = nick[1:]
channel.users[nick] = User(nick,
{self.server.prefix_mode[user[0]]})
else:
channel.users[user] = User(nick)
elif c == "366":
channel = self.channels[msg.params[1][1:]]
if channel:
self.emit("names", msg.params[1], channel.users, msg)
self.send("MODE", msg.params[1])
elif c == "332":
channel = self.channels[msg.params[1][1:]]
if channel:
channel.topic = msg.params[2]
# 301, 311, 312, 313, 317, 318, 319, 330, 321, 322, 323, 333
elif c == "TOPIC":
nick = nicklower(msg.nick)
self.emit("topic", msg.params[0], msg.params[1], nick, msg)
channel = self.channels[msg.params[0][1:]]
if channel:
channel.topic = msg.params[1]
channel.topicby = nick
elif c == "324":
channel = self.channels[msg.params[1][1:]]
if channel:
channel.mode = msg.params[2]
elif c == "329":
channel = self.channels[msg.params[1][1:]]
if channel:
channel.created = int(msg.params[2])
elif c == "JOIN":
chan = msg.params[0]
if nickeq(self.nick, msg.nick):
self.channels[chan[1:]] = Channel(chan)
else:
channel = self.channels[chan[1:]]
if channel:
channel.users[msg.nick] = User(nicklower(msg.nick))
self.emit("join", chan, nicklower(msg.nick), msg)
elif c == "PART":
chan = msg.params[0]
self.emit("part", chan, nicklower(msg.nick), msg)
if nickeq(self.nick, msg.nick):
self.channels.pop(chan[1:].lower())
else:
channel = self.channels[chan[1:]]
if channel:
channel.users.pop(msg.nick)
elif c == "KICK":
chan, who, reason, *rest = msg.params
self.emit("kick", chan, who, msg.nick, reason, msg)
if nickeq(self.nick, msg.nick):
self.channels.pop(chan[1:].lower())
else:
channel = self.channels[chan[1:]]
if channel:
channel.users.pop(who)
elif c == "KILL":
nick = nicklower(msg.params[0])
channels = []
for chan in self.channels:
if nick in chan.users:
channels.append(chan.name)
chan.users.pop(nick)
self.emit("kill", nick, msg.params[1], channels, msg)
elif c == "PRIVMSG":
fr, to = msg.nick, msg.params[0]
text = " ".join(msg.params[1:])
if to[0] in self.server.types:
self.channels[to[1:]].users[fr].messages.append(text)
if text[0] == "\x01" and "\x01" in text[1:]:
self._ctcp(fr, to, text, "privmsg")
self.emit("message", fr, to, text, msg)
elif c == "INVITE":
self.emit("invite", msg.params[1], msg.nick, msg)
elif c == "QUIT":
if nickeq(self.nick, msg.nick):
return
chans = list(filter(lambda c: msg.nick in c, self.channels.values()))
for chan in chans:
chan.users.pop(msg.nick)
self.emit("quit", msg.nick, [c.name for c in chans], msg)