This repository has been archived by the owner on Apr 24, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
hitbox_irc_server.py
344 lines (314 loc) · 14.2 KB
/
hitbox_irc_server.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
# vim: sts=4:sw=4:et:tw=80:nosta
import asyncio, config, json, logging
import hitbox_get_user_token
from hitbox_irc_socket import HitboxClient
class IRCServerProtocol(asyncio.Protocol):
"""Handles incoming IRC connections and creates a Hitbox chat connection
for each channel. Each connection creates a new instance of this class, so
you may allow multiple clients to connect. However, keep in mind that there
is a limit to the number of connections allowed to Hitbox chat per IP."""
def __init__(self):
"""Creates a new IRC server."""
asyncio.Protocol.__init__(self)
self._log = logging.getLogger("irc")
self._transport = None
self._nick = None
self._pass = None
self._loggedin = False
self._logintoken = None
self._channels = {}
def connection_made(self, transport):
"""Called by Protocol whenever a new connection is made to the IRC
server.
:transport: The transport to read and write from
"""
peername = transport.get_extra_info("peername")
self._log.info("Connection from {}".format(peername))
self._transport = transport
def data_received(self, data):
"""Called by Protocol whenever new data is available for the currrent
connection. The purpose of this class is prety much to parse a command,
determine whether to call it synchronously or asynchronously, then call
the appropriate function.
:data: The data received from the client
"""
msg = data.decode("UTF-8").split("\n")[:-1]
for line in msg:
# Some IRC clients also send \r with newlines - if this is the case,
# we need to remove it. For HexChat, this is the case, for mIRC this
# is not the case. (see #3)
line = line.strip() #some IRC clients also send \r - remove this
if line.split(" ")[0].strip().lower() != "pass":
self._log.info("<< {}".format(line))
else:
self._log.info("<< PASS ***")
tok = line.split(" ")
cmd = tok[0].strip().lower()
# We call the PASS, NICK, and USER commands synchronously to avoid
# having to lock and wait for subsequent commands to finish. This
# also allows us to respond if, for instance, the PASS command was
# not sent when the NICK and USER commands have been.
if cmd in ["pass", "nick", "user"]: #Call these in sync
func = getattr(self, "on_{}".format(cmd), None)
if func != None:
self._log.debug("Calling on_{} (synchronously)".format(cmd))
func(tok[1:])
else:
func = getattr(self, "on_{}".format(cmd), None)
if func != None:
self._log.debug("Calling on_{}".format(cmd))
asyncio.async(func(tok[1:]))
else:
self._log.debug("Unknown command {}({})".format(cmd, tok[1:]))
def on_pass(self, tok):
"""Called by data_received in response to a PASS command.
:tok: An array of tokens parsed from the command
"""
self._pass = tok[0]
self._log.debug("Pass set to ***")
def on_nick(self, tok):
"""Called by data_received in response to a NICK command.
:tok: An array of tokens parsed from the command
"""
self._nick = tok[0].strip()
self._log.debug("Nick set to {}".format(self._nick))
def on_user(self, tok):
"""Called by data_received in response to a USER command. Will block if
the nickname has not been set yet. This command ignores all of the
input past the command but handles all of the registration logic.
:tok: An array of tokens parsed from the command
"""
if self._loggedin == False:
if self._pass != None and self._nick != None:
self._log.debug("Logging in user {}".format(self._nick))
self._logintoken = hitbox_get_user_token \
.obtain_token(self._nick, self._pass)
self.authenticate()
elif self._nick == None:
self._log.debug("USER before NICK, ignoring")
return
elif self._pass == None:
self._log.debug("USER before PASS, sending error")
text = "464 {} :No password given. Closing connection" \
.format(self._nick)
asyncio.async(self.send(text))
asyncio.async(self.disconnect())
else:
self._log.debug(
"User {} attempted reregistration".format(self._nick))
asyncio.async(
self.send("462 {} :You have already registered." \
.format(self._nick)))
@asyncio.coroutine
def on_join(self, tok):
"""Called by data_received in response to a JOIN command. This command
handles the creation of a new Hitbox WS object, and sends it off to the
handle_socket function to handle incoming messages.
:tok: An array of tokens parsed from the command
"""
if not self._loggedin:
self._log.debug("JOIN before registration, ignoring")
else:
channel = tok[0].lstrip("#").lower()
if channel in self._channels:
self._log.debug("JOIN called but already in #{}"
.format(channel))
return
self._log.debug("Joining {}".format(channel))
self._channels[channel] = HitboxClient(channel, self._nick,
self._logintoken)
asyncio.async(self._channels[channel].connect())
yield from self.handle_socket(channel)
@asyncio.coroutine
def on_part(self, tok):
"""Called by data_received in response to a PART command. This command
handles the deletion of a specific Hitbox WS object.
:tok: An array of tokens parsed from the command
"""
if self._loggedin:
c = tok[0].lstrip("#").lower()
self._log.debug(tok[0])
self._log.debug(tok[0].lstrip("#").lower())
yield from self._channels[c].close_connection()
self._channels[c] = None
self._log.debug("Connection to #{} closed." \
.format(c))
@asyncio.coroutine
def on_names(self, tok):
"""Called by data_received in response to a NAMES command. This command
returns a full list of the users on a channel in RFC 1459 format.
:tok: An array of tokens parsed from the command
"""
if self._loggedin:
c = tok[0].lstrip("#").lower()
self._log.debug("Retrieving nick list for {}".format(c))
#yield from self._channels[c].signalNames()
#yield from self._channels[c].userList()
#TODO: unstub this when i fix the updater implementation
@asyncio.coroutine
def on_quit(self, tok):
"""Called by data_received in response to a QUIT command. This command
handles the deletion of all associated Hitbox WS objects.
:tok: An array of tokens parsed from the command
"""
if self._loggedin:
for k, c in self._channels.items():
try:
yield from c.close_connection()
self._channels[k] = None
self._log.debug("Connection to #{} closed." \
.format(k))
except AttributeError:
self._log.debug("All connections closed due to disconnect.")
@asyncio.coroutine
def on_privmsg(self, tok):
"""Called by data_received in response to a PRIVMSG command. This
command handles sending messages to either another user or a chhannel.
:tok: An array of tokens parsed from the command
"""
if self._loggedin:
if tok[0][0] == "#":
c = tok[0].lstrip("#").lower()
t = "".join(tok[1:])[1:]
yield from self._channels[c].sendMessage(t)
@asyncio.coroutine
def handle_socket(self, channel):
"""This command handles incoming messages from the Hitbox WS object.
When the user parts the channel, the client object is set to None,
so the while loop can break out. Individual messages are handed off to
a handle_*** command.
:channel: Channel name to handle incoming messages for
"""
self._log.debug("Socket handler for {} established." \
.format(channel))
while self._channels[channel] != None:
msg = yield from self._channels[channel].getNextMessage()
self._log.debug("incoming message from {}: {}" \
.format(channel, msg))
j = json.loads(msg)
#yes, this code is correct
cmd = json.loads(j["args"][0])["method"]
func = getattr(self, "handle_{}".format(cmd), None)
if func != None:
self._log.debug("Calling on_{}".format(cmd))
asyncio.async(func(json.loads(j["args"][0])))
else:
self._log.warning("Unknown HB command {}({})".format(cmd, j))
@asyncio.coroutine
def handle_loginMsg(self, json):
"""This command handles incoming join messages. This is sent by the
server in response to a JOIN command issued by the client.
:json: Parsed JSON.
"""
yield from self.sendn("JOIN :#{}".format(json["params"]["channel"]))
@asyncio.coroutine
def handle_chatMsg(self, json):
"""This command handles incoming chat messages. This is sent by the
server either because of buffered text on a JOIN, or because someone
actually sent a message. If it was due to the first,
json["params"]["buffer"] will be set to true.
:json: Parsed JSON.
"""
if json["params"]["name"] != self._nick:
yield from self.sendn("PRIVMSG #{} :{}" \
.format(json["params"]["channel"], json["params"]["text"]),
nick=json["params"]["name"])
@asyncio.coroutine
def handle_userList(self, json):
"""This command handles incoming userlist messages. This is sent by
the server if a NAMES request happened.
:json: Parsed JSON.
"""
@asyncio.coroutine
def iterate_list(nicklist, modechar):
for l in nicklist:
yield from self.send("353 {} = #{} :{}{}".format(
self._nick,
json["params"]["channel"],
modechar,
l))
self._log.debug("Calculating NAMES result...")
ownerlist = list(set(json["params"]["data"]["admin"])
.intersection([self._nick]))
adminlist = list(set(json["params"]["data"]["admin"]) - set(ownerlist))
modlist = json["params"]["data"]["user"]
fullsublist = json["params"]["data"]["isSubscriber"]
sublist = list(set(fullsublist) - set(adminlist) - set(modlist))
fullreglist = json["params"]["data"]["anon"]
reglist = list(set(fullreglist) - set(fullsublist))
for l in [adminlist, modlist, sublist, reglist]:
self._log.debug(repr(l))
for l in [[ownerlist, "~"], [adminlist, "&"], [modlist, "@"],
[sublist, "+"], [reglist, ""]]:
yield from iterate_list(l[0], l[1])
def authenticate(self):
"""Check the authentication result. If OK, send the welcome message.
If it fails, disconnect the user."""
if self._logintoken == None:
text = ("464 {} :Invalid password given. Closing connection") \
.format(self._nick)
asyncio.async(self.send(text))
asyncio.async(self.disconnect())
else:
self._loggedin = True
asyncio.async(self.welcome())
@asyncio.coroutine
def welcome(self):
"""Called after a successful registration to alert the clent that they
have been logged in."""
yield from self.send(
("001 {} :Welcome to the IRC Relay Network {}! {}@hitbox_irc_proxy")
.format(self._nick, self._nick, self._nick))
yield from self.send(
("002 {} :Your host is hitbox_irc_proxy, running v2.0")
.format(self._nick))
yield from self.send(
("003 {} :This server was created 5/6 3:40 PM")
.format(self._nick))
yield from self.send(
("005 {} PREFIX=(qaohv)~&@%+ CHANMODES=fm " +
":are supported by this server")
.format(self._nick))
@asyncio.coroutine
def send(self, data):
"""Sends the data to the client after prepending the server ID."""
b = (":hitbox_irc_proxy " + data + "\n").encode("UTF-8")
self._log.info(">> {}".format(b.decode("UTF-8").strip()))
self._transport.write(b)
@asyncio.coroutine
def sendn(self, data, nick=None):
"""Sends the data to the client after prepending the nick info."""
if nick == None:
nick = self._nick
b = (":{}!{}@hitbox_irc_proxy " \
.format(nick, nick) + data + "\n").encode("UTF-8")
self._log.info(">> {}".format(b.decode("UTF-8").strip()))
self._transport.write(b)
@asyncio.coroutine
def disconnect(self):
"""Disconnects the client gracefully."""
self._transport.close()
if __name__ == "__main__":
logs = [logging.getLogger(x) for x in ["irc", "asyncio", "main", "token",
"ws"]]
ch = logging.StreamHandler()
ch.setLevel(config.logLevel)
formatter = logging.Formatter(config.logFormat)
ch.setFormatter(formatter)
for x in logs:
x.setLevel(config.logLevel)
x.addHandler(ch)
loop = asyncio.get_event_loop()
coro = loop.create_server(IRCServerProtocol, port=7778)
server = loop.run_until_complete(coro)
thislog = logging.getLogger("main")
thislog.info("Serving requests on {}" \
.format(server.sockets[0].getsockname()))
try:
loop.run_forever()
except KeyboardInterrupt as e:
thislog.info("Interrupted, closing connections...")
finally:
server.close()
loop.run_until_complete(server.wait_closed())
loop.close()