Mercurial > libervia-backend
comparison src/core/sat_main.py @ 489:f36c705a5310
core: sendMessage refactoring:
- "auto" message type which autodetect XMPP message type according to context
- added "sendMessage" trigger so plugins can hook messages
author | Goffi <goffi@goffi.org> |
---|---|
date | Fri, 17 Aug 2012 03:11:03 +0200 |
parents | 23cbdf0a0777 |
children | e9634d2e7b38 |
comparison
equal
deleted
inserted
replaced
488:6edb4219fcf7 | 489:f36c705a5310 |
---|---|
24 'client_version' : u'0.2.0D', #Please add 'D' at the end for dev versions | 24 'client_version' : u'0.2.0D', #Please add 'D' at the end for dev versions |
25 } | 25 } |
26 | 26 |
27 from twisted.application import service | 27 from twisted.application import service |
28 from twisted.internet import defer | 28 from twisted.internet import defer |
29 from twisted.internet.defer import inlineCallbacks, returnValue | |
29 | 30 |
30 from twisted.words.protocols.jabber import jid, xmlstream | 31 from twisted.words.protocols.jabber import jid, xmlstream |
31 from twisted.words.xish import domish | 32 from twisted.words.xish import domish |
32 | 33 |
33 from twisted.internet import reactor | 34 from twisted.internet import reactor |
41 | 42 |
42 import sys | 43 import sys |
43 import os.path | 44 import os.path |
44 | 45 |
45 from sat.core import xmpp | 46 from sat.core import xmpp |
46 from sat.core.exceptions import ProfileUnknownError | 47 from sat.core.exceptions import ProfileUnknownError, UnknownEntityError |
47 from sat.memory.memory import Memory | 48 from sat.memory.memory import Memory |
48 from sat.tools.xml_tools import tupleList2dataForm | 49 from sat.tools.xml_tools import tupleList2dataForm |
49 from sat.tools.misc import TriggerManager | 50 from sat.tools.misc import TriggerManager |
50 from glob import glob | 51 from glob import glob |
51 | 52 |
443 return "" | 444 return "" |
444 | 445 |
445 | 446 |
446 ## jabber methods ## | 447 ## jabber methods ## |
447 | 448 |
448 def sendMessage(self, to, msg, subject=None, type='chat', profile_key='@DEFAULT@'): | 449 def sendMessage(self, to, msg, subject=None, mess_type='auto', profile_key='@DEFAULT@'): |
449 #FIXME: check validity of recipient | 450 #FIXME: check validity of recipient |
450 profile = self.memory.getProfileName(profile_key) | 451 profile = self.memory.getProfileName(profile_key) |
451 assert(profile) | 452 assert(profile) |
452 current_jid = self.profiles[profile].jid | 453 client = self.profiles[profile] |
453 debug(_("Sending jabber message to %s..."), to) | 454 current_jid = client.jid |
455 mess_data = { #we put data in a dict, so trigger methods can change them | |
456 "to": jid.JID(to), | |
457 "message": msg, | |
458 "subject": subject, | |
459 "type": mess_type | |
460 } | |
461 if not self.trigger.point("sendMessage", mess_data, profile): | |
462 return | |
463 if mess_data["type"] == "auto": | |
464 #XXX: if the type has not been changed by a trigger method, | |
465 # we try to guess it | |
466 if mess_data["subject"]: | |
467 mess_data["type"] = 'normal' | |
468 elif not mess_data["to"].resource: #if to JID has a resource, the type is not 'groupchat' | |
469 #we may have a groupchat message, we check if the we know this jid | |
470 try: | |
471 entity_type = self.memory.getEntityData(mess_data["to"], ['type'], profile)["type"] | |
472 #FIXME: should entity_type manage ressources ? | |
473 except (UnknownEntityError, KeyError): | |
474 entity_type = "contact" | |
475 | |
476 if entity_type == "chatroom": | |
477 mess_data["type"] = 'groupchat' | |
478 else: | |
479 mess_data["type"] = 'chat' | |
480 else: | |
481 mess_data["type"] == 'chat' | |
482 mess_data["type"] == "chat" if mess_data["subject"] else "normal" | |
483 debug(_("Sending jabber message of type [%(type)s] to %(to)s...") % {"type": mess_data["type"], "to": to}) | |
454 message = domish.Element(('jabber:client','message')) | 484 message = domish.Element(('jabber:client','message')) |
455 message["to"] = jid.JID(to).full() | 485 message["to"] = mess_data["to"].full() |
456 message["from"] = current_jid.full() | 486 message["from"] = current_jid.full() |
457 message["type"] = type | 487 message["type"] = mess_data["type"] |
458 if subject: | 488 if mess_data["subject"]: |
459 message.addElement("subject", "jabber:client", subject) | 489 message.addElement("subject", "jabber:client", subject) |
460 message.addElement("body", "jabber:client", msg) | 490 message.addElement("body", "jabber:client", msg) |
461 self.profiles[profile].xmlstream.send(message) | 491 client.xmlstream.send(message) |
462 if type!="groupchat": | 492 if mess_data["type"]!="groupchat": |
463 self.memory.addToHistory(current_jid, jid.JID(to), unicode(msg), profile=profile) #we don't add groupchat message to history, as we get them back | 493 self.memory.addToHistory(current_jid, jid.JID(to), unicode(msg), profile=profile) #we don't add groupchat message to history, as we get them back |
464 #and they will we added then | 494 #and they will be added then |
465 self.bridge.newMessage(message['from'], unicode(msg), mess_type=type, to_jid=message['to'], profile=profile) #We send back the message, so all clients are aware of it | 495 self.bridge.newMessage(message['from'], unicode(msg), mess_type=mess_data["type"], to_jid=message['to'], profile=profile) #We send back the message, so all clients are aware of it |
466 | 496 |
467 | 497 |
468 def setPresence(self, to="", show="", priority = 0, statuses={}, profile_key='@DEFAULT@'): | 498 def setPresence(self, to="", show="", priority = 0, statuses={}, profile_key='@DEFAULT@'): |
469 """Send our presence information""" | 499 """Send our presence information""" |
470 profile = self.memory.getProfileName(profile_key) | 500 profile = self.memory.getProfileName(profile_key) |