More fixes on purple.pyx.
[python-purple] / nullclient.py
index bd428d0..a79f3da 100644 (file)
@@ -2,6 +2,7 @@ import purple
 import ecore
 import getpass
 import sys
 import ecore
 import getpass
 import sys
+from xml.dom import minidom
 
 cbs = {}
 acc_cbs = {}
 
 cbs = {}
 acc_cbs = {}
@@ -10,7 +11,6 @@ conn_cbs = {}
 conv_cbs = {}
 notify_cbs = {}
 request_cbs = {}
 conv_cbs = {}
 notify_cbs = {}
 request_cbs = {}
-signal_cbs = {}
 
 def account_callback(name):
     print "---- account callback example: %s" % name
 
 def account_callback(name):
     print "---- account callback example: %s" % name
@@ -27,16 +27,67 @@ def blist_callback(name):
     print "---- blist callback example: %s" % name
 
 blist_cbs["new_list"] = blist_callback
     print "---- blist callback example: %s" % name
 
 blist_cbs["new_list"] = blist_callback
-blist_cbs["new_node"] = blist_callback
 blist_cbs["show"] = blist_callback
 blist_cbs["show"] = blist_callback
-blist_cbs["update"] = blist_callback
-blist_cbs["remove"] = blist_callback
 blist_cbs["destroy"] = blist_callback
 blist_cbs["set_visible"] = blist_callback
 blist_cbs["request_add_buddy"] = blist_callback
 blist_cbs["request_add_chat"] = blist_callback
 blist_cbs["request_add_group"] = blist_callback
 
 blist_cbs["destroy"] = blist_callback
 blist_cbs["set_visible"] = blist_callback
 blist_cbs["request_add_buddy"] = blist_callback
 blist_cbs["request_add_chat"] = blist_callback
 blist_cbs["request_add_group"] = blist_callback
 
+def new_node_cb(type, name=None, totalsize=None, currentsize=None, online=None):
+    if type == 0:
+        print "---- blist callback: new node (group): %s (%s/%s) (%s online)" % \
+                (name, totalsize, currentsize, online)
+    elif type == 1:
+        print "---- blist callback: new node (contact): %s (%s/%s) (%s online)" % \
+                (name, totalsize, currentsize, online)
+    elif type == 2: # totalsize = alias
+        print "---- blist callback: new node (buddy): %s (%s)" % (name, totalsize)
+    elif type == 3:
+        print "---- blist callback: new node (chat): %s" % alias
+    elif type == 4:
+        print "---- blist callback: new node (other type)"
+    else:
+        print "---- blist callback: new node (unknown type %s)" % type
+
+def update_cb(type, name=None, totalsize=None, currentsize=None, online=None):
+    if type == 0:
+        print "---- blist callback: update (group): %s (%s/%s) (%s online)" % \
+                (name, totalsize, currentsize, online)
+    elif type == 1:
+        print "---- blist callback: update (contact): %s (%s/%s) (%s online)" % \
+                (name, totalsize, currentsize, online)
+    elif type == 2: # totalsize = alias
+        print "---- blist callback: update (buddy): %s (%s)" % \
+                (name, totalsize)
+    elif type == 3:
+        print "---- blist callback: update (chat): %s" % alias
+    elif type == 4:
+        print "---- blist callback: update (other type)"
+    else:
+        print "---- blist callback: update (unknown type %s)" % type
+
+def remove_cb(type, name=None, totalsize=None, currentsize=None, online=None):
+    if type == 0:
+        print "---- blist callback: remove (group): %s (%s/%s) (%s online)" % \
+                (name, totalsize, currentsize, online)
+    elif type == 1:
+        print "---- blist callback: remove (contact): %s (%s/%s) (%s online)" % \
+                (name, totalsize, currentsize, online)
+    elif type == 2: # totalsize = alias
+        print "---- blist callback: remove (buddy): %s (%s)" % \
+                (name, totalsize)
+    elif type == 3:
+        print "---- blist callback: remove (chat): %s" % alias
+    elif type == 4:
+        print "---- blist callback: remove (other type)"
+    else:
+        print "---- blist callback: remove (unknown type %s)" % type
+
+blist_cbs["new_node"] = new_node_cb
+blist_cbs["update"] = update_cb
+blist_cbs["remove"] = remove_cb
+
 cbs["blist"] = blist_cbs
 
 def conn_callback(name):
 cbs["blist"] = blist_cbs
 
 def conn_callback(name):
@@ -72,10 +123,13 @@ cbs["connection"] = conn_cbs
 def conv_callback(name):
     print "---- conversation callback example: %s" % name
 
 def conv_callback(name):
     print "---- conversation callback example: %s" % name
 
