Welcome to MAPS 2.0
[clearscm.git] / maps / bin / maps
index 1564e2a..52e234f 100755 (executable)
@@ -38,18 +38,17 @@ $Date: 2013/06/12 14:05:47 $
 
 =head1 SYNOPSIS
 
  Usage maps: [-u|ser <username>] [-ve|rbose] [-deb|ug] [-e|xecute]
 
  Where:
    -u|ser <username>: Set context to this username
+
    -v|erbose:         Be verbose
    -de|bug:           Output debug messages
-   
+
    -[no]e|xecute:     Set execute mode.         
 
-# (c) Copyright 2000-2006, Andrew@DeFaria.com, all rights reserved.
+# (c) Copyright 2000-2021, Andrew@DeFaria.com, all rights reserved.
 
 =cut 
 
@@ -57,11 +56,12 @@ use strict;
 use warnings;
 
 use Getopt::Long;
+use Email::Valid;
 use FindBin;
 use File::Temp qw (tempfile);
 use Net::Domain qw (hostdomain);
 
-use lib $FindBin::Bin, '/opt/clearscm/lib';
+use lib "$FindBin::Bin/../lib", "$FindBin::Bin/../../lib";
 
 use MAPS;
 use MAPSLog;
@@ -69,18 +69,18 @@ use MAPSLog;
 use Display;
 use Utils;
 
-my $verbose    = 0;
-my $execute    = 1;
-my $userid     = $ENV{USER};
+my $verbose   = 0;
+my $execute   = 1;
+my $userid    = $ENV{USER};
 
-my $logpath    = "$FindBin::Bin/../log";
-my $logfile    = "$logpath/debug.log";
+my $logpath   = "$FindBin::Bin/../log";
+my $logfile   = "$logpath/debug.log";
 my $forwardto = $ENV{MAPS_FORWARDTO} || 'adefaria@gmail.com';
 
 # For some reason I'm not parsing messages correctly but it only seems to
 # happen when the message is piped in from the MTA. This routine will
 # temporarily save the messages in a file.
-sub SaveStdin () {
+sub SaveStdin() {
   # Generate tempfile
   my $msgfile = tempfile ();
 
@@ -96,22 +96,7 @@ sub SaveStdin () {
   return $msgfile;
 } # SaveStdin
 
-sub save_msg {
-  my ($sender, $sender_long, $reply_to, $subject, $data) = @_;
-
-  open SAVED_MSG, '>>', "$logpath/$sender"
-    or die "Unable to open $logpath/$sender - $!\n";
-
-  print SAVED_MSG "Sender = $sender\n";
-  print SAVED_MSG "Sender long = $sender\n";
-  print SAVED_MSG "reply_to = $reply_to\n";
-  print SAVED_MSG "subject  = $subject\n";
-  print SAVED_MSG "data:\n\n";
-  print SAVED_MSG $data;
-  print SAVED_MSG "*** END OF DATA***\n";
-} # save_msg
-
-sub ValidDomainUser ($) {
+sub ValidDomainUser($) {
   my ($sender) = @_;
 
   my ($username, $domainname);
@@ -136,8 +121,7 @@ sub ValidDomainUser ($) {
 sub ProcessMsgs ($$$) {
   my ($msgfile, $username, $user_email) = @_;
 
-  return
-    unless $execute;
+  return unless $execute;
 
   while (!eof *$msgfile) {
     my ($sender, $sender_long, $reply_to, $subject, $data) = ReadMsg (*$msgfile);
@@ -152,9 +136,9 @@ sub ProcessMsgs ($$$) {
       #close $mail
         #or die "Unable to forward email to $forwardto - $!";      
     #} # if
-    
+
     my ($onlist, $rule, $sequence, $hit_count);
-    
+
     # Algorithm change: We now first check to see if the sender is not found
     # in the message and skip it if so. Then we handle if we are the sender
     # and that the from address is formatted properly. Spammers often use 
@@ -172,12 +156,9 @@ sub ProcessMsgs ($$$) {
     #
     # Finally, we handle return processing
 
-    # Special sender handling: 
-    if ($sender !~ /.+\@.+/) {
-      verbose "Sender not found in message or invalid";
-      next;
-    } # if
-    
+    # Discard any email with an invalid email address
+    next unless Email::Valid->address($sender);
+
     if ($sender eq $user_email and
             (lc ($sender_long) !~ lc ("\"$username\" <$user_email>") and
              lc ($sender_long) !~ lc ("$username <$user_email>"))) {
@@ -185,7 +166,7 @@ sub ProcessMsgs ($$$) {
       Nulllist $sender;
       next;
     } # if
-    
+
     # Check whitelist:
     ($onlist, $rule, $sequence, $hit_count) = OnWhitelist $sender;
 
@@ -197,18 +178,24 @@ sub ProcessMsgs ($$$) {
         verbose "Sender from this domain but user not found";
         Nulllist $sender;
       } # if
-      
+
       next;
     } # if
-    
+
     # Check blacklist:
     ($onlist, $rule, $sequence, $hit_count) = OnBlacklist $sender;
 
     if ($onlist) {
       verbose "Blacklisting $sender";
-      my @msg = split /\n/, $data;
 
-      Blacklist $sender, $sequence, $hit_count, @msg;
+      Blacklist(
+        userid    => $userid,
+        sender    => $sender,
+        sequence  => $sequence,
+        hit_count => $hit_count,
+        data      => $data,
+      );
+
       next;
     } # if 
 
@@ -223,12 +210,18 @@ sub ProcessMsgs ($$$) {
 
     # Return processing:
     verbose "Returning message from $sender";
-    ReturnMsg $sender, $reply_to, $subject, $data;
+    ReturnMsg(
+      userid   => $userid,
+      sender   => $sender,
+      reply_to => $reply_to,
+      subject  => $subject,
+      data     => $data,
+    );
   } # while
 } # ProcessMsgs
 
 # Main
-GetOptions (
+GetOptions(
   'user=s'      => \$userid,
   'verbose'     => sub { set_verbose },
   'debug'       => sub { set_debug },
@@ -239,9 +232,9 @@ GetOptions (
 my $msgfile;
 
 if ($ARGV[0] and $ARGV[0] ne "") {
-  open $msgfile, $ARGV[0];
+  open $msgfile, '<', $ARGV[0];
 
-  if (!$msgfile) {
+  unless ($msgfile) {
     Error "Unable to open file ($ARGV[0]): $!\n";
     exit 1;
   } # if
@@ -251,9 +244,9 @@ if ($ARGV[0] and $ARGV[0] ne "") {
 
 verbose "Starting MAPS....";
 
-my ($username, $user_email) = SetContext $userid
+my %userOptions = SetContext $userid
   or die "$userid is not a registered MAPS user\n";
 
-ProcessMsgs $msgfile, $username, $user_email;
+ProcessMsgs $msgfile, $userOptions{name}, $userOptions{email};
 
 exit 0;