comparison src/plugins/plugin_exp_parrot.py @ 1955:633b5c21aefd

backend, frontend: messages refactoring (huge commit, not finished): /!\ database schema has been modified, do a backup before updating message have been refactored, here are the main changes: - languages are now handled - all messages have an uid (internal to SàT) - message updating is anticipated - subject is now first class - new naming scheme is used newMessage => messageNew, getHistory => historyGet, sendMessage => messageSend - minimal compatibility refactoring in quick_frontend/Primitivus, better refactoring should follow - threads handling - delayed messages are saved into history - info messages may also be saved in history (e.g. to keep track of people joining/leaving a room) - duplicate messages should be avoided - historyGet return messages in right order, no need to sort again - plugins have been updated to follow new features, some of them need to be reworked (e.g. OTR) - XEP-0203 (Delayed Delivery) is now fully handled in core, the plugin just handle disco and creation of a delay element - /!\ jp and Libervia are currently broken, as some features of Primitivus It has been put in one huge commit to avoid breaking messaging between changes. This is the main part of message refactoring, other commits will follow to take profit of the new features/behaviour.
author Goffi <goffi@goffi.org>
date Tue, 24 May 2016 22:11:04 +0200
parents 2daf7b4c6756
children a2bc5089c2eb
comparison
equal deleted inserted replaced
1943:ccfe45302a5c 1955:633b5c21aefd
41 41
42 class Exp_Parrot(object): 42 class Exp_Parrot(object):
43 """Parrot mode plugin: repeat messages from one entity or MUC room to another one""" 43 """Parrot mode plugin: repeat messages from one entity or MUC room to another one"""
44 #XXX: This plugin can be potentially dangerous if we don't trust entities linked 44 #XXX: This plugin can be potentially dangerous if we don't trust entities linked
45 # this is specially true if we have other triggers. 45 # this is specially true if we have other triggers.
46 # sendMessageTrigger avoid other triggers execution, it's deactivated to allow 46 # messageSendTrigger avoid other triggers execution, it's deactivated to allow
47 # /unparrot command in text commands plugin. 47 # /unparrot command in text commands plugin.
48 48
49 def __init__(self, host): 49 def __init__(self, host):
50 log.info(_("Plugin Parrot initialization")) 50 log.info(_("Plugin Parrot initialization"))
51 self.host = host 51 self.host = host
52 host.trigger.add("MessageReceived", self.MessageReceivedTrigger, priority=100) 52 host.trigger.add("MessageReceived", self.MessageReceivedTrigger, priority=100)
53 #host.trigger.add("sendMessage", self.sendMessageTrigger, priority=100) 53 #host.trigger.add("messageSend", self.messageSendTrigger, priority=100)
54 try: 54 try:
55 self.host.plugins[C.TEXT_CMDS].registerTextCommands(self) 55 self.host.plugins[C.TEXT_CMDS].registerTextCommands(self)
56 except KeyError: 56 except KeyError:
57 log.info(_("Text commands not available")) 57 log.info(_("Text commands not available"))
58 58
59 #def sendMessageTrigger(self, mess_data, treatments, profile): 59 #def messageSendTrigger(self, client, mess_data, treatments):
60 # """ Deactivate other triggers if recipient is in parrot links """ 60 # """ Deactivate other triggers if recipient is in parrot links """
61 # client = self.host.getClient(profile)
62 # try: 61 # try:
63 # _links = client.parrot_links 62 # _links = client.parrot_links
64 # except AttributeError: 63 # except AttributeError:
65 # return True 64 # return True
66 # 65 #
67 # if mess_data['to'].userhostJID() in _links.values(): 66 # if mess_data['to'].userhostJID() in _links.values():
68 # log.debug("Parrot link detected, skipping other triggers") 67 # log.debug("Parrot link detected, skipping other triggers")
69 # raise trigger.SkipOtherTriggers 68 # raise trigger.SkipOtherTriggers
70 69
71 def MessageReceivedTrigger(self, message, post_treat, profile): 70 def MessageReceivedTrigger(self, client, message, post_treat):
72 """ Check if source is linked and repeat message, else do nothing """ 71 """ Check if source is linked and repeat message, else do nothing """
72 # TODO: many things are not repeated (subject, thread, etc)
73 profile = client.profile
73 client = self.host.getClient(profile) 74 client = self.host.getClient(profile)
74 from_jid = jid.JID(message["from"]) 75 from_jid = jid.JID(message["from"])
75 76
76 try: 77 try:
77 _links = client.parrot_links 78 _links = client.parrot_links
79 return True 80 return True
80 81
81 if not from_jid.userhostJID() in _links: 82 if not from_jid.userhostJID() in _links:
82 return True 83 return True
83 84
84 for e in message.elements(): 85 message = {}
85 if e.name == "body": 86 for e in message.elements(C.NS_CLIENT, 'body'):
86 mess_body = e.children[0] if e.children else "" 87 body = unicode(e)
88 lang = e.getAttribute('lang') or ''
87 89
88 try: 90 try:
89 entity_type = self.host.memory.getEntityData(from_jid, ['type'], profile)["type"] 91 entity_type = self.host.memory.getEntityData(from_jid, ['type'], profile)["type"]
90 except (UnknownEntityError, KeyError): 92 except (UnknownEntityError, KeyError):
91 entity_type = "contact" 93 entity_type = "contact"
92 if entity_type == 'chatroom': 94 if entity_type == 'chatroom':
93 src_txt = from_jid.resource 95 src_txt = from_jid.resource
94 if src_txt == self.host.plugins["XEP-0045"].getRoomNick(from_jid.userhostJID(), profile): 96 if src_txt == self.host.plugins["XEP-0045"].getRoomNick(from_jid.userhostJID(), profile):
95 #we won't repeat our own messages 97 #we won't repeat our own messages
96 return True 98 return True
97 else: 99 else:
98 src_txt = from_jid.user 100 src_txt = from_jid.user
99 msg = "[%s] %s" % (src_txt, mess_body) 101 message[lang] = u"[{}] {}".format(src_txt, body)
100 102
101 linked = _links[from_jid.userhostJID()] 103 linked = _links[from_jid.userhostJID()]
102 104
103 self.host.sendMessage(jid.JID(unicode(linked)), msg, None, "auto", no_trigger=True, profile_key=profile) 105 self.host.messageSend(jid.JID(unicode(linked)), message, None, "auto", no_trigger=True, profile_key=profile)
104 else:
105 log.warning("No body element found in message, following normal behaviour")
106 106
107 return True 107 return True
108 108
109 def addParrot(self, source_jid, dest_jid, profile): 109 def addParrot(self, source_jid, dest_jid, profile):
110 """Add a parrot link from one entity to another one 110 """Add a parrot link from one entity to another one