comparison sat/core/xmpp.py @ 3173:343b8076e967

core (xmpp): new message key to handle attachments
author Goffi <goffi@goffi.org>
date Tue, 18 Feb 2020 18:17:14 +0100
parents dcebc585c29f
children b9a2dd4d750a
comparison
equal deleted inserted replaced
3172:dcebc585c29f 3173:343b8076e967
18 18
19 import sys 19 import sys
20 import time 20 import time
21 import calendar 21 import calendar
22 import uuid 22 import uuid
23 import mimetypes
23 from functools import partial 24 from functools import partial
24 import shortuuid 25 import shortuuid
25 from sat.core.i18n import _ 26 from sat.core.i18n import _
26 from sat.core.constants import Const as C 27 from sat.core.constants import Const as C
27 from sat.memory import cache 28 from sat.memory import cache
654 655
655 def _cancelErrorTrap(self, failure): 656 def _cancelErrorTrap(self, failure):
656 """A message sending can be cancelled by a plugin treatment""" 657 """A message sending can be cancelled by a plugin treatment"""
657 failure.trap(exceptions.CancelError) 658 failure.trap(exceptions.CancelError)
658 659
660 def isMessagePrintable(self, mess_data):
661 """Return True if a message contain payload to show in frontends"""
662 return (
663 mess_data["message"] or mess_data["subject"]
664 or mess_data["extra"].get(C.MESS_KEY_ATTACHMENTS)
665 or mess_data["type"] == C.MESS_TYPE_INFO
666 )
667
659 def messageAddToHistory(self, data): 668 def messageAddToHistory(self, data):
660 """Store message into database (for local history) 669 """Store message into database (for local history)
661 670
662 @param data: message data dictionnary 671 @param data: message data dictionnary
663 @param client: profile's client 672 @param client: profile's client
664 """ 673 """
665 if data["type"] != C.MESS_TYPE_GROUPCHAT: 674 if data["type"] != C.MESS_TYPE_GROUPCHAT:
666 # we don't add groupchat message to history, as we get them back 675 # we don't add groupchat message to history, as we get them back
667 # and they will be added then 676 # and they will be added then
668 if data["message"] or data["subject"]: # we need a message to store 677
678 # we need a message to store
679 if self.isMessagePrintable(data):
669 self.host_app.memory.addToHistory(self, data) 680 self.host_app.memory.addToHistory(self, data)
670 else: 681 else:
671 log.warning( 682 log.warning(
672 "No message found" 683 "No message found"
673 ) # empty body should be managed by plugins before this point 684 ) # empty body should be managed by plugins before this point
687 @param client: profile's client 698 @param client: profile's client
688 """ 699 """
689 if data["type"] != C.MESS_TYPE_GROUPCHAT: 700 if data["type"] != C.MESS_TYPE_GROUPCHAT:
690 # we don't send groupchat message to bridge, as we get them back 701 # we don't send groupchat message to bridge, as we get them back
691 # and they will be added the 702 # and they will be added the
692 if (data["message"] or data["subject"]): # we need a message to send 703
693 # something 704 # we need a message to send something
705 if self.isMessagePrintable(data):
694 706
695 # We send back the message, so all frontends are aware of it 707 # We send back the message, so all frontends are aware of it
696 self.host_app.bridge.messageNew( 708 self.host_app.bridge.messageNew(
697 *self.messageGetBridgeArgs(data), 709 *self.messageGetBridgeArgs(data),
698 profile=self.profile 710 profile=self.profile
993 def __init__(self, host): 1005 def __init__(self, host):
994 xmppim.MessageProtocol.__init__(self) 1006 xmppim.MessageProtocol.__init__(self)
995 self.host = host 1007 self.host = host
996 1008
997 def parseMessage(self, message_elt): 1009 def parseMessage(self, message_elt):
998 """parse a message XML and return message_data 1010 """Parse a message XML and return message_data
999 1011
1000 @param message_elt(domish.Element): raw <message> xml 1012 @param message_elt(domish.Element): raw <message> xml
1001 @param client(SatXMPPClient, None): client to map message id to uid 1013 @param client(SatXMPPClient, None): client to map message id to uid
1002 if None, mapping will not be done 1014 if None, mapping will not be done
1003 @return(dict): message data 1015 @return(dict): message data
1121 def addToHistory(self, data): 1133 def addToHistory(self, data):
1122 if data.pop("history", None) == C.HISTORY_SKIP: 1134 if data.pop("history", None) == C.HISTORY_SKIP:
1123 log.debug("history is skipped as requested") 1135 log.debug("history is skipped as requested")
1124 data["extra"]["history"] = C.HISTORY_SKIP 1136 data["extra"]["history"] = C.HISTORY_SKIP
1125 else: 1137 else:
1126 if data["message"] or data["subject"]: # we need a message to store 1138 # we need a message to store
1139 if self.parent.isMessagePrintable(data):
1127 return self.host.memory.addToHistory(self.parent, data) 1140 return self.host.memory.addToHistory(self.parent, data)
1128 else: 1141 else:
1129 log.debug("not storing empty message to history: {data}" 1142 log.debug("not storing empty message to history: {data}"
1130 .format(data=data)) 1143 .format(data=data))
1131 1144
1136 except KeyError: 1149 except KeyError:
1137 pass 1150 pass
1138 if C.MESS_KEY_ENCRYPTION in data: 1151 if C.MESS_KEY_ENCRYPTION in data:
1139 data["extra"]["encrypted"] = C.BOOL_TRUE 1152 data["extra"]["encrypted"] = C.BOOL_TRUE
1140 if data is not None: 1153 if data is not None:
1141 if data["message"] or data["subject"] or data["type"] == C.MESS_TYPE_INFO: 1154 if self.parent.isMessagePrintable(data):
1142 self.host.bridge.messageNew( 1155 self.host.bridge.messageNew(
1143 data["uid"], 1156 data["uid"],
1144 data["timestamp"], 1157 data["timestamp"],
1145 data["from"].full(), 1158 data["from"].full(),
1146 data["to"].full(), 1159 data["to"].full(),