commit 2db6fade2e537f4ddb75b816dd3fb197ba8f4f8d from: noodle date: Sun Jun 15 14:32:17 2025 UTC move main loop to a function to implement reconnection commit - 8c7ebe8f11d0d6d1803e7037024e41d52b8f0550 commit + 2db6fade2e537f4ddb75b816dd3fb197ba8f4f8d blob - dc0dc9b84b1d3a73e67fc0a41dbd77319bd90d56 blob + 453b5a2aa4f8a3b9e804fbc29bf91b92a0cac7de --- mata_bot.pl +++ mata_bot.pl @@ -240,74 +240,79 @@ sub respond { return 0; } -# start the connection -my $sock; -if ($tls) { - $sock = IO::Socket::SSL->new( - Domain => AF_INET, - Type => SOCK_STREAM, - PeerHost => $host, - PeerPort => $port, - ) || die "Can't open socket: $IO::Socket::errstr"; -} else { - $sock = IO::Socket->new( - Domain => AF_INET, - Type => SOCK_STREAM, - proto => 'tcp', - PeerHost => $host, - PeerPort => $port, - ) || die "Can't open socket: $IO::Socket::errstr"; -} +sub evasdrop { + my $sock = shift; + my $buffer = ''; + my $chunk = ''; + my $message = ''; + while (1) { + # buffer up + if ($tls) { + $chunk = <$sock>; + } else { + $sock->recv($chunk, $CHUNK_LENGTH); + } + $chunk =~ /^([^\r\n]+)(\r\n)?([^\r\n]+)?$/; + # keep reading if chunk is empty + next if (not $1); + # if chunks isn't empty, check for framing point + if ($2) { + # if we found a framing point, flush buffer and text till framing point + $message = $buffer . $1; + if ($3) { + # if we have text after framing, make it the new content of buffer + $buffer = $3; + } else { + # if we have no text after framing, clear buffer + $buffer = ''; + } + } else { + # if there's no framing. append chunk to end of buffer and keep reading + $buffer .= $chunk; + next; + } -# set user, real, and nick, then join -out($sock, "USER $USER * * :$REAL"); -out($sock, "NICK $NICK"); -out($sock, "JOIN $chan"); + # log message + logger($message) if ($logging); -# evasdrop -my $buffer = ''; -my $chunk = ''; -my $message = ''; + # respond to message + if ($message =~ /^PING :([^\000\r\n\ ]+)$/) { + # if we got a ping, pong back + out($sock, "PONG :$1"); + } elsif ($message =~ /^:([^\000\r\n\#\&\ ][^\000\r\n\ ]*)![^\000\r\n\ ]+@[^\000\r\n\ ]+ PRIVMSG ${chan} :([^\000\r\n]*)$/) { + # if we got a message to our chan. read and act accordingly + my $sender_nick = $1; + my $sender_message = $2; + unless (respond($sock, $sender_nick, $sender_message)) { + $subbuffer{$sender_nick} = $sender_message; + } + } + } +} + while (1) { - # buffer up + # start the connection + my $sock; if ($tls) { - $chunk = <$sock>; + $sock = IO::Socket::SSL->new( + Domain => AF_INET, + Type => SOCK_STREAM, + PeerHost => $host, + PeerPort => $port, + ) || die "Can't open socket: $IO::Socket::errstr"; } else { - $sock->recv($chunk, $CHUNK_LENGTH); + $sock = IO::Socket->new( + Domain => AF_INET, + Type => SOCK_STREAM, + proto => 'tcp', + PeerHost => $host, + PeerPort => $port, + ) || die "Can't open socket: $IO::Socket::errstr"; } - $chunk =~ /^([^\r\n]+)(\r\n)?([^\r\n]+)?$/; - # keep reading if chunk is empty - next if (not $1); - # if chunks isn't empty, check for framing point - if ($2) { - # if we found a framing point, flush buffer and text till framing point - $message = $buffer . $1; - if ($3) { - # if we have text after framing, make it the new content of buffer - $buffer = $3; - } else { - # if we have no text after framing, clear buffer - $buffer = ''; - } - } else { - # if there's no framing. append chunk to end of buffer and keep reading - $buffer .= $chunk; - next; - } - - # log message - logger($message) if ($logging); - - # respond to message - if ($message =~ /^PING :([^\000\r\n\ ]+)$/) { - # if we got a ping, pong back - out($sock, "PONG :$1"); - } elsif ($message =~ /^:([^\000\r\n\#\&\ ][^\000\r\n\ ]*)![^\000\r\n\ ]+@[^\000\r\n\ ]+ PRIVMSG ${chan} :([^\000\r\n]*)$/) { - # if we got a message to our chan. read and act accordingly - my $sender_nick = $1; - my $sender_message = $2; - unless (respond($sock, $sender_nick, $sender_message)) { - $subbuffer{$sender_nick} = $sender_message; - } - } + # set user, real, and nick, then join + out($sock, "USER $USER * * :$REAL"); + out($sock, "NICK $NICK"); + out($sock, "JOIN $chan"); + # main loop + evasdrop($sock); }