forked from stv0g/transwhat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.py
104 lines (84 loc) · 2.88 KB
/
bot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
__author__ = "Steffen Vogel"
__copyright__ = "Copyright 2015, Steffen Vogel"
__license__ = "GPLv3"
__maintainer__ = "Steffen Vogel"
__email__ = "[email protected]"
"""
This file is part of transWhat
transWhat is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
any later version.
transwhat is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with transWhat. If not, see <http://www.gnu.org/licenses/>.
"""
import threading
import inspect
import re
import urllib
import time
import os
import utils
class Bot():
def __init__(self, session, name = "Bot"):
self.session = session
self.name = name
self.commands = {
"help": self._help,
"prune": self._prune,
"groups": self._groups,
"getgroups": self._getgroups
}
def parse(self, message):
args = message.strip().split(" ")
cmd = args.pop(0)
if len(cmd) > 0 and cmd[0] == '\\':
try:
self.call(cmd[1:], args)
except KeyError:
self.send("invalid command")
except TypeError:
self.send("invalid syntax")
else:
self.send("a valid command starts with a backslash")
def call(self, cmd, args = []):
func = self.commands[cmd.lower()]
spec = inspect.getargspec(func)
maxs = len(spec.args) - 1
reqs = maxs - len(spec.defaults or [])
if (reqs > len(args)) or (len(args) > maxs):
raise TypeError()
thread = threading.Thread(target=func, args=tuple(args))
thread.start()
def send(self, message):
self.session.backend.handleMessage(self.session.user, self.name, message)
# commands
def _help(self):
self.send("""following bot commands are available:
\\help show this message
\\prune clear your buddylist
following user commands are available:
\\lastseen request last online timestamp from buddy
following group commands are available
\\leave permanently leave group chat
\\groups print all attended groups
\\getgroups get current groups from WA""")
def _prune(self):
self.session.buddies.prune()
self.session.updateRoster()
self.send("buddy list cleared")
def _groups(self):
for group in self.session.groups:
buddy = self.session.groups[group].owner
try:
nick = self.session.buddies[buddy].nick
except KeyError:
nick = buddy
self.send(self.session.groups[group].id + "@" + self.session.backend.spectrum_jid + " " + self.session.groups[group].subject + " Owner: " + nick )
def _getgroups(self):
#self.session.call("group_getGroups", ("participating",))
self.session.requestGroupsList(self.session._updateGroups)