Hi, all.

I was poking through the ph wrappers on Brandon Long's page, and none of them
really let me do what I wanted (and none used the perl PH module, which is
handy), so I wrote my own.

The big difference is that it takes a bunch of arguments on the commandline
that let you customize the output a bit. Firstly, the server; this will let
you change query_command with a macro or hook to change your ph server. Then,
it lets you specify the name of the fields that the 'name' and 'email address'
are in. It's "name" and "email" here, but I'm not sure if that's consistent
everywhere. Lastly, it lets you specify other arbitrary fields to fill in
the "info" blank in the query results with whatever you like.

Brandon, I'd be charmed if you included this with the rest of 'em on your
mutt page..

  -Rich


#!/usr/bin/perl -w
#
# muttph -- query program for mutt using Net::PH.
# Rich Lafferty <[EMAIL PROTECTED]>
#
# This program is free software; you can redistribute and/or modify it under
# the same terms as Perl itself (although the author is particularly fond of
# the Artistic License, he'll tolerate the GPL too).
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# 
# To use, add to your muttrc (one line):
#
# set query_command = 
#   "/path/to/muttph '%s' phserver namefield emailfield [otherfield1 [otherfield2..]]"
#
#  where phserver is the name of your PH server
#        namefield is the field which contains names
#        emailfield is the field which contains email addresses
#        otherfield1 etc. are fields which you wish returned in the query
#            for informational purposes.
#
#  For example, I use
#   "muttph '%s' phone.concordia.ca name email phone dept title"
#
# If you get an error like "Can't locate Net/PH.pm in @INC", then you need
# to install Net::PH, which is part of the libnet bundle:
#
# $ perl -MCPAN -e'install Net::PH'
#
# Querying is documented in the mutt manual, but you've already read that,
# so you knew that. :-) 

require 5.004;
use strict;
use Net::PH;
my $version = "0.1";

my ($query, $phserver, $namefield, $emailfield, @otherfields) = @ARGV;

print qq(Results for "$query" from $phserver by muttph $version\n);

my $ph = Net::PH->new($phserver,
                      Port    => 105,
                      Timeout => 120,
                      Debug   => 0);

# Ask PH server
my $q = $ph->query({ name => "$query" },
                   [$namefield, $emailfield, @otherfields]);

if ($ph) {
    foreach my $handle (@{$q}) {
        print ${$handle}{$emailfield}->text . "\t";

# Uncomment this if your PH server returns names in the format "SMITH JOHN"
# and you want to send mail to John Smith rather than SMITH JOHN. Of course,
# this breaks on anything more complex than LASTNAME FIRSTNAME, but it saves
# having to retype them *all* to make them look pretty. :-)
#
#       my ($lastname, $firstname) = split(" ",${$handle}{$namefield}->text,2);
#       $lastname = ucfirst(lc($lastname));
#       $firstname = ucfirst(lc($firstname));
#       print "$firstname $lastname\t";
#
# Comment this out if you've uncommented the bit above, and vice-versa.
        print ${$handle}{$namefield}->text . "\t";

        # Include other fields
        foreach my $field (@otherfields) {
            my $entry = ${$handle}{$field}->text;
            print "$entry ";
        }
    print "\n";
    }
}

Reply via email to