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