raw
genesis                 1 import socket
genesis 2 import time
genesis 3 import sys
genesis 4 import re
genesis 5 import string
genesis 6 import os
genesis 7 import base64
genesis 8 from lib.server import VERSION
genesis 9 from funcs import *
genesis 10 from lib.commands import BROADCAST
genesis 11 from lib.commands import DIRECT
genesis 12
genesis 13 class Client(object):
genesis 14 __linesep_regexp = re.compile(r"\r?\n")
genesis 15 # The RFC limit for nicknames is 9 characters, but what the heck.
genesis 16 __valid_nickname_regexp = re.compile(
genesis 17 r"^[][\`_^{|}A-Za-z][][\`_^{|}A-Za-z0-9-]{0,50}$")
genesis 18 __valid_channelname_regexp = re.compile(
genesis 19 r"^[&#+!][^\x00\x07\x0a\x0d ,:]{0,50}$")
genesis 20
genesis 21 def __init__(self, server, socket):
genesis 22 self.server = server
genesis 23 self.socket = socket
genesis 24 self.channels = {} # irc_lower(Channel name) --> Channel
genesis 25 self.nickname = None
genesis 26 self.user = None
genesis 27 self.realname = None
genesis 28 (self.host, self.port) = socket.getpeername()
genesis 29 self.__timestamp = time.time()
genesis 30 self.__readbuffer = ""
genesis 31 self.__writebuffer = ""
genesis 32 self.__sent_ping = False
genesis 33 if self.server.password:
genesis 34 self.__handle_command = self.__pass_handler
genesis 35 else:
genesis 36 self.__handle_command = self.__registration_handler
genesis 37
genesis 38 def is_addressed_to_me(self, message):
genesis 39 command = self.__parse_udp_message(message)
genesis 40 if command[0] == 'PRIVMSG':
genesis 41 if command[1][0][0] == '#' or command[1][0] == self.nickname:
genesis 42 return True
genesis 43 else:
genesis 44 return False
genesis 45 else:
genesis 46 return True
genesis 47
genesis 48 def get_prefix(self):
genesis 49 return "%s!%s@%s" % (self.nickname, self.user, self.host)
genesis 50 prefix = property(get_prefix)
genesis 51
genesis 52 def check_aliveness(self):
genesis 53 now = time.time()
genesis 54 if self.__timestamp + 180 < now:
genesis 55 self.disconnect("ping timeout")
genesis 56 return
genesis 57 if not self.__sent_ping and self.__timestamp + 90 < now:
genesis 58 if self.__handle_command == self.__command_handler:
genesis 59 # Registered.
genesis 60 self.message("PING :%s" % self.server.name)
genesis 61 self.__sent_ping = True
genesis 62 else:
genesis 63 # Not registered.
genesis 64 self.disconnect("ping timeout")
genesis 65
genesis 66 def write_queue_size(self):
genesis 67 return len(self.__writebuffer)
genesis 68
genesis 69 def __parse_udp_message(self, message):
genesis 70 data = " ".join(message.split()[1:]) + "\r\n"
genesis 71 lines = self.__linesep_regexp.split(data)
genesis 72 lines = lines[:-1]
genesis 73 commands = []
genesis 74 for line in lines:
genesis 75 if not line:
genesis 76 # Empty line. Ignore.
genesis 77 continue
genesis 78 x = line.split(" ", 1)
genesis 79 command = x[0].upper()
genesis 80 if len(x) == 1:
genesis 81 arguments = []
genesis 82 else:
genesis 83 if len(x[1]) > 0 and x[1][0] == ":":
genesis 84 arguments = [x[1][1:]]
genesis 85 else:
genesis 86 y = string.split(x[1], " :", 1)
genesis 87 arguments = string.split(y[0])
genesis 88 if len(y) == 2:
genesis 89 arguments.append(y[1])
genesis 90 commands.append([command, arguments])
genesis 91 return commands[0]
genesis 92
genesis 93 def __parse_read_buffer(self):
genesis 94 lines = self.__linesep_regexp.split(self.__readbuffer)
genesis 95 self.__readbuffer = lines[-1]
genesis 96 lines = lines[:-1]
genesis 97 for line in lines:
genesis 98 if not line:
genesis 99 # Empty line. Ignore.
genesis 100 continue
genesis 101 x = line.split(" ", 1)
genesis 102 command = x[0].upper()
genesis 103 if len(x) == 1:
genesis 104 arguments = []
genesis 105 else:
genesis 106 if len(x[1]) > 0 and x[1][0] == ":":
genesis 107 arguments = [x[1][1:]]
genesis 108 else:
genesis 109 y = string.split(x[1], " :", 1)
genesis 110 arguments = string.split(y[0])
genesis 111 if len(y) == 2:
genesis 112 arguments.append(y[1])
genesis 113 self.__handle_command(command, arguments)
genesis 114
genesis 115 def __pass_handler(self, command, arguments):
genesis 116 server = self.server
genesis 117 if command == "PASS":
genesis 118 if len(arguments) == 0:
genesis 119 self.reply_461("PASS")
genesis 120 else:
genesis 121 if arguments[0].lower() == server.password:
genesis 122 self.__handle_command = self.__registration_handler
genesis 123 else:
genesis 124 self.reply("464 :Password incorrect")
genesis 125 elif command == "QUIT":
genesis 126 self.disconnect("Client quit")
genesis 127 return
genesis 128
genesis 129 def __registration_handler(self, command, arguments):
genesis 130 server = self.server
genesis 131 if command == "NICK":
genesis 132 if len(arguments) < 1:
genesis 133 self.reply("431 :No nickname given")
genesis 134 return
genesis 135 nick = arguments[0]
genesis 136 if server.get_client(nick):
genesis 137 self.reply("433 * %s :Nickname is already in use" % nick)
genesis 138 elif not self.__valid_nickname_regexp.match(nick):
genesis 139 self.reply("432 * %s :Erroneous nickname" % nick)
genesis 140 else:
genesis 141 self.nickname = nick
genesis 142 server.client_changed_nickname(self, None)
genesis 143 elif command == "USER":
genesis 144 if len(arguments) < 4:
genesis 145 self.reply_461("USER")
genesis 146 return
genesis 147 self.user = arguments[0]
genesis 148 self.realname = arguments[3]
genesis 149 elif command == "QUIT":
genesis 150 self.disconnect("Client quit")
genesis 151 return
genesis 152 if self.nickname and self.user:
genesis 153 self.reply("001 %s :Hi, welcome to IRC" % self.nickname)
genesis 154 self.reply("002 %s :Your host is %s, running version blatta-%s"
genesis 155 % (self.nickname, server.name, VERSION))
genesis 156 self.reply("003 %s :This server was created sometime"
genesis 157 % self.nickname)
genesis 158 self.reply("004 %s :%s blatta-%s o o"
genesis 159 % (self.nickname, server.name, VERSION))
genesis 160 self.send_lusers()
genesis 161 self.send_motd()
genesis 162 self.__handle_command = self.__command_handler
genesis 163
genesis 164 def __command_handler(self, command, arguments):
genesis 165 def away_handler():
genesis 166 pass
genesis 167
genesis 168 def ison_handler():
genesis 169 if len(arguments) < 1:
genesis 170 self.reply_461("ISON")
genesis 171 return
genesis 172 nicks = arguments
genesis 173 online = [n for n in nicks if server.get_client(n)]
genesis 174 self.reply("303 %s :%s" % (self.nickname, " ".join(online)))
genesis 175
genesis 176 def join_handler():
genesis 177 if len(arguments) < 1:
genesis 178 self.reply_461("JOIN")
genesis 179 return
genesis 180 if arguments[0] == "0":
genesis 181 for (channelname, channel) in self.channels.items():
genesis 182 self.message_channel(channel, "PART", channelname, True)
genesis 183 self.channel_log(channel, "left", meta=True)
genesis 184 server.remove_member_from_channel(self, channelname)
genesis 185 self.channels = {}
genesis 186 return
genesis 187 channelnames = arguments[0].split(",")
genesis 188 if len(arguments) > 1:
genesis 189 keys = arguments[1].split(",")
genesis 190 else:
genesis 191 keys = []
genesis 192 keys.extend((len(channelnames) - len(keys)) * [None])
genesis 193 for (i, channelname) in enumerate(channelnames):
genesis 194 if irc_lower(channelname) in self.channels:
genesis 195 continue
genesis 196 if not valid_channel_re.match(channelname):
genesis 197 self.reply_403(channelname)
genesis 198 continue
genesis 199 channel = server.get_channel(channelname)
genesis 200 if channel.key is not None and channel.key != keys[i]:
genesis 201 self.reply(
genesis 202 "475 %s %s :Cannot join channel (+k) - bad key"
genesis 203 % (self.nickname, channelname))
genesis 204 continue
genesis 205 channel.add_member(self)
genesis 206 self.channels[irc_lower(channelname)] = channel
genesis 207 self.message_channel(channel, "JOIN", channelname, True)
genesis 208 self.channel_log(channel, "joined", meta=True)
genesis 209 if channel.topic:
genesis 210 self.reply("332 %s %s :%s"
genesis 211 % (self.nickname, channel.name, channel.topic))
genesis 212 else:
genesis 213 self.reply("331 %s %s :No topic is set"
genesis 214 % (self.nickname, channel.name))
genesis 215 self.reply("353 %s = %s :%s"
genesis 216 % (self.nickname,
genesis 217 channelname,
genesis 218 " ".join(sorted(x.nickname
genesis 219 for x in channel.members))))
genesis 220 self.reply("366 %s %s :End of NAMES list"
genesis 221 % (self.nickname, channelname))
genesis 222
genesis 223 def list_handler():
genesis 224 if len(arguments) < 1:
genesis 225 channels = server.channels.values()
genesis 226 else:
genesis 227 channels = []
genesis 228 for channelname in arguments[0].split(","):
genesis 229 if server.has_channel(channelname):
genesis 230 channels.append(server.get_channel(channelname))
genesis 231 channels.sort(key=lambda x: x.name)
genesis 232 for channel in channels:
genesis 233 self.reply("322 %s %s %d :%s"
genesis 234 % (self.nickname, channel.name,
genesis 235 len(channel.members), channel.topic))
genesis 236 self.reply("323 %s :End of LIST" % self.nickname)
genesis 237
genesis 238 def lusers_handler():
genesis 239 self.send_lusers()
genesis 240
genesis 241 def mode_handler():
genesis 242 if len(arguments) < 1:
genesis 243 self.reply_461("MODE")
genesis 244 return
genesis 245 targetname = arguments[0]
genesis 246 if server.has_channel(targetname):
genesis 247 channel = server.get_channel(targetname)
genesis 248 if len(arguments) < 2:
genesis 249 if channel.key:
genesis 250 modes = "+k"
genesis 251 if irc_lower(channel.name) in self.channels:
genesis 252 modes += " %s" % channel.key
genesis 253 else:
genesis 254 modes = "+"
genesis 255 self.reply("324 %s %s %s"
genesis 256 % (self.nickname, targetname, modes))
genesis 257 return
genesis 258 flag = arguments[1]
genesis 259 if flag == "+k":
genesis 260 if len(arguments) < 3:
genesis 261 self.reply_461("MODE")
genesis 262 return
genesis 263 key = arguments[2]
genesis 264 if irc_lower(channel.name) in self.channels:
genesis 265 channel.key = key
genesis 266 self.message_channel(
genesis 267 channel, "MODE", "%s +k %s" % (channel.name, key),
genesis 268 True)
genesis 269 self.channel_log(
genesis 270 channel, "set channel key to %s" % key, meta=True)
genesis 271 else:
genesis 272 self.reply("442 %s :You're not on that channel"
genesis 273 % targetname)
genesis 274 elif flag == "-k":
genesis 275 if irc_lower(channel.name) in self.channels:
genesis 276 channel.key = None
genesis 277 self.message_channel(
genesis 278 channel, "MODE", "%s -k" % channel.name,
genesis 279 True)
genesis 280 self.channel_log(
genesis 281 channel, "removed channel key", meta=True)
genesis 282 else:
genesis 283 self.reply("442 %s :You're not on that channel"
genesis 284 % targetname)
genesis 285 else:
genesis 286 self.reply("472 %s %s :Unknown MODE flag"
genesis 287 % (self.nickname, flag))
genesis 288 elif targetname == self.nickname:
genesis 289 if len(arguments) == 1:
genesis 290 self.reply("221 %s +" % self.nickname)
genesis 291 else:
genesis 292 self.reply("501 %s :Unknown MODE flag" % self.nickname)
genesis 293 else:
genesis 294 self.reply_403(targetname)
genesis 295
genesis 296 def motd_handler():
genesis 297 self.send_motd()
genesis 298
genesis 299 def nick_handler():
genesis 300 if len(arguments) < 1:
genesis 301 self.reply("431 :No nickname given")
genesis 302 return
genesis 303 newnick = arguments[0]
genesis 304 client = server.get_client(newnick)
genesis 305 if newnick == self.nickname:
genesis 306 pass
genesis 307 elif client and client is not self:
genesis 308 self.reply("433 %s %s :Nickname is already in use"
genesis 309 % (self.nickname, newnick))
genesis 310 elif not self.__valid_nickname_regexp.match(newnick):
genesis 311 self.reply("432 %s %s :Erroneous Nickname"
genesis 312 % (self.nickname, newnick))
genesis 313 else:
genesis 314 for x in self.channels.values():
genesis 315 self.channel_log(
genesis 316 x, "changed nickname to %s" % newnick, meta=True)
genesis 317 oldnickname = self.nickname
genesis 318 self.nickname = newnick
genesis 319 server.client_changed_nickname(self, oldnickname)
genesis 320 self.message_related(
genesis 321 ":%s!%s@%s NICK %s"
genesis 322 % (oldnickname, self.user, self.host, self.nickname),
genesis 323 True)
genesis 324
genesis 325 def notice_and_privmsg_handler():
genesis 326 if len(arguments) == 0:
genesis 327 self.reply("411 %s :No recipient given (%s)"
genesis 328 % (self.nickname, command))
genesis 329 return
genesis 330 if len(arguments) == 1:
genesis 331 self.reply("412 %s :No text to send" % self.nickname)
genesis 332 return
genesis 333 targetname = arguments[0]
genesis 334 message = arguments[1]
genesis 335 client = server.get_client(targetname)
genesis 336
genesis 337 if server.has_channel(targetname):
genesis 338 channel = server.get_channel(targetname)
genesis 339 self.message_channel(
genesis 340 channel, command, "%s :%s" % (channel.name, message))
genesis 341 self.channel_log(channel, message)
genesis 342 else:
genesis 343 formatted_message = ":%s %s %s :%s" % (self.prefix, command, targetname, message)
genesis 344 self.server.peer_message({
genesis 345 "speaker": self.nickname,
genesis 346 "handle": targetname,
genesis 347 "body": formatted_message,
genesis 348 "bounces": 0,
genesis 349 "command": DIRECT
genesis 350 })
genesis 351 if(client):
genesis 352 client.message(formatted_message)
genesis 353
genesis 354 def part_handler():
genesis 355 if len(arguments) < 1:
genesis 356 self.reply_461("PART")
genesis 357 return
genesis 358 if len(arguments) > 1:
genesis 359 partmsg = arguments[1]
genesis 360 else:
genesis 361 partmsg = self.nickname
genesis 362 for channelname in arguments[0].split(","):
genesis 363 if not valid_channel_re.match(channelname):
genesis 364 self.reply_403(channelname)
genesis 365 elif not irc_lower(channelname) in self.channels:
genesis 366 self.reply("442 %s %s :You're not on that channel"
genesis 367 % (self.nickname, channelname))
genesis 368 else:
genesis 369 channel = self.channels[irc_lower(channelname)]
genesis 370 self.message_channel(
genesis 371 channel, "PART", "%s :%s" % (channelname, partmsg),
genesis 372 True)
genesis 373 self.channel_log(channel, "left (%s)" % partmsg, meta=True)
genesis 374 del self.channels[irc_lower(channelname)]
genesis 375 server.remove_member_from_channel(self, channelname)
genesis 376
genesis 377 def ping_handler():
genesis 378 if len(arguments) < 1:
genesis 379 self.reply("409 %s :No origin specified" % self.nickname)
genesis 380 return
genesis 381 self.reply("PONG %s :%s" % (server.name, arguments[0]))
genesis 382
genesis 383 def pong_handler():
genesis 384 pass
genesis 385
genesis 386 def quit_handler():
genesis 387 if len(arguments) < 1:
genesis 388 quitmsg = self.nickname
genesis 389 else:
genesis 390 quitmsg = arguments[0]
genesis 391 self.disconnect(quitmsg)
genesis 392
genesis 393 def topic_handler():
genesis 394 if len(arguments) < 1:
genesis 395 self.reply_461("TOPIC")
genesis 396 return
genesis 397 channelname = arguments[0]
genesis 398 channel = self.channels.get(irc_lower(channelname))
genesis 399 if channel:
genesis 400 if len(arguments) > 1:
genesis 401 newtopic = arguments[1]
genesis 402 channel.topic = newtopic
genesis 403 self.message_channel(
genesis 404 channel, "TOPIC", "%s :%s" % (channelname, newtopic),
genesis 405 True)
genesis 406 self.channel_log(
genesis 407 channel, "set topic to %r" % newtopic, meta=True)
genesis 408 else:
genesis 409 if channel.topic:
genesis 410 self.reply("332 %s %s :%s"
genesis 411 % (self.nickname, channel.name,
genesis 412 channel.topic))
genesis 413 else:
genesis 414 self.reply("331 %s %s :No topic is set"
genesis 415 % (self.nickname, channel.name))
genesis 416 else:
genesis 417 self.reply("442 %s :You're not on that channel" % channelname)
genesis 418
genesis 419 def wallops_handler():
genesis 420 if len(arguments) < 1:
genesis 421 self.reply_461(command)
genesis 422 message = arguments[0]
genesis 423 for client in server.clients.values():
genesis 424 client.message(":%s NOTICE %s :Global notice: %s"
genesis 425 % (self.prefix, client.nickname, message))
genesis 426
genesis 427 def who_handler():
genesis 428 if len(arguments) < 1:
genesis 429 return
genesis 430 targetname = arguments[0]
genesis 431 if server.has_channel(targetname):
genesis 432 channel = server.get_channel(targetname)
genesis 433 for member in channel.members:
genesis 434 self.reply("352 %s %s %s %s %s %s H :0 %s"
genesis 435 % (self.nickname, targetname, member.user,
genesis 436 member.host, server.name, member.nickname,
genesis 437 member.realname))
genesis 438 self.reply("315 %s %s :End of WHO list"
genesis 439 % (self.nickname, targetname))
genesis 440
genesis 441 def whois_handler():
genesis 442 if len(arguments) < 1:
genesis 443 return
genesis 444 username = arguments[0]
genesis 445 user = server.get_client(username)
genesis 446 if user:
genesis 447 self.reply("311 %s %s %s %s * :%s"
genesis 448 % (self.nickname, user.nickname, user.user,
genesis 449 user.host, user.realname))
genesis 450 self.reply("312 %s %s %s :%s"
genesis 451 % (self.nickname, user.nickname, server.name,
genesis 452 server.name))
genesis 453 self.reply("319 %s %s :%s"
genesis 454 % (self.nickname, user.nickname,
genesis 455 " ".join(user.channels)))
genesis 456 self.reply("318 %s %s :End of WHOIS list"
genesis 457 % (self.nickname, user.nickname))
genesis 458 else:
genesis 459 self.reply("401 %s %s :No such nick"
genesis 460 % (self.nickname, username))
genesis 461
genesis 462 def wot_handler():
genesis 463 if len(arguments) < 1:
genesis 464 # Display the current WOT
9992-handle-edge-... 465 peers = self.server.state.get_peers()
9992-handle-edge-... 466 if len(peers) > 0:
9992-handle-edge-... 467 for peer in peers:
9992-handle-edge-... 468 self.pest_reply("%s %s:%s" % (string.join(peer.handles, ","), peer.address, peer.port))
9992-handle-edge-... 469 else:
9992-handle-edge-... 470 self.pest_reply("WOT is empty")
genesis 471 elif len(arguments) == 1:
genesis 472 # Display all WOT data concerning the peer identified by HANDLE,
genesis 473 # including all known keys, starting with the most recently used, for that peer.
genesis 474 handle = arguments[0]
genesis 475 peer = self.server.state.get_peer_by_handle(handle)
9992-handle-edge-... 476 if peer:
9992-handle-edge-... 477 self.pest_reply("keys:")
9992-handle-edge-... 478 for key in peer.keys:
9992-handle-edge-... 479 self.pest_reply("%s" % key)
9992-handle-edge-... 480 else:
9992-handle-edge-... 481 self.pest_reply("unknown peer: %s" % handle)
genesis 482
genesis 483 else:
genesis 484 pass
genesis 485
genesis 486 def peer_handler():
genesis 487 if len(arguments) == 1:
9991-improved-log... 488 try:
9991-improved-log... 489 self.server.state.add_peer(arguments[0])
9991-improved-log... 490 self.pest_reply("added new peer %s" % arguments[0])
9991-improved-log... 491 except:
9991-improved-log... 492 self.pest_reply("error attempting to add peer %s" % arguments[0])
genesis 493 else:
genesis 494 self.pest_reply("Usage: PEER <HANDLE>")
genesis 495
genesis 496 def unpeer_handler():
genesis 497 if len(arguments) == 1:
genesis 498 try:
genesis 499 self.server.state.remove_peer(arguments[0])
genesis 500 self.pest_reply("removed peer %s" % arguments[0])
9992-handle-edge-... 501 except Exception, e:
9992-handle-edge-... 502 self.server.print_debug(e)
genesis 503 self.pest_reply("Error attempting to remove peer")
genesis 504 else:
genesis 505 self.pest_reply("Usage: UNPEER <HANDLE>")
genesis 506
genesis 507 def genkey_handler():
genesis 508 self.pest_reply(base64.b64encode(os.urandom(64)))
genesis 509
genesis 510 def key_handler():
genesis 511 if len(arguments) != 2:
genesis 512 self.pest_reply("Usage: KEY <HANDLE> <KEY>")
genesis 513 else:
genesis 514 handle = arguments[0]
genesis 515 key = arguments[1]
genesis 516 try:
genesis 517 self.server.state.add_key(handle, key)
9992-handle-edge-... 518 self.pest_reply("added key: %s" % key)
genesis 519 except:
genesis 520 self.pest_reply("Error attempting to add key")
genesis 521
genesis 522 def unkey_handler():
genesis 523 if len(arguments) != 1:
genesis 524 self.pest_reply("Usage: UNKEY <KEY>")
genesis 525 else:
genesis 526 try:
genesis 527 self.server.state.remove_key(arguments[0])
9992-handle-edge-... 528 self.pest_reply("removed key: %s" % arguments[0])
9992-handle-edge-... 529 except Exception, e:
genesis 530 self.pest_reply("Error attempting to remove key")
9992-handle-edge-... 531 self.server.print_debug(e)
genesis 532
genesis 533 def at_handler():
genesis 534 if len(arguments) == 0:
genesis 535 at = self.server.state.get_at()
genesis 536 elif len(arguments) == 1:
genesis 537 handle = arguments[0]
genesis 538 at = self.server.state.get_at(handle)
genesis 539 elif len(arguments) == 2:
genesis 540 handle, address = arguments
genesis 541 address_ip, port = string.split(address, ":")
genesis 542 self.server.state.update_address_table({"handle": handle,
genesis 543 "address": address_ip,
9991-improved-log... 544 "port": port},
9991-improved-log... 545 False)
9992-handle-edge-... 546 self.pest_reply("updated address table: %s %s" % (handle, address))
genesis 547 return
genesis 548 elif len(arguments) > 2:
genesis 549 self.pest_reply("Usage: AT [<HANDLE>] [<ADDRESS>]")
genesis 550 return
9992-handle-edge-... 551 if len(at) > 0:
9992-handle-edge-... 552 for address in at:
9992-handle-edge-... 553 self.pest_reply("%s %s %s" % (address["handle"],
9992-handle-edge-... 554 address["address"],
9991-improved-log... 555 address["active_at"]))
9992-handle-edge-... 556 else:
9992-handle-edge-... 557 self.pest_reply("no results")
genesis 558
genesis 559 handler_table = {
genesis 560 "AWAY": away_handler,
genesis 561 "AT": at_handler,
genesis 562 "GENKEY": genkey_handler,
genesis 563 "ISON": ison_handler,
genesis 564 "JOIN": join_handler,
genesis 565 "KEY": key_handler,
genesis 566 "LIST": list_handler,
genesis 567 "LUSERS": lusers_handler,
genesis 568 "MODE": mode_handler,
genesis 569 "MOTD": motd_handler,
genesis 570 "NICK": nick_handler,
genesis 571 "NOTICE": notice_and_privmsg_handler,
genesis 572 "PART": part_handler,
genesis 573 "PEER": peer_handler,
genesis 574 "PING": ping_handler,
genesis 575 "PONG": pong_handler,
genesis 576 "PRIVMSG": notice_and_privmsg_handler,
genesis 577 "QUIT": quit_handler,
genesis 578 "TOPIC": topic_handler,
genesis 579 "UNKEY": unkey_handler,
genesis 580 "UNPEER": unpeer_handler,
genesis 581 "WALLOPS": wallops_handler,
genesis 582 "WHO": who_handler,
genesis 583 "WHOIS": whois_handler,
genesis 584 "WOT": wot_handler
genesis 585 }
genesis 586 server = self.server
genesis 587 valid_channel_re = self.__valid_channelname_regexp
genesis 588 try:
genesis 589 handler_table[command]()
genesis 590 except KeyError:
genesis 591 self.reply("421 %s %s :Unknown command" % (self.nickname, command))
genesis 592
genesis 593 def socket_readable_notification(self):
genesis 594 try:
genesis 595 data = self.socket.recv(2 ** 10)
genesis 596 self.server.print_debug(
genesis 597 "[%s:%d] -> %r" % (self.host, self.port, data))
genesis 598 quitmsg = "EOT"
genesis 599 except socket.error as x:
genesis 600 data = ""
genesis 601 quitmsg = x
genesis 602 if data:
genesis 603 self.__readbuffer += data
genesis 604 self.__parse_read_buffer()
genesis 605 self.__timestamp = time.time()
genesis 606 self.__sent_ping = False
genesis 607 else:
genesis 608 self.disconnect(quitmsg)
genesis 609
genesis 610 def socket_writable_notification(self):
genesis 611 try:
genesis 612 sent = self.socket.send(self.__writebuffer)
genesis 613 self.server.print_debug(
genesis 614 "[%s:%d] <- %r" % (
genesis 615 self.host, self.port, self.__writebuffer[:sent]))
genesis 616 self.__writebuffer = self.__writebuffer[sent:]
genesis 617 except socket.error as x:
genesis 618 self.disconnect(x)
genesis 619
genesis 620 def disconnect(self, quitmsg):
genesis 621 self.message("ERROR :%s" % quitmsg)
genesis 622 self.server.print_info(
genesis 623 "Disconnected connection from %s:%s (%s)." % (
genesis 624 self.host, self.port, quitmsg))
genesis 625 self.socket.close()
genesis 626 self.server.remove_client(self, quitmsg)
genesis 627
genesis 628 def message(self, msg):
genesis 629 self.__writebuffer += msg + "\r\n"
genesis 630
genesis 631 def reply(self, msg):
genesis 632 self.message(":%s %s" % (self.server.name, msg))
genesis 633
genesis 634 def pest_reply(self, msg):
genesis 635 self.message("NOTICE %s :%s" % (self.nickname, msg))
genesis 636
genesis 637 def reply_403(self, channel):
genesis 638 self.reply("403 %s %s :No such channel" % (self.nickname, channel))
genesis 639
genesis 640 def reply_461(self, command):
genesis 641 nickname = self.nickname or "*"
genesis 642 self.reply("461 %s %s :Not enough parameters" % (nickname, command))
genesis 643
genesis 644 def message_channel(self, channel, command, message, include_self=False):
genesis 645 line = ":%s %s %s" % (self.prefix, command, message)
genesis 646 for client in channel.members:
genesis 647 if client != self or include_self:
genesis 648 client.message(line)
genesis 649 # send the channel message to peers as well
genesis 650 self.server.peer_message({
genesis 651 "speaker": self.nickname,
genesis 652 "command": BROADCAST,
genesis 653 "bounces": 0,
genesis 654 "body": line
genesis 655 })
genesis 656
genesis 657 def channel_log(self, channel, message, meta=False):
genesis 658 if not self.server.logdir:
genesis 659 return
genesis 660 if meta:
genesis 661 format = "[%s] * %s %s\n"
genesis 662 else:
genesis 663 format = "[%s] <%s> %s\n"
genesis 664 timestamp = datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S UTC")
genesis 665 logname = channel.name.replace("_", "__").replace("/", "_")
genesis 666 fp = open("%s/%s.log" % (self.server.logdir, logname), "a")
genesis 667 fp.write(format % (timestamp, self.nickname, message))
genesis 668 fp.close()
genesis 669
genesis 670 def message_related(self, msg, include_self=False):
genesis 671 clients = set()
genesis 672 if include_self:
genesis 673 clients.add(self)
genesis 674 for channel in self.channels.values():
genesis 675 clients |= channel.members
genesis 676 if not include_self:
genesis 677 clients.discard(self)
genesis 678 for client in clients:
genesis 679 client.message(msg)
genesis 680
genesis 681 def send_lusers(self):
genesis 682 self.reply("251 %s :There are %d users and 0 services on 1 server"
genesis 683 % (self.nickname, len(self.server.clients)))
genesis 684
genesis 685 def send_motd(self):
genesis 686 server = self.server
genesis 687 motdlines = server.get_motd_lines()
genesis 688 if motdlines:
genesis 689 self.reply("375 %s :- %s Message of the day -"
genesis 690 % (self.nickname, server.name))
genesis 691 for line in motdlines:
genesis 692 self.reply("372 %s :- %s" % (self.nickname, line.rstrip()))
genesis 693 self.reply("376 %s :End of /MOTD command" % self.nickname)
genesis 694 else:
genesis 695 self.reply("422 %s :MOTD File is missing" % self.nickname)