#!/usr/bin/perl
# cPanel cPsupport waitnotice - Idle ticket messenger. 
# (C) 2004 cPanel Inc.
#
# $Id: waitnotice 133 2005-01-26 21:19:47Z nirosys $

BEGIN { require("../config.cgi"); }

use cpSupport;
use cpSupport::User;
use cpSupport::Config;
use cpSupport::TicketType;
use cpSupport::Transaction;
use HTML::Template;

my $user = getpwuid($>);

my $homedir = (getpwnam($user))[7];

if (!cpSupport::loadConfig($domain)) { exit; }
my $dbh = cpSupport::connectToDb();
if (!defined($dbh)) {exit;};
my $config = new cpSupport::Config($dbh);
my $domain = $config->get("domain");
print "Sending emails for $domain\n";
send48HourNotices($dbh);
send96HourNotices($dbh);


# send48HourNotices ################################################## {{{
sub send48HourNotices {
   my ($dbh) = @_;
   my $conf = cpSupport::Config->new($dbh);
   my $tempselect = $dbh->prepare("SELECT * FROM ${cpSupport::DB_PREFIX}_emails WHERE name = 'notice48hours'");
   $tempselect->execute;
   my $temprow = $tempselect->fetchrow_hashref();
   my $content = $$temprow{content};
   my $template = HTML::Template->new( scalarref => \$content, die_on_bad_params=>0 );

   my $select = $dbh->prepare("SELECT * FROM ${cpSupport::DB_PREFIX}_tickets WHERE UNIX_TIMESTAMP(NOW())-UNIX_TIMESTAMP(lastresponse) > ? AND ".
                              "UNIX_TIMESTAMP(NOW())-UNIX_TIMESTAMP(lastresponse) < ? AND status = 'open' AND ".
                              "needres = 'n'");
   $select->execute(60 * 60 * 24 * 2, 60 * 60 * 24 * 4);
   while (my $rowRef = $select->fetchrow_hashref()) {
      my $type = new cpSupport::TicketType;
      $type->load_id_from_dbh($$rowRef{type},$dbh);
      my $user = new cpSupport::User;
      $user->load_id_from_dbh($$rowRef{user},$dbh);
      $template->param( ticketid => $$rowRef{ticketid} );
      my $tran = new cpSupport::Transaction(
                        ticketId => $$rowRef{ticketid},
                        type     => "sent",
                        user     => 1,
                        body     => $template->output,
                     );
      $tran->insert($dbh);

      my $update = $dbh->prepare("UPDATE ${cpSupport::DB_PREFIX}_tickets SET status='waiting' WHERE ticketid=?");
      $update->execute($$rowRef{ticketid});
      
      open(SENDMAIL,"|/usr/sbin/sendmail -t");
      print SENDMAIL "To: ".$user->email."\n";
      print SENDMAIL "From: ".$type->type."@".$conf->get("domain")."\n";
      print SENDMAIL "Subject: We haven't heard from you in 48 hours!\n";
      print SENDMAIL $template->output;
      close(SENDMAIL);
   }
   $select->finish;
}
# }}}

# send96HourNotices ############################################### {{{
sub send96HourNotices {
   my ($dbh) = @_;
   my $conf = cpSupport::Config->new($dbh);
   my $tempselect = $dbh->prepare("SELECT * FROM ${cpSupport::DB_PREFIX}_emails WHERE name = 'notice96hours'");
   $tempselect->execute;
   my $temprow = $tempselect->fetchrow_hashref();
   $content = $$temprow{content};
   my $template = HTML::Template->new( scalarref => \$content, die_on_bad_params => 0 );

   my $select = $dbh->prepare("SELECT * FROM ${cpSupport::DB_PREFIX}_tickets WHERE UNIX_TIMESTAMP(NOW())-UNIX_TIMESTAMP(lastresponse) > ? ".
                              "AND status = 'waiting' AND needres = 'n'");
   $select->execute(60 * 60 * 24 * 4);

   while (my $rowRef = $select->fetchrow_hashref()) {
      my $type = new cpSupport::TicketType;
      $type->load_id_from_dbh($$rowRef{type},$dbh);
      my $user = new cpSupport::User;
      $user->load_id_from_dbh($$rowRef{user},$dbh);
      $template->param( ticketid => $$rowRef{ticketid} );
      my $tran = new cpSupport::Transaction(
            ticketId => $$rowRef{ticketid},
            type     => "close",
            user     => 1,
            );
      $tran->insert($dbh);
      my $update = $dbh->prepare("UPDATE ${cpSupport::DB_PREFIX}_tickets SET close=NOW() , status='closed' WHERE ticketid=?");
      $update->execute($$rowRef{ticketid});

      open(SENDMAIL,"|/usr/sbin/sendmail -t");
      print SENDMAIL "To: ".$user->email."\n";
      print SENDMAIL "From: ".$type->type."@".$conf->get("domain")."\n";
      print SENDMAIL "Subject: We haven't heard from you in 96 hours!\n";
      print SENDMAIL $template->output;
      close(SENDMAIL);
   }
}
# }}}
