3 irkerd - a simple IRC multiplexer daemon
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
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.
15 Options: -d sets the debug-message level (probably only of interest to
16 developers). The -V option prints the program version and exits.
18 Design and code by Eric S. Raymond <esr@thyrsus.com>. See the project
19 resource page at <http://www.catb.org/~esr/irker/>.
21 Requires Python 2.6 and the irc client library at version >= 2.0.2: see
23 http://pypi.python.org/pypi/irc/
25 # These things might need tuning
30 NAMESTYLE = "irker%03d" # IRC nick template - must contain '%d'
31 XMIT_TTL = (3 * 60 * 60) # Time to live, seconds from last transmit
32 PING_TTL = (15 * 60) # Time to live, seconds from last PING
33 DISCONNECT_TTL = (24 * 60 * 60) # Time to live, seconds from last connect
34 UNSEEN_TTL = 60 # Time to live, seconds since first request
35 CHANNEL_MAX = 18 # Max channels open per socket (default)
36 ANTI_FLOOD_DELAY = 0.5 # Anti-flood delay after transmissions, seconds
37 ANTI_BUZZ_DELAY = 0.09 # Anti-buzz delay after queue-empty check
39 # No user-serviceable parts below this line
41 # This black magic imports support for green threads (coroutines),
42 # then has kinky sex with the import library internals, replacing
43 # "threading" with a coroutine-using imposter. Threads then become
44 # ultra-light-weight and cooperatively scheduled.
47 eventlet.monkey_patch()
49 # With greenlets we don't worry about thread exhaustion, only the
50 # file descriptor limit (typically 1024 on modern Unixes). Thus we
51 # can handle a lot more concurrent sessions and generate less
52 # join/leave spam under heavy load.
55 # Threads are more expensive if we have to use OS-level ones
56 # rather than greenlets. We need to avoid pushing thread limits
57 # as well as fd limits. See security.txt for discussion.
61 import sys, getopt, urlparse, time, random, socket
62 import threading, Queue, SocketServer
63 import irc.client, logging
65 import simplejson as json # Faster, also makes us Python-2.4-compatible
71 # Sketch of implementation:
73 # One Irker object manages multiple IRC sessions. It holds a map of
74 # Dispatcher objects, one per (server, port) combination, which are
75 # responsible for routing messages to one of any number of Connection
76 # objects that do the actual socket conversations. The reason for the
77 # Dispatcher layer is that IRC daemons limit the number of channels a
78 # client (that is, from the daemon's point of view, a socket) can be
79 # joined to, so each session to a server needs a flock of Connection
80 # instances each with its own socket.
82 # Connections are timed out and removed when either they haven't seen a
83 # PING for a while (indicating that the server may be stalled or down)
84 # or there has been no message traffic to them for a while, or
85 # even if the queue is nonempty but efforts to connect have failed for
88 # There are multiple threads. One accepts incoming traffic from all servers.
89 # Each Connection also has a consumer thread and a thread-safe message queue.
90 # The program main appends messages to queues as JSON requests are received;
91 # the consumer threads try to ship them to servers. When a socket write
92 # stalls, it only blocks an individual consumer thread; if it stalls long
93 # enough, the session will be timed out.
95 # Message delivery is thus not reliable in the face of network stalls,
96 # but this was considered acceptable because IRC (notoriously) has the
97 # same problem - there is little point in reliable delivery to a relay
98 # that is down or unreliable.
100 # This code uses only NICK, JOIN, MODE, and PRIVMSG. It is strictly
101 # compliant to RFC1459, except for the interpretation and use of the
102 # DEAF and CHANLIMIT and (obsolete) MAXCHANNELS features. CHANLIMIT
103 # is as described in the Internet RFC draft
104 # draft-brocklesby-irc-isupport-03 at <http://www.mirc.com/isupport.html>.
107 def __init__(self, irkerd, servername, port):
109 self.servername = servername
111 self.nick_trial = None
112 self.connection = None
114 self.last_xmit = time.time()
115 self.last_ping = time.time()
116 self.channels_joined = []
117 self.channel_limits = {}
118 # The consumer thread
119 self.queue = Queue.Queue()
121 def nickname(self, n=None):
122 "Return a name for the nth server connection."
125 return (NAMESTYLE % n)
126 def handle_ping(self):
127 "Register the fact that the server has pinged this connection."
128 self.last_ping = time.time()
129 def handle_welcome(self):
130 "The server says we're OK, with a non-conflicting nick."
131 self.status = "ready"
132 self.irker.debug(1, "nick %s accepted" % self.nickname())
133 def handle_badnick(self):
134 "The server says our nick has a conflict."
135 self.irker.debug(1, "nick %s rejected" % self.nickname())
136 # Randomness prevents a malicious user or bot from antcipating the
137 # next trial name in order to block us from completing the handshake.
138 self.nick_trial += random.randint(1, 3)
139 self.connection.nick(self.nickname())
140 def handle_disconnect(self):
141 "Server disconnected us for flooding or some other reason."
142 self.connection = None
143 def handle_kick(self, outof):
145 self.status = "handshaking"
147 self.channels_joined.remove(outof)
149 self.irker.logerr("kicked by %s from %s that's not joined"
150 % (self.servername, outof))
152 while not self.queue.empty():
153 (channel, message) = self.queue.get()
155 qcopy.append((channel, message))
156 for (channel, message) in qcopy:
157 self.queue.put((channel, message))
158 self.status = "ready"
159 def enqueue(self, channel, message):
160 "Enque a message for transmission."
161 if self.thread is None or not self.thread.is_alive():
162 self.status = "unseen"
163 self.thread = threading.Thread(target=self.dequeue)
164 self.thread.setDaemon(True)
166 self.queue.put((channel, message))
168 "Try to ship pending messages from the queue."
171 # We want to be kind to the IRC servers and not hold unused
172 # sockets open forever, so they have a time-to-live. The
173 # loop is coded this particular way so that we can drop
174 # the actual server connection when its time-to-live
175 # expires, then reconnect and resume transmission if the
176 # queue fills up again.
177 if self.queue.empty():
178 # Queue is empty, at some point we want to time out
179 # the connection rather than holding a socket open in
180 # the server forever.
182 if (now > self.last_xmit + XMIT_TTL \
183 or now > self.last_ping + PING_TTL) \
184 and self.status != "disconnected":
185 self.irker.debug(1, "timing out inactive connection to %s at %s" % (self.servername, time.asctime()))
186 self.connection.context = None
187 self.connection.quit("transmission timeout")
188 self.connection.close()
189 self.connection = None
190 self.status = "disconnected"
192 # Prevent this thread from hogging the CPU by pausing
193 # for just a little bit after the queue-empty check.
194 # As long as this is less that the duration of a human
195 # reflex arc it is highly unlikely any human will ever
197 time.sleep(ANTI_BUZZ_DELAY)
198 elif not self.connection:
199 # Queue is nonempty but server isn't connected.
200 self.connection = self.irker.irc.server()
201 self.connection.context = self
202 # Try to avoid colliding with other instances
203 self.nick_trial = random.randint(1, 990)
204 self.channels_joined = []
205 # This will throw irc.client.ServerConnectionError on failure
207 self.connection.connect(self.servername,
209 nickname=self.nickname(),
211 ircname="irker relaying client")
212 self.status = "handshaking"
213 self.irker.debug(1, "XMIT_TTL bump (%s connection) at %s" % (self.servername, time.asctime()))
214 self.last_xmit = time.time()
215 except irc.client.ServerConnectionError:
216 self.status = "disconnected"
217 elif self.status == "handshaking":
218 # Don't buzz on the empty-queue test while we're
220 time.sleep(ANTI_BUZZ_DELAY)
221 elif self.status == "disconnected" \
222 and time.time() > self.last_xmit + DISCONNECT_TTL:
223 # Queue is nonempty, but the IRC server might be
224 # down. Letting failed connections retain queue
225 # space forever would be a memory leak.
226 self.status = "expired"
228 elif self.status == "unseen" \
229 and time.time() > self.last_xmit + UNSEEN_TTL:
230 # Nasty people could attempt a denial-of-service
231 # attack by flooding us with requests with invalid
232 # servernames. We guard against this by rapidly
233 # expiring connections that have a nonempty queue but
234 # have never had a successful open.
235 self.status = "expired"
237 elif self.status == "ready":
238 (channel, message) = self.queue.get()
239 if channel not in self.channels_joined:
240 self.channels_joined.append(channel)
241 self.connection.join(channel)
242 self.irker.debug(1, "joining %s on %s." % (channel, self.servername))
243 for segment in message.split("\n"):
244 self.connection.privmsg(channel, segment)
245 time.sleep(ANTI_FLOOD_DELAY)
246 self.last_xmit = time.time()
247 self.irker.debug(1, "XMIT_TTL bump (%s transmission) at %s" % (self.servername, time.asctime()))
248 self.queue.task_done()
250 (exc_type, _exc_value, _exc_traceback) = sys.exc_info()
251 self.irker.logerr("exception %s in thread for %s" % \
252 (exc_type, self.servername))
254 "Should this connection not be scavenged?"
255 return self.status != "expired"
256 def joined_to(self, channel):
257 "Is this connection joined to the specified channel?"
258 return channel in self.channels_joined
259 def accepting(self, channel):
260 "Can this connection accept a join of this channel?"
261 if self.channel_limits:
263 for already in self.channels_joined:
264 if already[0] == channel[0]:
266 return match_count < self.channel_limits.get(channel[0], CHANNEL_MAX)
268 return len(self.channels_joined) < CHANNEL_MAX
271 "Represent a transmission target."
272 def __init__(self, url):
273 parsed = urlparse.urlparse(url)
274 irchost, _, ircport = parsed.netloc.partition(':')
277 self.servername = irchost
278 # IRC channel names are case-insensitive. If we don't smash
279 # case here we may run into problems later. There was a bug
280 # observed on irc.rizon.net where an irkerd user specified #Channel,
281 # got kicked, and irkerd crashed because the server returned
282 # "#channel" in the notification that our kick handler saw.
283 self.channel = parsed.path.lstrip('/').lower()
284 if self.channel and self.channel[0] not in "#&+":
285 self.channel = "#" + self.channel
286 self.port = int(ircport)
288 "Both components must be present for a valid target."
289 return self.servername and self.channel
291 "Return a hashable tuple representing the destination server."
292 return (self.servername, self.port)
295 "Manage connections to a particular server-port combination."
296 def __init__(self, irkerd, servername, port):
298 self.servername = servername
300 self.connections = []
301 def dispatch(self, channel, message):
302 "Dispatch messages for our server-port combination."
303 connections = [x for x in self.connections if x.live()]
304 eligibles = [x for x in connections if x.joined_to(channel)] \
305 or [x for x in connections if x.accepting(channel)]
307 newconn = Connection(self.irker,
310 self.connections.append(newconn)
311 eligibles = [newconn]
312 eligibles[0].enqueue(channel, message)
314 "Does this server-port combination have any live connections?"
315 self.connections = [x for x in self.connections if x.live()]
316 return len(self.connections) > 0
318 "Return the time of the most recent transmission."
319 return max([x.last_xmit for x in self.connections])
322 "Persistent IRC multiplexer."
323 def __init__(self, debuglevel=0):
324 self.debuglevel = debuglevel
325 self.irc = irc.client.IRC()
326 self.irc.add_global_handler("ping", self._handle_ping)
327 self.irc.add_global_handler("welcome", self._handle_welcome)
328 self.irc.add_global_handler("erroneusnickname", self._handle_badnick)
329 self.irc.add_global_handler("nicknameinuse", self._handle_badnick)
330 self.irc.add_global_handler("nickcollision", self._handle_badnick)
331 self.irc.add_global_handler("unavailresource", self._handle_badnick)
332 self.irc.add_global_handler("featurelist", self._handle_features)
333 self.irc.add_global_handler("disconnect", self._handle_disconnect)
334 self.irc.add_global_handler("kick", self._handle_kick)
335 thread = threading.Thread(target=self.irc.process_forever)
336 thread.setDaemon(True)
337 self.irc._thread = thread
340 def logerr(self, errmsg):
341 "Log a processing error."
342 sys.stderr.write("irkerd: " + errmsg + "\n")
343 def debug(self, level, errmsg):
344 "Debugging information."
345 if self.debuglevel >= level:
346 sys.stderr.write("irkerd: %s\n" % errmsg)
347 def _handle_ping(self, connection, _event):
348 "PING arrived, bump the last-received time for the connection."
349 if connection.context:
350 connection.context.handle_ping()
351 def _handle_welcome(self, connection, _event):
352 "Welcome arrived, nick accepted for this connection."
353 if connection.context:
354 connection.context.handle_welcome()
355 def _handle_badnick(self, connection, _event):
356 "Nick not accepted for this connection."
357 if connection.context:
358 connection.context.handle_badnick()
359 def _handle_features(self, connection, event):
360 "Determine if and how we can set deaf mode."
361 if connection.context:
362 cxt = connection.context
363 for lump in event.arguments():
364 if lump.startswith("DEAF="):
365 connection.mode(cxt.nickname(), "+"+lump[5:])
366 elif lump.startswith("MAXCHANNELS="):
369 cxt.channel_limits[pref] = m
370 self.debug(1, "%s maxchannels is %d"
371 % (connection.server, m))
372 elif lump.startswith("CHANLIMIT=#:"):
373 limits = lump[10:].split(",")
376 (prefixes, limit) = token.split(":")
379 cxt.channel_limits[c] = limit
380 self.debug(1, "%s channel limit map is %s"
381 % (connection.server, cxt.channel_limits))
383 self.logerr("ill-formed CHANLIMIT property")
384 def _handle_disconnect(self, connection, _event):
385 "Server hung up the connection."
386 self.debug(1, "server %s disconnected" % connection.server)
387 if connection.context:
388 connection.context.handle_disconnect()
389 def _handle_kick(self, connection, event):
390 "Server hung up the connection."
391 self.debug(1, "irker has been kicked from %s on %s" % (event.target(), connection.server))
392 if connection.context:
393 connection.context.handle_kick(event.target())
394 def handle(self, line):
395 "Perform a JSON relay request."
397 request = json.loads(line.strip())
398 if not isinstance(request, dict):
399 self.logerr("request is not a JSON dictionary: %r" % request)
400 elif "to" not in request or "privmsg" not in request:
401 self.logerr("malformed request - 'to' or 'privmsg' missing: %r" % request)
403 channels = request['to']
404 message = request['privmsg']
405 if type(channels) not in (type([]), type(""), type(u"")):
406 self.logerr("malformed request - unexpected channel type: %r" % channels)
407 if type(message) not in (type(""), type(u"")):
408 self.logerr("malformed request - unexpected message type: %r" % message)
410 if type(channels) != type([]):
411 channels = [channels]
413 if not type(url) in (type(""), type(u"")):
414 self.logerr("malformed request - URL has unexpected type: %r" % url)
417 if not target.valid():
419 if target.server() not in self.servers:
420 self.servers[target.server()] = Dispatcher(self, target.servername, target.port)
421 self.servers[target.server()].dispatch(target.channel, message)
422 # GC dispatchers with no active connections
423 servernames = self.servers.keys()
424 for servername in servernames:
425 if not self.servers[servername].live():
426 del self.servers[servername]
427 # If we might be pushing a resource limit
428 # even after garbage collection, remove a
429 # session. The goal here is to head off
430 # DoS attacks that aim at exhausting
431 # thread space or file descriptors. The
432 # cost is that attempts to DoS this
433 # service will cause lots of join/leave
434 # spam as we scavenge old channels after
435 # connecting to new ones. The particular
436 # method used for selecting a session to
437 # be terminated doesn't matter much; we
438 # choose the one longest idle on the
439 # assumption that message activity is likely
442 oldtime = float("inf")
443 if len(self.servers) >= CONNECTION_MAX:
444 for (name, server) in self.servers.items():
445 if server.last_xmit() < oldtime:
447 oldtime = server.last_xmit()
448 del self.servers[oldest]
450 self.logerr("can't recognize JSON on input: %r" % line)
452 self.logerr("wildly malformed JSON blew the parser stack.")
454 class IrkerTCPHandler(SocketServer.StreamRequestHandler):
457 line = self.rfile.readline()
460 irker.handle(line.strip())
462 class IrkerUDPHandler(SocketServer.BaseRequestHandler):
464 data = self.request[0].strip()
465 #socket = self.request[1]
468 if __name__ == '__main__':
470 (options, arguments) = getopt.getopt(sys.argv[1:], "d:V")
471 for (opt, val) in options:
472 if opt == '-d': # Enable debug/progress messages
475 logging.basicConfig(level=logging.DEBUG)
476 elif opt == '-V': # Emit version and exit
477 sys.stdout.write("irkerd version %s\n" % version)
479 irker = Irker(debuglevel=debuglvl)
480 irker.debug(1, "irkerd version %s" % version)
482 tcpserver = SocketServer.TCPServer((HOST, PORT), IrkerTCPHandler)
483 udpserver = SocketServer.UDPServer((HOST, PORT), IrkerUDPHandler)
484 for server in [tcpserver, udpserver]:
485 server = threading.Thread(target=server.serve_forever)
486 server.setDaemon(True)
491 except KeyboardInterrupt:
493 except socket.error, e:
494 sys.stderr.write("irkerd: server launch failed: %r\n" % e)