Commit a4945d26 authored by Philipp Hörist's avatar Philipp Hörist

Refactor message handlers

- Remove 'decrypt' extension point, its not needed anymore
- Remove decryption finished methods
- Remove MessageReceived Events, DecryptedMessageReceived servs the same purpose now

Plugins decrypt messages before we execute this handler
parent 44630205
......@@ -197,6 +197,37 @@ class MAM(BaseModule):
parse_oob(properties, additional_data)
msgtxt = properties.body
if properties.is_encrypted:
additional_data['encrypted'] = properties.encrypted.additional_data
else:
if properties.eme is not None:
msgtxt = get_eme_message(properties.eme)
if not msgtxt:
# For example Chatstates, Receipts, Chatmarkers
self._log.debug(stanza.getProperties())
return
with_ = properties.jid.getStripped()
if properties.is_muc_pm:
# we store the message with the full JID
with_ = str(with_)
if properties.is_self_message:
# Self messages can only be deduped with origin-id
if message_id is None:
self._log.warning('Self message without origin-id found')
return
stanza_id = message_id
if properties.mam.namespace == nbxmpp.NS_MAM_1:
if app.logger.search_for_duplicate(
self._account, with_, properties.mam.timestamp, msgtxt):
self._log.info('Found duplicate with fallback for mam:1')
return
event_attrs.update(
{'conn': self._con,
'account': self._account,
......@@ -217,23 +248,19 @@ class MAM(BaseModule):
'namespace': properties.mam.namespace,
})
if groupchat:
event = MamGcMessageReceivedEvent(None, **event_attrs)
else:
event = MamMessageReceivedEvent(None, **event_attrs)
if properties.is_encrypted:
event.additional_data['encrypted'] = properties.encrypted.additional_data
self._decryption_finished(event)
else:
app.plugin_manager.extension_point(
'decrypt', self._con, event, self._decryption_finished)
if not event.encrypted:
if properties.eme is not None:
event.msgtxt = get_eme_message(properties.eme)
self._decryption_finished(event)
app.logger.insert_into_logs(self._account,
with_,
properties.mam.timestamp,
event_attrs['kind'],
unread=False,
message=msgtxt,
contact_name=event_attrs['nick'],
additional_data=additional_data,
stanza_id=stanza_id,
message_id=properties.id)
raise nbxmpp.NodeProcessed
app.nec.push_incoming_event(
NetworkEvent('mam-decrypted-message-received', **event_attrs))
@staticmethod
def _parse_gc_attrs(properties):
......@@ -255,45 +282,6 @@ class MAM(BaseModule):
'nick': None,
'kind': kind}
def _decryption_finished(self, event):
if not event.msgtxt:
# For example Chatstates, Receipts, Chatmarkers
self._log.debug(event.message.getProperties())
return
with_ = event.with_.getStripped()
if event.muc_pm:
# we store the message with the full JID
with_ = str(event.with_)
stanza_id = event.stanza_id
if event.self_message:
# Self messages can only be deduped with origin-id
if event.origin_id is None:
self._log.warning('Self message without origin-id found')
return
stanza_id = event.origin_id
if event.namespace == nbxmpp.NS_MAM_1:
if app.logger.search_for_duplicate(
self._account, with_, event.timestamp, event.msgtxt):
self._log.info('Found duplicate with fallback for mam:1')
return
app.logger.insert_into_logs(self._account,
with_,
event.timestamp,
event.kind,
unread=False,
message=event.msgtxt,
contact_name=event.nick,
additional_data=event.additional_data,
stanza_id=stanza_id,
message_id=event.message_id)
app.nec.push_incoming_event(
MamDecryptedMessageReceived(None, **vars(event)))
def _is_valid_request(self, properties):
valid_id = self._mam_query_ids.get(str(properties.mam.archive), None)
return valid_id == properties.mam.query_id
......@@ -634,18 +622,6 @@ class MAM(BaseModule):
MAMPreferenceSaved(None, conn=self._con))
class MamMessageReceivedEvent(NetworkIncomingEvent):
name = 'mam-message-received'
class MamGcMessageReceivedEvent(NetworkIncomingEvent):
name = 'mam-gc-message-received'
class MamDecryptedMessageReceived(NetworkIncomingEvent):
name = 'mam-decrypted-message-received'
class MAMPreferenceError(NetworkIncomingEvent):
name = 'mam-prefs-error'
......
......@@ -22,7 +22,6 @@ from nbxmpp.const import MessageType
from gajim.common import app
from gajim.common.i18n import _
from gajim.common.nec import NetworkIncomingEvent
from gajim.common.nec import NetworkEvent
from gajim.common.helpers import AdditionalDataDict
from gajim.common.const import KindConstant
......@@ -146,6 +145,41 @@ class Message(BaseModule):
'gajim', 'user_timestamp', properties.user_timestamp)
parse_oob(properties, additional_data)
xhtml = parse_xhtml(properties)
app.nec.push_incoming_event(NetworkEvent('update-client-info',
account=self._account,
jid=jid,
resource=resource))
if properties.is_encrypted:
additional_data['encrypted'] = properties.encrypted.additional_data
else:
if properties.eme is not None:
msgtxt = get_eme_message(properties.eme)
if type_.is_error:
if not msgtxt:
msgtxt = _('message')
if gc_control:
gc_control.add_info_message(msgtxt)
else:
error_msg = stanza.getErrorMsg() or msgtxt
msgtxt = None if error_msg == msgtxt else msgtxt
self._log_error_message(error_msg,
jid,
properties.timestamp)
app.nec.push_incoming_event(
MessageErrorEvent(None,
conn=self._con,
fjid=fjid,
error_code=stanza.getErrorCode(),
error_msg=error_msg,
msg=msgtxt,
time_=properties.timestamp,
session=session,
stanza=stanza))
return
event_attr = {
'conn': self._con,
......@@ -173,90 +207,40 @@ class Message(BaseModule):
'muc_pm': properties.is_muc_pm,
'gc_control': gc_control,
'attention': properties.attention,
'xhtml': parse_xhtml(properties),
'xhtml': xhtml,
'user_nick': properties.nickname,
'subject': properties.subject,
}
app.nec.push_incoming_event(NetworkEvent('update-client-info',
account=self._account,
jid=jid,
resource=resource))
event = MessageReceivedEvent(None, **event_attr)
app.nec.push_incoming_event(event)
if properties.is_encrypted:
event.additional_data['encrypted'] = properties.encrypted.additional_data
self._on_message_decrypted(event)
else:
app.plugin_manager.extension_point(
'decrypt', self._con, event, self._on_message_decrypted)
if not event.encrypted:
if properties.eme is not None:
event.msgtxt = get_eme_message(properties.eme)
self._on_message_decrypted(event)
def _on_message_decrypted(self, event):
groupchat = event.mtype == 'groupchat'
event_attr = {
'popup': False,
'msg_log_id': None,
'displaymarking': parse_securitylabel(event.stanza),
'displaymarking': parse_securitylabel(stanza),
}
for name, value in event_attr.items():
setattr(event, name, value)
if event.mtype == 'error':
if not event.msgtxt:
event.msgtxt = _('message')
if event.gc_control:
event.gc_control.add_info_message(event.msgtxt)
else:
self._log_error_message(event)
error_msg = event.stanza.getErrorMsg() or event.msgtxt
msgtxt = None if error_msg == event.msgtxt else event.msgtxt
app.nec.push_incoming_event(
MessageErrorEvent(None,
conn=self._con,
fjid=event.fjid,
error_code=event.stanza.getErrorCode(),
error_msg=error_msg,
msg=msgtxt,
time_=event.timestamp,
session=event.session,
stanza=event.stanza))
return
if groupchat:
if not event.msgtxt:
if type_.is_groupchat:
if not msgtxt:
return
event.room_jid = event.jid
event.nickname = event.resource
event.xhtml_msgtxt = event.xhtml
event.nick = event.resource or ''
app.nec.push_incoming_event(NetworkEvent('gc-message-received',
**vars(event)))
event_attr.update({
'room_jid': jid,
'nickname': resource,
'xhtml_msgtxt': xhtml,
'nick': resource or '',
})
event = NetworkEvent('gc-message-received', **event_attr)
app.nec.push_incoming_event(event)
# TODO: Some plugins modify msgtxt in the GUI event
self._log_muc_message(event)
return
app.nec.push_incoming_event(
DecryptedMessageReceivedEvent(
None, **vars(event)))
NetworkEvent('decrypted-message-received', **event_attr))
def _log_error_message(self, event):
error_msg = event.stanza.getErrorMsg() or event.msgtxt
if app.config.should_log(self._account, event.jid):
def _log_error_message(self, error_msg, jid, timestamp):
if app.config.should_log(self._account, jid):
app.logger.insert_into_logs(self._account,
event.jid,
event.timestamp,
jid,
timestamp,
KindConstant.ERROR,
message=error_msg,
subject=event.subject)
message=error_msg)
def _log_muc_message(self, event):
if event.mtype == 'error':
......@@ -311,13 +295,5 @@ class Message(BaseModule):
return None, None
class MessageReceivedEvent(NetworkIncomingEvent):
name = 'message-received'
class DecryptedMessageReceivedEvent(NetworkIncomingEvent):
name = 'decrypted-message-received'
def get_instance(*args, **kwargs):
return Message(*args, **kwargs), 'Message'
......@@ -27,7 +27,7 @@ from gajim.common import app
from gajim.common import connection_handlers
from gajim.common.i18n import _
from gajim.common.helpers import AdditionalDataDict
from gajim.common.nec import NetworkIncomingEvent, NetworkEvent
from gajim.common.nec import NetworkEvent
from gajim.common.const import KindConstant
from gajim.common.modules.util import get_eme_message
from gajim.common.modules.misc import parse_correction
......@@ -42,15 +42,6 @@ STATUS_LIST = ['offline', 'connecting', 'online', 'chat', 'away', 'xa', 'dnd',
'invisible']
class ZeroconfMessageReceivedEvent(NetworkIncomingEvent):
name = 'message-received'
class DecryptedMessageReceivedEvent(NetworkIncomingEvent):
name = 'decrypted-message-received'
class ConnectionHandlersZeroconf(connection_handlers.ConnectionHandlersBase):
def __init__(self):
connection_handlers.ConnectionHandlersBase.__init__(self)
......@@ -88,11 +79,37 @@ class ConnectionHandlersZeroconf(connection_handlers.ConnectionHandlersBase):
if thread_id and not session.received_thread_id:
session.received_thread_id = True
session.last_receive = time.time()
timestamp = time.time()
session.last_receive = timestamp
additional_data = AdditionalDataDict()
parse_oob(properties, additional_data)
if properties.is_encrypted:
additional_data['encrypted'] = properties.encrypted.additional_data
else:
if properties.eme is not None:
msgtxt = get_eme_message(properties.eme)
if type_ == 'error':
if not msgtxt:
msgtxt = _('message')
self._log_error_message(stanza, msgtxt, jid, timestamp)
error_msg = stanza.getErrorMsg() or msgtxt
msgtxt = None if error_msg == msgtxt else msgtxt
app.nec.push_incoming_event(
MessageErrorEvent(None,
conn=self,
fjid=fjid,
error_code=stanza.getErrorCode(),
error_msg=error_msg,
msg=msgtxt,
time_=timestamp,
session=session,
stanza=stanza))
return
event_attr = {
'conn': self,
'stanza': stanza,
......@@ -120,57 +137,20 @@ class ConnectionHandlersZeroconf(connection_handlers.ConnectionHandlersBase):
'xhtml': parse_xhtml(properties),
'user_nick': properties.nickname,
'subject': 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:
if properties.eme is not None:
event.msgtxt = get_eme_message(properties.eme)
self._on_message_decrypted(event)
def _on_message_decrypted(self, event):
event_attr = {
'popup': False,
'msg_log_id': None,
'displaymarking': None,
'stanza_id': event.unique_id
'stanza_id': id_,
}
for name, value in event_attr.items():
setattr(event, name, value)
if event.mtype == 'error':
if not event.msgtxt:
event.msgtxt = _('message')
self._log_error_message(event)
error_msg = event.stanza.getErrorMsg() or event.msgtxt
msgtxt = None if error_msg == event.msgtxt else event.msgtxt
app.nec.push_incoming_event(
MessageErrorEvent(None,
conn=self,
fjid=event.fjid,
error_code=event.stanza.getErrorCode(),
error_msg=error_msg,
msg=msgtxt,
time_=event.timestamp,
session=event.session,
stanza=event.stanza))
return
app.nec.push_incoming_event(
DecryptedMessageReceivedEvent(None, **vars(event)))
NetworkEvent('decrypted-message-received', **event_attr))
def _log_error_message(self, event):
error_msg = event.stanza.getErrorMsg() or event.msgtxt
if app.config.should_log(self.name, event.jid):
def _log_error_message(self, stanza, msgtxt, jid, timestamp):
error_msg = stanza.getErrorMsg() or msgtxt
if app.config.should_log(self.name, jid):
app.logger.insert_into_logs(self.name,
event.jid,
event.timestamp,
jid,
timestamp,
KindConstant.ERROR,
message=error_msg,
subject=event.subject)
message=error_msg)
......@@ -10,8 +10,6 @@ from gajim.common import app
from gajim.common import nec
from gajim.common import ged
from gajim.common.nec import NetworkEvent
from gajim.common.modules.message import MessageReceivedEvent
from gajim.common.modules.message import DecryptedMessageReceivedEvent
from gajim.session import ChatControlSession
from gajim.roster_window import RosterWindow
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment