Revert "Embed Safe 2.07 into Vend::Safe to avoid various problems with recent version...
[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 # $Id: add_gpg_key.coretag,v 1.6 2007-03-30 23:40:54 pajamian Exp $
9
10 UserTag add-gpg-key Order   name
11 UserTag add-gpg-key addAttr
12 UserTag add-gpg-key Version $Revision: 1.6 $
13 UserTag add-gpg-key Routine <<EOR
14 sub {
15         my ($name, $opt) = @_;
16         my $gpgexe = $Global::Variable->{GPG_PATH} || 'gpg';
17
18         my $outfile = "$Vend::Cfg->{ScratchDir}/$Vend::Session->{id}.gpg_results";
19
20         my $flags = "--import --batch 2> $outfile";
21 #::logDebug("gpg_add flags=$flags");
22         
23         my $keytext = $opt->{text} || $CGI::values{$name};
24         $keytext =~ s/^\s+//;
25         $keytext =~ s/\s+$//;
26         open(GPGIMP, "| $gpgexe $flags") 
27                 or die "Can't fork: $!";
28         print GPGIMP $keytext;
29         close GPGIMP;
30
31         if($?) {
32                 $::Scratch->{ui_failure} = ::errmsg("Failed GPG key import.");
33                 return defined $opt->{failure} ? $opt->{failure} : undef;
34         }
35         else {
36                 my $keylist = `$gpgexe --list-keys`;
37                 $::Scratch->{ui_message} =
38                                                         ::errmsg(
39                                                                 "GPG key imported successfully.<PRE>\n%s\n</PRE>",
40                                                                 $keylist,
41                                                                 );
42         }
43
44         if($opt->{return_id}) {
45                 open(GETGPGID, "< $outfile")
46                         or do {
47                                 ::logGlobal("GPG key ID read -- can't read %s: %s", $outfile, $!);
48                                 return undef;
49                         };
50                 my $id;
51                 while(<GETGPGID>) {
52                         next unless /\bkey\s+(\w+)\s*:\s+(public\s+key|)(.*)(imported|not\s+changed)/i;
53                         $id = $1;
54                         last;
55                 }
56                 close GETGPGID;
57                 return $id || 'Failed ID get?';
58                 
59         }
60         elsif (defined $opt->{success}) {
61                 return $opt->{success};
62         }
63         else {
64                 return 1;
65         }
66 }
67 EOR