Add exclam module to handle channel/chat commands begining with exclamation mark (!)
patch 5b2c938f7967a3345435fe21b2127999d0975f50
Author: E. Bosch <presidev@AT@gmail.com>
Date: Mon Jun 26 00:17:22 CEST 2023
* Add exclam module to handle channel/chat commands begining with exclamation mark (!)
Implement reply (!re) as first exclam command
addfile ./exclam.py
hunk ./exclam.py 1
+# irgramd: IRC-Telegram gateway
+# exclam.py: IRC exclamation command handlers
+#
+# Copyright (c) 2023 E. Bosch <presidev@AT@gmail.com>
+#
+# 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 command, HELP
+
+class exclam(command):
+ def __init__(self, telegram):
+ self.commands = \
+ { # Command Handler Arguments Min Max Maxsplit
+ '!re': (self.handle_command_re, 2, 2, 2),
+ }
+ self.tg = telegram
+ self.irc = telegram.irc
+ self.tmp_ircnick = None
+ self.tmp_telegram_id = None
+ self.tmp_tg_msg = None
+
+ async def command(self, message, telegram_id, user):
+ self.tmp_telegram_id = telegram_id
+ res = await self.parse_command(message, nick=None)
+ if isinstance(res, tuple):
+ await self.irc.send_msg(self.irc.service_user, None, res[0], user)
+ res = False
+ return res, self.tmp_tg_msg
+
+ async def handle_command_re(self, cid=None, msg=None, help=None):
+ if not help:
+ id = self.tg.mid.id_to_num_offset(self.tmp_telegram_id, cid)
+ chk_msg = await self.tg.telegram_client.get_messages(entity=self.tmp_telegram_id, ids=id)
+ if chk_msg is not None:
+ self.tmp_tg_msg = await self.tg.telegram_client.send_message(self.tmp_telegram_id, msg, reply_to=id)
+ reply = True
+ else:
+ reply = ('Unknown message to reply',)
+ else: # HELP.brief or HELP.desc (first line)
+ reply = (' !re Reply to a message',)
+ if help == HELP.desc: # rest of HELP.desc
+ reply += \
+ (
+ ' !re <compact_id> <message>',
+ 'Reply with <message> to a message with <compact_id> on current',
+ 'channel/chat.'
+ )
+ return reply
hunk ./irc.py 25
+from exclam import exclam
hunk ./irc.py 113
+ self.exclam = exclam(self.tg)
hunk ./irc.py 413
- tg_msg = await self.tg.telegram_client.send_message(telegram_id, message)
-
- mid = self.tg.mid.num_to_id_offset(telegram_id, tg_msg.id)
- text = '[{}] {}'.format(mid, message)
- self.tg.to_cache(tg_msg.id, mid, text, message, user, chan, media=None)
-
- if defered_send:
- await defered_send(user, defered_target, text)
+ if message[0] == '!':
+ cont, tg_msg = await self.exclam.command(message, telegram_id, user)
+ else:
+ tg_msg = await self.tg.telegram_client.send_message(telegram_id, message)
+ cont = True
+ if cont:
+ mid = self.tg.mid.num_to_id_offset(telegram_id, tg_msg.id)
+ text = '[{}] {}'.format(mid, message)
+ self.tg.to_cache(tg_msg.id, mid, text, message, user, chan, media=None)
+
+ if defered_send:
+ await defered_send(user, defered_target, text)
hunk ./service.py 9
-from utils import compact_date, command
+from utils import compact_date, command, HELP
hunk ./service.py 15
- { # Command Handler Arguments Min Max
- 'code': (self.handle_command_code, 1, 1),
- 'dialog': (self.handle_command_dialog, 1, 2),
- 'get': (self.handle_command_get, 2, 2),
- 'help': (self.handle_command_help, 0, 1),
- 'history': (self.handle_command_history, 1, 3),
- 'mark_read': (self.handle_command_mark_read, 1, 1),
+ { # Command Handler Arguments Min Max Maxsplit
+ 'code': (self.handle_command_code, 1, 1, -1),
+ 'dialog': (self.handle_command_dialog, 1, 2, -1),
+ 'get': (self.handle_command_get, 2, 2, -1),
+ 'help': (self.handle_command_help, 0, 1, -1),
+ 'history': (self.handle_command_history, 1, 3, -1),
+ 'mark_read': (self.handle_command_mark_read, 1, 1, -1),
hunk ./service.py 147
+ 'The commands begining with ! (exclamation) must be used directly',
+ 'in channels or chats. The following ! commands are available:',
+ )
+ for command in self.irc.exclam.commands.values():
+ handler = command[0]
+ help_text += await handler(help=HELP.brief)
+ help_text += \
+ (
hunk ./service.py 159
- elif help_command in self.commands.keys():
- handler = self.commands[help_command][0]
+ elif help_command in (all_commands := dict(**self.commands, **self.irc.exclam.commands)).keys():
+ handler = all_commands[help_command][0]
hunk ./service.py 261
-
-class HELP:
- desc = 1
- brief = 2
hunk ./utils.py 26
- words = line.split()
- command = words.pop(0).lower()
+ command = line.partition(' ')[0].lower()
hunk ./utils.py 29
- handler, min_args, max_args = self.commands[command]
+ handler, min_args, max_args, maxsplit = self.commands[command]
+ words = line.split(maxsplit=maxsplit)[1:]
hunk ./utils.py 41
+class HELP:
+ desc = 1
+ brief = 2
+