Mercurial > libervia-backend
annotate 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 |
rev | line source |
---|---|
506
2e43c74815ad
plugin text commands: Text commands is a new plugin that bring IRC-like commands
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1 #!/usr/bin/python |
2e43c74815ad
plugin text commands: Text commands is a new plugin that bring IRC-like commands
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
2 # -*- coding: utf-8 -*- |
2e43c74815ad
plugin text commands: Text commands is a new plugin that bring IRC-like commands
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
3 |
2e43c74815ad
plugin text commands: Text commands is a new plugin that bring IRC-like commands
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
4 """ |
2e43c74815ad
plugin text commands: Text commands is a new plugin that bring IRC-like commands
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
5 SàT plugin for managing text commands |
2e43c74815ad
plugin text commands: Text commands is a new plugin that bring IRC-like commands
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
6 Copyright (C) 2009, 2010, 2011, 2012 Jérôme Poisson (goffi@goffi.org) |
2e43c74815ad
plugin text commands: Text commands is a new plugin that bring IRC-like commands
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
7 |
2e43c74815ad
plugin text commands: Text commands is a new plugin that bring IRC-like commands
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
8 This program is free software: you can redistribute it and/or modify |
2e43c74815ad
plugin text commands: Text commands is a new plugin that bring IRC-like commands
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
9 it under the terms of the GNU Affero General Public License as published by |
2e43c74815ad
plugin text commands: Text commands is a new plugin that bring IRC-like commands
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
10 the Free Software Foundation, either version 3 of the License, or |
2e43c74815ad
plugin text commands: Text commands is a new plugin that bring IRC-like commands
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
11 (at your option) any later version. |
2e43c74815ad
plugin text commands: Text commands is a new plugin that bring IRC-like commands
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
12 |
2e43c74815ad
plugin text commands: Text commands is a new plugin that bring IRC-like commands
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
13 This program is distributed in the hope that it will be useful, |
2e43c74815ad
plugin text commands: Text commands is a new plugin that bring IRC-like commands
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
14 but WITHOUT ANY WARRANTY; without even the implied warranty of |
2e43c74815ad
plugin text commands: Text commands is a new plugin that bring IRC-like commands
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
2e43c74815ad
plugin text commands: Text commands is a new plugin that bring IRC-like commands
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
16 GNU Affero General Public License for more details. |
2e43c74815ad
plugin text commands: Text commands is a new plugin that bring IRC-like commands
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
17 |
2e43c74815ad
plugin text commands: Text commands is a new plugin that bring IRC-like commands
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
18 You should have received a copy of the GNU Affero General Public License |
2e43c74815ad
plugin text commands: Text commands is a new plugin that bring IRC-like commands
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
19 along with this program. If not, see <http://www.gnu.org/licenses/>. |
2e43c74815ad
plugin text commands: Text commands is a new plugin that bring IRC-like commands
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
20 """ |
2e43c74815ad
plugin text commands: Text commands is a new plugin that bring IRC-like commands
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
21 |
2e43c74815ad
plugin text commands: Text commands is a new plugin that bring IRC-like commands
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
22 from logging import debug, info, warning, error |
2e43c74815ad
plugin text commands: Text commands is a new plugin that bring IRC-like commands
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
23 |
2e43c74815ad
plugin text commands: Text commands is a new plugin that bring IRC-like commands
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
24 PLUGIN_INFO = { |
2e43c74815ad
plugin text commands: Text commands is a new plugin that bring IRC-like commands
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
25 "name": "Text commands", |
2e43c74815ad
plugin text commands: Text commands is a new plugin that bring IRC-like commands
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
26 "import_name": "text-commands", |
2e43c74815ad
plugin text commands: Text commands is a new plugin that bring IRC-like commands
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
27 "type": "Misc", |
2e43c74815ad
plugin text commands: Text commands is a new plugin that bring IRC-like commands
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
28 "protocols": [], |
2e43c74815ad
plugin text commands: Text commands is a new plugin that bring IRC-like commands
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
29 "dependencies": ["XEP-0045"], |
2e43c74815ad
plugin text commands: Text commands is a new plugin that bring IRC-like commands
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
30 "main": "TextCommands", |
2e43c74815ad
plugin text commands: Text commands is a new plugin that bring IRC-like commands
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
31 "handler": "no", |
2e43c74815ad
plugin text commands: Text commands is a new plugin that bring IRC-like commands
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
32 "description": _("""IRC like text commands""") |
2e43c74815ad
plugin text commands: Text commands is a new plugin that bring IRC-like commands
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
33 } |
2e43c74815ad
plugin text commands: Text commands is a new plugin that bring IRC-like commands
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
34 |
2e43c74815ad
plugin text commands: Text commands is a new plugin that bring IRC-like commands
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
35 class TextCommands(): |
2e43c74815ad
plugin text commands: Text commands is a new plugin that bring IRC-like commands
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
36 |
2e43c74815ad
plugin text commands: Text commands is a new plugin that bring IRC-like commands
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
37 def __init__(self, host): |
2e43c74815ad
plugin text commands: Text commands is a new plugin that bring IRC-like commands
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
38 info(_("Text commands initialization")) |
2e43c74815ad
plugin text commands: Text commands is a new plugin that bring IRC-like commands
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
39 self.host = host |
2e43c74815ad
plugin text commands: Text commands is a new plugin that bring IRC-like commands
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
40 host.trigger.add("sendMessage", self.sendMessageTrigger) |
2e43c74815ad
plugin text commands: Text commands is a new plugin that bring IRC-like commands
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
41 |
2e43c74815ad
plugin text commands: Text commands is a new plugin that bring IRC-like commands
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
42 def sendMessageTrigger(self, mess_data, profile): |
2e43c74815ad
plugin text commands: Text commands is a new plugin that bring IRC-like commands
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
43 """ Check text commands in message, and react consequently """ |
2e43c74815ad
plugin text commands: Text commands is a new plugin that bring IRC-like commands
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
44 msg = mess_data["message"] |
2e43c74815ad
plugin text commands: Text commands is a new plugin that bring IRC-like commands
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
45 if msg: |
2e43c74815ad
plugin text commands: Text commands is a new plugin that bring IRC-like commands
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
46 if msg[0] == '/': |
2e43c74815ad
plugin text commands: Text commands is a new plugin that bring IRC-like commands
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
47 command = msg[1:].partition(' ')[0].lower() |
2e43c74815ad
plugin text commands: Text commands is a new plugin that bring IRC-like commands
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
48 if command.isalpha(): |
2e43c74815ad
plugin text commands: Text commands is a new plugin that bring IRC-like commands
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
49 # looks like an actual command, we try to call the corresponding method |
2e43c74815ad
plugin text commands: Text commands is a new plugin that bring IRC-like commands
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
50 try: |
2e43c74815ad
plugin text commands: Text commands is a new plugin that bring IRC-like commands
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
51 mess_data["unparsed"] = msg[1+len(command):] #part not yet parsed of the message |
2e43c74815ad
plugin text commands: Text commands is a new plugin that bring IRC-like commands
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
52 return getattr(self,"cmd_%s" % command)(mess_data,profile) |
2e43c74815ad
plugin text commands: Text commands is a new plugin that bring IRC-like commands
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
53 except AttributeError: |
2e43c74815ad
plugin text commands: Text commands is a new plugin that bring IRC-like commands
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
54 pass |
2e43c74815ad
plugin text commands: Text commands is a new plugin that bring IRC-like commands
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
55 elif msg[0] == '\\': #we have escape char |
2e43c74815ad
plugin text commands: Text commands is a new plugin that bring IRC-like commands
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
56 try: |
2e43c74815ad
plugin text commands: Text commands is a new plugin that bring IRC-like commands
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
57 if msg[1] in ('/','\\'): # we have '\/' or '\\', we escape to '/' or '\' |
2e43c74815ad
plugin text commands: Text commands is a new plugin that bring IRC-like commands
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
58 mess_data["message"] = msg[1:] |
2e43c74815ad
plugin text commands: Text commands is a new plugin that bring IRC-like commands
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
59 except IndexError: |
2e43c74815ad
plugin text commands: Text commands is a new plugin that bring IRC-like commands
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
60 pass |
2e43c74815ad
plugin text commands: Text commands is a new plugin that bring IRC-like commands
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
61 return True |
2e43c74815ad
plugin text commands: Text commands is a new plugin that bring IRC-like commands
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
62 |
2e43c74815ad
plugin text commands: Text commands is a new plugin that bring IRC-like commands
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
63 |
2e43c74815ad
plugin text commands: Text commands is a new plugin that bring IRC-like commands
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
64 def cmd_nick(self, mess_data, profile): |
2e43c74815ad
plugin text commands: Text commands is a new plugin that bring IRC-like commands
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
65 debug("Catched nick command") |
2e43c74815ad
plugin text commands: Text commands is a new plugin that bring IRC-like commands
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
66 |
2e43c74815ad
plugin text commands: Text commands is a new plugin that bring IRC-like commands
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
67 if mess_data['type'] != "groupchat": |
2e43c74815ad
plugin text commands: Text commands is a new plugin that bring IRC-like commands
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
68 #/nick command does nothing if we are not on a group chat |
2e43c74815ad
plugin text commands: Text commands is a new plugin that bring IRC-like commands
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
69 info("Ignoring /nick command on a non groupchat message") |
2e43c74815ad
plugin text commands: Text commands is a new plugin that bring IRC-like commands
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
70 |
2e43c74815ad
plugin text commands: Text commands is a new plugin that bring IRC-like commands
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
71 return True |
2e43c74815ad
plugin text commands: Text commands is a new plugin that bring IRC-like commands
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
72 |
2e43c74815ad
plugin text commands: Text commands is a new plugin that bring IRC-like commands
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
73 nick = mess_data["unparsed"].strip() |
2e43c74815ad
plugin text commands: Text commands is a new plugin that bring IRC-like commands
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
74 room = mess_data["to"] |
2e43c74815ad
plugin text commands: Text commands is a new plugin that bring IRC-like commands
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
75 |
2e43c74815ad
plugin text commands: Text commands is a new plugin that bring IRC-like commands
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
76 self.host.plugins["XEP-0045"].nick(room,nick,profile) |
2e43c74815ad
plugin text commands: Text commands is a new plugin that bring IRC-like commands
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
77 |
2e43c74815ad
plugin text commands: Text commands is a new plugin that bring IRC-like commands
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
78 return False |