matabot

a silly irc bot
git clone ssh://anon@git.pastanoggin.com
Log | Files | Refs | README | LICENSE

commit 3af4a1f76a207c8113cce7539b513a3af26dff8d
parent 32d1199ddfacc2acda862cd1ad7032be610f2d37
Author: noodle <noodle@pastanoggin.com>
Date:   Fri,  9 Jan 2026 17:27:01 +0000

fix msgmax calculation and add an initial msgmax heuristic to fallback on

fixed incorrect msgmax calculation in response to WHO reply code 352, which
didn't account for the":nick!user@" part before hostname nor the space before
$priv.

additionally, an upcoming RSS feature would sometimes need to send news before
reading any messages back from server (and WHO reply code 352 might never return
or might not return on time), so a conservative fallback msgmax heurestic
calculation was added before the evasdrop loop. it uses a maximum hostname
length of 63 from RFC 2812 and accounts for the the non-ident tilde (~) that
might get prepended to username until we know what the actual hostname and
username are.

Diffstat:
Mmata_bot.pl | 24+++++++++++++++---------
1 file changed, 15 insertions(+), 9 deletions(-)

diff --git a/mata_bot.pl b/mata_bot.pl @@ -11,8 +11,10 @@ use constant { LOG_WARN => 1, LOG_DEBUG => 2, CONNECT_TIMEOUT => 60, + CRLF => 2, # RFC 2812 DEFAULT_PORT => 6667, DEFAULT_TLS => 0, + HOSTMAX => 63, # RFC 2812 IRCMAX => 512, LAG_CHECK_TIME => 120, MAX_LAG => 300, @@ -298,6 +300,8 @@ sub evasdrop { $pingsent = 0; $priv = "PRIVMSG ${chan} :"; + $msgmax = IRCMAX - length(":${MYNICK}!~${MYUSER}\@ ${priv}") + - HOSTMAX - CRLF; # conservative max message length heuristic $msgtime = $pingtime = time; while (1) { if ($pingsent) { @@ -319,19 +323,21 @@ sub evasdrop { sendmsg($s, 'PONG :' . $1); } elsif (/^:[^ ]+ PONG/) { $pingsent = 0; - } elsif (/^:[^ ]+ 352 [^ ]+ [^ ]+ [^ ]+ ([^ ]+)/) { - # the extra 2 bytes account for \r\n - $msgmax = IRCMAX - length($1) - length($priv) - 2; + } elsif ( + /^:[^ ]+ 352 ${MYNICK} [^ ]+ ([^ ]+) ([^ ]+) [^ ]+ (${MYNICK})/ + ) { + # refine maximum message length heuristic + $msgmax = IRCMAX - length(":${3}!${1}\@${2} ${priv}") + - CRLF; } elsif (/^:([^ !#&][^ !]*)![^ \@]+\@[^ ]+ ${priv}(.+)$/) { # respond to chan message - my $r; + my ($r, $len); - unless ($msgmax) { - logger(LOG_ERROR, 'could not calculate msgmax'); - return; - } $r = respond($1, $2, @$lists); - sendmsg($s, $priv . substr($r,0,$msgmax)) if length($r); + if ($len = length($r)) { + $r = substr($r,0,$msgmax-1).'-' if $len>$msgmax; + sendmsg($s, $priv . $r); + } } } }