comparison src/plugins/plugin_exp_command_export.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 1fe00f0c9a91
children 0befb14ecf62
comparison
equal deleted inserted replaced
992:f51a1895275c 993:301b342c697a
16 16
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 logging import debug, info, warning, error 21 from sat.core.log import getLogger
22 log = getLogger(__name__)
22 from twisted.words.protocols.jabber import jid 23 from twisted.words.protocols.jabber import jid
23 from twisted.internet import reactor, protocol 24 from twisted.internet import reactor, protocol
24 25
25 from sat.tools.misc import SkipOtherTriggers 26 from sat.tools.misc import SkipOtherTriggers
26 from sat.tools.utils import clean_ustr 27 from sat.tools.utils import clean_ustr
45 self.options = options 46 self.options = options
46 self.profile = profile 47 self.profile = profile
47 48
48 def _clean(self, data): 49 def _clean(self, data):
49 if not data: 50 if not data:
50 error ("data should not be empty !") 51 log.error ("data should not be empty !")
51 return u"" 52 return u""
52 decoded = data.decode('utf-8', 'ignore')[:-1 if data[-1] == '\n' else None] 53 decoded = data.decode('utf-8', 'ignore')[:-1 if data[-1] == '\n' else None]
53 return clean_ustr(decoded) 54 return clean_ustr(decoded)
54 55
55 def connectionMade(self): 56 def connectionMade(self):
56 info("connectionMade :)") 57 log.info("connectionMade :)")
57 58
58 def outReceived(self, data): 59 def outReceived(self, data):
59 self.parent.host.sendMessage(self.target, self._clean(data), no_trigger=True, profile_key=self.profile) 60 self.parent.host.sendMessage(self.target, self._clean(data), no_trigger=True, profile_key=self.profile)
60 61
61 def errReceived(self, data): 62 def errReceived(self, data):
62 self.parent.host.sendMessage(self.target, self._clean(data), no_trigger=True, profile_key=self.profile) 63 self.parent.host.sendMessage(self.target, self._clean(data), no_trigger=True, profile_key=self.profile)
63 64
64 def processEnded(self, reason): 65 def processEnded(self, reason):
65 info (u"process finished: %d" % (reason.value.exitCode,)) 66 log.info (u"process finished: %d" % (reason.value.exitCode,))
66 self.parent.removeProcess(self.target, self) 67 self.parent.removeProcess(self.target, self)
67 68
68 def write(self, message): 69 def write(self, message):
69 self.transport.write(message.encode('utf-8')) 70 self.transport.write(message.encode('utf-8'))
70 71
81 """Command export plugin: export a command to an entity""" 82 """Command export plugin: export a command to an entity"""
82 #XXX: This plugin can be potentially dangerous if we don't trust entities linked 83 #XXX: This plugin can be potentially dangerous if we don't trust entities linked
83 # this is specially true if we have other triggers. 84 # this is specially true if we have other triggers.
84 85
85 def __init__(self, host): 86 def __init__(self, host):
86 info(_("Plugin command export initialization")) 87 log.info(_("Plugin command export initialization"))
87 self.host = host 88 self.host = host
88 self.spawned = {} # key = entity 89 self.spawned = {} # key = entity
89 host.trigger.add("MessageReceived", self.MessageReceivedTrigger, priority=10000) 90 host.trigger.add("MessageReceived", self.MessageReceivedTrigger, priority=10000)
90 host.bridge.addMethod("exportCommand", ".plugin", in_sign='sasasa{ss}s', out_sign='', method=self._exportCommand) 91 host.bridge.addMethod("exportCommand", ".plugin", in_sign='sasasa{ss}s', out_sign='', method=self._exportCommand)
91 92
107 spawned_key = (from_jid.userhostJID(), profile) 108 spawned_key = (from_jid.userhostJID(), profile)
108 try: 109 try:
109 body = [e for e in message.elements() if e.name == 'body'][0] 110 body = [e for e in message.elements() if e.name == 'body'][0]
110 except IndexError: 111 except IndexError:
111 # do not block message without body (chat state notification...) 112 # do not block message without body (chat state notification...)
112 warning("No body element found in message, following normal behaviour") 113 log.warning("No body element found in message, following normal behaviour")
113 return True 114 return True
114 115
115 mess_data = unicode(body) + '\n' 116 mess_data = unicode(body) + '\n'
116 117
117 if spawned_key in self.spawned: 118 if spawned_key in self.spawned:
139 - pty: if set, launch in a pseudo terminal 140 - pty: if set, launch in a pseudo terminal
140 - continue: continue normal MessageReceived handling 141 - continue: continue normal MessageReceived handling
141 """ 142 """
142 profile = self.host.memory.getProfileName(profile_key) 143 profile = self.host.memory.getProfileName(profile_key)
143 if not profile: 144 if not profile:
144 warning("Unknown profile [%s]" % (profile,)) 145 log.warning("Unknown profile [%s]" % (profile,))
145 return 146 return
146 147
147 for target in targets: 148 for target in targets:
148 try: 149 try:
149 _jid = jid.JID(target) 150 _jid = jid.JID(target)
150 if not _jid.user or not _jid.host: 151 if not _jid.user or not _jid.host:
151 raise jid.InvalidFormat 152 raise jid.InvalidFormat
152 _jid = _jid.userhostJID() 153 _jid = _jid.userhostJID()
153 except (jid.InvalidFormat, RuntimeError): 154 except (jid.InvalidFormat, RuntimeError):
154 info(u"invalid target ignored: %s" % (target,)) 155 log.info(u"invalid target ignored: %s" % (target,))
155 continue 156 continue
156 process_prot = ExportCommandProtocol(self, _jid, options, profile) 157 process_prot = ExportCommandProtocol(self, _jid, options, profile)
157 self.spawned.setdefault((_jid, profile),set()).add(process_prot) 158 self.spawned.setdefault((_jid, profile),set()).add(process_prot)
158 reactor.spawnProcess(process_prot, command, args, usePTY = process_prot.boolOption('pty')) 159 reactor.spawnProcess(process_prot, command, args, usePTY = process_prot.boolOption('pty'))
159 160