Initial commit
[clearscm.git] / maps / bin / mapsutil
1 #!/usr/bin/perl
2 #################################################################################
3 # File:         $RCSfile: mapsutil,v $
4 # Revision:     $Revision: 1.1 $
5 # Description:  This script implements a small command interpreter to exercise
6 #               MAPS functions.
7 # Author:       Andrew@DeFaria.com
8 # Created:      Fri Nov 29 14:17:21  2002
9 # Modified:     $Date: 2013/06/12 14:05:47 $
10 # Language:     perl
11 #
12 # (c) Copyright 2000-2006, Andrew@DeFaria.com, all rights reserved.
13 #
14 ################################################################################use strict;
15 use warnings;
16
17 use FindBin;
18
19 use lib $FindBin::Bin;
20
21 use MAPS;
22 use MAPSLog;
23 use Term::ReadLine;
24 use Term::ReadLine::Gnu;
25 use Term::ReadKey;
26
27 sub EncryptPassword {
28   my $password = shift;
29   my $userid   = shift;
30
31   my $encrypted_password = Encrypt $password, $userid;
32
33   print "Password: $password = $encrypted_password\n";
34 } # EncryptPassword
35
36 sub DecryptPassword {
37   my $password = shift;
38   my $userid   = shift;
39
40   my $decrypted_password = Decrypt $password, $userid;
41
42   print "Password: $password = $decrypted_password\n";
43 } # DecryptPassword
44
45 sub Resequence {
46   my $userid    = shift;
47   my $type      = shift;
48
49   ResequenceList $userid, $type;
50 } # Resequence
51
52 sub GetPassword {
53   print "Password:";
54   ReadMode "noecho";
55   my $password = ReadLine (0);
56   chomp $password;
57   print "\n";
58   ReadMode "normal";
59
60   return $password
61 } # GetPassword
62
63 sub Login2MAPS {
64   my $username = shift;
65   my $password = shift;
66
67   if ($username ne "") {
68     $password = GetPassword if !defined $password or $password eq "";
69   } # if
70
71   while (Login ($username, $password) != 0) {
72     print "Login failed!\n";
73     print "Username:";
74     $username = <>;
75     if ($username eq "") {
76       print "Login aborted!\n";
77       return undef;
78     } # if
79     chomp $username;
80     $password = GetPassword;
81   } # if
82
83   return $username;
84 } # Login2MAPS
85
86 sub LoadListFile {
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;
90
91   my $listtype;
92
93   if ($listfilename eq "white.list") {
94     $listtype = "white";
95   } elsif ($listfilename eq "black.list") {
96     $listtype = "black";
97   } elsif ($listfilename eq "null.list") {
98     $listtype = "null";
99   } else {
100     print "Unknown list file: $listfilename\n";
101     return;
102   } # if
103
104   if (!open LISTFILE, "<$listfilename") {
105     print "Unable to open $listfilename\n";
106     return;
107   } # if
108
109   my $sequence = 0;
110
111   Info "Adding $listfilename to $listtype list";
112
113   while (<LISTFILE>) {
114     chomp;
115     next if m/^#/ || m/^$/;
116
117     my ($pattern, $comment) = split /\,/;
118
119     AddList $listtype, $pattern, 0, $comment;
120     $sequence++;
121   } # while
122
123   if ($sequence == 0) {
124     print "No messages found to load ";
125   } elsif ($sequence == 1) {
126     print "Loaded 1 message ";
127   } else {
128     print "Loaded $sequence messages ";
129   } # if
130   print "from $listfilename\n";
131
132   close LISTFILE;
133 } # LoadListFile
134
135 sub LoadEmail {
136   # This function loads an mbox file.
137   my $file = shift;
138
139   if (!open FILE, "<$file") {
140     print "Unable to open \"$file\" - $!\n";
141     return;
142   } # if
143
144   binmode FILE;
145
146   my $nbr_msgs;
147
148   while (! eof FILE) {
149     my ($sender, $reply_to, $subject, $data) = ReadMsg (*FILE);
150
151     $nbr_msgs++;
152
153     AddEmail $sender, $subject, $data;
154
155     Info "Added message from $sender to email";
156   } # while
157
158   if ($nbr_msgs == 0) {
159     print "No messages found to load ";
160   } elsif ($nbr_msgs == 1) {
161     print "Loaded 1 message ";
162   } else {
163     print "Loaded $nbr_msgs messages ";
164   } # if
165   print "from $file\n";
166 } # LoadEmail
167
168 sub DumpEmail {
169   # This function unloads email to a mbox file.
170   my $file = shift;
171
172   if (!open FILE, ">$file") {
173     print "Unable to open \"$file\" - $!\n";
174     return;
175   } # if
176
177   binmode FILE;
178
179   my $i = 0;
180   my $handle = FindEmail;
181   my ($userid, $sender, $subject, $timestamp, $message);
182
183   while (($userid, $sender, $subject, $timestamp, $message) = GetEmail $handle) {
184     print FILE $message;
185     $i++;
186   } # while
187
188   print "$i messages dumped to $file\n";
189
190   close FILE;
191 } # DumpEmail
192
193 sub SwitchUser {
194   my $new_user = shift;
195
196   if ($new_user = Login2MAPS $new_user) {
197     print "You are now logged in as $new_user\n";
198   } # if
199 } # SwitchContext
200
201 sub ShowSpace {
202   my $detail = shift;
203
204   my $userid = GetContext;
205
206   if (defined $detail) {
207     my %msg_space = MAPS::Space $userid;
208
209     foreach (sort (keys (%msg_space))) {
210       my $sender        = $_;
211       my $size          = $msg_space {$_};
212       format PER_MSG=
213 @######### @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
214 $size,$sender
215 .
216 $~ = "PER_MSG";
217       write ();
218     } # foreach
219   } else {
220     my $total_space = MAPS::Space $userid;
221     $total_space = $total_space / (1024 * 1024);
222     format TOTALSIZE=
223 Total size @###.### Meg
224 $total_space
225 .
226 $~ = "TOTALSIZE";
227     write ();
228   } # if
229 } # ShowSpace
230
231 sub ShowUser {
232   print "Current userid is " . GetContext () . "\n";
233 } # ShowContext
234
235 sub ShowUsers {
236   my $handle = FindUser;
237
238   my ($userid, $name, $email);
239
240   format USERLIST =
241 User ID: @<<<<<<<<< Name: @<<<<<<<<<<<<<<<<<<< Email: @<<<<<<<<<<<<<<<<<<<<<<<
242 $userid,$name,$email
243 .
244 $~ = "USERLIST";
245   while (($userid, $name, $email) = GetUser $handle) {
246     last if ! defined $userid;
247     write ();
248   } # while
249
250   $handle->finish;
251 } # ShowUsers
252
253 sub ShowEmail {
254   my $handle = FindEmail;
255
256   my ($userid, $sender, $subject, $timestamp, $message);
257
258 format EMAIL =
259 @<<<<<<<<<<<<<<<<<<<@<<<<<<<<<<<<<<<<<< @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
260 $timestamp,$sender,$subject
261 .
262 $~ = "EMAIL";
263   while (($userid, $sender, $subject, $timestamp, $message) = GetEmail $handle) {
264     last if ! defined $userid;
265     write ();
266   } # while
267
268   $handle->finish;
269 } # ShowEmail
270
271 sub ShowLog {
272   my $how_many = shift;
273
274   $how_many = defined $how_many ? $how_many : -20;
275
276   my $handle = FindLog $how_many;
277
278   my ($userid, $timestamp, $sender, $type, $message);
279
280 format LOG =
281 @<<<<<<<<<<<<<<<<<<<@<<<<<<<<< @<<<<<<<<<<<<<<<< @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
282 $timestamp,$type,$sender,$message
283 .
284 $~ = "LOG";
285   while (($userid, $timestamp, $sender, $type, $message) = GetLog $handle) {
286     last if ! defined $userid;
287     write ();
288   } # while
289
290   $handle->finish;
291 } # ShowLog
292
293 sub ShowList {
294   my $type = shift;
295
296   my $lines = 10;
297   my $next  = 0;
298   my @list;
299   my %record;
300
301 format LIST =
302 @>> @<<<<<<<<<<<<<<<<<<<<<<<< @<<<<<<<<<<<<<<<<<<<<<<<< @<<<<<<<<<<<<<<<<<<<<<
303 $record{sequence},$record{pattern},$record{domain},$record{comment}
304 .
305 $~ = "LIST";
306
307   while (@list = ReturnList $type, $next, $lines) {
308     foreach (@list) {
309       %record = %{$_};
310       write ();
311     } # foreach
312     print "Hit any key to continue";
313     ReadLine (0);
314     $next += $lines;
315   } # while
316 } # ShowList
317
318 sub ShowStats {
319   my $nbr_days  = shift;
320
321   $nbr_days = 1 if !defined $nbr_days;
322
323   my %dates = GetStats $nbr_days;
324
325   foreach my $date (keys (%dates)) {
326     foreach (keys (%{$dates{$date}})) {
327       print "$date $_:";
328         print "\t$dates{$date}{$_}\n";
329     } # foreach
330   } # foreach
331 } # ShowStats
332
333 sub Deliver {
334   my $file = shift;
335
336   if (!open MESSAGE, "<$file") {
337     print "Unable to open message file $file\n";
338     return;
339   } # if
340
341   my $data;
342   while (<MESSAGE>) {
343     $data = $data . $_;
344   } # while
345
346   Whitelist "Andrew\@DeFaria.com", $data;
347 } # Deliver
348
349 sub ParseCommand {
350   # Crude parser...
351   my $cmd   = shift;
352   my $parm1 = shift;
353   my $parm2 = shift;
354   my $parm3 = shift;
355   my $parm4 = shift;
356
357   $_ = $cmd . " ";
358   SWITCH: {
359     /^$/ && do {
360       last SWITCH
361     };
362
363     /^resequence / && do {
364       Resequence GetContext (), $parm1;
365       last SWITCH
366     };
367
368     /^encrypt / && do {
369       EncryptPassword $parm1, $parm2;
370       last SWITCH
371     };
372
373     /^decrypt / && do {
374       my $password = UserExists (GetContext());
375       DecryptPassword $password;
376       last SWITCH
377     };
378
379     /^deliver / && do {
380       Deliver $parm1;
381       last SWITCH
382     };
383
384     /^add2whitelist / && do {
385       Add2Whitelist $parm1, GetContext (), $parm2;
386       last SWITCH
387     };
388
389     /^showusers / && do {
390       ShowUsers;
391       last SWITCH
392     };
393
394     /^adduser / && do {
395       AddUser $parm1, $parm2, $parm3, $parm4;
396       last SWITCH;
397     };
398
399     /^cleanemail / && do {
400       if ($parm1 eq "") {
401         $parm1 = "9999-12-31 23:59:59";
402       } # if
403       my $nbr_entries = CleanEmail $parm1;
404       print "$nbr_entries email entries cleaned\n";
405       last SWITCH;
406     };
407
408     /^deleteemail / && do {
409       my $nbr_entries = DeleteEmail $parm1;
410       print "$nbr_entries email entries deleted\n";
411       last SWITCH;
412     };
413
414     /^cleanlog / && do {
415       if ($parm1 eq "") {
416         $parm1 = "9999-12-31 23:59:59";
417       } # if
418       my $nbr_entries = CleanLog $parm1;
419       print "$nbr_entries log entries cleaned\n";
420       last SWITCH;
421     };
422
423     /^loadlist / && do {
424       LoadListFile $parm1;
425       last SWITCH;
426     };
427
428     /^loademail / && do {
429       LoadEmail $parm1;
430       last SWITCH;
431     };
432
433     /^dumpemail / && do {
434       DumpEmail $parm1;
435       last SWITCH;
436     };
437
438     /^log / && do {
439       Logmsg "info", "$parm1 $parm2", $parm3;
440       last SWITCH;
441     };
442
443     /^switchuser / && do {
444       SwitchUser $parm1;
445       last SWITCH;
446     };
447
448     /^showuser / && do {
449       ShowUser;
450       last SWITCH;
451     };
452
453     /^showemail / && do {
454       ShowEmail;
455       last SWITCH
456     };
457
458     /^showlog / && do {
459       ShowLog $parm1;
460       last SWITCH
461     };
462
463     /^showlist / && do {
464       ShowList $parm1;
465       last SWITCH
466     };
467
468     /^space / && do {
469       ShowSpace $parm1;
470       last SWITCH
471     };
472
473     /^showstats / && do {
474       ShowStats $parm1;
475       last SWITCH
476     };
477
478     /^help / && 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";
502       last SWITCH;
503     };
504
505     print "Unknown command: $_";
506
507     print " ($parm1" if defined $parm1;
508     print ", $parm2" if defined $parm2;
509     print ", $parm3" if defined $parm3;
510     print ", $parm4" if defined $parm4;
511
512     if (defined $parm1) {
513       print ")\n";
514     } else {
515       print "\n";
516     } # if
517   } # SWITCH
518 } # ParseCommand
519
520 sub GetOpts {
521 } # GetOpts
522
523 my $maps_username = $ENV{MAPS_USERNAME} ? $ENV{MAPS_USERNAME} : $ENV{USER};
524 my $username = Login2MAPS $maps_username, $ENV{MAPS_PASSWORD};
525
526 if (defined $ARGV [0]) {
527   ParseCommand $ARGV [0], $ARGV [1], $ARGV [2], $ARGV [3];
528   exit;
529 } # if
530
531 # Use ReadLine
532 my $term = new Term::ReadLine 'mapsutil';
533
534 while (1) {
535   $_ = $term->readline ("MAPSUtil:");
536
537   last if !defined $_;
538
539   my ($cmd, $parm1, $parm2, $parm3, $parm4) = split;
540
541   last if ($cmd =~ /exit/i || $cmd =~ /quit/i);
542
543   ParseCommand $cmd, $parm1, $parm2, $parm3, $parm4 if defined $cmd;
544 } # while
545
546 print "\n" if !defined $_;
547
548 exit;