#!/bin/sh -- # A comment mentioning perl, indented for bash's sake. eval 'exec perl -S $0 ${1+"$@"}' if 0; chop($Program = `basename $0`); $Url_Cmd = 'url %s'; system("sh -c 'type netscape_remote' >/dev/null 2>&1"); if ($? == 0) { $Netscape_Remote = 'netscape_remote'; } else { $Netscape_Remote = 'netscape'; } $Encode_Cmd = 'urlencode -as %s'; $Pab_File = "$ENV{HOME}/.netscape/pab.na2"; $Pab = ''; $Sleep = 5; $output_mode = ''; select(STDERR); $| = 1; select(STDOUT); $| = 1; $Usage = <<"END"; $Program: parse a stream of vcards. Usage: $Program Options: -d debug People: -p, -people output mode is for people(1) Netscape Personal address book junk: -pab input to netscape personal addressbook. is $Pab_File by default. This file is only read to avoid dups. -s Sleep n secs between pab-s. 0 for html. -pab_id X window id -pab_html Output HTML of addbook:add HREFs -pab_import netscape pab import file type. (N/A) Notes: for people -> vcard use: people -v -vcard | ... rm -f ~/.netscape/pab.na2 xwininfo # click and get people -v -vcard | vcard -pab_id since netscape only uses the first email address, a hack would be: people -v -vcard | grep -v 'EMAIL;INTERNET;' | vcard -pab_id END LOOP: while (@ARGV) { $_ = shift; CASE: { /^-d$/ && ($Debug = 1, last CASE); /^(-p|-people)$/ && ($output_mode = 'people', last CASE); /^-pab$/ && ($output_mode = 'pab', $Pab = 'pab', $Pab_File = shift, last CASE); /^-pab_id$/ && ($output_mode = 'pab', $Pab = 'pab_id', $Id = shift, $Sleep = 1, last CASE); /^-pab_html$/ && ($output_mode = 'pab', $Pab = 'pab_html', last CASE); /^-pab_import$/ && ($output_mode = 'pab_import', last CASE); /^-s$/ && ($Sleep = shift, last CASE); /^--$/ && (last LOOP); # -- means end of switches /^-(-.*)$/ && (unshift(@ARGV, $1), last CASE); /^(-h|-help)$/ && ((print STDERR $Usage), exit 0, last CASE); if ( /^-(..+)$/ ) { # split bundled switches: local($y, $x) = ($1, ''); foreach $x (reverse(split(//, $y))) { unshift(@ARGV,"-$x") }; last CASE; } /^-/ && ((print STDERR "Invalid arg: $_\n$Usage"), exit 1, last CASE); unshift(@ARGV,$_); last LOOP; } } print STDERR "Pab: $Pab\n" if $Debug; $card = ''; while (<>) { next if /^\s*$/; if ( /BEGIN:VCARD/i ) { if ( $card ne '' ) { print STDERR "%%%%%%%%%%%%\n$card%%%%%%%%%%%%\n" if $Debug; if ( $card =~ /BEGIN:VCARD/i ) { if ( $card !~ /END:VCARD/i ) { print STDERR "*** VCARD ERROR:\n$card\n"; } else { $o = &vcard_parse($card); &print_vcard($o); } $card = ''; } } } $card .= $_; } $o = &vcard_parse($card); &print_vcard($o); ######################################################################3 sub vcard_parse { my($input) = @_; my $key = ''; my $val = ''; my %obj; my (@keys, %vals); $obj{'input'} = $input; foreach $line (split(/\n/, $input)) { $line =~ s/\r$//; next if $line eq ''; if ( $line =~ /^\s+/ ) { $vals{$key} .= ' ' . $line; } elsif ( $line =~ /^([^\s:]+):/ ) { $key = $1; $val = $'; $key =~ y/a-z/A-Z/; $vals{$key} = $val; push(@keys, $key); } } my(%saw); my($name, $props); foreach $key (@keys) { next if $saw{$key}; if ( $Debug ) { print "KEY=$key\n"; print "VAL=$vals{$key}\n"; } $saw{$key}++; ($name, $params) = split(/;/, $key, 2); $params = 'default' unless $params ne ''; $obj{$name}{$params} = $vals{$key}; } return \%obj; } sub print_vcard { my($o) = @_; print STDERR "print_vcard: o: $o\n" if $Debug; if ( $output_mode eq 'people' ) { &print_people($o); } elsif ( $output_mode eq 'pab' ) { &print_pab($o); } elsif ( $output_mode eq 'pab_import' ) { &print_pab_db($o); } else { &print_default($o); } } sub print_default { my($o) = @_; print "======================\n"; print "o: $o\n"; foreach $k (keys %{$o} ) { print "k: $k\n"; foreach $p (keys %{$o->{$k}} ) { print " p: $p\n"; print " v: $o->{$k}{$p}\n"; } } print "\n"; } sub print_pab { my($o) = @_; print "PAB: o: $o\n" if $Debug; $Pab_Count++; if ( $Sleep == 0 && $Pab_Count == 1 ) { print "Click the following to add to personal address book:
\n"; } my $tmp = "/tmp/${Program}.$$.$Pab_Count.vcf"; my $pab_file = $Pab_File; # if ( $Pab =~ /^pab:/ ) { # if ( "$'" ne '' && -f "$'" ) { # $pab_file = $'; # } # } if ( ! %Pab_Strings && -f $pab_file ) { open("PAB", "strings -a $pab_file|") || die "$!"; print STDERR "PAB_FILE: $pab_file\n"; my $l; while ($l = ) { chop($l); next if $l eq ''; $Pab_Strings{$l} = 1; } close(PAB); } my $fn = $o->{'FN'}{'default'}; printf STDERR "==============\nFN: $fn\n"; if ( $fn !~ /^\s*$/ ) { my $skip = 0; if ( $Pab_Strings{$fn} ) { $skip = 1; } else { my $key; foreach $key (keys(%Pab_Strings)) { $skip = 1 if $key =~ /$fn/; } } if ( $skip ) { print STDERR "PAB_SKIP_DUP: $fn\n"; return; } } open("VCF", ">$tmp"); print VCF $o->{input} . "\n"; close(VCF); # print STDERR "\n$o->{input}"; if ( ! $Sleep ) { $Sleep = 1; } if ( $Pab eq 'pab_id' ) { my $cmd = $Encode_Cmd; $cmd =~ s/%s/$tmp/g; my $str; $str = `$cmd`; system("$Netscape_Remote -id $Id -remote 'openURL(addbook:add?vcard=$str)'"); sleep $Sleep; } elsif ( $Pab eq 'pab_html' ) { my $cmd = $Encode_Cmd; $cmd =~ s/%s/$tmp/g; my $str; $str = `$cmd`; print STDOUT " $fn
\n"; } else { my $cmd = $Url_Cmd; $cmd =~ s/%s/$tmp/g; print STDERR "$cmd\n"; system($cmd); sleep $Sleep; } unlink $tmp; } sub print_pab_import { my($o) = @_; my $out = ''; my $fn = &trim($o->{FN}{default}); if ( $fn eq '' ) { my($n3, $n1, $n2) = split(/\s*;\s*/, $o->{N}{default}); $fn = "$n1 $n2 $n3"; $fn =~ s/\s+/ /g; $fn = &trim($fn); } if ( $fn eq '' ) { print STDERR "ERROR: $o->{'input'}\n"; } # dh: # locality: # givenname: # sn: # st: # description: # streetaddress: # postalcode: # countryname: # telephonenumber: # facsimiletelephonenumber: # homeurl: # cellphone: # xmozillaanyphone: # mail: # modifytimestamp: # pager: $out = "dn: cn=$fn;\n"; $out .= "cn: $fn;\n"; $out .= "xmozillausehtmlmail: FALSE\n"; $out .= "xmozillauseconferenceserver: 0\n"; $out .= "objectclass: top\n"; $out .= "objectclass: person\n"; my ($k, $tel); foreach $k (keys %{$o->{'TEL'}}) { $tel = $o->{'TEL'}{$k}; $tel =~ s/[(\[]//g; $tel =~ s/[)\\]]/-/g; $tel =~ s/\s+/-/g; $tel =~ s/^-+//g; $tel =~ s/-+$//g; $tel =~ s/-+/-/g; $k2 = ''; $k2 = ":$k" if $k ne '' && $k ne 'default'; $k2 =~ s/[;=]/,/g; if ( $k =~ /fax/i ) { $k2 =~ s/fax;//ig; $k2 =~ s/fax//ig; $out .= "\tFAX$k2=$tel;\n"; } else { $out .= "\tTEL$k2=$tel;\n"; } } my($poa, $ea, $str, $loc, $reg, $code, $cn); my ($val, $k2); foreach $k (keys %{$o->{'ADR'}}) { $val = $o->{'ADR'}{$k}; ($poa, $ea, $str, $loc, $reg, $code, $cn) = split(/\s*;\s*/, $val); $k2 = ''; $k2 = ":$k" if $k ne '' && $k ne 'default'; $k2 =~ s/[;=]/,/g; $out .= "\tADR$k2=$poa, $ea, $str, $loc, $reg, $code, $cn;\n"; } foreach my $type (qw(EMAIL URL BDAY SSN NOTE)) { foreach $k (keys %{$o->{$type}}) { $val = $o->{$type}{$k}; $k2 = ''; $k2 = ":$k" if $k ne '' && $k ne 'default'; $k2 =~ s/[;=]/,/g; $out .= "\t$type$k2=$val;\n"; } } print STDOUT "$out\n"; } sub print_people { my($o) = @_; my $out = ''; my $fn = &trim($o->{FN}{default}); if ( $fn eq '' ) { my($n3, $n1, $n2) = split(/\s*;\s*/, $o->{N}{default}); $fn = "$n1 $n2 $n3"; $fn =~ s/\s+/ /g; $fn = &trim($fn); } if ( $fn eq '' ) { print STDERR "ERROR: $o->{'input'}\n"; } $out = ":$fn;\n"; my ($k, $tel); foreach $k (keys %{$o->{'TEL'}}) { $tel = $o->{'TEL'}{$k}; $tel =~ s/[(\[]//g; $tel =~ s/[)\\]]/-/g; $tel =~ s/\s+/-/g; $tel =~ s/^-+//g; $tel =~ s/-+$//g; $tel =~ s/-+/-/g; $k2 = ''; $k2 = ":$k" if $k ne '' && $k ne 'default'; $k2 =~ s/[;=]/,/g; if ( $k =~ /fax/i ) { $k2 =~ s/fax;//ig; $k2 =~ s/fax//ig; $out .= "\tFAX$k2=$tel;\n"; } else { $out .= "\tTEL$k2=$tel;\n"; } } my($poa, $ea, $str, $loc, $reg, $code, $cn); my ($val, $k2); foreach $k (keys %{$o->{'ADR'}}) { $val = $o->{'ADR'}{$k}; ($poa, $ea, $str, $loc, $reg, $code, $cn) = split(/\s*;\s*/, $val); $k2 = ''; $k2 = ":$k" if $k ne '' && $k ne 'default'; $k2 =~ s/[;=]/,/g; $out .= "\tADR$k2=$poa, $ea, $str, $loc, $reg, $code, $cn;\n"; } foreach my $type (qw(EMAIL URL BDAY SSN NOTE)) { foreach $k (keys %{$o->{$type}}) { $val = $o->{$type}{$k}; $k2 = ''; $k2 = ":$k" if $k ne '' && $k ne 'default'; $k2 =~ s/[;=]/,/g; $out .= "\t$type$k2=$val;\n"; } } print STDOUT "$out\n"; } ###################################################### sub trim { local($x) = @_; $x =~ s/^\s*//; $x =~ s/\s*$//; return $x; } sub basename { # Like basename(1), returns basename of a path. local($x) = @_; $x =~ s,/+$,,; # remove trailing /'s if ( $x =~ m,/([^/]+)$, ) { # check if matches / $x = $1; # grab stuff after the last / } $x = '.' if $x eq ''; # evidently input was null. return $x; } sub dirname { # Like dirname(1), returns dirname of a path. local($x) = @_; $x =~ s,/+$,,; # remove trailing /'s $x =~ s,/+,/,g; # change multiple /'s to single / if ( $x =~ m,/([^/]+)$, ) { # check if matches / $x = $`; # grab stuff before / $x = '/' if $x eq ''; # is root if null. } else { # it was a bare word without / $x = '.'; } $x = '.' if $x eq ''; # never return null string return $x; } __END__ notes: This is a multi-part message in MIME format. --------------236DDBF8437996A1FCD12B5B Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Hi, I am trying to have page where the function addbook shall be called (to open the addressbook). Can anyone tell me why addbook works as a href like in the first example and why I can not use it like in the second example in combination with JavaScript ? The reason why I need to have the addbook in JavaScript is because I would like to build the string myself according to input fields. any help is appreciated thanks in advance Johannes PS by the way, I am using communicator 4.61 addbook working in a pure html page ***************************************************************************** test_fax_svi  

 Add Mr.Test to addressbook (Fax) ********************************************************************************* addbook in JavaScript (not working) ********************************************************************************* (Type a title for your page here)

