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 use lib "$FindBin::Bin/../lib";
26 use Term::ReadLine::Gnu;
31 sub EncryptPassword($$) {
32 my ($password, $userid) = @_;
34 my $encrypted_password = Encrypt $password, $userid;
36 print "Password: $password = $encrypted_password\n";
41 sub DecryptPassword($$) {
42 my ($password, $userid) = @_;
44 my $decrypted_password = Decrypt($password, $userid);
46 print "Password: $password = $decrypted_password\n";
52 my ($userid, $type) = @_;
54 MAPS::ResequenceList($userid, $type);
60 my $password = ReadLine(0);
69 my ($username, $password) = @_;
71 if ($username ne '') {
72 $password = GetPassword if !defined $password or $password eq "";
75 while (Login($username, $password) != 0) {
76 print "Login failed!\n";
79 if ($username eq "") {
80 print "Login aborted!\n";
84 $password = GetPassword;
91 # This function loads a ".list" file. This is to "import" our old ".list"
92 # files. Note it assumes that the ".list" files have specific names.
93 my ($listfilename) = @_;
97 if ($listfilename eq "white.list") {
99 } elsif ($listfilename eq "black.list") {
101 } elsif ($listfilename eq "null.list") {
104 print "Unknown list file: $listfilename\n";
110 if (!open $listfile, '<', $listfilename) {
111 print "Unable to open $listfilename\n";
117 Info("Adding $listfilename to $listtype list");
121 next if m/^#/ || m/^$/;
123 my ($pattern, $comment) = split /\,/;
125 AddList($listtype, $pattern, 0, $comment);
129 if ($sequence == 0) {
130 print "No messages found to load ";
131 } elsif ($sequence == 1) {
132 print "Loaded 1 message ";
134 print "Loaded $sequence messages ";
136 print "from $listfilename\n";
142 # This function loads an mbox file.
147 if (!open $file, '<', $filename) {
148 print "Unable to open \"$filename\" - $!\n";
157 my ($sender, $reply_to, $subject, $data) = ReadMsg (*$file);
161 AddEmail($sender, $subject, $data);
163 Info("Added message from $sender to email");
166 if ($nbr_msgs == 0) {
167 print "No messages found to load ";
168 } elsif ($nbr_msgs == 1) {
169 print "Loaded 1 message ";
171 print "Loaded $nbr_msgs messages ";
173 print "from $file\n";
177 # This function unloads email to a mbox file.
182 if (!open $file, '>', $filename) {
183 print "Unable to open \"$filename\" - $!\n";
190 my $handle = FindEmail;
192 my ($userid, $sender, $subject, $timestamp, $message);
194 while (($userid, $sender, $subject, $timestamp, $message) = GetEmail($handle)) {
195 print $file $message;
199 print "$i messages dumped to $file\n";
207 if ($new_user = Login2MAPS($new_user)) {
208 print "You are now logged in as $new_user\n";
215 my $userid = GetContext;
218 my %msg_space = Space($userid);
220 for (sort (keys (%msg_space))) {
222 my $size = $msg_space{$_};
224 @######### @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
231 my $total_space = Space($userid);
233 $total_space = $total_space / (1024 * 1024);
236 Total size @###.### Meg
245 print "Current userid is " . GetContext() . "\n";
249 my ($handle) = FindUser;
251 my ($userid, $name, $email);
254 User ID: @<<<<<<<<< Name: @<<<<<<<<<<<<<<<<<<< Email: @<<<<<<<<<<<<<<<<<<<<<<<
258 while (($userid, $name, $email) = GetUser($handle)) {
259 last if ! defined $userid;
267 my ($handle) = FindEmail;
269 my ($userid, $sender, $subject, $timestamp, $message);
272 @<<<<<<<<<<<<<<<<<<<@<<<<<<<<<<<<<<<<<< @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
273 $timestamp,$sender,$subject
276 while (($userid, $sender, $subject, $timestamp, $message) = GetEmail($handle)) {
287 $how_many = defined $how_many ? $how_many : -20;
289 my $handle = FindLog($how_many);
291 my ($userid, $timestamp, $sender, $type, $message);
294 @<<<<<<<<<<<<<<<<<<<@<<<<<<<<< @<<<<<<<<<<<<<<<< @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
295 $timestamp,$type,$sender,$message
298 while (($userid, $timestamp, $sender, $type, $message) = GetLog $handle) {
315 @>> @<<<<<<<<<<<<<<<<<<<<<<<< @<<<<<<<<<<<<<<<<<<<<<<<< @<<<<<<<<<<<<<<<<<<<<<
316 $record{sequence},$record{pattern},$record{domain},$record{comment}
320 while (@list = ReturnList($type, $next, $lines)) {
325 print "Hit any key to continue";
336 my %dates = GetStats($nbr_days);
338 for my $date (keys(%dates)) {
339 for (keys(%{$dates{$date}})) {
341 print "\t$dates{$date}{$_}\n";
351 if (!open $message, '<', $filename) {
352 print "Unable to open message file $filename\n";
362 Whitelist "Andrew\@DeFaria.com", $data;
369 sub ParseCommand($$$$$){
370 my ($cmd, $parm1, $parm2, $parm3,$parm4) = @_;
379 /^resequence / && do {
380 Resequence(GetContext(), $parm1);
385 EncryptPassword($parm1, $parm2);
390 my $password = UserExists(GetContext());
391 DecryptPassword($password, $maps_username);
400 /^add2whitelist / && do {
401 Add2Whitelist($parm1, GetContext(), $parm2);
405 /^showusers / && do {
411 AddUser($parm1, $parm2, $parm3, $parm4);
415 /^cleanemail / && do {
417 $parm1 = "9999-12-31 23:59:59";
419 my $nbr_entries = CleanEmail($parm1);
420 print "$nbr_entries email entries cleaned\n";
424 /^deleteemail / && do {
425 my $nbr_entries = DeleteEmail($parm1);
426 print "$nbr_entries email entries deleted\n";
432 $parm1 = "9999-12-31 23:59:59";
434 my $nbr_entries = CleanLog($parm1);
435 print "$nbr_entries log entries cleaned\n";
440 LoadListFile($parm1);
444 /^loademail / && do {
449 /^dumpemail / && do {
455 Logmsg("info", "$parm1 $parm2", $parm3);
459 /^switchuser / && do {
469 /^showemail / && do {
489 /^showstats / && do {
495 print "Valid commands are:\n\n";
496 print "adduser <userid> <realname> <email> <password>\tAdd user to DB\n";
497 print "add2whitelist <sender> <name>\t\tAdd sender to whitelist\n";
498 print "cleanlog [timestamp]\t\tCleans out old log entries\n";
499 print "log <message>\t\t\tLogs a message\n";
500 print "loadlist <listfile>\t\t\tLoad a list file\n";
501 print "cleanemail [timestamp]\t\tCleans out old email entries\n";
502 print "deliver <message>\t\t\tDelivers a message\n";
503 print "loademail <mbox>\t\t\tLoad an mbox file\n";
504 print "dumpemail <mbox>\t\t\tDump email from DB to an mbox file\n";
505 print "deleteemail <sender>\t\t\tDelete email from sender\n";
506 print "switchuser <userid>\t\t\tSwitch to user\n";
507 print "showuser\t\t\t\tShow current user\n";
508 print "showusers\t\t\t\tShows users in the DB\n";
509 print "showemail\t\t\t\tDisplays email\n";
510 print "showlog <nbr>\t\t\tDisplays <nbr> log entries\n";
511 print "space\t <detail>\t\t\tDisplay space usage\n";
512 print "showlist <type>\t\t\tShow list by type\n";
513 print "showstats <nbr>\t\t\tDisplays <nbr> days of stats\n";
514 print "encrypt <password>\t\t\tEncrypt a password\n";
515 print "resequence <list>\t\t\tResequences a list\n";
516 print "help\t\t\t\t\tThis screen\n";
517 print "exit\t\t\t\t\tExit mapsutil\n";
521 print "Unknown command: $_";
523 print " ($parm1" if $parm1;
524 print ", $parm2" if $parm2;
525 print ", $parm3" if $parm3;
526 print ", $parm4" if $parm4;
531 $maps_username = $ENV{MAPS_USERNAME} ? $ENV{MAPS_USERNAME} : $ENV{USER};
533 my $username = Login2MAPS($maps_username, $ENV{MAPS_PASSWORD});
536 ParseCommand($ARGV[0], $ARGV[1], $ARGV[2], $ARGV[3], $ARGV[4]);
541 my $term = new Term::ReadLine 'mapsutil';
544 $_ = $term->readline ("MAPSUtil:");
548 my ($cmd, $parm1, $parm2, $parm3, $parm4) = split;
550 last if ($cmd =~ /exit/i || $cmd =~ /quit/i);
552 ParseCommand($cmd, $parm1, $parm2, $parm3, $parm4) if defined $cmd;
555 print "\n" unless $_;