Comment improvements.
[irker.git] / irkerd
1 #!/usr/bin/env python
2 """
3 irkerd - a simple IRC multiplexer daemon
4
5 Listens for JSON objects of the form {'to':<irc-url>, 'privmsg':<text>}
6 and relays messages to IRC channels. Each request must be followed by
7 a newline.
8
9 The <text> must be a string.  The value of the 'to' attribute can be a
10 string containing an IRC URL (e.g. 'irc://chat.freenet.net/botwar') or
11 a list of such strings; in the latter case the message is broadcast to
12 all listed channels.  Note that the channel portion of the URL need
13 *not* have a leading '#' unless the channel name itself does.
14
15 Options: -d sets the debug-message level (probably only of interest to
16 developers). The -V option prints the program version and exits.
17
18 Design and code by Eric S. Raymond <esr@thyrsus.com>. See the project
19 resource page at <http://www.catb.org/~esr/irker/>.
20
21 Requires Python 2.6 or 2.5 with the simplejson library installed, and
22 the irc client library at version >= 2.0.2: see
23
24 http://pypi.python.org/pypi/irc/
25 """
26 # These things might need tuning
27
28 HOST = "localhost"
29 PORT = 6659
30
31 NAMESTYLE = "irker%03d"         # IRC nick template - must contain '%d'
32 XMIT_TTL = (3 * 60 * 60)        # Time to live, seconds from last transmit
33 PING_TTL = (15 * 60)            # Time to live, seconds from last PING
34 HANDSHAKE_TTL = 60              # Time to live, seconds from nick transmit
35 CHANNEL_TTL = (3 * 60 * 60)     # Time to live, seconds from last transmit
36 DISCONNECT_TTL = (24 * 60 * 60) # Time to live, seconds from last connect
37 UNSEEN_TTL = 60                 # Time to live, seconds since first request
38 CHANNEL_MAX = 18                # Max channels open per socket (default)
39 ANTI_FLOOD_DELAY = 0.5          # Anti-flood delay after transmissions, seconds
40 ANTI_BUZZ_DELAY = 0.09          # Anti-buzz delay after queue-empty check
41
42 # No user-serviceable parts below this line
43
44 version = "1.9"
45
46 # This black magic imports support for green threads (coroutines),
47 # then has kinky sex with the import library internals, replacing
48 # "threading" with a coroutine-using imposter.  Threads then become
49 # ultra-light-weight and cooperatively scheduled.
50 try:
51     import eventlet
52     eventlet.monkey_patch()
53     green_threads = True
54     # With greenlets we don't worry about thread exhaustion, only the
55     # file descriptor limit (typically 1024 on modern Unixes). Thus we
56     # can handle a lot more concurrent sessions and generate less
57     # join/leave spam under heavy load.
58     CONNECTION_MAX = 1000
59 except ImportError:
60     # Threads are more expensive if we have to use OS-level ones
61     # rather than greenlets.  We need to avoid pushing thread limits
62     # as well as fd limits.  See security.txt for discussion.
63     CONNECTION_MAX = 200
64     green_threads = False
65
66 import sys, getopt, urlparse, time, random, socket
67 import threading, Queue, SocketServer
68 import irc.client, logging
69 try:
70     import simplejson as json   # Faster, also makes us Python-2.4-compatible
71 except ImportError:
72     import json
73
74 # Sketch of implementation:
75 #
76 # One Irker object manages multiple IRC sessions.  It holds a map of
77 # Dispatcher objects, one per (server, port) combination, which are
78 # responsible for routing messages to one of any number of Connection
79 # objects that do the actual socket conversations.  The reason for the
80 # Dispatcher layer is that IRC daemons limit the number of channels a
81 # client (that is, from the daemon's point of view, a socket) can be
82 # joined to, so each session to a server needs a flock of Connection
83 # instances each with its own socket.
84 #
85 # Connections are timed out and removed when either they haven't seen a
86 # PING for a while (indicating that the server may be stalled or down)
87 # or there has been no message traffic to them for a while, or
88 # even if the queue is nonempty but efforts to connect have failed for
89 # a long time.
90 #
91 # There are multiple threads. One accepts incoming traffic from all servers.
92 # Each Connection also has a consumer thread and a thread-safe message queue.
93 # The program main appends messages to queues as JSON requests are received;
94 # the consumer threads try to ship them to servers.  When a socket write
95 # stalls, it only blocks an individual consumer thread; if it stalls long
96 # enough, the session will be timed out.
97 #
98 # Message delivery is thus not reliable in the face of network stalls,
99 # but this was considered acceptable because IRC (notoriously) has the
100 # same problem - there is little point in reliable delivery to a relay
101 # that is down or unreliable.
102 #
103 # This code uses only NICK, JOIN, PART, MODE, and PRIVMSG. It is strictly
104 # compliant to RFC1459, except for the interpretation and use of the
105 # DEAF and CHANLIMIT and (obsolete) MAXCHANNELS features.  CHANLIMIT
106 # is as described in the Internet RFC draft
107 # draft-brocklesby-irc-isupport-03 at <http://www.mirc.com/isupport.html>.
108 # The ",isnick" feature is as described in
109 # <http://ftp.ics.uci.edu/pub/ietf/uri/draft-mirashi-url-irc-01.txt>.
110
111 class Connection:
112     def __init__(self, irkerd, servername, port):
113         self.irker = irkerd
114         self.servername = servername
115         self.port = port
116         self.nick_trial = None
117         self.connection = None
118         self.status = None
119         self.last_xmit = time.time()
120         self.last_ping = time.time()
121         self.channels_joined = {}
122         self.channel_limits = {}
123         # The consumer thread
124         self.queue = Queue.Queue()
125         self.thread = None
126     def nickname(self, n=None):
127         "Return a name for the nth server connection."
128         if n is None:
129             n = self.nick_trial
130         return (NAMESTYLE % n)
131     def handle_ping(self):
132         "Register the fact that the server has pinged this connection."
133         self.last_ping = time.time()
134     def handle_welcome(self):
135         "The server says we're OK, with a non-conflicting nick."
136         self.status = "ready"
137         self.irker.debug(1, "nick %s accepted" % self.nickname())
138     def handle_badnick(self):
139         "The server says our nick has a conflict."
140         self.irker.debug(1, "nick %s rejected" % self.nickname())
141         # Randomness prevents a malicious user or bot from antcipating the
142         # next trial name in order to block us from completing the handshake.
143         self.nick_trial += random.randint(1, 3)
144         self.last_xmit = time.time()
145         self.connection.nick(self.nickname())
146     def handle_disconnect(self):
147         "Server disconnected us for flooding or some other reason."
148         self.connection = None
149         self.status = "disconnected"
150     def handle_kick(self, outof):
151         "We've been kicked."
152         self.status = "handshaking"
153         try:
154             del self.channels_joined[outof]
155         except KeyError:
156             self.irker.logerr("kicked by %s from %s that's not joined"
157                               % (self.servername, outof))
158         qcopy = []
159         while not self.queue.empty():
160             (channel, message) = self.queue.get()
161             if channel != outof:
162                 qcopy.append((channel, message))
163         for (channel, message) in qcopy:
164             self.queue.put((channel, message))
165         self.status = "ready"
166     def enqueue(self, channel, message):
167         "Enque a message for transmission."
168         if self.thread is None or not self.thread.is_alive():
169             self.status = "unseen"
170             self.thread = threading.Thread(target=self.dequeue)
171             self.thread.setDaemon(True)
172             self.thread.start()
173         self.queue.put((channel, message))
174     def dequeue(self):
175         "Try to ship pending messages from the queue."
176         try:
177             while True:
178                 # We want to be kind to the IRC servers and not hold unused
179                 # sockets open forever, so they have a time-to-live.  The
180                 # loop is coded this particular way so that we can drop
181                 # the actual server connection when its time-to-live
182                 # expires, then reconnect and resume transmission if the
183                 # queue fills up again.
184                 if self.queue.empty():
185                     # Queue is empty, at some point we want to time out
186                     # the connection rather than holding a socket open in
187                     # the server forever.
188                     now = time.time()
189                     xmit_timeout = now > self.last_xmit + XMIT_TTL
190                     ping_timeout = now > self.last_ping + PING_TTL
191                     if self.status == "disconnected":
192                         # If the queue is empty, we can drop this connection.
193                         self.status = "expired"
194                         break
195                     elif xmit_timeout or ping_timeout:
196                         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))
197                         with self.irker.library_lock:
198                             self.connection.context = None
199                             self.connection.quit("transmission timeout")
200                             self.connection = None
201                         self.status = "disconnected"
202                     else:
203                         # Prevent this thread from hogging the CPU by pausing
204                         # for just a little bit after the queue-empty check.
205                         # As long as this is less that the duration of a human
206                         # reflex arc it is highly unlikely any human will ever
207                         # notice.
208                         time.sleep(ANTI_BUZZ_DELAY)
209                 elif self.status == "disconnected" \
210                          and time.time() > self.last_xmit + DISCONNECT_TTL:
211                     # Queue is nonempty, but the IRC server might be
212                     # down. Letting failed connections retain queue
213                     # space forever would be a memory leak.
214                     self.status = "expired"
215                     break
216                 elif not self.connection:
217                     # Queue is nonempty but server isn't connected.
218                     with self.irker.library_lock:
219                         self.connection = self.irker.irc.server()
220                         self.connection.context = self
221                         # Try to avoid colliding with other instances
222                         self.nick_trial = random.randint(1, 990)
223                         self.channels_joined = {}
224                         try:
225                             # This will throw
226                             # irc.client.ServerConnectionError on failure
227                             self.connection.connect(self.servername,
228                                                 self.port,
229                                                 nickname=self.nickname(),
230                                                 username="irker",
231                                                 ircname="irker relaying client")
232                             self.status = "handshaking"
233                             self.irker.debug(1, "XMIT_TTL bump (%s connection) at %s" % (self.servername, time.asctime()))
234                             self.last_xmit = time.time()
235                         except irc.client.ServerConnectionError:
236                             self.status = "disconnected"
237                 elif self.status == "handshaking":
238                     if time.time() > self.last_xmit + HANDSHAKE_TTL:
239                         self.status = "expired"
240                         break
241                     else:
242                         # Don't buzz on the empty-queue test while we're
243                         # handshaking
244                         time.sleep(ANTI_BUZZ_DELAY)
245                 elif self.status == "unseen" \
246                          and time.time() > self.last_xmit + UNSEEN_TTL:
247                     # Nasty people could attempt a denial-of-service
248                     # attack by flooding us with requests with invalid
249                     # servernames. We guard against this by rapidly
250                     # expiring connections that have a nonempty queue but
251                     # have never had a successful open.
252                     self.status = "expired"
253                     break
254                 elif self.status == "ready":
255                     with self.irker.library_lock:
256                         (channel, message) = self.queue.get()
257                         if channel not in self.channels_joined:
258                             self.connection.join(channel)
259                             self.irker.debug(1, "joining %s on %s." % (channel, self.servername))
260                         for segment in message.split("\n"):
261                             self.connection.privmsg(channel, segment)
262                             time.sleep(ANTI_FLOOD_DELAY)
263                         self.last_xmit = self.channels_joined[channel] = time.time()
264                         self.irker.debug(1, "XMIT_TTL bump (%s transmission) at %s" % (self.servername, time.asctime()))
265                         self.queue.task_done()
266         except:
267             (exc_type, _exc_value, exc_traceback) = sys.exc_info()
268             self.irker.logerr("exception %s in thread for %s" % \
269                               (exc_type, self.servername))
270             # This is so we can see tracebacks for errors inside the thread
271             # when we need to be able to for debugging purposes.
272             if debuglvl > 0:
273                 raise exc_type, _exc_value, exc_traceback
274     def live(self):
275         "Should this connection not be scavenged?"
276         return self.status != "expired"
277     def joined_to(self, channel):
278         "Is this connection joined to the specified channel?"
279         return channel in self.channels_joined
280     def accepting(self, channel):
281         "Can this connection accept a join of this channel?"
282         if self.channel_limits:
283             match_count = 0
284             for already in self.channels_joined:
285                 # This obscure code is because the RFCs allow separate limits
286                 # by channel type (indicated by the first character of the name)
287                 # a feature that is almost never actually used.
288                 if already[0] == channel[0]:
289                     match_count += 1
290             return match_count < self.channel_limits.get(channel[0], CHANNEL_MAX)
291         else:
292             return len(self.channels_joined) < CHANNEL_MAX
293
294 class Target():
295     "Represent a transmission target."
296     def __init__(self, url):
297         parsed = urlparse.urlparse(url)
298         irchost, _, ircport = parsed.netloc.partition(':')
299         if not ircport:
300             ircport = 6667
301         self.servername = irchost
302         # IRC channel names are case-insensitive.  If we don't smash
303         # case here we may run into problems later. There was a bug
304         # observed on irc.rizon.net where an irkerd user specified #Channel,
305         # got kicked, and irkerd crashed because the server returned
306         # "#channel" in the notification that our kick handler saw.
307         self.channel = parsed.path.lstrip('/').lower()
308         isnick = self.channel.endswith(",isnick")
309         if isnick:
310             self.channel = self.channel[:-7]
311         if self.channel and not isnick and self.channel[0] not in "#&+":
312             self.channel = "#" + self.channel
313         self.port = int(ircport)
314     def valid(self):
315         "Both components must be present for a valid target."
316         return self.servername and self.channel
317     def server(self):
318         "Return a hashable tuple representing the destination server."
319         return (self.servername, self.port)
320
321 class Dispatcher:
322     "Manage connections to a particular server-port combination."
323     def __init__(self, irkerd, servername, port):
324         self.irker = irkerd
325         self.servername = servername
326         self.port = port
327         self.connections = []
328     def dispatch(self, channel, message):
329         "Dispatch messages for our server-port combination."
330         # First, check if there is room for another channel
331         # on any of our existing connections.
332         connections = [x for x in self.connections if x.live()]
333         eligibles = [x for x in connections if x.joined_to(channel)] \
334                     or [x for x in connections if x.accepting(channel)]
335         if eligibles:
336             eligibles[0].enqueue(channel, message)
337             return
338         # All connections are full up. Look for one old enough to be
339         # scavenged.
340         ancients = []
341         for connection in connections:
342             for (chan, age) in connections.channels_joined.items():
343                 if age < time.time() - CHANNEL_TTL:
344                     ancients.append((connection, chan, age))
345         if ancients:
346             ancients.sort(key=lambda x: x[2]) 
347             (found_connection, drop_channel, _drop_age) = ancients[0]
348             found_connection.part(drop_channel, "scavenged by irkerd")
349             del found_connection.channels_joined[drop_channel]
350             #time.sleep(ANTI_FLOOD_DELAY)
351             found_connection.enqueue(channel, message)
352             return
353         # Didn't find any channels with no recent activity
354         newconn = Connection(self.irker,
355                              self.servername,
356                              self.port)
357         self.connections.append(newconn)
358         newconn.enqueue(channel, message)
359     def live(self):
360         "Does this server-port combination have any live connections?"
361         self.connections = [x for x in self.connections if x.live()]
362         return len(self.connections) > 0
363     def last_xmit(self):
364         "Return the time of the most recent transmission."
365         return max(x.last_xmit for x in self.connections)
366
367 class Irker:
368     "Persistent IRC multiplexer."
369     def __init__(self, debuglevel=0):
370         self.debuglevel = debuglevel
371         self.irc = irc.client.IRC()
372         self.irc.add_global_handler("ping", self._handle_ping)
373         self.irc.add_global_handler("welcome", self._handle_welcome)
374         self.irc.add_global_handler("erroneusnickname", self._handle_badnick)
375         self.irc.add_global_handler("nicknameinuse", self._handle_badnick)
376         self.irc.add_global_handler("nickcollision", self._handle_badnick)
377         self.irc.add_global_handler("unavailresource", self._handle_badnick)
378         self.irc.add_global_handler("featurelist", self._handle_features)
379         self.irc.add_global_handler("disconnect", self._handle_disconnect)
380         self.irc.add_global_handler("kick", self._handle_kick)
381         self.library_lock = threading.Lock()
382         thread = threading.Thread(target=self._process_forever)
383         thread.setDaemon(True)
384         self.irc._thread = thread
385         thread.start()
386         self.servers = {}
387     def logerr(self, errmsg):
388         "Log a processing error."
389         sys.stderr.write("irkerd: " + errmsg + "\n")
390     def debug(self, level, errmsg):
391         "Debugging information."
392         if self.debuglevel >= level:
393             sys.stderr.write("irkerd: %s\n" % errmsg)
394     def _process_forever(self):
395         "IRC library process_forever with mutex."
396         self.debug(1, "process_forever()")
397         while True:
398             with self.library_lock:
399                 self.irc.process_once(ANTI_BUZZ_DELAY)
400     def _handle_ping(self, connection, _event):
401         "PING arrived, bump the last-received time for the connection."
402         if connection.context:
403             connection.context.handle_ping()
404     def _handle_welcome(self, connection, _event):
405         "Welcome arrived, nick accepted for this connection."
406         if connection.context:
407             connection.context.handle_welcome()
408     def _handle_badnick(self, connection, _event):
409         "Nick not accepted for this connection."
410         if connection.context:
411             connection.context.handle_badnick()
412     def _handle_features(self, connection, event):
413         "Determine if and how we can set deaf mode."
414         if connection.context:
415             cxt = connection.context
416             for lump in event.arguments():
417                 if lump.startswith("DEAF="):
418                     connection.mode(cxt.nickname(), "+"+lump[5:])
419                 elif lump.startswith("MAXCHANNELS="):
420                     m = int(lump[12:])
421                     for pref in "#&+":
422                         cxt.channel_limits[pref] = m
423                     self.debug(1, "%s maxchannels is %d"
424                                % (connection.server, m))
425                 elif lump.startswith("CHANLIMIT=#:"):
426                     limits = lump[10:].split(",")
427                     try:
428                         for token in limits:
429                             (prefixes, limit) = token.split(":")
430                             limit = int(limit)
431                             for c in prefixes:
432                                 cxt.channel_limits[c] = limit
433                         self.debug(1, "%s channel limit map is %s"
434                                    % (connection.server, cxt.channel_limits))
435                     except ValueError:
436                         self.logerr("ill-formed CHANLIMIT property")
437     def _handle_disconnect(self, connection, _event):
438         "Server hung up the connection."
439         self.debug(1, "server %s disconnected" % connection.server)
440         connection.close()
441         if connection.context:
442             connection.context.handle_disconnect()
443     def _handle_kick(self, connection, event):
444         "Server hung up the connection."
445         self.debug(1, "irker has been kicked from %s on %s" % (event.target(), connection.server))
446         if connection.context:
447             connection.context.handle_kick(event.target())
448     def handle(self, line):
449         "Perform a JSON relay request."
450         try:
451             request = json.loads(line.strip())
452             if not isinstance(request, dict):
453                 self.logerr("request is not a JSON dictionary: %r" % request)
454             elif "to" not in request or "privmsg" not in request:
455                 self.logerr("malformed request - 'to' or 'privmsg' missing: %r" % request)
456             else:
457                 channels = request['to']
458                 message = request['privmsg']
459                 if not isinstance(channels, (list, basestring)):
460                     self.logerr("malformed request - unexpected channel type: %r" % channels)
461                 if not isinstance(message, basestring):
462                     self.logerr("malformed request - unexpected message type: %r" % message)
463                 else:
464                     if not isinstance(channels, list):
465                         channels = [channels]
466                     for url in channels:
467                         if not isinstance(url, basestring):
468                             self.logerr("malformed request - URL has unexpected type: %r" % url)
469                         else:
470                             target = Target(url)
471                             if not target.valid():
472                                 return
473                             if target.server() not in self.servers:
474                                 self.servers[target.server()] = Dispatcher(self, target.servername, target.port)
475                             self.servers[target.server()].dispatch(target.channel, message)
476                             # GC dispatchers with no active connections
477                             servernames = self.servers.keys()
478                             for servername in servernames:
479                                 if not self.servers[servername].live():
480                                     del self.servers[servername]
481                             # If we might be pushing a resource limit
482                             # even after garbage collection, remove a
483                             # session.  The goal here is to head off
484                             # DoS attacks that aim at exhausting
485                             # thread space or file descriptors.  The
486                             # cost is that attempts to DoS this
487                             # service will cause lots of join/leave
488                             # spam as we scavenge old channels after
489                             # connecting to new ones. The particular
490                             # method used for selecting a session to
491                             # be terminated doesn't matter much; we
492                             # choose the one longest idle on the
493                             # assumption that message activity is likely
494                             # to be clumpy.
495                             if len(self.servers) >= CONNECTION_MAX:
496                                 oldest = min(self.servers.keys(), key=lambda name: self.servers[name].last_xmit())
497                                 del self.servers[oldest]
498         except ValueError:
499             self.logerr("can't recognize JSON on input: %r" % line)
500         except RuntimeError:
501             self.logerr("wildly malformed JSON blew the parser stack.")
502
503 class IrkerTCPHandler(SocketServer.StreamRequestHandler):
504     def handle(self):
505         while True:
506             line = self.rfile.readline()
507             if not line:
508                 break
509             irker.handle(line.strip())
510
511 class IrkerUDPHandler(SocketServer.BaseRequestHandler):
512     def handle(self):
513         data = self.request[0].strip()
514         #socket = self.request[1]
515         irker.handle(data)
516
517 if __name__ == '__main__':
518     debuglvl = 0
519     (options, arguments) = getopt.getopt(sys.argv[1:], "d:V")
520     for (opt, val) in options:
521         if opt == '-d':         # Enable debug/progress messages
522             debuglvl = int(val)
523             if debuglvl > 1:
524                 logging.basicConfig(level=logging.DEBUG)
525         elif opt == '-V':       # Emit version and exit
526             sys.stdout.write("irkerd version %s\n" % version)
527             sys.exit(0)
528     irker = Irker(debuglevel=debuglvl)
529     irker.debug(1, "irkerd version %s" % version)
530     try:
531         tcpserver = SocketServer.TCPServer((HOST, PORT), IrkerTCPHandler)
532         udpserver = SocketServer.UDPServer((HOST, PORT), IrkerUDPHandler)
533         for server in [tcpserver, udpserver]:
534             server = threading.Thread(target=server.serve_forever)
535             server.setDaemon(True)
536             server.start()
537         try:
538             while True:
539                 time.sleep(10)
540         except KeyboardInterrupt:
541             raise SystemExit(1)
542     except socket.error, e:
543         sys.stderr.write("irkerd: server launch failed: %r\n" % e)
544
545 # end