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