pvl.irker: implement part
authorTero Marttila <terom@paivola.fi>
Sun, 13 Jan 2013 03:30:11 +0200
changeset 127 f143171884f9
parent 126 bf2555ae7d06
child 128 42d4bd708373
pvl.irker: implement part
pvl/irker/irc.py
pvl/irker/irk.py
--- a/pvl/irker/irc.py	Sun Jan 13 03:11:53 2013 +0200
+++ b/pvl/irker/irc.py	Sun Jan 13 03:30:11 2013 +0200
@@ -51,14 +51,27 @@
 
         self.encoding = encoding
 
+    def encode (self, unicode) :
+        if unicode :
+            return unicode.encode(self.encoding)
+        else :
+            return None
+
     def privmsg (self, *msgs) :
         for msg in msgs :
             # XXX: encode
-            self.client.msg(self.channel, msg.encode(self.encoding))
+            self.client.msg(self.channel, self.encode(msg))
 
     def notice (self, *msgs) :
         for msg in msgs :
-            self.client.notice(self.channel, msg.encode(self.encoding))
+            self.client.notice(self.channel, self.encode(msg))
+
+    def part (self, msg=None) :
+        """
+            Remove channel from our list of channels.
+        """
+
+        self.client.leave(self.channel, self.encode(msg))
 
     def errback (self, failure) :
         """
--- a/pvl/irker/irk.py	Sun Jan 13 03:11:53 2013 +0200
+++ b/pvl/irker/irk.py	Sun Jan 13 03:30:11 2013 +0200
@@ -57,6 +57,8 @@
         Manage connected Irk clients.
     """
 
+    MAXATTR = 16
+
     protocol = Irk
 
     def __init__ (self, irc) :
@@ -77,22 +79,55 @@
         
         # MUST NOT be unicode
         # XXX: ValueError?
-        url = urlparse.urlparse(str(irk['to']))
+        url = urlparse.urlparse(str(irk.pop('to')))
         
-        # connect, join, etc.
+        # connect, register, join
         target = yield self.irc.target(url)
         
-        # privmsg?
-        for attr, method in (
-            ( 'privmsg',    target.privmsg  ),
-            ( 'notice',     target.notice   ),
-        ) :
-            value = irk.get(attr)
+        # dispatch attrs
+        for attr, value in irk.iteritems() :
+            if len(attr) >= self.MAXATTR or not attr.islower() :
+                raise ValueError("invalid attr: %s" % (attr, ))
+    
+            method = getattr(self, 'irk_' + attr, None)
+
+            if not method :
+                raise ValueError("unknown attr: %s" % (attr, ))
 
             if value :
-                # MUST be unicode
                 value = unicode(value)
-                
-                # dispatch
-                method(value)
+            else :
+                value = None
 
+            method(target, value)
+    
+    # XXX: explicitly enable?
+    def irk_privmsg (self, target, value) :
+        """
+            Send PRIVMSG to target.
+        """
+        
+        if not value :
+            # legacy
+            return
+
+        target.privmsg(value)
+
+    def irk_notice (self, target, value) :
+        """
+            Send NOTICE to target.
+        """
+
+        if not value :
+            raise ValueError("empty notice")
+
+        target.notice(value)
+
+    # TODO: refcounting vs join!
+    def irk_part (self, target, value) :
+        """
+            PART target.
+        """
+        
+        # value is optional
+        target.part(value)