import sys, getopt, urlparse, time, random, socket, signal, re
import threading, Queue, SocketServer, select
-import logging
try:
import simplejson as json # Faster, also makes us Python-2.4-compatible
except ImportError:
class IRCClient():
"An IRC client session to one or more servers."
- def __init__(self):
+ def __init__(self, debuglevel):
self.mutex = threading.RLock()
self.server_connections = []
self.event_handlers = {}
self.add_event_handler("ping",
lambda c, e: c.ship("PONG %s" % e.target))
+ self.debuglevel = debuglevel
def newserver(self):
"Initialize a new server-connection object."
with self.mutex:
self.server_connections.remove(connection)
+ def debug(self, level, errmsg):
+ "Debugging information."
+ if self.debuglevel >= level:
+ sys.stderr.write("irkerd: %s\n" % errmsg)
+
class LineBufferedStream():
"Line-buffer a read stream."
crlf_re = re.compile(b'\r?\n')
def connect(self, server, port, nickname,
password=None, username=None, ircname=None):
- log.debug("connect(server=%r, port=%r, nickname=%r, ...)",
- server, port, nickname)
+ self.master.debug(2, "connect(server=%r, port=%r, nickname=%r, ...)" %
+ (server, port, nickname))
if self.socket is not None:
self.disconnect("Changing servers")
self.buffer.append(incoming)
for line in self.buffer:
- log.debug("FROM: %s", line)
+ self.master.debug(2, "FROM: %s" % line)
if not line:
continue
target = arguments[0]
arguments = arguments[1:]
- log.debug("command: %s, source: %s, target: %s, "
- "arguments: %s", command, prefix, target, arguments)
+ self.master.debug(2,
+ "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):
"Ship a command to the server, appending CR/LF"
try:
self.socket.send(string + b'\r\n')
- log.debug("TO: %s", string)
+ self.master.debug(2, "TO: %s" % string)
except socket.error:
self.disconnect("Connection reset by peer.")
"Persistent IRC multiplexer."
def __init__(self, debuglevel=0):
self.debuglevel = debuglevel
- self.irc = IRCClient()
+ self.irc = IRCClient(self.debuglevel)
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)
""")
if __name__ == '__main__':
- log = logging.getLogger(__name__)
debuglvl = 0
namestyle = "irker%03d"
password = None
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 == '-l': # Logfile mode - report traffic read in
logfile = val
elif opt == '-n': # Force the nick