#!/usr/bin/perl
# Super simple little mbox to gopher script
# runs as a filter for gophernicus 
#
# save to your filter/ directory as mbox 
# and make sure those mbox files you want to serve up ends with .mbox 
#
#	13/10 added slightly better formation of emails, and simple
#	mail thread support 
# add the following to the gophernicus.conf 
# to make mbox files be handled as dirctories for the filter 
# ext=mbox:1
#
# Jacob Dahl Pind/2010
#
use Mail::Box::Manager;
use Mail::Box::Thread::Manager;
use Mail::Address;
#use double;
use strict;

my $mbox = $ARGV[0];
my $mgr = Mail::Box::Manager->new;
my $folder = $mgr->open(folder => $mbox, access => 'r', lock_type => 'none' );
my $lcount = 0;
my $req = $ENV{REQUEST};
my $mail = $ENV{SEARCHREQUEST};
my $server = $ENV{SERVER_HOST};
my $port = $ENV{SERVER_PORT};
my @followup_list;
chop $req;

sub dumpcgi{
    foreach my $var (sort(keys(%ENV))) {
        my $val = $ENV{$var};
	$val =~ s|\n|\\n|g;
	$val =~ s|"|\\"|g;
	print "${var}=\"${val}\"\n";
    }
}

sub mail2gopher{
    my $msg = $folder->message($_[0]);
    my $address_from = $msg->sender;

    print "iFrom: ".$address_from->format,"\r\n";
    print "iDate: ".localtime($msg->timestamp)."\r\n";
    print "iSubject: ".$msg->subject."\r\n";
    print "i\r\n";

    foreach (split(/\n/,$msg->body)){
        print "i".$_."\r\n";
    }
}

sub mail2thread{
    my $msg= $folder->message($mail);
    my $threads = $mgr->threads($folder);
    my $thread = $threads->threadStart($msg);

# loop though all mails and find the thread start

    foreach $msg ($folder->messages)
    {
	if ($msg->messageId eq $thread->messageId){
		if ( $lcount eq $mail){
		} else {
		    print "iThread start\r\n";
		    print "1".$msg->subject."\t".$req."?".$msg->seqnr."\t$server\t$port\r\n";
		}
	    }
        $lcount++;
    }

    foreach ($thread->followUps){

# loop through all mails and find those where In-Reply-To matchs the current messageId
	foreach $msg ($folder->messages)
	{
	if ($msg->messageId eq $_->message->messageId){
		    print "1Followup: ".$msg->subject."\t".$req."?".$msg->seqnr."\t$server\t$port\r\n";
	    }
        $lcount++;
	}
    }

}

sub mail2followup{
    my $threads_ = $mgr->threads($folder);
    my $thread = $threads_->threadStart($_->message);

    foreach ($thread->followUps){
    $lcount=0;
# loop through all mails and find those where In-Reply-To matchs the current messageId
	foreach my $msg ($folder->messages)
	{
	if ($msg->messageId eq $_->message->messageId){
		    print "1| ".$msg->subject."\t".$req."?".$lcount."\t$server\t$port\r\n";
	    }
        $lcount++;
	}
    }
}

sub mail_followups{
	    foreach ($_->followUps){
		undef my %is_follow;
		for(@followup_list){ $is_follow{$_} = 1}
		if ($is_follow{$_->message->seqnr}) {
		} else {
		    push(@followup_list,$_->message->seqnr);
		    print "1| ".$_->message->subject."\t".$req."?".$_->message->seqnr."\t$server\t$port\r\n";
		}
		&mail_followups($_->message);
	    }
	

}
# main loop

# lookup a single email from the box
if ($mail ne ""){
    &mail2gopher($mail);
    &mail2thread($mail);
} else {

# looper gennem alle mails med mbox, koer thread paa alle mails, og gem startmail seqnr 
# looper gennem arrayed af alle starts mails.

# list all emails in the box
    my $threads = $mgr->threads(folder =>$folder, window=>100);
    foreach ($folder->messages)
    {
	my $msg = $threads->threadStart($_);
# dummy is missing thread start
	if ($msg->message->isDummy){
	    print "1".$_->subject."\t".$req."?".$_->seqnr."\t$server\t$port\r\n";
#	    print "i".$msg->message->subject." : ".$msg->message->seqnr."\r\n";
#	    &mail_followups($msg);
	} else {
	    if ($_->seqnr eq $msg->message->seqnr){
		print "1".$_->subject."\t".$req."?".$_->seqnr."\t$server\t$port\r\n";
	    } else {
#	    print "1".$msg->message->subject." : ".$msg->message->seqnr."\r\n";
	    print "1".$_->subject."\t".$req."?".$_->seqnr."\t$server\t$port\r\n";
	    }
	}
    }
    foreach (@followup_list){
    	print "1".$_->message->subject."\t".$req."?".$_->message->seqnr."\t$server\t$port\r\n";

	&mail_followups($_);
    }
    exit 0;
    print "istarthere\r\n";
    my $threads = $mgr->threads(folder =>$folder, window=>100);
    foreach ($threads->sortedAll){
	if ($_->message->isDummy){
	    &mail_followups($_);
	} else {
		print "1".$_->message->subject."\t".$req."?".$_->message->seqnr."\t$server\t$port\r\n";
		push(@followup_list,$_->message->seqnr);
		&mail_followups($_);
#		&mail2followup($_);
	}
#	print "i".$_->message->messageId."\r\n";
	}
}