WIP
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

74 lines
2.2 KiB

8 years ago
import threading
import inspect
import re
import urllib
import time
import os
class Bot():
def __init__(self, session, name = "Bot"):
self.session = session
self.name = name
8 years ago
self.commands = {
"help": self._help,
"groups": self._groups,
"getgroups": self._getgroups
}
8 years ago
def parse(self, message):
args = message.strip().split(" ")
cmd = args.pop(0)
8 years ago
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")
8 years ago
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()
8 years ago
thread = threading.Thread(target=func, args=tuple(args))
thread.start()
8 years ago
def send(self, message):
self.session.backend.handleMessage(self.session.user, self.name, message)
8 years ago
# commands
def _help(self):
self.send("""following bot commands are available:
\\help show this message
8 years ago
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""")
8 years ago
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)