Annotation of ChivanetConvoBot/convobot, revision 1.2

1.1       snw         1: #!/usr/bin/env perl
                      2: 
                      3: # 
                      4: # ChivaNet Conversation Bot 
                      5: #  Copyright (C) 2025 Coherent Logic Development LLC
                      6: #
                      7: # Author: Serena Willis <snw@coherent-logic.com>
                      8: #
                      9: # Licensed AGPL-3.0
                     10: #
1.2     ! snw        11: # $Log: convobot,v $
        !            12: # Revision 1.1.1.1  2025/02/03 04:22:49  snw
        !            13: # Initial Commit
        !            14: #
1.1       snw        15: #
                     16: #
                     17: 
                     18: use Net::OSCAR;
                     19: use Getopt::Long;
                     20: use Data::Dumper;
                     21: use HTML::Strip;
                     22: 
                     23: my $idlemax = 1800;
                     24: my $botsn = '';
                     25: my $botsrv = '';
                     26: my $botpw = '';
                     27: my $rasurl = '';
                     28: my $chatroom = '';
                     29: my $online = 0;
                     30: my $chat_idle_seconds = 0;
                     31: my $last_chat_received = time();
                     32: my $start_time = time();
                     33: 
                     34: my @congregants = ();
                     35: my %seen = ();
                     36: 
                     37: GetOptions("aimsn=s" => \$botsn,
                     38:            "aimhost=s" => \$botsrv,
                     39:            "aimpw=s" => \$botpw,
                     40:            "idlemax=s" => \$idlemax,
                     41:            "chatroom=s" => \$chatroom)
                     42:             or die("error in command line arguments");
                     43: 
                     44: %signon = (
                     45:     screenname => $botsn,
                     46:     password => $botpw,
                     47:     host => $botsrv,
                     48: ); 
                     49: 
                     50: $oscar = Net::OSCAR->new();
                     51: #$room = Net::OSCAR::Connection::Chat;
                     52: 
                     53: sub signon_done {
                     54:     print "[OK]\n";
                     55:     $oscar->chat_join($chatroom, 5);   
                     56:     $online = 1;
                     57: }
                     58: 
                     59: sub chat_joined {
                     60:     my($oscar, $chatname, $chat) = @_;
                     61: 
                     62:     $room = $chat;
                     63:     bless $room, "Net::OSCAR::Connection::Chat";
                     64: }
                     65: 
                     66: sub chat_buddy_in {
                     67:     my ($oscar, $who, $chat, $buddy) = @_;
                     68: 
                     69:     $seen{$who} = localtime();
                     70:     
                     71:     if($who ne $botsn) {
                     72:         push(@congregants, $who);
                     73:         print "[$who] has joined\n";        
                     74:     }
                     75:     else {
                     76:         print "[$who] has joined (ignoring bot)\n";
                     77:     }
                     78: 
                     79:     if(time() - $start_time > 2) {
                     80:         my @phrases = ('Welcome to [room], [user]! :-)',
                     81:                        'How\'s it going, [user]?',
                     82:                        'Hey [user]! Bring any snacks?',
                     83:                        'Heya [user]! Hope your day is going well!',
                     84:                        'Ooo, [user] has joined [room]! Now the party can start!');
                     85:                 
                     86:         my $phrase = $phrases[rand @phrases];
                     87:         $phrase =~ s/\[user\]/$who/g;
                     88:         $phrase =~ s/\[room\]/$chatroom/g;       
                     89:         my $phrasefix = "<div id=convobot></div>$phrase";
                     90:         $chat->chat_send($phrasefix);
                     91:     }
                     92:     else {
                     93:         print "Not sending greeting for 2 seconds after startup\n";
                     94:     }
                     95: }
                     96: 
                     97: sub chat_buddy_out {
                     98:     my ($oscar, $who, $chat) = @_;
                     99:     my $index = 0;
                    100: 
                    101:     $index++ until $congregants[$index] eq $who;
                    102:     splice(@congregants, $index, 1);
                    103: 
                    104:     print "$who has left\n";
                    105: }
                    106: 
                    107: sub chat_im_in {
                    108:     my($oscar, $who, $chat, $message) = @_;
                    109: 
                    110:     my $hs = HTML::Strip->new();
                    111:     my $rawcmd = $hs->parse($message);
                    112:     my @cmd = split(' ', $rawcmd);
                    113: 
                    114:     $seen{$who} = localtime();
                    115:     
                    116:     if($cmd[0] eq "!seen") {
                    117:         if(exists($cmd[1])) {
                    118:             if(exists($seen{$cmd[1]})) {
                    119:                 $chat->chat_send("I last saw $cmd[1] at $seen{$cmd[1]}");
                    120:             }
                    121:             else {
                    122:                 $chat->chat_send("I've never seen $cmd[1]");
                    123:             }
                    124:         }
                    125:         else {
1.2     ! snw       126:             $chat->chat_send("Syntax: !seen <em>screenname</em>");
1.1       snw       127:         }
                    128:     }
                    129:     
                    130:         
                    131:     
                    132:     $last_chat_received = time();    
                    133: 
                    134:     print "chat received from $who; resetting idle counter\n";
                    135:     
                    136: }
                    137: 
                    138: sub send_idle_message {
                    139: 
                    140:     my @phrases = ('Hey [user]! How are you today?',
                    141:                    'I think [user] should bring us some pizza!',
                    142:                    'What\'s everyone up to here?',
                    143:                    'My, what a beautiful day for a chat here in [room]!',
                    144:                    '[user] always has the most interesting things to say.',
                    145:                    'Remember that time [user] was talking here in [room]?',
                    146:                    'What do all you [room] chatters think about pie?',
                    147:                    '[room] seems dead :\'(. That makes me sad! Maybe [user] has something interesting to say?');
                    148: 
                    149:     my $congregant = $congregants[rand @congregants];
                    150:     my $phrase = $phrases[rand @phrases];
                    151:     $phrase =~ s/\[user\]/$congregant/g;
                    152:     $phrase =~ s/\[room\]/$chatroom/g;    
                    153:     my $phrasefix = "<div id=convobot></div>$phrase";
                    154:     
                    155:     if(ref($room) eq "Net::OSCAR::Connection::Chat") {
                    156:         $room->chat_send($phrasefix);
                    157:         $last_chat_received = time();
                    158:     }
                    159: }
                    160: 
                    161: $oscar->set_callback_signon_done(\&signon_done);
                    162: $oscar->set_callback_chat_joined(\&chat_joined);
                    163: $oscar->set_callback_chat_buddy_in(\&chat_buddy_in);
                    164: $oscar->set_callback_chat_buddy_out(\&chat_buddy_out);
                    165: $oscar->set_callback_chat_im_in(\&chat_im_in);
                    166: 
                    167: print "ChivaNet Conversation Bot v0.0.1\n";
                    168: print " Copyright (C) 2025 Coherent Logic Development LLC\n\n";
                    169: 
                    170: print "bot:  attempting to sign in... ";
                    171: $oscar->signon(%signon);
                    172: 
                    173: while(1) {
                    174:     $oscar->do_one_loop();
                    175:     $chat_idle_seconds = time() - $last_chat_received;
                    176:     
                    177:     if($chat_idle_seconds > $idlemax) {
                    178:         send_idle_message();
                    179:     }
                    180: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>