2016-04-12 15:24:57 +00:00
|
|
|
# use unicode encoding for all literals by default (for python2.x)
|
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
2015-12-31 19:43:10 +00:00
|
|
|
__author__ = "Steffen Vogel"
|
|
|
|
__copyright__ = "Copyright 2015, Steffen Vogel"
|
|
|
|
__license__ = "GPLv3"
|
|
|
|
__maintainer__ = "Steffen Vogel"
|
|
|
|
__email__ = "post@steffenvogel.de"
|
2013-08-01 23:45:51 +00:00
|
|
|
|
2015-12-31 19:43:10 +00:00
|
|
|
"""
|
2013-08-01 23:45:51 +00:00
|
|
|
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/>.
|
|
|
|
"""
|
|
|
|
|
2013-06-28 19:38:19 +00:00
|
|
|
import threading
|
|
|
|
import inspect
|
|
|
|
import re
|
|
|
|
import urllib
|
|
|
|
import time
|
|
|
|
import os
|
|
|
|
import utils
|
|
|
|
|
|
|
|
class Bot():
|
2015-12-31 19:43:10 +00:00
|
|
|
def __init__(self, session, name = "Bot"):
|
2013-06-28 19:38:19 +00:00
|
|
|
self.session = session
|
|
|
|
self.name = name
|
|
|
|
|
|
|
|
self.commands = {
|
2015-12-31 19:43:10 +00:00
|
|
|
"help": self._help,
|
|
|
|
"prune": self._prune,
|
2016-04-06 20:02:40 +00:00
|
|
|
"groups": self._groups,
|
2015-12-31 19:43:10 +00:00
|
|
|
"getgroups": self._getgroups
|
2013-06-28 19:38:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
def parse(self, message):
|
2016-01-06 12:39:34 +00:00
|
|
|
args = message.strip().split(" ")
|
2013-06-28 19:38:19 +00:00
|
|
|
cmd = args.pop(0)
|
|
|
|
|
2016-01-06 18:56:30 +00:00
|
|
|
if len(cmd) > 0 and cmd[0] == '\\':
|
2013-06-28 19:38:19 +00:00
|
|
|
try:
|
|
|
|
self.call(cmd[1:], args)
|
|
|
|
except KeyError:
|
2015-12-31 19:43:10 +00:00
|
|
|
self.send("invalid command")
|
2013-06-28 19:38:19 +00:00
|
|
|
except TypeError:
|
2015-12-31 19:43:10 +00:00
|
|
|
self.send("invalid syntax")
|
2013-06-28 19:38:19 +00:00
|
|
|
else:
|
2015-12-31 19:43:10 +00:00
|
|
|
self.send("a valid command starts with a backslash")
|
2013-06-28 19:38:19 +00:00
|
|
|
|
|
|
|
def call(self, cmd, args = []):
|
2016-01-06 12:39:34 +00:00
|
|
|
func = self.commands[cmd.lower()]
|
2013-06-28 19:38:19 +00:00
|
|
|
spec = inspect.getargspec(func)
|
|
|
|
maxs = len(spec.args) - 1
|
|
|
|
reqs = maxs - len(spec.defaults or [])
|
2015-11-10 16:04:20 +00:00
|
|
|
if (reqs > len(args)) or (len(args) > maxs):
|
2013-06-28 19:38:19 +00:00
|
|
|
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):
|
2015-12-31 19:43:10 +00:00
|
|
|
self.send("""following bot commands are available:
|
2015-12-03 12:49:15 +00:00
|
|
|
\\help show this message
|
2013-06-28 19:38:19 +00:00
|
|
|
\\prune clear your buddylist
|
|
|
|
|
|
|
|
following user commands are available:
|
2015-12-03 12:49:15 +00:00
|
|
|
\\lastseen request last online timestamp from buddy
|
2015-11-27 20:01:16 +00:00
|
|
|
|
|
|
|
following group commands are available
|
2015-12-03 12:49:15 +00:00
|
|
|
\\leave permanently leave group chat
|
|
|
|
\\groups print all attended groups
|
|
|
|
\\getgroups get current groups from WA""")
|
2013-06-28 19:38:19 +00:00
|
|
|
|
|
|
|
def _prune(self):
|
|
|
|
self.session.buddies.prune()
|
|
|
|
self.session.updateRoster()
|
2015-12-31 19:43:10 +00:00
|
|
|
self.send("buddy list cleared")
|
2015-12-03 12:49:15 +00:00
|
|
|
|
2015-11-09 22:09:12 +00:00
|
|
|
def _groups(self):
|
|
|
|
for group in self.session.groups:
|
|
|
|
buddy = self.session.groups[group].owner
|
|
|
|
try:
|
2016-04-16 08:32:47 +00:00
|
|
|
nick = self.session.buddies[buddy].nick
|
|
|
|
except KeyError:
|
|
|
|
nick = buddy
|
2015-11-09 22:09:12 +00:00
|
|
|
|
2015-12-31 19:43:10 +00:00
|
|
|
self.send(self.session.groups[group].id + "@" + self.session.backend.spectrum_jid + " " + self.session.groups[group].subject + " Owner: " + nick )
|
2015-12-03 12:49:15 +00:00
|
|
|
|
2015-11-09 22:09:12 +00:00
|
|
|
def _getgroups(self):
|
2015-12-31 19:43:10 +00:00
|
|
|
#self.session.call("group_getGroups", ("participating",))
|
2015-11-09 22:09:12 +00:00
|
|
|
self.session.requestGroupsList(self.session._updateGroups)
|
|
|
|
|