pteichman / cobe

A Markov chain based text generation library and MegaHAL style chatbot
http://teichman.org/blog/
MIT License
242 stars 51 forks source link

irc.client.MessageTooLong: Messages limited to 512 bytes including CR/LF #14

Closed ghost closed 10 years ago

ghost commented 10 years ago
Traceback (most recent call last):
  File "/usr/bin/cobe", line 9, in <module>
    load_entry_point('cobe==2.1.0', 'console_scripts', 'cobe')()
  File "build/bdist.linux-x86_64/egg/cobe/control.py", line 42, in main
  File "build/bdist.linux-x86_64/egg/cobe/commands.py", line 244, in run

  File "build/bdist.linux-x86_64/egg/cobe/bot.py", line 117, in run
  File "build/bdist.linux-x86_64/egg/irc/client.py", line 1225, in start
  File "build/bdist.linux-x86_64/egg/irc/client.py", line 267, in process_forever
  File "build/bdist.linux-x86_64/egg/irc/client.py", line 248, in process_once
  File "build/bdist.linux-x86_64/egg/irc/client.py", line 213, in process_data
  File "build/bdist.linux-x86_64/egg/irc/client.py", line 561, in process_data
  File "build/bdist.linux-x86_64/egg/irc/client.py", line 629, in _process_line
  File "build/bdist.linux-x86_64/egg/irc/client.py", line 651, in _handle_event
  File "build/bdist.linux-x86_64/egg/irc/client.py", line 387, in _handle_event
  File "build/bdist.linux-x86_64/egg/cobe/bot.py", line 31, in _dispatcher
  File "build/bdist.linux-x86_64/egg/irc/client.py", line 1188, in _dispatcher
  File "build/bdist.linux-x86_64/egg/cobe/bot.py", line 105, in on_pubmsg
  File "build/bdist.linux-x86_64/egg/irc/client.py", line 855, in privmsg
  File "build/bdist.linux-x86_64/egg/irc/client.py", line 883, in send_raw
irc.client.MessageTooLong: Messages limited to 512 bytes including CR/LF

Any way to split up messages for sending to IRC to avoid this error?

ghost commented 10 years ago

Here's a terrible hacky, lazy quick 'fix':

        if to == conn.nickname:
            reply = self.brain.reply(text)
            replylen = len(reply)

            if replylen > 470:
                conn.privmsg(event.target, "%s: %s" % (user, reply[:470]))
                conn.privmsg(event.target, "%s: %s" % (user, reply[470:]))
            else:
                conn.privmsg(event.target, "%s: %s" % (user, reply))

Just breaks up reply into two chunks. Ugly and won't fix if you go way over. A better solution would be some kind of loop which I'll work on later. 470 seems to work for me. I'm not sure if the entire irc message needs to be less than 512 bytes including the actual "privmsg" part that sends the text to the channel... or just the actual string.

This at least prevents the crash. I'll work on something better later unless you or someone else has something in mind.

pteichman commented 10 years ago

Yeah, that can be Unicode and also there's some IRC protocol stuff that takes a few bytes. 512 is the maximum size for the whole IRC message. I settled on 425 for a different bot.

There's decent whitespace-based wrapping in the standard library. Want to try something like this?

for line in textwrap.wrap(reply, 425):
    conn.privmsg(event.target, "%s: %s" % (user, line))
ghost commented 10 years ago

looks good. added:

import textwrap

and made the conn.nickname if-statement read as follows:

        if to == conn.nickname:
            reply = self.brain.reply(text)

            for line in textwrap.wrap(reply, 425):
                conn.privmsg(event.target, "%s: %s" % (user, line))

seems to work great. better wrapping than what I was trying :)