comparison src/plugins/plugin_misc_text_commands.py @ 506:2e43c74815ad

plugin text commands: Text commands is a new plugin that bring IRC-like commands - Text commands catch message sent, and check commands starting by / - /nick command is managed
author Goffi <goffi@goffi.org>
date Thu, 27 Sep 2012 00:54:42 +0200
parents
children 7c6609dddb2c
comparison
equal deleted inserted replaced
505:2402668b5d05 506:2e43c74815ad
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 """
5 SàT plugin for managing text commands
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
24 PLUGIN_INFO = {
25 "name": "Text commands",
26 "import_name": "text-commands",
27 "type": "Misc",
28 "protocols": [],
29 "dependencies": ["XEP-0045"],
30 "main": "TextCommands",
31 "handler": "no",
32 "description": _("""IRC like text commands""")
33 }
34
35 class TextCommands():
36
37 def __init__(self, host):
38 info(_("Text commands initialization"))
39 self.host = host
40 host.trigger.add("sendMessage", self.sendMessageTrigger)
41
42 def sendMessageTrigger(self, mess_data, profile):
43 """ Check text commands in message, and react consequently """
44 msg = mess_data["message"]
45 if msg:
46 if msg[0] == '/':
47 command = msg[1:].partition(' ')[0].lower()
48 if command.isalpha():
49 # looks like an actual command, we try to call the corresponding method
50 try:
51 mess_data["unparsed"] = msg[1+len(command):] #part not yet parsed of the message
52 return getattr(self,"cmd_%s" % command)(mess_data,profile)
53 except AttributeError:
54 pass
55 elif msg[0] == '\\': #we have escape char
56 try:
57 if msg[1] in ('/','\\'): # we have '\/' or '\\', we escape to '/' or '\'
58 mess_data["message"] = msg[1:]
59 except IndexError:
60 pass
61 return True
62
63
64 def cmd_nick(self, mess_data, profile):
65 debug("Catched nick command")
66
67 if mess_data['type'] != "groupchat":
68 #/nick command does nothing if we are not on a group chat
69 info("Ignoring /nick command on a non groupchat message")
70
71 return True
72
73 nick = mess_data["unparsed"].strip()
74 room = mess_data["to"]
75
76 self.host.plugins["XEP-0045"].nick(room,nick,profile)
77
78 return False