2 ################################################################################
3 # File: $RCSfile: mapsutil,v $
4 # Revision: $Revision: 1.1 $
5 # Description: This script implements a small command interpreter to exercise
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-2006, Andrew@DeFaria.com, all rights reserved.
14 ################################################################################
20 # For use of the given/when (See https://perlmaven.com/switch-case-statement-in-perl5)
21 no warnings 'experimental';
27 use lib "$FindBin::Bin/../lib";
28 use lib "$FindBin::Bin/../../lib";
39 help => 'Add a user to MAPS',
40 description => 'Usage: adduser <userid> <name> <email> <password>',
43 help => 'Add sender to whitelist',
44 description => 'Usage: add2whitelist <sender> <retention>',
47 help => 'Cleans out old log entries',
48 description => 'Usage; cleanlog [timestamp]'
51 help => 'Logs a message',
52 description => 'Usage: log <message>',
55 help => 'Load a list file',
56 description => 'Usage: loadlist <listfile>',
59 help => 'Cleans out old email entries',
60 description => 'Usage: cleanemail [timestamp]',
63 help => 'Delivers a message',
64 description => 'Usage: deliver <message>',
67 help => 'Load an mbox file',
68 description => 'Usage: loademail <mbox>',
71 help => 'Dump email from DB to mbox file',
72 description => 'Usage: ',
75 help => 'Decrypt a password',
76 description => 'Usage: decrypt <password>',
79 help => 'Switch to user',
80 description => 'Usage: switchuser <userid>',
83 help => "Set a user's password",
84 description => 'Usage: setpassword',
87 help => 'Show current user',
88 description => 'Usage: showuser',
91 help => 'Shows users in the DB',
92 description => 'Usage: showusers',
95 help => 'Displays email',
96 description => 'Usage: showemail',
99 help => 'Displays <nbr> log entires',
100 description => 'Usage: showlog <nbr>',
103 help => 'Display space usage',
104 description => 'Usage: space',
107 help => 'Show list by <type>',
108 description => 'Usage: showlist <type>',
111 help => 'Encrypt a password',
112 description => 'Usage: encrypt <password>',
115 help => 'Resequences a <list>',
116 description => 'Usage: resequence <list>',
120 my $userid = GetContext;
122 sub EncryptPassword($$) {
123 my ($password, $userid) = @_;
125 my $encrypted_password = Encrypt $password, $userid;
127 say "Encrypted password: '$encrypted_password'";
132 sub DecryptPassword($$) {
133 my ($password, $userid) = @_;
135 my $decrypted_password = Decrypt($password, $userid);
137 say "Decrypted password: $decrypted_password";
143 my ($userid, $type) = @_;
151 sub Login2MAPS($;$) {
152 my ($username, $password) = @_;
154 if ($username ne '') {
155 $password = GetPassword unless $password;
158 while (Login($username, $password) != 0) {
165 if ($username eq '') {
166 say "Login aborted!";
173 $password = GetPassword;
179 sub LoadListFile($) {
180 # This function loads a ".list" file. This is to "import" our old ".list"
181 # files. Note it assumes that the ".list" files have specific names.
182 my ($listfilename) = @_;
186 if ($listfilename eq "white.list") {
188 } elsif ($listfilename eq "black.list") {
190 } elsif ($listfilename eq "null.list") {
193 say "Unknown list file: $listfilename";
199 if (!open $listfile, '<', $listfilename) {
200 say "Unable to open $listfilename";
206 Info("Adding $listfilename to $listtype list");
210 next if m/^#/ || m/^$/;
212 my ($pattern, $comment) = split /\,/;
214 AddList($listtype, $pattern, 0, $comment);
218 if ($sequence == 0) {
219 say "No messages found to load";
220 } elsif ($sequence == 1) {
221 say "Loaded 1 message ";
223 say "Loaded $sequence messages";
226 say "from $listfilename";
232 # This function loads an mbox file.
237 open $file, '<', $filename
238 or die "Unable to open \"$filename\" - $!\n";
245 my ($sender, $reply_to, $subject, $data) = ReadMsg (*$file);
256 Info("Added message from $sender to email");
259 if ($nbr_msgs == 0) {
260 say "No messages found to load";
261 } elsif ($nbr_msgs == 1) {
262 say "Loaded 1 message";
264 say "Loaded $nbr_msgs messages";
271 # This function unloads email to a mbox file.
276 open $file, '>', $filename or
277 die "Unable to open \"$filename\" - $!\n";
283 my ($err, $msg) = $MAPS::db->find(
285 "userid = '$userid'",
291 while (my $rec = $MAPS::db->getnext) {
292 say $file $rec->{data};
296 say "$i messages dumped to $file";
304 if ($new_user = Login2MAPS($new_user)) {
305 say "You are now logged in as $new_user";
310 FindUser(userid => $userid);
316 my $password = GetPassword('Enter new password');
317 my $repeat = GetPassword('Enter new password again');
319 if ($password ne $repeat) {
320 say "Passwords don't match!";
322 $rec->{password} = Encrypt($password, $userid);
326 say "Password updated";
333 my $userid = GetContext;
335 my $total_space = Space($userid);
337 $total_space = $total_space / (1024 * 1024);
340 Total size @###.### Meg
349 say "Current userid is " . GetContext();
354 fields => ['userid', 'name', 'email'],
360 User ID: @<<<<<<<<< Name: @<<<<<<<<<<<<<<<<<<< Email: @<<<<<<<<<<<<<<<<<<<<<<<
361 $rec->{userid},$rec->{name},$rec->{email}
364 while ($rec = GetUser) {
365 last unless $rec->{userid};
371 my ($err, $msg) = $MAPS::db->find(
374 qw(userid timestamp sender subject),
377 my ($timestamp, $sender, $subject);
380 @<<<<<<<<<<<<<<<<<<<@<<<<<<<<<<<<<<<<<< @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
381 $timestamp,$sender,$subject
385 while (my $rec = $MAPS::db->getnext) {
386 last unless $rec->{userid};
388 $timestamp = $rec->{timestamp};
389 $sender = $rec->{sender};
390 $subject = $rec->{subject};
399 $how_many = defined $how_many ? $how_many : -20;
401 my $handle = FindLog($how_many);
403 my ($userid, $timestamp, $sender, $type, $message);
406 @<<<<<<<<<<<<<<<<<<<@<<<<<<<<< @<<<<<<<<<<<<<<<< @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
407 $timestamp,$type,$sender,$message
413 while (my $rec = GetLog) {
414 $timestamp = $rec->{timestamp} || '';
415 $type = $rec->{type} || '';
416 $sender = $rec->{sender} || '';
417 $message = $rec->{message} || '';
421 last if $count > $how_many;
438 @>> @<<<<<<<<<<<<<<<<<<<<<<<< @<<<<<<<<<<<<<<<<<<<<<<<< @<<<<<<<<<<<<<<<<<<<<<
439 $record{sequence},$record{pattern},$record{domain},$record{comment}
443 # TODO: Why does ReturnList return a one entry array with a many entry array
444 # of hashes. Seems it should just return $list[0], right?
445 while (@list = ReturnList(
453 # Format blows up if any field is undefined so...
454 $record{pattern} //= '';
455 $record{domain} //= '';
456 $record{comment} //= '';
460 print 'Hit any key to continue - q to quit';
463 my $key = ReadKey(0);
466 if ($key eq 'q' or ord $key == 67) {
485 my %dates = GetStats(
490 for my $date (keys(%dates)) {
491 for (keys(%{$dates{$date}})) {
493 say "\t$dates{$date}{$_}";
503 if (!open $message, '<', $filename) {
504 say "Unable to open message file $filename";
514 Whitelist "Andrew\@DeFaria.com", $data;
524 my ($cmd, $parm1, $parm2, $parm3, $parm4) = split /\s+/, $line;
531 when (/^\s*resequence\s*$/) {
532 Resequence(GetContext(), $parm1);
535 when (/^s*encrypt\s*$/) {
536 EncryptPassword($parm1, $userid);
539 when (/^\s*encrypt\s*$/) {
540 EncryptPassword($parm1, $userid);
543 when (/^\s*decrypt\s*$/) {
544 DecryptPassword($parm1, $userid);
547 when (/^\s*deliver\s*$/) {
551 when (/^\s*add2whitelist\s*$/) {
553 $parm2 .= ' ' . $parm3
557 userid => GetContext,
564 when (/^\s*showusers\s*$/) {
568 when (/^\s*adduser\s*$/) {
573 password => Encrypt($parm4, $userid),
577 when (/^\s*cleanemail\s*$/) {
578 $parm1 = "9999-12-31 23:59:59" unless $parm1;
580 say CleanEmail($parm1);
583 when (/^\s*cleanlog\s*$/) {
584 $parm1 = "9999-12-31 23:59:59" unless $parm1;
586 say CleanLog($parm1);
589 when (/^\s*loadlist\s*$/) {
590 LoadListFile($parm1);
593 when (/^\s*loademail\s*$/) {
597 when (/^\s*dumpemail\s*$/) {
601 when (/^\s*log\s*$/) {
610 when (/^\s*switchuser\s*$/) {
614 when (/^\s*showuser\s*$/) {
618 when (/^\s*showemail\s*$/) {
622 when (/^\s*showlog\s*$/) {
626 when (/^\s*showlist\s*$/) {
630 when (/^\s*space\s*$/) {
634 when (/^\s*showstats\s*$/) {
638 when (/^\s*setpassword\s*$/) {
643 say "Unknown command: $_";
645 say "Parm1: $parm1" if $parm1;
646 say "Parm2: $parm2" if $parm2;
647 say "Parm3: $parm3" if $parm3;
648 say "Parm4: $parm4" if $parm4;
655 my $username = Login2MAPS($userid, $ENV{MAPS_PASSWORD});
658 ExecuteCmd join ' ', @ARGV;
663 $CmdLine::cmdline->set_cmds(%cmds);
664 $CmdLine::cmdline->set_eval(\&ExecuteCmd);
666 while (my ($line, $result) = $CmdLine::cmdline->get) {
669 last if $line =~ /^\s*exit\s*$/i or $line =~ /^\s*quit\s*$/i;