ref: upload avatar

This commit is contained in:
Czar 2023-08-31 07:50:57 -03:00
commit 4a9b4ccbb4
Signed by: czar
GPG key ID: 2DBA9558E9277C37

53
main.py
View file

@ -6,7 +6,6 @@ import configparser
import re
import io
import os
import mimetypes
import asyncio
from collections import defaultdict
from PythonSed import Sed
@ -44,6 +43,7 @@ req_list = ("http://", "https://")
html_files = ("text/html", "application/xhtml+xml")
class Lifo(list):
"""Limited size LIFO array to store messages and urls."""
@ -84,7 +84,7 @@ def get_yurl(path):
class AngelBot(ClientXMPP):
"""AngelBot class."""
messages = defaultdict(
lambda: {
"messages": Lifo(100),
@ -136,12 +136,12 @@ class AngelBot(ClientXMPP):
r = requests.get(url, stream=True, headers=headers, timeout=6)
if not r.ok:
return
ftype = normalize_mimetype(r.headers.get("content-type"))
if not ftype:
return
return
if ftype in html_files:
data = ""
for i in r.iter_content(chunk_size=1024, decode_unicode=False):
@ -181,7 +181,7 @@ class AngelBot(ClientXMPP):
filename = params.get("filename")
else:
filename = os.path.basename(uri.path)
ext = os.path.splitext(filename)[1] if filename else ".txt"
fname = filename if filename else f"file{ext}"
await self.embed_file(url, sender, mtype, ftype, fname, outfile)
@ -277,7 +277,7 @@ class AngelBot(ClientXMPP):
print(e)
async def update_info(self):
"""Update the bot info."""
"""Update the bot info."""
with open("angel.png", "rb") as avatar_file:
avatar = avatar_file.read()
@ -285,22 +285,6 @@ class AngelBot(ClientXMPP):
avatar_id = self.plugin["xep_0084"].generate_id(avatar)
avatar_bytes = len(avatar)
info = {
"id": avatar_id,
"type": avatar_type,
"bytes": avatar_bytes,
}
vcard = self.plugin["xep_0054"].make_vcard()
vcard["URL"] = "https://wiki.kalli.st/Angel"
vcard["DESC"] = "Angel is a bot that can do link previews and embeds."
vcard["NICKNAME"] = "Angel"
vcard["FN"] = "Angel"
vcard["PHOTO"] = info
asyncio.gather(self.plugin["xep_0054"].publish_vcard(vcard))
asyncio.gather(self.plugin["xep_0084"].publish_avatar(avatar))
asyncio.gather(
@ -310,15 +294,30 @@ class AngelBot(ClientXMPP):
)
)
info = {
"id": avatar_id,
"type": avatar_type,
"bytes": avatar_bytes,
}
asyncio.gather(self.plugin["xep_0084"].publish_avatar_metadata([info]))
vcard = self.plugin["xep_0054"].make_vcard()
vcard["URL"] = "https://wiki.kalli.st/Angel"
vcard["DESC"] = "Angel is a bot that can do link previews and embeds."
vcard["NICKNAME"] = "Angel"
vcard["FN"] = "Angel"
asyncio.gather(self.plugin["xep_0054"].publish_vcard(vcard))
async def message(self, msg):
"""Process a message."""
if msg["type"] in ("chat", "normal"):
edit = "urn:xmpp:message-correct:0" in str(msg)
if edit:
return
mtype = msg["type"]
sender = msg["from"].bare
@ -335,16 +334,16 @@ class AngelBot(ClientXMPP):
"""Process a groupchat message."""
if msg["type"] in ("groupchat", "normal"):
edit = "urn:xmpp:message-correct:0" in str(msg)
if edit:
return
if msg["mucnick"] == self.nick:
return
mtype = msg["type"]
sender = msg["from"].bare
try:
if not msg["oob"]["url"]: