Change the point where irgramd connects to Telegram
patch c154ded080ea797bb2188ea8318b54f7127eeb91
Author: E. Bosch <presidev@AT@gmail.com>
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
hunk ./irc.py 13
-from telegram import TelegramHandler
hunk ./irc.py 25
+IRC_JOIN_RX = re.compile(r'JOIN (?P<channel>[^\s]+)')
hunk ./irc.py 30
- def __init__(self, stream, address, config_dir):
+ def __init__(self, config_dir):
hunk ./irc.py 32
- self.address = '{}:{}'.format(address[0], address[1])
- self.stream = stream
hunk ./irc.py 35
- self.tg = TelegramHandler(self, config_dir)
hunk ./irc.py 39
- 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])
+
hunk ./irc.py 56
+ def set_telegram(self, tg):
+ self.tg = tg
+
hunk ./irc.py 68
+ (IRC_JOIN_RX , self.handle_irc_join),
hunk ./irc.py 105
- 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)
hunk ./irc.py 112
- self.tg.telegram_app_id = int(app_id)
- self.tg.telegram_app_hash = app_hash
hunk ./irgramd 10
-import telethon
-
hunk ./irgramd 12
-from irc import *
-
+from irc import IRCHandler
+from telegram import TelegramHandler
hunk ./irgramd 25
+ self.irc_handler = None
+ self.tg_handler = None
hunk ./irgramd 32
- handler = IRCHandler(stream, address, self.config_dir)
- await handler.run()
+ await self.irc_handler.run(stream, address)
hunk ./irgramd 39
+ 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()
+
hunk ./telegram.py 8
+# GET API_ID and API_HASH from https://my.telegram.org/apps
+# AND PUT HERE BEFORE RUNNING irgramd
+
+TELEGRAM_API_ID =
+TELEGRAM_API_HASH = ''
+
hunk ./telegram.py 23
- self.telegram_app_id = ''
- self.telegram_app_hash = ''
-
- async def initialize_telegram(self):
+
+ def initialize_telegram(self):
hunk ./telegram.py 36
- telegram_session = os.path.join(self.telegram_session_dir, self.irc.irc_nick)
+ telegram_session = os.path.join(self.telegram_session_dir, 'telegram')
hunk ./telegram.py 38
- self.telegram_app_id, # TODO: handle error
- self.telegram_app_hash,
+ TELEGRAM_API_ID, TELEGRAM_API_HASH
hunk ./telegram.py 53
- await self.telegram_client.start()
-
+ self.telegram_client.start()
hunk ./telegram.py 55
- 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