mata_bot

some cheeky bot for #unix_surrealism
git clone https://git.pastanoggin.com/mata_bot.git
Log | Files | Refs | README | LICENSE

commit 05d86fdaa37573e395f40904af86c8516c40456d
parent 2db6fade2e537f4ddb75b816dd3fb197ba8f4f8d
Author: noodle <noodle@pastanoggin.com>
Date:   Wed, 18 Jun 2025 23:13:44 +0300

Merge branch 'master' into fix_noreconnect

Diffstat:
Mball | 4++--
Mmata_bot.pl | 38++++++++++++++++++++++++++------------
Mmatabot.8 | 88+++++++++++++++++++++++++++++++++++++++++++++++++------------------------------
3 files changed, 83 insertions(+), 47 deletions(-)

diff --git a/ball b/ball @@ -1,5 +1,5 @@ -Dead-on! -Correct! +Deadass! +No cap! That's right! Yes, of course! Absolutely! diff --git a/mata_bot.pl b/mata_bot.pl @@ -10,6 +10,7 @@ use POSIX qw(strftime); my $SHAREDIR = '/usr/local/share'; my $CHUNK_LENGTH = 1024; my $NICK = 'mata_bot'; +my $NICK_RE = qr/mata_?bo[ity]+/i; my $USER = 'mata_bot_beta4'; my $REAL = 'death to technomage!!'; my $MOTHER = 'noodle'; @@ -153,7 +154,7 @@ sub respond_mention { my ($sock, $sender_nick, $message) = @_; if ($sender_nick eq $MOTHER) { msg($sock, "Yes, mother? ${MATA_HAPPY}"); - } elsif ($message =~ /^\W*${NICK}\W*$/) { + } elsif ($message =~ /^\b${NICK_RE}\b$/) { msg($sock, "${MATA_NORM} ?"); } else { msg($sock, "$quotes[int rand($quotes_num)] ${MATA_NORM}"); @@ -164,15 +165,27 @@ sub respond_mention { # returns 1 if bot shouldn't remember last message for s///, 0 otherwise sub respond { my ($sock, $sender_nick, $message) = @_; - if ($subbuffer{$sender_nick} && $message =~ m,\b{wb}s/([^\000\r\n/]*)/([^\000\r\n/]*)/?,) { + if ($subbuffer{$sender_nick} && $message =~ m,\b{wb}s/([^/]+)/([^/]*)(?:/([gi]*))?,) { # chat s/// - my $toreplace = $1; - my $replacement = $2; - eval { $subbuffer{$sender_nick} =~ s/$toreplace/$replacement/ }; - if (not $@ and $subbuffer{$sender_nick}) { + my $retext = $1; + my $repl = $2; + my $mods = $3 // ''; + my $regex; + my $imod = $mods =~ /i/ ? 'i' : ''; + eval { + $regex = qr/(?$imod:$retext)/ + }; + return 1 if $@; + my $ismatch; + if ($mods =~ /g/) { + $ismatch = $subbuffer{$sender_nick} =~ s/$regex/$repl/g; + } else { + $ismatch = $subbuffer{$sender_nick} =~ s/$regex/$repl/; + } + if ($ismatch) { msg($sock, "${sender_nick} meant to say: $subbuffer{$sender_nick}"); - return 1; } + return 1; } elsif ($message =~ m,watch\?v=([a-zA-Z0-9_-]+),) { # post youtube video titles from video ID my $video_id = $1; @@ -194,8 +207,8 @@ sub respond { } else { msg($sock, "failed to get title of video ${video_id} ${MATA_DEAD} (no videos matching ID $video_id!)"); } - } elsif ($message =~ m,(https?://[^\000\r\n ]+),) { - my $url = $1; + } elsif ($message =~ m,https?://[^ ]+,) { + my $url = $&; # get in it's HEAD to check if it's text/html my $response = HTTP::Tiny->new->head($url); unless ($response->{success}) { @@ -231,10 +244,11 @@ sub respond { } else { msg($sock, "No title found (GET ${url} ${MATA_DEAD}"); } - } elsif ($message =~ /^ *${NICK}[:, ] *([^\000\r\n ][^\000\r\n]*)$/ - or $message =~ /^ *([^\000\r\n ][^\000\r\n]*)[, ] *${NICK}\W*$/) { + # TODO: this part should use [^ ] and .* instead but i'm scared of .* + } elsif ($message =~ /^ *${NICK_RE}[:, ] *([^\000\r\n ][^\000\r\n]*)$/ + or $message =~ /^ *([^\000\r\n ][^\000\r\n]*)[, ] *${NICK_RE}\W*$/) { respond_command($sock, $sender_nick, $1); - } elsif ($message =~ /\b${NICK}\b/) { + } elsif ($message =~ /\b${NICK_RE}\b/) { respond_mention($sock, $sender_nick, $message); } return 0; diff --git a/matabot.8 b/matabot.8 @@ -22,19 +22,19 @@ starts an IRC bot nicknamed .Sq mata_bot and joins the channel .Sq #testmatabot -on localhost (port 6667) with with TLS turned off by default. It's typically -started as a daemon via an rc.d script. +on localhost (port 6667) with with TLS turned off by default. +It's typically started as a daemon via an rc.d script. .Pp The options are as follows: .Bl -tag -width Ds .It Fl H Ar path Load the hellos file from .Ar path -.Po default: Pa $PREFIX/share/matabot/hellos Pc Ns . +.Po default: Pa /usr/local/share/matabot/hellos Pc Ns . .It Fl b Ar path Load the 8ball file from .Ar path -.Po default: Pa $PREFIX/share/matabot/ball Pc Ns . +.Po default: Pa /usr/local/share/matabot/ball Pc Ns . .It Fl h Ar host Connect to .Ar host @@ -53,50 +53,65 @@ Connect to .It Fl q Ar path Load the quotes file from .Ar path -.Po default: Pa $PREFIX/share/matabot/quotes Pc Ns . +.Po default: Pa /usr/local/share/matabot/quotes Pc Ns . .It Fl t Turn on TLS. .El .Sh COMMANDS -Bot can do some tricks (non-exhaustive list. he's kinda flexible so just speak to him :3): +Bot can do some tricks (non-exhaustive, or it won't be funny): .Bl -tag -width Ds -.It ...mata_bot... -Says something! This boy talks back!? -.It mata_bot, What doth life? or mata_bot, What doth life? -Answers your burning questions... (can't hear you if you don't -.Sq \&? -him). -.It ...http://what.ever/idk#lol... or ...https://what.ever/idk#lol... -Reads the website title or content-type for you. +.It s/HaCkERS/wizards/ig +He corrects you. .It ...watch?=SOMEID... -Reads out the youtube video title for you (make sure that the char after end of +He reads out youtube video titles for you. +(Make sure that the character after the end of .Sq SOMEID -is outside the range of valid characters for youtube video ID's okay?). +is outside the range of valid characters for a youtube video ID, okay?). +.It ...http://what.ever/idk#lol... or ...https://what.ever/idk#lol... +He reads out website titles or the content-type for you. +(Make sure the character after the end of your link is a space) .It mata_bot ...${number1}d${number2}... -Rolls some ${number1} of ${number2}-sided dice. He's a dungeon master too!! -.It mata_bot, good boy! (or boi, girl, bot) -Praise your future master. -.It thank you mata_bot!! -Thank your future master. -.It hi, mata_bot! -Say hi to the boy +Ask him to roll ${number1} of ${number2}-sided dice. +He's a dungeon master too!! .It mata_bot: *pray* Stay prayed up!! -.It wassup mata_bot? -Ask him what he's up to. +.It MATABOT I LOVE U!!! +Love the boy. +.It thank you mata_bot!! +Thank your future master. +.It mata_boi: Who's the good boy?? (or boi, girl, bot, etc.). +Praise your future master. +.It .../mata_?bo[ity]/i... +Mention him! This boy talks back!? .It mata_bot, hru? Ask him how he's doin. +.It wassup mata_bot? +Ask him what he's up to. +.It mata_bot, good boy! (or boi, girl, bot, etc.). +Praise your future master. +.It hi, mata_boy! +Say hi to the boy +.It mata_bot, What doth life? +Ask him your burning questions... +(He can't hear you if you don't +.Sq \&? +him :c). .El .Sh FILES .Bl -tag -width Ds .It Pa /etc/rc.d/matabot OpenBSD rc script for running matabot as a service. .It Pa /usr/local/share/matabot/ball -8ball answers file. has 10 answers for positive, negative, and ambigious each +8ball answers file. +has 10 answers for positive, negative, and ambigious each; keep the ratio when adding new lines as the answer is chosen randomly from the file. +.It Pa /usr/local/share/matabot/hellos +Random greetings. .It Pa /usr/local/share/matabot/quotes -Random quotes; mostly from the https://analognowhere.com comic. +Random quotes; mostly from the +.Lk https://analognowhere.com +comic. .El . .Sh EXIT STATUS @@ -106,15 +121,18 @@ Random quotes; mostly from the https://analognowhere.com comic. Typical invocation .Po use these flags in your Pa /etc/rc.conf.local Pc Ns : .Pp -.Dl $ matabot -tlh irc.server.tld -j #analognowhere -p 6697 +.Dl $ matabot -th irc.server.tld -j #analognowhere -p 6697 .Pp -Test on a local ircd (without TLS): +Test on a local ircd with logging turned on. +(No TLS): .Pp .Dl $ matabot -l . .Sh SEE ALSO .Xr perl 1 , -.Xr IO::Socket 3p +.Xr HTTP::Tiny 3p , +.Xr IO::Socket 3p , +.Xr IO::Socket::SSL 3p . .Sh STANDARDS .Bl -item @@ -152,10 +170,14 @@ Test on a local ircd (without TLS): .An noodle Aq Mt noodle@pastanoggin.com Ns . . .Sh CAVEATS -Noodle (noodle) is the only person in chat who can give direct commands to mata_bot (it's hard-coded. sorry). +Noodle (noodle) is the only person in chat who can give direct commands to mata_bot. +(It's hard-coded. +Sorry). .Sh BUGS -The bot doesn't have the ability te re-connect on network loss yet. You can +The bot doesn't have the ability te re-connect on network loss yet. +You can .Sq rcctl restart matabot for now. .Pp -The bot cannot detach from terminal yet to become a daemon. daemon-izing is handled in the rc service script. +The bot cannot detach from terminal yet to become a daemon. +daemon-izing is handled in the rc service script.