patch 6017f51ba8b0e51288cb9ed128f5caa6aa304088 Author: E. Bosch Date: Sun Feb 27 00:47:48 CET 2022 * irc: Add service/control user (TelegramServ), by now without functions diff -rN -u old-irgramd/irc.py new-irgramd/irc.py --- old-irgramd/irc.py 2024-11-22 20:31:21.581010899 +0100 +++ new-irgramd/irc.py 2024-11-22 20:31:21.585010892 +0100 @@ -137,6 +137,9 @@ self.irc_channels_founder = collections.defaultdict(set) self.start_time = time.strftime('%a %d %b %Y %H:%M:%S %z') + 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 async def send_irc_command(self, user, command): self.logger.debug('Send IRC Command: %s', command) @@ -351,10 +354,12 @@ ))) if await self.tg.is_bot(ni): await self.reply_code(user, 'RPL_WHOISBOT', (real_ni,)) - elif usr.tls or not usr.stream: + elif usr.tls or (not usr.stream and not usr.is_service): proto = 'TLS' if usr.tls else 'MTProto' server = self.gethostname(user) if usr.stream else 'Telegram' await self.reply_code(user, 'RPL_WHOISSECURE', (real_ni, proto, server)) + if usr.is_service: + await self.reply_code(user, 'RPL_WHOISSERVICE', (real_ni,)) await self.reply_code(user, 'RPL_ENDOFWHOIS', (real_ni,)) else: await self.reply_code(user, 'ERR_NOSUCHNICK', (nick,)) @@ -373,6 +378,9 @@ self.logger.debug('Handling PRIVMSG: %s, %s', target, message) tgl = target.lower() + if self.service_user.irc_nick.lower() == tgl: + # TODO: handle serivce command + return # Echo channel messages from IRC to other IRC connections # because they won't receive event from Telegram if tgl in self.irc_channels.keys(): @@ -575,7 +583,7 @@ return 'localhost' if user.from_localhost else self.hostname class IRCUser(object): - def __init__(self, stream, address, irc_nick=None, username='', realname=None): + def __init__(self, stream, address, irc_nick=None, username='', realname=None, is_service=False): self.stream = stream self.address = address[0] self.from_localhost = True if address[0].split('.')[0] == '127' else False @@ -588,6 +596,7 @@ self.oper = False self.tls = False self.bot = None + self.is_service = is_service self.close_reason = '' def get_irc_mask(self): diff -rN -u old-irgramd/irc_replies.py new-irgramd/irc_replies.py --- old-irgramd/irc_replies.py 2024-11-22 20:31:21.585010892 +0100 +++ new-irgramd/irc_replies.py 2024-11-22 20:31:21.585010892 +0100 @@ -15,6 +15,7 @@ 'RPL_ISUPPORT': ('005', 'CASEMAPPING=ascii CHANLIMIT=#&+: CHANTYPES=&#+ CHANMODES=,,,nt CHANNELLEN={} NICKLEN={} SAFELIST :are supported by this server'), 'RPL_UMODEIS': ('221', ':{}'), 'RPL_USERHOST': ('302', ':{}'), + 'RPL_WHOISSERVICE': ('310', '{} :is an irgramd service'), 'RPL_WHOISUSER': ('311', '{} {} {} * :{}'), 'RPL_WHOISSERVER': ('312', '{} {} :irgramd gateway'), 'RPL_WHOISOPERATOR': ('313', '{} :is an irgramd operator'), diff -rN -u old-irgramd/irgramd new-irgramd/irgramd --- old-irgramd/irgramd 2024-11-22 20:31:21.585010892 +0100 +++ new-irgramd/irgramd 2024-11-22 20:31:21.589010886 +0100 @@ -78,6 +78,7 @@ tornado.options.define('pam_group', default=None, metavar='GROUP', help='Unix group allowed if `pam` enabled, if empty any user is allowed') tornado.options.define('phone', default=None, metavar='PHONE_NUMBER', help='Phone number associated with the Telegram account to receive the authorization codes if necessary') tornado.options.define('port', default=None, metavar='PORT', help='Port to listen on for IRC. (default 6667, default with TLS 6697)') + tornado.options.define('service_user', default='TelegramServ', metavar='SERVICE_NICK', help='Nick of the service/control user, must be a nick not used by a real Telegram user') tornado.options.define('test', default=False, help='Connect to Telegram test environment') tornado.options.define('test_datacenter', default=2, metavar='DATACENTER_NUMBER', help='Datacenter to connect to Telegram test environment') tornado.options.define('test_host', default=None, metavar='HOST_IP', help='Host to connect to Telegram test environment (default: use a internal table depending on datacenter)') diff -rN -u old-irgramd/telegram.py new-irgramd/telegram.py --- old-irgramd/telegram.py 2024-11-22 20:31:21.585010892 +0100 +++ new-irgramd/telegram.py 2024-11-22 20:31:21.589010886 +0100 @@ -201,6 +201,8 @@ return nicks async def get_telegram_idle(self, irc_nick, tid=None): + if self.irc.users[irc_nick].is_service: + return None tid = self.get_tid(irc_nick, tid) user = await self.telegram_client.get_entity(tid) if isinstance(user.status,tgty.UserStatusRecently) or \ @@ -219,15 +221,16 @@ return idle async def is_bot(self, irc_nick, tid=None): - if self.irc.users[irc_nick].stream: + user = self.irc.users[irc_nick] + if user.stream or user.is_service: bot = False else: - bot = self.irc.users[irc_nick].bot + bot = user.bot if bot == None: tid = self.get_tid(irc_nick, tid) - user = await self.telegram_client.get_entity(tid) - bot = user.bot - self.irc.users[irc_nick].bot = bot + tg_user = await self.telegram_client.get_entity(tid) + bot = tg_user.bot + user.bot = bot return bot async def get_channel_topic(self, channel, entity_cache):