1 | /* |
2 | gpgsetexpirytimeeditinteractor.cpp - Edit Interactor to change the expiry time of an OpenPGP key |
3 | Copyright (C) 2007 Klarälvdalens Datakonsult AB |
4 | |
5 | This file is part of GPGME++. |
6 | |
7 | GPGME++ is free software; you can redistribute it and/or |
8 | modify it under the terms of the GNU Library General Public |
9 | License as published by the Free Software Foundation; either |
10 | version 2 of the License, or (at your option) any later version. |
11 | |
12 | GPGME++ is distributed in the hope that it will be useful, |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 | GNU Library General Public License for more details. |
16 | |
17 | You should have received a copy of the GNU Library General Public License |
18 | along with GPGME++; see the file COPYING.LIB. If not, write to the |
19 | Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
20 | Boston, MA 02110-1301, USA. |
21 | */ |
22 | |
23 | #include "gpgsetexpirytimeeditinteractor.h" |
24 | #include "error.h" |
25 | |
26 | #include <gpgme.h> |
27 | |
28 | #include <cstring> |
29 | |
30 | using std::strcmp; |
31 | |
32 | // avoid conflict (msvc) |
33 | #ifdef ERROR |
34 | # undef ERROR |
35 | #endif |
36 | |
37 | using namespace GpgME; |
38 | |
39 | GpgSetExpiryTimeEditInteractor::GpgSetExpiryTimeEditInteractor( const std::string & t ) |
40 | : EditInteractor(), |
41 | m_strtime( t ) |
42 | { |
43 | |
44 | } |
45 | |
46 | GpgSetExpiryTimeEditInteractor::~GpgSetExpiryTimeEditInteractor() {} |
47 | |
48 | // work around --enable-final |
49 | namespace GpgSetExpiryTimeEditInteractor_Private { |
50 | enum { |
51 | START = EditInteractor::StartState, |
52 | COMMAND, |
53 | DATE, |
54 | QUIT, |
55 | SAVE, |
56 | |
57 | ERROR = EditInteractor::ErrorState |
58 | }; |
59 | } |
60 | |
61 | const char * GpgSetExpiryTimeEditInteractor::action( Error & err ) const { |
62 | |
63 | using namespace GpgSetExpiryTimeEditInteractor_Private; |
64 | |
65 | switch ( state() ) { |
66 | case COMMAND: |
67 | return "expire" ; |
68 | case DATE: |
69 | return m_strtime.c_str(); |
70 | case QUIT: |
71 | return "quit" ; |
72 | case SAVE: |
73 | return "Y" ; |
74 | case START: |
75 | case ERROR: |
76 | return 0; |
77 | default: |
78 | err = Error::fromCode( GPG_ERR_GENERAL ); |
79 | return 0; |
80 | } |
81 | } |
82 | |
83 | unsigned int GpgSetExpiryTimeEditInteractor::nextState( unsigned int status, const char * args, Error & err ) const { |
84 | |
85 | static const Error GENERAL_ERROR = Error::fromCode( GPG_ERR_GENERAL ); |
86 | static const Error INV_TIME_ERROR = Error::fromCode( GPG_ERR_INV_TIME ); |
87 | |
88 | if ( needsNoResponse( status ) ) { |
89 | return state(); |
90 | } |
91 | |
92 | using namespace GpgSetExpiryTimeEditInteractor_Private; |
93 | |
94 | switch ( state() ) { |
95 | case START: |
96 | if ( status == GPGME_STATUS_GET_LINE && |
97 | strcmp( args, "keyedit.prompt" ) == 0 ) { |
98 | return COMMAND; |
99 | } |
100 | err = GENERAL_ERROR; |
101 | return ERROR; |
102 | case COMMAND: |
103 | if ( status == GPGME_STATUS_GET_LINE && |
104 | strcmp( args, "keygen.valid" ) == 0 ) { |
105 | return DATE; |
106 | } |
107 | err = GENERAL_ERROR; |
108 | return ERROR; |
109 | case DATE: |
110 | if ( status == GPGME_STATUS_GET_LINE && |
111 | strcmp( args, "keyedit.prompt" ) == 0 ) { |
112 | return QUIT; |
113 | } else if ( status == GPGME_STATUS_GET_LINE && |
114 | strcmp( args, "keygen.valid" ) ) { |
115 | err = INV_TIME_ERROR; |
116 | return ERROR; |
117 | } |
118 | err = GENERAL_ERROR; |
119 | return ERROR; |
120 | case QUIT: |
121 | if ( status == GPGME_STATUS_GET_BOOL && |
122 | strcmp( args, "keyedit.save.okay" ) == 0 ) { |
123 | return SAVE; |
124 | } |
125 | err = GENERAL_ERROR; |
126 | return ERROR; |
127 | case ERROR: |
128 | if ( status == GPGME_STATUS_GET_LINE && |
129 | strcmp( args, "keyedit.prompt" ) == 0 ) { |
130 | return QUIT; |
131 | } |
132 | err = lastError(); |
133 | return ERROR; |
134 | default: |
135 | err = GENERAL_ERROR; |
136 | return ERROR; |
137 | } |
138 | } |
139 | |