From 3175b6c712f19d7ee9e6c31871de35857e24127d Mon Sep 17 00:00:00 2001 From: "mr.Shu" Date: Sun, 9 Jul 2017 13:16:55 +0200 Subject: [PATCH] irc: Fix maximal message length * The maximal message length has previously been set to 510 (probably in order to accomodate the b'\r\n' that are necessary for each IRC message). This turns out not to be enough, as the message starts with the obligatory `PRIVMSG :` and ends with the EXT (b'\x03) mark. * The size limit was therefore set to `499` in order to accomodate all these obligatory parts. * Furthermore, a new configuration option has been added (`MESSAGE_SIZE_LIMIT_INCLUDES_ADDRESEE`), which takes care of the fact that `` -- the addressee of the message -- comes directly after `PRIVMSG` and is thus part of the message. Note that this is currently only applied in the case of IRC. * Fixes #989 Signed-off-by: mr.Shu --- errbot/backends/irc.py | 9 ++++++++- errbot/bootstrap.py | 5 ++++- errbot/core.py | 9 ++++++++- 3 files changed, 20 insertions(+), 3 deletions(-) diff --git a/errbot/backends/irc.py b/errbot/backends/irc.py index ca09e4950..b033903ba 100644 --- a/errbot/backends/irc.py +++ b/errbot/backends/irc.py @@ -52,7 +52,13 @@ end_inline_code='') IRC_NICK_REGEX = r'[a-zA-Z\[\]\\`_\^\{\|\}][a-zA-Z0-9\[\]\\`_\^\{\|\}-]+' -IRC_MESSAGE_SIZE_LIMIT = 510 +# According to the RFC the IRC message size can be at most 512 bytes +# Out of these 2 bytes are necessary for '\r\n'. +# At least 10 bytes for 'PRIVMSG :' +# (note that is being taken care of in split_and_send_message +# using MESSAGE_SIZE_LIMIT_INCLUDES_ADDRESEE) +# And one more byte for b'\x03' -- EXT - End Of Text +IRC_MESSAGE_SIZE_LIMIT = 499 try: import irc.connection @@ -669,6 +675,7 @@ def __init__(self, config): ) self.md = irc_md() config.MESSAGE_SIZE_LIMIT = IRC_MESSAGE_SIZE_LIMIT + config.MESSAGE_SIZE_LIMIT_INCLUDES_ADDRESEE = True def send_message(self, msg): super().send_message(msg) diff --git a/errbot/bootstrap.py b/errbot/bootstrap.py index 5b9634c46..c087d5086 100644 --- a/errbot/bootstrap.py +++ b/errbot/bootstrap.py @@ -42,7 +42,10 @@ def bot_config_defaults(config): if not hasattr(config, 'DIVERT_TO_PRIVATE'): config.DIVERT_TO_PRIVATE = () if not hasattr(config, 'MESSAGE_SIZE_LIMIT'): - config.MESSAGE_SIZE_LIMIT = 10000 # Corresponds with what HipChat accepts + # Corresponds with what HipChat accepts + config.MESSAGE_SIZE_LIMIT = 10000 + if not hasattr(config, 'MESSAGE_SIZE_LIMIT_INCLUDES_ADDRESEE'): + config.MESSAGE_SIZE_LIMIT_INCLUDES_ADDRESEE = False if not hasattr(config, 'GROUPCHAT_NICK_PREFIXED'): config.GROUPCHAT_NICK_PREFIXED = False if not hasattr(config, 'AUTOINSTALL_DEPS'): diff --git a/errbot/core.py b/errbot/core.py index bbcbba078..14507854b 100644 --- a/errbot/core.py +++ b/errbot/core.py @@ -158,7 +158,14 @@ def send_templated(self, identifier, template_name, template_parameters, in_repl return self.send(identifier, text, in_reply_to, groupchat_nick_reply) def split_and_send_message(self, msg): - for part in split_string_after(msg.body, self.bot_config.MESSAGE_SIZE_LIMIT): + size_limit = self.bot_config.MESSAGE_SIZE_LIMIT + + # If MESSAGE_SIZE_LIMIT_INCLUDES_ADDRESEE is set to True, lower the + # size_limit by the size of the string representation of the addressee + if self.bot_config.MESSAGE_SIZE_LIMIT_INCLUDES_ADDRESEE: + size_limit -= len(str(msg.to)) + + for part in split_string_after(msg.body, size_limit): partial_message = msg.clone() partial_message.body = part self.send_message(partial_message)