#!/usr/bin/perl -w
# vim: set sw=4 ts=4 si et:
# Written by: Guido Socher, Copyright: GPL
#
no locale;
use MIME::Words;
use vars qw($opt_a $opt_h);
use Getopt::Std;
sub help();
sub hashstr($);
getopts("a:h")||die "ERROR: No such option. -h for help.\n";
help() if ($opt_h);
help() unless ($opt_a);
open(CKSUMS,">>$opt_a")||die "ERROR can not append to $opt_a\n";
my ($subject_orig,$subject,$sum,$subject_s);
while(<>){
    if (/^Subject: (...+)/){
        $subject_orig=$1;
        $subject=MIME::Words::decode_mimewords($subject_orig);
        $subject_s=substr($subject,0,75);
        $sum=hashstr(substr($subject_orig,0,35));
        print CKSUMS "$sum :: $subject_s\n";
    }
}
close CKSUMS;

# hash a fingerprint:
sub hashstr($){
    my $s=shift;
    my $num=1;
    my $i=0;
    my $o;
    while (length($s)){
        $i++;
        $o=ord(chop($s));
        $num = (($i + $o)*($i + $o)+$num) % 9328889;
    }
    return(sprintf("%07d",$num));
}

sub help(){
    print "spamygetcksums -- generate the checksums of subject lines from mail

USAGE: spamygetcksums -a file_to_append_checksums [e-mail-message ...]

One or more e-mail messages can be read from the command line or from
stdin.
";
    exit 0;
}
