changeset 102:94011f553cd0

misc bugfixes - wix: added forgotten profile in gateways management - xml_tools: removed XMLClass to the 2 methods as a direct methods - plugin_xep_100 (gateways discovery): added error callback
author Goffi <goffi@goffi.org>
date Tue, 22 Jun 2010 13:58:53 +0800
parents 783e9d6980ec
children 6be927a465ed
files frontends/quick_frontend/quick_app.py frontends/wix/card_game.py frontends/wix/gateways.py frontends/wix/main_window.py plugins/plugin_misc_cs.py plugins/plugin_misc_tarot.py plugins/plugin_xep_0077.py plugins/plugin_xep_0100.py sat.tac tools/xml_tools.py
diffstat 10 files changed, 115 insertions(+), 94 deletions(-) [+]
line wrap: on
line diff
--- a/frontends/quick_frontend/quick_app.py	Sat Jun 19 17:15:30 2010 +0800
+++ b/frontends/quick_frontend/quick_app.py	Tue Jun 22 13:58:53 2010 +0800
@@ -156,7 +156,7 @@
             return
         debug(_("Connected"))
         self.setStatusOnline(True)
-        self.bridge.joinMUC('conference.necton2.int', 'test', self.profiles[self.profile]['whoami'].node, self.profile) #gof:
+        #self.bridge.joinMUC('conference.necton2.int', 'test', self.profiles[self.profile]['whoami'].node, self.profile) #gof:
         
 
     def disconnected(self, profile):
--- a/frontends/wix/card_game.py	Sat Jun 19 17:15:30 2010 +0800
+++ b/frontends/wix/card_game.py	Tue Jun 22 13:58:53 2010 +0800
@@ -33,7 +33,7 @@
 MIN_WIDTH = 950 #Minimum size of the panel
 MIN_HEIGHT = 500
 
-suits_order = ['pique', 'coeur', 'trefle', 'carreau', 'atout'] #I have swith the usual order 'trefle' and 'carreau' because card are more easy to see if suit colour change (black, red, black, red)
+suits_order = ['pique', 'coeur', 'trefle', 'carreau', 'atout'] #I have switched the usual order 'trefle' and 'carreau' because card are more easy to see if suit colour change (black, red, black, red)
 values_order = map(str,range(1,11))+["valet","cavalier","dame","roi"]
 
 class Card():
--- a/frontends/wix/gateways.py	Sat Jun 19 17:15:30 2010 +0800
+++ b/frontends/wix/gateways.py	Tue Jun 22 13:58:53 2010 +0800
@@ -105,7 +105,7 @@
         """Open the gateway manager on given server"""
         server = self.ext_server_text.GetValue()
         debug(_("Opening gateways manager on [%s]") % server) 
-        id = self.host.bridge.findGateways(server)
+        id = self.host.bridge.findGateways(server, self.host.profile)
         self.host.current_action_ids.add(id)
         self.host.current_action_ids_cb[id] = self.host.onGatewaysFound
         self.MakeModal(False) 
@@ -157,7 +157,7 @@
         def register_cb(event):
             """Called when register button is clicked"""
             gateway_jid = event.GetEventObject().gateway_jid
-            id = self.host.bridge.in_band_register(gateway_jid)
+            id = self.host.bridge.in_band_register(gateway_jid, self.host.profile)
             self.host.current_action_ids.add(id)
             self.MakeModal(False) 
             self.Destroy()
@@ -165,7 +165,7 @@
         def unregister_cb(event):
             """Called when unregister button is clicked"""
             gateway_jid = event.GetEventObject().gateway_jid
-            id = self.host.bridge.gatewayRegister("CANCEL",gateway_jid, None)
+            id = self.host.bridge.gatewayRegister("CANCEL",gateway_jid, None, self.host.profile)
             self.host.current_action_ids.add(id)
             self.MakeModal(False) 
             self.Destroy()
--- a/frontends/wix/main_window.py	Sat Jun 19 17:15:30 2010 +0800
+++ b/frontends/wix/main_window.py	Tue Jun 22 13:58:53 2010 +0800
@@ -452,7 +452,7 @@
 
     def onFindGateways(self, e):
         debug(_("Find Gateways request"))
