2021-02-27 10:33:45 +01:00
|
|
|
import os
|
|
|
|
import re
|
|
|
|
import shutil
|
2021-01-30 23:15:36 +01:00
|
|
|
import urllib.request
|
2022-04-07 16:31:05 +02:00
|
|
|
from datetime import datetime
|
2021-06-01 13:18:02 +02:00
|
|
|
from urllib.parse import urlparse
|
2022-02-18 12:23:07 +01:00
|
|
|
from mimetypes import guess_type
|
2021-02-27 10:33:45 +01:00
|
|
|
import jinja2
|
|
|
|
from xbotlib import Bot
|
2023-06-08 01:34:14 +02:00
|
|
|
from PIL import Image
|
|
|
|
from io import BytesIO
|
|
|
|
import base64
|
2021-02-27 10:33:45 +01:00
|
|
|
|
2021-12-12 15:28:47 +01:00
|
|
|
# Main Logbot class
|
2021-06-03 16:39:45 +02:00
|
|
|
|
2021-02-27 10:46:40 +01:00
|
|
|
class Logbot(Bot):
|
2021-01-22 00:14:44 +01:00
|
|
|
|
2022-02-18 16:46:05 +01:00
|
|
|
baseurl = "https://vvvvvvaria.org/logs/" # hardcoding the url now, self.baseurl would be helpful to have in the conf
|
|
|
|
|
2021-02-27 10:33:45 +01:00
|
|
|
help = """Oh dear, logbot is here!
|
2021-02-27 11:38:28 +01:00
|
|
|
(You can speak to the bot using @logbot or logbot:)
|
|
|
|
|
|
|
|
<image>: Your image is added to the log.
|
|
|
|
|
|
|
|
logbot @help: Print this message
|
|
|
|
|
|
|
|
logbot @add <message>: Add a message to the log.
|
|
|
|
|
|
|
|
logbot @delete <num>: Delete posts from the log. For example: @logbot @delete 5
|
2021-01-22 00:14:44 +01:00
|
|
|
|
2021-02-27 11:38:28 +01:00
|
|
|
logbot @title <string>: Set the title of your log.
|
2021-01-22 00:14:44 +01:00
|
|
|
|
2021-05-06 22:25:34 +02:00
|
|
|
logbot @folder <string>: Change the foldername of your log (by default, the roomname of the groupchat is used). Small warning: this changes the URL to your log page.
|
|
|
|
|
|
|
|
logbot @style <stylesheet>: Switch to another stylesheet. For example: logbot @style log. Available stylesheets include: timeline (default), float, opentab.
|
2021-03-12 12:10:01 +01:00
|
|
|
|
2021-03-12 21:19:40 +01:00
|
|
|
logbot @font <font>: Switch to another font. For example: logbot @font font. Available fonts include: polsku, notcouriersans; or select None to switch back to default serif.
|
2021-01-22 00:14:44 +01:00
|
|
|
|
2022-02-18 17:05:36 +01:00
|
|
|
logbot @url: Ask logbot to send the url of the log.
|
|
|
|
|
2021-02-27 11:38:28 +01:00
|
|
|
logbot @uptime: To check how long @logbot has been around
|
2021-01-22 00:14:44 +01:00
|
|
|
|
2023-06-08 01:34:14 +02:00
|
|
|
logbot @caption <num> "message": Adding a caption to a particular file.
|
|
|
|
|
2021-02-27 11:38:28 +01:00
|
|
|
@bots: To see who is around :)
|
|
|
|
""" # noqa
|
2021-01-30 17:36:59 +01:00
|
|
|
|
2022-02-18 12:23:07 +01:00
|
|
|
# Functions that are used to process logged materials
|
|
|
|
# These are marked with a "_" before their function name
|
2021-01-30 17:36:59 +01:00
|
|
|
|
2021-02-27 11:38:28 +01:00
|
|
|
def _download(self, message):
|
2021-03-07 10:50:48 +01:00
|
|
|
"""Download media files."""
|
2022-02-18 12:23:07 +01:00
|
|
|
media_mime, encoding = guess_type(message.url.lower())
|
|
|
|
media_type = str(re.match(r".*/", media_mime).group()).replace("/", "")
|
2021-02-27 11:38:28 +01:00
|
|
|
|
|
|
|
# download file
|
|
|
|
data = urllib.request.urlopen(message.url).read()
|
|
|
|
if data:
|
2021-03-07 10:51:43 +01:00
|
|
|
self.log.info(f"downloading: { message.url }")
|
2021-02-27 11:38:28 +01:00
|
|
|
parsed_url = urlparse(message.url)
|
|
|
|
filename = (
|
|
|
|
os.path.basename(parsed_url.path).replace(" ", "_").replace("%20", "_")
|
|
|
|
) # safe url's
|
2021-03-07 10:51:43 +01:00
|
|
|
self.log.info(f"as the file: { filename }")
|
2021-05-06 23:26:36 +02:00
|
|
|
folder_name = self.db[message.room]["folder"]
|
|
|
|
path = os.path.join(self.output, folder_name, media_type)
|
2021-02-27 11:38:28 +01:00
|
|
|
if not os.path.isdir(path):
|
|
|
|
os.mkdir(path)
|
|
|
|
file_path = os.path.join(path, filename)
|
|
|
|
with open(file_path, "wb") as media_file:
|
|
|
|
media_file.write(data)
|
|
|
|
|
2022-02-18 16:46:05 +01:00
|
|
|
# define relative path to media file as "media_path"
|
2022-02-18 12:23:07 +01:00
|
|
|
media_path = os.path.join(media_type, filename)
|
|
|
|
|
|
|
|
# get the size of the file
|
|
|
|
media_size = os.path.getsize(os.path.join(self.output, folder_name, media_path))
|
2021-02-27 11:38:28 +01:00
|
|
|
|
2023-06-08 01:34:14 +02:00
|
|
|
if media_type == "image":
|
|
|
|
try:
|
|
|
|
size = (450, 450)
|
|
|
|
im = Image.open(file_path)
|
|
|
|
im.thumbnail(size)
|
|
|
|
|
|
|
|
if (im.mode == 'RGBA'):
|
|
|
|
bg = Image.new('RGBA', im.size, (255,255,255))
|
|
|
|
composite = Image.alpha_composite(bg, im)
|
|
|
|
im=composite.convert('RGB')
|
|
|
|
|
|
|
|
output = BytesIO()
|
|
|
|
im.save(output, format='JPEG')
|
|
|
|
im_data = output.getvalue()
|
|
|
|
data_url = base64.b64encode(im_data).decode()
|
|
|
|
data_url = "data:image/png;base64, " + data_url
|
|
|
|
except Exception as e:
|
|
|
|
print('Thumbnailer:', e)
|
|
|
|
data_url = media_path
|
|
|
|
else:
|
|
|
|
data_url=''
|
|
|
|
|
|
|
|
return media_type, media_mime, media_path, media_size, data_url, filename
|
2022-02-18 12:23:07 +01:00
|
|
|
|
|
|
|
def _href_wrap(self, post):
|
|
|
|
"""Wrap links in <a> tags."""
|
|
|
|
for url in re.findall(r"http\S+", post):
|
|
|
|
url_with_href = f"<a href='{url}'>{url}</a>"
|
|
|
|
post = post.replace(url, url_with_href)
|
|
|
|
return post
|
2021-02-27 11:38:28 +01:00
|
|
|
|
|
|
|
def _write_log(self, message):
|
2022-02-18 12:23:07 +01:00
|
|
|
"""Generate a new log webpage."""
|
2021-05-06 23:12:04 +02:00
|
|
|
folder_name = self.db[message.room]["folder"]
|
2021-06-01 13:18:02 +02:00
|
|
|
if "@" in folder_name: # hacky
|
2021-05-06 23:12:04 +02:00
|
|
|
folder_name = self._parse_room_name(folder_name)
|
2021-05-06 22:21:58 +02:00
|
|
|
log_path = os.path.join(self.output, folder_name, "index.html")
|
2023-06-08 01:34:14 +02:00
|
|
|
template = jinja2.Template(open("template.html").read()) # it would be useful to use self.template here
|
2021-02-27 11:38:28 +01:00
|
|
|
with open(log_path, "w") as out:
|
2021-02-27 20:20:19 +01:00
|
|
|
html = template.render(
|
2021-02-27 11:38:28 +01:00
|
|
|
title=self.db[message.room]["title"],
|
2021-06-01 13:18:02 +02:00
|
|
|
db=self.db[message.room]["messages"],
|
2022-02-18 12:23:07 +01:00
|
|
|
sorted_numbering=[str(num) for num in sorted([int(num) for num in self.db[message.room]["messages"].keys()])]
|
2021-02-27 11:38:28 +01:00
|
|
|
)
|
|
|
|
out.write(html)
|
2021-03-07 11:36:03 +01:00
|
|
|
self.log.info(f"writing to: { log_path }")
|
|
|
|
|
|
|
|
def _generate_feed(self, message):
|
2022-02-18 12:23:07 +01:00
|
|
|
""" Generate a RSS feed. """
|
2021-06-01 13:18:02 +02:00
|
|
|
folder_name = self.db[message.room]["folder"]
|
2022-02-18 12:23:07 +01:00
|
|
|
if "@" in folder_name: # hacky
|
2021-05-06 23:12:04 +02:00
|
|
|
folder_name = self._parse_room_name(folder_name)
|
2022-02-18 16:19:30 +01:00
|
|
|
feed_path = os.path.join(self.output, folder_name, "feed.rss.xml")
|
2023-03-17 12:32:16 +01:00
|
|
|
date = datetime.now().strftime("%a, %d %b %Y %H:%M:%S")
|
2022-02-21 14:48:56 +01:00
|
|
|
print(date)
|
2022-02-18 12:23:07 +01:00
|
|
|
template = jinja2.Template(open("template.rss").read()) # self.feedtemplate would be useful to have in the conf
|
2021-03-07 11:36:03 +01:00
|
|
|
with open(feed_path, "w") as out:
|
|
|
|
feed = template.render(
|
2023-06-08 01:34:14 +02:00
|
|
|
log_path=os.path.join(self.baseurl, folder_name, "index.html"),
|
2022-02-18 16:46:05 +01:00
|
|
|
feed_path=os.path.join(self.baseurl, folder_name, "feed.rss.xml"),
|
2021-03-07 11:36:03 +01:00
|
|
|
title=self.db[message.room]["title"],
|
|
|
|
db=self.db[message.room],
|
2022-02-21 14:48:56 +01:00
|
|
|
date=date,
|
2022-02-18 17:19:33 +01:00
|
|
|
log_folder_url=os.path.join(self.baseurl, folder_name),
|
2021-03-07 11:36:03 +01:00
|
|
|
)
|
|
|
|
out.write(feed)
|
|
|
|
self.log.info(f"writing to: { feed_path }")
|
2021-02-27 11:38:28 +01:00
|
|
|
|
2023-06-08 01:34:14 +02:00
|
|
|
def _add_to_db(self, message, media_type=None, media_path=None, media_size=None, data_url=None, filename=None):
|
2021-02-27 11:38:28 +01:00
|
|
|
"""Save new entry to database."""
|
|
|
|
keys = [x for x in self.db[message.room]["messages"].keys()]
|
|
|
|
keys.sort(key=int)
|
2023-03-17 12:32:16 +01:00
|
|
|
date = datetime.now().strftime("%a, %d %b %Y %H:%M:%S")
|
2021-02-27 11:38:28 +01:00
|
|
|
if not keys:
|
|
|
|
new_key = "0"
|
|
|
|
else:
|
|
|
|
new_key = str(int(keys[-1]) + 1)
|
2022-02-18 16:46:05 +01:00
|
|
|
if media_path:
|
2022-02-18 12:23:07 +01:00
|
|
|
self.db[message.room]["messages"][new_key] = {}
|
2022-02-18 16:46:05 +01:00
|
|
|
self.db[message.room]["messages"][new_key]['post'] = ''
|
2022-02-18 16:19:30 +01:00
|
|
|
self.db[message.room]["messages"][new_key]['date'] = date
|
2022-02-18 12:23:07 +01:00
|
|
|
self.db[message.room]["messages"][new_key]['media'] = {}
|
|
|
|
self.db[message.room]["messages"][new_key]['media']['type'] = media_type
|
2022-02-18 16:46:05 +01:00
|
|
|
self.db[message.room]["messages"][new_key]['media']['path'] = media_path
|
2022-02-18 12:23:07 +01:00
|
|
|
self.db[message.room]["messages"][new_key]['media']['size'] = media_size
|
2023-06-08 01:34:14 +02:00
|
|
|
self.db[message.room]["messages"][new_key]['media']['filename'] = filename
|
|
|
|
self.db[message.room]["messages"][new_key]['media']['data_url'] = data_url
|
|
|
|
self.db[message.room]["messages"][new_key]['media']['caption'] = ''
|
2021-02-27 11:38:28 +01:00
|
|
|
else:
|
2021-03-07 11:36:03 +01:00
|
|
|
post = message.content.replace("@add ", "")
|
2022-02-18 12:23:07 +01:00
|
|
|
post = self._href_wrap(post)
|
|
|
|
self.db[message.room]["messages"][new_key] = {}
|
2022-02-18 16:20:32 +01:00
|
|
|
self.db[message.room]["messages"][new_key]['post'] = post
|
2022-02-18 12:23:07 +01:00
|
|
|
self.db[message.room]["messages"][new_key]['date'] = date
|
2021-03-07 10:52:33 +01:00
|
|
|
self.db._dumps()
|
2022-02-18 15:04:22 +01:00
|
|
|
return new_key
|
|
|
|
|
2021-02-27 11:30:44 +01:00
|
|
|
def _parse_room_name(self, room):
|
2022-02-18 12:23:07 +01:00
|
|
|
"""Parse room name from full MUC address string."""
|
2021-02-27 11:30:44 +01:00
|
|
|
return str(re.match(r".*@", room).group()).replace("@", "")
|
|
|
|
|
|
|
|
def _setup_room(self, room):
|
|
|
|
"""Create directories and database entries for a new room."""
|
|
|
|
room_name = self._parse_room_name(room)
|
2022-02-18 15:43:48 +01:00
|
|
|
if room in self.db:
|
|
|
|
room_path = self.db[room]["folder"]
|
|
|
|
else:
|
|
|
|
room_path = os.path.join(self.output, room_name)
|
2022-02-18 12:23:07 +01:00
|
|
|
self.log.info(f"Processing setup logic for: { room_path }")
|
2021-02-27 11:30:44 +01:00
|
|
|
|
2021-02-27 20:15:17 +01:00
|
|
|
if room not in self.db:
|
2021-02-27 11:30:44 +01:00
|
|
|
self.db[room] = {}
|
2021-02-27 20:11:24 +01:00
|
|
|
if "messages" not in self.db[room]:
|
2021-02-27 11:30:44 +01:00
|
|
|
self.db[room]["messages"] = {}
|
2021-02-27 20:11:24 +01:00
|
|
|
if "title" not in self.db[room]:
|
2021-02-27 11:30:44 +01:00
|
|
|
self.db[room]["title"] = room
|
2021-05-06 22:21:58 +02:00
|
|
|
if "folder" not in self.db[room]:
|
2021-05-06 22:31:06 +02:00
|
|
|
self.db[room]["folder"] = self._parse_room_name(room)
|
2021-03-12 12:10:01 +01:00
|
|
|
if "stylesheet" not in self.db[room]:
|
2022-02-18 16:01:25 +01:00
|
|
|
self.db[room]["stylesheet"] = "timeline" # default stylesheet
|
2021-03-12 12:10:01 +01:00
|
|
|
if "font" not in self.db[room]:
|
2022-02-18 16:01:25 +01:00
|
|
|
self.db[room]["font"] = "none" # default font
|
2021-02-27 20:15:28 +01:00
|
|
|
self.db._dumps()
|
2021-02-27 20:11:24 +01:00
|
|
|
self.log.info(f"Added to the database: { room }")
|
2021-02-27 11:30:44 +01:00
|
|
|
|
|
|
|
if not os.path.exists(room_path):
|
|
|
|
os.mkdir(room_path)
|
2022-02-18 16:01:25 +01:00
|
|
|
stylesheet_path = os.path.join("stylesheets", self.db[room]["stylesheet"] + ".css")
|
2021-05-06 22:51:03 +02:00
|
|
|
stylesheet_dest_path = os.path.join(room_path, "stylesheet.css")
|
2021-03-12 12:10:01 +01:00
|
|
|
shutil.copy(stylesheet_path, stylesheet_dest_path)
|
2021-02-27 11:30:44 +01:00
|
|
|
self.log.info(f"Created a folder for: { room }")
|
2021-03-12 12:10:01 +01:00
|
|
|
self.log.info(f"Added stylesheet.css to: { room }")
|
2021-02-27 11:30:44 +01:00
|
|
|
|
2021-02-27 10:33:45 +01:00
|
|
|
def setup(self):
|
2021-03-07 10:50:48 +01:00
|
|
|
"""Setup a log for all the rooms LogBot is subscribed to."""
|
2021-02-27 10:45:19 +01:00
|
|
|
self.log.info(f"Output folder is set to: { self.output }")
|
2021-02-27 10:33:45 +01:00
|
|
|
for room in self.rooms:
|
2021-02-27 11:30:44 +01:00
|
|
|
self._setup_room(room)
|
2021-02-27 20:07:14 +01:00
|
|
|
for room in self._data["invited"]:
|
|
|
|
self._setup_room(room)
|
2021-02-27 10:33:45 +01:00
|
|
|
|
2021-02-27 11:30:44 +01:00
|
|
|
def group_invite(self, message):
|
|
|
|
"""Extend xbotlib invite response logic and do required room setup."""
|
|
|
|
super().group_invite(message)
|
2021-02-27 11:46:53 +01:00
|
|
|
self._setup_room(str(message["from"]))
|
2021-02-27 10:33:45 +01:00
|
|
|
|
|
|
|
def group(self, message):
|
2021-03-07 10:50:48 +01:00
|
|
|
"""All the things LogBot does when it receives a group message."""
|
2021-02-27 10:45:19 +01:00
|
|
|
self.log.info("------------------")
|
2021-03-07 10:51:43 +01:00
|
|
|
self.log.info(f"message: { message.text }")
|
|
|
|
self.log.info(f"room: { message.room }")
|
|
|
|
self.log.info(f"sender: { message.sender }")
|
2021-02-27 11:56:35 +01:00
|
|
|
self.log.info("------------------")
|
2021-02-27 10:33:45 +01:00
|
|
|
|
2021-03-07 10:55:29 +01:00
|
|
|
# Response to files: image / PDF / audio / video
|
2021-02-27 10:33:45 +01:00
|
|
|
if message.url:
|
2023-06-08 01:34:14 +02:00
|
|
|
media_type, media_mime, media_path, media_size, data_url, filename = self._download(message)
|
2022-02-18 12:23:07 +01:00
|
|
|
# TODO: Insert a list of accepted file types here.
|
2022-02-18 16:48:12 +01:00
|
|
|
if media_path:
|
2023-06-08 01:34:14 +02:00
|
|
|
num = self._add_to_db(message, media_type=media_mime, media_path=media_path, media_size=media_size, data_url=data_url, filename=filename)
|
2022-02-18 12:23:07 +01:00
|
|
|
media_type = media_type.replace("images", "image") # linguistic hack!
|
|
|
|
if 'pdf' in message.url:
|
|
|
|
media_type = 'PDF' # linguistic hack!
|
2022-02-18 15:04:22 +01:00
|
|
|
reply = f"Thanks for that { media_type }! If you want to remove it from the log, you can send 'logbot @delete { num }'"
|
2021-02-27 10:33:45 +01:00
|
|
|
else:
|
2021-02-27 11:08:00 +01:00
|
|
|
reply = "Sorry, can't process that :( (unknown media type?)"
|
2021-02-27 10:33:45 +01:00
|
|
|
|
2021-03-07 10:55:29 +01:00
|
|
|
# Response to @add
|
2021-02-27 10:33:45 +01:00
|
|
|
elif "@add" in message.text:
|
2021-02-27 11:38:28 +01:00
|
|
|
self._add_to_db(message)
|
2021-02-27 10:33:45 +01:00
|
|
|
reply = "Added, thanks!"
|
|
|
|
|
2022-02-18 17:05:36 +01:00
|
|
|
# Response to @url
|
|
|
|
elif "@url" in message.text:
|
2022-02-18 17:07:08 +01:00
|
|
|
reply = f'{ self.baseurl }{ self.db[message.room]["folder"] }/'
|
2022-02-18 17:05:36 +01:00
|
|
|
|
2021-03-07 10:55:29 +01:00
|
|
|
# Response to @delete
|
2021-02-27 10:33:45 +01:00
|
|
|
elif "@delete" in message.text:
|
2021-02-27 10:47:16 +01:00
|
|
|
match = re.findall(r"@delete \d*", message.content)[0]
|
2021-02-27 10:33:45 +01:00
|
|
|
key = str(match.replace("@delete ", ""))
|
|
|
|
|
|
|
|
if key in self.db[message.room]["messages"]:
|
2021-02-27 11:57:19 +01:00
|
|
|
self.log.info(
|
2021-03-07 10:51:43 +01:00
|
|
|
f"To be deleted: { self.db[message.room]['messages'][key] }"
|
2021-02-27 11:57:19 +01:00
|
|
|
)
|
2022-02-18 15:07:26 +01:00
|
|
|
if 'media' in self.db[message.room]['messages'][key]:
|
2022-02-18 17:24:09 +01:00
|
|
|
reply = f"Deleted: { self.db[message.room]['messages'][key]['media']['path'] }"
|
2022-02-18 15:07:26 +01:00
|
|
|
else:
|
2022-02-18 15:15:03 +01:00
|
|
|
reply = f"Deleted: { self.db[message.room]['messages'][key]['post'] }"
|
2021-02-27 11:38:28 +01:00
|
|
|
del self.db[message.room]["messages"][key]
|
2021-02-27 10:33:45 +01:00
|
|
|
else:
|
2022-02-18 15:15:03 +01:00
|
|
|
reply = "This one is already gone!"
|
2021-02-27 10:33:45 +01:00
|
|
|
|
2023-06-08 01:34:14 +02:00
|
|
|
# Response to @caption
|
|
|
|
elif "@caption" in message.text:
|
|
|
|
match = re.findall(r"@caption \d*", message.content)[0]
|
|
|
|
key = str(match.replace("@caption ", ""))
|
|
|
|
captiontext = re.findall(r'"(.*?)"', message.text)
|
|
|
|
self.db[message.room]["messages"][key]['media']['caption'] = captiontext
|
|
|
|
reply = captiontext
|
|
|
|
|
|
|
|
if key in self.db[message.room]["messages"]:
|
|
|
|
self.log.info(
|
|
|
|
f"To be captioned: { self.db[message.room]['messages'][key] }"
|
|
|
|
)
|
|
|
|
reply = f"Added a caption for file number {key}."
|
|
|
|
|
2021-03-07 10:55:29 +01:00
|
|
|
# Response to @title
|
2021-02-27 10:33:45 +01:00
|
|
|
elif "@title" in message.text:
|
|
|
|
match = re.findall("@title .*", message.content)[0]
|
2021-03-12 12:10:01 +01:00
|
|
|
title = match.replace("@title ", "")
|
2021-02-27 10:33:45 +01:00
|
|
|
self.db[message.room]["title"] = title
|
|
|
|
reply = f"The title of the log is changed to: { title }"
|
|
|
|
|
2021-05-06 22:21:58 +02:00
|
|
|
# Response to @folder
|
|
|
|
# https://git.vvvvvvaria.org/varia/bots/issues/5
|
|
|
|
elif "@folder" in message.text:
|
|
|
|
match = re.findall("@folder .*", message.content)[0]
|
2021-05-06 22:40:37 +02:00
|
|
|
new_folder_name = match.replace("@folder ", "")
|
|
|
|
current_folder_name = self.db[message.room]["folder"]
|
2021-06-01 13:18:02 +02:00
|
|
|
if "@" in current_folder_name: # hacky
|
2021-05-06 22:40:37 +02:00
|
|
|
current_folder_name = self._parse_room_name(current_folder_name)
|
|
|
|
current_folder_name_path = os.path.join(self.output, current_folder_name)
|
|
|
|
new_folder_name_path = os.path.join(self.output, new_folder_name)
|
2021-06-01 13:18:02 +02:00
|
|
|
try:
|
2021-05-06 22:48:54 +02:00
|
|
|
os.rename(current_folder_name_path, new_folder_name_path)
|
|
|
|
self.db[message.room]["folder"] = new_folder_name
|
2022-02-18 16:46:05 +01:00
|
|
|
reply = f"The foldername of the log is changed to: { new_folder_name }. The URL of the log changed into: { self.baseurl }{ new_folder_name}"
|
2021-05-06 22:48:54 +02:00
|
|
|
except:
|
2021-06-01 13:18:02 +02:00
|
|
|
reply = f"Sorry i couldn't shange that foldername into: '{ new_folder_name }'. Try again with: 'logbot @folder newname'."
|
2021-05-06 22:21:58 +02:00
|
|
|
|
2021-03-07 10:55:29 +01:00
|
|
|
# Response to @style
|
2021-02-27 10:33:45 +01:00
|
|
|
elif "@style" in message.text:
|
2021-03-12 12:10:01 +01:00
|
|
|
match = re.findall("@style .*", message.content)[0]
|
|
|
|
stylesheet = match.replace("@style ", "")
|
|
|
|
stylesheet = stylesheet.lower()
|
|
|
|
self.db[message.room]["stylesheet"] = stylesheet
|
|
|
|
room_name = self._parse_room_name(message.room)
|
2021-05-06 22:48:54 +02:00
|
|
|
room_path = os.path.join(self.output, self.db[message.room]["folder"])
|
2021-06-01 13:18:02 +02:00
|
|
|
if "@" in room_path: # hacky
|
2021-05-06 23:14:15 +02:00
|
|
|
room_path = self._parse_room_name(room_path)
|
2021-03-12 12:10:01 +01:00
|
|
|
stylesheet_path = os.path.join("stylesheets", f"{ stylesheet }.css")
|
|
|
|
stylesheet_dest_path = os.path.join(room_path, "stylesheet.css")
|
|
|
|
try:
|
|
|
|
shutil.copy(stylesheet_path, stylesheet_dest_path)
|
2021-06-01 13:18:02 +02:00
|
|
|
self.log.info(
|
|
|
|
f"Stylesheet in room { room_name } switched to: { stylesheet }"
|
|
|
|
)
|
2021-03-12 12:10:01 +01:00
|
|
|
reply = f"I'm switching the stylesheet of this log to: { stylesheet }."
|
|
|
|
except:
|
2021-12-12 13:12:35 +01:00
|
|
|
reply = f"The stylesheet '{ stylesheet }' is unknown to me. Check logbot @help to see the available stylesheets."
|
2021-03-12 12:10:01 +01:00
|
|
|
|
|
|
|
# Response to @font
|
|
|
|
elif "@font" in message.text:
|
|
|
|
match = re.findall("@font .*", message.content)[0]
|
|
|
|
font = match.replace("@font ", "")
|
|
|
|
font = font.lower()
|
|
|
|
self.db[message.room]["font"] = font
|
|
|
|
room_name = self._parse_room_name(message.room)
|
2021-05-06 22:48:54 +02:00
|
|
|
room_path = os.path.join(self.output, self.db[message.room]["folder"])
|
2021-06-01 13:18:02 +02:00
|
|
|
if "@" in room_path: # hacky
|
2021-05-06 23:14:15 +02:00
|
|
|
room_path = self._parse_room_name(room_path)
|
2021-03-12 12:10:01 +01:00
|
|
|
font_path = os.path.join("fonts", f"{ font }.ttf")
|
|
|
|
font_dest_path = os.path.join(room_path, "font.ttf")
|
2021-06-01 13:18:02 +02:00
|
|
|
if font == "none":
|
2021-03-12 12:10:01 +01:00
|
|
|
os.remove(font_dest_path)
|
|
|
|
reply = "I removed the font and switched back to default serif."
|
|
|
|
else:
|
|
|
|
try:
|
|
|
|
shutil.copy(font_path, font_dest_path)
|
|
|
|
self.log.info(f"font in room { room_name } switched to: { font }")
|
|
|
|
reply = f"I'm switching the font of this log to: { font }."
|
|
|
|
except:
|
|
|
|
reply = f"The font '{ font }' is unknown to me. Check @help to see the available fonts."
|
2021-02-27 10:33:45 +01:00
|
|
|
|
|
|
|
else:
|
2021-12-12 13:12:35 +01:00
|
|
|
reply = "Hmm ... not sure what you want to do? Check logbot @help to see the available commands."
|
2021-02-27 10:33:45 +01:00
|
|
|
|
2021-03-07 10:56:47 +01:00
|
|
|
# Regenerate the log webpage
|
2021-02-27 11:50:54 +01:00
|
|
|
self._write_log(message)
|
2021-03-07 10:56:47 +01:00
|
|
|
|
2021-03-07 11:36:03 +01:00
|
|
|
# Regenerate the RSS feed
|
|
|
|
self._generate_feed(message)
|
|
|
|
|
2021-03-07 10:56:47 +01:00
|
|
|
# Reply to the groupchat
|
2021-02-27 10:46:10 +01:00
|
|
|
self.reply(reply, room=message.room)
|
2021-02-27 10:33:45 +01:00
|
|
|
|
2021-02-27 10:46:40 +01:00
|
|
|
Logbot()
|