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;
161 my $MAPS_username = "maps";
162 my $MAPS_password = "spam";
164 OpenDB($MAPS_username, $MAPS_password);
167 sub Add2Blacklist(%) {
170 # Add2Blacklist will add an entry to the blacklist
171 # First SetContext to the userid whose black list we are adding to
172 SetContext($params{userid});
175 $params{sequence} = 0;
176 my ($err, $msg) = AddList(%params);
178 # Log that we black listed the sender
179 Info("Added $params{sender} to " . ucfirst $params{userid} . "'s black list");
182 my $count = DeleteEmail(
183 userid => $params{userid},
184 sender => $params{sender},
187 # Log out many emails we managed to remove
188 Info("Removed $count emails from $params{sender}");
193 sub Add2Nulllist(%) {
196 # First SetContext to the userid whose null list we are adding to
197 SetContext($params{userid});
200 $params{sequence} = 0;
201 my ($err, $msg) = AddList(%params);
203 # Log that we null listed the sender
204 Info("Added $params{sender} to " . ucfirst $params{userid }. "'s null list");
207 my $count = DeleteEmail(
208 userid => $params{userid},
209 sender => $params{sender},
212 # Log out many emails we managed to remove
213 Info("Removed $count emails from $params{sender}");
218 sub Add2Whitelist(%) {
221 # Add2Whitelist will add an entry to the whitelist
222 # First SetContext to the userid whose white list we are adding to
223 SetContext($params{userid});
226 $params{sequence} = 0;
228 my ($err, $msg) = AddList(%params);
230 return -$err, $msg if $err;
232 # Log that we registered a user
234 userid => $params{userid},
235 type => 'registered',
236 sender => $params{sender},
237 message => 'Registered new sender',
240 # Check to see if there are any old messages to deliver
241 ($err, $msg) = $db->find('email', "sender = '$params{sender}'", ['userid', 'sender', 'data']);
243 return ($err, $msg) if $err;
249 while (my $rec = $db->getnext) {
250 last unless $rec->{userid};
252 $status = Whitelist($rec->{sender}, $rec->{data});
259 # Return if we has a problem delivering email
260 return -1, 'Problem delivering some email' if $status;
262 # Remove delivered messages
264 userid => $params{userid},
265 sender => $params{sender},
268 return $messages, 'Messages delivered';
274 CheckParms(['userid', 'sender', 'subject', 'data'], \%rec);
276 $rec{timestamp} = UnixDatetime2SQLDatetime(scalar(localtime));
278 return $db->add('email', %rec);
284 CheckParms(['userid', 'type', 'sender', 'sequence'], \%rec);
286 croak "Type $rec{type} not valid. Must be one of white, black or null"
287 unless $rec{type} =~ /(white|black|null)/;
289 croak "Sender must contain \@" unless $rec{sender} =~ /\@/;
291 $rec{retention} //= '';
292 $rec{retention} = lc $rec{retention};
294 $rec{hit_count} //= $db->count(
296 "userid = '$rec{userid}' and sender like '%$rec{sender}%'"
299 ($rec{pattern}, $rec{domain}) = split /\@/, delete $rec{sender};
301 $rec{sequence} = GetNextSequenceNo(%rec);
303 $rec{last_hit} //= UnixDatetime2SQLDatetime(scalar (localtime));
305 return $db->add('list', %rec);
311 $params{timestamp} = UnixDatetime2SQLDatetime(scalar(localtime));
313 return $db->add('log', %params);
319 CheckParms(['userid', 'name', 'email', 'password'], \%rec);
321 return 1 if UserExists($rec{userid});
323 return $db->add('user', %rec);
326 sub AddUserOptions(%) {
329 croak('Userid is required') unless $rec{userid};
330 croak('No options to add') unless $rec{options};
332 return (1, "User doesn't exists") unless UserExist($rec{userid});
334 my %useropts = delete $rec{userid};
335 my %opts = delete $rec{options};
339 for my $key (%opts) {
340 $useropts{name} = $_;
341 $useropts{value} = $opts{$_};
343 ($err, $msg) = $db->add('useropts', %useropts);
348 return ($err, $msg) if $err;
352 # Blacklist will send a message back to the $sender telling them that
353 # they've been blacklisted. Currently we save a copy of the message.
354 # In the future we should just disregard the message.
357 # Check to see if this sender has already emailed us.
358 my $msg_count = $db->count('email', "userid='$rec{userid}' and sender like '%$rec{sender}%'");
360 if ($msg_count < 5) {
362 my @spammsg = split "\n", $rec{data};
365 userid => $rec{userid},
366 sender => $rec{sender},
367 subject => 'Your email has been discarded by MAPS',
368 msgfile => "$mapsbase/blacklist.html",
375 sender => $rec{sender},
376 message => 'Sent blacklist reply',
382 sender => $rec{sender},
383 message => 'Mail loop encountered',
387 $rec{hit_count}++ if $rec{sequence};
392 sequence => $rec{sequence},
393 hit_count => $rec{hit_count},
399 sub CheckEmail(;$$) {
400 my ($username, $domain) = @_;
402 return lc "$username\@$domain" if $username and $domain;
404 # Check to see if a full email address in either $username or $domain
406 if ($username =~ /(.*)\@(.*)/) {
409 return lc "$username\@";
412 if ($domain =~ /(.*)\@(.*)/) {
420 sub CheckOnList ($$;$) {
421 # CheckOnList will check to see if the $sender is on the list. Return 1 if
423 my ($listtype, $sender, $update) = @_;
428 my ($rule, $sequence);
431 my $condition = "userid='$userid' and type='$listtype'";
433 my ($err, $errmsg) = $db->find($table, $condition, '*', 'order by sequence');
435 my ($email_on_file, $rec);
437 while ($rec = $db->getnext) {
438 unless ($rec->{domain}) {
439 $email_on_file = $rec->{pattern};
441 unless ($rec->{pattern}) {
442 $email_on_file = '@' . $rec->{domain};
444 $email_on_file = $rec->{pattern} . '@' . $rec->{domain};
448 # Escape some special characters
449 $email_on_file =~ s/\@/\\@/;
450 $email_on_file =~ s/^\*/.\*/;
452 # We want to terminate the search string with a "$" iff there's an
453 # "@" in there. This is because some "email_on_file" may have no
454 # domain (e.g. "mailer-daemon" with no domain). In that case we
455 # don't want to terminate the search string with a "$" rather we
456 # wish to terminate it with an "@". But in the case of say
457 # "@ti.com" if we don't terminate the search string with "$" then
458 # "@ti.com" would also match "@tixcom.com"!
459 my $search_for = $email_on_file =~ /\@/
461 : !defined $rec->{domain}
464 if ($sender and $sender =~ /$search_for/i) {
465 my $comment = $rec->{comment} ? " - $rec->{comment}" : '';
467 $rule = "Matching rule: ($listtype:$rec->{sequence}) \"$email_on_file$comment\"";
468 $rule .= " - $rec->{comment}" if $rec->{comment};
471 $rec->{hit_count} //= 0;
476 sequence => $rec->{sequence},
477 hit_count => $rec->{hit_count} + 1,
484 return ($status, $rule, $rec->{sequence}, $rec->{hit_count});
487 sub CleanEmail($;$) {
488 my ($timestamp, $dryrun) = @_;
490 return _cleanTables 'email', $timestamp, $dryrun;
494 my ($timestamp, $dryrun) = @_;
496 return _cleanTables('log', $timestamp, $dryrun);
502 CheckParms(['userid', 'type'], \%params);
504 my $dryrunstr = $params{dryrun} ? '(dryrun)' : '';
507 my $condition = "userid='$params{userid}' and type='$params{type}'";
511 # First let's go through the list to see if we have an domain level entry
512 # (e.g. @spammer.com) and also individual entries (baddude@spammer.com) then
513 # we don't really need any of the individual entries since the domain block
515 $db->find($table, $condition, ['domain'], ' and pattern is null');
517 while (my $domains = $db->getnext) {
518 for my $recs ($db->get($table, $condition, ['sequence', 'pattern', 'domain'],
519 " and domain='$domains->{domain}' and pattern is not null")) {
520 if (@$recs and not $params{dryrun}) {
521 for my $rec (@$recs) {
523 userid => $params{userid},
524 type => $params{type},
525 sequence => $rec->{sequence},
528 $params{log}->msg("Deleted $params{userid}:$params{type}:$rec->{sequence} "
529 . "$rec->{pattern}\@$rec->{domain} $dryrunstr")
536 $params{log}->msg("The domain $domains->{domain} has the following subrecords");
538 for my $rec (@$recs) {
539 $params{log}->msg("$rec->{pattern}\@$rec->{domain}");
546 $condition = "userid='$params{userid}' and type='$params{type}' and retention is not null";
548 # First see if anything needs to be deleted
549 ($count, $msg) = $db->count($table, $condition);
551 return 0 unless $count;
555 my ($err, $errmsg) = $db->find($table, $condition);
557 croak "Unable to find $params{type} entries for $condition - $errmsg" if $err;
559 my $todaysDate = Today2SQLDatetime;
561 while (my $rec = $db->getnext) {
562 my $days = _retention2Days($rec->{retention});
564 my $agedDate = SubtractDays($todaysDate, $days);
566 # If last_hit < retentiondays then delete
567 if (Compare($rec->{last_hit}, $agedDate) == -1) {
568 unless ($params{dryrun}) {
570 userid => $params{userid},
571 type => $params{type},
572 sequence => $rec->{sequence},
576 $rec->{pattern} //= '';
577 $rec->{domain} //= '';
579 $params{log}->msg("Deleted $rec->{userid}:$params{type}:$rec->{sequence} "
580 . "$rec->{pattern}\@$rec->{domain} $dryrunstr");
581 $params{log}->dbug("last hit = $rec->{last_hit} < agedDate = $agedDate");
587 $params{log}->dbug("$rec->{userid}:$params{type}:$rec->{sequence}: nodelete $dryrunstr "
588 . "last hit = $rec->{last_hit} >= agedDate = $agedDate")
594 userid => $params{userid},
595 type => $params{type},
596 ) if $count && !$params{dryrun};
604 CheckParms(['userid'], \%params);
607 my $condition = "userid='$params{userid}'";
608 $condition .= " and $params{additional}" if $params{additional};
610 return $db->count($table, $condition);
616 CheckParms(['userid', 'type'], \%params);
619 my $condition = "userid='$params{userid}' and type='$params{type}'";
621 return $db->count($table, $condition);
627 CheckParms(['userid'], \%params);
629 my ($additional_condition) = delete $params{additional} || '';
631 my $condition = "userid='$userid'";
632 $condition .= " and $additional_condition" if $additional_condition;
634 return $db->count('log', $condition);
637 sub CountLogDistinct(%) {
640 CheckParms(['userid', 'column'], \%params);
642 my ($additional_condition) = delete $params{additional} || '';
644 my $condition = "userid='$userid'";
645 $condition .= " and $additional_condition" if $additional_condition;
647 return $db->count_distinct('log', $params{column}, $condition);
651 my ($password, $userid) = @_;
653 return $db->decode($password, $userid);
661 CheckParms(['userid', 'sender'], \%rec);
663 my ($username, $domain) = split /@/, $rec{sender};
667 $condition = "userid = '$rec{userid}' and sender = '$rec{sender}'";
669 $condition = "userid = '$rec{userid}' and sender like '%\@$domain'";
672 return $db->delete($table, $condition);
678 CheckParms(['userid', 'type', 'sequence'], \%rec);
680 my $condition = "userid = '$rec{userid}' and "
681 . "type = '$rec{type}' and "
682 . "sequence = $rec{sequence}";
684 return $db->delete('list', $condition);
688 my ($password, $userid) = @_;
690 return $db->encode($password, $userid);
696 CheckParms(['userid'], \%params);
699 my $condition = "userid='$params{userid}'";
700 $condition .= " and sender='$params{sender}'" if $params{sender};
701 $condition .= " and timestamp='$params{timestamp}'" if $params{timestamp};
703 return $db->find($table, $condition);
709 my ($type, $sender) = @_;
711 CheckParms(['userid', 'type'], \%params);
714 my $condition = "userid='$params{userid}' and type='$params{type}'";
716 if ($params{sender}) {
717 my ($username, $domain) = split /\@/, $params{sender};
719 # Split will return '' if either username or domain is missing. This messes
720 # up SQL's find as '' ~= NULL. Therefore we only specify username or domain
722 $condition .= " and pattern='$username'" if $username;
723 $condition .= " and domain='$domain'" if $domain;
726 return $db->find($table, $condition);
733 my $end_at = CountLog(
738 $start_at = $end_at - abs ($how_many);
739 $start_at = 0 if ($start_at < 0);
743 my $condition = "userid='$userid'";
744 my $additional = "order by timestamp limit $start_at, $end_at";
746 return $db->find($table, $condition, '*', $additional);
755 $condition = "userid='$userid'" if $params{userid};
757 return $db->find($table, $condition, $params{fields});
761 return $db->find('user', '', ['userid']);
780 sub GetNextSequenceNo(%) {
783 CheckParms(['userid', 'type'], \%rec);
786 my $condition = "userid='$rec{userid}' and type='$rec{type}'";
788 my $count = $db->count('list', $condition);
791 } # GetNextSequenceNo
800 my $userinfo = $db->getone('user', "userid='$userid'", ['name', 'email']);
802 return %{$db->getone('user', "userid='$userid'", ['name', 'email'])};
807 sub GetUserOptions($) {
810 my $table = 'useropts';
811 my $condition = "userid='$userid'";
813 $db->find($table, $condition);
817 while (my $rec = $db->getnext) {
818 $useropts{$rec->{name}} = $rec->{value};
825 my ($userid, $password) = @_;
827 $password = Encrypt($password, $userid);
829 # Check if user exists
830 my $dbpassword = UserExists($userid);
832 # Return -1 if user doesn't exist
833 return -1 unless $dbpassword;
835 # Return -2 if password does not match
836 if ($password eq $dbpassword) {
845 # Nulllist will simply discard the message.
846 my ($sender, $sequence, $hit_count) = @_;
851 sequence => $sequence,
852 hit_count => ++$hit_count,
860 message => 'Discarded message'
866 sub OnBlacklist($;$) {
867 my ($sender, $update) = @_;
869 return CheckOnList('black', $sender, $update);
872 sub OnNulllist($;$) {
873 my ($sender, $update) = @_;
875 return CheckOnList('null', $sender, $update);
878 sub OnWhitelist($;$$) {
879 my ($sender, $userid, $update) = @_;
881 SetContext($userid) if $userid;
883 return CheckOnList('white', $sender, $update);
887 my @tables = qw(email list log user useropts);
889 my ($err, $msg) = $db->lock('read', \@tables);
891 croak "Unable to lock table - $msg" if $err;
893 ($err, $msg) = $db->check(\@tables);
895 croak 'Unable to check tables ' . $msg if $err;
897 ($err, $msg) = $db->optimize(\@tables);
899 croak 'Unable to optimize tables ' . $msg if $err;
901 return $db->unlock();
905 # Reads an email message file from $input. Returns sender, subject,
906 # date and data, which is a copy of the entire message.
910 my $sender_long = '';
911 my $envelope_sender = '';
917 # Find first message's "From " line indicating start of message
923 # If we hit eof here then the message was garbled. Return indication of this
925 $data = "Garbled message - unable to find From line";
926 return $sender, $sender_long, $reply_to, $subject, $data;
930 $envelope_sender = $1;
931 $sender_long = $envelope_sender;
934 push @data, $_ if /^From /;
940 # Blank line indicates start of message body
941 last if ($_ eq "" || $_ eq "\r");
943 # Extract sender's address
949 if (/<(\S*)@(\S*)>/) {
950 $sender = lc ("$1\@$2");
951 } elsif (/(\S*)@(\S*)\ /) {
952 $sender = lc ("$1\@$2");
953 } elsif (/(\S*)@(\S*)/) {
954 $sender = lc ("$1\@$2");
956 } elsif (/^subject: .*/i) {
957 $subject = substr ($_, 9);
958 } elsif (/^reply-to: .*/i) {
959 $_ = substr ($_, 10);
960 if (/<(\S*)@(\S*)>/) {
961 $reply_to = lc ("$1\@$2");
962 } elsif (/(\S*)@(\S*)\ /) {
963 $reply_to = lc ("$1\@$2");
964 } elsif (/(\S*)@(\S*)/) {
965 $reply_to = lc ("$1\@$2");
978 # Set file pointer back by length of the line just read
979 seek ($input, -length () - 1, 1) if !eof $input;
981 # Sanitize email addresses
982 $envelope_sender =~ s/\<//g;
983 $envelope_sender =~ s/\>//g;
984 $envelope_sender =~ s/\"//g;
985 $envelope_sender =~ s/\'//g;
990 $reply_to =~ s/\<//g;
991 $reply_to =~ s/\>//g;
992 $reply_to =~ s/\"//g;
993 $reply_to =~ s/\'//g;
995 # Determine best addresses
996 $sender = $envelope_sender if $sender eq "";
997 $reply_to = $sender if $reply_to eq "";
999 return $sender, $sender_long, $reply_to, $subject, join "\n", @data;
1005 CheckParms(['userid', 'type', 'sequence', ], \%rec);
1007 my $current_date = UnixDatetime2SQLDatetime(scalar(localtime));
1010 my $condition = "userid='rec{userid} and type=$rec{type} and sequence='$rec{sequence}";
1014 condition => $condition,
1019 sub ResequenceList(%) {
1022 CheckParms(['userid', 'type'], \%params);
1025 return 1 unless $params{type} =~ /(white|black|null)/;
1026 return 2 unless UserExists($params{userid});
1029 my $condition = "userid='$params{userid}' and type ='$params{type}'";
1032 $db->lock('write', $table);
1034 # Get all records for $userid and $type
1035 my $listrecs = $db->get($table, $condition,'*', 'order by hit_count desc');
1037 # Delete all of the list entries for this $userid and $type
1038 my ($count, $msg) = $db->delete($table, $condition);
1040 # Now re-add list entries renumbering them
1044 $_->{sequence} = $sequence++;
1046 my ($err, $msg) = $db->add($table, %$_);
1059 CheckParms(['userid', 'type'], \%params);
1061 my $start_at = delete $params{start_at} || 0;
1062 my $lines = delete $params{lines} || 10;
1065 my $condition = "userid='$params{userid}' and type='$params{type}'";
1066 my $additional = "order by sequence limit $start_at, $lines";
1068 return $db->get($table, $condition, '*', $additional);
1074 # ReturnMsg will send back to the $sender the register message.
1075 # Messages are saved to be delivered when the $sender registers.
1077 # Added reply_to. Previously we passed reply_to into here as sender. This
1078 # caused a problem in that we were filtering as per sender but logging it
1079 # as reply_to. We only need reply_to for SendMsg so as to honor reply_to
1080 # so we now pass in both sender and reply_to
1082 CheckParms(['userid', 'sender', 'reply_to', 'subject', 'data'], \%params);
1084 #my ($sender, $reply_to, $subject, $data) = @_;
1086 # Check to see if this sender has already emailed us.
1087 my $msg_count = $db->count('email', "userid='$userid' and sender like '%$params{sender}%'");
1089 if ($msg_count < 5) {
1090 # Return register message
1092 userid => $params{userid},
1093 sender => $params{reply_to},
1094 subject => 'Your email has been returned by MAPS',
1095 msgfile => "$mapsbase/register.html",
1096 data => $params{data},
1097 ) if $msg_count == 0;
1100 userid => $params{userid},
1102 sender => $params{sender},
1103 message => 'Sent register reply',
1107 SaveMsg($params{sender}, $params{subject}, $params{data});
1109 Add2Nulllist($params{sender}, GetContext, "Auto Null List - Mail loop");
1112 userid => $params{userid},
1114 sender => $params{sender},
1115 message => 'Mail loop encountered',
1122 sub ReturnMessages(%) {
1125 CheckParms(['userid', 'sender'], \%params);
1127 my $table = 'email';
1128 my $condition = "userid='$params{userid}' and sender='$params{sender}'";
1129 my $fields = ['subject', 'timestamp'];
1130 my $additional = 'group by timestamp desc';
1132 return $db->get($table, $condition, $fields, $additional);
1135 sub ReturnSenders(%) {
1137 # This subroutine returns an array of senders in reverse chronological
1138 # order based on time timestamp from the log table of when we returned
1139 # their message. The complication here is that a single sender may
1140 # send multiple times in a single day. So if spammer@foo.com sends
1141 # spam @ 1 second after midnight and then again at 2 Pm there will be
1142 # at least two records in the log table saying that we returned his
1143 # email. Getting records sorted by timestamp desc will have
1144 # spammer@foo.com listed twice. But we want him listed only once, as
1145 # the first entry in the returned array. Plus we may be called
1146 # repeatedly with different $start_at's. Therefore we need to process
1147 # the whole list of returns for today, eliminate duplicate entries for
1148 # a single sender then slice the resulting array.
1149 CheckParms(['userid', 'type', 'lines'], \%params);
1152 my $condition = "userid='$params{userid}' and type='$params{type}'";
1153 my $additional = 'order by timestamp desc';
1155 $params{start_at} ||= 0;
1157 if ($params{date}) {
1158 $condition .= "and timestamp > '$params{date} 00:00:00' and "
1159 . "timestamp < '$params{date} 23:59:59'";
1162 $db->find($table, $condition, '*', $additional);
1164 # Watch the distinction between senders (plural) and sender (singular)
1167 # Run through the results and add to %senders by sender key. This
1168 # results in a hash that has the sender in it and the first
1169 # timestamp value. Since we already sorted timestamp desc by the
1170 # above select statement, and we've narrowed it down to only log
1171 # message that occurred for the given $date, we will have a hash
1172 # containing 1 sender and the latest timestamp for the day.
1173 while (my $rec = $db->getnext) {
1174 $senders{$rec->{sender}} = $rec->{timestamp}
1175 unless $senders{$rec->{sender}};
1178 # Make a hash whose keys are the timestamp (so we can later sort on
1180 my %sendersByTimestamp = reverse %senders;
1184 # Sort by timestamp desc and push on to the @senders array
1185 push @senders, $sendersByTimestamp{$_}
1186 for (sort { $b cmp $a } keys %sendersByTimestamp);
1188 # Finally slice for the given range
1189 my $end_at = $params{start_at} + $params{lines} - 1;
1191 $end_at = (@senders - 1)
1192 if $end_at > @senders;
1194 return (@senders) [$params{start_at} .. $end_at];
1198 my ($sender, $subject, $data) = @_;
1203 subject => $subject,
1210 sub SearchEmails(%) {
1213 CheckParms(['userid', 'search'], \%params);
1215 my $table = 'email';
1216 my $fields = ['sender', 'subject', 'timestamp'];
1217 my $condition = "userid='$params{userid}' and (sender like '\%$params{search}\%' "
1218 . "or subject like '\%$params{search}\%')";
1219 my $additional = 'order by timestamp desc';
1221 my ($err, $msg) = $db->find($table, $condition, $fields, $additional);
1225 while (my $rec = $db->getnext) {
1233 # SendMsg will send the message contained in $msgfile.
1236 #my ($sender, $subject, $msgfile, $userid, @spammsg) = @_;
1240 # Open return message template file
1241 open my $return_msg_file, '<', $params{msgfile}
1242 or die "Unable to open return msg file ($params{msgfile}): $!\n";
1244 # Read return message template file and print it to $msg_body
1245 while (<$return_msg_file>) {
1248 s/\$userid/$userid/;
1252 s/\$sender/$params{sender}/;
1258 close $return_msg_file;
1260 # Create the message, and set up the mail headers:
1261 my $msg = MIME::Entity->build(
1262 From => "MAPS\@DeFaria.com",
1263 To => $params{sender},
1264 Subject => $params{subject},
1265 Type => "text/html",
1269 # Need to obtain the spam message here...
1270 my @spammsg = split "\n", $params{data};
1274 Disposition => "attachment",
1279 open my $mail, '|-', '/usr/lib/sendmail -t -oi -oem'
1280 or croak "SendMsg: Unable to open pipe to sendmail $!";
1282 $msg->print(\*$mail);
1292 if (UserExists($to_user)) {
1295 return GetUserInfo $userid;
1304 my $total_space = 0;
1305 my $table = 'email';
1306 my $condition = "userid='$userid'";
1308 $db->find($table, $condition);
1310 while (my $rec = $db->getnext) {
1312 length($rec->{userid}) +
1313 length($rec->{sender}) +
1314 length($rec->{subject}) +
1315 length($rec->{timestamp}) +
1316 length($rec->{data});
1319 return $total_space;
1325 CheckParms(['userid', 'type', 'sequence'], \%rec);
1328 my $condition = "userid = '$rec{userid}' and type = '$rec{type}' and sequence = $rec{sequence}";
1330 if ($rec{pattern} =~ /\@/ and !$rec{domain}) {
1331 ($rec{pattern}, $rec{domain}) = split /\@/, $rec{pattern};
1332 } elsif (!$rec{pattern} and $rec{domain} =~ /\@/) {
1333 ($rec{pattern}, $rec{domain}) = split /\@/, $rec{domain};
1334 } elsif (!$rec{pattern} and !$rec{domain}) {
1335 return "Must specify either Username or Domain";
1338 $rec{pattern} //= 'null';
1339 $rec{domain} //= 'null';
1340 $rec{comment} //= 'null';
1342 if ($rec{retention}) {
1343 $rec{retention} = lc $rec{retention};
1346 return $db->update($table, $condition, %rec);
1352 CheckParms(['userid', 'name', 'email'], \%rec);
1354 return 1 unless UserExists($rec{userid});
1357 my $condition = "userid='$rec{userid}'";
1359 return $db->update($table, $condition, %rec);
1362 sub UpdateUserOptions ($@) {
1363 my ($userid, %options) = @_;
1365 return unless UserExists($userid);
1367 my $table = 'useropts';
1368 my $condition = "userid='$userid' and name=";
1370 $db->update($table, "$condition'$_'", (name=>$_, value=>$options{$_})) for (keys %options);
1373 } # UpdateUserOptions
1378 return 0 unless $userid;
1381 my $condition = "userid='$userid'";
1383 my $rec = $db->get($table, $condition);
1385 return 0 if scalar(@$rec) == 0;
1387 return $rec->[0]{password};
1390 sub Whitelist ($$;$$) {
1391 # Whitelist will deliver the message.
1392 my ($sender, $data, $sequence, $hit_count) = @_;
1394 my $userid = GetContext;
1396 # Dump message into a file
1397 open my $message, '>', "/tmp/MAPSMessage.$$"
1398 or Error("Unable to open message file (/tmp/MAPSMessage.$$): $!\n"), return -1;
1400 print $message $data;
1404 # Now call MAPSDeliver
1405 my $status = system "$FindBin::Bin/MAPSDeliver $userid /tmp/MAPSMessage.$$";
1407 unlink "/tmp/MAPSMessage.$$";
1412 type => 'whitelist',
1414 message => 'Delivered message',
1417 Error("Unable to deliver message - is MAPSDeliver setgid? - $!");
1420 $hit_count++ if $sequence;
1425 sequence => $sequence,
1426 hit_count => $hit_count,