Mercurial > libervia-backend
comparison src/plugins/plugin_exp_parrot.py @ 526:d67f0ce83530
new plugin: Parrot is an experimental plugin which repeat messages between 2 entities
author | Goffi <goffi@goffi.org> |
---|---|
date | Sun, 21 Oct 2012 19:43:14 +0200 |
parents | |
children | c18e0e108925 |
comparison
equal
deleted
inserted
replaced
525:5431136501ab | 526:d67f0ce83530 |
---|---|
1 #!/usr/bin/python | |
2 # -*- coding: utf-8 -*- | |
3 | |
4 """ | |
5 SAT plugin for parrot mode (experimental) | |
6 Copyright (C) 2009, 2010, 2011, 2012 Jérôme Poisson (goffi@goffi.org) | |
7 | |
8 This program is free software: you can redistribute it and/or modify | |
9 it under the terms of the GNU Affero General Public License as published by | |
10 the Free Software Foundation, either version 3 of the License, or | |
11 (at your option) any later version. | |
12 | |
13 This program is distributed in the hope that it will be useful, | |
14 but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 GNU Affero General Public License for more details. | |
17 | |
18 You should have received a copy of the GNU Affero General Public License | |
19 along with this program. If not, see <http://www.gnu.org/licenses/>. | |
20 """ | |
21 | |
22 from logging import debug, info, warning, error | |
23 from twisted.words.protocols.jabber import jid | |
24 | |
25 | |
26 from sat.core.exceptions import UnknownEntityError | |
27 from sat.tools.misc import SkipOtherTriggers | |
28 | |
29 PLUGIN_INFO = { | |
30 "name": "Parrot Plugin", | |
31 "import_name": "EXP-PARROT", | |
32 "type": "EXP", | |
33 "protocols": [], | |
34 "dependencies": ["XEP-0045"], | |
35 "main": "Exp_Parrot", | |
36 "handler": "no", | |
37 "description": _("""Implementation of parrot mode (repeat messages between 2 entities)""") | |
38 } | |
39 | |
40 class Exp_Parrot(): | |
41 """Parrot mode plugin: repeat messages from one entity or MUC room to another one""" | |
42 #XXX: This plugin can be potentially dangerous if we don't trust entities linked | |
43 # this is specially true if we have other triggers. | |
44 # sendMessageTrigger avoid other triggers execution, it's deactivated to allow | |
45 # /unparrot command in text commands plugin. | |
46 | |
47 def __init__(self, host): | |
48 info(_("Plugin Parrot initialization")) | |
49 self.host = host | |
50 host.trigger.add("MessageReceived", self.MessageReceivedTrigger, priority=100) | |
51 #host.trigger.add("sendMessage", self.sendMessageTrigger, priority=100) | |
52 | |
53 #def sendMessageTrigger(self, mess_data, profile): | |
54 # """ Deactivate other triggers if recipient is in parrot links """ | |
55 # client = self.host.getClient(profile) | |
56 # try: | |
57 # _links = client.parrot_links | |
58 # except AttributeError: | |
59 # return True | |
60 # | |
61 # if mess_data['to'].userhostJID() in _links.values(): | |
62 # debug("Parrot link detected, skipping other triggers") | |
63 # raise SkipOtherTriggers | |
64 | |
65 def MessageReceivedTrigger(self, message, profile): | |
66 """ Check if source is linked and repeat message, else do nothing """ | |
67 client = self.host.getClient(profile) | |
68 from_jid = jid.JID(message["from"]) | |
69 | |
70 try: | |
71 _links = client.parrot_links | |
72 except AttributeError: | |
73 return True | |
74 | |
75 if not from_jid.userhostJID() in _links: | |
76 return True | |
77 | |
78 for e in message.elements(): | |
79 if e.name == "body": | |
80 mess_body = e.children[0] if e.children else "" | |
81 | |
82 try: | |
83 entity_type = self.host.memory.getEntityData(from_jid, ['type'], profile)["type"] | |
84 except (UnknownEntityError, KeyError): | |
85 entity_type = "contact" | |
86 if entity_type == 'chatroom': | |
87 src_txt = from_jid.resource | |
88 if src_txt == self.host.plugins["XEP-0045"].getRoomNick(from_jid.userhost(), profile): | |
89 #we won't repeat our own messages | |
90 print "\n\nDISCARD\n\n" | |
91 return True | |
92 else: | |
93 src_txt = from_jid.user | |
94 msg = "[%s] %s" % (src_txt, mess_body) | |
95 | |
96 linked = _links[from_jid.userhostJID()] | |
97 | |
98 self.host.sendMessage(unicode(linked), msg, None, "auto", profile_key=profile) | |
99 else: | |
100 warning("No body element found in message, following normal behaviour") | |
101 | |
102 return True | |
103 | |
104 def addParrot(self, source_jid, dest_jid, profile): | |
105 """Add a parrot link from one entity to another one | |
106 @param source_jid: entity from who messages will be repeated | |
107 @param dest_jid: entity where the messages will be repeated | |
108 @param profile: %(doc_profile_key)s""" | |
109 client = self.host.getClient(profile) | |
110 try: | |
111 _links = client.parrot_links | |
112 except AttributeError: | |
113 _links = client.parrot_links = {} | |
114 | |
115 _links[source_jid.userhostJID()] = dest_jid | |
116 info("Parrot mode: %s will be repeated to %s" % (source_jid.userhost(), unicode(dest_jid))) | |
117 | |
118 def removeParrot(self, source_jid, profile): | |
119 """Remove parrot link | |
120 @param source_jid: this entity will no more be repeated | |
121 @param profile: %(doc_profile_key)s""" | |
122 client = self.host.getClient(profile) | |
123 try: | |
124 del client.parrot_links[source_jid.userhostJID()] | |
125 except (AttributeError, KeyError): | |
126 pass | |
127 | |
128 |