Add error handling upon failure to to join to a groupchat (Thanks f_ from postmarketOS)

This commit is contained in:
Schimon Jehudah 2024-05-12 16:25:21 +00:00
parent 06002be363
commit 57e0425c13
5 changed files with 83 additions and 33 deletions

View file

@ -127,7 +127,13 @@ async def xmpp_muc_autojoin(self, bookmarks):
'bookmark {}'.format(bookmark['name'])) 'bookmark {}'.format(bookmark['name']))
alias = bookmark["nick"] alias = bookmark["nick"]
muc_jid = bookmark["jid"] muc_jid = bookmark["jid"]
await XmppGroupchat.join(self, muc_jid, alias) 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' logger.info('Autojoin groupchat\n'
'Name : {}\n' 'Name : {}\n'
'JID : {}\n' 'JID : {}\n'

View file

@ -1,2 +1,2 @@
__version__ = '0.1.60' __version__ = '0.1.61'
__version_info__ = (0, 1, 60) __version_info__ = (0, 1, 61)

View file

@ -940,8 +940,13 @@ class Chat:
muc_jid = uri.check_xmpp_uri(message_text[5:]) muc_jid = uri.check_xmpp_uri(message_text[5:])
if muc_jid: if muc_jid:
# TODO probe JID and confirm it's a groupchat # 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) # await XmppBookmark.add(self, jid=muc_jid)
if result == 'ban':
response = ('{} is banned from {}'
.format(self.alias, muc_jid))
else:
await XmppBookmark.add(self, muc_jid)
response = ('Joined groupchat {}' response = ('Joined groupchat {}'
.format(message_text)) .format(message_text))
else: else:

View file

@ -215,8 +215,24 @@ class Slixfeed(slixmpp.ClientXMPP):
message_log = '{}: jid_full: {}' message_log = '{}: jid_full: {}'
logger.debug(message_log.format(function_name, jid_full)) logger.debug(message_log.format(function_name, jid_full))
muc_jid = message['groupchat_invite']['jid'] muc_jid = message['groupchat_invite']['jid']
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) await XmppBookmark.add(self, muc_jid)
await XmppGroupchat.join(self, muc_jid)
message_body = ('Greetings! I am {}, the news anchor.\n' message_body = ('Greetings! I am {}, the news anchor.\n'
'My job is to bring you the latest ' 'My job is to bring you the latest '
'news from sources you provide me with.\n' 'news from sources you provide me with.\n'
@ -237,8 +253,14 @@ class Slixfeed(slixmpp.ClientXMPP):
message_log = '{}: jid_full: {}' message_log = '{}: jid_full: {}'
logger.debug(message_log.format(function_name, jid_full)) logger.debug(message_log.format(function_name, jid_full))
muc_jid = message['groupchat_invite']['jid'] muc_jid = message['groupchat_invite']['jid']
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) await XmppBookmark.add(self, muc_jid)
await XmppGroupchat.join(self, muc_jid)
message_body = ('Greetings! I am {}, the news anchor.\n' message_body = ('Greetings! I am {}, the news anchor.\n'
'My job is to bring you the latest ' 'My job is to bring you the latest '
'news from sources you provide me with.\n' 'news from sources you provide me with.\n'

View file

@ -17,6 +17,7 @@ FIXME
""" """
import logging import logging
from slixmpp.exceptions import IqError, IqTimeout, PresenceError
class XmppGroupchat: class XmppGroupchat:
@ -45,10 +46,26 @@ class XmppGroupchat:
.format(jid)) .format(jid))
jid_from = str(self.boundjid) if self.is_component else None jid_from = str(self.boundjid) if self.is_component else None
if alias == None: self.alias if alias == None: self.alias
try:
await self.plugin['xep_0045'].join_muc_wait(jid, await self.plugin['xep_0045'].join_muc_wait(jid,
alias, alias,
presence_options = {"pfrom" : jid_from}, presence_options = {"pfrom" : jid_from},
password=password) 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): def leave(self, jid):