2006-05-26 16:27:42 +02:00
|
|
|
##
|
|
|
|
## Copyright (C) 2006 Gajim Team
|
|
|
|
##
|
|
|
|
## Contributors for this file:
|
2010-02-08 15:08:40 +01:00
|
|
|
## - Yann Leboulanger <asterix@lagaule.org>
|
|
|
|
## - 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
|
|
|
|
2018-07-19 21:26:45 +02:00
|
|
|
import time
|
|
|
|
|
2012-12-09 21:37:51 +01:00
|
|
|
import nbxmpp
|
2006-05-26 16:27:42 +02:00
|
|
|
|
2017-08-13 13:18:56 +02:00
|
|
|
from gajim.common import app
|
2018-07-19 21:26:45 +02:00
|
|
|
|
2017-06-13 23:58:06 +02:00
|
|
|
from gajim.common.protocol.bytestream import ConnectionSocks5BytestreamZeroconf
|
2018-07-19 21:26:45 +02:00
|
|
|
from gajim.common.zeroconf.zeroconf import Constant
|
|
|
|
from gajim.common import connection_handlers
|
|
|
|
from gajim.common.nec import NetworkIncomingEvent, NetworkEvent
|
|
|
|
from gajim.common.modules.user_nickname import parse_nickname
|
|
|
|
from gajim.common.modules.chatstates import parse_chatstate
|
|
|
|
from gajim.common.modules.misc import parse_eme
|
|
|
|
from gajim.common.modules.misc import parse_correction
|
|
|
|
from gajim.common.modules.misc import parse_attention
|
|
|
|
from gajim.common.modules.misc import parse_oob
|
|
|
|
from gajim.common.modules.misc import parse_xhtml
|
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',
|
2018-07-19 21:26:45 +02:00
|
|
|
'invisible']
|
2006-05-26 16:27:42 +02:00
|
|
|
# kind of events we can wait for an answer
|
|
|
|
AGENT_REMOVED = 'agent_removed'
|
|
|
|
|
2018-07-19 21:26:45 +02:00
|
|
|
|
|
|
|
class ZeroconfMessageReceivedEvent(NetworkIncomingEvent):
|
|
|
|
name = 'message-received'
|
|
|
|
|
|
|
|
|
|
|
|
class DecryptedMessageReceivedEvent(NetworkIncomingEvent):
|
|
|
|
name = 'decrypted-message-received'
|
|
|
|
|
2007-10-09 05:46:51 +02:00
|
|
|
|
2018-06-30 19:23:10 +02:00
|
|
|
class ConnectionVcard:
|
2017-09-16 11:49:31 +02:00
|
|
|
def add_sha(self, p, *args):
|
2010-02-08 15:08:40 +01:00
|
|
|
return p
|
2006-11-23 16:17:24 +01:00
|
|
|
|
2010-02-08 15:08:40 +01:00
|
|
|
def add_caps(self, p):
|
|
|
|
return p
|
2006-11-23 16:17:24 +01:00
|
|
|
|
2006-09-21 01:24:07 +02:00
|
|
|
|
2010-02-22 16:29:01 +01:00
|
|
|
class ConnectionHandlersZeroconf(ConnectionVcard,
|
2018-07-08 18:37:53 +02:00
|
|
|
ConnectionSocks5BytestreamZeroconf,
|
2018-07-05 20:20:22 +02:00
|
|
|
connection_handlers.ConnectionHandlersBase,
|
2013-04-06 10:14:28 +02:00
|
|
|
connection_handlers.ConnectionJingle):
|
2010-02-08 15:08:40 +01:00
|
|
|
def __init__(self):
|
|
|
|
ConnectionVcard.__init__(self)
|
2010-02-23 19:10:51 +01:00
|
|
|
ConnectionSocks5BytestreamZeroconf.__init__(self)
|
2010-03-14 22:12:10 +01:00
|
|
|
connection_handlers.ConnectionJingle.__init__(self)
|
2010-02-08 15:08:40 +01:00
|
|
|
connection_handlers.ConnectionHandlersBase.__init__(self)
|
|
|
|
|
2018-07-19 21:26:45 +02:00
|
|
|
def _messageCB(self, ip, con, stanza):
|
2010-02-08 15:08:40 +01:00
|
|
|
"""
|
|
|
|
Called when we receive a message
|
|
|
|
"""
|
|
|
|
log.debug('Zeroconf MessageCB')
|
2018-07-19 21:26:45 +02:00
|
|
|
|
|
|
|
app.nec.push_incoming_event(NetworkEvent(
|
|
|
|
'raw-message-received',
|
|
|
|
conn=self,
|
|
|
|
stanza=stanza,
|
|
|
|
account=self.name))
|
|
|
|
|
|
|
|
type_ = stanza.getType()
|
|
|
|
if type_ is None:
|
|
|
|
type_ = 'normal'
|
|
|
|
|
|
|
|
id_ = stanza.getID()
|
|
|
|
|
|
|
|
fjid = str(stanza.getFrom())
|
|
|
|
|
|
|
|
if fjid is None:
|
|
|
|
for key in self.connection.zeroconf.contacts:
|
|
|
|
if ip == self.connection.zeroconf.contacts[key][
|
|
|
|
Constant.ADDRESS]:
|
|
|
|
fjid = key
|
|
|
|
break
|
|
|
|
|
|
|
|
jid, resource = app.get_room_and_nick_from_fjid(fjid)
|
|
|
|
|
|
|
|
thread_id = stanza.getThread()
|
|
|
|
msgtxt = stanza.getBody()
|
|
|
|
|
|
|
|
session = self.get_or_create_session(fjid, thread_id)
|
|
|
|
|
|
|
|
if thread_id and not session.received_thread_id:
|
|
|
|
session.received_thread_id = True
|
|
|
|
|
|
|
|
session.last_receive = time.time()
|
|
|
|
|
|
|
|
event_attr = {
|
|
|
|
'conn': self,
|
|
|
|
'stanza': stanza,
|
|
|
|
'account': self.name,
|
|
|
|
'id_': id_,
|
|
|
|
'encrypted': False,
|
|
|
|
'additional_data': {},
|
|
|
|
'forwarded': False,
|
|
|
|
'sent': False,
|
|
|
|
'timestamp': time.time(),
|
|
|
|
'fjid': fjid,
|
|
|
|
'jid': jid,
|
|
|
|
'resource': resource,
|
|
|
|
'unique_id': id_,
|
|
|
|
'mtype': type_,
|
|
|
|
'msgtxt': msgtxt,
|
|
|
|
'thread_id': thread_id,
|
|
|
|
'session': session,
|
|
|
|
'self_message': False,
|
|
|
|
'muc_pm': False,
|
|
|
|
'gc_control': None}
|
|
|
|
|
|
|
|
event = ZeroconfMessageReceivedEvent(None, **event_attr)
|
|
|
|
app.nec.push_incoming_event(event)
|
|
|
|
|
|
|
|
app.plugin_manager.extension_point(
|
|
|
|
'decrypt', self, event, self._on_message_decrypted)
|
|
|
|
if not event.encrypted:
|
|
|
|
eme = parse_eme(event.stanza)
|
|
|
|
if eme is not None:
|
|
|
|
event.msgtxt = eme
|
|
|
|
self._on_message_decrypted(event)
|
|
|
|
|
|
|
|
def _on_message_decrypted(self, event):
|
|
|
|
try:
|
|
|
|
self.get_module('Receipts').delegate(event)
|
|
|
|
except nbxmpp.NodeProcessed:
|
|
|
|
return
|
|
|
|
|
|
|
|
event_attr = {
|
|
|
|
'popup': False,
|
|
|
|
'msg_log_id': None,
|
|
|
|
'subject': None,
|
|
|
|
'displaymarking': None,
|
|
|
|
'form_node': None,
|
|
|
|
'attention': parse_attention(event.stanza),
|
|
|
|
'correct_id': parse_correction(event.stanza),
|
|
|
|
'user_nick': parse_nickname(event.stanza),
|
|
|
|
'xhtml': parse_xhtml(event.stanza),
|
|
|
|
'chatstate': parse_chatstate(event.stanza),
|
|
|
|
'stanza_id': event.unique_id
|
|
|
|
}
|
|
|
|
|
|
|
|
parse_oob(event.stanza, event.additional_data)
|
|
|
|
|
|
|
|
for name, value in event_attr.items():
|
|
|
|
setattr(event, name, value)
|
|
|
|
|
|
|
|
if event.mtype == 'error':
|
|
|
|
if not event.msgtxt:
|
|
|
|
event.msgtxt = _('message')
|
|
|
|
self.dispatch_error_message(
|
|
|
|
event.stanza, event.msgtxt,
|
|
|
|
event.session, event.fjid, event.timestamp)
|
|
|
|
return
|
|
|
|
|
|
|
|
app.nec.push_incoming_event(
|
|
|
|
DecryptedMessageReceivedEvent(None, **vars(event)))
|