comparison src/core/sat_main.py @ 922:c897c8d321b3

core: sendMessageTrigger now manage pre and post treatments, which happen before or after XML generation
author Goffi <goffi@goffi.org>
date Sat, 22 Mar 2014 15:34:05 +0100
parents ed9841e6d84a
children e77948faaef3
comparison
equal deleted inserted replaced
921:8dd168c7741c 922:c897c8d321b3
487 "message": msg, 487 "message": msg,
488 "subject": subject, 488 "subject": subject,
489 "type": mess_type, 489 "type": mess_type,
490 "extra": extra, 490 "extra": extra,
491 } 491 }
492 treatments = defer.Deferred() # XXX: plugin can add their treatments to this deferred 492 pre_xml_treatments = defer.Deferred() # XXX: plugin can add their pre XML treatments to this deferred
493 post_xml_treatments = defer.Deferred() # XXX: plugin can add their post XML treatments to this deferred
493 494
494 if mess_data["type"] == "auto": 495 if mess_data["type"] == "auto":
495 # we try to guess the type 496 # we try to guess the type
496 if mess_data["subject"]: 497 if mess_data["subject"]:
497 mess_data["type"] = 'normal' 498 mess_data["type"] = 'normal'
510 else: 511 else:
511 mess_data["type"] == 'chat' 512 mess_data["type"] == 'chat'
512 mess_data["type"] == "chat" if mess_data["subject"] else "normal" 513 mess_data["type"] == "chat" if mess_data["subject"] else "normal"
513 514
514 if not no_trigger: 515 if not no_trigger:
515 if not self.trigger.point("sendMessage", mess_data, treatments, profile): 516 if not self.trigger.point("sendMessage", mess_data, pre_xml_treatments, post_xml_treatments, profile):
516 return defer.succeed(None) 517 return defer.succeed(None)
517 518
518 debug(_("Sending jabber message of type [%(type)s] to %(to)s...") % {"type": mess_data["type"], "to": to_jid.full()}) 519 debug(_("Sending jabber message of type [%(type)s] to %(to)s...") % {"type": mess_data["type"], "to": to_jid.full()})
519 mess_data['xml'] = domish.Element((None, 'message')) 520
520 mess_data['xml']["to"] = mess_data["to"].full() 521 def generateXML(mess_data):
521 mess_data['xml']["from"] = current_jid.full() 522 mess_data['xml'] = domish.Element((None, 'message'))
522 mess_data['xml']["type"] = mess_data["type"] 523 mess_data['xml']["to"] = mess_data["to"].full()
523 if mess_data["subject"]: 524 mess_data['xml']["from"] = current_jid.full()
524 mess_data['xml'].addElement("subject", None, subject) 525 mess_data['xml']["type"] = mess_data["type"]
525 # message without body are used to send chat states 526 if mess_data["subject"]:
526 if mess_data["message"]: 527 mess_data['xml'].addElement("subject", None, subject)
527 mess_data['xml'].addElement("body", None, mess_data["message"]) 528 # message without body are used to send chat states
529 if mess_data["message"]:
530 mess_data['xml'].addElement("body", None, mess_data["message"])
531 return mess_data
528 532
529 def sendErrback(e): 533 def sendErrback(e):
530 text = '%s: %s' % (e.value.__class__.__name__, e.getErrorMessage()) 534 text = '%s: %s' % (e.value.__class__.__name__, e.getErrorMessage())
531 if e.check(MessageSentAndStored): 535 if e.check(MessageSentAndStored):
532 debug(text) 536 debug(text)
534 warning(text) 538 warning(text)
535 return e 539 return e
536 else: 540 else:
537 error("Unmanaged exception: %s" % text) 541 error("Unmanaged exception: %s" % text)
538 return e 542 return e
539 543 pre_xml_treatments.addCallback(generateXML)
540 treatments.addCallbacks(self.sendAndStoreMessage, sendErrback, [False, profile]) 544 pre_xml_treatments.chainDeferred(post_xml_treatments)
541 treatments.callback(mess_data) 545 post_xml_treatments.addCallback(self.sendAndStoreMessage, False, profile)
542 return treatments 546 post_xml_treatments.addErrback(sendErrback)
547 pre_xml_treatments.callback(mess_data)
548 return pre_xml_treatments
543 549
544 def sendAndStoreMessage(self, mess_data, skip_send=False, profile=None): 550 def sendAndStoreMessage(self, mess_data, skip_send=False, profile=None):
545 """Actually send and store the message to history, after all the treatments have been done. 551 """Actually send and store the message to history, after all the treatments have been done.
546 This has been moved outside the main sendMessage method because it is used by XEP-0033 to complete a server-side feature not yet 552 This has been moved outside the main sendMessage method because it is used by XEP-0033 to complete a server-side feature not yet
547 implemented by the prosody plugin. 553 implemented by the prosody plugin.