patch 3ec451218b80bdd3c409b4aec91ae94ef4fa5c38 Author: E. Bosch Date: Thu Jun 15 20:08:03 CEST 2023 * telegram, irc: Add conversion of "mention:" (IRC style) to "@mention" in sent messages diff -rN -u old-irgramd/irc.py new-irgramd/irc.py --- old-irgramd/irc.py 2024-11-22 19:54:18.872517068 +0100 +++ new-irgramd/irc.py 2024-11-22 19:54:18.876517062 +0100 @@ -406,6 +406,7 @@ tgt = tgl if tgt in self.iid_to_tid: + message = self.tg.replace_mentions(message, me_nick='', received=False) telegram_id = self.iid_to_tid[tgt] tg_msg = await self.tg.telegram_client.send_message(telegram_id, message) diff -rN -u old-irgramd/telegram.py new-irgramd/telegram.py --- old-irgramd/telegram.py 2024-11-22 19:54:18.872517068 +0100 +++ new-irgramd/telegram.py 2024-11-22 19:54:18.876517062 +0100 @@ -395,22 +395,40 @@ 'media': media } - def replace_mentions(self, text, me_nick=''): - def repl_mentioned(text, me_nick): - if text and text[0] == '@': - part = text[1:].lower() + def replace_mentions(self, text, me_nick='', received=True): + # For received replace @mention to ~mention~ + # For sent replace mention: to @mention + rargs = {} + def repl_mentioned(text, me_nick, received, mark, index, rargs): + if text and text[index] == mark: + if received: + subtext = text[1:] + else: + subtext = text[:-1] + part = subtext.lower() if me_nick and part == self.tg_username: - return replacement(me_nick) + return replacement(me_nick, **rargs) if part in self.irc.users: - return replacement(self.irc.users[part].irc_nick) + return replacement(self.irc.users[part].irc_nick, **rargs) return text - def replacement(nick): - return '{}{}{}'.format('~',nick, '~') + def replacement(nick, repl_pref, repl_suff): + return '{}{}{}'.format(repl_pref, nick, repl_suff) - if text.find('@') != -1: + if received: + mark = '@' + index = 0 + rargs['repl_pref'] = '~' + rargs['repl_suff'] = '~' + else: + mark = ':' + index = -1 + rargs['repl_pref'] = '@' + rargs['repl_suff'] = '' + + if text.find(mark) != -1: words = text.split(' ') - words_replaced = [repl_mentioned(elem, me_nick) for elem in words] + words_replaced = [repl_mentioned(elem, me_nick, received, mark, index, rargs) for elem in words] text_replaced = ' '.join(words_replaced) else: text_replaced = text