Changeset 9648
- Timestamp:
- 05/17/08 04:23:46 (3 months ago)
- Location:
- trunk/src/common
- Files:
-
- 3 modified
-
connection_handlers.py (modified) (31 diffs)
-
zeroconf/connection_handlers_zeroconf.py (modified) (15 diffs)
-
zeroconf/connection_zeroconf.py (modified) (4 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/common/connection_handlers.py
r9644 r9648 83 83 return False 84 84 return True 85 85 86 86 def send_success_connect_reply(self, streamhost): 87 87 ''' send reply to the initiator of FT that we … … 98 98 stream_tag.setAttr('jid', streamhost['jid']) 99 99 self.connection.send(iq) 100 100 101 101 def remove_transfers_for_contact(self, contact): 102 102 ''' stop all active transfer for contact ''' … … 113 113 file_props['error'] = -3 114 114 self.remove_transfer(file_props) 115 115 116 116 def remove_all_transfers(self): 117 117 ''' stops and removes all active connections from the socks5 pool ''' … … 120 120 del(self.files_props) 121 121 self.files_props = {} 122 122 123 123 def remove_transfer(self, file_props, remove_from_list = True): 124 124 if file_props is None: … … 131 131 if self.files_props.has_key('sid'): 132 132 del(self.files_props['sid']) 133 133 134 134 def disconnect_transfer(self, file_props): 135 135 if file_props is None: … … 143 143 gajim.socks5queue.remove_receiver(host['idx']) 144 144 gajim.socks5queue.remove_sender(host['idx']) 145 145 146 146 def send_socks5_info(self, file_props, fast = True, receiver = None, 147 147 sender = None): … … 166 166 proxies.remove(default) 167 167 proxies.insert(0, default) 168 168 169 169 for proxy in proxies: 170 170 (host, _port, jid) = gajim.proxy65_manager.get_proxy(proxy, self.name) … … 314 314 field.addOption(common.xmpp.NS_BYTESTREAM) 315 315 self.connection.send(iq) 316 316 317 317 def _result_socks5_sid(self, sid, hash_id): 318 318 ''' store the result of sha message from auth. ''' … … 322 322 file_props['hash'] = hash_id 323 323 return 324 324 325 325 def _connect_error(self, to, _id, sid, code = 404): 326 326 ''' cb, when there is an error establishing BS connection, or … … 361 361 iq.setID(auth_id) 362 362 self.connection.send(iq) 363 363 364 364 # register xmpppy handlers for bytestream and FT stanzas 365 365 def _bytestreamErrorCB(self, con, iq_obj): … … 377 377 self.dispatch('FILE_REQUEST_ERROR', (jid, file_props, '')) 378 378 raise common.xmpp.NodeProcessed 379 379 380 380 def _bytestreamSetCB(self, con, iq_obj): 381 381 gajim.log.debug('_bytestreamSetCB') … … 441 441 gajim.socks5queue.activate_proxy(host['idx']) 442 442 raise common.xmpp.NodeProcessed 443 443 444 444 def _bytestreamResultCB(self, con, iq_obj): 445 445 gajim.log.debug('_bytestreamResultCB') … … 448 448 query = iq_obj.getTag('query') 449 449 gajim.proxy65_manager.resolve_result(frm, query) 450 450 451 451 try: 452 452 streamhost = query.getTag('streamhost-used') … … 513 513 self._connect_error(frm, fasts[0]['id'], file_props['sid'], 514 514 code = 406) 515 515 516 516 raise common.xmpp.NodeProcessed 517 517 518 518 def _siResultCB(self, con, iq_obj): 519 519 gajim.log.debug('_siResultCB') … … 552 552 self.send_socks5_info(file_props, fast = True) 553 553 raise common.xmpp.NodeProcessed 554 554 555 555 def _siSetCB(self, con, iq_obj): 556 556 gajim.log.debug('_siSetCB') … … 617 617 For feature: var is mandatory''' 618 618 self._discover(common.xmpp.NS_DISCO_INFO, jid, node, id_prefix) 619 619 620 620 def request_register_agent_info(self, agent): 621 621 if not self.connection: … … 643 643 # fixed: blocking 644 644 common.xmpp.features_nb.register(self.connection, agent, info, None) 645 646 645 646 647 647 def _discover(self, ns, jid, node = None, id_prefix = None): 648 648 if not self.connection: … … 655 655 iq.setQuerynode(node) 656 656 self.connection.send(iq) 657 657 658 658 def _ReceivedRegInfo(self, con, resp, agent): 659 659 common.xmpp.features_nb._ReceivedRegInfo(con, resp, agent) 660 660 self._IqCB(con, resp) 661 661 662 662 def _discoGetCB(self, con, iq_obj): 663 663 ''' get disco info ''' … … 676 676 feature.setAttr('var', f) 677 677 query.addChild(node=feature) 678 678 679 679 self.connection.send(iq) 680 680 raise common.xmpp.NodeProcessed 681 681 682 682 def _DiscoverItemsErrorCB(self, con, iq_obj): 683 683 gajim.log.debug('DiscoverItemsErrorCB') … … 711 711 items.append(attr) 712 712 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, 714 714 'hostname') 715 715 id = iq_obj.getID() … … 740 740 if self.commandInfoQuery(con, iq_obj): 741 741 raise common.xmpp.NodeProcessed 742 742 743 743 else: 744 744 iq = iq_obj.buildReply('result') … … 852 852 return self.add_caps(p) 853 853 return p 854 854 855 855 def add_caps(self, p): 856 856 ''' advertise our capabilities in presence stanza (xep-0115)''' … … 860 860 c.setAttr('ver', gajim.caps_hash) 861 861 return p 862 862 863 863 def node_to_dict(self, node): 864 864 dict = {} … … 903 903 except IOError, e: 904 904 self.dispatch('ERROR', (_('Disk Write Error'), str(e))) 905 905 906 906 def get_cached_vcard(self, fjid, is_fake_jid = False): 907 907 '''return the vcard as a dict … … 931 931 if not isinstance(vcard['PHOTO'], dict): 932 932 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 {} 933 939 vcard['jid'] = jid 934 940 vcard['resource'] = gajim.get_resource_from_jid(fjid) … … 1196 1202 self.dispatch('VCARD', vcard) 1197 1203 1198 class ConnectionHandlers(ConnectionVcard, ConnectionBytestream, ConnectionDisco, ConnectionCommands, ConnectionPubSub, ConnectionCaps): 1204 # basic connection handlers used here and in zeroconf 1205 class ConnectionHandlersBase: 1199 1206 def __init__(self): 1200 ConnectionVcard.__init__(self)1201 ConnectionBytestream.__init__(self)1202 ConnectionCommands.__init__(self)1203 ConnectionPubSub.__init__(self)1204 self.gmail_url = None1205 1207 # List of IDs we are waiting answers for {id: (type_of_request, data), } 1206 1208 self.awaiting_answers = {} … … 1211 1213 # SUBSCRIBED event to gui 1212 1214 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 1292 haven't received a thread_id yet and returns the session that we last 1293 sent 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 1328 class 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 1213 1337 # keep the latest subscribed event for each jid to prevent loop when we 1214 # ackno ledge presences1338 # acknowledge presences 1215 1339 self.subscribed_events = {} 1216 1340 # IDs of jabber:iq:last requests … … 1221 1345 self.awaiting_xmpp_ping_id = None 1222 1346 1223 # keep track of sessions this connection has with other JIDs1224 self.sessions = {}1225 1347 try: 1226 1348 idle.init() … … 1235 1357 common.xmpp.protocol.ERR_NOT_AUTHORIZED) 1236 1358 self.connection.send(err) 1237 1359 1238 1360 def _HttpAuthCB(self, con, iq_obj): 1239 1361 gajim.log.debug('HttpAuthCB') … … 1247 1369 msg = iq_obj.getTagData('body') # In case it's a message with a body 1248 1370 self.dispatch('HTTP_AUTH', (method, url, id, iq_obj, msg)); 1249 raise common.xmpp.NodeProcessed1250 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.NodeProcessed1268 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 1276 1371 raise common.xmpp.NodeProcessed 1277 1372 … … 1727 1822 self.dispatch('GC_INVITATION',(frm, jid_from, reason, password, 1728 1823 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 = True1734 if not gajim.interface.is_pm_contact(jid, self.name):1735 pm = False1736 jid = gajim.get_jid_without_resource(jid)1737 1738 session = self.find_session(jid, thread_id)1739 1740 if session:1741 return session1742 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 None1756 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 pass1773 1774 def find_null_session(self, jid):1775 '''finds all of the sessions between us and a remote jid in which we1776 haven't received a thread_id yet and returns the session that we last1777 sent a message to.'''1778 1779 sessions = self.sessions[jid].values()1780 1781 # sessions that we haven't received a thread ID in1782 idless = filter(lambda s: not s.received_thread_id, sessions)1783 1784 # filter out everything exceptthe default session type1785 chat_sessions = filter(lambda s: isinstance(s, ChatControlSession), idless)1786 1787 if chat_sessions:1788 # return the session that we last sent a message in1789 chat_sessions.sort(key=lambda s: s.last_send)1790 return chat_sessions[-1]1791 else:1792 return None1793 1794 def make_new_session(self, jid, thread_id=None, type='chat', klass=None):1795 if not klass:1796 klass = ChatControlSession1797 1798 # determine if this session is a pm session1799 # if not, discard the resource1800 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] = sess1809 1810 return sess1811 1824 1812 1825 def _pubsubEventCB(self, con, msg): -
trunk/src/common/zeroconf/connection_handlers_zeroconf.py
r9625 r9648 36 36 from common import gajim 37 37 from common.zeroconf import zeroconf 38 38 39 STATUS_LIST = ['offline', 'connecting', 'online', 'chat', 'away', 'xa', 'dnd', 39 40 'invisible'] … … 49 50 HAS_IDLE = False 50 51 51 from common.stanza_session import EncryptedStanzaSession 52 53 class ConnectionVcard: 54 def __init__(self): 55 self.vcard_sha = None 56 self.vcard_shas = {} # sha of contacts 57 self.room_jids = [] # list of gc jids so that vcard are saved in a folder 58 59 def add_sha(self, p, send_caps = True): 60 pass 61 52 from common import connection_handlers 53 from session import ChatControlSession 54 55 class ConnectionVcard(connection_handlers.ConnectionVcard): 56 def add_sha(self, p, send_caps = True): 57 pass 58 62 59 def add_caps(self, p): 63 60 pass 64 61 65 def node_to_dict(self, node):66 dict = {}67 68 for info in node.getChildren():69 name = info.getName()70 if name in ('ADR', 'TEL', 'EMAIL'): # we can have several71 if not dict.has_key(name):72 dict[name] = []73 entry = {}74 for c in info.getChildren():75 entry[c.getName()] = c.getData()76 dict[name].append(entry)77 elif info.getChildren() == []:78 dict[name] = info.getData()79 else:80 dict[name] = {}81 for c in info.getChildren():82 dict[name][c.getName()] = c.getData()83 84 return dict85 86 def save_vcard_to_hd(self, full_jid, card):87 jid, nick = gajim.get_room_and_nick_from_fjid(full_jid)88 puny_jid = helpers.sanitize_filename(jid)89 path = os.path.join(gajim.VCARD_PATH, puny_jid)90 if jid in self.room_jids or os.path.isdir(path):91 # remove room_jid file if needed92 if os.path.isfile(path):93 os.remove(path)94 # create folder if needed95 if not os.path.isdir(path):96 os.mkdir(path, 0700)97 puny_nick = helpers.sanitize_filename(nick)98 path_to_file = os.path.join(gajim.VCARD_PATH, puny_jid, puny_nick)99 else:100 path_to_file = path101 fil = open(path_to_file, 'w')102 fil.write(str(card))103 fil.close()104 105 def get_cached_vcard(self, fjid, is_fake_jid = False):106 '''return the vcard as a dict107 return {} if vcard was too old108 return None if we don't have cached vcard'''109 jid, nick = gajim.get_room_and_nick_from_fjid(fjid)110 puny_jid = helpers.sanitize_filename(jid)111 if is_fake_jid:112 puny_nick = helpers.sanitize_filename(nick)113 path_to_file = os.path.join(gajim.VCARD_PATH, puny_jid, puny_nick)114 else:115 path_to_file = os.path.join(gajim.VCARD_PATH, puny_jid)116 if not os.path.isfile(path_to_file):117 return None118 # We have the vcard cached119 f = open(path_to_file)120 c = f.read()121 f.close()122 card = common.xmpp.Node(node = c)123 vcard = self.node_to_dict(card)124 if vcard.has_key('PHOTO'):125 if not isinstance(vcard['PHOTO'], dict):126 del vcard['PHOTO']127 elif vcard['PHOTO'].has_key('SHA'):128 cached_sha = vcard['PHOTO']['SHA']129 if self.vcard_shas.has_key(jid) and self.vcard_shas[jid] != \130 cached_sha:131 # user change his vcard so don't use the cached one132 return {}133 vcard['jid'] = jid
