Convert README to Markdown for nice GitHub viewing
[interchange.git] / code / UI_Tag / add_gpg_key.coretag
1 # Copyright 2002-2007 Interchange Development Group and others
2
3 # This program is free software; you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation; either version 2 of the License, or
6 # (at your option) any later version.  See the LICENSE file for details.
7
8 UserTag add-gpg-key Order   name
9 UserTag add-gpg-key addAttr
10 UserTag add-gpg-key Version 1.6
11 UserTag add-gpg-key Routine <<EOR
12 sub {
13         my ($name, $opt) = @_;
14         my $gpgexe = $Global::Variable->{GPG_PATH} || 'gpg';
15
16         my $outfile = "$Vend::Cfg->{ScratchDir}/$Vend::Session->{id}.gpg_results";
17
18         my $flags = "--import --batch 2> $outfile";
19 #::logDebug("gpg_add flags=$flags");
20         
21         my $keytext = $opt->{text} || $CGI::values{$name};
22         $keytext =~ s/^\s+//;
23         $keytext =~ s/\s+$//;
24         open(GPGIMP, "| $gpgexe $flags") 
25                 or die "Can't fork: $!";
26         print GPGIMP $keytext;
27         close GPGIMP;
28
29         if($?) {
30                 $::Scratch->{ui_failure} = ::errmsg("Failed GPG key import.");
31                 return defined $opt->{failure} ? $opt->{failure} : undef;
32         }
33         else {
34                 my $keylist = `$gpgexe --list-keys`;
35                 $::Scratch->{ui_message} =
36                                                         ::errmsg(
37                                                                 "GPG key imported successfully.<PRE>\n%s\n</PRE>",
38                                                                 $keylist,
39                                                                 );
40         }
41
42         if($opt->{return_id}) {
43                 open(GETGPGID, "< $outfile")
44                         or do {
45                                 ::logGlobal("GPG key ID read -- can't read %s: %s", $outfile, $!);
46                                 return undef;
47                         };
48                 my $id;
49                 while(<GETGPGID>) {
50                         next unless /\bkey\s+(\w+)\s*:\s+(public\s+key|)(.*)(imported|not\s+changed)/i;
51                         $id = $1;
52                         last;
53                 }
54                 close GETGPGID;
55                 return $id || 'Failed ID get?';
56                 
57         }
58         elsif (defined $opt->{success}) {
59                 return $opt->{success};
60         }
61         else {
62                 return 1;
63         }
64 }
65 EOR