core (XMPP): message received (onMessage) refactoring:

- better separation of actions
    - use of failure.trap and generic exception (CancelError/SkipHistory)
This commit is contained in:
Goffi 2014-06-07 16:38:53 +02:00
commit d113c2a295

View file

@ -17,7 +17,7 @@
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from sat.core.i18n import _, D_
from sat.core.i18n import _
from sat.core.constants import Const as C
from twisted.internet import task, defer
from twisted.words.protocols.jabber import jid, xmlstream
@ -130,7 +130,12 @@ class SatMessageProtocol(xmppim.MessageProtocol):
data['type'] = message['type'] if message.hasAttribute('type') else 'normal'
def after_treatments(data):
def bridgeSignal(data):
if data is not None:
self.host.bridge.newMessage(data['from'], data['body'], data['type'], data['to'], data['extra'], profile=self.parent.profile)
return data
def addToHistory(data):
# set message body to empty string by default, and later
# also forward message without body (chat state notification...)
try:
@ -142,9 +147,20 @@ class SatMessageProtocol(xmppim.MessageProtocol):
self.host.memory.addToHistory(jid.JID(data['from']), jid.JID(data['to']), data['body'], data['type'], data['extra'], timestamp, profile=self.parent.profile)
except IndexError:
self.host.memory.addToHistory(jid.JID(data['from']), jid.JID(data['to']), data['body'], data['type'], data['extra'], profile=self.parent.profile)
self.host.bridge.newMessage(data['from'], data['body'], data['type'], data['to'], data['extra'], profile=self.parent.profile)
return data
post_treat.addCallback(after_treatments)
def treatmentsEb(failure):
failure.trap(exceptions.SkipHistory)
return data
def cancelErrorTrap(failure):
"""A message sending can be cancelled by a plugin treatment"""
failure.trap(exceptions.CancelError)
post_treat.addCallback(addToHistory)
post_treat.addErrback(treatmentsEb)
post_treat.addCallback(bridgeSignal)
post_treat.addErrback(cancelErrorTrap)
post_treat.callback(data)