patch 2e88501e2cdbf503df49404f4b4fffeed574658b Author: E. Bosch Date: Sat Mar 19 23:26:56 CET 2022 * service: Add dialog command (only with list subcommand by now) diff -rN -u old-irgramd/irc.py new-irgramd/irc.py --- old-irgramd/irc.py 2024-11-26 08:54:51.445726728 +0100 +++ new-irgramd/irc.py 2024-11-26 08:54:51.449726721 +0100 @@ -380,7 +380,7 @@ tgl = target.lower() if self.service_user.irc_nick.lower() == tgl: - reply = await self.service.parse_command(message) + reply = await self.service.parse_command(message, user.irc_nick) for reply_line in reply: await self.send_msg(self.service_user, None, reply_line, user) return diff -rN -u old-irgramd/service.py new-irgramd/service.py --- old-irgramd/service.py 2024-11-26 08:54:51.449726721 +0100 +++ new-irgramd/service.py 2024-11-26 08:54:51.449726721 +0100 @@ -6,20 +6,25 @@ # Use of this source code is governed by a MIT style license that # can be found in the LICENSE file included in this project. +from utils import compact_date + class service: def __init__(self, settings, telegram): self.commands = \ { # Command Handler Arguments Min Max 'code': (self.handle_command_code, 1, 1), + 'dialog': (self.handle_command_dialog, 1, 2), 'help': (self.handle_command_help, 0, 1), } self.ask_code = settings['ask_code'] self.tg = telegram + self.tmp_ircnick = None - async def parse_command(self, line): + async def parse_command(self, line, nick): words = line.split() command = words.pop(0).lower() + self.tmp_ircnick = nick if command in self.commands.keys(): handler, min_args, max_args = self.commands[command] num_words = len(words) @@ -60,6 +65,46 @@ ) return reply + async def handle_command_dialog(self, command=None, id=None, help=None): + if not help: + if command == 'archive': + pass + elif command == 'delete': + pass + elif command == 'list': + reply = \ + ( + 'Dialogs:', + ' {:<11} {:<9} {:<9} {:4} {:<3} {:<4} {:<6} {}'.format( + 'Id', 'Unread', 'Mentions', 'Type', 'Pin', 'Arch', 'Last', 'Name'), + ) + async for dialog in self.tg.telegram_client.iter_dialogs(): + id = dialog.id + unr = dialog.unread_count + men = dialog.unread_mentions_count + ty = 'User' if dialog.is_user else 'Chat' if dialog.is_group else 'Chan' + pin = 'Yes' if dialog.pinned else 'No' + arch = 'Yes' if dialog.archived else 'No' + last = compact_date(dialog.date) + name_in_irc = self.tmp_ircnick if id == self.tg.id else self.tg.tid_to_iid[id] + reply += (' {:<11d} {:<9d} {:<9d} {:4} {:<3} {:<4} {:<6} {}'.format( + id, unr, men, ty, pin, arch, last, name_in_irc), + ) + + else: # HELP.brief or HELP.desc (first line) + reply = (' dialog Manage conversations (dialogs)',) + if help == HELP.desc: # rest of HELP.desc + reply += \ + ( + ' dialog [id]', + 'Manage conversations (dialogs) established in Telegram, the', + 'following subcommands are available:', + ' archive Archive the dialog specified by id', + ' delete Delete the dialog specified by id', + ' list Show all dialogs', + ) + return reply + async def handle_command_help(self, help_command=None, help=None): start_help = ('*** Telegram Service Help ***',) diff -rN -u old-irgramd/telegram.py new-irgramd/telegram.py --- old-irgramd/telegram.py 2024-11-26 08:54:51.449726721 +0100 +++ new-irgramd/telegram.py 2024-11-26 08:54:51.453726715 +0100 @@ -142,8 +142,8 @@ async def set_irc_channel_from_telegram(self, chat): channel = self.get_telegram_channel(chat) - self.tid_to_iid[chat.id] = channel - self.irc.iid_to_tid[channel.lower()] = chat.id + self.tid_to_iid[-chat.id] = channel + self.irc.iid_to_tid[channel.lower()] = -chat.id chan = channel.lower() # Add users from the channel async for user in self.telegram_client.iter_participants(chat.id): diff -rN -u old-irgramd/utils.py new-irgramd/utils.py --- old-irgramd/utils.py 2024-11-26 08:54:51.449726721 +0100 +++ new-irgramd/utils.py 2024-11-26 08:54:51.453726715 +0100 @@ -10,6 +10,7 @@ import itertools import textwrap import re +import datetime # Constants @@ -108,3 +109,15 @@ if m > 0: res += str(m) + 'm' if s > 0: res += str(s) + 's' return res + +def compact_date(date): + delta = datetime.datetime.now(datetime.timezone.utc) - date + + if delta.days < 1: + compact_date = date.strftime('%H:%M') + elif delta.days < 366: + compact_date = date.strftime('%d-%b') + else: + compact_date = date.strftime('%Y') + + return compact_date