2 #################################################################################
4 # File: $RCSfile: MAPS.pm,v $
5 # Revision: $Revision: 1.1 $
6 # Description: Main module for Mail Authentication and Permission System (MAPS)
7 # Author: Andrew@DeFaria.com
8 # Created: Fri Nov 29 14:17:21 2002
9 # Modified: $Date: 2013/06/12 14:05:47 $
12 # (c) Copyright 2000-2018, Andrew@DeFaria.com, all rights reserved.
14 ################################################################################
33 use base qw(Exporter);
40 my $userid = $ENV{MAPS_USERNAME} ? $ENV{MAPS_USERNAME} : $ENV{USER};
104 my $mapsbase = "$FindBin::Bin/..";
107 sub _cleanTables($$;$) {
108 my ($table, $timestamp, $dryrun) = @_;
110 my $condition = "userid = '$userid' and timestamp < '$timestamp'";
113 return $db->count($table, $condition);
115 my ($count, $msg) = $db->delete($table, $condition);
121 sub _retention2Days($) {
122 my ($retention) = @_;
124 # Of the retnetion periods I'm thinking of where they are <n> and then
125 # something like (days|weeks|months|years) none are tricky except for months
126 # because months, unlike (days|weeks|years) are ill-defined. Are there 28, 29
127 # 30 or 31 days in a month? Days are simple <n> days. Weeks are simple <n> * 7
128 # days. Years are simple - just change the year (a little oddity of 365 or
129 # 366) days this year? To keep things simple, we will ignore the oddities of
130 # leap years and just use 30 for number of days in month. We really don't need
131 # to be that accurate here...
133 # BTW we aren't checking for odd things like 34320 weeks or 5000 years...
134 if ($retention =~ /(\d+)\s+(day|days)/) {
136 } elsif ($retention =~ /(\d+)\s+(week|weeks)/){
138 } elsif ($retention =~ /(\d+)\s+(month|months)/) {
140 } elsif ($retention =~ /(\d+)\s+(year|years)/) {
150 my ($username, $password) = @_;
153 my $dbserver = $ENV{MAPS_SERVER} || 'localhost';
155 $db = MyDB->new($username, $password, $dbname, $dbserver);
157 croak "Unable to instantiate MyDB ($username\@$dbserver:$dbname)" unless $db;
163 my $MAPS_username = "maps";
164 my $MAPS_password = "spam";
166 OpenDB($MAPS_username, $MAPS_password);
169 sub Add2Blacklist(%) {
172 # Add2Blacklist will add an entry to the blacklist
173 # First SetContext to the userid whose black list we are adding to
174 SetContext($params{userid});
177 $params{sequence} = 0;
178 my ($err, $msg) = AddList(%params);
180 # Log that we black listed the sender
181 Info("Added $params{sender} to " . ucfirst $params{userid} . "'s black list");
184 my $count = DeleteEmail(
185 userid => $params{userid},
186 sender => $params{sender},
189 # Log out many emails we managed to remove
190 Info("Removed $count emails from $params{sender}");
195 sub Add2Nulllist(%) {
198 # First SetContext to the userid whose null list we are adding to
199 SetContext($params{userid});
202 $params{sequence} = 0;
203 my ($err, $msg) = AddList(%params);
205 # Log that we null listed the sender
206 Info("Added $params{sender} to " . ucfirst $params{userid }. "'s null list");
209 my $count = DeleteEmail(
210 userid => $params{userid},
211 sender => $params{sender},
214 # Log out many emails we managed to remove
215 Info("Removed $count emails from $params{sender}");
220 sub Add2Whitelist(%) {
223 # Add2Whitelist will add an entry to the whitelist
224 # First SetContext to the userid whose white list we are adding to
225 SetContext($params{userid});
228 $params{sequence} = 0;
230 my ($err, $msg) = AddList(%params);
232 return -$err, $msg if $err;
234 # Log that we registered a user
236 userid => $params{userid},
237 type => 'registered',
238 sender => $params{sender},
239 message => 'Registered new sender',
242 # Check to see if there are any old messages to deliver
243 ($err, $msg) = $db->find('email', "sender = '$params{sender}'", ['userid', 'sender', 'data']);
245 return ($err, $msg) if $err;
251 while (my $rec = $db->getnext) {
252 last unless $rec->{userid};
254 $status = Whitelist($rec->{sender}, $rec->{data});
261 # Return if we has a problem delivering email
262 return -1, 'Problem delivering some email' if $status;
264 # Remove delivered messages
266 userid => $params{userid},
267 sender => $params{sender},
270 return $messages, 'Messages delivered';
276 CheckParms(['userid', 'sender', 'subject', 'data'], \%rec);
278 $rec{timestamp} = UnixDatetime2SQLDatetime(scalar(localtime));
280 return $db->add('email', %rec);
286 CheckParms(['userid', 'type', 'sender', 'sequence'], \%rec);
288 croak "Type $rec{type} not valid. Must be one of white, black or null"
289 unless $rec{type} =~ /(white|black|null)/;
291 croak "Sender must contain \@" unless $rec{sender} =~ /\@/;
293 $rec{retention} //= '';
294 $rec{retention} = lc $rec{retention};
296 $rec{hit_count} //= $db->count(
298 "userid = '$rec{userid}' and sender like '%$rec{sender}%'"
301 ($rec{pattern}, $rec{domain}) = split /\@/, delete $rec{sender};
303 $rec{sequence} = GetNextSequenceNo(%rec);
305 $rec{last_hit} //= UnixDatetime2SQLDatetime(scalar (localtime));
307 return $db->add('list', %rec);
313 $params{timestamp} = UnixDatetime2SQLDatetime(scalar(localtime));
315 return $db->add('log', %params);
321 CheckParms(['userid', 'name', 'email', 'password'], \%rec);
323 return 1 if UserExists($rec{userid});
325 return $db->add('user', %rec);
328 sub AddUserOptions(%) {
331 croak('Userid is required') unless $rec{userid};
332 croak('No options to add') unless $rec{options};
334 return (1, "User doesn't exists") unless UserExist($rec{userid});
336 my %useropts = delete $rec{userid};
337 my %opts = delete $rec{options};
341 for my $key (%opts) {
342 $useropts{name} = $_;
343 $useropts{value} = $opts{$_};
345 ($err, $msg) = $db->add('useropts', %useropts);
350 return ($err, $msg) if $err;
355 # Blacklist will send a message back to the $sender telling them that
356 # they've been blacklisted. Currently we save a copy of the message.
357 # In the future we should just disregard the message.
360 # Check to see if this sender has already emailed us.
361 my $msg_count = $db->count('email', "userid='$rec{userid}' and sender like '%$rec{sender}%'");
363 if ($msg_count < 5) {
365 my @spammsg = split "\n", $rec{data};
368 userid => $rec{userid},
369 sender => $rec{sender},
370 subject => 'Your email has been discarded by MAPS',
371 msgfile => "$mapsbase/blacklist.html",
378 sender => $rec{sender},
379 message => 'Sent blacklist reply',
385 sender => $rec{sender},
386 message => 'Mail loop encountered',
390 $rec{hit_count}++ if $rec{sequence};
395 sequence => $rec{sequence},
396 hit_count => $rec{hit_count},
402 sub CheckEmail(;$$) {
403 my ($username, $domain) = @_;
405 return lc "$username\@$domain" if $username and $domain;
407 # Check to see if a full email address in either $username or $domain
409 if ($username =~ /(.*)\@(.*)/) {
412 return lc "$username\@";
415 if ($domain =~ /(.*)\@(.*)/) {
423 sub CheckOnList ($$;$) {
424 # CheckOnList will check to see if the $sender is on the list. Return 1 if
426 my ($listtype, $sender, $update) = @_;
431 my ($rule, $sequence);
434 my $condition = "userid='$userid' and type='$listtype'";
436 my ($err, $errmsg) = $db->find($table, $condition, '*', 'order by sequence');
438 my ($email_on_file, $rec);
440 while ($rec = $db->getnext) {
441 unless ($rec->{domain}) {
442 $email_on_file = $rec->{pattern};
444 unless ($rec->{pattern}) {
445 $email_on_file = '@' . $rec->{domain};
447 $email_on_file = $rec->{pattern} . '@' . $rec->{domain};
451 # Escape some special characters
452 $email_on_file =~ s/\@/\\@/;
453 $email_on_file =~ s/^\*/.\*/;
455 # We want to terminate the search string with a "$" iff there's an
456 # "@" in there. This is because some "email_on_file" may have no
457 # domain (e.g. "mailer-daemon" with no domain). In that case we
458 # don't want to terminate the search string with a "$" rather we
459 # wish to terminate it with an "@". But in the case of say
460 # "@ti.com" if we don't terminate the search string with "$" then
461 # "@ti.com" would also match "@tixcom.com"!
462 my $search_for = $email_on_file =~ /\@/
464 : !defined $rec->{domain}
467 if ($sender and $sender =~ /$search_for/i) {
468 my $comment = $rec->{comment} ? " - $rec->{comment}" : '';
470 $rule = "Matching rule: ($listtype:$rec->{sequence}) \"$email_on_file$comment\"";
471 $rule .= " - $rec->{comment}" if $rec->{comment};
474 $rec->{hit_count} //= 0;
479 sequence => $rec->{sequence},
480 hit_count => $rec->{hit_count} + 1,
487 return ($status, $rule, $rec->{sequence}, $rec->{hit_count});
490 sub CleanEmail($;$) {
491 my ($timestamp, $dryrun) = @_;
493 return _cleanTables 'email', $timestamp, $dryrun;
497 my ($timestamp, $dryrun) = @_;
499 return _cleanTables('log', $timestamp, $dryrun);
505 CheckParms(['userid', 'type'], \%params);
507 my $dryrunstr = $params{dryrun} ? '(dryrun)' : '';
510 my $condition = "userid='$params{userid}' and type='$params{type}'";
514 # First let's go through the list to see if we have an domain level entry
515 # (e.g. @spammer.com) and also individual entries (baddude@spammer.com) then
516 # we don't really need any of the individual entries since the domain block
518 $db->find($table, $condition, ['domain'], ' and pattern is null');
520 while (my $domains = $db->getnext) {
521 for my $recs ($db->get($table, $condition, ['sequence', 'pattern', 'domain'],
522 " and domain='$domains->{domain}' and pattern is not null")) {
523 if (@$recs and not $params{dryrun}) {
524 for my $rec (@$recs) {
526 userid => $params{userid},
527 type => $params{type},
528 sequence => $rec->{sequence},
531 $params{log}->msg("Deleted $params{userid}:$params{type}:$rec->{sequence} "
532 . "$rec->{pattern}\@$rec->{domain} $dryrunstr")
539 $params{log}->msg("The domain $domains->{domain} has the following subrecords");
541 for my $rec (@$recs) {
542 $params{log}->msg("$rec->{pattern}\@$rec->{domain}");
549 $condition = "userid='$params{userid}' and type='$params{type}' and retention is not null";
551 # First see if anything needs to be deleted
552 ($count, $msg) = $db->count($table, $condition);
554 return 0 unless $count;
558 my ($err, $errmsg) = $db->find($table, $condition);
560 croak "Unable to find $params{type} entries for $condition - $errmsg" if $err;
562 my $todaysDate = Today2SQLDatetime;
564 while (my $rec = $db->getnext) {
565 my $days = _retention2Days($rec->{retention});
567 my $agedDate = SubtractDays($todaysDate, $days);
569 # If last_hit < retentiondays then delete
570 if (Compare($rec->{last_hit}, $agedDate) == -1) {
571 unless ($params{dryrun}) {
573 userid => $params{userid},
574 type => $params{type},
575 sequence => $rec->{sequence},
579 $rec->{pattern} //= '';
580 $rec->{domain} //= '';
582 $params{log}->msg("Deleted $rec->{userid}:$params{type}:$rec->{sequence} "
583 . "$rec->{pattern}\@$rec->{domain} $dryrunstr");
584 $params{log}->dbug("last hit = $rec->{last_hit} < agedDate = $agedDate");
590 $params{log}->dbug("$rec->{userid}:$params{type}:$rec->{sequence}: nodelete $dryrunstr "
591 . "last hit = $rec->{last_hit} >= agedDate = $agedDate")
597 userid => $params{userid},
598 type => $params{type},
599 ) if $count && !$params{dryrun};
607 CheckParms(['userid'], \%params);
610 my $condition = "userid='$params{userid}'";
611 $condition .= " and $params{additional}" if $params{additional};
613 return $db->count($table, $condition);
619 CheckParms(['userid', 'type'], \%params);
622 my $condition = "userid='$params{userid}' and type='$params{type}'";
624 return $db->count($table, $condition);
630 CheckParms(['userid'], \%params);
632 my ($additional_condition) = delete $params{additional} || '';
634 my $condition = "userid='$userid'";
635 $condition .= " and $additional_condition" if $additional_condition;
637 return $db->count('log', $condition);
640 sub CountLogDistinct(%) {
643 CheckParms(['userid', 'column'], \%params);
645 my ($additional_condition) = delete $params{additional} || '';
647 my $condition = "userid='$userid'";
648 $condition .= " and $additional_condition" if $additional_condition;
650 return $db->count_distinct('log', $params{column}, $condition);
654 my ($password, $userid) = @_;
656 return $db->decode($password, $userid);
664 CheckParms(['userid', 'sender'], \%rec);
666 my ($username, $domain) = split /@/, $rec{sender};
670 $condition = "userid = '$rec{userid}' and sender = '$rec{sender}'";
672 $condition = "userid = '$rec{userid}' and sender like '%\@$domain'";
675 return $db->delete($table, $condition);
681 CheckParms(['userid', 'type', 'sequence'], \%rec);
683 my $condition = "userid = '$rec{userid}' and "
684 . "type = '$rec{type}' and "
685 . "sequence = $rec{sequence}";
687 return $db->delete('list', $condition);
691 my ($password, $userid) = @_;
693 return $db->encode($password, $userid);
699 CheckParms(['userid'], \%params);
702 my $condition = "userid='$params{userid}'";
703 $condition .= " and sender='$params{sender}'" if $params{sender};
704 $condition .= " and timestamp='$params{timestamp}'" if $params{timestamp};
706 return $db->find($table, $condition);
712 my ($type, $sender) = @_;
714 CheckParms(['userid', 'type'], \%params);
717 my $condition = "userid='$params{userid}' and type='$params{type}'";
719 if ($params{sender}) {
720 my ($username, $domain) = split /\@/, $params{sender};
722 # Split will return '' if either username or domain is missing. This messes
723 # up SQL's find as '' ~= NULL. Therefore we only specify username or domain
725 $condition .= " and pattern='$username'" if $username;
726 $condition .= " and domain='$domain'" if $domain;
729 return $db->find($table, $condition);
736 my $end_at = CountLog(
741 $start_at = $end_at - abs ($how_many);
742 $start_at = 0 if ($start_at < 0);
746 my $condition = "userid='$userid'";
747 my $additional = "order by timestamp limit $start_at, $end_at";
749 return $db->find($table, $condition, '*', $additional);
758 $condition = "userid='$userid'" if $params{userid};
760 return $db->find($table, $condition, $params{fields});
764 return $db->find('user', '', ['userid']);
783 sub GetNextSequenceNo(%) {
786 CheckParms(['userid', 'type'], \%rec);
789 my $condition = "userid='$rec{userid}' and type='$rec{type}'";
791 my $count = $db->count('list', $condition);
794 } # GetNextSequenceNo
803 return %{$db->getone('user', "userid='$userid'", ['name', 'email'])};
806 sub GetUserOptions($) {
809 my $table = 'useropts';
810 my $condition = "userid='$userid'";
812 $db->find($table, $condition);
816 while (my $rec = $db->getnext) {
817 $useropts{$rec->{name}} = $rec->{value};
824 my ($userid, $password) = @_;
826 $password = Encrypt($password, $userid);
828 # Check if user exists
829 my $dbpassword = UserExists($userid);
831 # Return -1 if user doesn't exist
832 return -1 unless $dbpassword;
834 # Return -2 if password does not match
835 if ($password eq $dbpassword) {
844 # Nulllist will simply discard the message.
845 my ($sender, $sequence, $hit_count) = @_;
850 sequence => $sequence,
851 hit_count => ++$hit_count,
859 message => 'Discarded message'
865 sub OnBlacklist($;$) {
866 my ($sender, $update) = @_;
868 return CheckOnList('black', $sender, $update);
871 sub OnNulllist($;$) {
872 my ($sender, $update) = @_;
874 return CheckOnList('null', $sender, $update);
877 sub OnWhitelist($;$$) {
878 my ($sender, $userid, $update) = @_;
880 SetContext($userid) if $userid;
882 return CheckOnList('white', $sender, $update);
886 my @tables = qw(email list log user useropts);
888 my ($err, $msg) = $db->lock('read', \@tables);
890 croak "Unable to lock table - $msg" if $err;
892 ($err, $msg) = $db->check(\@tables);
894 croak 'Unable to check tables ' . $msg if $err;
896 ($err, $msg) = $db->optimize(\@tables);
898 croak 'Unable to optimize tables ' . $msg if $err;
900 return $db->unlock();
904 # Reads an email message file from $input. Returns sender, subject,
905 # date and data, which is a copy of the entire message.
909 my $sender_long = '';
910 my $envelope_sender = '';
916 # Find first message's "From " line indicating start of message
922 # If we hit eof here then the message was garbled. Return indication of this
924 $data = "Garbled message - unable to find From line";
925 return $sender, $sender_long, $reply_to, $subject, $data;
929 $envelope_sender = $1;
930 $sender_long = $envelope_sender;
933 push @data, $_ if /^From /;
939 # Blank line indicates start of message body
940 last if ($_ eq "" || $_ eq "\r");
942 # Extract sender's address
948 if (/<(\S*)@(\S*)>/) {
949 $sender = lc ("$1\@$2");
950 } elsif (/(\S*)@(\S*)\ /) {
951 $sender = lc ("$1\@$2");
952 } elsif (/(\S*)@(\S*)/) {
953 $sender = lc ("$1\@$2");
955 } elsif (/^subject: .*/i) {
956 $subject = substr ($_, 9);
957 } elsif (/^reply-to: .*/i) {
958 $_ = substr ($_, 10);
959 if (/<(\S*)@(\S*)>/) {
960 $reply_to = lc ("$1\@$2");
961 } elsif (/(\S*)@(\S*)\ /) {
962 $reply_to = lc ("$1\@$2");
963 } elsif (/(\S*)@(\S*)/) {
964 $reply_to = lc ("$1\@$2");
977 # Set file pointer back by length of the line just read
978 seek ($input, -length () - 1, 1) if !eof $input;
980 # Sanitize email addresses
981 $envelope_sender =~ s/\<//g;
982 $envelope_sender =~ s/\>//g;
983 $envelope_sender =~ s/\"//g;
984 $envelope_sender =~ s/\'//g;
989 $reply_to =~ s/\<//g;
990 $reply_to =~ s/\>//g;
991 $reply_to =~ s/\"//g;
992 $reply_to =~ s/\'//g;
994 # Determine best addresses
995 $sender = $envelope_sender if $sender eq "";
996 $reply_to = $sender if $reply_to eq "";
998 return $sender, $sender_long, $reply_to, $subject, join "\n", @data;
1004 CheckParms(['userid', 'type', 'sequence', ], \%rec);
1006 my $current_date = UnixDatetime2SQLDatetime(scalar(localtime));
1009 my $condition = "userid='rec{userid} and type=$rec{type} and sequence='$rec{sequence}";
1013 condition => $condition,
1018 sub ResequenceList(%) {
1021 CheckParms(['userid', 'type'], \%params);
1024 return 1 unless $params{type} =~ /(white|black|null)/;
1025 return 2 unless UserExists($params{userid});
1028 my $condition = "userid='$params{userid}' and type ='$params{type}'";
1031 $db->lock('write', $table);
1033 # Get all records for $userid and $type
1034 my $listrecs = $db->get($table, $condition,'*', 'order by hit_count desc');
1036 # Delete all of the list entries for this $userid and $type
1037 my ($count, $msg) = $db->delete($table, $condition);
1039 # Now re-add list entries renumbering them
1043 $_->{sequence} = $sequence++;
1045 my ($err, $msg) = $db->add($table, %$_);
1058 CheckParms(['userid', 'type'], \%params);
1060 my $start_at = delete $params{start_at} || 0;
1061 my $lines = delete $params{lines} || 10;
1064 my $condition = "userid='$params{userid}' and type='$params{type}'";
1065 my $additional = "order by sequence limit $start_at, $lines";
1067 return $db->get($table, $condition, '*', $additional);
1073 # ReturnMsg will send back to the $sender the register message.
1074 # Messages are saved to be delivered when the $sender registers.
1076 # Added reply_to. Previously we passed reply_to into here as sender. This
1077 # caused a problem in that we were filtering as per sender but logging it
1078 # as reply_to. We only need reply_to for SendMsg so as to honor reply_to
1079 # so we now pass in both sender and reply_to
1081 CheckParms(['userid', 'sender', 'reply_to', 'subject', 'data'], \%params);
1083 #my ($sender, $reply_to, $subject, $data) = @_;
1085 # Check to see if this sender has already emailed us.
1086 my $msg_count = $db->count('email', "userid='$userid' and sender like '%$params{sender}%'");
1088 if ($msg_count < 5) {
1089 # Return register message
1091 userid => $params{userid},
1092 sender => $params{reply_to},
1093 subject => 'Your email has been returned by MAPS',
1094 msgfile => "$mapsbase/register.html",
1095 data => $params{data},
1096 ) if $msg_count == 0;
1099 userid => $params{userid},
1101 sender => $params{sender},
1102 message => 'Sent register reply',
1106 SaveMsg($params{sender}, $params{subject}, $params{data});
1108 Add2Nulllist($params{sender}, GetContext, "Auto Null List - Mail loop");
1111 userid => $params{userid},
1113 sender => $params{sender},
1114 message => 'Mail loop encountered',
1121 sub ReturnMessages(%) {
1124 CheckParms(['userid', 'sender'], \%params);
1126 my $table = 'email';
1127 my $condition = "userid='$params{userid}' and sender='$params{sender}'";
1128 my $fields = ['subject', 'timestamp'];
1129 my $additional = 'group by timestamp desc';
1131 return $db->get($table, $condition, $fields, $additional);
1134 sub ReturnSenders(%) {
1136 # This subroutine returns an array of senders in reverse chronological
1137 # order based on time timestamp from the log table of when we returned
1138 # their message. The complication here is that a single sender may
1139 # send multiple times in a single day. So if spammer@foo.com sends
1140 # spam @ 1 second after midnight and then again at 2 Pm there will be
1141 # at least two records in the log table saying that we returned his
1142 # email. Getting records sorted by timestamp desc will have
1143 # spammer@foo.com listed twice. But we want him listed only once, as
1144 # the first entry in the returned array. Plus we may be called
1145 # repeatedly with different $start_at's. Therefore we need to process
1146 # the whole list of returns for today, eliminate duplicate entries for
1147 # a single sender then slice the resulting array.
1148 CheckParms(['userid', 'type', 'lines'], \%params);
1151 my $condition = "userid='$params{userid}' and type='$params{type}'";
1152 my $additional = 'order by timestamp desc';
1154 $params{start_at} ||= 0;
1156 if ($params{date}) {
1157 $condition .= "and timestamp > '$params{date} 00:00:00' and "
1158 . "timestamp < '$params{date} 23:59:59'";
1161 $db->find($table, $condition, '*', $additional);
1163 # Watch the distinction between senders (plural) and sender (singular)
1166 # Run through the results and add to %senders by sender key. This
1167 # results in a hash that has the sender in it and the first
1168 # timestamp value. Since we already sorted timestamp desc by the
1169 # above select statement, and we've narrowed it down to only log
1170 # message that occurred for the given $date, we will have a hash
1171 # containing 1 sender and the latest timestamp for the day.
1172 while (my $rec = $db->getnext) {
1173 $senders{$rec->{sender}} = $rec->{timestamp}
1174 unless $senders{$rec->{sender}};
1177 # Make a hash whose keys are the timestamp (so we can later sort on
1179 my %sendersByTimestamp = reverse %senders;
1183 # Sort by timestamp desc and push on to the @senders array
1184 push @senders, $sendersByTimestamp{$_}
1185 for (sort { $b cmp $a } keys %sendersByTimestamp);
1187 # Finally slice for the given range
1188 my $end_at = $params{start_at} + $params{lines} - 1;
1190 $end_at = (@senders - 1) if $end_at >= @senders;
1192 return (@senders) [$params{start_at} .. $end_at];
1196 my ($sender, $subject, $data) = @_;
1201 subject => $subject,
1208 sub SearchEmails(%) {
1211 CheckParms(['userid', 'search'], \%params);
1213 my $table = 'email';
1214 my $fields = ['sender', 'subject', 'timestamp'];
1215 my $condition = "userid='$params{userid}' and (sender like '\%$params{search}\%' "
1216 . "or subject like '\%$params{search}\%')";
1217 my $additional = 'order by timestamp desc';
1219 my ($err, $msg) = $db->find($table, $condition, $fields, $additional);
1223 while (my $rec = $db->getnext) {
1231 # SendMsg will send the message contained in $msgfile.
1234 #my ($sender, $subject, $msgfile, $userid, @spammsg) = @_;
1238 # Open return message template file
1239 open my $return_msg_file, '<', $params{msgfile}
1240 or die "Unable to open return msg file ($params{msgfile}): $!\n";
1242 # Read return message template file and print it to $msg_body
1243 while (<$return_msg_file>) {
1246 s/\$userid/$userid/;
1250 s/\$sender/$params{sender}/;
1256 close $return_msg_file;
1258 # Create the message, and set up the mail headers:
1259 my $msg = MIME::Entity->build(
1260 From => "MAPS\@DeFaria.com",
1261 To => $params{sender},
1262 Subject => $params{subject},
1263 Type => "text/html",
1267 # Need to obtain the spam message here...
1268 my @spammsg = split "\n", $params{data};
1272 Disposition => "attachment",
1277 open my $mail, '|-', '/usr/lib/sendmail -t -oi -oem'
1278 or croak "SendMsg: Unable to open pipe to sendmail $!";
1280 $msg->print(\*$mail);
1290 if (UserExists($to_user)) {
1293 return GetUserOptions $userid;
1302 my $total_space = 0;
1303 my $table = 'email';
1304 my $condition = "userid='$userid'";
1306 $db->find($table, $condition);
1308 while (my $rec = $db->getnext) {
1310 length($rec->{userid}) +
1311 length($rec->{sender}) +
1312 length($rec->{subject}) +
1313 length($rec->{timestamp}) +
1314 length($rec->{data});
1317 return $total_space;
1323 CheckParms(['userid', 'type', 'sequence'], \%rec);
1326 my $condition = "userid = '$rec{userid}' and type = '$rec{type}' and sequence = $rec{sequence}";
1328 if ($rec{pattern} =~ /\@/ && !$rec{domain}) {
1329 ($rec{pattern}, $rec{domain}) = split /\@/, $rec{pattern};
1330 } elsif (!$rec{pattern} && $rec{domain} =~ /\@/) {
1331 ($rec{pattern}, $rec{domain}) = split /\@/, $rec{domain};
1332 } elsif (!$rec{pattern} && !$rec{domain}) {
1333 return "Must specify either Username or Domain";
1336 $rec{pattern} //= 'null';
1337 $rec{domain} //= 'null';
1338 $rec{comment} //= 'null';
1340 if ($rec{retention}) {
1341 $rec{retention} = lc $rec{retention};
1344 return $db->update($table, $condition, %rec);
1350 CheckParms(['userid', 'name', 'email'], \%rec);
1352 return 1 unless UserExists($rec{userid});
1355 my $condition = "userid='$rec{userid}'";
1357 return $db->update($table, $condition, %rec);
1360 sub UpdateUserOptions ($@) {
1361 my ($userid, %options) = @_;
1363 return unless UserExists($userid);
1365 my $table = 'useropts';
1366 my $condition = "userid='$userid' and name=";
1368 $db->update($table, "$condition'$_'", (name=>$_, value=>$options{$_})) for (keys %options);
1371 } # UpdateUserOptions
1376 return 0 unless $userid;
1379 my $condition = "userid='$userid'";
1381 my $rec = $db->get($table, $condition);
1383 return 0 if scalar(@$rec) == 0;
1385 return $rec->[0]{password};
1388 sub Whitelist ($$;$$) {
1389 # Whitelist will deliver the message.
1390 my ($sender, $data, $sequence, $hit_count) = @_;
1392 my $userid = GetContext;
1394 # Dump message into a file
1395 open my $message, '>', "/tmp/MAPSMessage.$$"
1396 or Error("Unable to open message file (/tmp/MAPSMessage.$$): $!\n"), return -1;
1398 print $message $data;
1402 # Now call MAPSDeliver
1403 my $status = system "$FindBin::Bin/MAPSDeliver $userid /tmp/MAPSMessage.$$";
1405 unlink "/tmp/MAPSMessage.$$";
1410 type => 'whitelist',
1412 message => 'Delivered message',
1415 Error("Unable to deliver message - is MAPSDeliver setgid? - $!");
1418 $hit_count++ if $sequence;
1423 sequence => $sequence,
1424 hit_count => $hit_count,