X-Git-Url: http://git.tremily.us/?a=blobdiff_plain;f=irkerd;h=0893381cb6a3cddfcc09b37d4158ab03b0c6d030;hb=c9f6f03abd219db14cf619200a4ca5ba963efc0e;hp=35632cf80c9b6a6b96e03716720f1fbb1a558e47;hpb=927256505d39c5501aebc9a7d70c28eaab1ee465;p=irker.git diff --git a/irkerd b/irkerd index 35632cf..0893381 100755 --- a/irkerd +++ b/irkerd @@ -12,23 +12,21 @@ a list of such strings; in the latter case the message is broadcast to all listed channels. Note that the channel portion of the URL need *not* have a leading '#' unless the channel name itself does. -Options: -d sets the debug-message level (probably only of interest to -developers). The -V option prints the program version and exits. - Design and code by Eric S. Raymond . See the project resource page at . -Requires Python 2.6 or 2.5 with the simplejson library installed, and -the irc client library at version >= 3.2.2 which requires 2.7: see - -http://pypi.python.org/pypi/irc/ +Requires Python 2.7, or: +* 2.6 with the argparse package installed. """ + +from __future__ import unicode_literals +from __future__ import with_statement + # These things might need tuning HOST = "localhost" PORT = 6659 -NAMESTYLE = "irker%03d" # IRC nick template - must contain '%d' XMIT_TTL = (3 * 60 * 60) # Time to live, seconds from last transmit PING_TTL = (15 * 60) # Time to live, seconds from last PING HANDSHAKE_TTL = 60 # Time to live, seconds from nick transmit @@ -36,21 +34,50 @@ CHANNEL_TTL = (3 * 60 * 60) # Time to live, seconds from last transmit DISCONNECT_TTL = (24 * 60 * 60) # Time to live, seconds from last connect UNSEEN_TTL = 60 # Time to live, seconds since first request CHANNEL_MAX = 18 # Max channels open per socket (default) -ANTI_FLOOD_DELAY = 0.5 # Anti-flood delay after transmissions, seconds +ANTI_FLOOD_DELAY = 1.0 # Anti-flood delay after transmissions, seconds ANTI_BUZZ_DELAY = 0.09 # Anti-buzz delay after queue-empty check CONNECTION_MAX = 200 # To avoid hitting a thread limit # No user-serviceable parts below this line -version = "1.12" +version = "2.7" + +import argparse +import logging +import json +try: # Python 3 + import queue +except ImportError: # Python 2 + import Queue as queue +import random +import re +import select +import signal +import socket +try: # Python 3 + import socketserver +except ImportError: # Python 2 + import SocketServer as socketserver +import ssl +import sys +import threading +import time +import traceback +try: # Python 3 + import urllib.parse as urllib_parse +except ImportError: # Python 2 + import urlparse as urllib_parse + + +LOG = logging.getLogger(__name__) +LOG.setLevel(logging.ERROR) +LOG_LEVELS = ['critical', 'error', 'warning', 'info', 'debug'] + +try: # Python 2 + UNICODE_TYPE = unicode +except NameError: # Python 3 + UNICODE_TYPE = str -import sys, getopt, urlparse, time, random, socket, signal -import threading, Queue, SocketServer -import irc.client, logging -try: - import simplejson as json # Faster, also makes us Python-2.4-compatible -except ImportError: - import json # Sketch of implementation: # @@ -85,19 +112,333 @@ except ImportError: # same problem - there is little point in reliable delivery to a relay # that is down or unreliable. # -# This code uses only NICK, JOIN, PART, MODE, and PRIVMSG. It is strictly -# compliant to RFC1459, except for the interpretation and use of the -# DEAF and CHANLIMIT and (obsolete) MAXCHANNELS features. CHANLIMIT -# is as described in the Internet RFC draft +# This code uses only NICK, JOIN, PART, MODE, PRIVMSG, USER, and QUIT. +# It is strictly compliant to RFC1459, except for the interpretation and +# use of the DEAF and CHANLIMIT and (obsolete) MAXCHANNELS features. +# +# CHANLIMIT is as described in the Internet RFC draft # draft-brocklesby-irc-isupport-03 at . # The ",isnick" feature is as described in # . +# Historical note: the IRCClient and IRCServerConnection classes +# (~270LOC) replace the overweight, overcomplicated 3KLOC mass of +# irclib code that irker formerly used as a service library. They +# still look similar to parts of irclib because I contributed to that +# code before giving up on it. + +class IRCError(Exception): + "An IRC exception" + pass + + +class InvalidRequest (ValueError): + "An invalid JSON request" + pass + + +class IRCClient(): + "An IRC client session to one or more servers." + def __init__(self): + self.mutex = threading.RLock() + self.server_connections = [] + self.event_handlers = {} + self.add_event_handler("ping", + lambda c, e: c.ship("PONG %s" % e.target)) + + def newserver(self): + "Initialize a new server-connection object." + conn = IRCServerConnection(self) + with self.mutex: + self.server_connections.append(conn) + return conn + + def spin(self, timeout=0.2): + "Spin processing data from connections forever." + # Outer loop should specifically *not* be mutex-locked. + # Otherwise no other thread would ever be able to change + # the shared state of an IRC object running this function. + while True: + nextsleep = 0 + with self.mutex: + connected = [x for x in self.server_connections + if x is not None and x.socket is not None] + sockets = [x.socket for x in connected] + if sockets: + connmap = dict([(c.socket.fileno(), c) for c in connected]) + (insocks, _o, _e) = select.select(sockets, [], [], timeout) + for s in insocks: + connmap[s.fileno()].consume() + else: + nextsleep = timeout + time.sleep(nextsleep) + + def add_event_handler(self, event, handler): + "Set a handler to be called later." + with self.mutex: + event_handlers = self.event_handlers.setdefault(event, []) + event_handlers.append(handler) + + def handle_event(self, connection, event): + with self.mutex: + h = self.event_handlers + th = sorted(h.get("all_events", []) + h.get(event.type, [])) + for handler in th: + handler(connection, event) + + def drop_connection(self, connection): + with self.mutex: + self.server_connections.remove(connection) + + +class LineBufferedStream(): + "Line-buffer a read stream." + _crlf_re = re.compile(b'\r?\n') + + def __init__(self): + self.buffer = b'' + + def append(self, newbytes): + self.buffer += newbytes + + def lines(self): + "Iterate over lines in the buffer." + lines = self._crlf_re.split(self.buffer) + self.buffer = lines.pop() + return iter(lines) + + def __iter__(self): + return self.lines() + +class IRCServerConnectionError(IRCError): + pass + +class IRCServerConnection(): + command_re = re.compile("^(:(?P[^ ]+) +)?(?P[^ ]+)( *(?P .+))?") + # The full list of numeric-to-event mappings is in Perl's Net::IRC. + # We only need to ensure that if some ancient server throws numerics + # for the ones we actually want to catch, they're mapped. + codemap = { + "001": "welcome", + "005": "featurelist", + "432": "erroneusnickname", + "433": "nicknameinuse", + "436": "nickcollision", + "437": "unavailresource", + } + + def __init__(self, master): + self.master = master + self.socket = None + + def _wrap_socket(self, socket, target, cafile=None, + protocol=ssl.PROTOCOL_TLSv1): + try: # Python 3.2 and greater + ssl_context = ssl.SSLContext(protocol) + except AttributeError: # Python < 3.2 + self.socket = ssl.wrap_socket( + socket, cert_reqs=ssl.CERT_REQUIRED, + ssl_version=protocol, ca_certs=cafile) + else: + ssl_context.verify_mode = ssl.CERT_REQUIRED + if cafile: + ssl_context.load_verify_locations(cafile=cafile) + else: + ssl_context.set_default_verify_paths() + kwargs = {} + if ssl.HAS_SNI: + kwargs['server_hostname'] = target.servername + self.socket = ssl_context.wrap_socket(socket, **kwargs) + return self.socket + + def _check_hostname(self, target): + if hasattr(ssl, 'match_hostname'): # Python >= 3.2 + cert = self.socket.getpeercert() + try: + ssl.match_hostname(cert, target.servername) + except ssl.CertificateError as e: + raise IRCServerConnectionError( + 'Invalid SSL/TLS certificate: %s' % e) + else: # Python < 3.2 + LOG.warning( + 'cannot check SSL/TLS hostname with Python %s' % sys.version) + + def connect(self, target, nickname, username=None, realname=None, + **kwargs): + LOG.debug("connect(server=%r, port=%r, nickname=%r, ...)" % ( + target.servername, target.port, nickname)) + if self.socket is not None: + self.disconnect("Changing servers") + + self.buffer = LineBufferedStream() + self.event_handlers = {} + self.real_server_name = "" + self.target = target + self.nickname = nickname + try: + self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + if target.ssl: + self.socket = self._wrap_socket( + socket=self.socket, target=target, **kwargs) + self.socket.bind(('', 0)) + self.socket.connect((target.servername, target.port)) + except socket.error as err: + raise IRCServerConnectionError("Couldn't connect to socket: %s" % err) + + if target.ssl: + self._check_hostname(target=target) + if target.password: + self.ship("PASS " + target.password) + self.nick(self.nickname) + self.user( + username=target.username or username or 'irker', + realname=realname or 'irker relaying client') + return self + + def close(self): + # Without this thread lock, there is a window during which + # select() can find a closed socket, leading to an EBADF error. + with self.master.mutex: + self.disconnect("Closing object") + self.master.drop_connection(self) + + def consume(self): + try: + incoming = self.socket.recv(16384) + except socket.error: + # Server hung up on us. + self.disconnect("Connection reset by peer") + return + if not incoming: + # Dead air also indicates a connection reset. + self.disconnect("Connection reset by peer") + return + + self.buffer.append(incoming) + + for line in self.buffer: + if not isinstance(line, UNICODE_TYPE): + line = UNICODE_TYPE(line, 'utf-8') + LOG.debug("FROM: %s" % line) + + if not line: + continue + + prefix = None + command = None + arguments = None + self.handle_event(Event("every_raw_message", + self.real_server_name, + None, + [line])) + + m = IRCServerConnection.command_re.match(line) + if m.group("prefix"): + prefix = m.group("prefix") + if not self.real_server_name: + self.real_server_name = prefix + if m.group("command"): + command = m.group("command").lower() + if m.group("argument"): + a = m.group("argument").split(" :", 1) + arguments = a[0].split() + if len(a) == 2: + arguments.append(a[1]) + + command = IRCServerConnection.codemap.get(command, command) + if command in ["privmsg", "notice"]: + target = arguments.pop(0) + else: + target = None + + if command == "quit": + arguments = [arguments[0]] + elif command == "ping": + target = arguments[0] + else: + target = arguments[0] + arguments = arguments[1:] + + LOG.debug("command: %s, source: %s, target: %s, arguments: %s" % ( + command, prefix, target, arguments)) + self.handle_event(Event(command, prefix, target, arguments)) + + def handle_event(self, event): + self.master.handle_event(self, event) + if event.type in self.event_handlers: + for fn in self.event_handlers[event.type]: + fn(self, event) + + def is_connected(self): + return self.socket is not None + + def disconnect(self, message=""): + if self.socket is None: + return + # Don't send a QUIT here - causes infinite loop! + try: + self.socket.shutdown(socket.SHUT_WR) + self.socket.close() + except socket.error: + pass + del self.socket + self.socket = None + self.handle_event( + Event("disconnect", self.target.server, "", [message])) + + def join(self, channel, key=""): + self.ship("JOIN %s%s" % (channel, (key and (" " + key)))) + + def mode(self, target, command): + self.ship("MODE %s %s" % (target, command)) + + def nick(self, newnick): + self.ship("NICK " + newnick) + + def part(self, channel, message=""): + cmd_parts = ['PART', channel] + if message: + cmd_parts.append(message) + self.ship(' '.join(cmd_parts)) + + def privmsg(self, target, text): + self.ship("PRIVMSG %s :%s" % (target, text)) + + def quit(self, message=""): + self.ship("QUIT" + (message and (" :" + message))) + + def user(self, username, realname): + self.ship("USER %s 0 * :%s" % (username, realname)) + + def ship(self, string): + "Ship a command to the server, appending CR/LF" + try: + self.socket.send(string.encode('utf-8') + b'\r\n') + LOG.debug("TO: %s" % string) + except socket.error: + self.disconnect("Connection reset by peer.") + +class Event(object): + def __init__(self, evtype, source, target, arguments=None): + self.type = evtype + self.source = source + self.target = target + if arguments is None: + arguments = [] + self.arguments = arguments + +def is_channel(string): + return string and string[0] in "#&+!" + class Connection: - def __init__(self, irkerd, servername, port): - self.irker = irkerd - self.servername = servername - self.port = port + def __init__(self, irker, target, nick_template, nick_needs_number=False, + password=None, **kwargs): + self.irker = irker + self.target = target + self.nick_template = nick_template + self.nick_needs_number = nick_needs_number + self.password = password + self.kwargs = kwargs self.nick_trial = None self.connection = None self.status = None @@ -106,56 +447,68 @@ class Connection: self.channels_joined = {} self.channel_limits = {} # The consumer thread - self.queue = Queue.Queue() + self.queue = queue.Queue() self.thread = None def nickname(self, n=None): "Return a name for the nth server connection." if n is None: n = self.nick_trial - return (NAMESTYLE % n) + if self.nick_needs_number: + return (self.nick_template % n) + else: + return self.nick_template def handle_ping(self): "Register the fact that the server has pinged this connection." self.last_ping = time.time() def handle_welcome(self): "The server says we're OK, with a non-conflicting nick." self.status = "ready" - self.irker.debug(1, "nick %s accepted" % self.nickname()) + LOG.info("nick %s accepted" % self.nickname()) + if self.password: + self.connection.privmsg("nickserv", "identify %s" % self.password) def handle_badnick(self): - "The server says our nick has a conflict." - self.irker.debug(1, "nick %s rejected" % self.nickname()) - # Randomness prevents a malicious user or bot from antcipating the - # next trial name in order to block us from completing the handshake. - self.nick_trial += random.randint(1, 3) - self.last_xmit = time.time() - self.connection.nick(self.nickname()) + "The server says our nick is ill-formed or has a conflict." + LOG.info("nick %s rejected" % self.nickname()) + if self.nick_needs_number: + # Randomness prevents a malicious user or bot from + # anticipating the next trial name in order to block us + # from completing the handshake. + self.nick_trial += random.randint(1, 3) + self.last_xmit = time.time() + self.connection.nick(self.nickname()) + # Otherwise fall through, it might be possible to + # recover manually. def handle_disconnect(self): "Server disconnected us for flooding or some other reason." self.connection = None - self.status = "disconnected" + if self.status != "expired": + self.status = "disconnected" def handle_kick(self, outof): "We've been kicked." self.status = "handshaking" try: del self.channels_joined[outof] except KeyError: - self.irker.logerr("kicked by %s from %s that's not joined" - % (self.servername, outof)) + LOG.error("kicked by %s from %s that's not joined" % ( + self.target, outof)) qcopy = [] while not self.queue.empty(): - (channel, message) = self.queue.get() + (channel, message, key) = self.queue.get() if channel != outof: - qcopy.append((channel, message)) - for (channel, message) in qcopy: - self.queue.put((channel, message)) + qcopy.append((channel, message, key)) + for (channel, message, key) in qcopy: + self.queue.put((channel, message, key)) self.status = "ready" - def enqueue(self, channel, message): + def enqueue(self, channel, message, key, quit_after=False): "Enque a message for transmission." if self.thread is None or not self.thread.is_alive(): self.status = "unseen" self.thread = threading.Thread(target=self.dequeue) self.thread.setDaemon(True) self.thread.start() - self.queue.put((channel, message)) + self.queue.put((channel, message, key)) + if quit_after: + self.queue.put((channel, None, key)) def dequeue(self): "Try to ship pending messages from the queue." try: @@ -178,7 +531,11 @@ class Connection: self.status = "expired" break elif xmit_timeout or ping_timeout: - self.irker.debug(1, "timing out connection to %s at %s (ping_timeout=%s, xmit_timeout=%s)" % (self.servername, time.asctime(), ping_timeout, xmit_timeout)) + LOG.info(( + "timing out connection to %s at %s " + "(ping_timeout=%s, xmit_timeout=%s)") % ( + self.target, time.asctime(), ping_timeout, + xmit_timeout)) with self.irker.irc.mutex: self.connection.context = None self.connection.quit("transmission timeout") @@ -198,28 +555,30 @@ class Connection: # space forever would be a memory leak. self.status = "expired" break - elif not self.connection: + elif not self.connection and self.status != "expired": # Queue is nonempty but server isn't connected. with self.irker.irc.mutex: - self.connection = self.irker.irc.server() + self.connection = self.irker.irc.newserver() self.connection.context = self # Try to avoid colliding with other instances self.nick_trial = random.randint(1, 990) self.channels_joined = {} try: # This will throw - # irc.client.ServerConnectionError on failure - self.connection.connect(self.servername, - self.port, - nickname=self.nickname(), - username="irker", - ircname="irker relaying client") + # IRCServerConnectionError on failure + self.connection.connect( + target=self.target, + nickname=self.nickname(), + **self.kwargs) self.status = "handshaking" - self.irker.debug(1, "XMIT_TTL bump (%s connection) at %s" % (self.servername, time.asctime())) + LOG.info("XMIT_TTL bump (%s connection) at %s" % ( + self.target, time.asctime())) self.last_xmit = time.time() self.last_ping = time.time() - except irc.client.ServerConnectionError: - self.status = "disconnected" + except IRCServerConnectionError as e: + LOG.error(e) + self.status = "expired" + break elif self.status == "handshaking": if time.time() > self.last_xmit + HANDSHAKE_TTL: self.status = "expired" @@ -238,25 +597,54 @@ class Connection: self.status = "expired" break elif self.status == "ready": - with self.irker.irc.mutex: - (channel, message) = self.queue.get() - if channel not in self.channels_joined: - self.connection.join(channel) - self.irker.debug(1, "joining %s on %s." % (channel, self.servername)) + (channel, message, key) = self.queue.get() + if channel not in self.channels_joined: + self.connection.join(channel, key=key) + LOG.info("joining %s on %s." % (channel, self.target)) + # None is magic - it's a request to quit the server + if message is None: + self.connection.quit() + # An empty message might be used as a keepalive or + # to join a channel for logging, so suppress the + # privmsg send unless there is actual traffic. + elif message: for segment in message.split("\n"): - self.connection.privmsg(channel, segment) + # Truncate the message if it's too long, + # but we're working with characters here, + # not bytes, so we could be off. + # 500 = 512 - CRLF - 'PRIVMSG ' - ' :' + maxlength = 500 - len(channel) + if len(segment) > maxlength: + segment = segment[:maxlength] + try: + self.connection.privmsg(channel, segment) + except ValueError as err: + LOG.warning(( + "irclib rejected a message to %s on %s " + "because: %s") % ( + channel, self.target, UNICODE_TYPE(err))) + LOG.debug(traceback.format_exc()) time.sleep(ANTI_FLOOD_DELAY) - self.last_xmit = self.channels_joined[channel] = time.time() - self.irker.debug(1, "XMIT_TTL bump (%s transmission) at %s" % (self.servername, time.asctime())) - self.queue.task_done() - except: - (exc_type, _exc_value, exc_traceback) = sys.exc_info() - self.irker.logerr("exception %s in thread for %s" % \ - (exc_type, self.servername)) - # This is so we can see tracebacks for errors inside the thread - # when we need to be able to for debugging purposes. - if debuglvl > 0: - raise exc_type, _exc_value, exc_traceback + self.last_xmit = self.channels_joined[channel] = time.time() + LOG.info("XMIT_TTL bump (%s transmission) at %s" % ( + self.target, time.asctime())) + self.queue.task_done() + elif self.status == "expired": + LOG.error( + "We're expired but still running! This is a bug.") + break + except Exception as e: + LOG.error("exception %s in thread for %s" % (e, self.target)) + # Maybe this should have its own status? + self.status = "expired" + LOG.debug(traceback.format_exc()) + finally: + try: + # Make sure we don't leave any zombies behind + self.connection.close() + except: + # Irclib has a habit of throwing fresh exceptions here. Ignore that + pass def live(self): "Should this connection not be scavenged?" return self.status != "expired" @@ -280,38 +668,59 @@ class Connection: class Target(): "Represent a transmission target." def __init__(self, url): - parsed = urlparse.urlparse(url) - irchost, _, ircport = parsed.netloc.partition(':') - if not ircport: - ircport = 6667 - self.servername = irchost + self.url = url + parsed = urllib_parse.urlparse(url) + self.ssl = parsed.scheme == 'ircs' + if self.ssl: + default_ircport = 6697 + else: + default_ircport = 6667 + self.username = parsed.username + self.password = parsed.password + self.servername = parsed.hostname + self.port = parsed.port or default_ircport # IRC channel names are case-insensitive. If we don't smash # case here we may run into problems later. There was a bug # observed on irc.rizon.net where an irkerd user specified #Channel, # got kicked, and irkerd crashed because the server returned # "#channel" in the notification that our kick handler saw. self.channel = parsed.path.lstrip('/').lower() + # This deals with a tweak in recent versions of urlparse. + if parsed.fragment: + self.channel += "#" + parsed.fragment isnick = self.channel.endswith(",isnick") if isnick: self.channel = self.channel[:-7] if self.channel and not isnick and self.channel[0] not in "#&+": self.channel = "#" + self.channel - self.port = int(ircport) - def valid(self): - "Both components must be present for a valid target." - return self.servername and self.channel + # support both channel?secret and channel?key=secret + self.key = "" + if parsed.query: + self.key = re.sub("^key=", "", parsed.query) + + def __str__(self): + "Represent this instance as a string" + return self.servername or self.url or repr(self) + + def validate(self): + "Raise InvalidRequest if the URL is missing a critical component" + if not self.servername: + raise InvalidRequest( + 'target URL missing a servername: %r' % self.url) + if not self.channel: + raise InvalidRequest( + 'target URL missing a channel: %r' % self.url) def server(self): "Return a hashable tuple representing the destination server." return (self.servername, self.port) class Dispatcher: "Manage connections to a particular server-port combination." - def __init__(self, irkerd, servername, port): - self.irker = irkerd - self.servername = servername - self.port = port + def __init__(self, irker, **kwargs): + self.irker = irker + self.kwargs = kwargs self.connections = [] - def dispatch(self, channel, message): + def dispatch(self, channel, message, key, quit_after=False): "Dispatch messages for our server-port combination." # First, check if there is room for another channel # on any of our existing connections. @@ -319,7 +728,7 @@ class Dispatcher: eligibles = [x for x in connections if x.joined_to(channel)] \ or [x for x in connections if x.accepting(channel)] if eligibles: - eligibles[0].enqueue(channel, message) + eligibles[0].enqueue(channel, message, key, quit_after) return # All connections are full up. Look for one old enough to be # scavenged. @@ -334,48 +743,45 @@ class Dispatcher: found_connection.part(drop_channel, "scavenged by irkerd") del found_connection.channels_joined[drop_channel] #time.sleep(ANTI_FLOOD_DELAY) - found_connection.enqueue(channel, message) + found_connection.enqueue(channel, message, key, quit_after) return - # Didn't find any channels with no recent activity - newconn = Connection(self.irker, - self.servername, - self.port) + # All existing channels had recent activity + newconn = Connection(self.irker, **self.kwargs) self.connections.append(newconn) - newconn.enqueue(channel, message) + newconn.enqueue(channel, message, key, quit_after) def live(self): "Does this server-port combination have any live connections?" self.connections = [x for x in self.connections if x.live()] return len(self.connections) > 0 + def pending(self): + "Return all connections with pending traffic." + return [x for x in self.connections if not x.queue.empty()] def last_xmit(self): "Return the time of the most recent transmission." return max(x.last_xmit for x in self.connections) class Irker: "Persistent IRC multiplexer." - def __init__(self, debuglevel=0): - self.debuglevel = debuglevel - self.irc = irc.client.IRC() - self.irc.add_global_handler("ping", self._handle_ping) - self.irc.add_global_handler("welcome", self._handle_welcome) - self.irc.add_global_handler("erroneusnickname", self._handle_badnick) - self.irc.add_global_handler("nicknameinuse", self._handle_badnick) - self.irc.add_global_handler("nickcollision", self._handle_badnick) - self.irc.add_global_handler("unavailresource", self._handle_badnick) - self.irc.add_global_handler("featurelist", self._handle_features) - self.irc.add_global_handler("disconnect", self._handle_disconnect) - self.irc.add_global_handler("kick", self._handle_kick) - thread = threading.Thread(target=self.irc.process_forever) + def __init__(self, logfile=None, **kwargs): + self.logfile = logfile + self.kwargs = kwargs + self.irc = IRCClient() + self.irc.add_event_handler("ping", self._handle_ping) + self.irc.add_event_handler("welcome", self._handle_welcome) + self.irc.add_event_handler("erroneusnickname", self._handle_badnick) + self.irc.add_event_handler("nicknameinuse", self._handle_badnick) + self.irc.add_event_handler("nickcollision", self._handle_badnick) + self.irc.add_event_handler("unavailresource", self._handle_badnick) + self.irc.add_event_handler("featurelist", self._handle_features) + self.irc.add_event_handler("disconnect", self._handle_disconnect) + self.irc.add_event_handler("kick", self._handle_kick) + self.irc.add_event_handler("every_raw_message", self._handle_every_raw_message) + self.servers = {} + def thread_launch(self): + thread = threading.Thread(target=self.irc.spin) thread.setDaemon(True) self.irc._thread = thread thread.start() - self.servers = {} - def logerr(self, errmsg): - "Log a processing error." - sys.stderr.write("irkerd: " + errmsg + "\n") - def debug(self, level, errmsg): - "Debugging information." - if self.debuglevel >= level: - sys.stderr.write("irkerd: %s\n" % errmsg) def _handle_ping(self, connection, _event): "PING arrived, bump the last-received time for the connection." if connection.context: @@ -392,15 +798,16 @@ class Irker: "Determine if and how we can set deaf mode." if connection.context: cxt = connection.context - for lump in event.arguments(): + arguments = event.arguments + for lump in arguments: if lump.startswith("DEAF="): - connection.mode(cxt.nickname(), "+"+lump[5:]) + if not self.logfile: + connection.mode(cxt.nickname(), "+"+lump[5:]) elif lump.startswith("MAXCHANNELS="): m = int(lump[12:]) for pref in "#&+": cxt.channel_limits[pref] = m - self.debug(1, "%s maxchannels is %d" - % (connection.server, m)) + LOG.info("%s maxchannels is %d" % (connection.server, m)) elif lump.startswith("CHANLIMIT=#:"): limits = lump[10:].split(",") try: @@ -409,115 +816,198 @@ class Irker: limit = int(limit) for c in prefixes: cxt.channel_limits[c] = limit - self.debug(1, "%s channel limit map is %s" - % (connection.server, cxt.channel_limits)) + LOG.info("%s channel limit map is %s" % ( + connection.target, cxt.channel_limits)) except ValueError: - self.logerr("ill-formed CHANLIMIT property") + LOG.error("ill-formed CHANLIMIT property") def _handle_disconnect(self, connection, _event): "Server hung up the connection." - self.debug(1, "server %s disconnected" % connection.server) + LOG.info("server %s disconnected" % connection.target) connection.close() if connection.context: connection.context.handle_disconnect() def _handle_kick(self, connection, event): "Server hung up the connection." - self.debug(1, "irker has been kicked from %s on %s" % (event.target(), connection.server)) + target = event.target + LOG.info("irker has been kicked from %s on %s" % ( + target, connection.target)) if connection.context: - connection.context.handle_kick(event.target()) - def handle(self, line): + connection.context.handle_kick(target) + def _handle_every_raw_message(self, _connection, event): + "Log all messages when in watcher mode." + if self.logfile: + with open(self.logfile, "a") as logfp: + logfp.write("%03f|%s|%s\n" % \ + (time.time(), event.source, event.arguments[0])) + def pending(self): + "Do we have any pending message traffic?" + return [k for (k, v) in self.servers.items() if v.pending()] + + def _parse_request(self, line): + "Request-parsing helper for the handle() method" + request = json.loads(line.strip()) + if not isinstance(request, dict): + raise InvalidRequest( + "request is not a JSON dictionary: %r" % request) + if "to" not in request or "privmsg" not in request: + raise InvalidRequest( + "malformed request - 'to' or 'privmsg' missing: %r" % request) + channels = request['to'] + message = request['privmsg'] + if not isinstance(channels, (list, UNICODE_TYPE)): + raise InvalidRequest( + "malformed request - unexpected channel type: %r" % channels) + if not isinstance(message, UNICODE_TYPE): + raise InvalidRequest( + "malformed request - unexpected message type: %r" % message) + if not isinstance(channels, list): + channels = [channels] + targets = [] + for url in channels: + try: + if not isinstance(url, UNICODE_TYPE): + raise InvalidRequest( + "malformed request - URL has unexpected type: %r" % + url) + target = Target(url) + target.validate() + except InvalidRequest as e: + LOG.error(UNICODE_TYPE(e)) + else: + targets.append(target) + return (targets, message) + + def handle(self, line, quit_after=False): "Perform a JSON relay request." try: - request = json.loads(line.strip()) - if not isinstance(request, dict): - self.logerr("request is not a JSON dictionary: %r" % request) - elif "to" not in request or "privmsg" not in request: - self.logerr("malformed request - 'to' or 'privmsg' missing: %r" % request) - else: - channels = request['to'] - message = request['privmsg'] - if not isinstance(channels, (list, basestring)): - self.logerr("malformed request - unexpected channel type: %r" % channels) - if not isinstance(message, basestring): - self.logerr("malformed request - unexpected message type: %r" % message) - else: - if not isinstance(channels, list): - channels = [channels] - for url in channels: - if not isinstance(url, basestring): - self.logerr("malformed request - URL has unexpected type: %r" % url) - else: - target = Target(url) - if not target.valid(): - return - if target.server() not in self.servers: - self.servers[target.server()] = Dispatcher(self, target.servername, target.port) - self.servers[target.server()].dispatch(target.channel, message) - # GC dispatchers with no active connections - servernames = self.servers.keys() - for servername in servernames: - if not self.servers[servername].live(): - del self.servers[servername] - # If we might be pushing a resource limit - # even after garbage collection, remove a - # session. The goal here is to head off - # DoS attacks that aim at exhausting - # thread space or file descriptors. The - # cost is that attempts to DoS this - # service will cause lots of join/leave - # spam as we scavenge old channels after - # connecting to new ones. The particular - # method used for selecting a session to - # be terminated doesn't matter much; we - # choose the one longest idle on the - # assumption that message activity is likely - # to be clumpy. - if len(self.servers) >= CONNECTION_MAX: - oldest = min(self.servers.keys(), key=lambda name: self.servers[name].last_xmit()) - del self.servers[oldest] + targets, message = self._parse_request(line=line) + for target in targets: + if target.server() not in self.servers: + self.servers[target.server()] = Dispatcher( + self, target=target, **self.kwargs) + self.servers[target.server()].dispatch( + target.channel, message, target.key, quit_after=quit_after) + # GC dispatchers with no active connections + servernames = self.servers.keys() + for servername in servernames: + if not self.servers[servername].live(): + del self.servers[servername] + # If we might be pushing a resource limit even + # after garbage collection, remove a session. The + # goal here is to head off DoS attacks that aim at + # exhausting thread space or file descriptors. + # The cost is that attempts to DoS this service + # will cause lots of join/leave spam as we + # scavenge old channels after connecting to new + # ones. The particular method used for selecting a + # session to be terminated doesn't matter much; we + # choose the one longest idle on the assumption + # that message activity is likely to be clumpy. + if len(self.servers) >= CONNECTION_MAX: + oldest = min( + self.servers.keys(), + key=lambda name: self.servers[name].last_xmit()) + del self.servers[oldest] + except InvalidRequest as e: + LOG.error(UNICODE_TYPE(e)) except ValueError: self.logerr("can't recognize JSON on input: %r" % line) except RuntimeError: self.logerr("wildly malformed JSON blew the parser stack.") -class IrkerTCPHandler(SocketServer.StreamRequestHandler): +class IrkerTCPHandler(socketserver.StreamRequestHandler): def handle(self): while True: line = self.rfile.readline() if not line: break - irker.handle(line.strip()) + if not isinstance(line, UNICODE_TYPE): + line = UNICODE_TYPE(line, 'utf-8') + irker.handle(line=line.strip()) -class IrkerUDPHandler(SocketServer.BaseRequestHandler): +class IrkerUDPHandler(socketserver.BaseRequestHandler): def handle(self): - data = self.request[0].strip() + line = self.request[0].strip() #socket = self.request[1] - irker.handle(data) + if not isinstance(line, UNICODE_TYPE): + line = UNICODE_TYPE(line, 'utf-8') + irker.handle(line=line.strip()) + if __name__ == '__main__': - debuglvl = 0 - (options, arguments) = getopt.getopt(sys.argv[1:], "d:V") - for (opt, val) in options: - if opt == '-d': # Enable debug/progress messages - debuglvl = int(val) - if debuglvl > 1: - logging.basicConfig(level=logging.DEBUG) - elif opt == '-V': # Emit version and exit - sys.stdout.write("irkerd version %s\n" % version) - sys.exit(0) - irker = Irker(debuglevel=debuglvl) - irker.debug(1, "irkerd version %s" % version) - try: - tcpserver = SocketServer.TCPServer((HOST, PORT), IrkerTCPHandler) - udpserver = SocketServer.UDPServer((HOST, PORT), IrkerUDPHandler) - for server in [tcpserver, udpserver]: - server = threading.Thread(target=server.serve_forever) - server.setDaemon(True) - server.start() - try: - signal.pause() - except KeyboardInterrupt: + parser = argparse.ArgumentParser( + description=__doc__.strip().splitlines()[0]) + parser.add_argument( + '-c', '--ca-file', metavar='PATH', + help='file of trusted certificates for SSL/TLS') + parser.add_argument( + '-d', '--log-level', metavar='LEVEL', choices=LOG_LEVELS, + help='how much to log to the log file (one of %(choices)s)') + parser.add_argument( + '-l', '--log-file', metavar='PATH', + help='file for saving captured message traffic') + parser.add_argument( + '-n', '--nick', metavar='NAME', default='irker%03d', + help="nickname (optionally with a '%%.*d' server connection marker)") + parser.add_argument( + '-p', '--password', metavar='PASSWORD', + help='NickServ password') + parser.add_argument( + '-i', '--immediate', metavar='IRC-URL', + help=( + 'send a single message to IRC-URL and exit. The message is the ' + 'first positional argument.')) + parser.add_argument( + '-V', '--version', action='version', + version='%(prog)s {0}'.format(version)) + parser.add_argument( + 'message', metavar='MESSAGE', nargs='?', + help='message for --immediate mode') + args = parser.parse_args() + + handler = logging.StreamHandler() + LOG.addHandler(handler) + if args.log_level: + log_level = getattr(logging, args.log_level.upper()) + LOG.setLevel(log_level) + + irker = Irker( + logfile=args.log_file, + nick_template=args.nick, + nick_needs_number=re.search('%.*d', args.nick), + password=args.password, + cafile=args.ca_file, + ) + LOG.info("irkerd version %s" % version) + if args.immediate: + if not args.message: + LOG.error( + '--immediate set (%r), but message argument not given' % ( + args.immediate)) raise SystemExit(1) - except socket.error, e: - sys.stderr.write("irkerd: server launch failed: %r\n" % e) + irker.irc.add_event_handler("quit", lambda _c, _e: sys.exit(0)) + irker.handle('{"to":"%s","privmsg":"%s"}' % ( + args.immediate, args.message), quit_after=True) + irker.irc.spin() + else: + if args.message: + LOG.error( + 'message argument given (%r), but --immediate not set' % ( + args.message)) + raise SystemExit(1) + irker.thread_launch() + try: + tcpserver = socketserver.TCPServer((HOST, PORT), IrkerTCPHandler) + udpserver = socketserver.UDPServer((HOST, PORT), IrkerUDPHandler) + for server in [tcpserver, udpserver]: + server = threading.Thread(target=server.serve_forever) + server.setDaemon(True) + server.start() + try: + signal.pause() + except KeyboardInterrupt: + raise SystemExit(1) + except socket.error as e: + LOG.error("server launch failed: %r\n" % e) # end