Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

irc: Fix maximal message length #1063

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

mrshu
Copy link
Member

@mrshu mrshu commented Jul 9, 2017

  • 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 <to> : 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 <to> -- 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 IRC: Error sending messages bigger than 512 bytes. #989

Signed-off-by: mr.Shu [email protected]

* 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 <to> :` 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 `<to>` -- 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 errbotio#989

Signed-off-by: mr.Shu <[email protected]>
@mrshu
Copy link
Member Author

mrshu commented Jul 9, 2017

@gbin @zoni just a quick comment on the failed Travis test -- it seems to be related to flows and not really to the changes introduced in this PR.

With regards to this PR, I am happy to add some tests, although I did not find one for the IRC backend in the core repo.

Copy link
Member

@zoni zoni left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is nice!

However, this MESSAGE_SIZE_LIMIT_INCLUDES_ADDRESEE is quite IRC specific, which I'd prefer not to have leak through into errbot itself quite so much by introducing a new parameter in bot_config. I've suggested a different approach, let me know your thoughts.

# 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))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about taking a slightly different approach of modifying the signature of split_and_send_message to:

def split_and_send_message(self, msg, max_length=None):
    if max_length is None:
        max_length = self.bot_config.MESSAGE_SIZE_LIMIT

    for part in split_string_after(msg.body, size_limit):
        partial_message = msg.clone()
        partial_message.body = part
        self.send_message(partial_message)

You can then do the max_length -= len(str(msg.to)) calculation in the IRC backend directly and pass max_length to split_and_send_message explicitly. That way it's completely self-contained within the IRC backend.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@zoni Sorry for my late reply here.

Your solution would indeed be much better but sadly, split_and_send_message is not called from the IRC backend. The IRC backend could still set some sort of object variable/flag on IRCBackend (which inherits from ErrBot which in turn contains split_and_send_message) that would cause the split_and_send_message function to execute code similar to what there currently is in the PR, without the global config option.

I may also not fully understand what you meant, so please feel free to point out if I missed anything.

Thanks!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mrshu Hey! Following up for @zoni

I think the goal here is trying to keep this specific IRC code in the backend. While split_and_send_message is not called from IRCBackend, I agree that it still could be self-contained in the backend.

I recommend looking at the SlackBackend's implementation for their size limits:

limit = min(self.bot_config.MESSAGE_SIZE_LIMIT, SLACK_MESSAGE_LIMIT)
parts = self.prepare_message_body(body, limit)
timestamps = []
for part in parts:
data = {
'channel': to_channel_id,
'text': part,
'unfurl_media': 'true',
'link_names': '1',
'as_user': 'true',
}
# Keep the thread_ts to answer to the same thread.
if 'thread_ts' in msg.extras:
data['thread_ts'] = msg.extras['thread_ts']
result = self.api_call('chat.postMessage', data=data)

it's completely self contained. Is there a reason to have a toggleable option for additional size limitation? Wouldn't all IRC messages contain ADDRESEE? or is that only for PRIVMSG? It's bene a while since I've interacted with the IRC backend.

@mrshu
Copy link
Member Author

mrshu commented Mar 12, 2018

@zoni I am sorry for pinging you again -- would you happen to find a moment to consider my reply above?

Thanks a ton!

@GenPage
Copy link
Contributor

GenPage commented Oct 19, 2018

@mrshu Sorry for the delay! I'm following up on your comments!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

IRC: Error sending messages bigger than 512 bytes.
4 participants