#!/usr/bin/perl -w
use strict;
use FindBin;
use lib $FindBin::Bin;
#use lib '/data/httpd/lib/perl';
use ot_const qw( %r &is &res &is_err );
use ot_maillib qw( &debug );
use ot_receive qw( &receive );

=head1 NAME

ot_mail.pl - general mail interface of open theory

=head1 SYNOPSIS

Receives, analyzes and executes mail commands.

=head1 DESCRIPTION

Commands are received by mail. They are analyzed, stored, validated and
executed. The result is send to the user by mail.

=cut

my $res;
my $msg_id;
my $from;

debug( "##### Start ot_mail.pl: ".localtime, 0 );

( $res, $msg_id, $from ) = receive();
debug( 'receive-result: '.res( $res ), 1 );

if( $res == $r{ok} ) { exit; }	#### should never happen, too unspecific
if( $res == $r{ok_nothing_done} ) { exit; }	# normal

if( !is_err( $res )){
	if( $res == $r{ok_ab_mode} ) {			# address-based mode
		require ot_parse_a;
		ot_parse_a->import( qw( &parse ));
	} elsif( $res == $r{ok_bb_mode} ) {		# body-based mode
		require ot_parse_b;
		ot_parse_b->import( qw( &parse ));
	}
}

if( !is_err( $res )){
	$res = parse( $msg_id );
	debug( 'parse-result: '.res( $res ), 1 );
}

if( !is_err( $res )){
	require ot_validate;
	ot_validate->import( qw( &validate ));
	$res = validate( $msg_id );
	debug( 'validate-result: '.res( $res ), 1 );
}

require ot_execute;
ot_execute->import( qw( &execute ));
$res = execute( $msg_id );
debug( 'execute-result: '.res( $res ), 1 );

debug( "#### End ####\n", 0 );
