Merge branch 'master' of /opt/git/clearscm
[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;
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 $type   =   param('type');
32 my $userid =   cookie('MAPSUser');
33    $userid //= $ENV{USER};
34 my $Userid =   ucfirst $userid;
35
36 my %opts = (
37   usage => sub { pod2usage },
38   help  => sub { pod2usage (-verbose => 2)},
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     } # unless
72   } # while
73
74   close $file;
75
76   return $count;
77 } # importList
78
79 # Main
80 GetOptions (
81   \%opts,
82   'usage',
83   'help',
84   'verbose',
85   'debug',
86   'file=s',
87 );
88
89 pod2usage "Type not specified" unless $type;
90 pod2usage '-file should be specified' unless $opts{file};
91 pod2usage "Unable to read $opts{file}" unless -r $opts{file};
92
93 $userid = Heading (
94   'getcookie',
95   '',
96   'Import List',
97   'Import List',
98 );
99
100 SetContext $userid;
101
102 NavigationBar $userid;
103
104 my $count = importList $type;
105
106 if ($count == 1) {
107   print br "$count list entry imported";
108 } elsif ($count == 0) {
109   print br 'No entries imported';
110 } else {
111   print br "$count list entries imported";
112 } # if
113
114 exit;