chiark / gitweb /
5f9991e133b44864818b6d73b9079660d57f3d76
[bin.git] / query-hermes.pl
1 #! /usr/bin/perl -w
2 use diagnostics;
3 use strict;
4
5 my $arg = shift;
6
7 print "Querying hermes for $arg ... ";
8
9 my $pid = open FINGER, "-|";
10 if (!defined $pid)
11 {
12         print "can't fork: $!\n";
13         exit 2;
14 }
15 elsif (!$pid)
16 {
17         if (not exec 'finger', "$arg\@hermes")
18         {
19                 print "can't exec finger: $!\n";
20                 exit 2;
21         }
22 }
23 else
24 {
25         <FINGER>;
26         <FINGER>;
27         $/ = undef;
28         my $finger = <FINGER>;
29         close FINGER;
30
31         unless (defined $finger)
32         {
33                 print "no matches\n";
34                 exit 1;
35         }
36
37         my @people = split /\n/, $finger;
38         foreach my $person (@people)
39         {
40                 $person =~ s/^([^ ]*) *(.{1,20}[^ ]).*$/$1\t$2/ or $person = '';
41         }
42         @people = grep /./, @people;
43
44         if    (@people == 0) { print "no matches\n"; exit 1; }
45         elsif (@people == 1) { print "1 match\n"; }
46         else                 { print scalar(@people), " matches\n"; }
47
48         print "$_\n" foreach @people;
49 }
50