#!/usr/local/bin/perl

use CGI qw/:standard/;
use AvantGo;

##### Configuration #############################################
my $AVG = new AvantGo("/home/slash/avantgo.user");
my $FEEDBACK_EMAIL = "momo_102@bluemail.ch";
my $FEEDBACK_SUBJECT = "PDA-Feedback";
my $FEEDBACK_SENDMAIL = "/usr/sbin/sendmail";
my $FEEDBACK_EOL = "\n";

#################################################################
sub main {
   $AVG->env_load();    
   $AVG->user_load(); 
   pda_header("PDA Feedback");
   pda_feedback_nav();
   print "<HR>\n";
   if (lc(CGI::param("cmd")) ne "send") {
      pda_feedback_edit();
   } else {
      pda_feedback_send();
   }
   pda_footer();
}

#################################################################
sub pda_header {
   my $title = shift;
   print CGI::header();
   print <<EOT if $title;
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<HTML><HEAD><TITLE>$title</TITLE></HEAD><BODY>
EOT
}

#################################################################
sub pda_footer {
   print end_html(),"\n";
}

#################################################################
sub pda_feedback_nav {
   print "<A HREF=\"index.pl\">Home</A>\n";
}

#################################################################
sub pda_feedback_send {
    my $self = shift;

    my %Mail;

    $Mail{'Subject'} = $FEEDBACK_SUBJECT;
    $Mail{'To'} = $FEEDBACK_EMAIL;
    if (CGI::param("email")) {
       $Mail{'From'} = CGI::param("email");
    } else {
       my ($name,$passwd,$uid,$gid,$quota,$comment,$gcos,$dir,$shell,$expire) = getpwnam(getlogin());
       if ($comment ne "") {
          $Mail{'From'} = $name;
       } else {
          $Mail{'From'} = "$comment <$name>";
       }
    }
    $Mail{'X-Mailer'} = "PDA-Perl/1.0";

    $Mail{'Date'} = localtime;
    $Mail{'MIME-Version'} = "1.0";
    $Mail{'Content-Type'} = "text/plain; charset=iso-8859-1";
    my $Text = CGI::param("text");
    if (!open(FD, "|".$FEEDBACK_SENDMAIL." ".$Mail{'To'})) {
	print "<H1 ALIGN=\"\">Error while sending mail.</H1>\n";
        return;
    }
    foreach (keys %Mail) {
        if (length($Mail{$_}) > 0) {
            print FD $_.": ".$Mail{$_}.$FEEDBACK_EOL;
        }
    }
    print FD $FEEDBACK_EOL;
    print FD $Text.$FEEDBACK_EOL;
    print FD $FEEDBACK_EOL;
    foreach ($AVG->env_keys()) {
        print FD $_." = ".$AVG->env_get($_).$FEEDBACK_EOL;
    }

    print FD ".".$FEEDBACK_EOL;
    if (close(FD)) {
        print "<CENTER>Feedback gesendet.</CENTER>\n";
    }
}

#################################################################
sub pda_feedback_edit {
   print "<FORM METHOD=\"POST\" ACTION=\"feedback.pl\">\n";
   print "<INPUT TYPE=\"hidden\" NAME=\"cmd\" VALUE=\"send\">\n";
   print "EMail: <INPUT TYPE=\"text\" NAME=\"email\" SIZE=\"18\"><BR>\n";
   print "Text:  <TEXTAREA NAME=\"text\" ROWS=\"4\" COLS=\"20\"></TEXTAREA><P>\n";
   print "<INPUT TYPE=\"submit\" VALUE=\"Send\">";
   print "</FORM>\n";
}

#################################################################
main();
#################################################################
