LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Perl guys! Time delay 4 Eliza bot Gaim response - want to have fun :) (https://www.linuxquestions.org/questions/programming-9/perl-guys-time-delay-4-eliza-bot-gaim-response-want-to-have-fun-238249/)

lrt2003 10-03-2004 11:44 AM

Perl guys! Time delay 4 Eliza bot Gaim response - want to have fun :)
 
Hey everyone!

I was wondering if you could help me. There's this IM bot called Eliza for Gaim.

I would like to fool a few friends... but it answers too damn fast. do you know how to modify it to set a time delay?

I have posted the code for convenience.

It could only be a few lines? If you could help it would be much appreciated.

#ElizaLovesYou v0.2 - Rogerian psychotherapist for Gaim
#Copyright (C) 2004 Scott Wolchok

#This program is free software; you can redistribute it and/or
#modify it under the terms of the GNU General Public License
#as published by the Free Software Foundation; either version 2
#of the License, or (at your option) any later version.

#This program is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
#GNU General Public License for more details.

#You should have received a copy of the GNU General Public License
#along with this program; if not, write to the Free Software
#Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

#(You can find the GPL at http://www.gnu.org/copyleft/gpl.html)

#You'll need to install Chatbot::Eliza from CPAN before this will work.
#Run 'perl -MCPAN -e "shell"' and answer the questions, then type
#'install Chatbot::Eliza' at the prompt. That should do it!

#Please note that this plugin is just a hook for the Perl Chatbot::Eliza
#module - the credit for the actual "bot" goes to the John Nolan, the
#writer of that module.

#Superdug is great!!!!

use Gaim;
use Chatbot::Eliza;
use strict;

my %bots;
my $plugin;
#my @timeouts; #Gaim segfaults if we leave the send_message timeout and unload the plugin

%::PLUGIN_INFO = (
perl_api_version => 2,
name => "ElizaLovesYou",
version => "0.2",
summary => "Rogerian psychotherapist for Gaim",
description => "Automatically responds to incoming messages on all accounts using the Eliza algorithm",
author => "Scott Wolchok <evilsporkman\@gmail.com>",
url => "none",

load => "plugin_load",
unload => "plugin_unload"
);

sub plugin_init {
return %::PLUGIN_INFO;
}

sub do_eliza {
Gaim::debug_info($::PLUGIN_INFO{name}, "Entering do_eliza\n");
my ($account, $sender, $message, $flags) = @_;
Gaim::debug_info($::PLUGIN_INFO{name}, "Displayed: $message\n");
$bots{$sender} = new Chatbot::Eliza unless $bots{$sender};

while($message =~ s/<.*?>//) {}; #HTML si t3h sux0r!

my $theIM = getIM($sender);

#Tried to use a queue to hold timeouts so we could remove them at plugin_unload - the thing segfaults if there's a timeout pending and you unload it
# push @timeouts, Gaim::timeout_add($plugin,10,\&send_message,[$theIM,$bots{$sender}->transform($message)]);
Gaim::timeout_add($plugin,1,\&send_message,[$theIM,$bots{$sender}->transform($message)]);

Gaim::debug_info($::PLUGIN_INFO{name}, "Leaving do_eliza\n");
}

sub send_message {
Gaim::debug_info($::PLUGIN_INFO{name}, "Entering send_message\n");
my ($IM, $message) = @{$_[0]};

#Gaim::debug_info($::PLUGIN_INFO{name}, "Shifting timeouts\n");
#shift @timeouts;

Gaim::debug_info($::PLUGIN_INFO{name}, "Sending message\n");
Gaim::Conversation::IM::send($IM,$message);

return undef;
}
sub plugin_load {
$plugin = shift;

Gaim::signal_connect(Gaim::Conversations::handle, "received-im-msg", $plugin, \&do_eliza, 0);
}

sub plugin_unload {
my $plugin = shift;

# for my $timeout (@timeouts) {
# Gaim::timeout_remove($timeout);
# }
}

#Note: I got this from forsaken in #gaim on FreeNode and renamed it to getIM, his original credit follows:
#From Split.pl and edited.
#Michael Wozniak <martianpenguin@hackermail.com> and Anthony Noto <agnoto1@yahoo.com>
sub getIM
{
my $thing = $_[0];
my @im_array = Gaim::ims(); #get the array of IM's
foreach my $element (@im_array)
{
if ( Gaim::Conversation::get_name( Gaim::Conversation::IM::get_conversation ( $element ) ) eq $thing )
{
return $element; #return the correct IM
}
}

return undef;
}

lrt2003 10-03-2004 12:04 PM

Ah, I got it.

Set Gaim::timeout_add($plugin,1,\&send_message,[$theIM,$bots{$sender}->transform($message)]);

to Gaim::timeout_add($plugin,5,\&send_message,[$theIM,$bots{$sender}->transform($message)]);

for a realistic reply :)


All times are GMT -5. The time now is 07:26 PM.