Fixed bug with counting messages
[clearscm.git] / maps / bin / processaction.cgi
1 #!/usr/bin/perl
2 #################################################################################
3 #
4 # File:         $RCSfile: processaction.cgi,v $
5 # Revision:     $Revision: 1.1 $
6 # Description:  Process the action
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 use lib "$FindBin::Bin/../lib";
21 use lib "$FindBin::Bin/../../lib";
22
23 use MAPS;
24 use MAPSWeb;
25
26 use CGI qw (:standard *table start_Tr end_Tr);
27 use CGI::Carp 'fatalsToBrowser';
28
29 my $type   = param 'type';
30 my $action = param 'action';
31 my $next   = param 'next';
32 my $userid = cookie 'MAPSUser';
33 my $lines;
34 my $total;
35 my $table_name = 'list';
36
37 my @scripts = ('ListActions.js');
38
39 sub ReturnSequenceNbrs {
40   my @names = param;
41   my @sequence_nbrs;
42
43   for (@names) {
44     if (/action(\d+)/) {
45       push @sequence_nbrs, $1;
46     } # if
47   } # for
48
49   return @sequence_nbrs;
50 } # ReturnSequenceNbrs
51
52 sub DeleteEntries {
53   my ($type) = @_;
54
55   my @sequence_nbrs = ReturnSequenceNbrs;
56
57   my $count;
58
59   for (@sequence_nbrs) {
60     my ($err, $msg) = DeleteList(
61       userid   => $userid,
62       type     => $type,
63       sequence => $_, 
64     );
65
66     # How to best handle error?
67     croak $msg if $err < 0;
68
69     $count += $err;
70   } # for
71
72   if ($count == 0) {
73     DisplayError('Nothing to delete!');
74   } else {
75     ResequenceList(
76       userid => $userid,
77       type   => $type
78     );
79
80     if ($count == 1) {
81       print redirect ("/maps/php/list.php?type=$type&next=$next&message=Deleted entry");
82     } else {
83       print redirect ("/maps/php/list.php?type=$type&next=$next&message=Deleted $count entries");
84     } # if
85   } # if
86
87   return $count;
88 } # DeleteEntries
89
90 sub PrintInputLine ($$$$$) {
91   my ($nextseq, $email_nbr, $leftclass, $dataclass, $rightclass) = @_;
92
93   my $email     = '';
94   my $pattern   = '';
95   my $domain    = '';
96   my $retention = '';
97   my $hit_count = '';
98
99   if (defined $email_nbr && $email_nbr ne '') {
100     $email = param "email$email_nbr";
101     if ($email && $email ne '') {
102       ($pattern, $domain) = split /\@/, $email;
103     } # if
104
105     $hit_count = CountEmail(
106       userid => $userid,
107       additional => "sender like '%$email%'",
108     );
109   } # if
110
111   print Tr [
112     td {-class      => $leftclass,
113         -align      => 'center'}, "$nextseq",
114     td {-class      => $dataclass,
115         -align      => 'right'},
116       (textfield {-class     => 'inputfieldright',
117                   -style     => 'width:100%',
118                   -size      => 25,
119                   -maxlength => '255',
120                   -name      => "pattern$nextseq",
121                   -value     => $pattern}),
122     td {-class      => $dataclass,
123         -align      => 'center'}, '@',
124     td {-class      => $dataclass},
125       (textfield {-class      => 'inputfield',
126                   -style      => 'width:100%',
127                   -size       => 25,
128                   -maxlength  => '255',
129                   -name       => "domain$nextseq",
130                   -value      => $domain}),
131     td {-class      => $dataclass},
132       (textfield {-class     => 'inputfieldright',
133                   -style     => 'width:100%',
134                   -size      => 25,
135                   -maxlength => '255',
136                   -name      => "hit_count$nextseq",
137                   -value     => $hit_count}),
138     td {-class      => $dataclass},
139       (textfield {-class      => 'inputfieldright',
140                   -style      => 'width:100%',
141                   -size       => 20,
142                   -maxlength  => '40',
143                   -name       => "retention$nextseq",
144                   -value      => $retention}),
145     td {-class      => $rightclass},
146       (textfield {-class     => 'inputfield',
147                   -style     => 'width:100%',
148                   -size      => 25,
149                   -maxlength => '255',
150                   -name      => "comment$nextseq",
151                   -value     => ''}),
152   ];
153
154   return;
155 } # PrintInputLine
156
157 sub AddNewEntry {
158   my ($type, @selected)  = @_;
159
160   # First display the last page and add the appropriate number of
161   # empty, editable entries (possibly filled in) for the user to add
162   # the new entry
163   my $selected = @selected;
164   my $nextseq = GetNextSequenceNo(
165     userid => $userid,
166     type   => $type,
167   );
168
169   my $next = ($nextseq - $lines) + $selected - 1;
170
171   $next = 0 if $next < 0;
172
173   my $Type = ucfirst $type;
174
175   Heading(
176     'getcookie',
177     '',
178     "Add to $Type List",
179     "Add to $Type List",
180     '',
181     $table_name,
182     @scripts
183   );
184
185   NavigationBar($userid);
186
187   # Now display table and new entry
188   print start_form {
189     -method => 'post',
190     -action => "add2${type}list.cgi",
191     -name   => 'list'
192   };
193
194   print start_table {-align       => 'center',
195                      -id          => $table_name,
196                      -border      => 0,
197                      -cellspacing => 0,
198                      -cellpadding => 4,
199                      -width       => '100%'};
200   print Tr [
201     th {-class => 'tableleftend'},  'Seq',
202     th {-class => 'tableheader'},   'Username',
203     th {-class => 'tableheader'},   '@',
204     th {-class => 'tableheader'},   'Domain',
205     th {-class => 'tableheader'},   'Hit Count',
206     th {-class => 'tableheader'},   'Retention',
207     th {-class => 'tablerightend'}, 'Comments',
208   ];
209
210   my $list = ReturnList(
211     userid   => $userid,
212     type     => $type,
213     start_at => $next,
214     lines    => $lines,
215   );
216
217   my $i = 1;
218
219   for my $record (@$list) {
220     $i++;
221
222     # Normalize fields
223     $record->{pattern}   //= '&nbsp;';
224     $record->{domain}    //= '&nbsp;';
225     $record->{comment}   //= '&nbsp;';
226     $record->{hit_count} //= '&nbsp;';
227     $record->{retention} //= '&nbsp;';
228
229     print Tr [
230       td {-class  => 'tableleftdata',  -align  => 'center'}, $record->{sequence},
231       td {-class  => 'tabledata',      -align  => 'right'},  $record->{pattern},
232       td {-class  => 'tabledata',      -align  => 'center'}, '@',
233       td {-class  => 'tabledata',      -align  => 'left'},   $record->{domain},
234       td {-class  => 'tabledata',      -align  => 'right'},  $record->{hit_count},
235       td {-class  => 'tabledata',      -align  => 'right'},  $record->{retention},
236       td {-class  => 'tablerightdata', -align  => 'left'},   $record->{comment},
237     ];
238   } # for
239
240   # Now the input line(s)
241   if (@selected == 0) {
242     PrintInputLine($nextseq, undef, 'tablebottomleft', 'tablebottomdata',
243                                     'tablebottomright');
244   } else {
245     for (@selected) {
246       my $leftclass  = $i == $lines ? 'tablebottomleft'  : 'tableleftdata';
247       my $dataclass  = $i == $lines ? 'tablebottomdata'  : 'tabledata';
248       my $rightclass = $i == $lines ? 'tablebottomright' : 'tablerightdata';
249       $i++;
250       PrintInputLine($nextseq++, $_, $leftclass, $dataclass, $rightclass);
251     } # for
252   } # for
253
254   print end_table;
255   print br,
256     '<center>',
257       submit ({-name    => 'update',
258                -value   => 'Update',
259                -onClick => 'return CheckEntry (document.list);'}),
260       submit ({-name    => 'Reset',
261                -value   => 'Reset',
262                -onClick => 'history.back(); return false'}),
263     '</center>';
264   print end_form;
265
266   return;
267 } # AddNewEntry
268
269 sub ModifyEntries {
270   my ($type) = @_;
271
272   my @selected = ReturnSequenceNbrs;
273
274   my $Type = ucfirst $type;
275
276   Heading(
277     'getcookie',
278     '',
279     "Modify $Type List",
280     "Modify $Type List",
281     '',
282     $table_name,
283     @scripts
284   );
285
286   NavigationBar($userid);
287
288   # Redisplay the page but open up the lines that are getting modified
289   print start_form {
290     -method  => 'post',
291     -action  => 'modifyentries.cgi',
292     -name    => 'list'
293   };
294
295   # Print some hidden fields to pass along
296   print hidden ({-name    => 'type', -default => $type}),
297         hidden ({-name    => 'next', -default => $next});
298
299   print start_table {-align       => 'center',
300                      -id          => $table_name,
301                      -border      => 0,
302                      -cellspacing => 0,
303                      -cellpadding => 4,
304                      -width       => '100%'};
305   print Tr [
306     th {-class => 'tableleftend'},  'Seq',
307     th {-class => 'tableheader'},   'Username',
308     th {-class => 'tableheader'},   '@',
309     th {-class => 'tableheader'},   'Domain',
310     th {-class => 'tableheader'},   'Hit Count',
311     th {-class => 'tableheader'},   'Retention',
312     th {-class => 'tablerightend'}, 'Comments',
313   ];
314
315   my $list = ReturnList(
316     userid   => $userid,
317     type     => $type,
318     start_at => $next,
319     lines    => $lines,
320   );
321
322   my $s = 0;
323   my $i = 1;
324
325   for my $record (@$list) {
326     my $leftclass  = ($i == @$list || $record->{sequence} eq $total) ?
327       'tablebottomleft'  : 'tableleftdata';
328     my $dataclass  = ($i == @$list || $record->{sequence} eq $total) ?
329       'tablebottomdata'  : 'tabledata';
330     my $rightclass = ($i == @$list || $record->{sequence} eq $total) ?
331       'tablebottomright' : 'tablerightdata';
332
333     $i++;
334
335     print start_Tr,
336       td {-class  => $leftclass,
337           -align  => 'center'}, $record->{sequence};
338
339     if ($selected[$s] and $record->{sequence} eq $selected[$s]) {
340       $s++;
341       # Normalize fields
342       $record->{pattern}   //= '';
343       $record->{domain}    //= '';
344       $record->{comment}   //= '';
345       $record->{hit_count} //= '';
346       $record->{retention} //= '';
347
348       print
349         td {-class               => $dataclass,
350             -align               => 'right'},
351           (textfield {-class     => 'inputfieldright',
352                       -style     => 'width:100%',
353                       -align     => 'right',
354                       -size      => 25,
355                       -maxlength => '255',
356                       -name      => "pattern$record->{sequence}",
357                       -value     => $record->{pattern}}),
358         td {-class               => $dataclass,
359             -align               => 'center'}, '@',
360         td {-class               => $dataclass},
361           (textfield {-class     => 'inputfield',
362                       -style     => 'width:100%',
363                       -align     => 'left',
364                       -size      => 25,
365                       -maxlength => '255',
366                       -name      => "domain$record->{sequence}",
367                       -value     => $record->{domain}}),
368         td {-class               => $dataclass},
369           (textfield {-class     => 'inputfieldright',
370                       -style     => 'width:100%',
371                       -align     => 'left',
372                       -size      => 25,
373                       -maxlength => '255',
374                       -name      => "hit_count$record->{sequence}",
375                       -value     => $record->{hit_count}}),
376         td {-class               => $dataclass},
377           (textfield {-class     => 'inputfieldright',
378                       -style     => 'width:100%',
379                       -align     => 'left',
380                       -size      => 25,
381                       -maxlength => '40',
382                       -name      => "retention$record->{sequence}",
383                       -value     => $record->{retention}}),
384         td {-class               => $rightclass},
385           (textfield {-class     => 'inputfield',
386                       -style     => 'width:100%',
387                       -align     => 'left',
388                       -size      => 25,
389                       -maxlength => '255',
390                       -name      => "comment$record->{sequence}",
391                       -value     => $record->{comment}});
392     } else {
393       # Normalize fields
394       # Put in '&nbsp;' for undefined fields
395       $record->{pattern}   //= '&nbsp;';
396       $record->{domain}    //= '&nbsp;';
397       $record->{comment}   //= '&nbsp;';
398       $record->{hit_count} //= '&nbsp;';
399       $record->{retention} //= '&nbsp;';
400
401       print
402         td {-class => $dataclass,
403             -align => 'right'}, $record->{pattern},
404         td {-class => $dataclass,
405             -align => 'center'}, '@',
406         td {-class => $dataclass,
407             -align => 'left'}, $record->{domain},
408         td {-class => $dataclass,
409             -align => 'right'}, $record->{hit_count},
410         td {-class => $dataclass,
411             -align => 'right'}, $record->{retention},
412         td {-class => $rightclass,
413             -align => 'left'}, $record->{comment};
414     } # if
415
416     print end_Tr;
417   } # for
418
419   print end_table;
420   print br,
421     '<center>',
422       submit ({-name    => 'update',
423                -value   => 'Update',
424                -onClick => 'return CheckEntry (document.list);'}),
425       submit ({-name    => 'Reset',
426                -value   => 'Reset',
427                -onClick => 'history.back(); return false'}),
428     '</center>';
429   print end_form;
430
431   return;
432 } # ModifyEntries
433
434 sub WhitelistMarked {
435   AddNewEntry('white', ReturnSequenceNbrs);
436
437   return;
438 } # WhitelistMarked
439
440 sub BlacklistMarked {
441   AddNewEntry('black', ReturnSequenceNbrs);
442
443   return;
444 } # BlacklistMarked
445
446 sub NulllistMarked {
447   AddNewEntry('null', ReturnSequenceNbrs);
448
449   return;
450 } # NulllistMarked
451
452 # Main
453 $userid ||= $ENV{USER};
454
455 SetContext($userid);
456
457 my %options = GetUserOptions($userid);
458
459 $lines = $options{'Page'};
460
461 $total = CountList(
462   userid => $userid,
463   type   => $type,
464 ) if $type;
465
466 if ($action eq 'Add') {
467   AddNewEntry($type);
468 } elsif ($action eq 'Delete') {
469   DeleteEntries($type);
470 } elsif ($action eq 'Modify') {
471   ModifyEntries($type);
472 } elsif ($action eq 'Whitelist') {
473   WhitelistMarked;
474 } elsif ($action eq 'Blacklist') {
475   BlacklistMarked;
476 } elsif ($action eq 'Nulllist') {
477   NulllistMarked;
478 } else {
479   Heading(
480     'getcookie',
481     '',
482     "Unknown action ($action)",
483     "Unknown action ($action)"
484   );
485
486   NavigationBar($userid);
487   DisplayError("Unknown action encountered ($action)");
488 } # if
489
490 Footing($table_name);
491
492 exit;