Import Peter Scott's use of green threads.
[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 and the irc client library at version >= 2.0.2: see
22
23 http://pypi.python.org/pypi/irc/
24 """
25 # These things might need tuning
26
27 HOST = "localhost"
28 PORT = 6659
29
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.125        # Anti-flood delay after transmissions, seconds
37 ANTI_BUZZ_DELAY = 0.09          # Anti-buzz delay after queue-empty check
38 CONNECTION_MAX = 200            # Avoid pushing per-process thread or fd limits
39
40 # No user-serviceable parts below this line
41
42 # This black magic imports support for green threads (coroutines),
43 # then has kinky sex with the threading library internals.
44 # Threads then become ultra-light-weight and cooperatively
45 # scheduled.
46 try:
47     import eventlet; eventlet.monkey_patch()
48     green_threads = True
49 except ImportError:
50     green_threads = False
51
52 import sys, json, getopt, urlparse, time, random
53 import threading, Queue, SocketServer
54 import irc.client, logging
55
56 version = "1.2"
57
58 # Sketch of implementation:
59 #
60 # One Irker object manages multiple IRC sessions.  It holds a map of
61 # Dispatcher objects, one per (server, port) combination, which are
62 # responsible for routing messages to one of any number of Connection
63 # objects that do the actual socket conversations.  The reason for the
64 # Dispatcher layer is that IRC daemons limit the number of channels a
65 # client (that is, from the daemon's point of view, a socket) can be
66 # joined to, so each session to a server needs a flock of Connection
67 # instances each with its own socket.
68 #
69 # Connections are timed out and removed when either they haven't seen a
70 # PING for a while (indicating that the server may be stalled or down)
71 # or there has been no message traffic to them for a while, or
72 # even if the queue is nonempty but efforts to connect have failed for
73 # a long time.
74 #
75 # There are multiple threads. One accepts incoming traffic from all servers.
76 # Each Connection also has a consumer thread and a thread-safe message queue.
77 # The program main appends messages to queues as JSON requests are received;
78 # the consumer threads try to ship them to servers.  When a socket write
79 # stalls, it only blocks an individual consumer thread; if it stalls long
80 # enough, the session will be timed out.
81 #
82 # Message delivery is thus not reliable in the face of network stalls,
83 # but this was considered acceptable because IRC (notoriously) has the
84 # same problem - there is little point in reliable delivery to a relay
85 # that is down or unreliable.
86 #
87 # This code uses only NICK, JOIN, MODE, and PRIVMSG. It is strictly
88 # compliant to RFC1459, except for the interpretation and use of the
89 # DEAF and CHANLIMIT and (obsolete) MAXCHANNELS features.  CHANLIMIT
90 # is as described in the Internet RFC draft
91 # draft-brocklesby-irc-isupport-03 at <http://www.mirc.com/isupport.html>.
92
93 class Connection:
94     def __init__(self, irkerd, servername, port, nick_base=1):
95         self.irker = irkerd
96         self.servername = servername
97         self.port = port
98         self.nick_trial = nick_base
99         self.connection = None
100         self.status = "unseen"
101         self.last_xmit = time.time()
102         self.last_ping = time.time()
103         self.channels_joined = []
104         self.channel_limits = {}
105         # The consumer thread
106         self.queue = Queue.Queue()
107         self.thread = threading.Thread(target=self.dequeue)
108         self.thread.start()
109     def nickname(self, n=None):
110         "Return a name for the nth server connection."
111         if n is None:
112             n = self.nick_trial
113         return (NAMESTYLE % n)
114     def handle_ping(self):
115         "Register the fact that the server has pinged this connection."
116         self.last_ping = time.time()
117     def handle_welcome(self):
118         "The server says we're OK, with a non-conflicting nick."
119         self.status = "ready"
120         self.irker.debug(1, "nick %s accepted" % self.nickname())
121     def handle_badnick(self):
122         "The server says our nick has a conflict."
123         self.irker.debug(1, "nick %s rejected" % self.nickname())
124         # Randomness prevents a malicious user or bot from antcipating the
125         # next trial name in order to block us from completing the handshake.
126         self.nick_trial += random.randint(1, 3)
127         self.connection.nick(self.nickname())
128     def enqueue(self, channel, message):
129         "Enque a message for transmission."
130         self.queue.put((channel, message))
131     def dequeue(self):
132         "Try to ship pending messages from the queue."
133         while True:
134             # We want to be kind to the IRC servers and not hold unused
135             # sockets open forever, so they have a time-to-live.  The
136             # loop is coded this particular way so that we can drop
137             # the actual server connection when its time-to-live
138             # expires, then reconnect and resume transmission if the
139             # queue fills up again.
140             if not self.connection:
141                 self.connection = self.irker.irc.server()
142                 self.connection.context = self
143                 self.nick_trial = 1
144                 self.channels_joined = []
145                 # This will throw irc.client.ServerConnectionError on failure
146                 try:
147                     self.connection.connect(self.servername,
148                                         self.port,
149                                         nickname=self.nickname(),
150                                         username="irker",
151                                         ircname="irker relaying client")
152                     self.status = "handshaking"
153                     self.irker.debug(1, "XMIT_TTL bump (%s connection) at %s" % (self.servername, time.asctime()))
154                     self.last_xmit = time.time()
155                 except irc.client.ServerConnectionError:
156                     self.status = "disconnected"
157             elif self.status == "handshaking":
158                 # Don't buzz on the empty-queue test while we're handshaking 
159                 time.sleep(ANTI_BUZZ_DELAY)
160             elif self.queue.empty():
161                 # Queue is empty, at some point we want to time out
162                 # the connection rather than holding a socket open in
163                 # the server forever.
164                 now = time.time()
165                 if now > self.last_xmit + XMIT_TTL \
166                        or now > self.last_ping + PING_TTL:
167                     self.irker.debug(1, "timing out inactive connection to %s at %s" % (self.servername, time.asctime()))
168                     self.connection.context = None
169                     self.connection.close()
170                     self.connection = None
171                     self.status = "disconnected"
172                 else:
173                     # Prevent this thread from hogging the CPU by pausing
174                     # for just a little bit after the queue-empty check.
175                     # As long as this is less that the duration of a human
176                     # reflex arc it is highly unlikely any human will ever
177                     # notice.
178                     time.sleep(ANTI_BUZZ_DELAY)
179             elif self.status == "disconnected" \
180                      and time.time() > self.last_xmit + DISCONNECT_TTL:
181                 # Queue is nonempty, but the IRC server might be down. Letting
182                 # failed connections retain queue space forever would be a
183                 # memory leak.  
184                 self.status = "expired"
185                 break
186             elif self.status == "unseen" \
187                      and time.time() > self.last_xmit + UNSEEN_TTL:
188                 # Nasty people could attempt a denial-of-service
189                 # attack by flooding us with requests with invalid
190                 # servernames. We guard against this by rapidly
191                 # expiring connections that have a nonempty queue but
192                 # have never had a successful open.
193                 self.status = "expired"
194                 break
195             elif self.status == "ready":
196                 (channel, message) = self.queue.get()
197                 if channel not in self.channels_joined:
198                     self.channels_joined.append(channel)
199                     if channel[0] not in "#&+":
200                         channel = "#" + channel
201                     self.connection.join(channel)
202                 for segment in message.split("\n"):
203                     self.connection.privmsg(channel, segment)
204                 self.last_xmit = time.time()
205                 self.irker.debug(1, "XMIT_TTL bump (%s transmission) at %s" % (self.servername, time.asctime()))
206                 self.queue.task_done()
207                 time.sleep(ANTI_FLOOD_DELAY)
208     def live(self):
209         "Should this connection not be scavenged?"
210         return self.status != "expired"
211     def joined_to(self, channel):
212         "Is this connection joined to the specified channel?"
213         return channel in self.channels_joined
214     def accepting(self, channel):
215         "Can this connection accept a join of this channel?"
216         if self.channel_limits:
217             match_count = 0
218             for already in self.channels_joined:
219                 if already[0] == channel[0]:
220                     match_count += 1
221             return match_count < self.channel_limits.get(channel[0], CHANNEL_MAX)
222         else:
223             return len(self.channels_joined) < CHANNEL_MAX
224
225 class Target():
226     "Represent a transmission target."
227     def __init__(self, url):
228         parsed = urlparse.urlparse(url)
229         irchost, _, ircport = parsed.netloc.partition(':')
230         if not ircport:
231             ircport = 6667
232         self.servername = irchost
233         self.channel = parsed.path.lstrip('/')
234         self.port = int(ircport)
235     def server(self):
236         "Return a hashable tuple representing the destination server."
237         return (self.servername, self.port)
238
239 class Dispatcher:
240     "Manage connections to a particular server-port combination."
241     def __init__(self, irkerd, servername, port):
242         self.irker = irkerd
243         self.servername = servername
244         self.port = port
245         self.connections = []
246     def dispatch(self, channel, message):
247         "Dispatch messages for our server-port combination."
248         connections = [x for x in self.connections if x.live()]
249         eligibles = [x for x in connections if x.joined_to(channel)] \
250                     or [x for x in connections if x.accepting(channel)]
251         if not eligibles:
252             newconn = Connection(self.irker,
253                                  self.servername,
254                                  self.port,
255                                  len(self.connections)+1)
256             self.connections.append(newconn)
257             eligibles = [newconn]
258         eligibles[0].enqueue(channel, message)
259     def live(self):
260         "Does this server-port combination have any live connections?"
261         self.connections = [x for x in self.connections if x.live()]
262         return len(self.connections) > 0
263
264 class Irker:
265     "Persistent IRC multiplexer."
266     def __init__(self, debuglevel=0):
267         self.debuglevel = debuglevel
268         self.irc = irc.client.IRC()
269         self.irc.add_global_handler("ping", self._handle_ping)
270         self.irc.add_global_handler("welcome", self._handle_welcome)
271         self.irc.add_global_handler("erroneusnickname", self._handle_badnick)
272         self.irc.add_global_handler("nicknameinuse", self._handle_badnick)
273         self.irc.add_global_handler("nickcollision", self._handle_badnick)
274         self.irc.add_global_handler("unavailresource", self._handle_badnick)
275         self.irc.add_global_handler("featurelist", self._handle_features)
276         thread = threading.Thread(target=self.irc.process_forever)
277         self.irc._thread = thread
278         thread.start()
279         self.servers = {}
280     def logerr(self, errmsg):
281         "Log a processing error."
282         sys.stderr.write("irkerd: " + errmsg + "\n")
283     def debug(self, level, errmsg):
284         "Debugging information."
285         if self.debuglevel >= level:
286             sys.stderr.write("irkerd: %s\n" % errmsg)
287     def _handle_ping(self, connection, _event):
288         "PING arrived, bump the last-received time for the connection."
289         if connection.context:
290             connection.context.handle_ping()
291     def _handle_welcome(self, connection, _event):
292         "Welcome arrived, nick accepted for this connection."
293         if connection.context:
294             connection.context.handle_welcome()
295     def _handle_badnick(self, connection, _event):
296         "Nick not accepted for this connection."
297         if connection.context:
298             connection.context.handle_badnick()
299     def _handle_features(self, connection, event):
300         "Determine if and how we can set deaf mode."
301         if connection.context:
302             cxt = connection.context
303             for lump in event.arguments():
304                 if lump.startswith("DEAF="):
305                     connection.mode(cxt.nickname(), "+"+lump[5:])
306                 elif lump.startswith("MAXCHANNELS="):
307                     m = int(lump[12:])
308                     for pref in "#&+":
309                         cxt.channel_limits[pref] = m
310                     self.debug(1, "%s maxchannels is %d" \
311                                % (connection.server, m))
312                 elif lump.startswith("CHANLIMIT=#:"):
313                     limits = lump[10:].split(",")
314                     try:
315                         for token in limits:
316                             (prefixes, limit) = token.split(":")
317                             limit = int(limit)
318                             for c in prefixes:
319                                 cxt.channel_limits[c] = limit
320                         self.debug(1, "%s channel limit map is %s" \
321                                    % (connection.server, cxt.channel_limits))
322                     except ValueError:
323                         self.logerr("ill-formed CHANLIMIT property")
324     def handle(self, line):
325         "Perform a JSON relay request."
326         try:
327             request = json.loads(line.strip())
328             if type(request) != type({}):
329                 self.logerr("request in tot a JSON dictionary: %s" % repr(request))
330             elif "to" not in request or "privmsg" not in request:
331                 self.logerr("malformed reqest - 'to' or 'privmsg' missing: %s" % repr(request))
332             else:
333                 channels = request['to']
334                 message = request['privmsg']
335                 if type(channels) not in (type([]), type(u"")) \
336                        or type(message) != type(u""):
337                     self.logerr("malformed request - unexpected types: %s" % repr(request))
338                 else:
339                     if type(channels) == type(u""):
340                         channels = [channels]
341                     for url in channels:
342                         if type(url) != type(u""):
343                             self.logerr("malformed request - unexpected type: %s" % repr(request))
344                         else:
345                             target = Target(url)
346                             if target.server() not in self.servers:
347                                 self.servers[target.server()] = Dispatcher(self, target.servername, target.port)
348                             self.servers[target.server()].dispatch(target.channel, message)
349                             # GC dispatchers with no active connections
350                             servernames = self.servers.keys()
351                             for servername in servernames:
352                                 if not self.servers[servername].live():
353                                     del self.servers[servername]
354                             # If we might be pushing a resource limit
355                             # even after garbage collection, remove a
356                             # session.  The goal here is to head off
357                             # DoS attacks that aim at exhausting
358                             # thread space or file descriptors.  The
359                             # cost is that attempts to DoS this
360                             # service will cause lots of join/leave
361                             # spam as we scavenge old channels after
362                             # connecting to new ones. The particular
363                             # method used for selecting a session to
364                             # be terminated doesn't matter much; we
365                             # choose the one longest idle on the
366                             # assumption that message activity is likely
367                             # to be clumpy.
368                             oldest = None
369                             if len(self.servers) >= CONNECTION_MAX:
370                                 for (name, server) in self.servers.items():
371                                     if not oldest or server.last_xmit < self.servers[oldest].last_xmit:
372                                         oldest = name
373                                 del self.servers[oldest]
374         except ValueError:
375             self.logerr("can't recognize JSON on input: %s" % repr(line))
376
377 class IrkerTCPHandler(SocketServer.StreamRequestHandler):
378     def handle(self):
379         while True:
380             line = self.rfile.readline()
381             if not line:
382                 break
383             irker.handle(line.strip())
384
385 class IrkerUDPHandler(SocketServer.BaseRequestHandler):
386     def handle(self):
387         data = self.request[0].strip()
388         #socket = self.request[1]
389         irker.handle(data)
390
391 if __name__ == '__main__':
392     debuglvl = 0
393     (options, arguments) = getopt.getopt(sys.argv[1:], "d:V")
394     for (opt, val) in options:
395         if opt == '-d':         # Enable debug/progress messages
396             debuglvl = int(val)
397             if debuglvl > 1:
398                 logging.basicConfig(level=logging.DEBUG)
399         elif opt == '-V':       # Emit version and exit
400             sys.stdout.write("irkerd version %s\n" % version)
401             sys.exit(0)
402     irker = Irker(debuglevel=debuglvl)
403     tcpserver = SocketServer.TCPServer((HOST, PORT), IrkerTCPHandler)
404     udpserver = SocketServer.UDPServer((HOST, PORT), IrkerUDPHandler)
405     threading.Thread(target=tcpserver.serve_forever).start()
406     threading.Thread(target=udpserver.serve_forever).start()
407     # Main thread has to stay alive forever for the cooperative
408     # scheduling of the green threads to work.
409     if green_threads:
410         while True:
411             time.sleep(10)
412
413 # end