comparison src/plugins/plugin_exp_parrot.py @ 993:301b342c697a

core: use of the new core.log module: /!\ this is a massive refactoring and was largely automated, it probably did bring some bugs /!\
author Goffi <goffi@goffi.org>
date Sat, 19 Apr 2014 19:19:19 +0200
parents cd150dd947e3
children bf3f669a6052
comparison
equal deleted inserted replaced
992:f51a1895275c 993:301b342c697a
17 # You should have received a copy of the GNU Affero General Public License 17 # You should have received a copy of the GNU Affero General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. 18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19 19
20 from sat.core.i18n import _ 20 from sat.core.i18n import _
21 from sat.core.constants import Const as C 21 from sat.core.constants import Const as C
22 from logging import debug, info, warning, error 22 from sat.core.log import getLogger
23 log = getLogger(__name__)
23 from twisted.words.protocols.jabber import jid 24 from twisted.words.protocols.jabber import jid
24 25
25 from sat.core.exceptions import UnknownEntityError 26 from sat.core.exceptions import UnknownEntityError
26 #from sat.tools.misc import SkipOtherTriggers 27 #from sat.tools.misc import SkipOtherTriggers
27 28
44 # this is specially true if we have other triggers. 45 # this is specially true if we have other triggers.
45 # sendMessageTrigger avoid other triggers execution, it's deactivated to allow 46 # sendMessageTrigger avoid other triggers execution, it's deactivated to allow
46 # /unparrot command in text commands plugin. 47 # /unparrot command in text commands plugin.
47 48
48 def __init__(self, host): 49 def __init__(self, host):
49 info(_("Plugin Parrot initialization")) 50 log.info(_("Plugin Parrot initialization"))
50 self.host = host 51 self.host = host
51 host.trigger.add("MessageReceived", self.MessageReceivedTrigger, priority=100) 52 host.trigger.add("MessageReceived", self.MessageReceivedTrigger, priority=100)
52 #host.trigger.add("sendMessage", self.sendMessageTrigger, priority=100) 53 #host.trigger.add("sendMessage", self.sendMessageTrigger, priority=100)
53 try: 54 try:
54 self.host.plugins[C.TEXT_CMDS].registerTextCommands(self) 55 self.host.plugins[C.TEXT_CMDS].registerTextCommands(self)
55 except KeyError: 56 except KeyError:
56 info(_("Text commands not available")) 57 log.info(_("Text commands not available"))
57 58
58 #def sendMessageTrigger(self, mess_data, treatments, profile): 59 #def sendMessageTrigger(self, mess_data, treatments, profile):
59 # """ Deactivate other triggers if recipient is in parrot links """ 60 # """ Deactivate other triggers if recipient is in parrot links """
60 # client = self.host.getClient(profile) 61 # client = self.host.getClient(profile)
61 # try: 62 # try:
62 # _links = client.parrot_links 63 # _links = client.parrot_links
63 # except AttributeError: 64 # except AttributeError:
64 # return True 65 # return True
65 # 66 #
66 # if mess_data['to'].userhostJID() in _links.values(): 67 # if mess_data['to'].userhostJID() in _links.values():
67 # debug("Parrot link detected, skipping other triggers") 68 # log.debug("Parrot link detected, skipping other triggers")
68 # raise SkipOtherTriggers 69 # raise SkipOtherTriggers
69 70
70 def MessageReceivedTrigger(self, message, post_treat, profile): 71 def MessageReceivedTrigger(self, message, post_treat, profile):
71 """ Check if source is linked and repeat message, else do nothing """ 72 """ Check if source is linked and repeat message, else do nothing """
72 client = self.host.getClient(profile) 73 client = self.host.getClient(profile)
99 100
100 linked = _links[from_jid.userhostJID()] 101 linked = _links[from_jid.userhostJID()]
101 102
102 self.host.sendMessage(jid.JID(unicode(linked)), msg, None, "auto", no_trigger=True, profile_key=profile) 103 self.host.sendMessage(jid.JID(unicode(linked)), msg, None, "auto", no_trigger=True, profile_key=profile)
103 else: 104 else:
104 warning("No body element found in message, following normal behaviour") 105 log.warning("No body element found in message, following normal behaviour")
105 106
106 return True 107 return True
107 108
108 def addParrot(self, source_jid, dest_jid, profile): 109 def addParrot(self, source_jid, dest_jid, profile):
109 """Add a parrot link from one entity to another one 110 """Add a parrot link from one entity to another one
115 _links = client.parrot_links 116 _links = client.parrot_links
116 except AttributeError: 117 except AttributeError:
117 _links = client.parrot_links = {} 118 _links = client.parrot_links = {}
118 119
119 _links[source_jid.userhostJID()] = dest_jid 120 _links[source_jid.userhostJID()] = dest_jid
120 info("Parrot mode: %s will be repeated to %s" % (source_jid.userhost(), unicode(dest_jid))) 121 log.info("Parrot mode: %s will be repeated to %s" % (source_jid.userhost(), unicode(dest_jid)))
121 122
122 def removeParrot(self, source_jid, profile): 123 def removeParrot(self, source_jid, profile):
123 """Remove parrot link 124 """Remove parrot link
124 @param source_jid: this entity will no more be repeated 125 @param source_jid: this entity will no more be repeated
125 @param profile: %(doc_profile_key)s""" 126 @param profile: %(doc_profile_key)s"""
129 except (AttributeError, KeyError): 130 except (AttributeError, KeyError):
130 pass 131 pass
131 132
132 def cmd_parrot(self, mess_data, profile): 133 def cmd_parrot(self, mess_data, profile):
133 """activate Parrot mode between 2 entities, in both directions.""" 134 """activate Parrot mode between 2 entities, in both directions."""
134 debug("Catched parrot command") 135 log.debug("Catched parrot command")
135 txt_cmd = self.host.plugins[C.TEXT_CMDS] 136 txt_cmd = self.host.plugins[C.TEXT_CMDS]
136 137
137 try: 138 try:
138 link_left_jid = jid.JID(mess_data["unparsed"].strip()) 139 link_left_jid = jid.JID(mess_data["unparsed"].strip())
139 if not link_left_jid.user or not link_left_jid.host: 140 if not link_left_jid.user or not link_left_jid.host:
151 152
152 return False 153 return False
153 154
154 def cmd_unparrot(self, mess_data, profile): 155 def cmd_unparrot(self, mess_data, profile):
155 """remove Parrot mode between 2 entities, in both directions.""" 156 """remove Parrot mode between 2 entities, in both directions."""
156 debug("Catched unparrot command") 157 log.debug("Catched unparrot command")
157 txt_cmd = self.host.plugins[C.TEXT_CMDS] 158 txt_cmd = self.host.plugins[C.TEXT_CMDS]
158 159
159 try: 160 try:
160 link_left_jid = jid.JID(mess_data["unparsed"].strip()) 161 link_left_jid = jid.JID(mess_data["unparsed"].strip())
161 if not link_left_jid.user or not link_left_jid.host: 162 if not link_left_jid.user or not link_left_jid.host: