|
|
| version 1.4, 2025/02/03 17:31:28 | version 1.6, 2025/02/05 01:03:18 |
|---|---|
| Line 9 | Line 9 |
| # Licensed AGPL-3.0 | # Licensed AGPL-3.0 |
| # | # |
| # $Log$ | # $Log$ |
| # Revision 1.6 2025/02/05 01:03:18 snw | |
| # Fix invite and seen commands to support screen names with spaces | |
| # | |
| # Revision 1.5 2025/02/03 18:14:15 snw | |
| # Further work on bot | |
| # | |
| # Revision 1.4 2025/02/03 17:31:28 snw | # Revision 1.4 2025/02/03 17:31:28 snw |
| # Further MySQL work | # Further MySQL work |
| # | # |
| Line 48 my $online = 0; | Line 54 my $online = 0; |
| my $chat_idle_seconds = 0; | my $chat_idle_seconds = 0; |
| my $last_chat_received = time(); | my $last_chat_received = time(); |
| my $start_time = time(); | my $start_time = time(); |
| my $dbh = ''; | |
| my $dsn = ''; | |
| my @congregants = (); | my @congregants = (); |
| Line 73 sub update_seen_status { | Line 81 sub update_seen_status { |
| my($sn) = @_; | my($sn) = @_; |
| my $del = $dbh->prepare("DELETE FROM seen WHERE aim_server=? AND aim_sn=? AND aim_chatroom=? AND sn=?"); | my $del = $dbh->prepare("DELETE FROM seen WHERE aim_server=? AND aim_sn=? AND aim_chatroom=? AND sn=?"); |
| $del->execute($botsrv, $botsn, $chatroom, $sn); | $del->execute($botsrv, $botsn, $chatroom, $sn) or die "error DBI->errstr()"; |
| my $ins = $dbh->prepare("INSERT INTO seen (aim_server, aim_sn, aim_chatroom, sn, seen_time) VALUES (?, ?, ?, ?, ?"); | my $ins = $dbh->prepare("INSERT INTO seen (aim_server, aim_sn, aim_chatroom, sn, seen_time) VALUES (?, ?, ?, ?, ?)"); |
| $ins->execute($botsrv, $botsn, $chatroom, $sn, localtime); | my $seentime = localtime(); |
| $ins->execute($botsrv, $botsn, $chatroom, $sn, $seentime) or die "error DBI->errstr()"; | |
| } | } |
| sub signon_done { | sub signon_done { |
| print "[OK]\n"; | print "[OK]\n"; |
| print "Joining $chatroom..."; | print "convobot: joining $chatroom..."; |
| $oscar->chat_join($chatroom, 5); | $oscar->chat_join($chatroom, 5); |
| print "[OK]\n"; | print "[OK]\n"; |
| $online = 1; | $online = 1; |
| } | } |
| Line 91 sub oscar_error { | Line 100 sub oscar_error { |
| my($oscar, $connection, $error, $description, $fatal) = @_; | my($oscar, $connection, $error, $description, $fatal) = @_; |
| if($fatal != 0) { | if($fatal != 0) { |
| die "\nFatal OSCAR error: $description\n"; | die "\nconvobot: fatal OSCAR error: $description\n"; |
| } | } |
| else { | else { |
| print "\nRecoverable OSCAR error: $description\n"; | print "\nconvobot: recoverable OSCAR error: $description\n"; |
| } | } |
| } | } |
| Line 102 sub oscar_error { | Line 111 sub oscar_error { |
| sub chat_joined { | sub chat_joined { |
| my($oscar, $chatname, $chat) = @_; | my($oscar, $chatname, $chat) = @_; |
| print "bot: chat joined [$chatname]\n"; | |
| $room = $chat; | $room = $chat; |
| bless $room, "Net::OSCAR::Connection::Chat"; | bless $room, "Net::OSCAR::Connection::Chat"; |
| print "convobot: connecting to database $dbname\@$dbhost..."; | |
| $dsn = "DBI:mysql:database=$dbname;host=$dbhost;port=3306;mysql_connect_timeout=5;"; | |
| $dbh = DBI->connect($dsn, $dbusername, $dbpw, {RaiseError => 1}); | |
| die "convobot: failed to connect to MySQL database: DBI->errstr()" unless $dbh; | |
| print "[OK]\n"; | |
| $oscar->set_callback_chat_buddy_in(\&chat_buddy_in); | |
| $oscar->set_callback_chat_buddy_out(\&chat_buddy_out); | |
| } | } |
| sub chat_buddy_in { | sub chat_buddy_in { |
| Line 113 sub chat_buddy_in { | Line 135 sub chat_buddy_in { |
| if($who ne $botsn) { | if($who ne $botsn) { |
| push(@congregants, $who); | push(@congregants, $who); |
| print "[$who] has joined\n"; | print "convobot: [$who] has joined\n"; |
| } | } |
| else { | else { |
| print "[$who] has joined (ignoring bot)\n"; | print "convobot: [$who] has joined (ignoring bot)\n"; |
| } | } |
| Line 135 sub chat_buddy_in { | Line 157 sub chat_buddy_in { |
| $chat->chat_send($phrasefix); | $chat->chat_send($phrasefix); |
| } | } |
| else { | else { |
| print "Not sending greeting for 2 seconds after startup\n"; | print "convobot: not sending greeting for 2 seconds after startup\n"; |
| } | } |
| } | } |
| } | } |
| Line 147 sub chat_buddy_out { | Line 169 sub chat_buddy_out { |
| $index++ until $congregants[$index] eq $who; | $index++ until $congregants[$index] eq $who; |
| splice(@congregants, $index, 1); | splice(@congregants, $index, 1); |
| print "$who has left\n"; | print "convobot: $who has left\n"; |
| } | } |
| sub chat_im_in { | sub chat_im_in { |
| Line 158 sub chat_im_in { | Line 180 sub chat_im_in { |
| my @cmd = split(' ', $rawcmd); | my @cmd = split(' ', $rawcmd); |
| update_seen_status($who); | update_seen_status($who); |
| if($cmd[0] eq "!seen") { | if($who ne $botsn) { |
| if(exists($cmd[1])) { | if($cmd[0] eq "!seen") { |
| get_seen_status($cmd[1], $chat); | if(exists($cmd[1])) { |
| } | my @sna = @cmd[1..$#cmd]; |
| else { | my $ssn = join(' ', @sna); |
| $chat->chat_send("Syntax: !seen <em>screenname</em>"); | get_seen_status($ssn, $chat); |
| } | } |
| else { | |
| $chat->chat_send("Syntax: !seen <em>screenname</em>"); | |
| } | |
| } | |
| elsif($cmd[0] eq "!speak") { | |
| send_idle_message(); | |
| } | |
| elsif($cmd[0] eq "!quote") { | |
| my $fortune = `/usr/games/fortune`; | |
| $room->chat_send($fortune); | |
| } | |
| elsif($cmd[0] eq "!invite") { | |
| if(exists($cmd[1])) { | |
| my @sna = @cmd[1..$#cmd]; | |
| my $ssn = join(' ', @sna); | |
| $chat->invite($ssn, "Please join us in $chatroom! <br><em>Requested by $who</em>"); | |
| } | |
| } | |
| elsif($cmd[0] eq "!help") { | |
| $room->chat_send("You can enter the following commands:"); | |
| $room->chat_send(" <code>!seen <em>screenname</em></code> (find out when <em>screenname</em> was last in the chat)"); | |
| $room->chat_send(" <code>!invite <em>screenname</em></code> (invite <em>screenname</em> to the chat)"); | |
| $room->chat_send(" <code>!speak</code> (send a random message)"); | |
| $room->chat_send(" <code>!quote</code> (send a quote)"); | |
| } | |
| } | } |
| $last_chat_received = time(); | $last_chat_received = time(); |
| print "chat received from $who; resetting idle counter\n"; | print "convobot: chat received from $who; resetting idle counter\n"; |
| } | } |
| Line 197 sub send_idle_message { | Line 243 sub send_idle_message { |
| $room->chat_send($phrasefix); | $room->chat_send($phrasefix); |
| $last_chat_received = time(); | $last_chat_received = time(); |
| } | } |
| } | } |
| $oscar->set_callback_signon_done(\&signon_done); | $oscar->set_callback_signon_done(\&signon_done); |
| $oscar->set_callback_chat_joined(\&chat_joined); | $oscar->set_callback_chat_joined(\&chat_joined); |
| $oscar->set_callback_chat_buddy_in(\&chat_buddy_in); | |
| $oscar->set_callback_chat_buddy_out(\&chat_buddy_out); | |
| $oscar->set_callback_chat_im_in(\&chat_im_in); | $oscar->set_callback_chat_im_in(\&chat_im_in); |
| $oscar->set_callback_error(\&oscar_error); | $oscar->set_callback_error(\&oscar_error); |
| Line 236 print "DB Username: $dbusername\n" | Line 281 print "DB Username: $dbusername\n" |
| print "Idle before ping: $idlemax\n"; | print "Idle before ping: $idlemax\n"; |
| print "Auto-Greet: $autogreet\n\n"; | print "Auto-Greet: $autogreet\n\n"; |
| print "Connecting to database $dbname\@$dbhost..."; | |
| my $dsn = "DBI:mysql:database=$dbname;host=$dbhost;port=3306;mysql_connect_timeout=5;"; | |
| my $dbh = DBI->connect($dsn, $dbusername, $dbpw, {RaiseError => 1}); | |
| die "Failed to connect to MySQL database: DBI->errstr()" unless $dbh; | |
| print "[OK]\n"; | |
| print "bot: attempting to sign in... "; | print "convobot: attempting to sign in..."; |
| $oscar->signon(%signon); | $oscar->signon(%signon); |
| while(1) { | while(1) { |