2021-01-10 13:10:39 +00:00
|
|
|
"""XMPP bots for humans."""
|
|
|
|
|
2021-01-16 15:38:12 +00:00
|
|
|
import re
|
2021-01-14 21:26:13 +00:00
|
|
|
from argparse import ArgumentParser
|
2021-01-10 13:10:39 +00:00
|
|
|
from configparser import ConfigParser
|
2021-01-14 22:45:23 +00:00
|
|
|
from datetime import datetime as dt
|
2021-01-10 13:10:39 +00:00
|
|
|
from getpass import getpass
|
2021-01-15 10:08:07 +00:00
|
|
|
from imghdr import what
|
2021-01-16 12:05:30 +00:00
|
|
|
from inspect import cleandoc
|
2021-01-14 18:18:27 +00:00
|
|
|
from logging import DEBUG, INFO, basicConfig, getLogger
|
2021-01-12 20:39:50 +00:00
|
|
|
from os import environ
|
2021-01-10 13:10:39 +00:00
|
|
|
from os.path import exists
|
|
|
|
from pathlib import Path
|
2021-01-15 20:14:04 +00:00
|
|
|
from random import choice
|
2021-01-14 21:26:13 +00:00
|
|
|
from sys import exit, stdout
|
2021-01-10 13:10:39 +00:00
|
|
|
|
2021-01-14 22:45:23 +00:00
|
|
|
from humanize import naturaldelta
|
2021-01-15 20:14:04 +00:00
|
|
|
from redis import Redis
|
2021-01-10 13:10:39 +00:00
|
|
|
from slixmpp import ClientXMPP
|
|
|
|
|
|
|
|
|
2021-01-13 13:00:59 +00:00
|
|
|
class SimpleMessage:
|
2021-01-10 13:49:22 +00:00
|
|
|
"""A simple message interface."""
|
|
|
|
|
2021-01-16 15:38:12 +00:00
|
|
|
def __init__(self, message, bot):
|
|
|
|
"""Initialise the object."""
|
2021-01-10 13:49:22 +00:00
|
|
|
self.message = message
|
2021-01-16 15:38:12 +00:00
|
|
|
self.bot = bot
|
2021-01-10 13:49:22 +00:00
|
|
|
|
|
|
|
@property
|
2021-01-16 15:38:12 +00:00
|
|
|
def text(self):
|
|
|
|
"""The entire message text."""
|
2021-01-10 13:49:22 +00:00
|
|
|
return self.message["body"]
|
|
|
|
|
2021-01-16 15:38:12 +00:00
|
|
|
@property
|
|
|
|
def content(self):
|
|
|
|
"""The content of the message received.
|
|
|
|
|
|
|
|
This implementation aims to match and extract the content of the
|
|
|
|
messages directed at bots in group chats. So, for example, when sending
|
|
|
|
messages like so.
|
|
|
|
|
|
|
|
echobot: hi
|
|
|
|
echobot, hi
|
|
|
|
echobot hi
|
|
|
|
|
|
|
|
The result produced by `message.content` will always be "hi". This
|
|
|
|
makes it easier to work with various commands and avoid messy parsing
|
|
|
|
logic in end-user implementations.
|
|
|
|
"""
|
|
|
|
body = self.message["body"]
|
|
|
|
|
|
|
|
try:
|
2021-01-16 16:46:50 +00:00
|
|
|
match = fr"^{self.bot.nick}.?(\s)"
|
2021-01-16 15:38:12 +00:00
|
|
|
split = re.split(match, body)
|
|
|
|
filtered = list(filter(None, split))
|
|
|
|
return filtered[-1].strip()
|
|
|
|
except Exception as exception:
|
|
|
|
self.bot.log.error(f"Couldn't parse {body}: {exception}")
|
|
|
|
return None
|
|
|
|
|
2021-01-10 13:49:22 +00:00
|
|
|
@property
|
2021-01-13 13:51:23 +00:00
|
|
|
def sender(self):
|
2021-01-16 15:38:12 +00:00
|
|
|
"""The sender of the message."""
|
2021-01-13 13:42:26 +00:00
|
|
|
return self.message["from"]
|
|
|
|
|
|
|
|
@property
|
|
|
|
def room(self):
|
2021-01-16 15:38:12 +00:00
|
|
|
"""The room from which the message originated."""
|
2021-01-10 15:51:20 +00:00
|
|
|
return self.message["from"].bare
|
2021-01-10 13:49:22 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def receiver(self):
|
2021-01-16 15:38:12 +00:00
|
|
|
"""The receiver of the message."""
|
2021-01-10 13:49:22 +00:00
|
|
|
return self.message["to"]
|
|
|
|
|
2021-01-10 15:31:17 +00:00
|
|
|
@property
|
|
|
|
def type(self):
|
2021-01-16 15:38:12 +00:00
|
|
|
"""The type of the message."""
|
2021-01-10 15:31:17 +00:00
|
|
|
return self.message["type"]
|
|
|
|
|
2021-01-16 15:38:12 +00:00
|
|
|
@property
|
|
|
|
def nick(self):
|
|
|
|
"""The nick of the message."""
|
|
|
|
return self.message["mucnick"]
|
|
|
|
|
2021-01-10 13:49:22 +00:00
|
|
|
|
2021-01-14 21:26:13 +00:00
|
|
|
class Config:
|
|
|
|
"""Bot file configuration."""
|
|
|
|
|
2021-01-14 21:36:03 +00:00
|
|
|
def __init__(self, name, config):
|
2021-01-16 15:38:12 +00:00
|
|
|
"""Initialise the object."""
|
2021-01-14 21:36:03 +00:00
|
|
|
self.name = name
|
2021-01-14 21:26:13 +00:00
|
|
|
self.config = config
|
2021-01-14 21:36:03 +00:00
|
|
|
self.section = config[self.name] if self.name in config else {}
|
2021-01-14 21:26:13 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def account(self):
|
2021-01-16 15:38:12 +00:00
|
|
|
"""The account of the bot."""
|
2021-01-14 21:26:13 +00:00
|
|
|
return self.section.get("account", None)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def password(self):
|
2021-01-16 15:38:12 +00:00
|
|
|
"""The password of the bot account."""
|
2021-01-14 21:26:13 +00:00
|
|
|
return self.section.get("password", None)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def nick(self):
|
2021-01-16 15:38:12 +00:00
|
|
|
"""The nickname of the bot."""
|
2021-01-14 21:26:13 +00:00
|
|
|
return self.section.get("nick", None)
|
|
|
|
|
2021-01-16 19:30:08 +00:00
|
|
|
@property
|
|
|
|
def avatar(self):
|
|
|
|
"""The avatar of the bot."""
|
|
|
|
return self.section.get("avatar", None)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def redis_url(self):
|
|
|
|
"""The Redis connection URL."""
|
|
|
|
return self.section.get("redis_url", None)
|
|
|
|
|
2021-01-16 20:16:22 +00:00
|
|
|
@property
|
|
|
|
def rooms(self):
|
|
|
|
"""A list of rooms to automatically join."""
|
|
|
|
rooms = self.section.get("rooms", None)
|
2021-01-16 20:46:22 +00:00
|
|
|
|
|
|
|
if rooms is None:
|
|
|
|
return None
|
|
|
|
|
2021-01-16 20:16:22 +00:00
|
|
|
return rooms.split(",")
|
|
|
|
|
2021-01-16 20:46:22 +00:00
|
|
|
@property
|
|
|
|
def no_auto_join(self):
|
|
|
|
"""Disable auto-join when invited."""
|
|
|
|
return self.section.get("no_auto_join", None)
|
|
|
|
|
2021-01-14 21:26:13 +00:00
|
|
|
|
2021-01-10 13:10:39 +00:00
|
|
|
class Bot(ClientXMPP):
|
2021-01-14 21:26:13 +00:00
|
|
|
"""XMPP bots for humans."""
|
|
|
|
|
2021-01-16 15:38:12 +00:00
|
|
|
DIRECT_MESSAGE_TYPES = ("chat", "normal")
|
|
|
|
GROUP_MESSAGE_TYPES = ("groupchat", "normal")
|
|
|
|
|
2021-01-10 13:10:39 +00:00
|
|
|
def __init__(self):
|
2021-01-16 15:38:12 +00:00
|
|
|
"""Initialise the object."""
|
2021-01-14 21:36:03 +00:00
|
|
|
self.name = type(self).__name__.lower()
|
2021-01-14 22:45:23 +00:00
|
|
|
self.start = dt.now()
|
|
|
|
|
2021-01-14 21:36:03 +00:00
|
|
|
self.CONFIG_FILE = f"{self.name}.conf"
|
|
|
|
|
2021-01-12 20:39:50 +00:00
|
|
|
self.parse_arguments()
|
2021-01-13 22:19:06 +00:00
|
|
|
self.setup_logging()
|
2021-01-10 13:10:39 +00:00
|
|
|
self.read_config()
|
|
|
|
self.init_bot()
|
|
|
|
self.register_xmpp_event_handlers()
|
|
|
|
self.register_xmpp_plugins()
|
2021-01-15 20:14:04 +00:00
|
|
|
self.init_db()
|
2021-01-10 13:10:39 +00:00
|
|
|
self.run()
|
|
|
|
|
2021-01-12 20:39:50 +00:00
|
|
|
def parse_arguments(self):
|
|
|
|
"""Parse command-line arguments."""
|
2021-01-14 21:26:13 +00:00
|
|
|
self.parser = ArgumentParser(description="XMPP bots for humans")
|
|
|
|
|
2021-01-13 22:19:06 +00:00
|
|
|
self.parser.add_argument(
|
|
|
|
"-d",
|
|
|
|
"--debug",
|
2021-01-14 21:26:13 +00:00
|
|
|
help="Enable verbose debug logs",
|
2021-01-13 22:19:06 +00:00
|
|
|
action="store_const",
|
|
|
|
dest="log_level",
|
|
|
|
const=DEBUG,
|
|
|
|
default=INFO,
|
|
|
|
)
|
2021-01-14 21:26:13 +00:00
|
|
|
self.parser.add_argument(
|
|
|
|
"-a",
|
|
|
|
"--account",
|
|
|
|
dest="account",
|
2021-01-16 21:26:22 +00:00
|
|
|
help="Account for the bot account",
|
2021-01-14 21:26:13 +00:00
|
|
|
)
|
|
|
|
self.parser.add_argument(
|
|
|
|
"-p",
|
|
|
|
"--password",
|
|
|
|
dest="password",
|
|
|
|
help="Password for the bot account",
|
|
|
|
)
|
|
|
|
self.parser.add_argument(
|
|
|
|
"-n",
|
|
|
|
"--nick",
|
|
|
|
dest="nick",
|
|
|
|
help="Nickname for the bot account",
|
|
|
|
)
|
2021-01-15 10:08:07 +00:00
|
|
|
self.parser.add_argument(
|
2021-01-16 19:24:03 +00:00
|
|
|
"-av", "--avatar", dest="avatar", help="Avatar for the bot account"
|
2021-01-15 10:08:07 +00:00
|
|
|
)
|
2021-01-16 19:21:22 +00:00
|
|
|
self.parser.add_argument(
|
2021-01-16 20:04:16 +00:00
|
|
|
"-ru",
|
2021-01-16 19:21:22 +00:00
|
|
|
"--redis-url",
|
|
|
|
dest="redis_url",
|
|
|
|
help="Redis storage connection URL",
|
|
|
|
)
|
2021-01-16 20:16:22 +00:00
|
|
|
self.parser.add_argument(
|
|
|
|
"-r",
|
|
|
|
"--rooms",
|
|
|
|
dest="rooms",
|
|
|
|
nargs="+",
|
|
|
|
help="Rooms to automatically join",
|
|
|
|
)
|
2021-01-16 20:46:22 +00:00
|
|
|
self.parser.add_argument(
|
|
|
|
"--no-auto-join",
|
|
|
|
default=False,
|
|
|
|
action="store_true",
|
|
|
|
dest="no_auto_join",
|
|
|
|
help="Disable automatically joining rooms when invited",
|
|
|
|
)
|
2021-01-14 21:26:13 +00:00
|
|
|
|
2021-01-12 20:39:50 +00:00
|
|
|
self.args = self.parser.parse_args()
|
|
|
|
|
2021-01-13 22:19:06 +00:00
|
|
|
def setup_logging(self):
|
|
|
|
"""Arrange logging for the bot."""
|
|
|
|
basicConfig(
|
|
|
|
level=self.args.log_level, format="%(levelname)-8s %(message)s"
|
|
|
|
)
|
2021-01-14 18:18:27 +00:00
|
|
|
self.log = getLogger(__name__)
|
2021-01-13 22:19:06 +00:00
|
|
|
|
2021-01-10 13:10:39 +00:00
|
|
|
def read_config(self):
|
|
|
|
"""Read configuration for running bot."""
|
2021-01-14 21:26:13 +00:00
|
|
|
config = ConfigParser()
|
2021-01-12 20:39:50 +00:00
|
|
|
|
2021-01-10 13:10:39 +00:00
|
|
|
config_file_path = Path(self.CONFIG_FILE).absolute()
|
2021-01-14 21:26:13 +00:00
|
|
|
|
|
|
|
if not exists(config_file_path) and stdout.isatty():
|
2021-01-15 13:32:01 +00:00
|
|
|
self.log.info(f"Did not find {config_file_path}")
|
2021-01-12 20:39:50 +00:00
|
|
|
self.generate_config_interactively()
|
2021-01-10 13:10:39 +00:00
|
|
|
|
2021-01-12 20:39:50 +00:00
|
|
|
if exists(config_file_path):
|
2021-01-14 21:26:13 +00:00
|
|
|
config.read(config_file_path)
|
2021-01-10 13:10:39 +00:00
|
|
|
|
2021-01-14 21:36:03 +00:00
|
|
|
self.config = Config(self.name, config)
|
2021-01-10 13:10:39 +00:00
|
|
|
|
2021-01-12 20:39:50 +00:00
|
|
|
def generate_config_interactively(self):
|
2021-01-10 13:10:39 +00:00
|
|
|
"""Generate bot configuration."""
|
2021-01-16 21:06:01 +00:00
|
|
|
print("*" * 79)
|
|
|
|
|
|
|
|
print(
|
|
|
|
"Please enter the XMPP user account of your bot. ",
|
|
|
|
"This is often referred to as the JID (Jabbed ID). ",
|
|
|
|
"An example would be echobot@vvvvvvaria.org",
|
|
|
|
sep="\n",
|
|
|
|
)
|
2021-01-14 21:26:13 +00:00
|
|
|
account = input("Account: ")
|
2021-01-16 21:06:01 +00:00
|
|
|
|
|
|
|
print("Please enter the password for your bot account")
|
2021-01-14 21:26:13 +00:00
|
|
|
password = getpass("Password: ")
|
2021-01-16 21:06:01 +00:00
|
|
|
|
|
|
|
print("Please enter the nickname for your bot account")
|
2021-01-14 21:26:13 +00:00
|
|
|
nick = input("Nickname: ")
|
2021-01-16 21:06:01 +00:00
|
|
|
|
|
|
|
print(
|
|
|
|
"Please enter the path to your avatar file",
|
|
|
|
"The default is an avatar.png file in the current directory",
|
|
|
|
"(leave empty if you want the default or have no avatar)",
|
|
|
|
sep="\n",
|
|
|
|
)
|
2021-01-16 18:59:09 +00:00
|
|
|
avatar = input("Avatar: ")
|
2021-01-16 21:06:01 +00:00
|
|
|
|
|
|
|
print(
|
|
|
|
"Please enter the connection URL of your Redis instance",
|
|
|
|
"(leave empty if you are not using Redis)",
|
|
|
|
sep="\n",
|
|
|
|
)
|
2021-01-16 19:21:22 +00:00
|
|
|
redis_url = input("Redis URL: ")
|
2021-01-16 21:06:01 +00:00
|
|
|
|
|
|
|
print(
|
|
|
|
"Please supply a list of rooms to automatically join",
|
|
|
|
"for example, foo@muc.vvvaria.org, bar@muc.vvvvvvaria.org",
|
|
|
|
"(leave empty if you don't want to join rooms when starting)",
|
|
|
|
sep="\n",
|
|
|
|
)
|
2021-01-16 20:46:22 +00:00
|
|
|
rooms = input("Rooms: ")
|
2021-01-16 21:06:01 +00:00
|
|
|
|
|
|
|
print(
|
|
|
|
"Please choose to disable auto-join on invite if you prefer",
|
|
|
|
"that your bot does not join after being invited to a room",
|
|
|
|
"(type 'y' to choose to disable)",
|
|
|
|
sep="\n",
|
|
|
|
)
|
2021-01-16 20:46:22 +00:00
|
|
|
no_auto_join = input("Disable auto-join on invite? ")
|
2021-01-10 13:10:39 +00:00
|
|
|
|
2021-01-16 21:06:01 +00:00
|
|
|
print("*" * 79)
|
|
|
|
|
2021-01-10 13:10:39 +00:00
|
|
|
config = ConfigParser()
|
2021-01-14 21:36:03 +00:00
|
|
|
config[self.name] = {"account": account, "password": password}
|
2021-01-10 13:10:39 +00:00
|
|
|
|
2021-01-10 14:45:12 +00:00
|
|
|
if nick:
|
2021-01-14 21:36:03 +00:00
|
|
|
config[self.name]["nick"] = nick
|
2021-01-16 18:59:09 +00:00
|
|
|
if avatar:
|
|
|
|
config[self.name]["avatar"] = avatar
|
2021-01-16 19:21:22 +00:00
|
|
|
if redis_url:
|
|
|
|
config[self.name]["redis_url"] = redis_url
|
2021-01-16 20:16:22 +00:00
|
|
|
if rooms:
|
|
|
|
config[self.name]["rooms"] = rooms
|
2021-01-16 20:46:22 +00:00
|
|
|
if no_auto_join:
|
2021-01-16 21:06:01 +00:00
|
|
|
config[self.name]["auto_join"] = (
|
|
|
|
True if no_auto_join == "y" else False
|
|
|
|
)
|
2021-01-16 18:59:09 +00:00
|
|
|
|
2021-01-14 21:36:03 +00:00
|
|
|
with open(self.CONFIG_FILE, "w") as file_handle:
|
2021-01-10 13:10:39 +00:00
|
|
|
config.write(file_handle)
|
2021-01-16 21:06:01 +00:00
|
|
|
self.log.info(f"Generated {self.CONFIG_FILE}")
|
2021-01-10 13:10:39 +00:00
|
|
|
|
|
|
|
def init_bot(self):
|
|
|
|
"""Initialise bot with connection details."""
|
2021-01-14 21:26:13 +00:00
|
|
|
account = (
|
|
|
|
self.args.account
|
|
|
|
or self.config.account
|
|
|
|
or environ.get("XBOT_ACCOUNT", None)
|
|
|
|
)
|
|
|
|
password = (
|
|
|
|
self.args.password
|
|
|
|
or self.config.password
|
|
|
|
or environ.get("XBOT_PASSWORD", None)
|
|
|
|
)
|
|
|
|
nick = (
|
|
|
|
self.args.nick or self.config.nick or environ.get("XBOT_NICK", None)
|
|
|
|
)
|
2021-01-16 18:59:09 +00:00
|
|
|
avatar = (
|
|
|
|
self.args.avatar
|
|
|
|
or self.config.avatar
|
|
|
|
or environ.get("XBOT_AVATAR", None)
|
2021-01-16 19:24:03 +00:00
|
|
|
or "avatar.png"
|
2021-01-16 18:59:09 +00:00
|
|
|
)
|
2021-01-16 19:21:22 +00:00
|
|
|
redis_url = (
|
|
|
|
self.args.redis_url
|
|
|
|
or self.config.redis_url
|
|
|
|
or environ.get("XBOT_REDIS_URL", None)
|
|
|
|
)
|
2021-01-16 20:16:22 +00:00
|
|
|
rooms = (
|
|
|
|
self.args.rooms
|
|
|
|
or self.config.rooms
|
|
|
|
or environ.get("XBOT_ROOMS", None)
|
|
|
|
)
|
2021-01-16 20:46:22 +00:00
|
|
|
no_auto_join = (
|
|
|
|
self.args.no_auto_join
|
|
|
|
or self.config.no_auto_join
|
|
|
|
or environ.get("XBOT_NO_AUTO_JOIN", None)
|
|
|
|
)
|
2021-01-14 21:26:13 +00:00
|
|
|
|
|
|
|
if not account:
|
|
|
|
self.log.error("Unable to discover account")
|
|
|
|
exit(1)
|
|
|
|
if not password:
|
|
|
|
self.log.error("Unable to discover password")
|
|
|
|
exit(1)
|
|
|
|
if not nick:
|
|
|
|
self.log.error("Unable to discover nick")
|
|
|
|
exit(1)
|
|
|
|
|
|
|
|
ClientXMPP.__init__(self, account, password)
|
|
|
|
|
|
|
|
self.account = account
|
|
|
|
self.password = password
|
|
|
|
self.nick = nick
|
2021-01-16 18:59:09 +00:00
|
|
|
self.avatar = avatar
|
2021-01-16 19:21:22 +00:00
|
|
|
self.redis_url = redis_url
|
2021-01-16 20:16:22 +00:00
|
|
|
self.rooms = rooms
|
2021-01-16 20:46:22 +00:00
|
|
|
self.no_auto_join = no_auto_join
|
2021-01-15 10:08:07 +00:00
|
|
|
|
2021-01-10 13:10:39 +00:00
|
|
|
def register_xmpp_event_handlers(self):
|
|
|
|
"""Register functions against specific XMPP event handlers."""
|
|
|
|
self.add_event_handler("session_start", self.session_start)
|
2021-01-13 21:49:52 +00:00
|
|
|
self.add_event_handler("groupchat_invite", self.group_invite)
|
2021-01-13 20:56:45 +00:00
|
|
|
self.add_event_handler("message", self.direct_message)
|
|
|
|
self.add_event_handler("groupchat_message", self.group_message)
|
2021-01-15 11:57:25 +00:00
|
|
|
self.add_event_handler("message_error", self.error_message)
|
|
|
|
|
|
|
|
def error_message(self, message):
|
2021-01-16 15:38:12 +00:00
|
|
|
message = SimpleMessage(message, self)
|
|
|
|
self.log.error(f"Received error message: {message.text}")
|
2021-01-10 13:10:39 +00:00
|
|
|
|
2021-01-13 20:56:45 +00:00
|
|
|
def direct_message(self, message):
|
2021-01-16 15:38:12 +00:00
|
|
|
"""Handle direct message events."""
|
|
|
|
message = SimpleMessage(message, self)
|
2021-01-14 22:45:23 +00:00
|
|
|
|
2021-01-16 15:38:12 +00:00
|
|
|
if message.type not in self.DIRECT_MESSAGE_TYPES:
|
|
|
|
return
|
2021-01-14 22:45:23 +00:00
|
|
|
|
2021-01-16 15:38:12 +00:00
|
|
|
if message.text.startswith("@"):
|
2021-01-16 19:33:53 +00:00
|
|
|
if self.command(message, to=message.sender):
|
|
|
|
return
|
2021-01-16 12:09:41 +00:00
|
|
|
|
|
|
|
try:
|
2021-01-16 15:38:12 +00:00
|
|
|
self.direct(message)
|
2021-01-16 12:09:41 +00:00
|
|
|
except AttributeError:
|
|
|
|
self.log.info(f"Bot.direct not implemented for {self.nick}")
|
2021-01-10 13:10:39 +00:00
|
|
|
|
2021-01-13 21:49:52 +00:00
|
|
|
def session_start(self, message):
|
2021-01-10 13:10:39 +00:00
|
|
|
"""Handle session_start event."""
|
|
|
|
self.send_presence()
|
|
|
|
self.get_roster()
|
2021-01-15 10:08:07 +00:00
|
|
|
self.publish_avatar()
|
2021-01-16 20:16:22 +00:00
|
|
|
self.join_rooms()
|
2021-01-15 10:08:07 +00:00
|
|
|
|
|
|
|
def publish_avatar(self):
|
|
|
|
"""Publish bot avatar."""
|
|
|
|
try:
|
|
|
|
abspath = Path(self.avatar).absolute()
|
|
|
|
with open(abspath, "rb") as handle:
|
|
|
|
contents = handle.read()
|
|
|
|
except IOError:
|
|
|
|
self.log.info(f"No avatar discovered (tried '{abspath}')")
|
|
|
|
return
|
|
|
|
|
|
|
|
id = self.plugin["xep_0084"].generate_id(contents)
|
|
|
|
info = {
|
|
|
|
"id": id,
|
|
|
|
"type": f"image/{what('', contents)}",
|
|
|
|
"bytes": len(contents),
|
|
|
|
}
|
|
|
|
|
|
|
|
self.plugin["xep_0084"].publish_avatar(contents)
|
|
|
|
self.plugin["xep_0084"].publish_avatar_metadata(items=[info])
|
2021-01-10 13:10:39 +00:00
|
|
|
|
2021-01-16 20:16:22 +00:00
|
|
|
def join_rooms(self):
|
|
|
|
"""Automatically join rooms if specified."""
|
2021-01-16 20:46:22 +00:00
|
|
|
if self.rooms is None:
|
|
|
|
return
|
|
|
|
|
2021-01-16 20:16:22 +00:00
|
|
|
for room in self.rooms:
|
|
|
|
self.plugin["xep_0045"].join_muc(room, self.config.nick)
|
|
|
|
self.log.info(f"Joining {room} automatically")
|
|
|
|
|
2021-01-13 21:49:52 +00:00
|
|
|
def group_invite(self, message):
|
|
|
|
"""Accept invites to group chats."""
|
2021-01-16 20:46:22 +00:00
|
|
|
room = message["from"]
|
|
|
|
|
|
|
|
if self.no_auto_join:
|
|
|
|
return self.log.info(f"Not joining {room} (disabled)")
|
|
|
|
|
|
|
|
self.plugin["xep_0045"].join_muc(room, self.config.nick)
|
|
|
|
self.log.info(f"Joining {room} as invited")
|
2021-01-10 15:31:17 +00:00
|
|
|
|
2021-01-13 20:56:45 +00:00
|
|
|
def group_message(self, message):
|
2021-01-16 15:38:12 +00:00
|
|
|
"""Handle group chat message events."""
|
|
|
|
message = SimpleMessage(message, self)
|
2021-01-14 22:45:23 +00:00
|
|
|
|
2021-01-16 15:38:12 +00:00
|
|
|
if message.text.startswith("@"):
|
|
|
|
return self.meta(message, room=message.room)
|
2021-01-16 12:09:41 +00:00
|
|
|
|
2021-01-16 15:38:12 +00:00
|
|
|
miss = message.type not in self.GROUP_MESSAGE_TYPES
|
|
|
|
loop = message.nick == self.nick
|
|
|
|
other = self.nick not in message.text
|
2021-01-16 12:17:55 +00:00
|
|
|
|
2021-01-16 15:38:12 +00:00
|
|
|
if miss or loop or other:
|
|
|
|
return
|
2021-01-15 14:05:49 +00:00
|
|
|
|
2021-01-16 15:38:12 +00:00
|
|
|
if message.content.startswith("@"):
|
2021-01-16 19:33:53 +00:00
|
|
|
if self.command(message, room=message.room):
|
|
|
|
return
|
2021-01-14 22:45:23 +00:00
|
|
|
|
2021-01-16 12:09:41 +00:00
|
|
|
try:
|
2021-01-16 15:38:12 +00:00
|
|
|
self.group(message)
|
2021-01-16 12:09:41 +00:00
|
|
|
except AttributeError:
|
|
|
|
self.log.info(f"Bot.group not implemented for {self.nick}")
|
2021-01-10 13:10:39 +00:00
|
|
|
|
|
|
|
def register_xmpp_plugins(self):
|
|
|
|
"""Register XMPP plugins that the bot supports."""
|
|
|
|
self.register_plugin("xep_0030") # Service Discovery
|
|
|
|
self.register_plugin("xep_0045") # Multi-User Chat
|
|
|
|
self.register_plugin("xep_0199") # XMPP Ping
|
2021-01-15 10:08:07 +00:00
|
|
|
self.register_plugin("xep_0084") # User Avatar
|
|
|
|
|
2021-01-16 19:37:25 +00:00
|
|
|
try:
|
|
|
|
for plugin in self.plugins:
|
|
|
|
self.register_plugin(plugin)
|
2021-01-16 20:48:33 +00:00
|
|
|
self.log.info(f"Loaded {plugin}")
|
2021-01-16 19:37:25 +00:00
|
|
|
except AttributeError:
|
|
|
|
self.log.info("No additional plugins loaded")
|
|
|
|
|
2021-01-15 20:14:04 +00:00
|
|
|
def init_db(self):
|
|
|
|
"""Initialise the Redis key/value store."""
|
2021-01-16 19:21:22 +00:00
|
|
|
if not self.redis_url:
|
2021-01-15 20:14:04 +00:00
|
|
|
self.db = None
|
2021-01-16 20:16:22 +00:00
|
|
|
return self.log.info("No Redis storage discovered")
|
2021-01-16 19:21:22 +00:00
|
|
|
|
2021-01-16 20:16:22 +00:00
|
|
|
try:
|
|
|
|
self.db = Redis.from_url(self.redis_url, decode_responses=True)
|
|
|
|
self.log.info("Successfully connected to Redis storage")
|
|
|
|
except ValueError:
|
|
|
|
self.log.info("Failed to connect to Redis storage")
|
2021-01-15 20:14:04 +00:00
|
|
|
|
2021-01-10 13:10:39 +00:00
|
|
|
def run(self):
|
|
|
|
"""Run the bot."""
|
|
|
|
self.connect()
|
2021-01-10 14:37:10 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
self.process()
|
|
|
|
except KeyboardInterrupt:
|
|
|
|
pass
|
2021-01-10 13:49:22 +00:00
|
|
|
|
2021-01-16 15:38:12 +00:00
|
|
|
def reply(self, text, to=None, room=None):
|
2021-01-10 18:17:10 +00:00
|
|
|
"""Send back a reply."""
|
|
|
|
if to is None and room is None:
|
2021-01-16 11:50:29 +00:00
|
|
|
self.log.error("`to` or `room` arguments required for `reply`")
|
2021-01-14 21:44:48 +00:00
|
|
|
exit(1)
|
2021-01-10 18:17:10 +00:00
|
|
|
|
|
|
|
if to is not None and room is not None:
|
2021-01-14 21:44:48 +00:00
|
|
|
self.log.error("Cannot send to both `to` and `room` for `reply`")
|
|
|
|
exit(1)
|
2021-01-10 18:17:10 +00:00
|
|
|
|
2021-01-16 15:38:12 +00:00
|
|
|
kwargs = {"mbody": text}
|
2021-01-10 18:17:10 +00:00
|
|
|
if to is not None:
|
|
|
|
kwargs["mto"] = to
|
|
|
|
kwargs["mtype"] = "chat"
|
|
|
|
else:
|
|
|
|
kwargs["mto"] = room
|
|
|
|
kwargs["mtype"] = "groupchat"
|
|
|
|
|
|
|
|
self.send_message(**kwargs)
|
2021-01-16 19:33:53 +00:00
|
|
|
return True
|
2021-01-10 18:29:34 +00:00
|
|
|
|
2021-01-14 22:45:23 +00:00
|
|
|
@property
|
|
|
|
def uptime(self):
|
|
|
|
"""Time since the bot came up."""
|
|
|
|
return naturaldelta(self.start - dt.now())
|
|
|
|
|
2021-01-16 15:38:12 +00:00
|
|
|
def meta(self, message, **kwargs):
|
|
|
|
"""Handle meta command invocations."""
|
|
|
|
if message.text.startswith("@bots"):
|
2021-01-16 19:33:53 +00:00
|
|
|
return self.reply("🖐️", **kwargs)
|
2021-01-16 15:38:12 +00:00
|
|
|
|
2021-01-16 12:14:42 +00:00
|
|
|
def command(self, message, **kwargs):
|
|
|
|
"""Handle command invocations."""
|
2021-01-16 15:38:12 +00:00
|
|
|
if message.content.startswith("@uptime"):
|
2021-01-16 19:33:53 +00:00
|
|
|
return self.reply(self.uptime, **kwargs)
|
2021-01-16 15:38:12 +00:00
|
|
|
elif message.content.startswith("@help"):
|
2021-01-14 22:45:23 +00:00
|
|
|
try:
|
2021-01-16 19:33:53 +00:00
|
|
|
return self.reply(cleandoc(self.help), **kwargs)
|
2021-01-14 22:45:23 +00:00
|
|
|
except AttributeError:
|
2021-01-16 19:33:53 +00:00
|
|
|
return self.reply("No help found 🤔️", **kwargs)
|
2021-01-14 22:45:23 +00:00
|
|
|
|
2021-01-13 13:08:44 +00:00
|
|
|
|
|
|
|
class EchoBot(Bot):
|
2021-01-13 20:56:45 +00:00
|
|
|
"""Responds with whatever you send.
|
2021-01-13 13:08:44 +00:00
|
|
|
|
2021-01-13 20:56:45 +00:00
|
|
|
Simply direct message the bot and see if you get back what you sent. It
|
|
|
|
also works in group chats but in this case you need to summon the bot using
|
2021-01-15 20:14:38 +00:00
|
|
|
its nickname.
|
2021-01-13 13:08:44 +00:00
|
|
|
"""
|
|
|
|
|
2021-01-16 11:34:26 +00:00
|
|
|
help = "I echo messages back 🖖️"
|
2021-01-14 22:45:23 +00:00
|
|
|
|
2021-01-13 20:56:45 +00:00
|
|
|
def direct(self, message):
|
|
|
|
"""Send back whatever we receive."""
|
2021-01-16 15:38:12 +00:00
|
|
|
self.reply(message.text, to=message.sender)
|
2021-01-13 13:31:00 +00:00
|
|
|
|
2021-01-13 20:56:45 +00:00
|
|
|
def group(self, message):
|
|
|
|
"""Send back whatever receive in group chats."""
|
2021-01-16 16:31:22 +00:00
|
|
|
self.reply(message.content, room=message.room)
|
2021-01-13 13:08:44 +00:00
|
|
|
|
|
|
|
|
|
|
|
class WhisperBot(Bot):
|
2021-01-13 20:56:45 +00:00
|
|
|
"""Anonymous whispering in group chats.
|
2021-01-13 13:08:44 +00:00
|
|
|
|
|
|
|
In order to activate this bot you can invite it to your group chat. Once
|
2021-01-13 20:56:45 +00:00
|
|
|
invited, you can start a private chat with the bot and tell it you want it
|
|
|
|
to whisper your message into the group chat. The bot will then do this on
|
|
|
|
your behalf and not reveal your identity. This is nice when you want to
|
|
|
|
communicate with the group anonymously.
|
2021-01-13 13:08:44 +00:00
|
|
|
"""
|
|
|
|
|
2021-01-16 11:34:26 +00:00
|
|
|
help = "I whisper private messages into group chats 😌️"
|
2021-01-14 22:45:23 +00:00
|
|
|
|
2021-01-13 20:56:45 +00:00
|
|
|
def direct(self, message):
|
|
|
|
"""Receive private messages and whisper them into group chats."""
|
2021-01-16 15:38:12 +00:00
|
|
|
self.reply(f"*pssttt...* {message.content}", room=message.room)
|
2021-01-15 20:14:54 +00:00
|
|
|
|
|
|
|
|
|
|
|
class GlossBot(Bot):
|
|
|
|
"""Building a shared glossary together.
|
|
|
|
|
|
|
|
A glossary is "an alphabetical list of terms in a particular domain of
|
|
|
|
knowledge with the definitions for those terms."
|
|
|
|
|
|
|
|
This bot reacts to commands which insert, list or delete items from a
|
|
|
|
shared glossary when summoned in a group chat. This bot makes use of
|
|
|
|
persistent storage so the glossary is always there even if the bot goes
|
|
|
|
away.
|
|
|
|
"""
|
|
|
|
|
2021-01-16 12:05:30 +00:00
|
|
|
help = """
|
|
|
|
I help build a shared glossary
|
2021-01-16 11:34:26 +00:00
|
|
|
glossbot: @add <entry> - <definition>
|
|
|
|
glossbot: @rm <entry>
|
|
|
|
glossbot: @rand
|
|
|
|
glossbot: @ls
|
2021-01-15 20:14:54 +00:00
|
|
|
"""
|
|
|
|
|
2021-01-15 20:18:20 +00:00
|
|
|
def group(self, message):
|
2021-01-16 11:29:37 +00:00
|
|
|
"""Handle glossary commands."""
|
2021-01-16 16:30:28 +00:00
|
|
|
if "@add" in message.content:
|
2021-01-15 20:14:54 +00:00
|
|
|
try:
|
2021-01-16 16:30:28 +00:00
|
|
|
parsed = self.parse_add(message)
|
|
|
|
self.add(*parsed, room=message.room)
|
|
|
|
except Exception:
|
|
|
|
response = f"Couldn't understand '{message.content}'?"
|
|
|
|
self.reply(response, room=message.sender)
|
|
|
|
elif "@rm" in message.content:
|
2021-01-15 20:14:54 +00:00
|
|
|
try:
|
2021-01-16 16:30:28 +00:00
|
|
|
parsed = message.content.split("@rm")[-1].strip()
|
|
|
|
self.rm(parsed, room=message.room)
|
|
|
|
except Exception:
|
|
|
|
response = f"Couldn't understand '{message.content}'?"
|
|
|
|
self.reply(response, room=message.sender)
|
|
|
|
elif "@rand" in message.content:
|
2021-01-16 11:30:19 +00:00
|
|
|
self.rand(room=message.room)
|
2021-01-16 16:30:28 +00:00
|
|
|
elif "@ls" in message.content:
|
2021-01-16 11:30:19 +00:00
|
|
|
self.ls(room=message.room)
|
2021-01-15 20:14:54 +00:00
|
|
|
else:
|
2021-01-16 16:30:28 +00:00
|
|
|
self.log.info(f"{message.text} not recognised as glossbot command")
|
|
|
|
|
|
|
|
def parse_add(self, message):
|
|
|
|
"""Parse the add command syntax."""
|
|
|
|
try:
|
|
|
|
replaced = message.content.replace("@add", "")
|
|
|
|
return [s.strip() for s in replaced.split("-")]
|
|
|
|
except ValueError:
|
|
|
|
self.log.error(f"Failed to parse {message.content}")
|
2021-01-15 20:14:54 +00:00
|
|
|
|
|
|
|
def add(self, entry, definition, **kwargs):
|
|
|
|
"""Add a new entry."""
|
|
|
|
self.db[entry] = definition
|
|
|
|
self.reply("Added ✌️", **kwargs)
|
|
|
|
|
|
|
|
def rand(self, **kwargs):
|
|
|
|
"""List a random entry."""
|
2021-01-16 11:30:37 +00:00
|
|
|
if not self.db.keys():
|
2021-01-16 16:30:28 +00:00
|
|
|
return self.reply("Glossary is empty 🙃️", **kwargs)
|
2021-01-16 11:30:37 +00:00
|
|
|
|
2021-01-15 20:14:54 +00:00
|
|
|
entry = choice(self.db.keys())
|
|
|
|
self.reply(f"{entry} - {self.db[entry]}", **kwargs)
|
|
|
|
|
|
|
|
def ls(self, **kwargs):
|
|
|
|
"""List all entries."""
|
|
|
|
if not self.db.keys():
|
2021-01-16 16:30:28 +00:00
|
|
|
return self.reply("Glossary is empty 🙃️", **kwargs)
|
2021-01-15 20:14:54 +00:00
|
|
|
|
2021-01-16 16:30:28 +00:00
|
|
|
for entry in sorted(self.db.keys()):
|
2021-01-15 20:14:54 +00:00
|
|
|
self.reply(f"{entry} - {self.db[entry]}", **kwargs)
|
|
|
|
|
|
|
|
def rm(self, entry, **kwargs):
|
|
|
|
"""Remove an entry."""
|
2021-01-16 16:30:28 +00:00
|
|
|
if entry not in self.db.keys():
|
|
|
|
return self.reply(f"{entry} doesn't exist?", **kwargs)
|
|
|
|
|
|
|
|
self.db.delete(entry)
|
|
|
|
self.reply("Removed ✌️", **kwargs)
|