Annotation of ChivanetAimDiscordSyncBot/chatsync.pl, revision 1.2

1.1       snw         1: #!/usr/bin/env perl
                      2: 
                      3: # 
                      4: # ChivaNet AIM<>Discord Chat Sync Bot 
                      5: #  Copyright (C) 2024, 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: chatsync.pl,v $
        !            12: # Revision 1.1.1.1  2025/01/30 15:56:56  snw
        !            13: # Initial commit
        !            14: #
1.1       snw        15: #
                     16: 
                     17: use Net::OSCAR qw(:standard);
                     18: use HTML::Strip;
                     19: use REST::Client;
                     20: use JSON::XS;
                     21: use Data::Dumper;
                     22: use Getopt::Long;
                     23: use WebService::Discord::Webhook;
                     24: 
                     25: my $botsn = '';
                     26: my $botsrv = '';
                     27: my $botpw = '';
                     28: my $chatroom = '';
                     29: my $webhook_url = '';
                     30: 
                     31: $chat = '';
                     32: 
                     33: GetOptions("aimsn=s" => \$botsn,
                     34:             "aimhost=s" => \$botsrv,
                     35:             "aimpw=s" => \$botpw,
                     36:             "chatroom=s" => \$chatroom,
                     37:             "url=s" => \$webhook_url)
                     38:             or die("error in command line arguments");
                     39: 
                     40: %signon = (
                     41:         screenname => $botsn,
                     42:         password => $botpw,
                     43:         host => $botsrv,
                     44: );       
                     45: 
                     46: $oscar = Net::OSCAR->new();
                     47: $webhook = WebService::Discord::Webhook->new( $webhook_url );
                     48: 
                     49: print ("ChivaNet Discord AIM Synchronizer v0.0.1\n");
                     50: print ("  Connecting to chat room $chatroom\n");
                     51: 
                     52: sub signon_done {
                     53:         print "[OK]\n";
                     54:         $chat = $oscar->chat_join($chatroom, 5);        
                     55:         $online = 1;
                     56: }
                     57: 
                     58: sub chat_joined {
                     59:     my($oscar, $chatname, $chat) = @_;
                     60: 
1.2     ! snw        61:     print "discord chatsync engaged for $chatroom\n";
1.1       snw        62: }
                     63: 
                     64: sub im_in {
                     65:     my($oscar, $sender, $message, $is_away) = @_;
                     66:     print "$sender: $message\n";
                     67: }
                     68: 
                     69: sub chat_im_in {
                     70:     my($oscar, $who, $chat, $message) = @_;
                     71:     
                     72:     my $hs = HTML::Strip->new();
                     73:     my $msg = $hs->parse($message);
                     74:     $msg =~ s/\@//g;
                     75:     $msg =~ s/https/gopher/g;
                     76:     $msg =~ s/http/gopher/g;
                     77:     $msg =~ s/ftp/gopher/g;
                     78: 
                     79:     $msg =~ s/:-\)/:smile:/g;
                     80:     $msg =~ s/;-\)/:wink:/g;
                     81:     $msg =~ s/:-$/:money_mouth:/g;
                     82:     $msg =~ s/=-O/:astonished:/g;
                     83:     $msg =~ s/:-X/:zipper_mouth:/g;
                     84: 
                     85:     
                     86:     
                     87:     my $fmsg = "**$who**: $msg";
                     88: 
                     89:     $webhook->execute($fmsg);
                     90: }
                     91: 
                     92: #OSCAR, SCREENNAME, CHAT, BUDDY DATA
                     93: sub chat_buddy_in {
                     94:     my ($oscar, $who, $chat, $buddy) = @_;
                     95:     my $fmsg = "*$who has joined the chat*";
                     96:     sleep(1);
                     97:     $webhook->execute($fmsg);
                     98: }
                     99: 
                    100: sub chat_buddy_out {
                    101:     my ($oscar, $who, $chat) = @_;
                    102:     my $fmsg = "*$who has left the chat*";
                    103:     sleep(1);
                    104:     $webhook->execute($fmsg);
                    105: }
                    106: 
                    107: 
                    108: 
                    109: $oscar->set_callback_signon_done(\&signon_done);
                    110: $oscar->set_callback_chat_joined(\&chat_joined);
                    111: $oscar->set_callback_im_in(\&im_in);
                    112: $oscar->set_callback_chat_im_in(\&chat_im_in);
                    113: #$oscar->set_callback_chat_buddy_in(\&chat_buddy_in);
                    114: #$oscar->set_callback_chat_buddy_out(\&chat_buddy_out);
                    115: 
                    116: print "chatsync:  attempting to sign in... ";
                    117: $oscar->signon(%signon);
                    118: 
                    119: while(1) {
                    120:     $oscar->do_one_loop();        
                    121: }

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