irc: Refactorize PRIVMSG handle and routines to send messages to IRC
patch d8ede54b5d94a3c78869abbc53736362686d1a64
Author: E. Bosch <presidev@AT@gmail.com>
Date: Thu Feb 3 21:11:10 CET 2022
* irc: Refactorize PRIVMSG handle and routines to send messages to IRC
Echo channel messages from IRC to other IRC connections
Change 'nick' argument with 'target' in PRIVMSG handle
Fix: send channel messages only to IRC connections that are in that channel
hunk ./irc.py 37
-IRC_PRIVMSG_RX = re.compile(PREFIX + r'PRIVMSG( +|\n)(?P<nick>[^ ]+)( +:| +|\n)(?P<message>[^\n]+|)')
+IRC_PRIVMSG_RX = re.compile(PREFIX + r'PRIVMSG( +|\n)(?P<target>[^ ]+)( +:| +|\n)(?P<message>[^\n]+|)')
hunk ./irc.py 349
- async def handle_irc_privmsg(self, user, nick, message):
- self.logger.debug('Handling PRIVMSG: %s, %s', nick, message)
-
- if nick == user.irc_nick:
- target = self.tg.tg_username
+ async def handle_irc_privmsg(self, user, target, message):
+ self.logger.debug('Handling PRIVMSG: %s, %s', target, message)
+
+ tgl = target.lower()
+ # Echo channel messages from IRC to other IRC connections
+ # because they won't receive event from Telegram
+ if tgl in self.irc_channels.keys():
+ await self.send_msg_others(user, tgl, message)
+
+ if tgl == user.irc_nick:
+ tgt = self.tg.tg_username.lower()
hunk ./irc.py 364
- target = nick
- tgt = target.lower()
+ tgt = tgl
hunk ./irc.py 383
+ tgt = target.lower() if target else ''
+ is_chan = tgt in self.irc_channels.keys()
+ # source None (False): it's self Telegram user, see [1]
hunk ./irc.py 388
- for irc_user in (x for x in self.users.values() if x.stream):
- src_mask = source_mask if source_mask else irc_user.get_irc_mask()
- tgt = target if target else irc_user.irc_nick
- await self.send_irc_command(irc_user, ':{} PRIVMSG {} :{}'.format(src_mask, tgt, msg))
+ if is_chan:
+ irc_users = (u for u in self.users.values() if u.stream and u.irc_nick in self.irc_channels[tgt])
+ else:
+ irc_users = (u for u in self.users.values() if u.stream)
+
+ for irc_user in irc_users:
+ await self.send_privmsg(irc_user, source_mask, target, msg)
+
+ async def send_msg_others(self, source, target, message):
+ source_mask = source.get_irc_mask()
+ is_chan = target in self.irc_channels.keys()
+ if is_chan:
+ irc_users = (u for u in self.users.values() if u.stream and u.irc_nick != source.irc_nick
+ and u.irc_nick in self.irc_channels[target])
+ else:
+ irc_users = (u for u in self.users.values() if u.stream and u.irc_nick != source.irc_nick)
+
+ for irc_user in irc_users:
+ await self.send_privmsg(irc_user, source_mask, target, message)
+
+ async def send_privmsg(self, user, source_mask, target, msg):
+ # reference [1]
+ src_mask = source_mask if source_mask else user.get_irc_mask()
+ # target None (False): it's private, not a channel
+ tgt = target if target else user.irc_nick
+ await self.send_irc_command(user, ':{} PRIVMSG {} :{}'.format(src_mask, tgt, msg))