ewtoo-eliza_1.4/
ewtoo-eliza_1.4/Chatbot/
#!/usr/bin/perl -w

# ewtoo-eliza
# Implimentation of Eliza for ewtoo talkers
# silver@ewtoo.org / 16-Aug-2001
# v1.0 - Initial release
# v1.1 - Bot can now join in conversations
# v1.2 - Bot can "hook" onto one individual for a period of time and
#        assume all says are directed at them
# v1.3 - Fixed room and recho exploit
# v1.4 - Really fixed recho exploit as it wasn't fixed before.
#        Bot picked up on "thinks"
#        Output to terminal window isn't cluttered with control characters

use strict;
# This needs to point to where the Chatbot directory is
use lib '/home/richard/ewtoo-eliza_1.4/';
use Chatbot::Eliza;
use IO::Socket;

# The talker to connect to
my $site = "address.com";
my $port = 1234;

# Username and password
my $username = "eliza";
my $password = "abc123";

# Who owns the bot and who, therefore, can control it.
my $owner = "YourName";

# how many "says" the bot will listen to once its attention is got
my $attention = 2; 

# 1 in x chance of saying something unprompted to the main room. The lower
# the more spammy.
my $chattyness = 15;

# YOU DON'T NEED TO EDIT ANYTHING BELOW HERE
# ==========================================================================

my $sock;
my $hook = "";  # who the bot is hooked onto 
my $hookcount = 0;
my $cmd = "";
my $liz;
if (-e "$username-data.txt") {
  $liz = new Chatbot::Eliza "$username", "$username-data.txt";
} else {
  $liz = new Chatbot::Eliza "$username";
}
my $version = "1.4";
my $reldate = "16-Aug-2001";

sub sendcmd
{
  return if (!$sock || !$_[0]);
  
  # sleep for a bit to look real
  select(undef, undef, undef, rand(2)+2);
  
  # send  
  print $sock $_[0];
}

sub make_connection
{
  $sock = IO::Socket::INET->new(PeerAddr => $site,
                                PeerPort => $port,
                                Proto    => "tcp",
                                Type     => SOCK_STREAM,
                                Timeout  => 5) || die "Can't connect to $site $port: $!";
  $sock->autoflush(1);  # just in case its an old version of IO::Socket
  
  sendcmd("$username\n$password\n\n\n\n");
}

sub makereply
{
  if ($_[1]) {
    # catch anyone using room to get it to tell the room
    return undef if ($_[0] =~ /^room$/i);

    # catch anyone using recho
    return undef if ($_[0] =~ /[^a-z_]/i);

    return "tell $_[0] " . $liz->transform($_[1]) . "\n";
  } else {
    return("say " . $liz->transform($_[0]) . "\n");
  }
}

sub toggle_hook
{
  if ($hook eq $_[0]) {
    $hookcount--;
    if ($hookcount <= 0) {
      print "## Attention to $hook lost.\n";
      return "";
    } else {
      return $hook;
    }
  }
  else {
    $hookcount = $attention;
    print "## Attention now on $_[0].\n";
    return $_[0];
  }
}

print "ewtoo-eliza v$version / Silver / $reldate\n";
print "username: $username  password: $password\n";
print "attention set to $attention.  Chattyness set to $chattyness.\n";

make_connection();

while(<$sock>) {
  tr/\t\n\f\r -~//cd;
  print "$_";
  chomp;
  
  # so people can remotely control the bot
  
  if (/^> *$owner (tells you|asks of you|exclaims to you|) 'hook: *(.+)'/i) {
    $hook = $2;
    $hookcount = $attention;
    sendcmd("tell $owner my attention is now on $2\n");
  }

  elsif (/^> *$owner (tells you|asks of you|exclaims to you|) 'execute: *(.+)'/i) {
    sendcmd("$2\nremote $owner just executed: $2\n");
  }
  
  # direct tell, so respond
    
  elsif (/^> *(\w+) (tells you|asks of you|exclaims to you|) '(.+)'/i) {
    sendcmd(makereply($1, $3));
  }
  
  # indirect tell. Note for consistancy we have a (thinks)
  # to ensure that $3 and $1 are the same.
  
  elsif (/^ *(\w+) (says|asks|exclaims) ' *(.+)'/i ||
         /^ *(\w+) (thinks) . o O \( (.+) \)/i) {
    # have they mentioned our name?
    my $msg = $3;
    my $target = $1;

    if ($hook eq $target || $msg =~ /\b$username\b/i) {
      sendcmd(makereply($msg));
      $hook = toggle_hook($target);
    }

    # they haven't mentioned our name but we might still say something

    elsif ($hook eq $target || int(rand($chattyness)) == int($chattyness/2)) {
      sendcmd(makereply($msg));
      $hook = toggle_hook($target);
    }
  }
    
}
close $sock;

print "-- Connection closed\n";
exit;