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
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 = {}
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
genesis 60 self.message("PING :%s" % self.server.name)
genesis 61 self.__sent_ping = True
genesis 62 else:
genesis 63
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
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
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
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
genesis 473
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:
genesis 488 self.server.state.add_peer(arguments[0])
genesis 489 self.pest_reply("added new peer %s" % arguments[0])
genesis 490 else:
genesis 491 self.pest_reply("Usage: PEER <HANDLE>")
genesis 492
genesis 493 def unpeer_handler():
genesis 494 if len(arguments) == 1:
genesis 495 try:
genesis 496 self.server.state.remove_peer(arguments[0])
genesis 497 self.pest_reply("removed peer %s" % arguments[0])
9992-handle-edge-... 498 except Exception, e:
9992-handle-edge-... 499 self.server.print_debug(e)
genesis 500 self.pest_reply("Error attempting to remove peer")
genesis 501 else:
genesis 502 self.pest_reply("Usage: UNPEER <HANDLE>")
genesis 503
genesis 504 def genkey_handler():
genesis 505 self.pest_reply(base64.b64encode(os.urandom(64)))
genesis 506
genesis 507 def key_handler():
genesis 508 if len(arguments) != 2:
genesis 509 self.pest_reply("Usage: KEY <HANDLE> <KEY>")
genesis 510 else:
genesis 511 handle = arguments[0]
genesis 512 key = arguments[1]
genesis 513 try:
genesis 514 self.server.state.add_key(handle, key)
9992-handle-edge-... 515 self.pest_reply("added key: %s" % key)
genesis 516 except:
genesis 517 self.pest_reply("Error attempting to add key")
genesis 518
genesis 519 def unkey_handler():
genesis 520 if len(arguments) != 1:
genesis 521 self.pest_reply("Usage: UNKEY <KEY>")
genesis 522 else:
genesis 523 try:
genesis 524 self.server.state.remove_key(arguments[0])
9992-handle-edge-... 525 self.pest_reply("removed key: %s" % arguments[0])
9992-handle-edge-... 526 except Exception, e:
genesis 527 self.pest_reply("Error attempting to remove key")
9992-handle-edge-... 528 self.server.print_debug(e)
genesis 529
genesis 530 def at_handler():
genesis 531 if len(arguments) == 0:
genesis 532 at = self.server.state.get_at()
genesis 533 elif len(arguments) == 1:
genesis 534 handle = arguments[0]
genesis 535 at = self.server.state.get_at(handle)
genesis 536 elif len(arguments) == 2:
genesis 537 handle, address = arguments
genesis 538 address_ip, port = string.split(address, ":")
genesis 539 self.server.state.update_address_table({"handle": handle,
genesis 540 "address": address_ip,
genesis 541 "port": port})
9992-handle-edge-... 542 self.pest_reply("updated address table: %s %s" % (handle, address))
genesis 543 return
genesis 544 elif len(arguments) > 2:
genesis 545 self.pest_reply("Usage: AT [<HANDLE>] [<ADDRESS>]")
genesis 546 return
9992-handle-edge-... 547 if len(at) > 0:
9992-handle-edge-... 548 for address in at:
9992-handle-edge-... 549 self.pest_reply("%s %s %s" % (address["handle"],
9992-handle-edge-... 550 address["address"],
9992-handle-edge-... 551 address["updated_at"]))
9992-handle-edge-... 552 else:
9992-handle-edge-... 553 self.pest_reply("no results")
genesis 554
genesis 555 handler_table = {
genesis 556 "AWAY": away_handler,
genesis 557 "AT": at_handler,
genesis 558 "GENKEY": genkey_handler,
genesis 559 "ISON": ison_handler,
genesis 560 "JOIN": join_handler,
genesis 561 "KEY": key_handler,
genesis 562 "LIST": list_handler,
genesis 563 "LUSERS": lusers_handler,
genesis 564 "MODE": mode_handler,
genesis 565 "MOTD": motd_handler,
genesis 566 "NICK": nick_handler,
genesis 567 "NOTICE": notice_and_privmsg_handler,
genesis 568 "PART": part_handler,
genesis 569 "PEER": peer_handler,
genesis 570 "PING": ping_handler,
genesis 571 "PONG": pong_handler,
genesis 572 "PRIVMSG": notice_and_privmsg_handler,
genesis 573 "QUIT": quit_handler,
genesis 574 "TOPIC": topic_handler,
genesis 575 "UNKEY": unkey_handler,
genesis 576 "UNPEER": unpeer_handler,
genesis 577 "WALLOPS": wallops_handler,
genesis 578 "WHO": who_handler,
genesis 579 "WHOIS": whois_handler,
genesis 580 "WOT": wot_handler
genesis 581 }
genesis 582 server = self.server
genesis 583 valid_channel_re = self.__valid_channelname_regexp
genesis 584 try:
genesis 585 handler_table[command]()
genesis 586 except KeyError:
genesis 587 self.reply("421 %s %s :Unknown command" % (self.nickname, command))
genesis 588
genesis 589 def socket_readable_notification(self):
genesis 590 try:
genesis 591 data = self.socket.recv(2 ** 10)
genesis 592 self.server.print_debug(
genesis 593 "[%s:%d] -> %r" % (self.host, self.port, data))
genesis 594 quitmsg = "EOT"
genesis 595 except socket.error as x:
genesis 596 data = ""
genesis 597 quitmsg = x
genesis 598 if data:
genesis 599 self.__readbuffer += data
genesis 600 self.__parse_read_buffer()
genesis 601 self.__timestamp = time.time()
genesis 602 self.__sent_ping = False
genesis 603 else:
genesis 604 self.disconnect(quitmsg)
genesis 605
genesis 606 def socket_writable_notification(self):
genesis 607 try:
genesis 608 sent = self.socket.send(self.__writebuffer)
genesis 609 self.server.print_debug(
genesis 610 "[%s:%d] <- %r" % (
genesis 611 self.host, self.port, self.__writebuffer[:sent]))
genesis 612 self.__writebuffer = self.__writebuffer[sent:]
genesis 613 except socket.error as x:
genesis 614 self.disconnect(x)
genesis 615
genesis 616 def disconnect(self, quitmsg):
genesis 617 self.message("ERROR :%s" % quitmsg)
genesis 618 self.server.print_info(
genesis 619 "Disconnected connection from %s:%s (%s)." % (
genesis 620 self.host, self.port, quitmsg))
genesis 621 self.socket.close()
genesis 622 self.server.remove_client(self, quitmsg)
genesis 623
genesis 624 def message(self, msg):
genesis 625 self.__writebuffer += msg + "\r\n"
genesis 626
genesis 627 def reply(self, msg):
genesis 628 self.message(":%s %s" % (self.server.name, msg))
genesis 629
genesis 630 def pest_reply(self, msg):
genesis 631 self.message("NOTICE %s :%s" % (self.nickname, msg))
genesis 632
genesis 633 def reply_403(self, channel):
genesis 634 self.reply("403 %s %s :No such channel" % (self.nickname, channel))
genesis 635
genesis 636 def reply_461(self, command):
genesis 637 nickname = self.nickname or "*"
genesis 638 self.reply("461 %s %s :Not enough parameters" % (nickname, command))
genesis 639
genesis 640 def message_channel(self, channel, command, message, include_self=False):
genesis 641 line = ":%s %s %s" % (self.prefix, command, message)
genesis 642 for client in channel.members:
genesis 643 if client != self or include_self:
genesis 644 client.message(line)
genesis 645
genesis 646 self.server.peer_message({
genesis 647 "speaker": self.nickname,
genesis 648 "command": BROADCAST,
genesis 649 "bounces": 0,
genesis 650 "body": line
genesis 651 })
genesis 652
genesis 653 def channel_log(self, channel, message, meta=False):
genesis 654 if not self.server.logdir:
genesis 655 return
genesis 656 if meta:
genesis 657 format = "[%s] * %s %s\n"
genesis 658 else:
genesis 659 format = "[%s] <%s> %s\n"
genesis 660 timestamp = datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S UTC")
genesis 661 logname = channel.name.replace("_", "__").replace("/", "_")
genesis 662 fp = open("%s/%s.log" % (self.server.logdir, logname), "a")
genesis 663 fp.write(format % (timestamp, self.nickname, message))
genesis 664 fp.close()
genesis 665
genesis 666 def message_related(self, msg, include_self=False):
genesis 667 clients = set()
genesis 668 if include_self:
genesis 669 clients.add(self)
genesis 670 for channel in self.channels.values():
genesis 671 clients |= channel.members
genesis 672 if not include_self:
genesis 673 clients.discard(self)
genesis 674 for client in clients:
genesis 675 client.message(msg)
genesis 676
genesis 677 def send_lusers(self):
genesis 678 self.reply("251 %s :There are %d users and 0 services on 1 server"
genesis 679 % (self.nickname, len(self.server.clients)))
genesis 680
genesis 681 def send_motd(self):
genesis 682 server = self.server
genesis 683 motdlines = server.get_motd_lines()
genesis 684 if motdlines:
genesis 685 self.reply("375 %s :- %s Message of the day -"
genesis 686 % (self.nickname, server.name))
genesis 687 for line in motdlines:
genesis 688 self.reply("372 %s :- %s" % (self.nickname, line.rstrip()))
genesis 689 self.reply("376 %s :End of /MOTD command" % self.nickname)
genesis 690 else:
genesis 691 self.reply("422 %s :MOTD File is missing" % self.nickname)