#!/usr/bin/perl

require "config.cgi";
&configure;

$mode = "register";

print "Content-type:text/html\n\n";

$ip    = "0.0.0.0";
$numip = 0;

if ( $multi eq "0" ) {

    open( FILE, "$emailpath1" );
    @LINES = <FILE>;
    close(FILE);

    foreach $line (@LINES) {

        #find the from line
        if ( $line =~ /^From:\s/i ) {
            $fromline = $line;

            #####extract the subscribed user name and email account
            $fromline =~ /<(.+\@.+)>$/;
            $email = $1;
            if ( $email eq "" ) {
                $fromline =~ /\s(.+\@.+)$/;
                $email = $1;
            }

        }
        elsif ( $line =~ /^To:\s/i ) {

            $toline = $line;

            $toline =~ /<(.+\@.+)>$/;
            $toemail = $1;
            if ( $toemail eq "" ) {
                $toline =~ /\s(.+\@.+)$/;
                $toemail = $1;
            }

            ( $ar, $domain ) = split( /@/, $toemail );
            $arflag = 0;

            @malias = split( /,/, $mailalias );

            foreach $alias (@malias) {
                if ( $ar eq $alias ) {
                    $arflag = 1;
                    last;
                }
            }
            if ( $arflag == 0 ) {

                $ar = "";
            }
        }
        elsif ( $line =~ /^Subject:\s/i ) {

            $subjectline = $line;

            ( $dummy, $user ) = split( /:\s/, $subjectline );
            $user =~ s/^\s+//g;
            $user =~ s/\s+$//g;

            if ( $user eq "" ) {
                $user = $emptyuname;
            }

        }
        elsif ( $line =~ /(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})/i
            and $numip < 2 )
        {

            $ip = "$1.$2.$3.$4";
            $numip++;
        }

        if ( $email ne "" and $user ne "" and $ar ne "" ) {

            $query =
                "mode=" . $mode . "&ar=" . $ar . "&user=" . $user
              . "&email="
              . $email
              . "&B1=Subscribe&ip="
              . $ip;

            use LWP::UserAgent;
            $ua = new LWP::UserAgent;
            $req = new HTTP::Request 'POST', "$scriptpath";
            $req->content_type('application/x-www-form-urlencoded');
            $req->content($query);
            $res = $ua->request($req);

            $email = "";
            $user  = "";
            $ar    = "";
            $ip    = "0.0.0.0";
            $numip = 0;
        }

    }

    open( FILE, ">$emailpath1" );
    print FILE '';
    close(FILE);

}
else {

    opendir THEDIR, "$emailpath2" or &oops("fail to open the directory.");
    my @allfiles;
    @allfiles = readdir THEDIR;
    my $file;
    my %pairs;

    foreach $file (@allfiles) {
        open( FILE, "$emailpath2/$file" );
        @LINES = <FILE>;
        close(FILE);

        foreach $line (@LINES) {

            #find the from line
            if ( $line =~ /^From:\s/i ) {
                $fromline = $line;

                $fromline =~ /<(.+\@.+)>$/;
                $email = $1;
                if ( $email eq "" ) {
                    $fromline =~ /\s(.+\@.+)$/;
                    $email = $1;
                }

            }
            elsif ( $line =~ /^To:\s/i ) {

                $toline = $line;

                $toline =~ /<(.+\@.+)>$/;
                $toemail = $1;
                if ( $toemail eq "" ) {
                    $toline =~ /\s(.+\@.+)$/;
                    $toemail = $1;
                }

                ( $ar, $domain ) = split( /@/, $toemail );
                $arflag = 0;

                @malias = split( /,/, $mailalias );

                foreach $alias (@malias) {
                    if ( $ar eq $alias ) {
                        $arflag = 1;
                        last;
                    }
                }

                if ( $arflag == 0 ) {
                    $ar = "";
                }
            }
            elsif ( $line =~ /^Subject:\s/i ) {
                $subjectline = $line;

                ( $dummy, $user ) = split( /:\s/, $subjectline );
                $user =~ s/^\s+//g;
                $user =~ s/\s+$//g;

                if ( $user eq "" ) {
                    $user = $emptyuname;
                }

            }
            elsif ( $line =~ /(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})/i
                and $numip < 2 )
            {

                $ip = "$1.$2.$3.$4";
                $numip++;
            }

            if ( $email ne "" and $user ne "" and $ar ne "" ) {

                $query =
                    "mode=" . $mode . "&ar=" . $ar . "&user=" . $user
                  . "&email="
                  . $email
                  . "&B1=Subscribe&ip="
                  . $ip;

                use LWP::UserAgent;
                $ua = new LWP::UserAgent;
                $req = new HTTP::Request 'POST', "$scriptpath";
                $req->content_type('application/x-www-form-urlencoded');
                $req->content($query);
                $res = $ua->request($req);

                # send_immediate_email($email,$user,$ar);

                last;
            }
        }

        `rm $emailpath2/$file`

    }

    closedir THEDIR;

}

my ($time);
if ( open( STATUS, "status.cfg" ) ) {

    #flock(STATUS,$LOCK_EX);

    #print "optin";
    $time = <STATUS>;
    chomp($time);
    my ( $mday, $mon, $year ) = ltime();

    if ( $time ne "$mday$mon$year" ) {
        $activate = 1;
        require "mailer.cgi";

    }
    close(STATUS);
}

sub oops {
    print "$_[0]";
    exit;
    die "Error: $_[0]\n";
}

sub ltime {
    my ( $sec, $min, $hour, $mday, $mon, $year ) = localtime();
    $year += 1900;
    $mon++;
    if ( $mday < 10 ) { $mday = "0$mday" }
    if ( $mon < 10 )  { $mon  = "0$mon" }
    return ( $mday, $mon, $year );
}

sub getarlist {
    my (@list);
    my ($file);
    if ( $_[0] eq "" ) { $file = 'responders.cfg' }
    else { $file = $_[0] }
    if ( open( RISP, "$file" ) ) {
        flock( RISP, $LOCK_EX );
        foreach (<RISP>) {
            chomp($_);
            push( @list, $_ );
        }
        flock( RISP, $LOCK_UN );
        close(RISP);
    }
    else {
    }
    return @list;
}
