#!/usr/bin/perl
#
# $Header: /var/cvs/batts/bin/ticket-via-email,v 1.13 2001/11/30 20:50:06 lasser Exp $
#
# Take an incoming e-mail on STDIN and add it to the ticketing system.
#
# BATTS and its ticket-via-email interface are copyright
# (C) 2001 Jon Lasser <jon@cluestickconsulting.com>
#
#   This program is free software; you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published
#   by the Free Software Foundation; either version 2 of the License,
#   or (at your option) any later version.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with this program; if not, write to the Free Software
#   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
#   02111-1307 USA
#
# $Log: ticket-via-email,v $
# Revision 1.13  2001/11/30 20:50:06  lasser
# Fix bugs on creation of new user and ticket simultaneously.
#
# Revision 1.12  2001/11/29 23:15:13  lasser
# Documentation continues apace; GPL included and relevant info included
# at top of BATTS programs
#
# Revision 1.11  2001/11/28 20:12:20  lasser
# Fix to use sendmail directly, add config option for sendmail to /etc/battsrc
#
# Revision 1.10  2001/11/28 18:26:51  lasser
# Make it work mostly, even in /etc/smrsh --- still problems with calling sendmail
# and thus full and pretty From: header support.
#
# Revision 1.9  2001/11/28 16:53:17  lasser
# Proper headers on the return mail, changes to configuration file stuff
#
# Revision 1.8  2001/11/28 14:34:36  lasser
# reset ticket status on incoming e-mail
#
# Revision 1.7  2001/11/28 14:18:32  lasser
# requisite improvements to the ticket-via-email interface.
#
# Revision 1.6  2001/11/20 16:04:47  lasser
# capitalization and spacing bugfixes in user creation and via email
#
# Revision 1.5  2001/11/20 15:46:59  lasser
# fix capitalization issues
#
# Revision 1.4  2001/11/19 20:46:50  lasser
# more ticket layout change fixes
#
# Revision 1.3  2001/11/19 19:08:37  lasser
# Cleanup of ticket-via-email, no more unnecessary resets of people
#
# Revision 1.2  2001/11/19 18:04:53  lasser
# prettier HTML output; start of changes to ticket-via-email
#
# Revision 1.1  2001/11/12 19:26:16  lasser
# ticket-via-email added, unsuccessful attempt made to make ticket-new
# return the correct stuff.
#

use strict;
use Env;
use FindBin;
use IPC::Open3;
use Mail::Internet;
use Mail::Header;

my ($message);			# Object for the incoming message
my ($reply);			# Object for a reply message
my (@replytext);		# text of the message reply.
my ($header, $replyheader);	# Object for the message header
my ($subject);			# the subject
my ($from);			# the from line
my ($ticketnum);		# An existing ticket number
my ($return_address);		# Our return address
my ($sendmail);			# How to call sendmail
my ($error_input) = "";		# Reading STDERR easily

# For breaking apart the From line
my ($from_username);
my ($from_fullname) = "New Person";
my ($from_address);

# For discovery of e-mail addresses
my (@people);			# List of all people
my ($person);			# the current person
my (@current);			# the current e-mail address
my ($reset)=1;			# reset the name by default

# Configuration file settings
my ($default_config) = "/etc/battsrc";	# Our configuration file

# Figure out our return address via our config files
{ package Settings; do "$default_config" }
if (defined $ENV{HOME}) {
	{ package Settings; do "$ENV{HOME}/.battsrc" }
}

if (defined $Settings::sendmail) {
	$sendmail = $Settings::sendmail;
}

if (defined $Settings::return_address) {
	$return_address = $Settings::return_address;
}

# Grab the message via STDIN
$message = new Mail::Internet \*STDIN;

# Grab the header from the message
$header = $message->head;

# grab the subject and from lines.
$subject=$header->get("Subject");
$from=$header->get("From");
chomp ($subject);
chomp ($from);

# Break apart the from line if the address is separate
if ($from !~ /<.*>/) {
	$from_address = $from;
	$from_username = $from;
	$from_username  =~ s/@.*$//;
} else {
	$from_address = $from;
	$from_address =~ s/^.*<// ;
	$from_address =~ s/>.*$// ;

	$from_username = $from_address;
	$from_username  =~ s/@.*$//;

	$from_fullname = $from;
	$from_fullname =~ s/<.*$// ;
}

$from_address = lc($from_address);
$from_username = lc($from_username);
$from_fullname =~ tr/"'//;
chomp $from_address;
chomp $from_fullname;
chomp $from_username;

# See if the person is in the database already
@people=`$FindBin::Bin/batts show-people`;

foreach (@people) {
	@current=split / : /;
	chomp $current[1];
	if ($current[1] eq $from_address) {
		$reset=0;
		last;
	}
}

# Check if we've got a ticket number in the subject line already
if ($subject =~ /\[[0-9]{1,6}\]/) {
	$ticketnum = $&;
	$ticketnum =~ s/[^0-9]//g;

# If so, and if the person does not already exist in the system, add
# them

if ($reset) {
	system("$FindBin::Bin/batts", "person-new", $from_username,
		$from_fullname, $from_address, "unknown");
	# A standard "or die" here didn't work, for some reason.
	if ($? != 0) {
		die("Can't add $from_username to user list.\n");
	}
}

# next, add the message to the ticket log as that person.
	open (THISTICKET,
 "| $FindBin::Bin/batts log $ticketnum $current[0] informational Y")
	    or die("Can't open ticket $ticketnum for logging.\n");
	$message->print_body(\*THISTICKET);
	close (THISTICKET);

# Reset the ticket status to assigned
	system("$FindBin::Bin/batts", "status-ticket",
		$ticketnum, "assigned");

# Finally, exit
	exit 0;
}
	

# If the ticket didn't already exit, feed it into batts as a new ticket
# and grab the ticket number from STDERR
open3(\*NEWTICKET, \*STDOUT, \*TNSTDERR,
	"$FindBin::Bin/batts ticket-new \"$from_address\" \"$subject\""
	) or die("Can't open ticket for writing.\n");
$message->print_body(\*NEWTICKET);
close(NEWTICKET);
while ($error_input = <TNSTDERR>) {
	if ($error_input =~ /[0-9]{6}$/) {
		$ticketnum = $error_input;
	}
}

# Send an auto-reply to the original message

# first, grab the text of our auto-reply
open(REPLYMESSAGE, "/etc/batts-reply.txt")
	or die("Can't open /etc/batts-reply.txt\n");
@replytext=<REPLYMESSAGE>;
close(REPLYMESSAGE);
push @replytext, "\n";

# modify the ticket number
$ticketnum =~ /[0-9]{6}$/;
$ticketnum = $&;
chomp $ticketnum;

$reply=$message->reply;
$reply->body(@replytext);
$replyheader = $reply->head;
$subject = "Re: [$ticketnum] $subject";
$replyheader->replace("Subject", $subject);

if (defined $return_address) {
 	$replyheader->replace("From", $return_address);
}

# Mail::Internet's send('sendmail') bit doesn't work, so we must...
# $reply->send();
open (SENDMAIL, "| $sendmail") or die ("Can't open sendmail!\n");
$reply->print(\*SENDMAIL);
close(SENDMAIL) or die ("I can't close sendmail!\n");


# No point checking the return value, we're on our way out anyway.
if ($reset) {
	system("$FindBin::Bin/batts", "person-username",$from_address,
		$from_username);
		
	system("$FindBin::Bin/batts", "person-fullname",$from_username,
		$from_fullname);
	}

# OK, we're done. For now.
