gajim-plural/gajim/common/zeroconf/connection_handlers_zerocon...

183 lines
5.7 KiB
Python
Raw Normal View History

# Contributors for this file:
# - Yann Leboulanger <asterix@lagaule.org>
# - Nikos Kouremenos <nkour@jabber.org>
# - Dimitur Kirov <dkirov@gmail.com>
# - Travis Shirk <travis@pobox.com>
# - Stefan Bethge <stefan@lanpartei.de>
#
# This file is part of Gajim.
#
# Gajim is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published
# by the Free Software Foundation; version 3 only.
#
# Gajim is distributed in the hope that it will be useful,
# 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.
#
# 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
import logging
2018-07-19 21:26:45 +02:00
import nbxmpp
2006-05-26 16:27:42 +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.i18n import _
2018-07-19 21:26:45 +02:00
from gajim.common.nec import NetworkIncomingEvent, NetworkEvent
from gajim.common.modules.user_nickname import parse_nickname
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
log = logging.getLogger('gajim.c.z.connection_handlers_zeroconf')
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:
def add_sha(self, p, *args):
return p
2006-11-23 16:17:24 +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
class ConnectionHandlersZeroconf(ConnectionVcard,
2018-07-08 18:37:53 +02:00
ConnectionSocks5BytestreamZeroconf,
2018-07-05 20:20:22 +02:00
connection_handlers.ConnectionHandlersBase,
connection_handlers.ConnectionJingle):
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)
connection_handlers.ConnectionHandlersBase.__init__(self)
2018-07-19 21:26:45 +02:00
def _messageCB(self, ip, con, stanza):
"""
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)
self.get_module('Chatstate').delegate(event)
2018-07-19 21:26:45 +02:00
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),
'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)))