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 ################################################################################use strict;
19 use lib $FindBin::Bin;
24 use Term::ReadLine::Gnu;
31 my $encrypted_password = Encrypt $password, $userid;
33 print "Password: $password = $encrypted_password\n";
40 my $decrypted_password = Decrypt $password, $userid;
42 print "Password: $password = $decrypted_password\n";
49 ResequenceList $userid, $type;
55 my $password = ReadLine (0);
67 if ($username ne "") {
68 $password = GetPassword if !defined $password or $password eq "";
71 while (Login ($username, $password) != 0) {
72 print "Login failed!\n";
75 if ($username eq "") {
76 print "Login aborted!\n";
80 $password = GetPassword;
87 # This function loads a ".list" file. This is to "import" our old ".list"
88 # files. Note it assumes that the ".list" files have specific names.
89 my $listfilename = shift;
93 if ($listfilename eq "white.list") {
95 } elsif ($listfilename eq "black.list") {
97 } elsif ($listfilename eq "null.list") {
100 print "Unknown list file: $listfilename\n";
104 if (!open LISTFILE, "<$listfilename") {
105 print "Unable to open $listfilename\n";
111 Info "Adding $listfilename to $listtype list";
115 next if m/^#/ || m/^$/;
117 my ($pattern, $comment) = split /\,/;
119 AddList $listtype, $pattern, 0, $comment;
123 if ($sequence == 0) {
124 print "No messages found to load ";
125 } elsif ($sequence == 1) {
126 print "Loaded 1 message ";
128 print "Loaded $sequence messages ";
130 print "from $listfilename\n";
136 # This function loads an mbox file.
139 if (!open FILE, "<$file") {
140 print "Unable to open \"$file\" - $!\n";
149 my ($sender, $reply_to, $subject, $data) = ReadMsg (*FILE);
153 AddEmail $sender, $subject, $data;
155 Info "Added message from $sender to email";
158 if ($nbr_msgs == 0) {
159 print "No messages found to load ";
160 } elsif ($nbr_msgs == 1) {
161 print "Loaded 1 message ";
163 print "Loaded $nbr_msgs messages ";
165 print "from $file\n";
169 # This function unloads email to a mbox file.
172 if (!open FILE, ">$file") {
173 print "Unable to open \"$file\" - $!\n";
180 my $handle = FindEmail;
181 my ($userid, $sender, $subject, $timestamp, $message);
183 while (($userid, $sender, $subject, $timestamp, $message) = GetEmail $handle) {
188 print "$i messages dumped to $file\n";
194 my $new_user = shift;
196 if ($new_user = Login2MAPS $new_user) {
197 print "You are now logged in as $new_user\n";
204 my $userid = GetContext;
206 if (defined $detail) {
207 my %msg_space = MAPS::Space $userid;
209 foreach (sort (keys (%msg_space))) {
211 my $size = $msg_space {$_};
213 @######### @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
220 my $total_space = MAPS::Space $userid;
221 $total_space = $total_space / (1024 * 1024);
223 Total size @###.### Meg
232 print "Current userid is " . GetContext () . "\n";
236 my $handle = FindUser;
238 my ($userid, $name, $email);
241 User ID: @<<<<<<<<< Name: @<<<<<<<<<<<<<<<<<<< Email: @<<<<<<<<<<<<<<<<<<<<<<<
245 while (($userid, $name, $email) = GetUser $handle) {
246 last if ! defined $userid;
254 my $handle = FindEmail;
256 my ($userid, $sender, $subject, $timestamp, $message);
259 @<<<<<<<<<<<<<<<<<<<@<<<<<<<<<<<<<<<<<< @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
260 $timestamp,$sender,$subject
263 while (($userid, $sender, $subject, $timestamp, $message) = GetEmail $handle) {
264 last if ! defined $userid;
272 my $how_many = shift;
274 $how_many = defined $how_many ? $how_many : -20;
276 my $handle = FindLog $how_many;
278 my ($userid, $timestamp, $sender, $type, $message);
281 @<<<<<<<<<<<<<<<<<<<@<<<<<<<<< @<<<<<<<<<<<<<<<< @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
282 $timestamp,$type,$sender,$message
285 while (($userid, $timestamp, $sender, $type, $message) = GetLog $handle) {
286 last if ! defined $userid;
302 @>> @<<<<<<<<<<<<<<<<<<<<<<<< @<<<<<<<<<<<<<<<<<<<<<<<< @<<<<<<<<<<<<<<<<<<<<<
303 $record{sequence},$record{pattern},$record{domain},$record{comment}
307 while (@list = ReturnList $type, $next, $lines) {
312 print "Hit any key to continue";
319 my $nbr_days = shift;
321 $nbr_days = 1 if !defined $nbr_days;
323 my %dates = GetStats $nbr_days;
325 foreach my $date (keys (%dates)) {
326 foreach (keys (%{$dates{$date}})) {
328 print "\t$dates{$date}{$_}\n";
336 if (!open MESSAGE, "<$file") {
337 print "Unable to open message file $file\n";
346 Whitelist "Andrew\@DeFaria.com", $data;
363 /^resequence / && do {
364 Resequence GetContext (), $parm1;
369 EncryptPassword $parm1, $parm2;
374 my $password = UserExists (GetContext());
375 DecryptPassword $password;
384 /^add2whitelist / && do {
385 Add2Whitelist $parm1, GetContext (), $parm2;
389 /^showusers / && do {
395 AddUser $parm1, $parm2, $parm3, $parm4;
399 /^cleanemail / && do {
401 $parm1 = "9999-12-31 23:59:59";
403 my $nbr_entries = CleanEmail $parm1;
404 print "$nbr_entries email entries cleaned\n";
408 /^deleteemail / && do {
409 my $nbr_entries = DeleteEmail $parm1;
410 print "$nbr_entries email entries deleted\n";
416 $parm1 = "9999-12-31 23:59:59";
418 my $nbr_entries = CleanLog $parm1;
419 print "$nbr_entries log entries cleaned\n";
428 /^loademail / && do {
433 /^dumpemail / && do {
439 Logmsg "info", "$parm1 $parm2", $parm3;
443 /^switchuser / && do {
453 /^showemail / && do {
473 /^showstats / && do {
479 print "Valid commands are:\n\n";
480 print "adduser <userid> <realname> <email> <password>\tAdd user to DB\n";
481 print "add2whitelist <sender> <name>\t\tAdd sender to whitelist\n";
482 print "cleanlog [timestamp]\t\tCleans out old log entries\n";
483 print "log <message>\t\t\tLogs a message\n";
484 print "loadlist <listfile>\t\t\tLoad a list file\n";
485 print "cleanemail [timestamp]\t\tCleans out old email entries\n";
486 print "deliver <message>\t\t\tDelivers a message\n";
487 print "loademail <mbox>\t\t\tLoad an mbox file\n";
488 print "dumpemail <mbox>\t\t\tDump email from DB to an mbox file\n";
489 print "deleteemail <sender>\t\t\tDelete email from sender\n";
490 print "switchuser <userid>\t\t\tSwitch to user\n";
491 print "showuser\t\t\t\tShow current user\n";
492 print "showusers\t\t\t\tShows users in the DB\n";
493 print "showemail\t\t\t\tDisplays email\n";
494 print "showlog <nbr>\t\t\tDisplays <nbr> log entries\n";
495 print "space\t <detail>\t\t\tDisplay space usage\n";
496 print "showlist <type>\t\t\tShow list by type\n";
497 print "showstats <nbr>\t\t\tDisplays <nbr> days of stats\n";
498 print "encrypt <password>\t\t\tEncrypt a password\n";
499 print "resequence <list>\t\t\tResequences a list\n";
500 print "help\t\t\t\t\tThis screen\n";
501 print "exit\t\t\t\t\tExit mapsutil\n";
505 print "Unknown command: $_";
507 print " ($parm1" if defined $parm1;
508 print ", $parm2" if defined $parm2;
509 print ", $parm3" if defined $parm3;
510 print ", $parm4" if defined $parm4;
512 if (defined $parm1) {
523 my $maps_username = $ENV{MAPS_USERNAME} ? $ENV{MAPS_USERNAME} : $ENV{USER};
524 my $username = Login2MAPS $maps_username, $ENV{MAPS_PASSWORD};
526 if (defined $ARGV [0]) {
527 ParseCommand $ARGV [0], $ARGV [1], $ARGV [2], $ARGV [3];
532 my $term = new Term::ReadLine 'mapsutil';
535 $_ = $term->readline ("MAPSUtil:");
539 my ($cmd, $parm1, $parm2, $parm3, $parm4) = split;
541 last if ($cmd =~ /exit/i || $cmd =~ /quit/i);
543 ParseCommand $cmd, $parm1, $parm2, $parm3, $parm4 if defined $cmd;
546 print "\n" if !defined $_;