plugin XEP-0085: Chat State Notifications

- new "options" parameter to send chat states
    - plugin command export: messages without body are now delivered (since all the chat states other than "active" need them)
This commit is contained in:
souliane 2013-09-05 20:48:47 +02:00
commit 9cb7071e89
2 changed files with 368 additions and 33 deletions

View file

@ -469,21 +469,24 @@ class SAT(service.Service):
ret.append((conf_id, conf_type, data))
return ret
def _sendMessage(self, to_s, msg, subject=None, mess_type='auto', profile_key='@NONE@'):
def _sendMessage(self, to_s, msg, subject=None, mess_type='auto', options={}, profile_key='@NONE@'):
to_jid = jid.JID(to_s)
self.sendMessage(to_jid, msg, subject, mess_type, profile_key=profile_key)
def sendMessage(self, to_jid, msg, subject=None, mess_type='auto', no_trigger = False, profile_key='@NONE@'):
self.sendMessage(to_jid, msg, subject, mess_type, options=options, profile_key=profile_key)
def sendMessage(self, to_jid, msg, subject=None, mess_type='auto', options={}, no_trigger=False, profile_key='@NONE@'):
#FIXME: check validity of recipient
profile = self.memory.getProfileName(profile_key)
assert(profile)
client = self.profiles[profile]
current_jid = client.jid
if options is None:
options = {}
mess_data = { # we put data in a dict, so trigger methods can change them
"to": to_jid,
"message": msg,
"subject": subject,
"type": mess_type
"type": mess_type,
"options": options,
}
if mess_data["type"] == "auto":
@ -517,12 +520,28 @@ class SAT(service.Service):
message["type"] = mess_data["type"]
if mess_data["subject"]:
message.addElement("subject", None, subject)
message.addElement("body", None, mess_data["message"])
# message without body are used to send chat states
if mess_data["message"]:
message.addElement("body", None, mess_data["message"])
if not no_trigger:
if not self.trigger.point("sendMessageXml", message,
mess_data, profile):
return
client.xmlstream.send(message)
if mess_data["type"] != "groupchat":
self.memory.addToHistory(current_jid, mess_data['to'], unicode(mess_data["message"]), unicode(mess_data["type"]), profile=profile) # we don't add groupchat message to history, as we get them back
# and they will be added then
self.bridge.newMessage(message['from'], unicode(mess_data["message"]), mess_type=mess_data["type"], to_jid=message['to'], extra={}, profile=profile) # We send back the message, so all clients are aware of it
# we don't add groupchat message to history, as we get them back
# and they will be added then
self.memory.addToHistory(current_jid, mess_data['to'],
unicode(mess_data["message"]),
unicode(mess_data["type"]),
profile=profile)
# We send back the message, so all clients are aware of it
if mess_data["message"]:
self.bridge.newMessage(message['from'],
unicode(mess_data["message"]),
mess_type=mess_data["type"],
to_jid=message['to'], extra={},
profile=profile)
def setPresence(self, to="", show="", priority=0, statuses={}, profile_key='@DEFAULT@'):
"""Send our presence information"""

View file

@ -108,23 +108,27 @@ class SatMessageProtocol(xmppim.MessageProtocol):
debug(_(u"got message from: %s"), message["from"])
if not self.host.trigger.point("MessageReceived", message, profile=self.parent.profile):
return
# set message body to empty string by default, and later
# also forward message without body (chat state notification...)
mess_body = ""
for e in message.elements():
if e.name == "body":
mess_type = message['type'] if message.hasAttribute('type') else 'normal'
mess_body = e.children[0] if e.children else ""
try:
_delay = delay.Delay.fromElement(filter(lambda elm: elm.name == 'delay', message.elements())[0])
timestamp = timegm(_delay.stamp.utctimetuple())
extra = {"archive": str(timestamp)}
if mess_type != 'groupchat': # XXX: we don't save delayed messages in history for groupchats
#TODO: add delayed messages to history if they aren't already in it
self.host.memory.addToHistory(jid.JID(message["from"]), jid.JID(message["to"]), mess_body, mess_type, timestamp, profile=self.parent.profile)
except IndexError:
extra = {}
self.host.memory.addToHistory(jid.JID(message["from"]), jid.JID(message["to"]), mess_body, mess_type, profile=self.parent.profile)
self.host.bridge.newMessage(message["from"], mess_body, mess_type, message['to'], extra, profile=self.parent.profile)
break
mess_type = message['type'] if message.hasAttribute('type') else 'normal'
try:
_delay = delay.Delay.fromElement(filter(lambda elm: elm.name == 'delay', message.elements())[0])
timestamp = timegm(_delay.stamp.utctimetuple())
extra = {"archive": str(timestamp)}
if mess_type != 'groupchat': # XXX: we don't save delayed messages in history for groupchats
#TODO: add delayed messages to history if they aren't already in it
self.host.memory.addToHistory(jid.JID(message["from"]), jid.JID(message["to"]), mess_body, mess_type, timestamp, profile=self.parent.profile)
except IndexError:
extra = {}
self.host.memory.addToHistory(jid.JID(message["from"]), jid.JID(message["to"]), mess_body, mess_type, profile=self.parent.profile)
self.host.bridge.newMessage(message["from"], mess_body, mess_type, message['to'], extra, profile=self.parent.profile)
class SatRosterProtocol(xmppim.RosterClientProtocol):