commit 2db6fade2e537f4ddb75b816dd3fb197ba8f4f8d
parent 8c7ebe8f11d0d6d1803e7037024e41d52b8f0550
Author: noodle <noodle@pastanoggin.com>
Date: Sun, 15 Jun 2025 17:32:17 +0300
move main loop to a function to implement reconnection
Diffstat:
| M | mata_bot.pl | | | 133 | +++++++++++++++++++++++++++++++++++++++++-------------------------------------- |
1 file changed, 69 insertions(+), 64 deletions(-)
diff --git a/mata_bot.pl b/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";
-}
-
-# set user, real, and nick, then join
-out($sock, "USER $USER * * :$REAL");
-out($sock, "NICK $NICK");
-out($sock, "JOIN $chan");
-
-# evasdrop
-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;
+sub evasdrop {
+ my $sock = shift;
+ my $buffer = '';
+ my $chunk = '';
+ my $message = '';
+ while (1) {
+ # buffer up
+ if ($tls) {
+ $chunk = <$sock>;
} else {
- # if we have no text after framing, clear buffer
- $buffer = '';
+ $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;
}
- } else {
- # if there's no framing. append chunk to end of buffer and keep reading
- $buffer .= $chunk;
- next;
- }
- # log message
- logger($message) if ($logging);
+ # 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;
+ # 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) {
+ # 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";
+ }
+ # set user, real, and nick, then join
+ out($sock, "USER $USER * * :$REAL");
+ out($sock, "NICK $NICK");
+ out($sock, "JOIN $chan");
+ # main loop
+ evasdrop($sock);
+}