File:  [Coherent Logic Development] / ChivanetChatBot / bot.pl
Revision 1.1.1.1 (vendor branch): download - view: text, annotated - select for diffs
Thu Jan 30 05:27:42 2025 UTC (2 months ago) by snw
Branches: MAIN, CoherentLogicDevelopment
CVS tags: start, HEAD
ChivaNet Chat Bot

#!/usr/bin/env perl

# 
# ChivaNet AIM Bot 
#  Copyright (C) 2024, 2025 Coherent Logic Development LLC
#
# Author: Serena Willis <snw@coherent-logic.com>
#
# Licensed AGPL-3.0
#
#   $Log: bot.pl,v $
#   Revision 1.1.1.1  2025/01/30 05:27:42  snw
#   ChivaNet Chat Bot
#
#

use Net::OSCAR qw(:standard :loglevels);
use HTML::Strip;
use REST::Client;
use JSON::XS;
use Data::Dumper;
use Getopt::Long;

my $botsn = '';
my $botsrv = '';
my $botpw = '';
my $rasurl = '';

GetOptions("aimsn=s" => \$botsn,
            "aimhost=s" => \$botsrv,
            "aimpw=s" => \$botpw,
            "rasurl=s" => \$rasurl)
            or die("error in command line arguments");


%signon = (
        screenname => $botsn,
        password => $botpw,
        host => $botsrv,
); 

$oscar = Net::OSCAR->new();
%invites;

sub signon_done {
        print "[OK]\n";
        $online = 1;
}

sub chat_joined {
    my($oscar, $chatname, $chat) = @_;

    print "bot:  joined $chatname\n";

    foreach my $i ( 0 .. $invites{$chatname}->$#* ) {
        $invitee = $invites{$chatname}[$i];
        print "bot:  inviting $invitee to $chatname...\n";
        $chat->chat_send("Inviting $invitee to $chatname...\n");
        $chat->invite($invitee, "Please join us in $chatname");
    }

    delete($invites{$chatname});

    $chat->part();
}

sub im_in {

    my($oscar, $sender, $message, $is_away) = @_;
    my $hs = HTML::Strip->new();
    my $rawcmd = $hs->parse($message);
    my @cmd = split(' ', $rawcmd);

    print "bot:  command $cmd[0] received from $sender\n";

    if($cmd[0] eq "ping") {
        $oscar->send_im($sender, "pong", $is_away);
    }
    elsif($cmd[0] eq "join") {
        my @rooma = @cmd[1..$#cmd];
        my $room = join(' ', @rooma);
        my $client = REST::Client->new();
        $client->GET("$rasurl/chat/room/public");
        my $json = $client->responseContent();
        my $arrayref = decode_json $json;
        foreach my $hash(@{$arrayref}) {
            if ($$hash{"name"} eq $room) {
                print "bot:  sending invite to $sender for $room\n";
                push $invites{$room}->@*, $sender;
                $oscar->send_im($sender, "Okay, I'll invite you to <b>$room</b>. You may need to have me in your buddy list for this to work.", $is_away);
                my $chat = $oscar->chat_join($room, 5);    
                return;
            }
        }
        $oscar->send_im($sender, "You have specified a chat room that does not exist. Please remember that chat room names are case-sensitive.", $is_away);

    }
    elsif($cmd[0] eq "listchat") {
        my $client = REST::Client->new();
        $client->GET("$rasurl/chat/room/public");
        my $json = $client->responseContent();
        my $arrayref = decode_json $json;
        
        print "bot:  sending chat list to $sender\n";
        foreach my $hash(@{$arrayref}) {
            my $url = "<a href=\"" . $$hash{"url"} . "\">" . $$hash{"name"} . "</a>";                     
            $oscar->send_im($sender, $url, $is_away);
        }
        $oscar->send_im($sender, "<b>You may join one of the above chat rooms by clicking the link or sending me 'join <i>chat-room-name</i>' to request an invite.</b>");
    }
    else {
        $oscar->send_im($sender, "<b>You can send me any of the following commands:</b>", $is_away);
        $oscar->send_im($sender, " <b>help</b>: lists available bot commands", $is_away);
        $oscar->send_im($sender, " <b>ping</b>: tests the bot for response", $is_away);
        $oscar->send_im($sender, " <b>listchat</b>: lists the public chat roooms available on ChivaNet", $is_away);
        $oscar->send_im($sender, " <b>join <i>chat-room-name</i></b>: joins public chat room <i>chat-room-name</i>", $is_away);   
        $oscar->send_im($sender, " <b><font color=red>Please note that <i>chat-room-name</i> is case-sensitive</font></b>", $is_away);             
    }

}

$oscar->set_callback_signon_done(\&signon_done);
$oscar->set_callback_im_in(\&im_in);
$oscar->set_callback_chat_joined(\&chat_joined);

print "ChivaNet AIM Bot v0.1.0\n";
print " Copyright (C) 2024, 2025 Coherent Logic Development LLC\n\n";

print "bot:  attempting to sign in... ";
$oscar->signon(%signon);

while(1) {
    $oscar->do_one_loop();        
}

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