bin/pvl.irker
changeset 81 448ed86d0510
child 92 715ca17d1260
equal deleted inserted replaced
80:231d3de7081a 81:448ed86d0510
       
     1 #!/usr/bin/python
       
     2 
       
     3 from twisted.internet import reactor, endpoints, defer
       
     4 from twisted.python import usage, log
       
     5 
       
     6 import urlparse
       
     7 
       
     8 import pvl.irker.irk
       
     9 import pvl.irker.irc
       
    10 
       
    11 class Options (usage.Options) :
       
    12     optParameters = [
       
    13             [ 'irc-nickname', 'n',  pvl.irker.irc.IRCFactory.NICKNAME, "Default IRC nickname" ],
       
    14     ]
       
    15 
       
    16     def __init__ (self) :
       
    17         usage.Options.__init__(self)
       
    18 
       
    19         self.listen = []
       
    20         self.connect = []
       
    21         self.target = []
       
    22         self.privmsg = []
       
    23 
       
    24     def opt_listen_tcp (self, listen) :
       
    25         """
       
    26             Twisted endpoint.
       
    27         """
       
    28 
       
    29         self.listen.append((endpoints.TCP4ServerEndpoint, (int(listen), )))
       
    30 
       
    31     def opt_connect (self, connect) :
       
    32         """
       
    33             Connect to given target.
       
    34         """
       
    35 
       
    36         self.connect.append(urlparse.urlparse(connect))
       
    37 
       
    38     def opt_target (self, target) :
       
    39         """
       
    40             Join given target.
       
    41         """
       
    42 
       
    43         self.target.append(urlparse.urlparse(target))
       
    44 
       
    45     def opt_privmsg (self, privmsg) :
       
    46         """
       
    47             Send message to targets
       
    48         """
       
    49 
       
    50         self.privmsg.append(privmsg)
       
    51 
       
    52 @defer.inlineCallbacks
       
    53 def connect (irc, connect) :
       
    54     """
       
    55         Connect to given urls.
       
    56     """
       
    57             
       
    58     try :
       
    59         clients = yield defer.gatherResults([irc.client(url) for url in connect])
       
    60 
       
    61     except Exception as ex :
       
    62         log.err(ex)
       
    63         return
       
    64 
       
    65     for client in clients :
       
    66         log.msg('--connect', client)
       
    67 
       
    68 @defer.inlineCallbacks
       
    69 def target (irc, target, privmsg) :
       
    70     """
       
    71         Connect to given urls.
       
    72     """
       
    73             
       
    74     try :
       
    75         targets = yield defer.gatherResults([irc.target(url) for url in target])
       
    76 
       
    77     except Exception as ex :
       
    78         log.err(ex)
       
    79         return
       
    80 
       
    81     for target in targets :
       
    82         log.msg('--target', target)
       
    83 
       
    84         target.privmsg(*privmsg)
       
    85 
       
    86 def main (args) :
       
    87     options = Options()
       
    88     options.parseOptions(args)
       
    89 
       
    90     # logging
       
    91     log.startLogging(sys.stderr, setStdout=False)
       
    92 
       
    93     # connect
       
    94     irc = pvl.irker.irc.IRCFactory()
       
    95     
       
    96     connect(irc, options.connect)
       
    97     target(irc, options.target, options.privmsg)
       
    98 
       
    99     # listen
       
   100     irk = pvl.irker.irk.IrkFactory(irc)
       
   101 
       
   102     for endpoint, args in options.listen :
       
   103         endpoint = endpoint(reactor, *args)
       
   104 
       
   105         log.msg("listen:", endpoint)
       
   106 
       
   107         endpoint.listen(irk)
       
   108 
       
   109 
       
   110     # go
       
   111     reactor.run()
       
   112 
       
   113     return 0
       
   114 
       
   115 if __name__ == '__main__' :
       
   116     import sys
       
   117     sys.exit(main(sys.argv[1:]))