Annotation of ChivanetConvoBot/convobot, revision 1.1

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

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