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
9987-embargoing 10 from lib.state import State
9990-keep-ephemer... 11 from lib.message import Message
genesis 12 from lib.server import VERSION
genesis 13 from funcs import *
genesis 14 from lib.commands import BROADCAST
genesis 15 from lib.commands import DIRECT
genesis 16
genesis 17 class Client(object):
genesis 18 __linesep_regexp = re.compile(r"\r?\n")
genesis 19
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 = {}
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
genesis 61 self.message("PING :%s" % self.server.name)
genesis 62 self.__sent_ping = True
genesis 63 else:
genesis 64
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
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()))))
genesis 184 self.reply("366 %s %s :End of NAMES list"
genesis 185 % (self.nickname, channelname))
genesis 186
genesis 187 def list_handler():
genesis 188 if len(arguments) < 1:
genesis 189 channels = server.channels.values()
genesis 190 else:
genesis 191 channels = []
genesis 192 for channelname in arguments[0].split(","):
genesis 193 if server.has_channel(channelname):
genesis 194 channels.append(server.get_channel(channelname))
genesis 195 channels.sort(key=lambda x: x.name)
genesis 196 for channel in channels:
genesis 197 self.reply("322 %s %s %d :%s"
genesis 198 % (self.nickname, channel.name,
genesis 199 len(channel.members), channel.topic))
genesis 200 self.reply("323 %s :End of LIST" % self.nickname)
genesis 201
genesis 202 def lusers_handler():
9987-embargoing 203 pass
genesis 204
genesis 205 def mode_handler():
genesis 206 if len(arguments) < 1:
genesis 207 self.reply_461("MODE")
genesis 208 return
genesis 209 targetname = arguments[0]
genesis 210 if server.has_channel(targetname):
genesis 211 channel = server.get_channel(targetname)
genesis 212 if len(arguments) < 2:
genesis 213 if channel.key:
genesis 214 modes = "+k"
genesis 215 if irc_lower(channel.name) in self.channels:
genesis 216 modes += " %s" % channel.key
genesis 217 else:
genesis 218 modes = "+"
genesis 219 self.reply("324 %s %s %s"
genesis 220 % (self.nickname, targetname, modes))
genesis 221 return
genesis 222 flag = arguments[1]
genesis 223 if flag == "+k":
genesis 224 if len(arguments) < 3:
genesis 225 self.reply_461("MODE")
genesis 226 return
genesis 227 key = arguments[2]
genesis 228 if irc_lower(channel.name) in self.channels:
genesis 229 channel.key = key
genesis 230 self.message_channel(
genesis 231 channel, "MODE", "%s +k %s" % (channel.name, key),
genesis 232 True)
genesis 233 else:
genesis 234 self.reply("442 %s :You're not on that channel"
genesis 235 % targetname)
genesis 236 elif flag == "-k":
genesis 237 if irc_lower(channel.name) in self.channels:
genesis 238 channel.key = None
genesis 239 self.message_channel(
genesis 240 channel, "MODE", "%s -k" % channel.name,
genesis 241 True)
genesis 242 else:
genesis 243 self.reply("442 %s :You're not on that channel"
genesis 244 % targetname)
genesis 245 else:
genesis 246 self.reply("472 %s %s :Unknown MODE flag"
genesis 247 % (self.nickname, flag))
genesis 248 elif targetname == self.nickname:
genesis 249 if len(arguments) == 1:
genesis 250 self.reply("221 %s +" % self.nickname)
genesis 251 else:
genesis 252 self.reply("501 %s :Unknown MODE flag" % self.nickname)
genesis 253 else:
genesis 254 self.reply_403(targetname)
genesis 255
genesis 256 def motd_handler():
genesis 257 self.send_motd()
genesis 258
genesis 259 def nick_handler():
genesis 260 if len(arguments) < 1:
genesis 261 self.reply("431 :No nickname given")
genesis 262 return
genesis 263 newnick = arguments[0]
genesis 264 client = server.get_client(newnick)
genesis 265 if newnick == self.nickname:
genesis 266 pass
genesis 267 elif client and client is not self:
genesis 268 self.reply("433 %s %s :Nickname is already in use"
genesis 269 % (self.nickname, newnick))
genesis 270 elif not self.__valid_nickname_regexp.match(newnick):
genesis 271 self.reply("432 %s %s :Erroneous Nickname"
genesis 272 % (self.nickname, newnick))
genesis 273 else:
genesis 274 oldnickname = self.nickname
genesis 275 self.nickname = newnick
genesis 276 server.client_changed_nickname(self, oldnickname)
genesis 277 self.message_related(
genesis 278 ":%s!%s@%s NICK %s"
genesis 279 % (oldnickname, self.user, self.host, self.nickname),
genesis 280 True)
genesis 281
genesis 282 def notice_and_privmsg_handler():
genesis 283 if len(arguments) == 0:
genesis 284 self.reply("411 %s :No recipient given (%s)"
genesis 285 % (self.nickname, command))
genesis 286 return
genesis 287 if len(arguments) == 1:
genesis 288 self.reply("412 %s :No text to send" % self.nickname)
genesis 289 return
genesis 290 targetname = arguments[0]
genesis 291 message = arguments[1]
genesis 292 client = server.get_client(targetname)
genesis 293
genesis 294 if server.has_channel(targetname):
genesis 295 channel = server.get_channel(targetname)
genesis 296 self.message_channel(
genesis 297 channel, command, "%s :%s" % (channel.name, message))
9987-embargoing 298
9987-embargoing 299 self.server.station.infosec.message(
9987-embargoing 300 Message(
9987-embargoing 301 {
9987-embargoing 302 "speaker": self.nickname,
9987-embargoing 303 "command": BROADCAST,
9987-embargoing 304 "bounces": 0,
9987-embargoing 305 "body": message
9987-embargoing 306 }))
genesis 307 else:
9987-embargoing 308 self.server.station.infosec.message(Message({
genesis 309 "speaker": self.nickname,
genesis 310 "handle": targetname,
9987-embargoing 311 "body": message,
genesis 312 "bounces": 0,
genesis 313 "command": DIRECT
9987-embargoing 314 }))
genesis 315 if(client):
genesis 316 client.message(formatted_message)
genesis 317
genesis 318 def part_handler():
genesis 319 if len(arguments) < 1:
genesis 320 self.reply_461("PART")
genesis 321 return
genesis 322 if len(arguments) > 1:
genesis 323 partmsg = arguments[1]
genesis 324 else:
genesis 325 partmsg = self.nickname
genesis 326 for channelname in arguments[0].split(","):
genesis 327 if not valid_channel_re.match(channelname):
genesis 328 self.reply_403(channelname)
genesis 329 elif not irc_lower(channelname) in self.channels:
genesis 330 self.reply("442 %s %s :You're not on that channel"
genesis 331 % (self.nickname, channelname))
genesis 332 else:
genesis 333 channel = self.channels[irc_lower(channelname)]
genesis 334 self.message_channel(
genesis 335 channel, "PART", "%s :%s" % (channelname, partmsg),
genesis 336 True)
genesis 337 del self.channels[irc_lower(channelname)]
genesis 338 server.remove_member_from_channel(self, channelname)
genesis 339
genesis 340 def ping_handler():
genesis 341 if len(arguments) < 1:
genesis 342 self.reply("409 %s :No origin specified" % self.nickname)
genesis 343 return
genesis 344 self.reply("PONG %s :%s" % (server.name, arguments[0]))
genesis 345
genesis 346 def pong_handler():
genesis 347 pass
genesis 348
genesis 349 def quit_handler():
genesis 350 if len(arguments) < 1:
genesis 351 quitmsg = self.nickname
genesis 352 else:
genesis 353 quitmsg = arguments[0]
genesis 354 self.disconnect(quitmsg)
genesis 355
genesis 356 def topic_handler():
genesis 357 if len(arguments) < 1:
genesis 358 self.reply_461("TOPIC")
genesis 359 return
genesis 360 channelname = arguments[0]
genesis 361 channel = self.channels.get(irc_lower(channelname))
genesis 362 if channel:
genesis 363 if len(arguments) > 1:
genesis 364 newtopic = arguments[1]
genesis 365 channel.topic = newtopic
genesis 366 self.message_channel(
genesis 367 channel, "TOPIC", "%s :%s" % (channelname, newtopic),
genesis 368 True)
genesis 369 else:
genesis 370 if channel.topic:
genesis 371 self.reply("332 %s %s :%s"
genesis 372 % (self.nickname, channel.name,
genesis 373 channel.topic))
genesis 374 else:
genesis 375 self.reply("331 %s %s :No topic is set"
genesis 376 % (self.nickname, channel.name))
genesis 377 else:
genesis 378 self.reply("442 %s :You're not on that channel" % channelname)
genesis 379
genesis 380 def wallops_handler():
genesis 381 if len(arguments) < 1:
genesis 382 self.reply_461(command)
genesis 383 message = arguments[0]
genesis 384 for client in server.clients.values():
genesis 385 client.message(":%s NOTICE %s :Global notice: %s"
genesis 386 % (self.prefix, client.nickname, message))
genesis 387
genesis 388 def who_handler():
genesis 389 if len(arguments) < 1:
genesis 390 return
genesis 391 targetname = arguments[0]
genesis 392 if server.has_channel(targetname):
genesis 393 channel = server.get_channel(targetname)
genesis 394 for member in channel.members:
genesis 395 self.reply("352 %s %s %s %s %s %s H :0 %s"
genesis 396 % (self.nickname, targetname, member.user,
genesis 397 member.host, server.name, member.nickname,
genesis 398 member.realname))
genesis 399 self.reply("315 %s %s :End of WHO list"
genesis 400 % (self.nickname, targetname))
genesis 401
genesis 402 def whois_handler():
genesis 403 if len(arguments) < 1:
genesis 404 return
genesis 405 username = arguments[0]
genesis 406 user = server.get_client(username)
genesis 407 if user:
genesis 408 self.reply("311 %s %s %s %s * :%s"
genesis 409 % (self.nickname, user.nickname, user.user,
genesis 410 user.host, user.realname))
genesis 411 self.reply("312 %s %s %s :%s"
genesis 412 % (self.nickname, user.nickname, server.name,
genesis 413 server.name))
genesis 414 self.reply("319 %s %s :%s"
genesis 415 % (self.nickname, user.nickname,
genesis 416 " ".join(user.channels)))
genesis 417 self.reply("318 %s %s :End of WHOIS list"
genesis 418 % (self.nickname, user.nickname))
genesis 419 else:
genesis 420 self.reply("401 %s %s :No such nick"
genesis 421 % (self.nickname, username))
genesis 422
genesis 423 def wot_handler():
genesis 424 if len(arguments) < 1:
genesis 425
9987-embargoing 426 peers = self.state.get_peers()
9992-handle-edge-... 427 if len(peers) > 0:
9992-handle-edge-... 428 for peer in peers:
9987-embargoing 429 if peer.address and peer.port:
9987-embargoing 430 address = "%s:%s" % (peer.address, peer.port)
9987-embargoing 431 else:
9987-embargoing 432 address = "<address not configured>"
9987-embargoing 433 self.pest_reply("%s %s" % (string.join(peer.handles, ","), address))
9992-handle-edge-... 434 else:
9992-handle-edge-... 435 self.pest_reply("WOT is empty")
genesis 436 elif len(arguments) == 1:
genesis 437
genesis 438
genesis 439 handle = arguments[0]
9987-embargoing 440 peer = self.state.get_peer_by_handle(handle)
9992-handle-edge-... 441 if peer:
9992-handle-edge-... 442 self.pest_reply("keys:")
9992-handle-edge-... 443 for key in peer.keys:
9992-handle-edge-... 444 self.pest_reply("%s" % key)
9992-handle-edge-... 445 else:
9992-handle-edge-... 446 self.pest_reply("unknown peer: %s" % handle)
genesis 447
genesis 448 else:
genesis 449 pass
genesis 450
genesis 451 def peer_handler():
genesis 452 if len(arguments) == 1:
9991-improved-log... 453 try:
9987-embargoing 454 self.state.add_peer(arguments[0])
9991-improved-log... 455 self.pest_reply("added new peer %s" % arguments[0])
9989-show-wot-nicks 456 self.message(":%s JOIN %s" % (arguments[0], self.server.channel_name))
9991-improved-log... 457 except:
9991-improved-log... 458 self.pest_reply("error attempting to add peer %s" % arguments[0])
genesis 459 else:
genesis 460 self.pest_reply("Usage: PEER <HANDLE>")
genesis 461
genesis 462 def unpeer_handler():
genesis 463 if len(arguments) == 1:
genesis 464 try:
9987-embargoing 465 self.state.remove_peer(arguments[0])
genesis 466 self.pest_reply("removed peer %s" % arguments[0])
9989-show-wot-nicks 467 self.message(":%s PART %s" % (arguments[0], self.server.channel_name))
9992-handle-edge-... 468 except Exception, e:
9987-embargoing 469 logging.debug(e)
genesis 470 self.pest_reply("Error attempting to remove peer")
genesis 471 else:
genesis 472 self.pest_reply("Usage: UNPEER <HANDLE>")
genesis 473
genesis 474 def genkey_handler():
genesis 475 self.pest_reply(base64.b64encode(os.urandom(64)))
genesis 476
genesis 477 def key_handler():
genesis 478 if len(arguments) != 2:
genesis 479 self.pest_reply("Usage: KEY <HANDLE> <KEY>")
genesis 480 else:
genesis 481 handle = arguments[0]
genesis 482 key = arguments[1]
genesis 483 try:
9987-embargoing 484 self.state.add_key(handle, key)
9992-handle-edge-... 485 self.pest_reply("added key: %s" % key)
genesis 486 except:
genesis 487 self.pest_reply("Error attempting to add key")
genesis 488
genesis 489 def unkey_handler():
genesis 490 if len(arguments) != 1:
genesis 491 self.pest_reply("Usage: UNKEY <KEY>")
genesis 492 else:
genesis 493 try:
9987-embargoing 494 self.state.remove_key(arguments[0])
9992-handle-edge-... 495 self.pest_reply("removed key: %s" % arguments[0])
9992-handle-edge-... 496 except Exception, e:
genesis 497 self.pest_reply("Error attempting to remove key")
9987-embargoing 498 logging.debug(e)
genesis 499
genesis 500 def at_handler():
genesis 501 if len(arguments) == 0:
9987-embargoing 502 at = self.state.get_at()
genesis 503 elif len(arguments) == 1:
genesis 504 handle = arguments[0]
9987-embargoing 505 at = self.state.get_at(handle)
genesis 506 elif len(arguments) == 2:
9990-keep-ephemer... 507 try:
9990-keep-ephemer... 508 handle, address = arguments
9990-keep-ephemer... 509 address_ip, port = string.split(address, ":")
9987-embargoing 510 self.state.update_at({"handle": handle,
9990-keep-ephemer... 511 "address": address_ip,
9990-keep-ephemer... 512 "port": port},
9990-keep-ephemer... 513 False)
9990-keep-ephemer... 514 self.pest_reply("updated address table: %s %s" % (handle, address))
9990-keep-ephemer... 515 except Exception as ex:
9990-keep-ephemer... 516 self.pest_reply("Error attempting to update address table")
9990-keep-ephemer... 517 stack = traceback.format_exc()
9986-rebroadcast-... 518 logging.debug(stack)
genesis 519 return
genesis 520 elif len(arguments) > 2:
genesis 521 self.pest_reply("Usage: AT [<HANDLE>] [<ADDRESS>]")
genesis 522 return
9992-handle-edge-... 523 if len(at) > 0:
9992-handle-edge-... 524 for address in at:
9992-handle-edge-... 525 self.pest_reply("%s %s %s" % (address["handle"],
9992-handle-edge-... 526 address["address"],
9991-improved-log... 527 address["active_at"]))
9992-handle-edge-... 528 else:
9992-handle-edge-... 529 self.pest_reply("no results")
genesis 530
genesis 531 handler_table = {
genesis 532 "AWAY": away_handler,
genesis 533 "AT": at_handler,
genesis 534 "GENKEY": genkey_handler,
genesis 535 "ISON": ison_handler,
genesis 536 "JOIN": join_handler,
genesis 537 "KEY": key_handler,
genesis 538 "LIST": list_handler,
genesis 539 "LUSERS": lusers_handler,
genesis 540 "MODE": mode_handler,
genesis 541 "MOTD": motd_handler,
genesis 542 "NICK": nick_handler,
genesis 543 "NOTICE": notice_and_privmsg_handler,
genesis 544 "PART": part_handler,
genesis 545 "PEER": peer_handler,
genesis 546 "PING": ping_handler,
genesis 547 "PONG": pong_handler,
genesis 548 "PRIVMSG": notice_and_privmsg_handler,
genesis 549 "QUIT": quit_handler,
genesis 550 "TOPIC": topic_handler,
genesis 551 "UNKEY": unkey_handler,
genesis 552 "UNPEER": unpeer_handler,
genesis 553 "WALLOPS": wallops_handler,
genesis 554 "WHO": who_handler,
genesis 555 "WHOIS": whois_handler,
genesis 556 "WOT": wot_handler
genesis 557 }
genesis 558 server = self.server
genesis 559 valid_channel_re = self.__valid_channelname_regexp
genesis 560 try:
genesis 561 handler_table[command]()
genesis 562 except KeyError:
9990-keep-ephemer... 563 self.reply("421 %s %s :Unknown command" % (self.nickname, command))
9990-keep-ephemer... 564 stack = traceback.format_exc()
9986-rebroadcast-... 565 logging.debug(stack)
genesis 566
genesis 567 def socket_readable_notification(self):
genesis 568 try:
genesis 569 data = self.socket.recv(2 ** 10)
9987-embargoing 570 logging.debug(
genesis 571 "[%s:%d] -> %r" % (self.host, self.port, data))
genesis 572 quitmsg = "EOT"
genesis 573 except socket.error as x:
genesis 574 data = ""
genesis 575 quitmsg = x
genesis 576 if data:
genesis 577 self.__readbuffer += data
genesis 578 self.__parse_read_buffer()
genesis 579 self.__timestamp = time.time()
genesis 580 self.__sent_ping = False
genesis 581 else:
genesis 582 self.disconnect(quitmsg)
genesis 583
genesis 584 def socket_writable_notification(self):
genesis 585 try:
genesis 586 sent = self.socket.send(self.__writebuffer)
9987-embargoing 587 logging.debug(
genesis 588 "[%s:%d] <- %r" % (
genesis 589 self.host, self.port, self.__writebuffer[:sent]))
genesis 590 self.__writebuffer = self.__writebuffer[sent:]
genesis 591 except socket.error as x:
genesis 592 self.disconnect(x)
genesis 593
genesis 594 def disconnect(self, quitmsg):
genesis 595 self.message("ERROR :%s" % quitmsg)
9987-embargoing 596 logging.info(
genesis 597 "Disconnected connection from %s:%s (%s)." % (
genesis 598 self.host, self.port, quitmsg))
genesis 599 self.socket.close()
genesis 600 self.server.remove_client(self, quitmsg)
genesis 601
genesis 602 def message(self, msg):
genesis 603 self.__writebuffer += msg + "\r\n"
genesis 604
genesis 605 def reply(self, msg):
genesis 606 self.message(":%s %s" % (self.server.name, msg))
genesis 607
genesis 608 def pest_reply(self, msg):
genesis 609 self.message("NOTICE %s :%s" % (self.nickname, msg))
genesis 610
genesis 611 def reply_403(self, channel):
genesis 612 self.reply("403 %s %s :No such channel" % (self.nickname, channel))
genesis 613
genesis 614 def reply_461(self, command):
genesis 615 nickname = self.nickname or "*"
genesis 616 self.reply("461 %s %s :Not enough parameters" % (nickname, command))
genesis 617
genesis 618 def message_channel(self, channel, command, message, include_self=False):
genesis 619 line = ":%s %s %s" % (self.prefix, command, message)
9987-embargoing 620 if include_self:
9987-embargoing 621 self.message(line)
genesis 622
genesis 623 def message_related(self, msg, include_self=False):
genesis 624 clients = set()
genesis 625 if include_self:
genesis 626 clients.add(self)
genesis 627 for channel in self.channels.values():
genesis 628 clients |= channel.members
genesis 629 if not include_self:
genesis 630 clients.discard(self)
genesis 631 for client in clients:
genesis 632 client.message(msg)
genesis 633
genesis 634 def send_motd(self):
genesis 635 server = self.server
genesis 636 motdlines = server.get_motd_lines()
genesis 637 if motdlines:
genesis 638 self.reply("375 %s :- %s Message of the day -"
genesis 639 % (self.nickname, server.name))
genesis 640 for line in motdlines:
genesis 641 self.reply("372 %s :- %s" % (self.nickname, line.rstrip()))
genesis 642 self.reply("376 %s :End of /MOTD command" % self.nickname)
genesis 643 else:
genesis 644 self.reply("422 %s :MOTD File is missing" % self.nickname)