Package common :: Package zeroconf :: Module connection_handlers_zeroconf
[hide private]
[frames] | no frames]

Source Code for Module common.zeroconf.connection_handlers_zeroconf

  1  ## 
  2  ## Copyright (C) 2006 Gajim Team 
  3  ## 
  4  ## Contributors for this file: 
  5  ##      - Yann Leboulanger <asterix@lagaule.org> 
  6  ##      - Nikos Kouremenos <nkour@jabber.org> 
  7  ##      - Dimitur Kirov <dkirov@gmail.com> 
  8  ##      - Travis Shirk <travis@pobox.com> 
  9  ## - Stefan Bethge <stefan@lanpartei.de> 
 10  ## 
 11  ## This file is part of Gajim. 
 12  ## 
 13  ## Gajim is free software; you can redistribute it and/or modify 
 14  ## it under the terms of the GNU General Public License as published 
 15  ## by the Free Software Foundation; version 3 only. 
 16  ## 
 17  ## Gajim is distributed in the hope that it will be useful, 
 18  ## but WITHOUT ANY WARRANTY; without even the implied warranty of 
 19  ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 20  ## GNU General Public License for more details. 
 21  ## 
 22  ## You should have received a copy of the GNU General Public License 
 23  ## along with Gajim.  If not, see <http://www.gnu.org/licenses/>. 
 24  ## 
 25   
 26  import time 
 27  import socket 
 28   
 29  from calendar import timegm 
 30   
 31  import common.xmpp 
 32   
 33  from common import helpers 
 34  from common import gajim 
 35  from common.zeroconf import zeroconf 
 36  from common.commands import ConnectionCommands 
 37  from common.pep import ConnectionPEP 
 38  from common.protocol.bytestream import ConnectionSocks5BytestreamZeroconf 
 39   
 40  import logging 
 41  log = logging.getLogger('gajim.c.z.connection_handlers_zeroconf') 
 42   
 43  STATUS_LIST = ['offline', 'connecting', 'online', 'chat', 'away', 'xa', 'dnd', 
 44          'invisible'] 
 45  # kind of events we can wait for an answer 
 46  VCARD_PUBLISHED = 'vcard_published' 
 47  VCARD_ARRIVED = 'vcard_arrived' 
 48  AGENT_REMOVED = 'agent_removed' 
 49  HAS_IDLE = True 
 50  try: 
 51      import idle 
 52  except Exception: 
 53      log.debug(_('Unable to load idle module')) 
 54      HAS_IDLE = False 
 55   
 56  from common import connection_handlers 
 57  from session import ChatControlSession 
 58   
59 -class ConnectionVcard(connection_handlers.ConnectionVcard):
60 - def add_sha(self, p, send_caps = True):
61 return p
62
63 - def add_caps(self, p):
64 return p
65
66 - def request_vcard(self, jid = None, is_fake_jid = False):
67 pass
68
69 - def send_vcard(self, vcard):
70 pass
71 72
73 -class ConnectionHandlersZeroconf(ConnectionVcard, 74 ConnectionSocks5BytestreamZeroconf, ConnectionCommands, ConnectionPEP, 75 connection_handlers.ConnectionHandlersBase, connection_handlers.ConnectionJingle):
76 - def __init__(self):
88
89 - def _messageCB(self, ip, con, msg):
90 """ 91 Called when we receive a message 92 """ 93 log.debug('Zeroconf MessageCB') 94 95 frm = msg.getFrom() 96 mtype = msg.getType() 97 thread_id = msg.getThread() 98 99 if not mtype: 100 mtype = 'normal' 101 102 if frm is None: 103 for key in self.connection.zeroconf.contacts: 104 if ip == self.connection.zeroconf.contacts[key][zeroconf.C_ADDRESS]: 105 frm = key 106 107 frm = unicode(frm) 108 109 session = self.get_or_create_session(frm, thread_id) 110 111 if thread_id and not session.received_thread_id: 112 session.received_thread_id = True 113 114 if msg.getTag('feature') and msg.getTag('feature').namespace == \ 115 common.xmpp.NS_FEATURE: 116 if gajim.HAVE_PYCRYPTO: 117 self._FeatureNegCB(con, msg, session) 118 return 119 120 if msg.getTag('init') and msg.getTag('init').namespace == \ 121 common.xmpp.NS_ESESSION_INIT: 122 self._InitE2ECB(con, msg, session) 123 124 encrypted = False 125 tim = msg.getTimestamp() 126 tim = helpers.datetime_tuple(tim) 127 tim = time.localtime(timegm(tim)) 128 129 if msg.getTag('c', namespace = common.xmpp.NS_STANZA_CRYPTO): 130 encrypted = True 131 132 try: 133 msg = session.decrypt_stanza(msg) 134 except Exception: 135 self.dispatch('FAILED_DECRYPT', (frm, tim)) 136 137 msgtxt = msg.getBody() 138 subject = msg.getSubject() # if not there, it's None 139 140 # invitations 141 invite = None 142 encTag = msg.getTag('x', namespace = common.xmpp.NS_ENCRYPTED) 143 144 if not encTag: 145 invite = msg.getTag('x', namespace = common.xmpp.NS_MUC_USER) 146 if invite and not invite.getTag('invite'): 147 invite = None 148 149 if encTag and self.USE_GPG: 150 #decrypt 151 encmsg = encTag.getData() 152 153 keyID = gajim.config.get_per('accounts', self.name, 'keyid') 154 if keyID: 155 decmsg = self.gpg.decrypt(encmsg, keyID) 156 # \x00 chars are not allowed in C (so in GTK) 157 msgtxt = decmsg.replace('\x00', '') 158 encrypted = True 159 160 if mtype == 'error': 161 self.dispatch_error_msg(msg, msgtxt, session, frm, tim, subject) 162 else: 163 # XXX this shouldn't be hardcoded 164 if isinstance(session, ChatControlSession): 165 session.received(frm, msgtxt, tim, encrypted, msg) 166 else: 167 session.received(msg)
168 # END messageCB 169
170 - def store_metacontacts(self, tags):
171 """ 172 Fake empty method 173 """ 174 # serverside metacontacts are not supported with zeroconf 175 # (there is no server) 176 pass
177
178 - def _DiscoverItemsGetCB(self, con, iq_obj):
179 log.debug('DiscoverItemsGetCB') 180 181 if not self.connection or self.connected < 2: 182 return 183 184 if self.commandItemsQuery(con, iq_obj): 185 raise common.xmpp.NodeProcessed 186 node = iq_obj.getTagAttr('query', 'node') 187 if node is None: 188 result = iq_obj.buildReply('result') 189 self.connection.send(result) 190 raise common.xmpp.NodeProcessed 191 if node==common.xmpp.NS_COMMANDS: 192 self.commandListQuery(con, iq_obj) 193 raise common.xmpp.NodeProcessed
194