2006-05-26 16:27:42 +02:00
|
|
|
##
|
|
|
|
## Copyright (C) 2006 Gajim Team
|
|
|
|
##
|
|
|
|
## Contributors for this file:
|
2007-10-22 13:33:50 +02:00
|
|
|
## - Yann Leboulanger <asterix@lagaule.org>
|
2006-05-26 16:27:42 +02:00
|
|
|
## - Nikos Kouremenos <nkour@jabber.org>
|
|
|
|
## - Dimitur Kirov <dkirov@gmail.com>
|
|
|
|
## - Travis Shirk <travis@pobox.com>
|
2008-12-03 22:56:12 +01:00
|
|
|
## - Stefan Bethge <stefan@lanpartei.de>
|
2006-05-26 16:27:42 +02:00
|
|
|
##
|
2007-10-22 13:13:13 +02:00
|
|
|
## This file is part of Gajim.
|
|
|
|
##
|
|
|
|
## Gajim is free software; you can redistribute it and/or modify
|
2006-05-26 16:27:42 +02:00
|
|
|
## it under the terms of the GNU General Public License as published
|
2007-10-22 13:13:13 +02:00
|
|
|
## by the Free Software Foundation; version 3 only.
|
2006-05-26 16:27:42 +02:00
|
|
|
##
|
2007-10-22 13:13:13 +02:00
|
|
|
## Gajim is distributed in the hope that it will be useful,
|
2006-05-26 16:27:42 +02:00
|
|
|
## but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
## GNU General Public License for more details.
|
|
|
|
##
|
2007-10-22 13:13:13 +02:00
|
|
|
## You should have received a copy of the GNU General Public License
|
|
|
|
## along with Gajim. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
##
|
2006-05-26 16:27:42 +02:00
|
|
|
|
|
|
|
import time
|
|
|
|
import socket
|
|
|
|
|
|
|
|
from calendar import timegm
|
|
|
|
|
|
|
|
import common.xmpp
|
|
|
|
|
|
|
|
from common import helpers
|
|
|
|
from common import gajim
|
2006-09-18 00:57:41 +02:00
|
|
|
from common.zeroconf import zeroconf
|
2009-08-30 00:57:49 +02:00
|
|
|
from common.commands import ConnectionCommands
|
2009-11-19 20:36:40 +01:00
|
|
|
from common.pep import ConnectionPEP
|
2009-12-10 21:52:32 +01:00
|
|
|
from common.protocol.bytestream import ConnectionBytestreamZeroconf
|
2009-08-30 00:57:49 +02:00
|
|
|
|
|
|
|
import logging
|
|
|
|
log = logging.getLogger('gajim.c.z.connection_handlers_zeroconf')
|
2008-05-17 04:23:46 +02:00
|
|
|
|
2006-05-26 16:27:42 +02:00
|
|
|
STATUS_LIST = ['offline', 'connecting', 'online', 'chat', 'away', 'xa', 'dnd',
|
|
|
|
'invisible']
|
|
|
|
# kind of events we can wait for an answer
|
|
|
|
VCARD_PUBLISHED = 'vcard_published'
|
|
|
|
VCARD_ARRIVED = 'vcard_arrived'
|
|
|
|
AGENT_REMOVED = 'agent_removed'
|
|
|
|
HAS_IDLE = True
|
|
|
|
try:
|
2006-11-08 14:23:38 +01:00
|
|
|
import idle
|
2008-10-11 11:37:13 +02:00
|
|
|
except Exception:
|
2009-08-30 00:57:49 +02:00
|
|
|
log.debug(_('Unable to load idle module'))
|
2006-11-08 14:23:38 +01:00
|
|
|
HAS_IDLE = False
|
2006-05-26 16:27:42 +02:00
|
|
|
|
2008-05-17 04:23:46 +02:00
|
|
|
from common import connection_handlers
|
|
|
|
from session import ChatControlSession
|
2007-10-09 05:46:51 +02:00
|
|
|
|
2008-05-17 04:23:46 +02:00
|
|
|
class ConnectionVcard(connection_handlers.ConnectionVcard):
|
|
|
|
def add_sha(self, p, send_caps = True):
|
2009-11-18 11:06:09 +01:00
|
|
|
return p
|
2006-11-23 16:17:24 +01:00
|
|
|
|
|
|
|
def add_caps(self, p):
|
2009-11-18 11:06:09 +01:00
|
|
|
return p
|
2006-11-23 16:17:24 +01:00
|
|
|
|
|
|
|
def request_vcard(self, jid = None, is_fake_jid = False):
|
|
|
|
pass
|
2008-05-17 04:23:46 +02:00
|
|
|
|
2006-11-23 16:17:24 +01:00
|
|
|
def send_vcard(self, vcard):
|
|
|
|
pass
|
2006-09-21 01:24:07 +02:00
|
|
|
|
|
|
|
|
2009-12-10 21:52:32 +01:00
|
|
|
class ConnectionHandlersZeroconf(ConnectionVcard, ConnectionBytestreamZeroconf,
|
2009-11-19 20:36:40 +01:00
|
|
|
ConnectionCommands, ConnectionPEP, connection_handlers.ConnectionHandlersBase):
|
2006-05-26 16:27:42 +02:00
|
|
|
def __init__(self):
|
2006-11-29 23:57:37 +01:00
|
|
|
ConnectionVcard.__init__(self)
|
2009-12-10 21:52:32 +01:00
|
|
|
ConnectionBytestreamZeroconf.__init__(self)
|
2009-08-30 00:57:49 +02:00
|
|
|
ConnectionCommands.__init__(self)
|
2008-05-17 04:23:46 +02:00
|
|
|
connection_handlers.ConnectionHandlersBase.__init__(self)
|
|
|
|
|
2006-05-26 16:27:42 +02:00
|
|
|
try:
|
|
|
|
idle.init()
|
2008-10-11 11:37:13 +02:00
|
|
|
except Exception:
|
2008-12-02 16:53:23 +01:00
|
|
|
global HAS_IDLE
|
2006-05-26 16:27:42 +02:00
|
|
|
HAS_IDLE = False
|
2008-05-17 04:23:46 +02:00
|
|
|
|
2006-09-18 00:57:41 +02:00
|
|
|
def _messageCB(self, ip, con, msg):
|
2009-11-26 13:27:47 +01:00
|
|
|
"""
|
|
|
|
Called when we receive a message
|
|
|
|
"""
|
2009-08-30 00:57:49 +02:00
|
|
|
log.debug('Zeroconf MessageCB')
|
2008-05-17 04:23:46 +02:00
|
|
|
|
|
|
|
frm = msg.getFrom()
|
2006-09-18 00:57:41 +02:00
|
|
|
mtype = msg.getType()
|
2007-10-09 05:46:51 +02:00
|
|
|
thread_id = msg.getThread()
|
2008-05-17 04:23:46 +02:00
|
|
|
|
|
|
|
if not mtype:
|
|
|
|
mtype = 'normal'
|
2007-10-09 05:46:51 +02:00
|
|
|
|
2008-10-11 11:44:12 +02:00
|
|
|
if frm is None:
|
2006-10-03 00:41:48 +02:00
|
|
|
for key in self.connection.zeroconf.contacts:
|
|
|
|
if ip == self.connection.zeroconf.contacts[key][zeroconf.C_ADDRESS]:
|
2006-09-18 00:57:41 +02:00
|
|
|
frm = key
|
2008-05-17 04:23:46 +02:00
|
|
|
|
2006-10-17 21:13:36 +02:00
|
|
|
frm = unicode(frm)
|
2007-10-09 05:46:51 +02:00
|
|
|
|
2008-05-17 04:23:46 +02:00
|
|
|
session = self.get_or_create_session(frm, thread_id)
|
2007-10-09 05:46:51 +02:00
|
|
|
|
|
|
|
if thread_id and not session.received_thread_id:
|
|
|
|
session.received_thread_id = True
|
2008-05-17 04:23:46 +02:00
|
|
|
|
2007-10-09 05:46:51 +02:00
|
|
|
if msg.getTag('feature') and msg.getTag('feature').namespace == \
|
|
|
|
common.xmpp.NS_FEATURE:
|
|
|
|
if gajim.HAVE_PYCRYPTO:
|
|
|
|
self._FeatureNegCB(con, msg, session)
|
|
|
|
return
|
2008-05-17 04:23:46 +02:00
|
|
|
|
2007-10-09 05:46:51 +02:00
|
|
|
if msg.getTag('init') and msg.getTag('init').namespace == \
|
|
|
|
common.xmpp.NS_ESESSION_INIT:
|
|
|
|
self._InitE2ECB(con, msg, session)
|
|
|
|
|
2006-09-18 00:57:41 +02:00
|
|
|
encrypted = False
|
2008-05-17 04:23:46 +02:00
|
|
|
tim = msg.getTimestamp()
|
|
|
|
tim = helpers.datetime_tuple(tim)
|
|
|
|
tim = time.localtime(timegm(tim))
|
2007-10-09 05:46:51 +02:00
|
|
|
|
2008-05-17 04:23:46 +02:00
|
|
|
if msg.getTag('c', namespace = common.xmpp.NS_STANZA_CRYPTO):
|
2007-10-09 05:46:51 +02:00
|
|
|
encrypted = True
|
|
|
|
|
|
|
|
try:
|
|
|
|
msg = session.decrypt_stanza(msg)
|
2008-10-11 11:37:13 +02:00
|
|
|
except Exception:
|
2007-10-09 05:46:51 +02:00
|
|
|
self.dispatch('FAILED_DECRYPT', (frm, tim))
|
2008-05-17 04:23:46 +02:00
|
|
|
|
2007-10-09 05:46:51 +02:00
|
|
|
msgtxt = msg.getBody()
|
|
|
|
subject = msg.getSubject() # if not there, it's None
|
|
|
|
|
2006-09-18 00:57:41 +02:00
|
|
|
# invitations
|
|
|
|
invite = None
|
2008-05-17 04:23:46 +02:00
|
|
|
encTag = msg.getTag('x', namespace = common.xmpp.NS_ENCRYPTED)
|
|
|
|
|
2006-09-18 00:57:41 +02:00
|
|
|
if not encTag:
|
|
|
|
invite = msg.getTag('x', namespace = common.xmpp.NS_MUC_USER)
|
|
|
|
if invite and not invite.getTag('invite'):
|
|
|
|
invite = None
|
|
|
|
|
2007-12-28 19:49:28 +01:00
|
|
|
if encTag and self.USE_GPG:
|
2006-09-18 00:57:41 +02:00
|
|
|
#decrypt
|
|
|
|
encmsg = encTag.getData()
|
2008-05-17 04:23:46 +02:00
|
|
|
|
2006-09-18 00:57:41 +02:00
|
|
|
keyID = gajim.config.get_per('accounts', self.name, 'keyid')
|
|
|
|
if keyID:
|
|
|
|
decmsg = self.gpg.decrypt(encmsg, keyID)
|
2007-12-31 02:19:08 +01:00
|
|
|
# \x00 chars are not allowed in C (so in GTK)
|
2008-05-17 04:23:46 +02:00
|
|
|
msgtxt = decmsg.replace('\x00', '')
|
|
|
|
encrypted = True
|
2007-10-09 05:46:51 +02:00
|
|
|
|
2008-05-17 04:23:46 +02:00
|
|
|
if mtype == 'error':
|
|
|
|
self.dispatch_error_msg(msg, msgtxt, session, frm, tim, subject)
|
2007-10-09 05:46:51 +02:00
|
|
|
else:
|
2008-05-17 04:23:46 +02:00
|
|
|
# XXX this shouldn't be hardcoded
|
|
|
|
if isinstance(session, ChatControlSession):
|
2008-06-13 17:59:04 +02:00
|
|
|
session.received(frm, msgtxt, tim, encrypted, msg)
|
2007-10-09 05:46:51 +02:00
|
|
|
else:
|
2008-05-17 04:23:46 +02:00
|
|
|
session.received(msg)
|
|
|
|
# END messageCB
|
2007-10-09 05:46:51 +02:00
|
|
|
|
2006-09-17 16:45:58 +02:00
|
|
|
def store_metacontacts(self, tags):
|
2009-11-26 13:27:47 +01:00
|
|
|
"""
|
|
|
|
Fake empty method
|
|
|
|
"""
|
2008-05-17 04:23:46 +02:00
|
|
|
# serverside metacontacts are not supported with zeroconf
|
2006-09-17 16:45:58 +02:00
|
|
|
# (there is no server)
|
|
|
|
pass
|
2006-11-20 22:02:15 +01:00
|
|
|
|
2009-08-30 00:57:49 +02:00
|
|
|
def _DiscoverItemsGetCB(self, con, iq_obj):
|
|
|
|
log.debug('DiscoverItemsGetCB')
|
|
|
|
|
|
|
|
if not self.connection or self.connected < 2:
|
|
|
|
return
|
|
|
|
|
|
|
|
if self.commandItemsQuery(con, iq_obj):
|
|
|
|
raise common.xmpp.NodeProcessed
|
|
|
|
node = iq_obj.getTagAttr('query', 'node')
|
|
|
|
if node is None:
|
|
|
|
result = iq_obj.buildReply('result')
|
|
|
|
self.connection.send(result)
|
|
|
|
raise common.xmpp.NodeProcessed
|
|
|
|
if node==common.xmpp.NS_COMMANDS:
|
|
|
|
self.commandListQuery(con, iq_obj)
|
|
|
|
raise common.xmpp.NodeProcessed
|
|
|
|
|
2008-12-02 15:44:26 +01:00
|
|
|
# vim: se ts=3:
|