#!/usr/bin/perl BEGIN { use lib '/usr/home/cfg' ; require push_inc ; } use CGI qw( :standard ); use CGI::Carp qw(fatalsToBrowser); use Fcntl qw(:flock); use MIME::Parser; use MIME::Lite; use Mail::Sendmail; use Text::Unidecode; require cfg ; print header; # CGI.pm method #--------------------------------------------------------------------------------------------------------------------------------------------------------------------- &today; #--------------------------------------------------------------------------------------------------------------------------------------------------------------------- our ($q) = CGI -> new() ; my $uniqueid = $q -> param('uniqueid') ; my $subject = $q -> param('subject') ; my $from = $q -> param('from') ; my $iaction = $q -> param('iaction') ; my $iemail = $q -> param('iemail') ; my $imsg = $q -> param('imsg') ; my $suid = $q -> param('suid') ; my $responded = $q -> param('responded') ; $subject =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg; # our $debug = 1 ; # &common_debug ("subject = [$subject]"); unless ($uniqueid) { print "No ticket id..." ; exit ; } # our $ticket_dir = "$mailpath/tickets/$uniqueid" ; #-------- page opts -------------------------------------------------------------------------------------------------------------------------------------------------- &page_opts ; #--------------------------------------------------------------------------------------------------------------------------------------------------------------------- if ($iaction eq 'send'){ &send_email ; &screen3 ; } else { &process_ticket_data; &screen2; } exit; #------------------------------------------------------------------------------------------ sub process_ticket_data { my $table = 'tickets' ; &db_min_ro($table,'*',"unique_id='$uniqueid' OR suid='$suid' OR unique_id='$suid'",'','') ; foreach my $tid (sort {$b <=> $a} keys %{$db{$table}}) { my $uid = $db{$table}{$tid}{unique_id} ; my $tdir = "$mailpath/tickets/$uid" ; &read_ticket_dir($tdir) ; &create_message($tdir) ; &create_attachment($uid) ; } } #------------------------------------------------------------------------------------------ sub read_ticket_dir { my ($ticket_dir) = @_ ; our $found_msg = 'n' ; our $msgfile = '' ; our @attachment_array = () ; &common_debug ("read_ticket_dir = [$ticket_dir]"); opendir(DIR, $ticket_dir) or die "cant open directory : $ticket_dir : $!\n"; while(defined($folder = readdir(DIR))) { if (length $folder < 3) { next ; } &common_debug ("folder = [$folder]"); if (substr($folder,0,4) eq 'msg-' && substr($folder,-4,4) eq '.txt') { $msgfile = $folder ; $found_msg = 'y' ; &common_debug ("msgfile = [$msgfile]"); } else { push @attachment_array, $folder ; } } closedir(DIR) ; # IF NO TXT FILE FOUND, REPEAT, LOOKING FOR HTML MSG if ($found_msg eq 'n') { opendir(DIR, $ticket_dir) or die "cant open directory : $ticket_dir : $!\n"; while(defined($folder = readdir(DIR))) { if (length $folder < 3) { next ; } if (substr($folder,0,4) eq 'msg-' && substr($folder,-5,5) eq '.html') { $msgfile = $folder ; $found_msg = 'y' ; } } closedir(DIR) ; } } #------------------------------------------------------------------------------------------ sub create_message { my ($ticket_dir) = @_ ; my $print_message = '' ; if ($found_msg eq 'y') { $messagefile = "$ticket_dir/$msgfile" ; &common_debug ("messagefile = [$messagefile]"); use open qw(:std :utf8); open (M1FILE, "$messagefile") or die "can't open $messagefile: $!" ; flock (M1FILE, LOCK_SH) or die "can't lock $messagefile: $!" ; while () { $print_message .= $_ ; } close (M1FILE); $print_message =~ s/\n/
/g; # These next two put in to bodge LTS emails coming in $print_message =~ s/ / /g; $print_message =~ s/font-size: 12pt;/font-size: 12px;/g; } else { $print_message = "No message"; } $print_box_content_rows .= qq~

$print_message

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------~ if $print_message ; } #------------------------------------------------------------------------------------------ sub create_attachment { my ($unique_id) = @_ ; my $print_attachments = '' ; my $acnt = 0 ; foreach $attachment (@attachment_array) { # $print_attachments .= qq~$attachment | ~; $acnt++; $print_attachments .= qq~ | ~ if $acnt > 1 ; $print_attachments .= qq~$attachment~; } $print_box_content_rows .= $print_attachments ; } #------------------------------------------------------------------------------------------ sub page_opts { our $glyphicon = 'envelope' ; } #------------------------------------------------------------------------------- sub send_email { # # my $bcc = 'accounts@kre8it.co.za;rory@mathew.za.net' ; # $msg = MIME::Lite -> new( # # From => "ITV Support ", # # From => "ITV Support ", # From => "ITV Support ", # To => $iemail, # Bcc => $bcc, # Subject => "$subject", # Type => 'multipart/mixed' # ); # $msg -> attach( # Type => 'TEXT', # Data => $imsg); # # &check_attachments ; # # &add_attachments ; # $msg -> send ('smtp','localhost', Debug=>0 ); # &common_send_mail($iemail,'','','html','',"$imsg","$subject") ; &common_send_mail($iemail,'','','','',"$imsg","$subject") ; $i{responded} = 1 ; $i{suid} = $suid if $suid ; &db_min_upd('tickets',"unique_id='$uniqueid'") ; } #------------------------------------------------------------------------------- sub screen2 { &common_min_alert_type ; &common_min_dialog ; # &common_debug ("screen2 [suid=$suid]"); # my $resubject = "$subject #$uniqueid" ; my $resubject = ($subject =~ /Re:/iog) ? '' : 'Re: ' ; my $resubject .= "$subject ##$suid##" ; # unless ($suid) { unless ($responded) { $reply_section = qq~
 
 
 
~ ; } print < $alert $reply_section

Emails Correspondance

$print_box_content_rows
$dialog{'common'}{'js'} $print_footer_jscript $print_footer_forms ENDOFTEXT # } #------------------------------------------------------------------------------------------ sub screen3 { print < $useropts{title} ENDOFTEXT # } #------------------------------------------------------------------------------------------ use db ; use common ; use today ; 1;