From 57e0425c13b593233a07726a6e4d56633bf73bb1 Mon Sep 17 00:00:00 2001 From: Schimon Jehudah Date: Sun, 12 May 2024 16:25:21 +0000 Subject: [PATCH] Add error handling upon failure to to join to a groupchat (Thanks f_ from postmarketOS) --- slixfeed/action.py | 22 +++++++++++------ slixfeed/version.py | 4 +-- slixfeed/xmpp/chat.py | 11 ++++++--- slixfeed/xmpp/client.py | 54 +++++++++++++++++++++++++++++------------ slixfeed/xmpp/muc.py | 25 ++++++++++++++++--- 5 files changed, 83 insertions(+), 33 deletions(-) diff --git a/slixfeed/action.py b/slixfeed/action.py index 0b5a7cf..320071c 100644 --- a/slixfeed/action.py +++ b/slixfeed/action.py @@ -127,14 +127,20 @@ async def xmpp_muc_autojoin(self, bookmarks): 'bookmark {}'.format(bookmark['name'])) alias = bookmark["nick"] muc_jid = bookmark["jid"] - await XmppGroupchat.join(self, muc_jid, alias) - logger.info('Autojoin groupchat\n' - 'Name : {}\n' - 'JID : {}\n' - 'Alias : {}\n' - .format(bookmark["name"], - bookmark["jid"], - bookmark["nick"])) + result = await XmppGroupchat.join(self, muc_jid, alias) + if result == 'ban': + await XmppBookmark.remove(self, muc_jid) + logger.warning('{} is banned from {}'.format(self.alias, muc_jid)) + logger.warning('Groupchat {} has been removed from bookmarks' + .format(muc_jid)) + else: + logger.info('Autojoin groupchat\n' + 'Name : {}\n' + 'JID : {}\n' + 'Alias : {}\n' + .format(bookmark["name"], + bookmark["jid"], + bookmark["nick"])) elif not bookmark["jid"]: logger.error('JID is missing for bookmark {}' .format(bookmark['name'])) diff --git a/slixfeed/version.py b/slixfeed/version.py index 4cdc574..3d5447b 100644 --- a/slixfeed/version.py +++ b/slixfeed/version.py @@ -1,2 +1,2 @@ -__version__ = '0.1.60' -__version_info__ = (0, 1, 60) +__version__ = '0.1.61' +__version_info__ = (0, 1, 61) diff --git a/slixfeed/xmpp/chat.py b/slixfeed/xmpp/chat.py index f3302af..ad658d8 100644 --- a/slixfeed/xmpp/chat.py +++ b/slixfeed/xmpp/chat.py @@ -940,10 +940,15 @@ class Chat: muc_jid = uri.check_xmpp_uri(message_text[5:]) if muc_jid: # TODO probe JID and confirm it's a groupchat - XmppGroupchat.join(self, muc_jid) + result = await XmppGroupchat.join(self, muc_jid) # await XmppBookmark.add(self, jid=muc_jid) - response = ('Joined groupchat {}' - .format(message_text)) + if result == 'ban': + response = ('{} is banned from {}' + .format(self.alias, muc_jid)) + else: + await XmppBookmark.add(self, muc_jid) + response = ('Joined groupchat {}' + .format(message_text)) else: response = ('> {}\n' 'XMPP URI is not valid.' diff --git a/slixfeed/xmpp/client.py b/slixfeed/xmpp/client.py index d92ac3a..2eb056a 100644 --- a/slixfeed/xmpp/client.py +++ b/slixfeed/xmpp/client.py @@ -215,14 +215,30 @@ class Slixfeed(slixmpp.ClientXMPP): message_log = '{}: jid_full: {}' logger.debug(message_log.format(function_name, jid_full)) muc_jid = message['groupchat_invite']['jid'] - await XmppBookmark.add(self, muc_jid) - await XmppGroupchat.join(self, muc_jid) - message_body = ('Greetings! I am {}, the news anchor.\n' - 'My job is to bring you the latest ' - 'news from sources you provide me with.\n' - 'You may always reach me via xmpp:{}?message' - .format(self.alias, self.boundjid.bare)) - XmppMessage.send(self, muc_jid, message_body, 'groupchat') + result = await XmppGroupchat.join(self, muc_jid) + if result == 'ban': + message_body = '{} is banned from {}'.format(self.alias, muc_jid) + jid_bare = message['from'].bare + # This might not be necessary because JID might not be of the inviter, but rather of the MUC + XmppMessage.send(self, jid_bare, message_body, 'chat') + logger.warning(message_body) + print("on_groupchat_invite") + print("on_groupchat_invite") + print("on_groupchat_invite") + print(jid_full) + print(jid_full) + print(jid_full) + print("on_groupchat_invite") + print("on_groupchat_invite") + print("on_groupchat_invite") + else: + await XmppBookmark.add(self, muc_jid) + message_body = ('Greetings! I am {}, the news anchor.\n' + 'My job is to bring you the latest ' + 'news from sources you provide me with.\n' + 'You may always reach me via xmpp:{}?message' + .format(self.alias, self.boundjid.bare)) + XmppMessage.send(self, muc_jid, message_body, 'groupchat') time_end = time.time() difference = time_end - time_begin if difference > 1: logger.warning('{} (time: {})'.format(function_name, @@ -237,14 +253,20 @@ class Slixfeed(slixmpp.ClientXMPP): message_log = '{}: jid_full: {}' logger.debug(message_log.format(function_name, jid_full)) muc_jid = message['groupchat_invite']['jid'] - await XmppBookmark.add(self, muc_jid) - await XmppGroupchat.join(self, muc_jid) - message_body = ('Greetings! I am {}, the news anchor.\n' - 'My job is to bring you the latest ' - 'news from sources you provide me with.\n' - 'You may always reach me via xmpp:{}?message' - .format(self.alias, self.boundjid.bare)) - XmppMessage.send(self, muc_jid, message_body, 'groupchat') + result = await XmppGroupchat.join(self, muc_jid) + if result == 'ban': + message_body = '{} is banned from {}'.format(self.alias, muc_jid) + jid_bare = message['from'].bare + XmppMessage.send(self, jid_bare, message_body, 'chat') + logger.warning(message_body) + else: + await XmppBookmark.add(self, muc_jid) + message_body = ('Greetings! I am {}, the news anchor.\n' + 'My job is to bring you the latest ' + 'news from sources you provide me with.\n' + 'You may always reach me via xmpp:{}?message' + .format(self.alias, self.boundjid.bare)) + XmppMessage.send(self, muc_jid, message_body, 'groupchat') time_end = time.time() difference = time_end - time_begin if difference > 1: logger.warning('{} (time: {})'.format(function_name, diff --git a/slixfeed/xmpp/muc.py b/slixfeed/xmpp/muc.py index 09ecfad..b70f2c2 100644 --- a/slixfeed/xmpp/muc.py +++ b/slixfeed/xmpp/muc.py @@ -17,6 +17,7 @@ FIXME """ import logging +from slixmpp.exceptions import IqError, IqTimeout, PresenceError class XmppGroupchat: @@ -45,10 +46,26 @@ class XmppGroupchat: .format(jid)) jid_from = str(self.boundjid) if self.is_component else None if alias == None: self.alias - await self.plugin['xep_0045'].join_muc_wait(jid, - alias, - presence_options = {"pfrom" : jid_from}, - password=password) + try: + await self.plugin['xep_0045'].join_muc_wait(jid, + alias, + presence_options = {"pfrom" : jid_from}, + password=password) + except IqError as e: + logging.error('Error XmppIQ') + logging.error(str(e)) + logging.error(jid) + except IqTimeout as e: + logging.error('Timeout XmppIQ') + logging.error(str(e)) + logging.error(jid) + except PresenceError as e: + logging.error('Error Presence') + logging.error(str(e)) + if (e.condition == 'forbidden' and + e.presence['error']['code'] == '403'): + logging.warning('{} is banned from {}'.format(self.alias, jid)) + return 'ban' def leave(self, jid):