irc: Add WHOIS command
patch 8e120e3fd8592748b35ae91c4acbf189036704d9
Author: E. Bosch <presidev@AT@gmail.com>
Date: Sun Feb 7 03:14:36 CET 2021
* irc: Add WHOIS command
hunk ./irc.py 33
+IRC_WHOIS_RX = re.compile(PREFIX + r'WHOIS( +:| +|\n)(?P<nicks>[^\n]+|)')
hunk ./irc.py 100
+ (IRC_WHOIS_RX, self.handle_irc_whois, True),
hunk ./irc.py 187
+ async def handle_irc_whois(self, user, nicks):
+ self.logger.debug('Handling WHO: %s', nicks)
+ for nick in nicks.split(','):
+ ni = nick.lower()
+ real_ni = self.users[ni].irc_nick
+ if ni in self.users.keys():
+ usr = self.users[ni]
+ await self.reply_code(user, 'RPL_WHOISUSER', (real_ni, usr.irc_username, usr.address, usr.irc_realname))
+ await self.reply_code(user, 'RPL_WHOISSERVER', (real_ni, self.hostname))
+ chans = usr.get_channels(self)
+ if chans: await self.reply_code(user, 'RPL_WHOISCHANNELS', (real_ni, chans))
+ idle = await self.tg.get_telegram_idle(ni)
+ if idle != None: await self.reply_code(user, 'RPL_WHOISIDLE', (real_ni, idle))
+ if usr.oper: await self.reply_code(user, 'RPL_WHOISOPERATOR', (real_ni,))
+ if usr.stream: await self.reply_code(user, 'RPL_WHOISACCOUNT', (real_ni,
+ '{}!{}@Telegram'.format(self.tg.tg_username, self.tg.id
+ )))
+ if await self.tg.is_bot(ni):
+ await self.reply_code(user, 'RPL_WHOISBOT', (real_ni,))
+ elif usr.tls or not usr.stream:
+ proto = 'TLS' if usr.tls else 'MTProto'
+ server = self.hostname if usr.stream else 'Telegram'
+ await self.reply_code(user, 'RPL_WHOISSECURE', (real_ni, proto, server))
+ await self.reply_code(user, 'RPL_ENDOFWHOIS', (real_ni,))
+ else:
+ await self.reply_code(user, 'ERR_NOSUCHNICK', (nick,))
+
hunk ./irc.py 316
+ self.oper = False
+ self.tls = False
+ self.bot = None
hunk ./irc.py 323
+ def get_channels(self, irc):
+ res = ''
+ for chan in irc.irc_channels.keys():
+ if self.irc_nick in irc.irc_channels[chan]:
+ res += irc.get_irc_op(self.irc_nick, chan) + chan + ' '
+ return res
+
hunk ./irc_replies.py 9
+ 'RPL_WHOISUSER': ('311', '{} {} {} * :{}'),
+ 'RPL_WHOISSERVER': ('312', '{} {} :irgramd gateway'),
+ 'RPL_WHOISOPERATOR': ('313', '{} :is an irgramd operator'),
hunk ./irc_replies.py 13
+ 'RPL_WHOISIDLE': ('317', '{} {} :seconds idle'),
+ 'RPL_ENDOFWHOIS': ('318', '{} :End of WHOIS command'),
+ 'RPL_WHOISCHANNELS': ('319', '{} :{}'),
+ 'RPL_WHOISACCOUNT': ('330', '{} {} :Telegram name'),
+ 'RPL_WHOISBOT': ('335', '{} :is a Telegram bot'),
hunk ./irc_replies.py 22
- 'ERR_NOSUCHNICK': ('401', 'No such nick'),
+ 'ERR_NOSUCHNICK': ('401', '{} :Nick not found'),
hunk ./irc_replies.py 66
+ 'RPL_WHOISSECURE': ('671', '{} :is using a secure {} connection with {}'),
hunk ./telegram.py 4
+import datetime
hunk ./telegram.py 161
+ async def get_telegram_idle(self, irc_nick, tid=None):
+ tid = self.get_tid(irc_nick, tid)
+ user = await self.telegram_client.get_entity(tid)
+ if isinstance(user.status,tgty.UserStatusRecently) or \
+ isinstance(user.status,tgty.UserStatusOnline):
+ idle = 0
+ elif isinstance(user.status,tgty.UserStatusOffline):
+ last = user.status.was_online
+ current = datetime.datetime.now(datetime.timezone.utc)
+ idle = int((current - last).total_seconds())
+ elif isinstance(user.status,tgty.UserStatusLastWeek):
+ idle = 604800
+ elif isinstance(user.status,tgty.UserStatusLastMonth):
+ idle = 2678400
+ else:
+ idle = None
+ return idle
+
+ async def is_bot(self, irc_nick, tid=None):
+ if self.irc.users[irc_nick].stream:
+ bot = False
+ else:
+ bot = self.irc.users[irc_nick].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
+ return bot
+
+ def get_tid(self, irc_nick, tid=None):
+ if tid:
+ pass
+ elif irc_nick in self.irc.iid_to_tid:
+ tid = self.irc.iid_to_tid[irc_nick.lower()]
+ else:
+ tid = self.id
+ return tid
+