-        id = self.bridge.findGateways(self.profiles[self.profile]['whoami'].domain)
+        id = self.bridge.findGateways(self.profiles[self.profile]['whoami'].domain, self.profile)
         self.current_action_ids.add(id)
         self.current_action_ids_cb[id] = self.onGatewaysFound
 
--- a/plugins/plugin_misc_cs.py	Sat Jun 19 17:15:30 2010 +0800
+++ b/plugins/plugin_misc_cs.py	Tue Jun 22 13:58:53 2010 +0800
@@ -26,7 +26,6 @@
 from twisted.words.protocols.jabber import error as jab_error
 from twisted.words.protocols.jabber.xmlstream import IQ
 from twisted.web.client import getPage
-#from twisted.web.http_headers import Headers
 import os.path
 import pdb
 import random
@@ -34,10 +33,10 @@
 from zope.interface import implements
 
 from wokkel import disco, iwokkel, data_form
-from tools.xml_tools import XMLTools
-#from twisted.web.iweb import IBodyProducer
+from tools.xml_tools import dataForm2xml
 import urllib
 
+from BeautifulSoup import BeautifulSoup
 
 
 
@@ -74,7 +73,13 @@
         host.memory.importParams(CS_Plugin.params)
         #menu
         host.importMenu(_("Plugin"), "CouchSurfing", self.menuSelected, help_string = _("Launch CoushSurfing mangement interface"))
-        self.session_cookies={} #TODO: delete cookies after a while
+        self.data={} #TODO: delete cookies/data after a while
+
+    def erroCB(self, e, id):
+        """Called when something is going wrong when contacting CS website"""
+        message_data={"reason": "connection error", "message":_(u"Impossible to contact CS website, please check your login/password, connection or try again later")}
+        self.host.bridge.actionResult("ERROR", id, message_data)
+
 
     def menuSelected(self, id, profile):
         """Called when the couchsurfing menu item is selected"""
@@ -87,24 +92,36 @@
 
         post_data = urllib.urlencode({'auth_login[un]':login,'auth_login[pw]':password,'auth_login[action]':'Login...'}) 
         
-        cookies = {}
-        d = getPage('http://www.couchsurfing.org/login.html', method='POST', postdata=post_data, headers={'Content-Type':'application/x-www-form-urlencoded'} , agent=AGENT, cookies=cookies)
-        #d = getPage('file:///home/goffi/tmp/CS_principale.html', method='POST', postdata=post_data, headers={'Content-Type':'application/x-www-form-urlencoded'} , agent=AGENT, cookies=cookies)
-        
+        self.data[profile] = {'cookies':{}}
 
         def connectionCB(html):
             print 'Response received'
-
-            d = getPage('http://www.couchsurfing.org/messages.html?message_status=inbox', agent=AGENT, cookies=cookies)
-            #d = getPage('file:///home/goffi/tmp/CS_inbox.html', agent=AGENT, cookies=cookies)
-            def toto(html):
-                print "cookies:",cookies
-                pdb.set_trace()
-            d.addBoth(toto)
-        d.addCallback(connectionCB)
-
-
-        self.host.bridge.actionResult("SUPPRESS", id, {})
+            soup = self.data[profile]['soup'] = BeautifulSoup(html)
+            user_nick = soup.find('a','item_link',href='/home.html').contents[0]
+            user_name = soup.html.head.title.string.split(' - ')[1]
+            unread_messages = int(soup.find('div','item_bubble').a.string)
+            form_xml = """
+            <form>
+                <elem type='text' value='Welcome %(name)s, you have %(nb_message)i unread messages'/>
+            </form>
+            """ % {'name':user_name, 'nb_message':unread_messages}
+            self.host.bridge.actionResult("FORM", id, {"type":"window", "xml":form_xml})
 
 
 
