home *** CD-ROM | disk | FTP | other *** search
/ linuxmafia.com 2016 / linuxmafia.com.tar / linuxmafia.com / pub / palmos / other-os / mpadq.pl < prev    next >
Perl Script  |  2001-02-14  |  3KB  |  115 lines

  1. #!/usr/bin/perl -T -w
  2.  
  3. =head1 NAME
  4.  
  5.   mpadq - mutt (pilot|palmpilot|palm) addressbook query
  6.   From:  http://www.quiet.demon.co.uk/software/mpadq
  7.  
  8. =head1 SYNOPSIS
  9.  
  10.   mpadq [query_string]
  11.  
  12.   In .muttrc: set query_command="mpadq %s"
  13.  
  14. =head1 DESCRIPTION
  15.  
  16.   This is meant to be called using the external query facility
  17.   of the mutt(1) mail user agent, although it could also be useful
  18.   for command line invocation.
  19.  
  20.   It does a case-insensitive regular expression match on the
  21.   first name, last name, company and email fields of all
  22.   addressbook entries, and returns matching results back in
  23.   the format mutt expects, with the company addressbook
  24.   field used in the 'optional information' position.
  25.  
  26.   Records that contain more than one email address produce
  27.   one line of output for each address.
  28.  
  29. =head1 RETURN VALUE
  30.  
  31.   Returns 0 when matching records found, 1 otherwise.
  32.  
  33. =head1 FILES
  34.  
  35.   ${HOME}/.jpilot/AddressDB.pdb - the raw pilot addressbook database.
  36.  
  37.   Change the script to point to wherever yours lives.
  38.  
  39. =head1 SEE ALSO
  40.  
  41.   mutt(1), pilot-link(1), the PDA::Pilot module.
  42.  
  43. =head1 BUGS
  44.  
  45.   Craig Sebenik (craig@netapp.com) reports truncation of email addresses
  46.   longer that about 25 characters under Solaris.  I haven't been able to
  47.   reproduce this on my system (Linux), but if it bites you let me know.
  48.  
  49. =head1 AUTHORS
  50.  
  51.   originally written by Lucas Maneos <lm@quiet.demon.co.uk>
  52.   bug fixes by Craig Sebenik <craig@netapp.com>
  53.  
  54. =cut
  55.  
  56. use strict;
  57. use PDA::Pilot;
  58.  
  59. my $ABFILE="$ENV{'HOME'}/.jpilot/AddressDB.pdb";
  60. my ($ab, $rec, $nr, $i, $addr, $tmp, @list);
  61. my ($fname, $lname, $email, $company, $name);
  62. my $qry=shift||"";
  63.  
  64. if ( ! -r $ABFILE ) {
  65.     if ( ! -e $ABFILE ) {
  66.         die "File \"$ABFILE\" does not exist.\n"
  67.     }
  68.     die "Cannot read \"$ABFILE\".\n"
  69. }
  70.  
  71. $ab=PDA::Pilot::File::open($ABFILE) || die("Can't open $ABFILE: $!\n");
  72. $nr=$ab->getRecords();
  73.  
  74. print "Searching database for '$qry' ... $nr entries ... ";
  75.  
  76. for ($i=0; $i<$nr; $i++) {
  77.     $rec=$ab->getRecord($i) || next;
  78.     $addr=PDA::Pilot::Address::Unpack($rec);
  79.     $email="";
  80.     for (3 .. 7) {  # any of the 'phone' fields could be an email address
  81.         $tmp=$addr->{entry}[$_];
  82.         next if (!defined($tmp));
  83.         if ($tmp =~ /\@/) {
  84.             $email.=$tmp." ";
  85.         }
  86.     }
  87.     next if ($email !~ /\@/);
  88.     $email=~s/[\015\012]+/ /g;
  89.     $lname=$addr->{entry}[1];
  90.     $lname="" if (!defined($lname));
  91.     $fname=$addr->{entry}[0];
  92.     $fname="" if (!defined($fname));
  93.     $company=$addr->{entry}[2];
  94.     $company="" if (!defined($company));
  95.     foreach (split(/ +/, $email)) {
  96.         $tmp="$lname $fname";
  97.         $tmp=~s/^ *//;
  98.         $tmp=~s/ *$//;
  99.         $tmp=~s/ +/ /g;
  100.         $tmp=$company if ($tmp eq "");
  101.         $name="$_\t$tmp\t$company";
  102.         $name=~s/[\015\012]+/ /g;
  103.         push @list, $name if ($name =~ /$qry/i);
  104.     }
  105. }
  106. $ab->close();
  107.  
  108. my $nmatch=scalar(@list);
  109. print "$nmatch match", ($nmatch==1)?"\n":"es\n";
  110. foreach (@list) {
  111.     print "$_\n";
  112. }
  113. exit !$nmatch;
  114.  
  115.