#!/usr/local/bin/perl

# This is a rather simple-minded program that, if added to the
# alias for a list:
#    list: "|..resend..","|/usr/misc/majordomo/.bin/ wrapper mj_log2 list"
# will save the From: line of the message sender to the Log.list
# file in the Majordomo list directory. Not particualrly 
# effective, but it is useful for tracking some messages and 
# identifying unused lists.
# 
# The script is based on the mj_log script that comes with majorcool package
# (look on the majorcool contrib directory)
#
# The output will be something like:
#
#   DATE PROCESS LIST FROM-ADDRESS NUMBER-OF-LINES
#
# For example:
# 
#    Jul 06 17:36:18 mj_log2 latinet Ricardo Marek <ricky@ornet.co.il> 89 lines
# 
#
# The command line options are:
# 
#    mj_log2 [-C config_file] [-m] [-l log_file] list
# 
#       -C config_file ......... The Majordomo config file.
#       -m ..................... Add an abbreviated month extension to
#                                the log file.
#       -l log_file ............ An optional log_file instead of ussing
#                                the $listdir/Log.$list
#       list ................... The list related to this message
#                                (mandatory) 
# 
# The mj_log2 is called in the same fashion than mj_log from majorcool 
# (from a alias using the wrapper)
# 
# For proper operation, all files must be copied to Majordomo's 
# home directory and be made executable (mode 550).
#
#========================================================================


# Before doing anything else tell the world I am majordomo
# The mj_ prefix is reserved for tools that are part of majordomo proper.

$main'program_name = 'mj_log2';              #'


require "getopts.pl";

die("Usage: $0 [-C config_file] [-m] [-l log_file] list")
	unless &Getopts("C:ml:");


die("No list specified") unless @ARGV;

$my_list = shift;

#
# parse for config file or default list

$cf = $ENV{"MAJORDOMO_CF"} || "/etc/majordomo.cf"; 
$cf = $opt_C if $opt_C;

# Read and execute the .cf file
die("$cf not readable; stopped") unless -r $cf;
die("require of majordomo.cf failed $@") unless require $cf;

# Go to the home directory specified by the .cf file
chdir("$homedir");

# All these should be in the standard PERL library
unshift(@INC, $homedir);
require "ctime.pl";                # To get MoY definitions for month abbrevs
require "majordomo_version.pl";    # What version of Majordomo is this?
require "majordomo.pl";            # all sorts of general-purpose Majordomo subs
require "shlock.pl";               # NNTP-style file locking
require "config_parse.pl";         # functions to parse the config files


# Here's where the fun begins...

# check to see if the cf file is valid
die("listdir not defined. Is majordomo.cf being included correctly?")
    if !defined($listdir);

#
# here comes the magic touch...
#

if (defined($logdir)) {
    $log_file = "$logdir/Log.$my_list";
} else {
    $log_file = "$listdir/Log.$my_list";
}

$log_file = $opt_l if $opt_l;

#
# Now get the message from STDIN
#

$in_header = 1;
while(<STDIN>) {
    if (($in_header) && (/^From:/)) {
        chop;
        $from_line = $_;
        next;
    } 

    if (/^\s*$/) {
        $in_header = 0;
    }

    $body_lines++ if ($in_header == 0);
}


#
# Now print the logging information
#

($message_from) = (split(/\s+/, $from_line, 2))[1];

#
# localtime:
# 
($sec,$min,$hour,$mday,$mon,) = (localtime)[0 .. 4];
$month_abv = $ctime'MoY[$mon];                  #'

#
# Add month extension if needed
#
$log_file .= ".$month_abv" if ($opt_m);

if (&main'lopen(LOG, ">>", $log_file)) {        #'
    
    #
    # printf LOG "%s %02d %02d:%02d:%02d %s list=%s from=<%s> size=%d lines\n",
    #            $month_abv, $mday, $hour, $min, $sec, $program_name, $list,  
    #            $message_from, $body_lines;
    
    printf LOG "%s %02d %02d:%02d:%02d %s %s %s %d lines\n",
               $month_abv, $mday, $hour, $min, $sec, $program_name, $my_list,  
               $message_from, $body_lines;

    &main'lclose(LOG);                          #'
} 

exit(0);

##############################################################################
#
#                                  END of FILE
#
#                       No not change any thing below this line
#
##############################################################################
# 
# RCS LOG Information
# -------------------
# $Log: mj_log2,v $
# Revision 1.3  1997/07/07 14:30:26  ricky
# Fixing typo mistakes
# Changing the catch of the From line and header lines
#
# Revision 1.2  1997/07/07 06:36:49  ricky
# Adding the use of $logdir if defined.
# I added to majordomo.cf file the definition of a $logdir,
# that points to "$homedir/log". (So all logging information
# goes to a single directory)
#
#
# Revision 1.1  1997/07/06 14:41:27  ricky
# Initial revision
#
#=============================================================================
# Do NOT remove these lines. They set mode in Emacs.
# perl-mode is OK for font locking.
#
#@ Local Variables: ***
#@ mode: perl ***
#@ End: ***



syntax highlighted by Code2HTML, v. 0.9.1