+            #d = getPage('http://www.couchsurfing.org/messages.html?message_status=inbox', agent=AGENT, cookies=self.session_cookies[profile])
+       
+        #tmp
+        f = open('/home/goffi/tmp/CS_principale.html','r')
+        html = f.read()
+        connectionCB(html)
+
+        """d = getPage('http://www.couchsurfing.org/login.html', method='POST', postdata=post_data, headers={'Content-Type':'application/x-www-form-urlencoded'} , agent=AGENT, cookies=self.data[profile]['cookies'])
+        d.addCallback(connectionCB)
+        d.addErrback(self.erroCB, id)"""
+
+
+        #self.host.bridge.actionResult("SUPPRESS", id, {})
+
+
+
--- a/plugins/plugin_misc_tarot.py	Sat Jun 19 17:15:30 2010 +0800
+++ b/plugins/plugin_misc_tarot.py	Tue Jun 22 13:58:53 2010 +0800
@@ -32,7 +32,7 @@
 from zope.interface import implements
 
 from wokkel import disco, iwokkel, data_form
-from tools.xml_tools import XMLTools
+from tools.xml_tools import dataForm2xml
 
 try:
     from twisted.words.protocols.xmlstream import XMPPHandler
@@ -55,7 +55,7 @@
 "description": _("""Implementation of Tarot card game""")
 }
 
-suits_order = ['pique', 'coeur', 'trefle', 'carreau', 'atout'] #I have swith the usual order 'trefle' and 'carreau' because card are more easy to see if suit colour change (black, red, black, red)
+suits_order = ['pique', 'coeur', 'trefle', 'carreau', 'atout'] #I have switched the usual order 'trefle' and 'carreau' because card are more easy to see if suit colour change (black, red, black, red)
 values_order = map(str,range(1,11))+["valet","cavalier","dame","roi"]
 
 class Card():
@@ -591,7 +591,7 @@
 
             elif elt.name == 'contrat': #it's time to choose contrat
                 form = data_form.Form.fromElement(elt.firstChildElement())
-                xml_data = XMLTools.dataForm2xml(form)
+                xml_data = dataForm2xml(form)
                 self.host.bridge.tarotGameChooseContrat(room_jid.userhost(), xml_data, profile)
            
             elif elt.name == 'contrat_choosed':
@@ -733,7 +733,7 @@
                 for looser in elt.elements(name='looser', uri=''):
                     loosers.append(unicode(looser))
                 form = data_form.Form.fromElement(form_elt)
-                xml_data = XMLTools.dataForm2xml(form)
+                xml_data = dataForm2xml(form)
                 self.host.bridge.tarotGameScore(room_jid.userhost(), xml_data, winners, loosers, profile)
             elif elt.name == 'error':
                 if elt['type'] == 'invalid_cards':
--- a/plugins/plugin_xep_0077.py	Sat Jun 19 17:15:30 2010 +0800
+++ b/plugins/plugin_xep_0077.py	Tue Jun 22 13:58:53 2010 +0800
@@ -24,7 +24,7 @@
 from twisted.words.protocols.jabber import error as jab_error
 from twisted.words.protocols.jabber.xmlstream import IQ
 from twisted.internet import reactor
-from tools.xml_tools import XMLTools
+from tools.xml_tools import dataForm2xml
 import pdb
 
 from wokkel import data_form
@@ -67,7 +67,7 @@
             return
         
         form = data_form.Form.fromElement(x_elem)
-        xml_data = XMLTools.dataForm2xml(form)
+        xml_data = dataForm2xml(form)
         self.host.bridge.actionResult("FORM", answer['id'], {"target":answer["from"], "type":"registration", "xml":xml_data})
 
     def reg_err(self, failure):
@@ -75,7 +75,7 @@
         info (_("Registration failure: %s") % str(failure.value))
         answer_data = {}
         answer_data['reason'] = 'unknown'
-        answer_data={"message":"%s [code: %s]" % (failure.value.condition, failure.value.code)}
+        answer_data={"message":"%s [code: %s]" % (failure.value.condition, unicode(failure.value))}
         answer_type = "ERROR"
         self.host.bridge.actionResult(answer_type, failure.value.stanza['id'], answer_data)
    
