#!/usr/bin/perl
use Data::Dumper;
use Acme::Tpyo;
use Acme::Wabby qw(:errors);
use Lingua::StopWords qw( getStopWords );
my $stopwords = getStopWords('de');

my $infodir = $ENV{CONTACT_INFODIR};
$infodir = shift(@ARGV) unless($infodir);

$msg =  "";
while(<>){$msg .= $_}
$msg =~ s/[^A-Za-z0-9äÄöÖüÜß\?\!\)\(\)|\-:;\s"']//g;
$msg =~ s/\s+/ /g;

$ggname = $ENV{'CONTACT_NICK'}||"";
$fname=`head -n 2 $infodir/info | tail -n 1`;
# $lname=`head -n 3 $ENV{CONTACT_INFODIR}/info | tail -n 1`;

my $wabby = Acme::Wabby->new( min_len => 3, max_len => 10,
	punctuation => [".","?","!","..."], case_sensitive => 0,
	hash_file => "$infodir/wabbyhash.dat", 
	list_file => "$infodir/wabbylist.dat",
	autosave_on_destroy => 0, max_attempts => 1000 );

my $hist = `cat $infodir/history`;
$hist =~ s/\d+//g;
$hist =~ s/OUT//g;
$hist =~ s///g;
$hist =~ s/MSG//g;
$hist =~ s/IN//g;
$hist =~ s/URL//g;
$hist =~ s/\n+/. /g;
$hist =~ s/\s+/ /g;
$hist =~ s/Ã¤/ae/g;
$hist =~ s/Ã¼/ue/g;
$hist =~ s/ÃŸ/ss/g;
$hist =~ s/Ã¶/oe/g;
$wabby->add($hist);

if($ggname =~ /^(DontBotherMe)$/){exit}

@outs = ("aha.", "hmm...", "ok...!?", "Unsinn.","...und nun?",
	 "bin grad ziemlich beschäftigt","was machen wir denn da?",
	 "wie jetzt?","ich hab' doch keine Ahnung.","ach.","WAS?");

my @nstopwords = grep {!$stopwords->{$_}} split /\s+/,$msg;

my $fallback = $wabby->spew;
if(length($hist) ){
	@outs = ( grep{$_}map{ $wabby->spew($_) } @nstopwords );
	push(@outs, $fallback) unless(scalar @outs);
}

if ($msg =~ /^\s*(hi|hallo|guten\s|re\s*|(bin\s*)?wieder\s*da)/im){
	@outs = ("hi ".$fname, "hallo ".$fname,"du hier!?") }
elsif ($msg =~ /['"](\w+)['"]/i){ @outs = ("$1!?", "wieso $1?") }
elsif ($msg =~ /(zeit|spaet|spät|morgen|abend|früh|frueh|mittag|\buhr\b|moin)/im){
    use POSIX qw(strftime);
    $tm = strftime("%H:%M", localtime);
    @outs = ("wieso $1? es ist $tm!", "\"$1\" um $tm!?", "$tm!"); }
elsif ($msg =~ /\.\.\s*$/i){ @outs = ("...ja?","d.h.?","...und?"); }
elsif ($msg =~ /(\d+[.,\d]*)/i){
    @outs =("$1? ganz schoen fett!", "was denn, nur $1?",
	    "$1 also...", "ganze $1!","$1 ist etwas zuviel ;-("); }
elsif ($msg =~ /(melde?\s*dich|huhu|biste da|bist du da|jemand da|jetzt? da|noch da|noch dran)/im){
    @outs = ("bin daa!", "ja doch", "yep, bin hier,.","yo"); }
elsif ($msg =~ /\!\s*$/i){
    @outs = ("nicht wirklich!", "ach was!", "nee, jetz?"); }
elsif ($msg =~ /^\s*(Wie|Wo)\b.*\?\s*$/i){  
    @outs = ("da hab' ich keine Ahnung.","kenn' mich nicht aus",
             "woher soll ich das wissen?", "frag' nicht-"); }
elsif ($msg =~ /^\s*(Wer|Was|Wen|Wem|Wessen)\b.*\?\s*$/i){
    @outs = ("Kenne ich nicht", "Sollte ich das wissen?","willst Du das wirklihc wissen?"); }
elsif ($msg =~ /[\s](\w+)\?\s*$/i){
    @outs = ("$1?","Keine ahnung..","weiß net","nö.","wieso \"$1\"?");}
elsif ($msg =~ /^\s*(cu|tschuess|tschüss|tschüß|bye|buy|ts?chau|c\s*ya)\s*$/im){
    @outs = ("cu2!", "bye!", "...und tschuess","c ya","bis bald"); }
elsif ($msg =~ /[;:]-?[\(\|\/]/im) { @outs = ("schade, nicht wahr.","*bemitleid*","armes")}
elsif ($msg =~ /[;:]-?[\)DP]/im){ @outs = ("luschtig.",";-)","")}

srand; $out = $outs[rand(@outs)];

@waits = (1..2);
sleep $waits[rand(@waits)];

# print misspell($out,undef,.001);
print "My auto-responder says: $out";

