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,14 +127,20 @@ 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)
logger.info('Autojoin groupchat\n' if result == 'ban':
'Name : {}\n' await XmppBookmark.remove(self, muc_jid)
'JID : {}\n' logger.warning('{} is banned from {}'.format(self.alias, muc_jid))
'Alias : {}\n' logger.warning('Groupchat {} has been removed from bookmarks'
.format(bookmark["name"], .format(muc_jid))
bookmark["jid"], else:
bookmark["nick"])) logger.info('Autojoin groupchat\n'
'Name : {}\n'
'JID : {}\n'
'Alias : {}\n'
.format(bookmark["name"],
bookmark["jid"],
bookmark["nick"]))
elif not bookmark["jid"]: elif not bookmark["jid"]:
logger.error('JID is missing for bookmark {}' logger.error('JID is missing for bookmark {}'
.format(bookmark['name'])) .format(bookmark['name']))

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,10 +940,15 @@ 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)
response = ('Joined groupchat {}' if result == 'ban':
.format(message_text)) response = ('{} is banned from {}'
.format(self.alias, muc_jid))
else:
await XmppBookmark.add(self, muc_jid)
response = ('Joined groupchat {}'
.format(message_text))
else: else:
response = ('> {}\n' response = ('> {}\n'
'XMPP URI is not valid.' 'XMPP URI is not valid.'

View file

@ -215,14 +215,30 @@ 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']
await XmppBookmark.add(self, muc_jid) result = await XmppGroupchat.join(self, muc_jid)
await XmppGroupchat.join(self, muc_jid) if result == 'ban':
message_body = ('Greetings! I am {}, the news anchor.\n' message_body = '{} is banned from {}'.format(self.alias, muc_jid)
'My job is to bring you the latest ' jid_bare = message['from'].bare
'news from sources you provide me with.\n' # This might not be necessary because JID might not be of the inviter, but rather of the MUC
'You may always reach me via xmpp:{}?message' XmppMessage.send(self, jid_bare, message_body, 'chat')
.format(self.alias, self.boundjid.bare)) logger.warning(message_body)
XmppMessage.send(self, muc_jid, message_body, 'groupchat') 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() time_end = time.time()
difference = time_end - time_begin difference = time_end - time_begin
if difference > 1: logger.warning('{} (time: {})'.format(function_name, if difference > 1: logger.warning('{} (time: {})'.format(function_name,
@ -237,14 +253,20 @@ 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']
await XmppBookmark.add(self, muc_jid) result = await XmppGroupchat.join(self, muc_jid)
await XmppGroupchat.join(self, muc_jid) if result == 'ban':
message_body = ('Greetings! I am {}, the news anchor.\n' message_body = '{} is banned from {}'.format(self.alias, muc_jid)
'My job is to bring you the latest ' jid_bare = message['from'].bare
'news from sources you provide me with.\n' XmppMessage.send(self, jid_bare, message_body, 'chat')
'You may always reach me via xmpp:{}?message' logger.warning(message_body)
.format(self.alias, self.boundjid.bare)) else:
XmppMessage.send(self, muc_jid, message_body, 'groupchat') 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() time_end = time.time()
difference = time_end - time_begin difference = time_end - time_begin
if difference > 1: logger.warning('{} (time: {})'.format(function_name, if difference > 1: logger.warning('{} (time: {})'.format(function_name,

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
await self.plugin['xep_0045'].join_muc_wait(jid, try:
alias, await self.plugin['xep_0045'].join_muc_wait(jid,
presence_options = {"pfrom" : jid_from}, alias,
password=password) 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): def leave(self, jid):