Improve and cleanup send (to IRC) routines, add breaks and "continued line" marks
patch 5a82675ae695a750607f1347e3cb01e180005ffb
Author: E. Bosch <presidev@AT@gmail.com>
Date: Mon Jan 24 22:35:55 CET 2022
* Improve and cleanup send (to IRC) routines, add breaks and "continued line" marks
hunk ./irc.py 16
-from utils import chunks, set_replace
+from utils import chunks, set_replace, split_lines
hunk ./irc.py 371
+ async def send_msg(self, source, target, message):
+ messages = split_lines(message)
+ source_mask = source.get_irc_mask() if source else ''
+ for msg in messages:
+ 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))
+
hunk ./telegram.py 246
- for message in event.message.message.splitlines():
- for irc_user in [x for x in self.irc.users.values() if x.stream]:
- usr = user if user else irc_user
- await self.irc.send_irc_command(irc_user, ':{} PRIVMSG {} :{}'.format(
- usr.get_irc_mask(), irc_user.irc_nick, message
- ))
+ message = event.message.message if event.message.message else ''
+
+ await self.irc.send_msg(user, None, message)
hunk ./telegram.py 259
- messages = event.message.message.splitlines() if event.message.message else []
- if event.message.media and (event.message.photo or event.message.gif):
- message = await self.download_telegram_media(event.message, 'Image')
- if message:
- messages.insert(0, message)
- elif event.message.media and (event.message.sticker):
- messages.insert(0, 'Sticker: {}'.format(event.message.sticker.id))
-
- # Send all messages to IRC
- for message in messages:
- for irc_user in [x for x in self.irc.users.values() if x.stream]:
- usr = user if user else irc_user
- await self.irc.send_irc_command(irc_user, ':{} PRIVMSG {} :{}'.format(
- usr.get_irc_mask(), channel, message
- ))
+ message = event.message.message if event.message.message else ''
+# if event.message.media and (event.message.photo or event.message.gif):
+# message = await self.download_telegram_media(event.message, 'Image')
+# if message:
+# messages.insert(0, message)
+# elif event.message.media and (event.message.sticker):
+# messages.insert(0, 'Sticker: {}'.format(event.message.sticker.id))
+
+ await self.irc.send_msg(user, channel, message)
hunk ./utils.py 3
+import textwrap
hunk ./utils.py 16
+
+def get_continued(items, mark, length):
+ # Add "continued" mark to lines, except last one
+ return (x + mark if n != length else x for n, x in enumerate(items, start=1))
+
+def split_lines(message):
+ MAX = 400
+ messages_limited = []
+ wr = textwrap.TextWrapper(width=MAX)
+
+ # Split when Telegram original message has breaks
+ messages = message.splitlines()
+ lm = len(messages)
+ if lm > 1:
+ # Add "continued line" mark (\) for lines that belong to the same message
+ # (split previously)
+ messages = get_continued(messages, ' \\', lm)
+ for m in messages:
+ wrapped = wr.wrap(text=m)
+ lw = len(wrapped)
+ if lw > 1:
+ # Add double "continued line" mark (\\) for lines that belong to the same message
+ # and have been wrapped to not exceed IRC limits
+ messages_limited += get_continued(wrapped, ' \\\\', lw)
+ else:
+ messages_limited += wrapped
+ del wr
+ return messages_limited