patch c154ded080ea797bb2188ea8318b54f7127eeb91 Author: E. Bosch Date: Sun Nov 22 23:57:38 CET 2020 * Change the point where irgramd connects to Telegram Before: it connected to Telegram on connection from IRC Now: it connects to Telegram on irgramd startup and keeps connected even if there is no user connected to IRC so works more like a bouncer The autojoin function is disabled and join command implemented diff -rN -u old-irgramd/irc.py new-irgramd/irc.py --- old-irgramd/irc.py 2024-11-23 02:01:53.833462849 +0100 +++ new-irgramd/irc.py 2024-11-23 02:01:53.833462849 +0100 @@ -10,7 +10,6 @@ # Local modules from utils import chunks -from telegram import TelegramHandler # Configuration @@ -23,25 +22,25 @@ IRC_PING_RX = re.compile(r'PING (?P[^\n\r]+)') IRC_PRIVMSG_RX = re.compile(r'PRIVMSG (?P[^\s]+) :(?P[^\n\r]+)') IRC_USER_RX = re.compile(r'USER (?P[^\s]+) [^\s]+ [^\s]+ :(?P[^\n\r]+)') +IRC_JOIN_RX = re.compile(r'JOIN (?P[^\s]+)') # IRC Handler class IRCHandler(object): - def __init__(self, stream, address, config_dir): + def __init__(self, config_dir): self.logger = logging.getLogger() - self.address = '{}:{}'.format(address[0], address[1]) - self.stream = stream self.ioloop = tornado.ioloop.IOLoop.current() self.hostname = socket.gethostname() self.config_dir = config_dir - self.tg = TelegramHandler(self, config_dir) # Initialize IRC self.initialize_irc() - self.logger.debug('Established client connection from %s', self.address) - async def run(self): + async def run(self, stream, address): + self.stream = stream + self.address = '{}:{}'.format(address[0], address[1]) + self.logger.debug('Running client connection from %s', self.address) while True: @@ -54,6 +53,9 @@ if matches: await handler(**matches.groupdict()) + def set_telegram(self, tg): + self.tg = tg + # IRC def initialize_irc(self): @@ -63,6 +65,7 @@ (IRC_PING_RX , self.handle_irc_ping), (IRC_PRIVMSG_RX, self.handle_irc_privmsg), (IRC_USER_RX , self.handle_irc_user), + (IRC_JOIN_RX , self.handle_irc_join), ) self.iid_to_tid = {} self.irc_channels = collections.defaultdict(set) @@ -99,12 +102,13 @@ self.hostname, self.irc_nick, 'End of MOTD command' )) - await self.tg.initialize_telegram() + async def handle_irc_join(self, channel): + self.logger.debug('Handling JOIN: %s', channel) + + await self.join_irc_channel(self.irc_nick, channel, True) async def handle_irc_pass(self, app_id, app_hash): self.logger.debug('Handling PASS: %s %s', app_id, app_hash) - self.tg.telegram_app_id = int(app_id) - self.tg.telegram_app_hash = app_hash async def handle_irc_ping(self, payload): self.logger.debug('Handling PING: %s', payload) diff -rN -u old-irgramd/irgramd new-irgramd/irgramd --- old-irgramd/irgramd 2024-11-23 02:01:53.833462849 +0100 +++ new-irgramd/irgramd 2024-11-23 02:01:53.833462849 +0100 @@ -7,12 +7,10 @@ import tornado.options import tornado.tcpserver -import telethon - # Local modules -from irc import * - +from irc import IRCHandler +from telegram import TelegramHandler # IRC Telegram Daemon @@ -24,19 +22,25 @@ self.address = address or '127.0.0.1' self.port = port self.config_dir = config_dir or os.path.expanduser('~/.config/irgramd') + self.irc_handler = None + self.tg_handler = None if not os.path.exists(self.config_dir): os.makedirs(self.config_dir) async def handle_stream(self, stream, address): - handler = IRCHandler(stream, address, self.config_dir) - await handler.run() + await self.irc_handler.run(stream, address) def run(self): self.listen(self.port, self.address) self.logger.info('irgramd listening on %s:%s', self.address, self.port) self.logger.info('Configuration Directory: %s', self.config_dir) + self.irc_handler = IRCHandler(self.config_dir) + self.tg_handler = TelegramHandler(self.irc_handler, self.config_dir) + self.irc_handler.set_telegram(self.tg_handler) + self.tg_handler.initialize_telegram() + tornado.ioloop.IOLoop.current().start() diff -rN -u old-irgramd/telegram.py new-irgramd/telegram.py --- old-irgramd/telegram.py 2024-11-23 02:01:53.833462849 +0100 +++ new-irgramd/telegram.py 2024-11-23 02:01:53.833462849 +0100 @@ -5,6 +5,12 @@ # Configuration +# GET API_ID and API_HASH from https://my.telegram.org/apps +# AND PUT HERE BEFORE RUNNING irgramd + +TELEGRAM_API_ID = +TELEGRAM_API_HASH = '' + NICK_MAX_LENGTH = 20 # Telegram @@ -14,10 +20,8 @@ self.logger = logging.getLogger() self.config_dir = config_dir self.irc = irc - self.telegram_app_id = '' - self.telegram_app_hash = '' - async def initialize_telegram(self): + def initialize_telegram(self): # Setup media folder self.telegram_media_dir = os.path.join(self.config_dir, 'media') if not os.path.exists(self.telegram_media_dir): @@ -29,10 +33,9 @@ os.makedirs(self.telegram_session_dir) # Construct Telegram client - telegram_session = os.path.join(self.telegram_session_dir, self.irc.irc_nick) + telegram_session = os.path.join(self.telegram_session_dir, 'telegram') self.telegram_client = telethon.TelegramClient(telegram_session, - self.telegram_app_id, # TODO: handle error - self.telegram_app_hash, + TELEGRAM_API_ID, TELEGRAM_API_HASH ) # Initialize Telegram ID to IRC nick mapping @@ -47,17 +50,18 @@ self.telegram_client.add_event_handler(handler, event) # Start Telegram client - await self.telegram_client.start() - + self.telegram_client.start() # Update IRC <-> Telegram mapping - telegram_me = await self.telegram_client.get_me() - iid = self.irc.irc_nick - tid = telegram_me.id - self.tid_to_iid[tid] = iid - self.irc.iid_to_tid[iid] = tid - - # Join all Telegram channels - await self.join_all_telegram_channels() + for dialog in self.telegram_client.iter_dialogs(): + chat = dialog.entity + if isinstance(chat, telethon.types.User): + user = self.get_telegram_nick(chat) + self.tid_to_iid[chat.id] = user + self.irc.iid_to_tid[user] = chat.id + else: + channel = self.get_telegram_channel(chat) + self.tid_to_iid[chat.id] = channel + self.irc.iid_to_tid[channel] = chat.id def get_telegram_nick(self, user): nick = (user.username