Bug fixes for mapsscrub.pl
[clearscm.git] / maps / bin / mapsscrub.pl
1 #!/usr/bin/perl
2
3 =pod
4
5 =head1 NAME $RCSfile: mapsscrub,v $
6
7 This script scrubs messages from the MAPS database based on the users settings
8
9 =head1 VERSION
10
11 =over
12
13 =item Author
14
15 Andrew DeFaria <Andrew@DeFaria.com>
16
17 $Revision: 1.1 $
18
19 =item Created:
20
21 Fri Nov 29 14:17:21  2002
22
23 =item Modified:
24
25 $Date: 2013/06/12 14:05:47 $
26
27 =back
28
29 =head1 SYNOPSIS
30
31  Usage: mapsscrub.pl [-usa|ge] [-h|elp] [-v|erbose] [-de|bug]
32                      [-n|optimize]
33
34   Where:
35    -usa|ge       Print this usage
36    -h|elp        Detailed help
37    -v|erbose     Verbose mode (Default: Not verbose)
38    -de|bug       Turn on debugging (Default: Off)
39
40    -user|id      User ID to scrub (Default: All users)
41    -n\oopitmize  Whether or not to optimize DB (Default: optimize)
42
43 =cut
44
45 use strict;
46 use warnings;
47
48 use FindBin;
49
50 use lib "$FindBin::Bin/../lib";
51 use lib "$FindBin::Bin/../../lib";
52
53 use Getopt::Long;
54 use Pod::Usage;
55
56 use DateUtils;
57 use Logger;
58 use MAPS;
59 use Utils;
60
61 my %opts = (
62   usage    => sub { pod2usage },
63   help     => sub { pod2usage (-verbose => 2)},
64   optimize => 1,
65 );
66
67 my ($log, %total);
68
69 sub CleanUp($) {
70   my ($userid) = @_;
71
72   my %options = GetUserOptions($userid);
73
74   my $timestamp = SubtractDays(Today2SQLDatetime, $options{History});
75
76   $total{'Emails cleaned'}      = CleanEmail $timestamp, $opts{dryrun};
77   $total{'Log entries removed'} = CleanLog   $timestamp, $opts{dryrun};
78
79   for (qw(white black null)) {
80     $total{"${_}list entries removed"} = CleanList(
81       userid => $userid,
82       type   => $_,
83       log    => $log,
84       dryrun => $opts{dryrun},
85     );
86   } # for
87
88   Stats \%total, $log;
89
90   return;
91 } # CleanUp
92
93 # Main
94 GetOptions(
95   \%opts,
96   'usage',
97   'help',
98   'verbose',
99   'debug',
100   'userid=s',
101   'optimize!',
102   'dryrun',
103 ) or pod2usage;
104
105 $log = Logger->new(
106   path        => '/var/local/log',
107   timestamped => 'yes',
108 );
109
110 FindUser(%opts{userid});
111
112 while (my $rec = GetUser) {
113   SetContext($rec->{userid});
114
115   CleanUp($rec->{userid});
116 } # while
117
118 # Now optimize the database
119 OptimizeDB if $opts{optimize};
120
121 exit;