Fixed importlist.cgi
[clearscm.git] / maps / bin / importlist.cgi
1 #!/usr/bin/perl
2 ################################################################################
3 #
4 # File:         $RCSfile: importlist.cgi,v $
5 # Revision:     $Revision: 1.1 $
6 # Description:  Export an address list
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
17 use FindBin;
18 local $0 = $FindBin::Script;
19
20 use lib "$FindBin::Bin/../lib";
21
22 use Getopt::Long;
23 use Pod::Usage;
24
25 use MAPS;
26 use MAPSWeb;
27
28 use CGI qw/:standard *table/;
29 use CGI::Carp "fatalsToBrowser";
30
31 my $userid =   cookie('MAPSUser');
32    $userid //= $ENV{USER};
33 my $Userid =   ucfirst $userid;
34
35 my $type = param 'type';
36 my $file = param 'file';
37
38 my %opts = (
39   usage => sub { pod2usage },
40   help  => sub { pod2usage (-verbose => 2)},
41   type  => $type,
42   file  => $file,
43 );
44
45 sub importList ($) {
46   my ($type) = @_;
47
48   my $count = 0;
49
50   open my $file, '<', $opts{file}
51     or die "Unable to open $opts{file} - $!\n";
52
53   while (<$file>) {
54     next if /^\s*#/;
55
56     chomp;
57
58     my ($pattern, $comment, $hit_count, $last_hit) = split /,/;
59
60     my $alreadyExists;
61
62     if ($type eq 'white') {
63       ($alreadyExists) = OnWhitelist($pattern, $userid);
64     } elsif ($type eq 'black') {
65       ($alreadyExists) = OnBlacklist($pattern, $userid);
66     } elsif ($type eq 'null') {
67       ($alreadyExists) = OnNulllist($pattern, $userid);
68     } # if
69
70     unless ($alreadyExists) {
71       AddList($type, $pattern, 0, $comment, $hit_count, $last_hit);
72
73       $count++;
74     } else {
75       print br "$pattern is already on your " . ucfirst($type) . 'list';
76     } # unless
77   } # while
78
79   close $file;
80
81   return $count;
82 } # importList
83
84 # Main
85 GetOptions(
86   \%opts,
87   'usage',
88   'help',
89   'verbose',
90   'debug',
91   'file=s',
92   'type=s',
93 );
94
95 pod2usage "Type not specified" unless $opts{type};
96 pod2usage '-file should be specified' unless $opts{file};
97 pod2usage "Unable to read $opts{file}" unless -r $opts{file};
98
99 $userid = Heading(
100   'getcookie',
101   '',
102   'Import List',
103   'Import List',
104 );
105
106 SetContext($userid);
107
108 NavigationBar($userid);
109
110 my $count = importList($opts{type});
111
112 if ($count == 1) {
113   print br "$count list entry imported";
114 } elsif ($count == 0) {
115   print br 'No entries imported';
116 } else {
117   print br "$count list entries imported";
118 } # if
119
120 exit;