2020-09-11 05:31:20 +03:00
|
|
|
import os
|
|
|
|
import itertools
|
2020-09-15 06:35:47 +03:00
|
|
|
import shlex
|
2020-09-11 05:31:20 +03:00
|
|
|
import requests
|
|
|
|
from modules.common.module import BotModule
|
|
|
|
|
|
|
|
|
|
|
|
class MatrixModule(BotModule):
|
|
|
|
"""
|
|
|
|
Detect new users who join the server, DM them a welcome message, and alert
|
|
|
|
bot owners that a new user has been welcomed.
|
|
|
|
|
|
|
|
Note: This module will only work if the bot is a server admin. This
|
|
|
|
privilege level has risks.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self, name):
|
|
|
|
super().__init__(name)
|
2020-09-15 06:35:47 +03:00
|
|
|
self.enabled = False
|
2020-09-11 05:31:20 +03:00
|
|
|
self.access_token = os.getenv("MATRIX_ACCESS_TOKEN")
|
2020-09-15 06:35:47 +03:00
|
|
|
self.welcome_settings = dict()
|
2020-09-11 05:31:20 +03:00
|
|
|
|
|
|
|
async def matrix_message(self, bot, room, event):
|
2020-09-15 06:35:47 +03:00
|
|
|
bot.must_be_owner(event)
|
|
|
|
args = shlex.split(event.body)
|
|
|
|
args.pop(0)
|
|
|
|
# Message body possibilities:
|
|
|
|
# ["welcome_message", "query_host", "settings"]
|
|
|
|
if args[0] == "welcome_message":
|
2020-09-15 17:05:12 +03:00
|
|
|
users = self.get_server_user_list()
|
2020-09-15 06:35:47 +03:00
|
|
|
welcome_settings = {
|
|
|
|
"last_server_user_count": len(users),
|
2020-09-15 17:05:12 +03:00
|
|
|
"last_server_users": users,
|
2020-09-15 06:35:47 +03:00
|
|
|
"welcome_message": event.body.split("welcome_message", 1)[1],
|
|
|
|
"user_query_host": os.getenv("MATRIX_SERVER")
|
|
|
|
}
|
|
|
|
self.welcome_settings = welcome_settings
|
|
|
|
bot.save_settings()
|
|
|
|
await bot.send_text(room, "Welcome settings configured for server: {settings}".format(settings=welcome_settings))
|
|
|
|
elif args[0] == "settings":
|
|
|
|
await bot.send_text(room, "Welcome settings for server: {settings}".format(settings=self.welcome_settings))
|
|
|
|
|
|
|
|
def get_settings(self):
|
|
|
|
data = super().get_settings()
|
|
|
|
data["welcome_settings"] = self.welcome_settings
|
|
|
|
return data
|
|
|
|
|
|
|
|
def set_settings(self, data):
|
|
|
|
super().set_settings(data)
|
|
|
|
if data.get("welcome_settings"):
|
|
|
|
self.welcome_settings = data["welcome_settings"]
|
2020-09-11 05:31:20 +03:00
|
|
|
|
|
|
|
async def matrix_poll(self, bot, pollcount):
|
2020-09-15 17:05:12 +03:00
|
|
|
server_user_delta = self.get_server_user_delta(bot)
|
2020-09-11 05:31:20 +03:00
|
|
|
|
|
|
|
# The first time this bot runs it will detect all users as new, so
|
|
|
|
# allow it to one once without taking action.
|
|
|
|
if pollcount != 1:
|
2020-09-15 17:05:12 +03:00
|
|
|
new_users = server_user_delta.get("recently_added", [])
|
2020-09-15 06:35:47 +03:00
|
|
|
await self.welcome_users(
|
|
|
|
new_users,
|
|
|
|
self.welcome_settings["welcome_message"],
|
|
|
|
bot
|
|
|
|
)
|
2020-09-11 05:31:20 +03:00
|
|
|
|
|
|
|
def help(self):
|
|
|
|
return "Poll for new users on the server and welcome them"
|
|
|
|
|
2020-09-15 06:35:47 +03:00
|
|
|
async def welcome_users(self, user_list, message, bot):
|
2020-09-15 17:05:12 +03:00
|
|
|
if len(user_list) > 1:
|
|
|
|
print(user_list)
|
|
|
|
return
|
2020-09-11 05:31:20 +03:00
|
|
|
for user in user_list:
|
|
|
|
await bot.send_msg(
|
|
|
|
user,
|
|
|
|
"Welcome",
|
2020-09-15 06:35:47 +03:00
|
|
|
message
|
2020-09-11 05:31:20 +03:00
|
|
|
)
|
2020-09-11 06:30:16 +03:00
|
|
|
if len(user_list) > 0:
|
|
|
|
for owner in bot.owners:
|
|
|
|
await bot.send_msg(
|
|
|
|
owner,
|
|
|
|
"Welcome Bot",
|
2020-09-15 06:35:47 +03:00
|
|
|
"Sent a welcome message to new server user(s): {users}".format(
|
|
|
|
users=user_list
|
2020-09-11 06:30:16 +03:00
|
|
|
)
|
2020-09-11 05:31:20 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
def get_user_list_delta(
|
|
|
|
self,
|
|
|
|
current_user_list,
|
|
|
|
previous_user_list
|
|
|
|
):
|
|
|
|
recently_added = list(itertools.filterfalse(
|
|
|
|
lambda u: u in previous_user_list,
|
|
|
|
current_user_list
|
|
|
|
))
|
|
|
|
recently_removed = list(itertools.filterfalse(
|
|
|
|
lambda u: u in current_user_list,
|
|
|
|
previous_user_list
|
|
|
|
))
|
|
|
|
total_change = len(recently_added) + len(recently_removed)
|
|
|
|
|
|
|
|
return {
|
|
|
|
"total_change": total_change,
|
|
|
|
"recently_removed": recently_removed,
|
|
|
|
"recently_added": recently_added
|
|
|
|
}
|
|
|
|
|
2020-09-15 17:05:12 +03:00
|
|
|
def get_server_user_delta(self, bot):
|
2020-09-11 05:31:20 +03:00
|
|
|
"""
|
|
|
|
Get the full user list for the server and return the change in users
|
|
|
|
since the last run.
|
|
|
|
"""
|
2020-09-15 17:05:12 +03:00
|
|
|
user_list = self.get_server_user_list()
|
|
|
|
user_delta = self.get_user_list_delta(
|
|
|
|
user_list,
|
|
|
|
self.welcome_settings["last_server_users"]
|
|
|
|
)
|
|
|
|
self.welcome_settings["last_server_users"] = [u for u in user_list]
|
|
|
|
self.welcome_settings["last_server_user_count"] = len(user_list)
|
|
|
|
bot.save_settings()
|
|
|
|
return user_delta
|
|
|
|
|
|
|
|
def get_server_user_list(self):
|
2020-09-11 05:31:20 +03:00
|
|
|
user_data = requests.get(
|
2020-09-15 06:35:47 +03:00
|
|
|
self.welcome_settings["user_query_host"] + "/_synapse/admin/v2/users",
|
2020-09-11 05:31:20 +03:00
|
|
|
headers={"Authorization": "Bearer {token}".format(
|
|
|
|
token=self.access_token
|
|
|
|
)}
|
|
|
|
)
|
|
|
|
user_data_json = user_data.json()
|
2020-09-15 17:05:12 +03:00
|
|
|
user_list = [u.get("name") for u in user_data_json.get("users", [])]
|
|
|
|
return user_list
|