Reimplement gpg-key2ps in perl. Didn't seem to be so difficult.
[pgp-tools.git] / gpg-key2ps / gpg-key2ps
1 #!/usr/bin/perl
2 #
3 # gpg-key2ps: convert a PGP/GnuPG key into paper slips.
4 #
5 # $Id$
6
7 $version = '$Rev$';
8 $version =~ s/\$Rev:\s*(\d+)\s*\$/$1/;
9 $usage = "Usage: $0 [-p papersize] [-r revoked-style] keyid-or-name\n";
10 $keyids = "";
11 $revokestyle="hide";
12
13 if ( $#ARGV < 0 ) {
14 print $usage;
15 exit 1;
16 }
17
18 use Getopt::Std;
19 getopt('pr', \%opts);
20 if ( $opts{r} ) { $revokestyle = $opts{'r'}; }
21 if ( $opts{p} ) { $ENV{'PAPERSIZE'} = $opts{'p'}; }
22 foreach (@ARGV) { $keyids .= $_ . " "; }
23
24 if ( $revokestyle !~ /^(grey|hide|note|show|strike)$/ ) {
25 print STDERR "Unknown style \"$revokestyle\". Please use one of\n";
26 print STDERR " grey - Print text in grey\n";
27 print STDERR " hide - Don't show revoked uids\n";
28 print STDERR " note - Add \"(revoked)\"\n";
29 print STDERR " show - List revoked uids normally\n";
30 print STDERR " strike - Strike through lines\n";
31 exit 1;
32 }
33
34 if ( -x "/usr/bin/paperconf" ) {
35 $w=`paperconf -w`;
36 $h=`paperconf -h`;
37 chomp($w);
38 chomp($h);
39 } else {
40 # Default to A4.
41 $w=596;
42 $h=842;
43 }
44
45 open(GPG, "gpg --fingerprint --with-colons $keyids |");
46
47 print <<EOF;
48 %!PS-Adobe-3.0
49 %%BoundingBox: 0 0 $w $h
50 %%Title:
51 %%Creator: gpg-key2ps $version
52 EOF
53 print "%%CreationDate: " . scalar(localtime) . "\n";
54 print <<EOF;
55 %%Pages: 1
56 %%EndComments
57
58 %%Page: 1 1
59
60 /w $w def
61 /h $h def
62
63 /Times-Roman findfont 9 scalefont setfont
64
65 /newline {
66 /y y 10 sub def
67 } def
68
69 /hline {
70 30 y 3 add moveto
71 w 2 div 30 sub y 3 add lineto stroke
72 newline
73 } def
74
75 /needhline {
76 /condhline { hline } def
77 } def
78
79 /noneedhline {
80 /condhline { } def
81 } def
82
83 /showAlgorithm {
84 << 1 (R) 2 (r) 3 (s) 16 (g) 20 (G) 17 (D) >> exch get
85 show
86 } def
87
88 /pub {
89 condhline
90 50 y moveto (pub) show
91 70 y moveto show showAlgorithm (/) show show
92 150 y moveto show
93 200 y moveto show
94 newline
95 needhline
96 } def
97
98 /fpr {
99 70 y moveto (Key fingerprint = ) show show
100 newline
101 } def
102
103 /uid {
104 50 y moveto (uid) show
105 200 y moveto show
106 newline
107 } def
108
109 EOF
110
111 if ( $revokestyle eq "grey" ) {
112 print "/revuid {\n";
113 print " .5 setgray\n";
114 print " uid\n";
115 print " 0 setgray\n";
116 print "} def\n";
117 } elsif ( $revokestyle eq "hide" ) {
118 print "/revuid {} def\n";
119 } elsif ( $revokestyle eq "note" ) {
120 print "/revuid {\n";
121 print " 50 y moveto (uid) show\n";
122 print " 200 y moveto show ([revoked]) show\n";
123 print " newline\n";
124 print "} def\n";
125 } elsif ( $revokestyle eq "show" ) {
126 print "/revuid { uid } def\n";
127 } elsif ( $revokestyle eq "strike" ) {
128 print "/revuid {\n";
129 print " uid\n";
130 print " 45 y 9 add moveto h 2 div 45 sub y 18 add lineto stroke\n";
131 print "} def\n";
132 }
133
134 print <<EOF;
135
136 /sbk {
137 50 y moveto (sub) show
138 70 y moveto show showAlgorithm (/) show show
139 150 y moveto show
140 newline
141 } def
142
143 /key {
144 noneedhline
145 EOF
146
147 $numlines = 0;
148 while(<GPG>) {
149 if ( /^(tru|uat):/ ) { next; }
150 if ( /^pub:/ ) { $numlines++; }
151 s/^pub:[^:]*:([^:]*):([0-9]*):.{8,8}(.{8,8}):([^:]*):[^:]*:[^:]*:[^:]*:([^:]*):[^:]*:[^:]*:.*/ ($5) ($4) ($3) $2 ($1) pub/;
152 if ( /^fpr:[^:]*:[^:]*:[^:]*:[^:]*:[^:]*:[^:]*:[^:]*:[^:]*:([^:]*):.*/ ) {
153 $fpr = $1;
154 # v4 key
155 $fpr =~ s/(\w{4})(\w{4})(\w{4})(\w{4})(\w{4})(\w{4})(\w{4})(\w{4})(\w{4})(\w{4})/$1 $2 $3 $4 $5 $6 $7 $8 $9 $10/;
156 # v3 key
157 $fpr =~ s/(\w{2})(\w{2})(\w{2})(\w{2})(\w{2})(\w{2})(\w{2})(\w{2})(\w{2})(\w{2})(\w{2})(\w{2})(\w{2})(\w{2})(\w{2})(\w{2})/$1 $2 $3 $4 $5 $6 $7 $8 $9 $10 $11 $12 $13 $14 $15 $16/g;
158 $_ = " ($fpr) fpr\n";
159 }
160 s/^uid:[^:r]*:[^:]*:[^:]*:[^:]*:[^:]*:[^:]*:[^:]*:[^:]*:([^:]*):.*/ ($1) uid/;
161 s/^uid:[^:r]*:[^:]*:[^:]*:[^:]*:[^:]*:[^:]*:[^:]*:[^:]*:([^:]*):.*/ ($1) uid/;
162 s/^uid:r[^:]*:[^:]*:[^:]*:[^:]*:[^:]*:[^:]*:[^:]*:[^:]*:([^:]*):.*/ ($1) revuid/;
163 s/^sub:[^:]*:([^:]*):([0-9]*):.{8,8}(.{8,8}):([^:]*):[^:]*:[^:]*:[^:]*:[^:]*:[^:]*:[^:]*:.*/ ($4) ($3) $2 ($1) sbk/;
164 $numlines++;
165 print;
166 }
167 $numlines -= 1;
168
169 close(GPG);
170
171 print <<EOF;
172 } def
173
174 EOF
175 print "/numlines $numlines def\n";
176 print <<EOF;
177 /num w 16 sub 10 div numlines div def
178
179 /column {
180 /y w 20 sub def
181 1 1 num {
182 gsave
183 0 0 h 2 div w rectclip
184 /upper y 11 add def
185 key
186 newline
187 /lower y 11 add def
188 0 upper h 2 div upper h 2 div lower 0 lower 0 upper moveto lineto lineto lineto lineto stroke
189 grestore
190 } for
191 } def
192
193 w 0 translate
194 90 rotate
195 column
196 h 2 div 0 translate
197 column
198
199 showpage
200
201 %%Trailer
202 %%EOF
203 EOF
204
205