From ed7491b5a9d3471136058d49510add1fb1a213ce Mon Sep 17 00:00:00 2001 From: "Schimon Jehudah, Adv." Date: Wed, 31 Jul 2024 20:53:42 +0300 Subject: [PATCH] Fix automated publishing mechanism of PubSub, so it will not halt other procedures. --- README.md | 2 +- slixfeed/log.py | 3 ++- slixfeed/syndication.py | 4 ++++ slixfeed/version.py | 4 ++-- slixfeed/xmpp/client.py | 26 +++++++++++++++--------- slixfeed/xmpp/encryption.py | 14 +++++++++++++ slixfeed/xmpp/publish.py | 40 +++++++++++++++++++++++++++++++++++++ 7 files changed, 80 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index d1a5b7e..8acfc74 100644 --- a/README.md +++ b/README.md @@ -88,7 +88,7 @@ $ slixfeed ## Recommended Clients -Slixfeed works with any XMPP chat client; if you want to make use of the visual interface Slixfeed has to offer (i.e. Ad-Hoc Commands), then you are advised to use [Cheogram](https://cheogram.com), [Converse](https://conversejs.org), [Gajim](https://gajim.org), [monocles chat](https://monocles.chat), [Movim](https://mov.im), [Poezio](https://poez.io), [Psi](https://psi-im.org) or [Psi+](https://psi-plus.com). +Slixfeed works with any XMPP chat client; if you want to make use of the visual interface Slixfeed has to offer (i.e. Ad-Hoc Commands), then you are advised to use [Cheogram](https://cheogram.com), [Converse](https://conversejs.org), [Gajim](https://gajim.org), [LeechCraft](https://leechcraft.org/plugins-azoth-xoox), [monocles chat](https://monocles.chat), [Movim](https://mov.im), [Poezio](https://poez.io), [Psi](https://psi-im.org) or [Psi+](https://psi-plus.com). ### Support diff --git a/slixfeed/log.py b/slixfeed/log.py index a0080aa..62deb0e 100644 --- a/slixfeed/log.py +++ b/slixfeed/log.py @@ -58,4 +58,5 @@ class Message: def printer(text): now = datetime.now() current_time = now.strftime("%H:%M:%S") - print('{} {}'.format(current_time, text), end='\r') +# print('{} {}'.format(current_time, text), end='\r') + print('{} {}'.format(current_time, text)) diff --git a/slixfeed/syndication.py b/slixfeed/syndication.py index ad5d031..4997362 100644 --- a/slixfeed/syndication.py +++ b/slixfeed/syndication.py @@ -1371,6 +1371,10 @@ class FeedTask: # ) +# Consider an endless loop. See XmppPubsubTask.loop_task +# def restart_task(self, jid_bare): + + def restart_task(self, jid_bare): if jid_bare == self.boundjid.bare: return diff --git a/slixfeed/version.py b/slixfeed/version.py index aa2753d..cb49764 100644 --- a/slixfeed/version.py +++ b/slixfeed/version.py @@ -1,2 +1,2 @@ -__version__ = '0.1.92' -__version_info__ = (0, 1, 92) +__version__ = '0.1.93' +__version_info__ = (0, 1, 93) diff --git a/slixfeed/xmpp/client.py b/slixfeed/xmpp/client.py index 7d63951..42b9306 100644 --- a/slixfeed/xmpp/client.py +++ b/slixfeed/xmpp/client.py @@ -349,21 +349,29 @@ class XmppClient(slixmpp.ClientXMPP): await self.get_roster() # self.service_reactions() XmppConnectTask.ping(self) - # results = await XmppPubsub.get_pubsub_services(self) - # for result in results + [{'jid' : self.boundjid.bare, - # 'name' : self.alias}]: - # jid_bare = result['jid'] - # if jid_bare not in self.settings: - # db_file = config.get_pathname_to_database(jid_bare) - # Config.add_settings_jid(self, jid_bare, db_file) - # await FeedTask.check_updates(self, jid_bare) - # XmppPubsubTask.task_publish(self, jid_bare) + bookmarks = await XmppBookmark.get_bookmarks(self) await XmppGroupchat.autojoin(self, bookmarks) if 'ipc' in self.defaults and self.defaults['ipc']['bsd']: # Start Inter-Process Communication print('POSIX sockets: Initiating IPC server...') self.ipc = asyncio.create_task(XmppIpcServer.ipc(self)) + + while True: + results = await XmppPubsub.get_pubsub_services(self) + for result in results + [{'jid' : self.boundjid.bare, + 'name' : self.alias}]: + jid_bare = result['jid'] + if jid_bare not in self.settings: + db_file = config.get_pathname_to_database(jid_bare) + Config.add_settings_jid(self, jid_bare, db_file) + #await FeedTask.check_updates(self, jid_bare) + #await XmppPubsubTask.task_publish(self, jid_bare) + FeedTask.restart_task(self, jid_bare) + #XmppPubsubTask.loop_task(self, jid_bare) + XmppPubsubTask.restart_task(self, jid_bare) + await asyncio.sleep(60 * 180) + time_end = time.time() difference = time_end - time_begin if difference > 1: logger.warning('{} (time: {})'.format(function_name, diff --git a/slixfeed/xmpp/encryption.py b/slixfeed/xmpp/encryption.py index d6362dd..f3f1154 100644 --- a/slixfeed/xmpp/encryption.py +++ b/slixfeed/xmpp/encryption.py @@ -49,6 +49,7 @@ class XmppOmemo: async def decrypt(self, message: Message, allow_untrusted: bool = False): jid = message['from'] try: + print('XmppOmemo.decrypt') message_omemo_encrypted = message['omemo_encrypted'] message_body = await self['xep_0384'].decrypt_message( message_omemo_encrypted, jid, allow_untrusted) @@ -62,6 +63,7 @@ class XmppOmemo: omemo_decrypted = response = None retry = None except (MissingOwnKey,) as exn: + print('XmppOmemo.decrypt. except: MissingOwnKey') # The message is missing our own key, it was not encrypted for # us, and we can't decrypt it. response = ('Error: Your message has not been encrypted for ' @@ -70,6 +72,7 @@ class XmppOmemo: retry = False logger.error(exn) except (NoAvailableSession,) as exn: + print('XmppOmemo.decrypt. except: NoAvailableSession') # We received a message from that contained a session that we # don't know about (deleted session storage, etc.). We can't # decrypt the message, and it's going to be lost. @@ -82,6 +85,8 @@ class XmppOmemo: retry = False logger.error(exn) except (UndecidedException, UntrustedException) as exn: + print('XmppOmemo.decrypt. except: UndecidedException') + print('XmppOmemo.decrypt. except: UntrustedException') # We received a message from an untrusted device. We can # choose to decrypt the message nonetheless, with the # `allow_untrusted` flag on the `decrypt_message` call, which @@ -98,6 +103,7 @@ class XmppOmemo: # We resend, setting the `allow_untrusted` parameter to True. # await XmppChat.process_message(self, message, allow_untrusted=True) except (EncryptionPrepareException,) as exn: + print('XmppOmemo.decrypt. except: EncryptionPrepareException') # Slixmpp tried its best, but there were errors it couldn't # resolve. At this point you should have seen other exceptions # and given a chance to resolve them already. @@ -107,6 +113,7 @@ class XmppOmemo: retry = False logger.error(exn) except (Exception,) as exn: + print('XmppOmemo.decrypt. except: Exception') response = ('Error: Your message has not been encrypted for ' 'Slixfeed (Unknown).') omemo_decrypted = False @@ -118,9 +125,12 @@ class XmppOmemo: async def encrypt(self, jid: JID, message_body): + print(jid) + print(message_body) expect_problems = {} # type: Optional[Dict[JID, List[int]]] while True: try: + print('XmppOmemo.encrypt') # `encrypt_message` excepts the plaintext to be sent, a list of # bare JIDs to encrypt to, and optionally a dict of problems to # expect per bare JID. @@ -137,6 +147,7 @@ class XmppOmemo: omemo_encrypted = True break except UndecidedException as exn: + print('XmppOmemo.encrypt. except: UndecidedException') # The library prevents us from sending a message to an # untrusted/undecided barejid, so we need to make a decision here. # This is where you prompt your user to ask what to do. In @@ -145,6 +156,7 @@ class XmppOmemo: omemo_encrypted = False # TODO: catch NoEligibleDevicesException except EncryptionPrepareException as exn: + print('XmppOmemo.encrypt. except: EncryptionPrepareException') # This exception is being raised when the library has tried # all it could and doesn't know what to do anymore. It # contains a list of exceptions that the user must resolve, or @@ -168,10 +180,12 @@ class XmppOmemo: device_list = expect_problems.setdefault(jid, []) device_list.append(error.device) except (IqError, IqTimeout) as exn: + print('XmppOmemo.encrypt. except: IqError, IqTimeout') message_body = ('An error occured while fetching information ' 'on a recipient.\n%r' % exn) omemo_encrypted = False except Exception as exn: + print('XmppOmemo.encrypt. except: Exception') message_body = ('An error occured while attempting to encrypt' '.\n%r' % exn) omemo_encrypted = False diff --git a/slixfeed/xmpp/publish.py b/slixfeed/xmpp/publish.py index 0dcff36..8b3cb2c 100644 --- a/slixfeed/xmpp/publish.py +++ b/slixfeed/xmpp/publish.py @@ -19,6 +19,7 @@ from slixfeed.syndication import Feed from slixfeed.utilities import String, Url, Utilities from slixfeed.xmpp.iq import XmppIQ import sys +import time logger = Logger(__name__) @@ -370,12 +371,51 @@ class XmppPubsubAction: await XmppIQ.send(self, iq_create_entry) ix = entry[0] await sqlite.mark_as_read(db_file, ix) + print(report) return report class XmppPubsubTask: + def loop_task(self, jid_bare): + db_file = config.get_pathname_to_database(jid_bare) + if jid_bare not in self.settings: + Config.add_settings_jid(self, jid_bare, db_file) + while True: + if jid_bare not in self.task_manager: + self.task_manager[jid_bare] = {} + logger.info('Creating new task manager for JID {}'.format(jid_bare)) + logger.info('Stopping task "publish" for JID {}'.format(jid_bare)) + try: + self.task_manager[jid_bare]['publish'].cancel() + except: + logger.info('No task "publish" for JID {} (XmppPubsubAction.send_unread_items)' + .format(jid_bare)) + logger.info('Starting tasks "publish" for JID {}'.format(jid_bare)) + self.task_manager[jid_bare]['publish'] = asyncio.create_task( + XmppPubsubAction.send_unread_items(self, jid_bare)) + time.sleep(60 * 180) + + + def restart_task(self, jid_bare): + db_file = config.get_pathname_to_database(jid_bare) + if jid_bare not in self.settings: + Config.add_settings_jid(self, jid_bare, db_file) + if jid_bare not in self.task_manager: + self.task_manager[jid_bare] = {} + logger.info('Creating new task manager for JID {}'.format(jid_bare)) + logger.info('Stopping task "publish" for JID {}'.format(jid_bare)) + try: + self.task_manager[jid_bare]['publish'].cancel() + except: + logger.info('No task "publish" for JID {} (XmppPubsubAction.send_unread_items)' + .format(jid_bare)) + logger.info('Starting tasks "publish" for JID {}'.format(jid_bare)) + self.task_manager[jid_bare]['publish'] = asyncio.create_task( + XmppPubsubAction.send_unread_items(self, jid_bare)) + + async def task_publish(self, jid_bare): db_file = config.get_pathname_to_database(jid_bare) if jid_bare not in self.settings: