Large MAPS update
[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 %opts = (
36   usage => sub { pod2usage },
37   help  => sub { pod2usage (-verbose => 2)},
38   type  => param('type'),
39   file  => param('file'),
40 );
41
42 sub importList ($) {
43   my ($type) = @_;
44
45   my $count = 0;
46
47   open my $file, '<', $opts{file}
48     or die "Unable to open $opts{file} - $!\n";
49
50   while (<$file>) {
51     next if /^\s*#/;
52
53     chomp;
54
55     my ($pattern, $comment, $hit_count, $last_hit) = split /,/;
56
57     my $alreadyExists;
58
59     if ($type eq 'white') {
60       ($alreadyExists) = OnWhitelist($pattern, $userid);
61     } elsif ($type eq 'black') {
62       ($alreadyExists) = OnBlacklist($pattern, $userid);
63     } elsif ($type eq 'null') {
64       ($alreadyExists) = OnNulllist($pattern, $userid);
65     } # if
66
67     unless ($alreadyExists) {
68       AddList($type, $pattern, 0, $comment, $hit_count, $last_hit);
69
70       $count++;
71     } else {
72       print br "$pattern is already on your " . ucfirst($type) . 'list';
73     } # unless
74   } # while
75
76   close $file;
77
78   return $count;
79 } # importList
80
81 # Main
82 GetOptions(
83   \%opts,
84   'usage',
85   'help',
86   'verbose',
87   'debug',
88   'file=s',
89   'type=s',
90 );
91
92 pod2usage "Type not specified" unless $opts{type};
93 pod2usage '-file should be specified' unless $opts{file};
94 pod2usage "Unable to read $opts{file}" unless -r $opts{file};
95
96 $userid = Heading(
97   'getcookie',
98   '',
99   'Import List',
100   'Import List',
101 );
102
103 SetContext($userid);
104
105 NavigationBar($userid);
106
107 my $count = importList($opts{type});
108
109 if ($count == 1) {
110   print br "$count list entry imported";
111 } elsif ($count == 0) {
112   print br 'No entries imported';
113 } else {
114   print br "$count list entries imported";
115 } # if
116
117 exit;