Merge branch 'master' of github.com:vranki/hemppa into sync-upstream

This commit is contained in:
Frank Becker 2020-02-09 19:07:06 +01:00
commit 8611d8cd2f
8 changed files with 48 additions and 20 deletions

View File

@ -25,6 +25,9 @@ Bot management commands.
* !bot reload - reload all bot modules (Must be done as bot owner) * !bot reload - reload all bot modules (Must be done as bot owner)
* !bot stats - show statistics on matrix users seen by bot * !bot stats - show statistics on matrix users seen by bot
* !bot leave - ask bot to leave this room (Must be done as admin in room) * !bot leave - ask bot to leave this room (Must be done as admin in room)
* !bot modules - list all modules including enabled status
* !bot enable [module] - enable module (Must be done as admin in room)
* !bot disable [module] - disable module (Must be done as admin in room)
### Help ### Help

9
bot.py
View File

@ -75,6 +75,12 @@ class Bot:
} }
await self.client.room_send(room.room_id, 'm.room.message', msg) await self.client.room_send(room.room_id, 'm.room.message', msg)
def remove_callback(self, callback):
for cb_object in bot.client.event_callbacks:
if cb_object.func == callback:
self.logger.info("remove callback")
bot.client.event_callbacks.remove(cb_object)
def get_room_by_id(self, room_id): def get_room_by_id(self, room_id):
return self.client.rooms[room_id] return self.client.rooms[room_id]
@ -148,7 +154,8 @@ class Bot:
except CommandRequiresOwner: except CommandRequiresOwner:
await self.send_text(room, f'Sorry, only bot owner can run that command.') await self.send_text(room, f'Sorry, only bot owner can run that command.')
except Exception: except Exception:
await self.send_text(room, f'Module {command} experienced difficulty: {sys.exc_info()[0]} - see log for details') await self.send_text(room,
f'Module {command} experienced difficulty: {sys.exc_info()[0]} - see log for details')
traceback.print_exc(file=sys.stderr) traceback.print_exc(file=sys.stderr)
else: else:
self.logger.error(f"Unknown command: {command}") self.logger.error(f"Unknown command: {command}")

View File

@ -105,9 +105,12 @@ class PollingService(BotModule):
await bot.send_text(room, f'Removed {self.service_name} account from this room') await bot.send_text(room, f'Removed {self.service_name} account from this room')
def get_settings(self): def get_settings(self):
return {'account_rooms': self.account_rooms} data = super().get_settings()
data['account_rooms'] = self.account_rooms
return data
def set_settings(self, data): def set_settings(self, data):
super().set_settings(data)
if data.get('account_rooms'): if data.get('account_rooms'):
self.account_rooms = data['account_rooms'] self.account_rooms = data['account_rooms']

View File

@ -1,5 +1,6 @@
from geopy.geocoders import Nominatim from geopy.geocoders import Nominatim
from nio import RoomMessageUnknown from nio import RoomMessageUnknown, AsyncClient
from modules.common.module import BotModule
from modules.common.module import BotModule from modules.common.module import BotModule
@ -12,6 +13,10 @@ class MatrixModule(BotModule):
self.bot = bot self.bot = bot
bot.client.add_event_callback(self.unknown_cb, RoomMessageUnknown) bot.client.add_event_callback(self.unknown_cb, RoomMessageUnknown)
def matrix_stop(self, bot):
super().matrix_stop(bot)
bot.remove_callback(self.unknown_cb)
async def unknown_cb(self, room, event): async def unknown_cb(self, room, event):
if event.msgtype != 'm.location': if event.msgtype != 'm.location':
return return

View File

@ -20,14 +20,17 @@ from modules.common.module import BotModule
class MatrixModule(BotModule): class MatrixModule(BotModule):
def matrix_start(self, bot): def __init__(self, name):
super().matrix_start(bot) super().__init__(name)
self.bot = bot
self.SCOPES = ['https://www.googleapis.com/auth/calendar.readonly']
self.credentials_file = "credentials.json" self.credentials_file = "credentials.json"
self.SCOPES = ['https://www.googleapis.com/auth/calendar.readonly']
self.bot = None
self.service = None self.service = None
self.calendar_rooms = dict() # Contains room_id -> [calid, calid] .. self.calendar_rooms = dict() # Contains room_id -> [calid, calid] ..
def matrix_start(self, bot):
super().matrix_start(bot)
self.bot = bot
creds = None creds = None
if not os.path.exists(self.credentials_file) or os.path.getsize(self.credentials_file) == 0: if not os.path.exists(self.credentials_file) or os.path.getsize(self.credentials_file) == 0:
@ -43,8 +46,7 @@ class MatrixModule(BotModule):
if creds and creds.expired and creds.refresh_token: if creds and creds.expired and creds.refresh_token:
creds.refresh(Request()) creds.refresh(Request())
else: else:
flow = InstalledAppFlow.from_client_secrets_file( flow = InstalledAppFlow.from_client_secrets_file(self.credentials_file, self.SCOPES)
self.credentials_file, self.SCOPES)
# urn:ietf:wg:oauth:2.0:oob # urn:ietf:wg:oauth:2.0:oob
creds = flow.run_local_server(port=0) creds = flow.run_local_server(port=0)
# Save the credentials for the next run # Save the credentials for the next run

View File

@ -3,6 +3,7 @@ from datetime import datetime
from pyteamup import Calendar from pyteamup import Calendar
# #
# TeamUp calendar notifications # TeamUp calendar notifications
# #

View File

@ -4,7 +4,7 @@ from functools import lru_cache
import httpx import httpx
from bs4 import BeautifulSoup from bs4 import BeautifulSoup
from nio import RoomMessageText from nio import RoomMessageText, AsyncClient
from modules.common.module import BotModule from modules.common.module import BotModule
@ -16,10 +16,13 @@ class MatrixModule(BotModule):
Everytime a url is seen in a message we do http request to it and try to get a title tag contents to spit out to the room. Everytime a url is seen in a message we do http request to it and try to get a title tag contents to spit out to the room.
""" """
bot = None def __init__(self, name):
status = dict() # room_id -> what to do with urls super().__init__(name)
STATUSES = { self.bot = None
self.status = dict() # room_id -> what to do with urls
self.STATUSES = {
"OFF": "Not spamming this channel", "OFF": "Not spamming this channel",
"TITLE": "Spamming this channel with titles", "TITLE": "Spamming this channel with titles",
"DESCRIPTION": "Spamming this channel with descriptions", "DESCRIPTION": "Spamming this channel with descriptions",
@ -34,6 +37,10 @@ class MatrixModule(BotModule):
self.bot = bot self.bot = bot
bot.client.add_event_callback(self.text_cb, RoomMessageText) bot.client.add_event_callback(self.text_cb, RoomMessageText)
def matrix_stop(self, bot):
super().matrix_stop(bot)
bot.remove_callback(self.text_cb)
async def text_cb(self, room, event): async def text_cb(self, room, event):
""" """
Handle client callbacks for all room text events Handle client callbacks for all room text events
@ -103,7 +110,7 @@ class MatrixModule(BotModule):
# try parse and get the title # try parse and get the title
try: try:
soup = BeautifulSoup(r.text, "html.parser") soup = BeautifulSoup(r.text, "html.parser")
title = soup.title.string title = soup.title.string.strip()
descr_tag = soup.find("meta", attrs={"name": "description"}) descr_tag = soup.find("meta", attrs={"name": "description"})
if descr_tag: if descr_tag:
description = descr_tag.get("content", None) description = descr_tag.get("content", None)