adding export to read mailbox status information, RT#15987
[freeside.git] / bin / svc_acct_pop.import
1 #!/usr/bin/perl
2
3 use strict;
4 use Text::CSV_XS;
5 use FS::UID qw(adminsuidsetup);
6 use FS::svc_acct_pop;
7
8 my @fields = qw( ac loc state city exch );
9 my $fixup = sub {
10                   my $hash = shift;
11                   $hash->{ac} =~ /^\s*(\d{3})\s*$/;
12                   $hash->{ac} = $1;
13                   $hash->{loc} =~ /^\s*(\d{3})(\d{4})\s*$/;
14                   $hash->{exch} = $1;
15                   $hash->{loc} = $2;
16                   $hash->{state} =~ /^\s*(\S{0,2})\s*$/;
17                   $hash->{state} = $1;
18                   $hash->{city} =~ /^\s*(.*?)\s*$/;
19                   $hash->{city} = $1;
20
21                 };
22
23 my $user = shift or usage();
24 adminsuidsetup $user;
25
26 my $file = shift or usage();
27 my $csv = new Text::CSV_XS;
28
29 open(FH, $file) or die "cannot open $file: $!";
30
31 sub usage {
32   die "Usage:\n\n  svc_acct_pop.import user popfile.csv\n\n";
33 }
34
35 ###
36
37 my $line;
38 while ( defined($line=<FH>) ) {
39   chomp $line;
40
41   $line &= "\177" x length($line); # i hope this isn't really necessary
42   $csv->parse($line)
43     or die "cannot parse: " . $csv->error_input();
44
45   my @values = $csv->fields();
46   my %hash;
47   foreach my $field (@fields) {
48     $hash{$field} = shift @values;
49   }
50     
51   &{$fixup}(\%hash);
52
53   my $svc_acct_pop = new FS::svc_acct_pop { %hash };
54
55   #my $error = $svc_acct_pop->check;
56   my $error = $svc_acct_pop->insert;
57   die $error if $error;
58
59 }