--- a/pvl/irker/irker.py Tue Feb 19 19:28:40 2013 +0200
+++ /dev/null Thu Jan 01 00:00:00 1970 +0000
@@ -1,162 +0,0 @@
-"""
- 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
-
-def myusername ():
- """
- Return username current process is running under.
- """
-
- import os, pwd
-
- return pwd.getpwuid(os.getuid()).pw_name
-
-
-class Options (usage.Options) :
- optParameters = [
- [ 'irc-nickname', 'n', pvl.irker.irc.IRCFactory.NICKNAME, "Default IRC nickname" ],
- [ 'irc-username', 'u', myusername(), "IRC username (default: system user)" ],
- ]
-
- 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'], options['irc-username'])
-
- 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(options['irc-nickname'], options['irc-username'])
-
- 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
-