Show
Ignore:
Timestamp:
05/17/08 04:23:46 (4 months ago)
Author:
bct
Message:

fixed link-local messaging (broken by session-centric) and removed a ton of duplicated/unused code

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • trunk/src/common/connection_handlers.py

    r9644 r9648  
    8383                        return False 
    8484                return True 
    85          
     85 
    8686        def send_success_connect_reply(self, streamhost): 
    8787                ''' send reply to the initiator of FT that we 
     
    9898                stream_tag.setAttr('jid', streamhost['jid']) 
    9999                self.connection.send(iq) 
    100          
     100 
    101101        def remove_transfers_for_contact(self, contact): 
    102102                ''' stop all active transfer for contact ''' 
     
    113113                                file_props['error'] = -3 
    114114                                self.remove_transfer(file_props) 
    115          
     115 
    116116        def remove_all_transfers(self): 
    117117                ''' stops and removes all active connections from the socks5 pool ''' 
     
    120120                del(self.files_props) 
    121121                self.files_props = {} 
    122          
     122 
    123123        def remove_transfer(self, file_props, remove_from_list = True): 
    124124                if file_props is None: 
     
    131131                        if self.files_props.has_key('sid'): 
    132132                                del(self.files_props['sid']) 
    133          
     133 
    134134        def disconnect_transfer(self, file_props): 
    135135                if file_props is None: 
     
    143143                                        gajim.socks5queue.remove_receiver(host['idx']) 
    144144                                        gajim.socks5queue.remove_sender(host['idx']) 
    145          
     145 
    146146        def send_socks5_info(self, file_props, fast = True, receiver = None, 
    147147                sender = None): 
     
    166166                                        proxies.remove(default) 
    167167                                proxies.insert(0, default) 
    168                          
     168 
    169169                        for proxy in proxies: 
    170170                                (host, _port, jid) = gajim.proxy65_manager.get_proxy(proxy, self.name) 
     
    314314                field.addOption(common.xmpp.NS_BYTESTREAM) 
    315315                self.connection.send(iq) 
    316          
     316 
    317317        def _result_socks5_sid(self, sid, hash_id): 
    318318                ''' store the result of sha message from auth. ''' 
     
    322322                file_props['hash'] = hash_id 
    323323                return 
    324          
     324 
    325325        def _connect_error(self, to, _id, sid, code = 404): 
    326326                ''' cb, when there is an error establishing BS connection, or  
     
    361361                iq.setID(auth_id) 
    362362                self.connection.send(iq) 
    363          
     363 
    364364        # register xmpppy handlers for bytestream and FT stanzas 
    365365        def _bytestreamErrorCB(self, con, iq_obj): 
     
    377377                self.dispatch('FILE_REQUEST_ERROR', (jid, file_props, '')) 
    378378                raise common.xmpp.NodeProcessed 
    379          
     379 
    380380        def _bytestreamSetCB(self, con, iq_obj): 
    381381                gajim.log.debug('_bytestreamSetCB') 
     
    441441                                                gajim.socks5queue.activate_proxy(host['idx']) 
    442442                                                raise common.xmpp.NodeProcessed 
    443          
     443 
    444444        def _bytestreamResultCB(self, con, iq_obj): 
    445445                gajim.log.debug('_bytestreamResultCB') 
     
    448448                query = iq_obj.getTag('query') 
    449449                gajim.proxy65_manager.resolve_result(frm, query) 
    450                  
     450 
    451451                try: 
    452452                        streamhost = query.getTag('streamhost-used') 
     
    513513                                        self._connect_error(frm, fasts[0]['id'], file_props['sid'], 
    514514                                                code = 406) 
    515                  
     515 
    516516                raise common.xmpp.NodeProcessed 
    517          
     517 
    518518        def _siResultCB(self, con, iq_obj): 
    519519                gajim.log.debug('_siResultCB') 
     
    552552                self.send_socks5_info(file_props, fast = True) 
    553553                raise common.xmpp.NodeProcessed 
    554          
     554 
    555555        def _siSetCB(self, con, iq_obj): 
    556556                gajim.log.debug('_siSetCB') 
     
    617617                        For feature: var is mandatory''' 
    618618                self._discover(common.xmpp.NS_DISCO_INFO, jid, node, id_prefix) 
    619          
     619 
    620620        def request_register_agent_info(self, agent): 
    621621                if not self.connection: 
     
    643643                        # fixed: blocking 
    644644                        common.xmpp.features_nb.register(self.connection, agent, info, None) 
    645          
    646          
     645 
     646 
    647647        def _discover(self, ns, jid, node = None, id_prefix = None): 
    648648                if not self.connection: 
     
    655655                        iq.setQuerynode(node) 
    656656                self.connection.send(iq) 
    657          
     657 
    658658        def _ReceivedRegInfo(self, con, resp, agent): 
    659659                common.xmpp.features_nb._ReceivedRegInfo(con, resp, agent) 
    660660                self._IqCB(con, resp) 
    661          
     661 
    662662        def _discoGetCB(self, con, iq_obj): 
    663663                ''' get disco info ''' 
     
    676676                        feature.setAttr('var', f) 
    677677                        query.addChild(node=feature) 
    678                  
     678 
    679679                self.connection.send(iq) 
    680680                raise common.xmpp.NodeProcessed 
    681          
     681 
    682682        def _DiscoverItemsErrorCB(self, con, iq_obj): 
    683683                gajim.log.debug('DiscoverItemsErrorCB') 
     
    711711                        items.append(attr) 
    712712                jid = helpers.get_full_jid_from_iq(iq_obj) 
    713                 hostname = gajim.config.get_per('accounts', self.name,  
     713                hostname = gajim.config.get_per('accounts', self.name, 
    714714                                                                                                        'hostname') 
    715715                id = iq_obj.getID() 
     
    740740                if self.commandInfoQuery(con, iq_obj): 
    741741                        raise common.xmpp.NodeProcessed 
    742                  
     742 
    743743                else: 
    744744                        iq = iq_obj.buildReply('result') 
     
    852852                        return self.add_caps(p) 
    853853                return p 
    854          
     854 
    855855        def add_caps(self, p): 
    856856                ''' advertise our capabilities in presence stanza (xep-0115)''' 
     
    860860                c.setAttr('ver', gajim.caps_hash) 
    861861                return p 
    862          
     862 
    863863        def node_to_dict(self, node): 
    864864                dict = {} 
     
    903903                except IOError, e: 
    904904                        self.dispatch('ERROR', (_('Disk Write Error'), str(e))) 
    905          
     905 
    906906        def get_cached_vcard(self, fjid, is_fake_jid = False): 
    907907                '''return the vcard as a dict 
     
    931931                        if not isinstance(vcard['PHOTO'], dict): 
    932932                                del vcard['PHOTO'] 
     933                        elif vcard['PHOTO'].has_key('SHA'): 
     934                                cached_sha = vcard['PHOTO']['SHA'] 
     935                                if self.vcard_shas.has_key(jid) and self.vcard_shas[jid] != \ 
     936                                        cached_sha: 
     937                                        # user change his vcard so don't use the cached one 
     938                                        return {} 
    933939                vcard['jid'] = jid 
    934940                vcard['resource'] = gajim.get_resource_from_jid(fjid) 
     
    11961202                        self.dispatch('VCARD', vcard) 
    11971203 
    1198 class ConnectionHandlers(ConnectionVcard, ConnectionBytestream, ConnectionDisco, ConnectionCommands, ConnectionPubSub, ConnectionCaps): 
     1204# basic connection handlers used here and in zeroconf 
     1205class ConnectionHandlersBase: 
    11991206        def __init__(self): 
    1200                 ConnectionVcard.__init__(self) 
    1201                 ConnectionBytestream.__init__(self) 
    1202                 ConnectionCommands.__init__(self) 
    1203                 ConnectionPubSub.__init__(self) 
    1204                 self.gmail_url = None 
    12051207                # List of IDs we are waiting answers for {id: (type_of_request, data), } 
    12061208                self.awaiting_answers = {} 
     
    12111213                # SUBSCRIBED event to gui 
    12121214                self.automatically_added = [] 
     1215 
     1216                # keep track of sessions this connection has with other JIDs 
     1217                self.sessions = {} 
     1218 
     1219        def _FeatureNegCB(self, con, stanza, session): 
     1220                gajim.log.debug('FeatureNegCB') 
     1221                feature = stanza.getTag(name='feature', namespace=common.xmpp.NS_FEATURE) 
     1222                form = common.xmpp.DataForm(node=feature.getTag('x')) 
     1223 
     1224                if form['FORM_TYPE'] == 'urn:xmpp:ssn': 
     1225                        self.dispatch('SESSION_NEG', (stanza.getFrom(), session, form)) 
     1226                else: 
     1227                        reply = stanza.buildReply() 
     1228                        reply.setType('error') 
     1229 
     1230                        reply.addChild(feature) 
     1231                        reply.addChild(node=common.xmpp.ErrorNode('service-unavailable', typ='cancel')) 
     1232 
     1233                        con.send(reply) 
     1234 
     1235                raise common.xmpp.NodeProcessed 
     1236 
     1237        def _InitE2ECB(self, con, stanza, session): 
     1238                gajim.log.debug('InitE2ECB') 
     1239                init = stanza.getTag(name='init', namespace=common.xmpp.NS_ESESSION_INIT) 
     1240                form = common.xmpp.DataForm(node=init.getTag('x')) 
     1241 
     1242                self.dispatch('SESSION_NEG', (stanza.getFrom(), session, form)) 
     1243 
     1244                raise common.xmpp.NodeProcessed 
     1245 
     1246        def get_or_create_session(self, jid, thread_id): 
     1247                '''returns an existing session between this connection and 'jid', returns a new one if none exist.''' 
     1248 
     1249                pm = True 
     1250                if not gajim.interface.is_pm_contact(jid, self.name): 
     1251                        pm = False 
     1252                        jid = gajim.get_jid_without_resource(jid) 
     1253 
     1254                session = self.find_session(jid, thread_id) 
     1255 
     1256                if session: 
     1257                        return session 
     1258 
     1259                if pm: 
     1260                        return self.make_new_session(jid, thread_id, type = 'pm') 
     1261                else: 
     1262                        return self.make_new_session(jid, thread_id) 
     1263 
     1264        def find_session(self, jid, thread_id): 
     1265                try: 
     1266                        if not thread_id: 
     1267                                return self.find_null_session(jid) 
     1268                        else: 
     1269                                return self.sessions[jid][thread_id] 
     1270                except KeyError: 
     1271                        return None 
     1272 
     1273        def terminate_sessions(self): 
     1274                '''send termination messages and delete all active sessions''' 
     1275                for jid in self.sessions: 
     1276                        for thread_id in self.sessions[jid]: 
     1277                                self.sessions[jid][thread_id].terminate() 
     1278 
     1279                self.sessions = {} 
     1280 
     1281        def delete_session(self, jid, thread_id): 
     1282                try: 
     1283                        del self.sessions[jid][thread_id] 
     1284 
     1285                        if not self.sessions[jid]: 
     1286                                del self.sessions[jid] 
     1287                except KeyError: 
     1288                        pass 
     1289 
     1290        def find_null_session(self, jid): 
     1291                '''finds all of the sessions between us and a remote jid in which we 
     1292haven't received a thread_id yet and returns the session that we last 
     1293sent a message to.''' 
     1294 
     1295                sessions = self.sessions[jid].values() 
     1296 
     1297                # sessions that we haven't received a thread ID in 
     1298                idless = filter(lambda s: not s.received_thread_id, sessions) 
     1299 
     1300                # filter out everything exceptthe default session type 
     1301                chat_sessions = filter(lambda s: isinstance(s, ChatControlSession), idless) 
     1302 
     1303                if chat_sessions: 
     1304                        # return the session that we last sent a message in 
     1305                        chat_sessions.sort(key=lambda s: s.last_send) 
     1306                        return chat_sessions[-1] 
     1307                else: 
     1308                        return None 
     1309 
     1310        def make_new_session(self, jid, thread_id=None, type='chat', klass=None): 
     1311                if not klass: 
     1312                        klass = ChatControlSession 
     1313 
     1314                # determine if this session is a pm session 
     1315                # if not, discard the resource 
     1316                if not type == 'pm': 
     1317                        jid = gajim.get_jid_without_resource(jid) 
     1318 
     1319                sess = klass(self, common.xmpp.JID(jid), thread_id, type) 
     1320 
     1321                if not jid in self.sessions: 
     1322                        self.sessions[jid] = {} 
     1323 
     1324                self.sessions[jid][sess.thread_id] = sess 
     1325 
     1326                return sess 
     1327 
     1328class ConnectionHandlers(ConnectionVcard, ConnectionBytestream, ConnectionDisco, ConnectionCommands, ConnectionPubSub, ConnectionCaps, ConnectionHandlersBase): 
     1329        def __init__(self): 
     1330                ConnectionVcard.__init__(self) 
     1331                ConnectionBytestream.__init__(self) 
     1332                ConnectionCommands.__init__(self) 
     1333                ConnectionPubSub.__init__(self) 
     1334                ConnectionHandlersBase.__init__(self) 
     1335                self.gmail_url = None 
     1336 
    12131337                # keep the latest subscribed event for each jid to prevent loop when we 
    1214                 # acknoledge presences 
     1338                # acknowledge presences 
    12151339                self.subscribed_events = {} 
    12161340                # IDs of jabber:iq:last requests 
     
    12211345                self.awaiting_xmpp_ping_id = None 
    12221346 
    1223                 # keep track of sessions this connection has with other JIDs 
    1224                 self.sessions = {} 
    12251347                try: 
    12261348                        idle.init() 
     
    12351357                                common.xmpp.protocol.ERR_NOT_AUTHORIZED) 
    12361358                        self.connection.send(err) 
    1237          
     1359 
    12381360        def _HttpAuthCB(self, con, iq_obj): 
    12391361                gajim.log.debug('HttpAuthCB') 
     
    12471369                        msg = iq_obj.getTagData('body') # In case it's a message with a body 
    12481370                        self.dispatch('HTTP_AUTH', (method, url, id, iq_obj, msg)); 
    1249                 raise common.xmpp.NodeProcessed 
    1250  
    1251         def _FeatureNegCB(self, con, stanza, session): 
    1252                 gajim.log.debug('FeatureNegCB') 
    1253                 feature = stanza.getTag(name='feature', namespace=common.xmpp.NS_FEATURE) 
    1254                 form = common.xmpp.DataForm(node=feature.getTag('x')) 
    1255  
    1256                 if form['FORM_TYPE'] == 'urn:xmpp:ssn': 
    1257                         self.dispatch('SESSION_NEG', (stanza.getFrom(), session, form)) 
    1258                 else: 
    1259                         reply = stanza.buildReply() 
    1260                         reply.setType('error') 
    1261  
    1262                         reply.addChild(feature) 
    1263                         reply.addChild(node=common.xmpp.ErrorNode('service-unavailable', typ='cancel')) 
    1264  
    1265                         con.send(reply) 
    1266  
    1267                 raise common.xmpp.NodeProcessed 
    1268  
    1269         def _InitE2ECB(self, con, stanza, session): 
    1270                 gajim.log.debug('InitE2ECB') 
    1271                 init = stanza.getTag(name='init', namespace=common.xmpp.NS_ESESSION_INIT) 
    1272                 form = common.xmpp.DataForm(node=init.getTag('x')) 
    1273  
    1274                 self.dispatch('SESSION_NEG', (stanza.getFrom(), session, form)) 
    1275  
    12761371                raise common.xmpp.NodeProcessed 
    12771372 
     
    17271822                self.dispatch('GC_INVITATION',(frm, jid_from, reason, password, 
    17281823                        is_continued)) 
    1729  
    1730         def get_or_create_session(self, jid, thread_id): 
    1731                 '''returns an existing session between this connection and 'jid', returns a new one if none exist.''' 
    1732  
    1733                 pm = True 
    1734                 if not gajim.interface.is_pm_contact(jid, self.name): 
    1735                         pm = False 
    1736                         jid = gajim.get_jid_without_resource(jid) 
    1737  
    1738                 session = self.find_session(jid, thread_id) 
    1739  
    1740                 if session: 
    1741                         return session 
    1742  
    1743                 if pm: 
    1744                         return self.make_new_session(jid, thread_id, type = 'pm') 
    1745                 else: 
    1746                         return self.make_new_session(jid, thread_id) 
    1747  
    1748         def find_session(self, jid, thread_id): 
    1749                 try: 
    1750                         if not thread_id: 
    1751                                 return self.find_null_session(jid) 
    1752                         else: 
    1753                                 return self.sessions[jid][thread_id] 
    1754                 except KeyError: 
    1755                         return None 
    1756  
    1757         def terminate_sessions(self): 
    1758                 '''send termination messages and delete all active sessions''' 
    1759                 for jid in self.sessions: 
    1760                         for thread_id in self.sessions[jid]: 
    1761                                 self.sessions[jid][thread_id].terminate() 
    1762  
    1763                 self.sessions = {} 
    1764  
    1765         def delete_session(self, jid, thread_id): 
    1766                 try: 
    1767                         del self.sessions[jid][thread_id] 
    1768  
    1769                         if not self.sessions[jid]: 
    1770                                 del self.sessions[jid] 
    1771                 except KeyError: 
    1772                         pass 
    1773  
    1774         def find_null_session(self, jid): 
    1775                 '''finds all of the sessions between us and a remote jid in which we 
    1776 haven't received a thread_id yet and returns the session that we last 
    1777 sent a message to.''' 
    1778  
    1779                 sessions = self.sessions[jid].values() 
    1780  
    1781                 # sessions that we haven't received a thread ID in 
    1782                 idless = filter(lambda s: not s.received_thread_id, sessions) 
    1783  
    1784                 # filter out everything exceptthe default session type 
    1785                 chat_sessions = filter(lambda s: isinstance(s, ChatControlSession), idless) 
    1786  
    1787                 if chat_sessions: 
    1788                         # return the session that we last sent a message in 
    1789                         chat_sessions.sort(key=lambda s: s.last_send) 
    1790                         return chat_sessions[-1] 
    1791                 else: 
    1792                         return None 
    1793  
    1794         def make_new_session(self, jid, thread_id=None, type='chat', klass=None): 
    1795                 if not klass: 
    1796                         klass = ChatControlSession 
    1797  
    1798                 # determine if this session is a pm session 
    1799                 # if not, discard the resource 
    1800                 if not type == 'pm': 
    1801                         jid = gajim.get_jid_without_resource(jid) 
    1802  
    1803                 sess = klass(self, common.xmpp.JID(jid), thread_id, type) 
    1804  
    1805                 if not jid in self.sessions: 
    1806                         self.sessions[jid] = {} 
    1807  
    1808                 self.sessions[jid][sess.thread_id] = sess 
    1809  
    1810                 return sess 
    18111824 
    18121825        def _pubsubEventCB(self, con, msg):