--- a/plugins/plugin_xep_0100.py	Sat Jun 19 17:15:30 2010 +0800
+++ b/plugins/plugin_xep_0100.py	Tue Jun 22 13:58:53 2010 +0800
@@ -88,8 +88,14 @@
         self.__gateways[request_id] = {'__total_items':len(disco._items), '__handled_items':0, '__private__':{'target':target.full()}}
         for item in disco._items:
             debug (_("item found: %s"), item.name)
-            client.disco.requestInfo(item.entity).addCallback(self.discoInfo, entity=item.entity, request_id=request_id)
-            client.disco.requestInfo(item.entity).addErrback(self.discoInfoErr, entity=item.entity, request_id=request_id)
+            client.disco.requestInfo(item.entity).addCallback(self.discoInfo, entity=item.entity, request_id=request_id).addErrback(self.discoInfoErr, entity=item.entity, request_id=request_id)
+
+    def discoItemsErr(self, failure, request_id, target, client):
+        """Something is going wrong with disco"""
+        error(_("Error when discovering [%(target)s]: %(condition)s") % {'target':target.full(), 'condition':unicode(failure.value)})
+        message_data={"reason": "connection error", "message":_(u"Error while trying to discover %(target)s gateways: %(error_mess)s") % {'target':target.full(), 'error_mess':unicode(failure.value)}}
+        self.host.bridge.actionResult("ERROR", request_id, message_data)
+
 
     def registrationSuccessful(self, target):
         """Called when in_band registration is ok, we must now follow the rest of procedure"""
@@ -107,10 +113,11 @@
         """Find gateways in the target JID, using discovery protocol
         Return an id used for retrieving the list of gateways
         """
+        profile = self.host.memory.getProfileName(profile_key)
         client = self.host.getClient(profile_key)
         assert(client)
         to_jid = jid.JID(target)
-        debug (_("find gateways (target = %s)") % to_jid.full())
+        debug (_("find gateways (target = %(target)s, profile = %(profile)s)") % {'target':to_jid.full(), 'profile':profile})
         request_id = self.host.get_next_id()
-        client.disco.requestItems(to_jid).addCallback(self.discoItems, request_id=request_id, target = to_jid, client = client)
+        client.disco.requestItems(to_jid).addCallback(self.discoItems, request_id=request_id, target = to_jid, client = client).addErrback(self.discoItemsErr, request_id=request_id, target = to_jid, client = client)
         return request_id
--- a/sat.tac	Sat Jun 19 17:15:30 2010 +0800
+++ b/sat.tac	Tue Jun 22 13:58:53 2010 +0800
@@ -47,7 +47,7 @@
 import os.path
 
 from tools.memory import Memory
-from tools.xml_tools import XMLTools 
+from tools.xml_tools import tupleList2dataForm
 from glob import glob
 
 import gettext
@@ -527,7 +527,7 @@
         iq["from"] = self.profiles[profile].jid.full()
         query = iq.addElement(('jabber:iq:register', 'query'))
         if action=='SUBMIT':
-            form = XMLTools.tupleList2dataForm(fields)
+            form = tupleList2dataForm(fields)
             query.addChild(form.toElement())
         elif action=='CANCEL':
             query.addElement('remove')
@@ -774,12 +774,12 @@
         if not profile_key:
             error (_('Non-exsitant profile'))
             return ""
-        try:
+        if self.menus.has_key((category,name,type)):
             id = self.get_next_id()
             self.menus[(category,name,type)]['callback'](id, profile)
             return id
-        except KeyError:
-            error (_("Trying to access an unknown menu"))
+        else:
+            error (_("Trying to access an unknown menu (%(category)s/%(name)s/%(type)s)")%{'category':category, 'name':name,'type':type})
             return ""
 
 
--- a/tools/xml_tools.py	Sat Jun 19 17:15:30 2010 +0800
+++ b/tools/xml_tools.py	Tue Jun 22 13:58:53 2010 +0800
@@ -24,65 +24,62 @@
 from wokkel import data_form
 import pdb
 
