changeset 2968:2a5fa7d79d86

GHOP: 2/3 of XEP-0115 implementation by bolekk
author Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
date Sun, 30 Dec 2007 23:49:08 +0100
parents fcfa07f447f5
children 690f64f95f91
files jabberbot/capat.py jabberbot/xmppbot.py
diffstat 2 files changed, 61 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/jabberbot/capat.py	Sun Dec 09 22:40:44 2007 +0100
+++ b/jabberbot/capat.py	Sun Dec 30 23:49:08 2007 +0100
@@ -14,6 +14,7 @@
 import base64
 import itertools
 from MoinMoin.support.python_compatibility import hash_new
+from pyxmpp.presence import Presence
 
 HASHALIASES = { # IANA Hash Function Textual Names Registry
                 # to `hashlib.new` mapping
@@ -81,3 +82,43 @@
             feat.append(item.prop('var'))
 
     return generate_ver(ident, feat, algo)
+
+# <identity /> and <feature /> attributes
+IDENT = (('category', 'client'),
+         ('type', 'bot'))
+FEAT = ('http://jabber.org/protocol/disco#info',
+        'jabber:x:data') # data forms
+NODE = "http://moinmo.in/#1.7"
+
+def create_presence(jid):
+    """ Creates a presence stanza (as described in XEP-0115)
+
+    @param jid: bot's jabber ID
+    """
+    pres = Presence(from_jid=jid)
+
+    c = pres.add_new_content('http://jabber.org/protocol/caps', 'c')
+    c.setProp('node', NODE)
+
+    ver = generate_ver(IDENT, FEAT)
+    c.setProp('ver', ver)
+
+    return pres
+
+def get_response(disco_query):
+    """ Creates an <Iq /> tag as a response to a service discovery query
+
+    @param disco_query: received query
+    """
+    response = disco_query.make_result_response()
+    query = response.new_query(ns_uri='http://jabber.org/protocol/disco#info')
+
+    ident = query.newChild(None, 'identity', None)
+    for item in IDENT:
+        ident.setProp(item[0], item[1])
+
+    for item in FEAT:
+        query.newChild(None, 'feature', None).setProp('var', item)
+
+    return response
+
--- a/jabberbot/xmppbot.py	Sun Dec 09 22:40:44 2007 +0100
+++ b/jabberbot/xmppbot.py	Sun Dec 30 23:49:08 2007 +0100
@@ -21,6 +21,7 @@
 import jabberbot.commands as cmd
 import jabberbot.i18n as i18n
 import jabberbot.oob as oob
+import jabberbot.capat as capat
 
 
 class Contact:
@@ -250,6 +251,17 @@
             self.expire_contacts(current_time)
             self.last_expiration = current_time
 
+    def session_started(self):
+        """Handle session started event.
+	Requests the user's roster and sends the initial presence with
+        a <c> child as described in XEP-0115 (Entity Capabilities)
+
+	"""
+        self.request_roster()
+        pres = capat.create_presence(self.jid)
+        self.stream.set_iq_get_handler("query", "http://jabber.org/protocol/disco#info", self.handle_disco_query)
+        self.stream.send(pres)
+
     def expire_contacts(self, current_time):
         """Check which contats have been offline for too long and should be removed
 
@@ -1257,6 +1269,14 @@
         request = cmd.GetUserLanguage(jid)
         self.from_commands.put_nowait(request)
 
+    def handle_disco_query(self, stanza):
+        """Handler for <Iq /> service discovery query
+
+	@param stanza: received query stanza
+	"""
+        response = capat.get_response(stanza)
+        self.get_stream().send(response)
+
     def service_discovery(self, jid):
         """Ask a client about supported features