pvl/irker/irk.py
changeset 127 f143171884f9
parent 93 d85d825cbca7
equal deleted inserted replaced
126:bf2555ae7d06 127:f143171884f9
    55 class IrkFactory (protocol.ServerFactory) :
    55 class IrkFactory (protocol.ServerFactory) :
    56     """
    56     """
    57         Manage connected Irk clients.
    57         Manage connected Irk clients.
    58     """
    58     """
    59 
    59 
       
    60     MAXATTR = 16
       
    61 
    60     protocol = Irk
    62     protocol = Irk
    61 
    63 
    62     def __init__ (self, irc) :
    64     def __init__ (self, irc) :
    63         self.irc = irc
    65         self.irc = irc
    64     
    66     
    75         if not 'to' in irk :
    77         if not 'to' in irk :
    76             raise ValueError("missing target: to")
    78             raise ValueError("missing target: to")
    77         
    79         
    78         # MUST NOT be unicode
    80         # MUST NOT be unicode
    79         # XXX: ValueError?
    81         # XXX: ValueError?
    80         url = urlparse.urlparse(str(irk['to']))
    82         url = urlparse.urlparse(str(irk.pop('to')))
    81         
    83         
    82         # connect, join, etc.
    84         # connect, register, join
    83         target = yield self.irc.target(url)
    85         target = yield self.irc.target(url)
    84         
    86         
    85         # privmsg?
    87         # dispatch attrs
    86         for attr, method in (
    88         for attr, value in irk.iteritems() :
    87             ( 'privmsg',    target.privmsg  ),
    89             if len(attr) >= self.MAXATTR or not attr.islower() :
    88             ( 'notice',     target.notice   ),
    90                 raise ValueError("invalid attr: %s" % (attr, ))
    89         ) :
    91     
    90             value = irk.get(attr)
    92             method = getattr(self, 'irk_' + attr, None)
       
    93 
       
    94             if not method :
       
    95                 raise ValueError("unknown attr: %s" % (attr, ))
    91 
    96 
    92             if value :
    97             if value :
    93                 # MUST be unicode
       
    94                 value = unicode(value)
    98                 value = unicode(value)
    95                 
    99             else :
    96                 # dispatch
   100                 value = None
    97                 method(value)
       
    98 
   101 
       
   102             method(target, value)
       
   103     
       
   104     # XXX: explicitly enable?
       
   105     def irk_privmsg (self, target, value) :
       
   106         """
       
   107             Send PRIVMSG to target.
       
   108         """
       
   109         
       
   110         if not value :
       
   111             # legacy
       
   112             return
       
   113 
       
   114         target.privmsg(value)
       
   115 
       
   116     def irk_notice (self, target, value) :
       
   117         """
       
   118             Send NOTICE to target.
       
   119         """
       
   120 
       
   121         if not value :
       
   122             raise ValueError("empty notice")
       
   123 
       
   124         target.notice(value)
       
   125 
       
   126     # TODO: refcounting vs join!
       
   127     def irk_part (self, target, value) :
       
   128         """
       
   129             PART target.
       
   130         """
       
   131         
       
   132         # value is optional
       
   133         target.part(value)