Give Usage when called with no arguments
[pgp-tools.git] / gpglist / gpglist
1 #!/usr/bin/perl
2
3 # $Id$
4
5 # small script to show in an intuitive way who signed which of your user ids
6 #
7 # Copyright (c) 2004 Uli Martens <uli@youam.net>
8 # Copyright (c) 2005 Peter Palfrader <peter@palfrader.org>
9 # All rights reserved.
10 #
11 # Redistribution and use in source and binary forms, with or without
12 # modification, are permitted provided that the following conditions
13 # are met:
14 # 1. Redistributions of source code must retain the above copyright
15 # notice, this list of conditions and the following disclaimer.
16 # 2. Redistributions in binary form must reproduce the above copyright
17 # notice, this list of conditions and the following disclaimer in the
18 # documentation and/or other materials provided with the distribution.
19 # 3. The name of the author may not be used to endorse or promote products
20 # derived from this software without specific prior written permission.
21 #
22 # THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23 # IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 # OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 # IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27 # NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31 # THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32
33 use strict;
34 use warnings;
35 use English;
36
37 my $key=shift @ARGV;
38 unless (defined $key) {
39 die "Usage: $PROGRAM_NAME <keyid>\n";
40 }
41
42 open SIGS, "gpg --fixed-list-mode --with-colons --list-sigs $key 2>/dev/null |"
43 or die "can't get gpg listing";
44
45 my $uid = "";
46 my @uids;
47 my %sigs;
48 my %rev;
49 my %ids;
50 my %unknownID;
51 my $longkey;
52 while (<SIGS>) {
53 if ( m/^uid:.:[^:]*:[^:]*:[^:]*:[^:]*:[^:]*:[^:]*:[^:]*:(.*):$/ ) {
54 $uid = $1;
55 push @uids, $1;
56 next;
57 }
58 if ( m/^sig:[^:]*:[^:]*:[^:]*:([0-9A-F]*):[^:]*:[^:]*:[^:]*:[^:]*:(.*):[^:]*:$/ ) {
59 $2 eq "[User id not found]" ? $unknownID{$1} = $1 : $ids{$2} = $1;
60 $sigs{$1}->{$uid} = "x" unless defined $sigs{$1}->{$uid};
61 next;
62 }
63 if ( m/^rev:[^:]*:[^:]*:[^:]*:([0-9A-F]*):[^:]*:[^:]*:[^:]*:[^:]*:(.*):[^:]*:$/ ) {
64 $rev{$uid} = "x" if ($longkey eq $1);
65 $sigs{$1}->{$uid} = "R";
66 next;
67 }
68 if ( m/^uat:.:[^:]*:[^:]*:[^:]*:[^:]*:[^:]*:[^:]*:[^:]*:(.*):$/ ) {
69 $uid = "Photo ID";
70 push @uids, $uid;
71 next;
72 }
73 if ( m/^pub:[^:]*:[^:]*:[^:]*:([^:]*):[^:]*:[^:]*:[^:]*:[^:]*:(.*):$/ ) {
74 $longkey = $1;
75 next;
76 }
77 last if ( m/^(sub):/ );
78 next if ( m/^(tru):/ );
79 print STDERR "hi, i'm a bug. please report me to my owner";
80 die "input: $_, key: $key";
81 }
82 close SIGS;
83
84 # XXX: Add an option for this
85 my $c = 0;
86 @uids = grep { ! defined $rev{$uids[$c++]} } @uids;
87
88 my $n = scalar @uids -1;
89 for ( my $a=0; $a <= $n; $a++ ) {
90 printf "| " x ($a)
91 . "+--"
92 . "---" x ($n-$a)
93 . (defined $rev{$uids[$a]} ? "R" : " ")
94 . "%2i $uids[$a]\n", $a+1;
95 }
96
97 for ( my $a=0; $a <= $n; $a++ ) {
98 printf "%-2i ", $a+1;
99 }
100 print "\n";
101
102 for my $id (sort keys %ids) {
103 print((defined $sigs{$ids{$id}}->{$_} ? $sigs{$ids{$id}}->{$_} : " ") . " ") for (@uids);
104 print $ids{$id}." $id\n";
105 }
106 for my $id (sort keys %unknownID) {
107 print((defined $sigs{$id}->{$_} ? $sigs{$id}->{$_} : " ") . " ") for (@uids);
108 print "$id [User id not found]\n";
109 }