--- a/bin/pvl.irker Fri Jan 11 23:19:40 2013 +0200
+++ b/bin/pvl.irker Sat Jan 12 00:05:22 2013 +0200
@@ -1,116 +1,7 @@
#!/usr/bin/python
-from twisted.internet import reactor, endpoints, defer
-from twisted.python import usage, log
-
-import urlparse
-
-import pvl.irker.irk
-import pvl.irker.irc
-
-class Options (usage.Options) :
- optParameters = [
- [ 'irc-nickname', 'n', pvl.irker.irc.IRCFactory.NICKNAME, "Default IRC nickname" ],
- ]
-
- def __init__ (self) :
- usage.Options.__init__(self)
-
- self.listen = []
- self.connect = []
- self.target = []
- self.privmsg = []
-
- def opt_listen_tcp (self, listen) :
- """
- Twisted endpoint.
- """
-
- self.listen.append((endpoints.TCP4ServerEndpoint, (int(listen), )))
-
- def opt_connect (self, connect) :
- """
- Connect to given target.
- """
-
- self.connect.append(urlparse.urlparse(connect))
-
- def opt_target (self, target) :
- """
- Join given target.
- """
-
- self.target.append(urlparse.urlparse(target))
-
- def opt_privmsg (self, privmsg) :
- """
- Send message to targets
- """
-
- self.privmsg.append(privmsg)
-@defer.inlineCallbacks
-def connect (irc, connect) :
- """
- Connect to given urls.
- """
-
- try :
- clients = yield defer.gatherResults([irc.client(url) for url in connect])
-
- except Exception as ex :
- log.err(ex)
- return
-
- for client in clients :
- log.msg('--connect', client)
-
-@defer.inlineCallbacks
-def target (irc, target, privmsg) :
- """
- Connect to given urls.
- """
-
- try :
- targets = yield defer.gatherResults([irc.target(url) for url in target])
-
- except Exception as ex :
- log.err(ex)
- return
-
- for target in targets :
- log.msg('--target', target)
-
- target.privmsg(*privmsg)
-
-def main (args) :
- options = Options()
- options.parseOptions(args)
-
- # logging
- log.startLogging(sys.stderr, setStdout=False)
-
- # connect
- irc = pvl.irker.irc.IRCFactory()
-
- connect(irc, options.connect)
- target(irc, options.target, options.privmsg)
-
- # listen
- irk = pvl.irker.irk.IrkFactory(irc)
-
- for endpoint, args in options.listen :
- endpoint = endpoint(reactor, *args)
-
- log.msg("listen:", endpoint)
-
- endpoint.listen(irk)
-
-
- # go
- reactor.run()
-
- return 0
+from pvl.irker.irker import main
if __name__ == '__main__' :
import sys
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/pvl/irker/irker.py Sat Jan 12 00:05:22 2013 +0200
@@ -0,0 +1,151 @@
+"""
+ Twisted application
+"""
+
+import sys
+
+from twisted.application import internet, service
+from twisted.internet import reactor, endpoints, defer
+from twisted.python import usage, log
+
+import urlparse
+
+import pvl.irker.irk
+import pvl.irker.irc
+
+class Options (usage.Options) :
+ optParameters = [
+ [ 'irc-nickname', 'n', pvl.irker.irc.IRCFactory.NICKNAME, "Default IRC nickname" ],
+ ]
+
+ def __init__ (self) :
+ usage.Options.__init__(self)
+
+ self.listen_tcp = []
+ self.connect = []
+ self.target = []
+ self.privmsg = []
+
+ def opt_listen_tcp (self, listen) :
+ """
+ Twisted endpoint.
+ """
+
+ if ':' in listen :
+ host, port = listen.split(':')
+ else :
+ host, port = '', listen
+
+ port = int(port)
+
+ self.listen_tcp.append((host, port))
+
+ def opt_connect (self, connect) :
+ """
+ Connect to given target.
+ """
+
+ self.connect.append(urlparse.urlparse(connect))
+
+ def opt_target (self, target) :
+ """
+ Join given target.
+ """
+
+ self.target.append(urlparse.urlparse(target))
+
+ def opt_privmsg (self, privmsg) :
+ """
+ Send message to targets
+ """
+
+ self.privmsg.append(privmsg)
+
+@defer.inlineCallbacks
+def connect (irc, connect) :
+ """
+ Connect to given urls.
+ """
+
+ try :
+ clients = yield defer.gatherResults([irc.client(url) for url in connect])
+
+ except Exception as ex :
+ log.err(ex)
+ return
+
+ for client in clients :
+ log.msg('--connect', client)
+
+@defer.inlineCallbacks
+def target (irc, target, privmsg) :
+ """
+ Connect to given urls.
+ """
+
+ try :
+ targets = yield defer.gatherResults([irc.target(url) for url in target])
+
+ except Exception as ex :
+ log.err(ex)
+ return
+
+ for target in targets :
+ log.msg('--target', target)
+
+ target.privmsg(*privmsg)
+
+def makeService (options) :
+ """
+ Return a Service for running irk -> irc.
+ """
+
+ s = service.MultiService()
+
+ # IRC
+ irc = pvl.irker.irc.IRCFactory(options['irc-nickname'])
+
+ connect(irc, options.connect)
+ target(irc, options.target, options.privmsg)
+
+ # IRK
+ irk = pvl.irker.irk.IrkFactory(irc)
+
+ for host, port in options.listen_tcp :
+ ss = internet.TCPServer(port, irk, interface=host)
+
+ log.msg("--listen", port)
+
+ ss.setServiceParent(s)
+
+ # return the service collection
+ return s
+
+def main (args) :
+ options = Options()
+ options.parseOptions(args)
+
+ # logging
+ log.startLogging(sys.stderr, setStdout=False)
+
+ # connect
+ irc = pvl.irker.irc.IRCFactory()
+
+ connect(irc, options.connect)
+ target(irc, options.target, options.privmsg)
+
+ # listen
+ irk = pvl.irker.irk.IrkFactory(irc)
+
+ for host, port in options.listen_tcp :
+ endpoint = endpoints.TCP4ServerEndpoint(reactor, port, interface=host)
+
+ log.msg("listen:", endpoint)
+
+ endpoint.listen(irk)
+
+ # go
+ reactor.run()
+
+ return 0
+
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/twisted/plugins/pvlirker_plugin.py Sat Jan 12 00:05:22 2013 +0200
@@ -0,0 +1,20 @@
+from zope.interface import implements
+
+from twisted import plugin
+from twisted.application import service
+from twisted.python import log
+
+import pvl.irker.irker
+
+class ServiceMaker (object) :
+ implements(service.IServiceMaker, plugin.IPlugin)
+ tapname = 'pvl-irker'
+ description = "Irker daemon"
+ options = pvl.irker.irker.Options
+
+ def makeService (self, options) :
+ log.msg('makeService', options)
+
+ return pvl.irker.irker.makeService(options)
+
+serviceMaker = ServiceMaker()