patch 2f1d894e6e9861771f4330aadef00d20279d4525 Author: E. Bosch Date: Wed Mar 2 20:18:03 CET 2022 * irc: Add class for service/control command parsing and handlers, by now with stub help command diff -rN -u old-irgramd/irc.py new-irgramd/irc.py --- old-irgramd/irc.py 2024-11-22 21:37:13.622732527 +0100 +++ new-irgramd/irc.py 2024-11-22 21:37:13.626732521 +0100 @@ -21,6 +21,7 @@ from include import VERSION, CHAN_MAX_LENGHT, NICK_MAX_LENGTH from irc_replies import irc_codes from utils import chunks, set_replace, split_lines +from service import service # Constants @@ -141,6 +142,7 @@ self.service_user = IRCUser(None, ('Services.{}'.format(self.hostname),), self.conf['service_user'], 'Control', 'Telegram Service', is_service=True) self.users[self.conf['service_user'].lower()] = self.service_user + self.service = service() async def send_irc_command(self, user, command): self.logger.debug('Send IRC Command: %s', command) @@ -380,7 +382,8 @@ tgl = target.lower() if self.service_user.irc_nick.lower() == tgl: - # TODO: handle serivce command + reply = self.service.parse_command(message) + await self.send_msg(self.service_user, user.irc_nick, reply) return # Echo channel messages from IRC to other IRC connections # because they won't receive event from Telegram diff -rN -u old-irgramd/service.py new-irgramd/service.py --- old-irgramd/service.py 1970-01-01 01:00:00.000000000 +0100 +++ new-irgramd/service.py 2024-11-22 21:37:13.626732521 +0100 @@ -0,0 +1,34 @@ +# irgramd: IRC-Telegram gateway +# service.py: IRC service/control command handlers +# +# Copyright (c) 2022 E. Bosch +# +# Use of this source code is governed by a MIT style license that +# can be found in the LICENSE file included in this project. + +class service: + def __init__(self): + self.commands = \ + { + # Command Handler Arguments Min Max + 'help': (self.handle_command_help, 0, 0), + } + + def parse_command(self, line): + + words = line.split() + command = words.pop(0).lower() + if command in self.commands.keys(): + handler, min_args, max_args = self.commands[command] + num_words = len(words) + if num_words < min_args or num_words > max_args: + reply = 'Wrong number of arguments' + else: + reply = handler(*words) + else: + reply = 'Unknown command' + + return reply + + def handle_command_help(self): + return 'help'