********************************************************************************* --------------236DDBF8437996A1FCD12B5B Content-Type: text/html; charset=us-ascii; name="pure_html_addbook.html" Content-Transfer-Encoding: 7bit Content-Disposition: inline; filename="pure_html_addbook.html" test_fax_svi  

 Add Mr.Test to addressbook (Fax) --------------236DDBF8437996A1FCD12B5B Content-Type: text/html; charset=us-ascii; name="js_addbook.html" Content-Transfer-Encoding: 7bit Content-Disposition: inline; filename="js_addbook.html" (Type a title for your page here)

--------------236DDBF8437996A1FCD12B5B Content-Type: text/x-vcard; charset=us-ascii; name="johannes.vcf" Content-Transfer-Encoding: 7bit Content-Description: Card for Johannes Ditterich Content-Disposition: attachment; filename="johannes.vcf" begin:vcard n:Ditterich;Johannes tel;fax:+49 - 811 - 5537 - 114 tel;work:+49 - 811 - 5537 - 256 x-mozilla-html:TRUE org:Netscape Communications;World Wide Professional Services version:2.1 email;internet:johannes@netscape.com title:Consultant adr;quoted-printable:;;Am Soeldnermoos 6=0D=0A;Munich - Hallbergmoos;Bavaria;D-85399;Germany x-mozilla-cpt:;-1 fn:Johannes Ditterich end:vcard --------------236DDBF8437996A1FCD12B5B--