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