-class XMLTools:
-    """This class help manage XML used in SàT (parameters, registration, etc) """
+"""This library help manage XML used in SàT (parameters, registration, etc) """
 
     
-    @staticmethod
-    def dataForm2xml(form):
-        """Take a data form (xep-0004, Wokkel's implementation) and convert it to a SàT xml"""
-        
-        impl = minidom.getDOMImplementation()
+def dataForm2xml(form):
+    """Take a data form (xep-0004, Wokkel's implementation) and convert it to a SàT xml"""
+    
+    impl = minidom.getDOMImplementation()
 
-        doc = impl.createDocument(None, "form", None)
-        top_element = doc.documentElement
+    doc = impl.createDocument(None, "form", None)
+    top_element = doc.documentElement
+    
+    #result_xml = ["<form>", "</form>"]
+    if form.instructions:
+        elem = doc.createElement('elem')
+        elem.setAttribute('name','instructions')
+        elem.setAttribute('type','text')
+        text = doc.createTextNode('\n'.join(form.instructions))
+        elem.appendChild(text)
+        top_element.appendChild(elem)
+    for field in form.fieldList:
+        if field.fieldType == 'fixed':
+            __field_type = 'text'
+        elif field.fieldType == 'text-single':
+            __field_type = "string"
+        elif field.fieldType == 'text-private':
+            __field_type = "password"
+        elif field.fieldType == 'list-single':
+            __field_type = "list"
+        else:
+            error (u"FIXME FIXME FIXME: Type [%s] is not managed yet by SàT" % field.fieldType)
+            __field_type = "string"
         
-        #result_xml = ["<form>", "</form>"]
-        if form.instructions:
-            elem = doc.createElement('elem')
-            elem.setAttribute('name','instructions')
-            elem.setAttribute('type','text')
-            text = doc.createTextNode('\n'.join(form.instructions))
-            elem.appendChild(text)
-            top_element.appendChild(elem)
-        for field in form.fieldList:
-            if field.fieldType == 'fixed':
-                __field_type = 'text'
-            elif field.fieldType == 'text-single':
-                __field_type = "string"
-            elif field.fieldType == 'text-private':
-                __field_type = "password"
-            elif field.fieldType == 'list-single':
-                __field_type = "list"
-            else:
-                error (u"FIXME FIXME FIXME: Type [%s] is not managed yet by SàT" % field.fieldType)
-                __field_type = "string"
-            
-            elem = doc.createElement('elem')
-            if field.var:
-                elem.setAttribute('name', field.var)
-            elem.setAttribute('type', __field_type)
-            elem.setAttribute('label', field.label or "")
-            if field.value:
-                elem.setAttribute('value', field.value)
-            top_element.appendChild(elem)
-            for option in field.options:
-                opt = doc.createElement('option')
-                opt.setAttribute('value', option.value)
-                elem.appendChild(opt)
-        result = doc.toxml()
-        doc.unlink()
-        return result
+        elem = doc.createElement('elem')
+        if field.var:
+            elem.setAttribute('name', field.var)
+        elem.setAttribute('type', __field_type)
+        elem.setAttribute('label', field.label or "")
+        if field.value:
+            elem.setAttribute('value', field.value)
+        top_element.appendChild(elem)
+        for option in field.options:
+            opt = doc.createElement('option')
+            opt.setAttribute('value', option.value)
+            elem.appendChild(opt)
+    result = doc.toxml()
+    doc.unlink()
+    return result
 
-    @staticmethod
-    def tupleList2dataForm(values):
-        """convert a list of tuples (name,value) to a wokkel submit data form"""
-        form = data_form.Form('submit')
-        for value in values:
-            field = data_form.Field(var=value[0], value=value[1])
-            form.addField(field)
+def tupleList2dataForm(values):
+    """convert a list of tuples (name,value) to a wokkel submit data form"""
+    form = data_form.Form('submit')
+    for value in values:
+        field = data_form.Field(var=value[0], value=value[1])
+        form.addField(field)
 
-        return form
+    return form