e8cd9e2b34e2dad626ea140b3bdd8a46fdf967d1
[clearscm.git] / maps / bin / add2whitelist.cgi
1 #!/usr/bin/perl
2 ################################################################################
3 #
4 # File:         $RCSfile: add2whitelist.cgi,v $
5 # Revision:     $Revision: 1.1 $
6 # Description:  Add an email address to the whitlist
7 # Author:       Andrew@DeFaria.com
8 # Created:      Mon Jan 16 20:25:32 PST 2006
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 local $0 = $FindBin::Script;
21
22 use lib "$FindBin::Bin/../lib";
23 use lib "$FindBin::Bin/../../lib";
24
25 use Utils;
26
27 use MAPS;
28 use MAPSLog;
29 use MAPSWeb;
30
31 use CGI qw/:standard *table/;
32 use CGI::Carp 'fatalsToBrowser';
33
34 sub Add2List(%) {
35   my (%rec) = @_;
36
37   CheckParms(['userid', 'type'], \%rec);
38
39   my $nextseq = GetNextSequenceNo(%rec);
40
41   my $Userid = ucfirst $rec{userid};
42
43   while () {
44     $rec{pattern}   = param "pattern$nextseq";
45     $rec{domain}    = param "domain$nextseq";
46     $rec{comment}   = param "comment$nextseq";
47     $rec{hit_count} = param "hit_count$nextseq";
48     $rec{retention} = param "retention$nextseq";
49
50     last unless $rec{pattern} or $rec{domain};
51
52     $rec{sender} = CheckEmail $rec{pattern}, $rec{domain};
53
54     my ($status, $rule) = OnWhitelist($rec{sender}, $rec{userid});
55
56     if ($status) {
57       print br {-class => 'error'},
58         "The email address $rec{sender} is already on ${Userid}'s $rec{type} list";
59     } else {
60       my ($messages, $msg) = Add2Whitelist(%rec);
61
62       if ($messages < -1) {
63         print br {-class => 'error'}, "Unable to add $rec{sender} to $rec{type} list\n$msg";
64         return;
65       } else {
66         print br "The email address, $rec{sender}, has been added to ${Userid}'s $rec{type} list";
67       } # if
68
69       if ($messages > 0) {
70         if ($messages == 1) {
71           print br 'Your previous message has been delivered';
72         } else {
73           print br "Your previous $messages messages have been delivered";
74         } # if
75       } elsif ($messages == 0) {
76         print br 'Unable to find any old messages but future messages will now be delivered.';
77       } elsif ($messages < 0) {
78         print br {-class => 'error'}, $msg;
79
80         return;
81       } # if
82
83       # Now remove this entry from the other lists (if present)
84       for my $otherlist ('black', 'null') {
85         FindList(
86           userid => $rec{userid},
87           type   => $otherlist,
88           sender => $rec{sender},
89         );
90
91         my $seq = GetList;
92
93         if ($seq->{sequence}) {
94           my $err;
95
96           ($err, $msg) = DeleteList(
97             userid   => $rec{userid},
98             type     => $otherlist,
99             sequence => $seq->{sequence},
100           );
101
102           croak $msg if $err < 0;
103
104           print br "Removed $rec{sender} from ${Userid}'s " . ucfirst $otherlist . ' list'
105             if $err > 0;
106
107           ResequenceList(
108             userid => $rec{userid},
109             type   => $otherlist,
110           );
111         } # if
112       } # for
113     } # if
114
115     $nextseq++;
116   } # while
117
118   return;
119 } # Add2List
120
121 # Main
122 my $userid = Heading(
123   'getcookie',
124   '',
125   'Add to White List',
126   'Add to White List',
127 );
128
129 $userid ||= $ENV{USER};
130
131 SetContext($userid);
132
133 NavigationBar($userid);
134
135 my $type = 'white';
136
137 Add2List(
138   userid => $userid,
139   type   => $type,
140 );
141
142 print start_form {
143   -method => 'post',
144   -action => 'processaction.cgi',
145   -name   => 'list'
146 };
147
148 print '<p></p><center>',
149   hidden ({-name    => 'type',
150            -default => $type}),
151   submit ({-name    => 'action',
152            -value   => 'Add'}),
153   '</center>';
154
155 Footing;
156
157 exit;