diff --git a/slixfeed/xmpp/chat.py b/slixfeed/xmpp/chat.py index f062e7f..312a32c 100644 --- a/slixfeed/xmpp/chat.py +++ b/slixfeed/xmpp/chat.py @@ -66,7 +66,8 @@ logger = Logger(__name__) 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 includes MUC messages and error messages. It is usually @@ -84,88 +85,35 @@ class XmppChat: message_type = message['type'] if message_type in ('chat', 'groupchat', 'normal'): jid_bare = message_from.bare - command = ' '.join(message['body'].split()) + message_body = message['body'] + command = ' '.join(message_body.split()) 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): command, omemo_decrypted = await XmppOmemo.decrypt( self, message) else: omemo_decrypted = None + # FIXME Code repetition. See below. 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 # assumption that a comma or a dot is added - alias_of_slixfeed_length = len(alias_of_slixfeed) + 1 - command = command[alias_of_slixfeed_length:].lstrip() + self_alias_length = len(self_alias) + 1 + command = command[self_alias_length:].lstrip() + if isinstance(command, Message): command = command['body'] - command_lowercase = command.lower() - # This is a work-around to empty messages that are caused by function # self.register_handler(CoroutineCallback( of module client.py. # The code was taken from the cho bot xample of slixmpp-omemo. @@ -192,7 +140,8 @@ class XmppChat: 'Usage: `help `' .format(command_list)) 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' '```\n{}\n```' .format(command_list)) @@ -203,7 +152,8 @@ class XmppChat: command_root = command[0] command_name = command[1] command_list = Documentation.manual( - 'commands.toml', section=command_root, command=command_name) + 'commands.toml', section=command_root, + command=command_name) if command_list: command_list = ''.join(command_list) response = (command_list) @@ -212,7 +162,8 @@ class XmppChat: .format(command_root, command_name)) elif len(command) == 1: command = command[0] - command_list = Documentation.manual('commands.toml', command) + command_list = Documentation.manual( + 'commands.toml', command) if command_list: command_list = ' '.join(command_list) response = ('Available command `{}` keys:\n' @@ -747,7 +698,8 @@ class XmppChatAction: if enclosure: enclosure = enclosure[0] title_f = sqlite.get_feed_title(db_file, feed_id) 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(result[0]) # breakpoint() @@ -781,7 +733,8 @@ class XmppChatAction: news_digest_encrypted, omemo_encrypted = await XmppOmemo.encrypt( self, jid, 'chat', news_digest) 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: # Send textual message XmppMessage.send(self, jid_bare, news_digest, chat_type) @@ -800,7 +753,8 @@ class XmppChatAction: if status: filesize = os.path.getsize(pathname) media_url_new = await XmppUpload.start( - self, jid_bare, Path(pathname), filesize, encrypted=encrypted) + self, jid_bare, Path(pathname), filesize, + encrypted=encrypted) else: media_url_new = media_url else: @@ -825,7 +779,8 @@ class XmppChatAction: # not work with http URLs. # url = saxutils.escape(url) # 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: # NOTE Tested against Gajim. # FIXME Jandle data: URIs. @@ -844,7 +799,8 @@ class XmppChatAction: news_digest_encrypted, omemo_encrypted = await XmppOmemo.encrypt( self, jid, 'chat', news_digest) 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: XmppMessage.send(self, jid_bare, news_digest, chat_type) # TODO Add while loop to assure delivery. @@ -964,7 +920,8 @@ class XmppChatTask: if jid_bare not in self.settings: Config.add_settings_jid(self, jid_bare, db_file) 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) last_update_time = sqlite.get_last_update_time(db_file) if last_update_time: