class Session():
"IRC session and message queue processing."
count = 1
- def __init__(self, irc, url):
- self.irc = irc
+ def __init__(self, irker, url):
+ self.irker = irker
self.url = url
# The consumer thread
self.queue = Queue.Queue()
self.servername = host
self.channel = parsed.path.lstrip('/')
self.port = int(port)
- self.server = self.irc.server()
- print "connecting: server=%s port=%s name=%s" % (self.servername, self.port, self.name())
- #self.server.connect(self.servername, self.port, self.name())
+ self.server = self.irker.irc.server()
+ self.irker.debug(1, "connecting: server=%s port=%s name=%s" % (self.servername, self.port, self.name()))
+ self.server.connect(self.servername, self.port, self.name())
Session.count += 1
def enqueue(self, message):
"Enque a message for transmission."
self.queue.join()
def ship(self, channel, message):
"Ship a message to the channel."
- print "%s: %s" % (channel, message)
- #self.server.connection.join(chaannel)
- #self.server.privmsg(channel, message)
+ self.irker.debug(1, "%s gets %s" % (channel, repr(message)))
+ self.server.join(channel)
+ self.server.privmsg(channel, message)
class Irker:
"Persistent IRC multiplexer."
- def __init__(self):
+ def __init__(self, debuglevel=0):
+ self.debuglevel = 0
self.irc = irclib.IRC()
thread = threading.Thread(target=self.irc.process_forever)
self.irc._thread = thread
def logerr(self, errmsg):
"Log a processing error."
sys.stderr.write("irker: " + errmsg + "\n")
+ def debug(self, level, errmsg):
+ "Debugging information."
+ if level >= self.debuglevel:
+ sys.stderr.write("irker: " + errmsg + "\n")
def run(self, ifp, await=True):
"Accept JSON relay requests from specified stream."
while True:
channel = request['channel']
message = request['message']
if channel not in self.sessions:
- self.sessions[channel] = Session(self.irc, channel)
+ self.sessions[channel] = Session(self, channel)
self.sessions[channel].enqueue(message)
if __name__ == '__main__':
- irker = Irker()
+ debuglevel = 0
+ (options, arguments) = getopt.getopt(sys.argv[1:], "-d:")
+ for (opt, val) in options:
+ if opt == '-v':
+ debuglevel = int(val)
+ irker = Irker(debuglevel=debuglevel)
irker.run(sys.stdin)