From 6a7d99f1a2e775072f93c3099ec132ca3d3ee377 Mon Sep 17 00:00:00 2001 From: Schimon Jehudah Date: Wed, 17 Jan 2024 14:36:28 +0000 Subject: [PATCH] Add functionality profile --- assets/accounts.ini | 22 ++++++++++-- assets/image.svg | 1 + slixfeed/__main__.py | 3 ++ slixfeed/action.py | 19 +++++++++-- slixfeed/config.py | 2 ++ slixfeed/xmpp/client.py | 4 ++- slixfeed/xmpp/process.py | 3 +- slixfeed/xmpp/profile.py | 72 ++++++++++++++++++++++++++++++++++++++++ slixfeed/xmpp/upload.py | 25 ++++++++++---- slixfeed/xmpp/utility.py | 1 + 10 files changed, 139 insertions(+), 13 deletions(-) create mode 100644 assets/image.svg create mode 100644 slixfeed/xmpp/profile.py diff --git a/assets/accounts.ini b/assets/accounts.ini index 98493c7..4557fb5 100644 --- a/assets/accounts.ini +++ b/assets/accounts.ini @@ -2,12 +2,30 @@ # and also from which accounts it receives instructions. [XMPP] -nickname = +nickname = Slixfeed username = password = -# port = 5347 operator = +[XMPP Profile] +name = Slixfeed +nickname = Slixfeed +role = Syndication News Bot +organization = RSS Task Force +url = https://gitgud.io/sjehuda/slixfeed +description = This is an XMPP syndication news bot. It supports Atom, JSON, RDF and RSS feeds. +note = This is a news bot powered by Slixfeed +birthday = 21 June 2022 + +[XMPP Connect] +port = 5347 +proxy_host = 127.0.0.1 +proxy_port = 9050 +proxy_username = +proxy_password = +proxy_enabled = 1 +reconnect_timeout = 30 + [ActivityPub] # Not yet implemented username = diff --git a/assets/image.svg b/assets/image.svg new file mode 100644 index 0000000..f0f4257 --- /dev/null +++ b/assets/image.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/slixfeed/__main__.py b/slixfeed/__main__.py index 88941a4..6bbec84 100644 --- a/slixfeed/__main__.py +++ b/slixfeed/__main__.py @@ -111,10 +111,13 @@ class Jabber: xmpp.register_plugin('xep_0030') # Service Discovery xmpp.register_plugin('xep_0045') # Multi-User Chat xmpp.register_plugin('xep_0048') # Bookmarks + xmpp.register_plugin('xep_0054') # vcard-temp xmpp.register_plugin('xep_0060') # Publish-Subscribe # xmpp.register_plugin('xep_0065') # SOCKS5 Bytestreams xmpp.register_plugin('xep_0066') # Out of Band Data xmpp.register_plugin('xep_0071') # XHTML-IM + xmpp.register_plugin('xep_0084') # User Avatar + xmpp.register_plugin('xep_0153') # vCard-Based Avatars xmpp.register_plugin('xep_0199', {'keepalive': True}) # XMPP Ping xmpp.register_plugin('xep_0249') # Multi-User Chat xmpp.register_plugin('xep_0363') # HTTP File Upload diff --git a/slixfeed/action.py b/slixfeed/action.py index 4e7e853..09548b1 100644 --- a/slixfeed/action.py +++ b/slixfeed/action.py @@ -442,7 +442,10 @@ async def add_feed(db_file, url): encoding = '' if "updated_parsed" in feed["feed"].keys(): updated = feed["feed"]["updated_parsed"] - updated = convert_struct_time_to_iso8601(updated) + try: + updated = convert_struct_time_to_iso8601(updated) + except: + updated = '' else: updated = '' version = feed["version"] @@ -668,7 +671,10 @@ async def scan(db_file, url): db_file, feed_id, valid) if "updated_parsed" in feed["feed"].keys(): updated = feed["feed"]["updated_parsed"] - updated = convert_struct_time_to_iso8601(updated) + try: + updated = convert_struct_time_to_iso8601(updated) + except: + updated = '' else: updated = '' feed_id = await sqlite.get_feed_id(db_file, url) @@ -855,7 +861,14 @@ async def extract_image_from_html(url): "Check that package readability is installed.") tree = html.fromstring(content) # TODO Exclude banners, class="share" links etc. - images = tree.xpath('//img/@src') + images = tree.xpath( + '//img[not(' + 'contains(@src, "avatar") or ' + 'contains(@src, "emoji") or ' + 'contains(@src, "icon") or ' + 'contains(@src, "logo") or ' + 'contains(@src, "smiley")' + ')]/@src') if len(images): image = images[0] image = str(image) diff --git a/slixfeed/config.py b/slixfeed/config.py index 753307b..37bc8f3 100644 --- a/slixfeed/config.py +++ b/slixfeed/config.py @@ -15,6 +15,8 @@ TODO 4) Copy file from /etc/slixfeed/ or /usr/share/slixfeed/ +5) Merge get_value_default into get_value. + """ import configparser diff --git a/slixfeed/xmpp/client.py b/slixfeed/xmpp/client.py index f15248b..3a110d7 100644 --- a/slixfeed/xmpp/client.py +++ b/slixfeed/xmpp/client.py @@ -61,8 +61,9 @@ from slixmpp.plugins.xep_0048.stanza import Bookmarks # from lxml import etree import slixfeed.xmpp.connect as connect -import slixfeed.xmpp.process as process import slixfeed.xmpp.muc as muc +import slixfeed.xmpp.process as process +import slixfeed.xmpp.profile as profile import slixfeed.xmpp.roster as roster import slixfeed.xmpp.state as state import slixfeed.xmpp.status as status @@ -169,6 +170,7 @@ class Slixfeed(slixmpp.ClientXMPP): async def on_session_start(self, event): await process.event(self, event) await muc.autojoin(self, event) + await profile.update(self) async def on_session_resumed(self, event): diff --git a/slixfeed/xmpp/process.py b/slixfeed/xmpp/process.py index 80ce9a8..ffddb9d 100644 --- a/slixfeed/xmpp/process.py +++ b/slixfeed/xmpp/process.py @@ -352,7 +352,8 @@ async def message(self, message): response = "Missing value." send_reply_message(self, message, response) case _ if message_lowercase.startswith("bookmark -"): - if jid == get_value("accounts", "XMPP", "operator"): + if jid == get_value( + "accounts", "XMPP", "operator"): muc_jid = message_text[11:] await bookmark.remove(self, muc_jid) response = ( diff --git a/slixfeed/xmpp/profile.py b/slixfeed/xmpp/profile.py new file mode 100644 index 0000000..82e4045 --- /dev/null +++ b/slixfeed/xmpp/profile.py @@ -0,0 +1,72 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- + +""" + +NOTE + +The VCard XML fields that can be set are as follows: + ‘FN’, ‘NICKNAME’, ‘URL’, ‘BDAY’, ‘ROLE’, ‘NOTE’, ‘MAILER’, + ‘TZ’, ‘REV’, ‘UID’, ‘DESC’, ‘TITLE’, ‘PRODID’, ‘SORT-STRING’, + ‘N’, ‘ADR’, ‘TEL’, ‘EMAIL’, ‘JABBERID’, ‘ORG’, ‘CATEGORIES’, + ‘NOTE’, ‘PRODID’, ‘REV’, ‘SORT-STRING’, ‘SOUND’, ‘UID’, ‘URL’, + ‘CLASS’, ‘KEY’, ‘MAILER’, ‘GEO’, ‘TITLE’, ‘ROLE’, + ‘LOGO’, ‘AGENT’ + +TODO + +1) Test XEP-0084. + +2) Make sure to support all type of servers. + +3) Catch IqError + ERROR:slixmpp.basexmpp:internal-server-error: Database failure + WARNING:slixmpp.basexmpp:You should catch IqError exceptions + +""" + +import glob +from slixfeed.config import get_value, get_default_config_directory +# from slixmpp.exceptions import IqTimeout, IqError +# import logging +import os + + +async def update(self): + """ + Update profile. + """ + await set_vcard(self) + await set_avatar(self) + + +async def set_avatar(self): + config_dir = get_default_config_directory() + if not os.path.isdir(config_dir): + config_dir = '/usr/share/slixfeed/' + filename = glob.glob(config_dir + "/image.*")[0] + image_file = os.path.join(config_dir, filename) + with open(image_file, "rb") as avatar_file: + avatar = avatar_file.read() + # await self.plugin["xep_0084"].publish_avatar(avatar) + await self.plugin["xep_0153"].set_avatar(avatar=avatar) + + +async def set_vcard(self): + vcard = self.plugin["xep_0054"].make_vcard() + fields = { + "BDAY": "birthday", + "DESC": "description", + "FN": "name", + "NICKNAME": "nickname", + "NOTE": "note", + "ORG": "organization", + "ROLE": "role", + "TITLE": "title", + "URL": "url", + } + for key in fields: + vcard[key] = get_value( + "accounts", "XMPP Profile", fields[key]) + await self.plugin["xep_0054"].publish_vcard(vcard) + diff --git a/slixfeed/xmpp/upload.py b/slixfeed/xmpp/upload.py index 6a32f76..9483ecc 100644 --- a/slixfeed/xmpp/upload.py +++ b/slixfeed/xmpp/upload.py @@ -7,7 +7,8 @@ https://codeberg.org/poezio/slixmpp/src/branch/master/examples/http_upload.py """ import logging -from slixmpp.exceptions import IqTimeout +from slixmpp.exceptions import IqTimeout, IqError +from slixmpp.plugins.xep_0363.http_upload import HTTPError # import sys @@ -25,11 +26,23 @@ async def start(self, jid, filename, domain=None): # return # elif self.encrypted: # upload_file = self['xep_0454'].upload_file - url = await upload_file( - filename, domain, timeout=10, - ) + try: + url = await upload_file( + filename, domain, timeout=10, + ) + logging.info('Upload successful!') + logging.info('Sending file to %s', jid) + except HTTPError: + url = ( + "Error: It appears that this server doesn't support " + "HTTP File Upload." + ) + logging.error( + "It appears that this server doesn't support HTTP File Upload." + ) + # raise HTTPError( + # "This server doesn't appear to support HTTP File Upload" + # ) except IqTimeout: raise TimeoutError('Could not send message in time') - logging.info('Upload successful!') - logging.info('Sending file to %s', jid) return url diff --git a/slixfeed/xmpp/utility.py b/slixfeed/xmpp/utility.py index ae992d8..f103098 100644 --- a/slixfeed/xmpp/utility.py +++ b/slixfeed/xmpp/utility.py @@ -4,6 +4,7 @@ from slixmpp.exceptions import IqTimeout import logging + async def jid_type(self, jid): """ Check whether a JID is of MUC.