+def write_im_cb(name, message):
+    print "---- (conversation) write_im: %s %s" % (name, message)
+
 conv_cbs["create_conversation"] = conv_callback
 conv_cbs["destroy_conversation"] = conv_callback
 conv_cbs["write_chat"] = conv_callback
 conv_cbs["create_conversation"] = conv_callback
 conv_cbs["destroy_conversation"] = conv_callback
 conv_cbs["write_chat"] = conv_callback
-conv_cbs["write_im"] = conv_callback
+conv_cbs["write_im"] = write_im_cb
 conv_cbs["write_conv"] = conv_callback
 conv_cbs["chat_add_users"] = conv_callback
 conv_cbs["chat_rename_user"] = conv_callback
 conv_cbs["write_conv"] = conv_callback
 conv_cbs["chat_add_users"] = conv_callback
 conv_cbs["chat_rename_user"] = conv_callback
@@ -119,35 +173,42 @@ request_cbs["request_folder"] = request_callback
 cbs["request"] = request_cbs
 
 def buddy_signed_off_cb(name):
 cbs["request"] = request_cbs
 
 def buddy_signed_off_cb(name):
-    print "---- sign off from buddy %s" % name
+    print "---- (signal) sign off from buddy %s" % name
 
 def receiving_im_msg_cb(sender, name, message):
 
 def receiving_im_msg_cb(sender, name, message):
-    print "---- receiving IM message from %s: %s" % (name, message)
+    print "---- (signal) receiving IM message from %s: %s" % (name, message)
     return False
 
     return False
 
-signal_cbs["buddy_signed_off"] = buddy_signed_off_cb
-signal_cbs["receiving_im_msg"] = receiving_im_msg_cb
+def jabber_received_xmlnode_cb(message):
+    xml = minidom.parse(message)
+
+    for msg in xml.getElementsByTagName("message"):
+        who = msg.getAttribute("from")
+        for geoloc in msg.getElementsByTagNameNS("http://jabber.org/protocol/geoloc", "geoloc"):
+            lat = geoloc.getElementsByTagName("lat")[0].childNodes[0].nodeValue
+            lon = geoloc.getElementsByTagName("lon")[0].childNodes[0].nodeValue
+            print "who: %s lat: %s lon: %s" % (who, lat, lon)
 
 class NullClient:
     def __init__(self):
         self.p = purple.Purple(debug_enabled=False)
         self.account = None
 
 class NullClient:
     def __init__(self):
         self.p = purple.Purple(debug_enabled=False)
         self.account = None
+        self.protocol_id = "prpl-jabber"
 
     def execute(self):
         global cbs
         self.p.purple_init(cbs)
 
 
     def execute(self):
         global cbs
         self.p.purple_init(cbs)
 
-    def set_protocol(self, protocol):
-        for i in self.p.get_protocols():
-            if i[1] == protocol:
-                print "-- NULLCLIENT --: Choosing %s as protocol" % protocol
-                self.protocol = i[0]
-                print "-- NULLCLIENT --: Protocol successfully chosen: %s" % i[0]
-                return
-
-    def new_account(self, username, protocol, password):
-        self.account = purple.Account(username, protocol)
-        self.account.set_password(password)
+    def new_account(self, username, password):
+        self.account = purple.Account(username, self.protocol_id)
+        self.account.password = password
+
+        self.account.proxy.set_type(purple.ProxyInfoType().HTTP)
+        self.account.proxy.set_host("172.18.216.211")
+        self.account.proxy.set_port(8080)
+
+        self.account.get_protocol_options()
+
         self.account.set_enabled("carman-purple-python", True)
     def get_buddies(self):
         buddies = self.account.get_buddies_online()
         self.account.set_enabled("carman-purple-python", True)
     def get_buddies(self):
         buddies = self.account.get_buddies_online()
@@ -162,15 +223,14 @@ def getpassword():
     return getpass.getpass()
 
 if __name__ == '__main__':
     return getpass.getpass()
 
 if __name__ == '__main__':
-
     client = NullClient()
     client.execute()
     client = NullClient()
     client.execute()
-    client.set_protocol("XMPP")
+    client.p.signal_connect("buddy-signed-off", buddy_signed_off_cb)
+    client.p.signal_connect("receiving-im-msg", receiving_im_msg_cb)
+    client.p.signal_connect("jabber-receiving-xmlnode", jabber_received_xmlnode_cb)
     username = getuser()
     password = getpassword()
     username = getuser()
     password = getpassword()
-    client.new_account(username, client.protocol, password)
 
     client.p.connect()
 
     client.p.connect()
-    client.p.attach_signals(signal_cbs)
     ecore.timer_add(20, client.get_buddies)
     ecore.main_loop_begin()
     ecore.timer_add(20, client.get_buddies)
     ecore.main_loop_begin()