Improve mapping between Telegram and IRC: users and channels
patch 4cff2e00c20989a0f7cf0bdcebd126a71967bf3a
Author: E. Bosch <presidev@AT@gmail.com>
Date: Sun Jan 31 20:23:45 CET 2021
* Improve mapping between Telegram and IRC: users and channels
hunk ./irc.py 50
- self.logger.debug('Running client connection from %s', user.address)
+ self.logger.debug('Running client connection from %s:%s', address[0], address[1])
hunk ./irc.py 203
- self.irc_channels[channel].add(user.irc_nick)
+ self.irc_channels[channel.lower()].add(user.irc_nick)
hunk ./irc.py 213
- # Add all users to channel
- tid = self.iid_to_tid[channel]
- nicks = await self.tg.get_telegram_channel_participants(tid)
+ # Get all users from channel
+ tid = self.iid_to_tid[channel.lower()]
+ nicks = self.irc_channels[channel.lower()]
hunk ./irc.py 236
- def __init__(self, stream, address):
+ def __init__(self, stream, address, irc_nick=None, username=None, realname=None):
hunk ./irc.py 238
- self.address = '{}:{}'.format(address[0], address[1])
- self.irc_nick = None
- self.irc_username = None
- self.irc_realname = None
+ self.address = address[0]
+ self.irc_nick = irc_nick
+ self.irc_username = str(username) if type(username) is int else username
+ self.irc_realname = realname
hunk ./telegram.py 9
+from irc import IRCUser
hunk ./telegram.py 69
- user = self.get_telegram_nick(chat)
- self.tid_to_iid[chat.id] = user
- self.irc.iid_to_tid[user] = chat.id
+ if not chat.is_self:
+ self.set_ircuser_from_telegram(chat)
hunk ./telegram.py 72
- channel = self.get_telegram_channel(chat)
- self.tid_to_iid[chat.id] = channel
- self.irc.iid_to_tid[channel] = chat.id
+ await self.set_irc_channel_from_telegram(chat)
+
+ def set_ircuser_from_telegram(self, user):
+ if user.id not in self.tid_to_iid:
+ tg_nick = self.get_telegram_nick(user)
+ irc_user = IRCUser(None, ('Telegram',), tg_nick, user.id, self.get_telegram_display_name(user))
+ self.irc.users[tg_nick.lower()] = irc_user
+ self.tid_to_iid[user.id] = tg_nick
+ self.irc.iid_to_tid[tg_nick.lower()] = user.id
+ else:
+ tg_nick = self.tid_to_iid[user.id]
+ return tg_nick
+
+ async def set_irc_channel_from_telegram(self, chat):
+ channel = self.get_telegram_channel(chat)
+ self.tid_to_iid[chat.id] = channel
+ self.irc.iid_to_tid[channel.lower()] = chat.id
+ # Add users from the channel
+ for user in [x async for x in self.telegram_client.iter_participants(chat.id) if not x.is_self]:
+ user_nick = self.set_ircuser_from_telegram(user)
+ self.irc.irc_channels[channel.lower()].add(user_nick)
hunk ./telegram.py 96
- or telethon.utils.get_display_name(user)
+ or self.get_telegram_display_name(user)
hunk ./telegram.py 98
- nick = nick.replace(' ', '')[:NICK_MAX_LENGTH]
+ nick = nick[:NICK_MAX_LENGTH]
hunk ./telegram.py 102
+
+ def get_telegram_display_name(self, user):
+ name = telethon.utils.get_display_name(user)
+ name = name.replace(' ', '_')
+ return name
hunk ./telegram.py 109
- return '#' + chat.title.lower().replace(' ', '-')
+ return '#' + chat.title.replace(' ', '-')
hunk ./telegram.py 153
- for user in self.irc.users.values():
+ for user in [x for x in self.irc.users.values() if x.stream]:
hunk ./telegram.py 164
- if channel not in self.irc.irc_channels:
- await self.irc.join_irc_channel(self.irc.irc_nick, channel, True)
+# if channel not in self.irc.irc_channels:
+# await self.irc.join_irc_channel(self.irc.irc_nick, channel, True)
hunk ./telegram.py 168
- if nick not in self.irc.irc_channels[channel]:
- await self.irc.join_irc_channel(nick, channel, False)
+# if nick not in self.irc.irc_channels[channel]:
+# await self.irc.join_irc_channel(nick, channel, False)
hunk ./telegram.py 182
- for user in self.irc.users.values():
+ for user in [x for x in self.irc.users.values() if x.stream]: