patch 5a82675ae695a750607f1347e3cb01e180005ffb Author: E. Bosch Date: Mon Jan 24 22:35:55 CET 2022 * Improve and cleanup send (to IRC) routines, add breaks and "continued line" marks diff -rN -u old-irgramd/irc.py new-irgramd/irc.py --- old-irgramd/irc.py 2024-11-22 21:36:58.870755871 +0100 +++ new-irgramd/irc.py 2024-11-22 21:36:58.874755865 +0100 @@ -13,7 +13,7 @@ from include import VERSION, CHAN_MAX_LENGHT, NICK_MAX_LENGTH from irc_replies import irc_codes -from utils import chunks, set_replace +from utils import chunks, set_replace, split_lines # Constants @@ -368,6 +368,15 @@ # IRC functions + 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)) + async def reply_command(self, user, prfx, comm, params): prefix = self.hostname if prfx == SRV else prfx.get_irc_mask() p = len(params) diff -rN -u old-irgramd/telegram.py new-irgramd/telegram.py --- old-irgramd/telegram.py 2024-11-22 21:36:58.870755871 +0100 +++ new-irgramd/telegram.py 2024-11-22 21:36:58.874755865 +0100 @@ -243,12 +243,9 @@ self.logger.debug('Handling Telegram Private Message: %s', event) user = self.get_irc_user_from_telegram(event.sender_id) - 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) async def handle_telegram_channel_message(self, event): self.logger.debug('Handling Telegram Channel Message: %s', event) @@ -259,21 +256,15 @@ user = self.get_irc_user_from_telegram(event.sender_id) # Format messages with media - 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) async def handle_telegram_chat_action(self, event): self.logger.debug('Handling Telegram Chat Action: %s', event) diff -rN -u old-irgramd/utils.py new-irgramd/utils.py --- old-irgramd/utils.py 2024-11-22 21:36:58.870755871 +0100 +++ new-irgramd/utils.py 2024-11-22 21:36:58.874755865 +0100 @@ -1,5 +1,6 @@ import itertools +import textwrap # Utilities @@ -12,3 +13,31 @@ if item in set: set.remove(item) set.add(new_item) + +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