#!/usr/bin/perl

################################################################################
#
# Programm    :  list mbox content
#
# Copyright (c) 2001-2021  The Eisfair Team, team(at)eisfair(dot)org
#
# Created     :  2002-12-01  jed
# Last updated:  $Id$
#
# Parameter   :  [mbox name in /var/spool/mail]
#
################################################################################

$fromaddr = "";                                            # intialize variables
$dateaddr = "";                                            #
$deldateaddr = "";                                         #
$subjaddr = "";                                            #

$debug    = 0;                                             # debug output

$header   = 1;
$idx      = 1;

### Check if one command line parameter can be found
unless (1 == scalar(@ARGV))
{
    die "Script requires one argument\n  list_mbox_content.pl [mbox name]\n";
}

# check if first character is / or
if (substr($ARGV[0],0,1) =~ /[\/\.]/)
  {
    # argument contains path
    $mboxname = $ARGV[0];                                  # get first argument
  }
else
  {
    # argument contains no path add default path
    $mboxname = "/var/spool/mail/" . $ARGV[0];             # get first argument
  }

# open mbox file for read access
open(MBOX, "< $mboxname") or die "cannot open $mboxname!\n";

while (<MBOX>)
  {
    if ($header)
      {
        if (/^[\s]*$/)
          {
            # empty line -> end of header
            $header = 0;

            # print previous message line
            if ($fromaddr && $subjaddr && $dateaddr)
              {
                if ($subjaddr !~ /DON'T DELETE THIS MESSAGE -- FOLDER IN/) # print not if internal data
                  {
                    # print output
                    $output = sprintf("%3d - %-35s  %-26s  %-50s", $idx, $fromaddr, $dateaddr, $subjaddr);
                    print "$output\n";

                    $idx = ++$idx;                         # increment counter
                  }

                $fromaddr = "";                            # reset variables
                $subjaddr = "";                            #
                $dateaddr = "";                            #
              }
          }
        else
          {
            # header
            if (/^[Ff][Rr][Oo][Mm]:/)
              {
                # from line found
                if (/<|>/)
                  {
                    ($fromaddr) = ($_ =~ /<(.*)>/);        # get email address
                  }
                else
                  {
                    ($fromaddr) = ($_ =~ /[: ](.*)/);      # get email address
                  }

                $fromaddr =~ s/^\s+//;                     # remove leading spaces
                $fromaddr =~ s/^\s+$//;                    # remove trailing spaces

                $fromaddr = $fromaddr ? $fromaddr : "* no sender given *";
                                                           # take $fromaddr if not empty,
                                                           # otherwise set to defined string

                $fromaddr = substr($fromaddr, 0, 35);      # cut length of string

                if ($debug)                                # debug output
                  {                                        #
                    print "FROM:$fromaddr:\n";             #
                  }                                        #
              }

            if (/^[Ss][Uu][Bb][Jj][Ee][Cc][Tt]:/)
              {
                # subject line found
                ($subjaddr) = ($_ =~ /[: ](.*)/);          # get subject line

                $subjaddr =~ s/^\s+//;                     # remove leading spaces
                $subjaddr =~ s/^\s+$//;                    # remove trailing spaces

                $subjaddr = $subjaddr ? $subjaddr : "* no subject given *";
                                                           # take $subjaddr if not empty,
                                                           # otherwise set to defined string

                $subjaddr = substr($subjaddr, 0, 50);      # cut length of string

                if ($debug)                                # debug output
                  {                                        #
                    print "SUBJ:$subjaddr:\n";             #
                  }                                        #
              }

            if (/^[Dd][Ee][Ll][Ii][Vv][Ee][Rr][Yy]-[Dd][Aa][Tt][Ee]:/)
              {
                if (/\,/)
                  {
                 # ($deldateaddr) = ($_ =~ /\,(.*)[+-]/);  # get date
                   ($deldateaddr) = ($_ =~ /\,(.*)/);      # get date
                  }
                else
                  {
                 # ($deldateaddr) = ($_ =~ /:(.*)[+-]/);   # get date
                   ($deldateaddr) = ($_ =~ /:(.*)/);       # get date
                  }

                $deldateaddr =~ s/^\s+//;                  # remove leading spaces
                $deldateaddr =~ s/^\s+$//;                 # remove trailing spaces

                $deldateaddr = substr($deldateaddr . "                          ", 0, 26);

                if ($debug)                                # debug output
                  {                                        #
                    print "DATE:$deldateaddr:\n";          #
                  }                                        #
              }

            if (/^[Dd][Aa][Tt][Ee]:/)
              {
                if (/\,/)
                  {
                 # ($dateaddr) = ($_ =~ /\,(.*)[+-]/);     # get date
                   ($dateaddr) = ($_ =~ /\,(.*)/);         # get date
                  }
                else
                  {
                 # ($dateaddr) = ($_ =~ /:(.*)[+-]/);      # get date
                   ($dateaddr) = ($_ =~ /:(.*)/);          # get date
                  }

                $dateaddr =~ s/^\s+//;                     # remove leading spaces
                $dateaddr =~ s/^\s+$//;                    # remove trailing spaces

                $dateaddr = substr($dateaddr . "                          ", 0, 26);

                if ($debug)                                # debug output
                  {                                        #
                    print "DATE:$dateaddr:\n";             #
                  }                                        #
              }

            if ($deldateaddr)
              {
                $dateaddr = $deldateaddr;
              }

            if (/^[Xx]-[Ii][Nn][Ff][Ee][Cc][Tt][Ee][Dd]:/)
              {
                # virus line found
                ($virname) = ($_ =~ /[: ](.*)/);           # get virus line

                $virname =~ s/^\s+//;                      # remove leading spaces
                $virname =~ s/^\s+$//;                     # remove trailing spaces

                $subjaddr = substr("[$virname] $subjaddr", 0, 50); # cut length of string

                if ($debug)                                # debug output
                  {                                        #
                    print "VIRUS:$virname:\n";             #
                  }                                        #
              }
          }

      }
    else    # $header
      {
        $_ =~ s/^[Ff][Rr][Oo][Mm]/>From/;                  # new header begins
        $header = 1;
      }

  }    # while

close(MBOX);                                               # close input file
