Improve alias handling.

This commit is contained in:
Schimon Jehudah, Adv. 2024-11-19 19:23:47 +02:00
parent 412acc7cbe
commit d6dfbc339e

View file

@ -66,7 +66,8 @@ logger = Logger(__name__)
class XmppChat: class XmppChat:
async def process_message(self, message: Message, allow_untrusted: bool = False) -> None: async def process_message(
self, message: Message, allow_untrusted: bool = False) -> None:
""" """
Process incoming message stanzas. Be aware that this also Process incoming message stanzas. Be aware that this also
includes MUC messages and error messages. It is usually includes MUC messages and error messages. It is usually
@ -84,88 +85,35 @@ class XmppChat:
message_type = message['type'] message_type = message['type']
if message_type in ('chat', 'groupchat', 'normal'): if message_type in ('chat', 'groupchat', 'normal'):
jid_bare = message_from.bare jid_bare = message_from.bare
command = ' '.join(message['body'].split()) message_body = message['body']
command = ' '.join(message_body.split())
command_time_start = time.time() command_time_start = time.time()
# if (message_type == 'groupchat' and
# message['muc']['nick'] == self.alias):
# return
# FIXME Code repetition. See below.
# TODO Check alias by nickname associated with conference
if message_type == 'groupchat':
alias = message['muc']['nick']
if (alias == self.alias):
return
if not XmppUtilities.is_moderator(self, jid_bare, alias):
return
# nick = message['from'][message['from'].index('/')+1:]
# nick = str(message['from'])
# nick = nick[nick.index('/')+1:]
alias_of_slixfeed = XmppUtilities.get_self_alias(self, jid_bare)
if (alias == self.alias or
not message['body'].startswith(alias_of_slixfeed)):
return
# token = await initdb(
# jid_bare,
# sqlite.get_setting_value,
# 'token'
# )
# if token == 'accepted':
# operator = await initdb(
# jid_bare,
# sqlite.get_setting_value,
# 'masters'
# )
# if operator:
# if nick not in operator:
# return
# approved = False
if not XmppUtilities.is_moderator(self, jid_bare, alias):
return
# if role == 'moderator':
# approved = True
# TODO Implement a list of temporary operators
# Once an operator is appointed, the control would last
# untile the participant has been disconnected from MUC
# An operator is a function to appoint non moderators.
# Changing nickname is fine and consist of no problem.
# if not approved:
# operator = await initdb(
# jid_bare,
# sqlite.get_setting_value,
# 'masters'
# )
# if operator:
# if nick in operator:
# approved = True
# if not approved:
# return
# # Begin processing new JID
# # Deprecated in favour of event 'presence_available'
# db_dir = config.get_default_data_directory()
# os.chdir(db_dir)
# if jid + '.db' not in os.listdir():
# await task_jid(jid)
# await compose.message(self, jid_bare, message)
if self.omemo_present and self['xep_0384'].is_encrypted(message): if self.omemo_present and self['xep_0384'].is_encrypted(message):
command, omemo_decrypted = await XmppOmemo.decrypt( command, omemo_decrypted = await XmppOmemo.decrypt(
self, message) self, message)
else: else:
omemo_decrypted = None omemo_decrypted = None
# FIXME Code repetition. See below.
if message_type == 'groupchat': if message_type == 'groupchat':
alias = message['muc']['nick']
self_alias = XmppUtilities.get_self_alias(self, jid_bare)
if (alias == self_alias or
not XmppUtilities.is_moderator(self, jid_bare, alias) or
(not message_body.startswith(self_alias + ' ') and
not message_body.startswith(self_alias + ',') and
not message_body.startswith(self_alias + ':'))):
return
# Adding one to the length because of # Adding one to the length because of
# assumption that a comma or a dot is added # assumption that a comma or a dot is added
alias_of_slixfeed_length = len(alias_of_slixfeed) + 1 self_alias_length = len(self_alias) + 1
command = command[alias_of_slixfeed_length:].lstrip() command = command[self_alias_length:].lstrip()
if isinstance(command, Message): command = command['body'] if isinstance(command, Message): command = command['body']
command_lowercase = command.lower() command_lowercase = command.lower()
# This is a work-around to empty messages that are caused by function # This is a work-around to empty messages that are caused by function
# self.register_handler(CoroutineCallback( of module client.py. # self.register_handler(CoroutineCallback( of module client.py.
# The code was taken from the cho bot xample of slixmpp-omemo. # The code was taken from the cho bot xample of slixmpp-omemo.
@ -192,7 +140,8 @@ class XmppChat:
'Usage: `help <key>`' 'Usage: `help <key>`'
.format(command_list)) .format(command_list))
case 'help all': case 'help all':
command_list = Documentation.manual('commands.toml', section='all') command_list = Documentation.manual(
'commands.toml', section='all')
response = ('Complete list of commands:\n' response = ('Complete list of commands:\n'
'```\n{}\n```' '```\n{}\n```'
.format(command_list)) .format(command_list))
@ -203,7 +152,8 @@ class XmppChat:
command_root = command[0] command_root = command[0]
command_name = command[1] command_name = command[1]
command_list = Documentation.manual( command_list = Documentation.manual(
'commands.toml', section=command_root, command=command_name) 'commands.toml', section=command_root,
command=command_name)
if command_list: if command_list:
command_list = ''.join(command_list) command_list = ''.join(command_list)
response = (command_list) response = (command_list)
@ -212,7 +162,8 @@ class XmppChat:
.format(command_root, command_name)) .format(command_root, command_name))
elif len(command) == 1: elif len(command) == 1:
command = command[0] command = command[0]
command_list = Documentation.manual('commands.toml', command) command_list = Documentation.manual(
'commands.toml', command)
if command_list: if command_list:
command_list = ' '.join(command_list) command_list = ' '.join(command_list)
response = ('Available command `{}` keys:\n' response = ('Available command `{}` keys:\n'
@ -747,7 +698,8 @@ class XmppChatAction:
if enclosure: enclosure = enclosure[0] if enclosure: enclosure = enclosure[0]
title_f = sqlite.get_feed_title(db_file, feed_id) title_f = sqlite.get_feed_title(db_file, feed_id)
title_f = title_f[0] title_f = title_f[0]
news_digest += await XmppChatAction.list_unread_entries(self, result, title_f, jid_bare) news_digest += await XmppChatAction.list_unread_entries(
self, result, title_f, jid_bare)
# print(db_file) # print(db_file)
# print(result[0]) # print(result[0])
# breakpoint() # breakpoint()
@ -781,7 +733,8 @@ class XmppChatAction:
news_digest_encrypted, omemo_encrypted = await XmppOmemo.encrypt( news_digest_encrypted, omemo_encrypted = await XmppOmemo.encrypt(
self, jid, 'chat', news_digest) self, jid, 'chat', news_digest)
if self.omemo_present and encrypt_omemo and omemo_encrypted: if self.omemo_present and encrypt_omemo and omemo_encrypted:
XmppMessage.send_omemo(self, jid, chat_type, news_digest_encrypted) XmppMessage.send_omemo(
self, jid, chat_type, news_digest_encrypted)
else: else:
# Send textual message # Send textual message
XmppMessage.send(self, jid_bare, news_digest, chat_type) XmppMessage.send(self, jid_bare, news_digest, chat_type)
@ -800,7 +753,8 @@ class XmppChatAction:
if status: if status:
filesize = os.path.getsize(pathname) filesize = os.path.getsize(pathname)
media_url_new = await XmppUpload.start( media_url_new = await XmppUpload.start(
self, jid_bare, Path(pathname), filesize, encrypted=encrypted) self, jid_bare, Path(pathname), filesize,
encrypted=encrypted)
else: else:
media_url_new = media_url media_url_new = media_url
else: else:
@ -825,7 +779,8 @@ class XmppChatAction:
# not work with http URLs. # not work with http URLs.
# url = saxutils.escape(url) # url = saxutils.escape(url)
# AttributeError: 'Encrypted' object has no attribute 'replace' # AttributeError: 'Encrypted' object has no attribute 'replace'
XmppMessage.send_omemo_oob(self, jid, media_url_new_encrypted, chat_type) XmppMessage.send_omemo_oob(
self, jid, media_url_new_encrypted, chat_type)
else: else:
# NOTE Tested against Gajim. # NOTE Tested against Gajim.
# FIXME Jandle data: URIs. # FIXME Jandle data: URIs.
@ -844,7 +799,8 @@ class XmppChatAction:
news_digest_encrypted, omemo_encrypted = await XmppOmemo.encrypt( news_digest_encrypted, omemo_encrypted = await XmppOmemo.encrypt(
self, jid, 'chat', news_digest) self, jid, 'chat', news_digest)
if self.omemo_present and encrypt_omemo and omemo_encrypted: if self.omemo_present and encrypt_omemo and omemo_encrypted:
XmppMessage.send_omemo(self, jid, chat_type, news_digest_encrypted) XmppMessage.send_omemo(
self, jid, chat_type, news_digest_encrypted)
else: else:
XmppMessage.send(self, jid_bare, news_digest, chat_type) XmppMessage.send(self, jid_bare, news_digest, chat_type)
# TODO Add while loop to assure delivery. # TODO Add while loop to assure delivery.
@ -964,7 +920,8 @@ class XmppChatTask:
if jid_bare not in self.settings: if jid_bare not in self.settings:
Config.add_settings_jid(self, jid_bare, db_file) Config.add_settings_jid(self, jid_bare, db_file)
while True: while True:
update_interval = Config.get_setting_value(self, jid_bare, 'interval') update_interval = Config.get_setting_value(
self, jid_bare, 'interval')
update_interval = 60 * int(update_interval) update_interval = 60 * int(update_interval)
last_update_time = sqlite.get_last_update_time(db_file) last_update_time = sqlite.get_last_update_time(db_file)
if last_update_time: if last_update_time: