1#!/usr/local/bin/perl
2# Import addresses from a file
3use strict;
4use warnings;
5our (%text, %in);
6
7require './mailbox-lib.pl';
8&ReadParseMime();
9&error_setup($text{'import_err'});
10
11# Get the data to import
12my $data;
13if ($in{'src'} == 0) {
14	$in{'upload'} || &error($text{'import_eupload'});
15	$data = $in{'upload'};
16	}
17else {
18	$in{'paste'} =~ s/\r//g;
19	$in{'paste'} =~ /\S/ || &error($text{'import_epaste'});
20	$data = $in{'paste'};
21	}
22
23# Parse the data
24my @addrs;
25if ($in{'fmt'} eq 'csv') {
26	# CSV or tab-separated format
27	foreach my $l (split(/\n/, $data)) {
28		next if ($l !~ /\S/);
29		my @row;
30		while($l =~ s/^\s*"([^"]*)",?(.*)/$2/ ||
31		      $l =~ s/^\s*'([^']*)',?(.*)/$2/ ||
32		      $l =~ s/^\s*"([^"]*)"\t?(.*)/$2/ ||
33		      $l =~ s/^\s*'([^']*)'\t?(.*)/$2/ ||
34		      $l =~ s/^\s*([^,]+),?(.*)/$2/ ||
35		      $l =~ s/^\s*([^\t]+)\t?(.*)/$2/) {
36			push(@row, $1);
37			}
38		push(@addrs, [ $row[0], $row[1], int($row[2]) ]);
39		}
40	}
41else {
42	# Vcard
43	eval "use Net::vCard";
44	$@ && &error($text{'import_enetvcard'});
45	my $temp = &transname();
46	open(my $TEMP, ">", "$temp");
47	print $TEMP $data;
48	close($TEMP);
49	my $cards = Net::vCard->loadFile($temp);
50	&unlink_file($temp);
51	foreach my $card (@$cards) {
52		my $n = $card->givenName;
53		$n .= " " if ($n);
54		$n .= $card->familyName;
55		next if (!$n);
56		push(@addrs, [ $card->{'EMAIL'}->{'internet'}, $n, 0 ]);
57		}
58	}
59@addrs || &error($text{'import_enone'});
60
61# Update addresses, and tell user
62&ui_print_header(undef, $text{'import_title'}, "");
63
64print $text{'import_doing'},"<p>\n";
65
66my %old = map { lc($_->[0]), $_ } &list_addresses();
67print &ui_columns_start([ $text{'address_addr'}, $text{'address_name'},
68			  $text{'import_action'} ], 100);
69foreach my $a (@addrs) {
70	my $o = $old{lc($a->[0])};
71	$a->[0] =~ s/^\s+//; $a->[0] =~ s/\s+$//;
72	$a->[1] =~ s/^\s+//; $a->[1] =~ s/\s+$//;
73	if ($a->[0] !~ /^\S+\@\S+$/) {
74		# Invalid email
75		print &ui_columns_row([ &html_escape($a->[0]),
76					&html_escape($a->[1]),
77					$text{'import_noemail'} ]);
78		}
79	elsif (!$o) {
80		# Missing, need to add
81		print &ui_columns_row([ &html_escape($a->[0]),
82					&html_escape($a->[1]),
83					$text{'import_add'} ]);
84		&create_address($a->[0], $a->[1], $a->[2]);
85		}
86	elsif ($in{'dup'}) {
87		if ($o->[1] ne $a->[1] && $o->[2] ne '') {
88			# Exists but is different .. update
89			print &ui_columns_row([ &html_escape($a->[0]),
90						&html_escape($a->[1]),
91						$text{'import_update'} ]);
92			&modify_address($o->[2], $a->[0], $a->[1], $a->[2]);
93			}
94		elsif ($o->[1] ne $a->[1] && $o->[2] eq '') {
95			# Exists but cannot be updated
96			print &ui_columns_row([ &html_escape($a->[0]),
97						&html_escape($a->[1]),
98						$text{'import_readonly'} ]);
99			}
100		else {
101			# Exists and is same
102			print &ui_columns_row([ &html_escape($a->[0]),
103						&html_escape($a->[1]),
104						$text{'import_same'} ]);
105			}
106		}
107	else {
108		# Exists, skip
109		print &ui_columns_row([ &html_escape($a->[0]),
110					&html_escape($a->[1]),
111					$text{'import_skip'} ]);
112		}
113	}
114print &ui_columns_end();
115
116&ui_print_footer("list_addresses.cgi", $text{'address_return'});
117