1 | /* |
2 | gpgadduserideditinteractor.cpp - Edit Interactor to add a new UID to an OpenPGP key |
3 | Copyright (C) 2008 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 "gpgadduserideditinteractor.h" |
24 | |
25 | #include "error.h" |
26 | |
27 | #include <gpgme.h> |
28 | |
29 | #include <cstring> |
30 | |
31 | using std::strcmp; |
32 | |
33 | // avoid conflict (msvc) |
34 | #ifdef ERROR |
35 | # undef ERROR |
36 | #endif |
37 | |
38 | using namespace GpgME; |
39 | |
40 | GpgAddUserIDEditInteractor::GpgAddUserIDEditInteractor() |
41 | : EditInteractor(), |
42 | m_name(), |
43 | m_email(), |
44 | m_comment() |
45 | { |
46 | |
47 | } |
48 | |
49 | GpgAddUserIDEditInteractor::~GpgAddUserIDEditInteractor() {} |
50 | |
51 | void GpgAddUserIDEditInteractor::setNameUtf8( const std::string & name ) { |
52 | m_name = name; |
53 | } |
54 | |
55 | void GpgAddUserIDEditInteractor::setEmailUtf8( const std::string & email ) { |
56 | m_email = email; |
57 | } |
58 | |
59 | void GpgAddUserIDEditInteractor::( const std::string & ) { |
60 | m_comment = comment; |
61 | } |
62 | |
63 | // work around --enable-final |
64 | namespace GpgAddUserIDEditInteractor_Private { |
65 | enum { |
66 | START = EditInteractor::StartState, |
67 | COMMAND, |
68 | NAME, |
69 | EMAIL, |
70 | , |
71 | QUIT, |
72 | SAVE, |
73 | |
74 | ERROR = EditInteractor::ErrorState |
75 | }; |
76 | } |
77 | |
78 | const char * GpgAddUserIDEditInteractor::action( Error & err ) const { |
79 | |
80 | using namespace GpgAddUserIDEditInteractor_Private; |
81 | |
82 | switch ( state() ) { |
83 | case COMMAND: |
84 | return "adduid" ; |
85 | case NAME: |
86 | return m_name.c_str(); |
87 | case EMAIL: |
88 | return m_email.c_str(); |
89 | case COMMENT: |
90 | return m_comment.c_str(); |
91 | case QUIT: |
92 | return "quit" ; |
93 | case SAVE: |
94 | return "Y" ; |
95 | case START: |
96 | case ERROR: |
97 | return 0; |
98 | default: |
99 | err = Error::fromCode( GPG_ERR_GENERAL ); |
100 | return 0; |
101 | } |
102 | } |
103 | |
104 | unsigned int GpgAddUserIDEditInteractor::nextState( unsigned int status, const char * args, Error & err ) const { |
105 | |
106 | static const Error GENERAL_ERROR = Error::fromCode( GPG_ERR_GENERAL ); |
107 | static const Error INV_NAME_ERROR = Error::fromCode( GPG_ERR_INV_NAME ); |
108 | static const Error INV_EMAIL_ERROR = Error::fromCode( GPG_ERR_INV_USER_ID ); |
109 | static const Error = Error::fromCode( GPG_ERR_INV_USER_ID ); |
110 | |
111 | if ( needsNoResponse( status ) ) { |
112 | return state(); |
113 | } |
114 | |
115 | using namespace GpgAddUserIDEditInteractor_Private; |
116 | |
117 | switch ( state() ) { |
118 | case START: |
119 | if ( status == GPGME_STATUS_GET_LINE && |
120 | strcmp( args, "keyedit.prompt" ) == 0 ) { |
121 | return COMMAND; |
122 | } |
123 | err = GENERAL_ERROR; |
124 | return ERROR; |
125 | case COMMAND: |
126 | if ( status == GPGME_STATUS_GET_LINE && |
127 | strcmp( args, "keygen.name" ) == 0 ) { |
128 | return NAME; |
129 | } |
130 | err = GENERAL_ERROR; |
131 | return ERROR; |
132 | case NAME: |
133 | if ( status == GPGME_STATUS_GET_LINE && |
134 | strcmp( args, "keygen.email" ) == 0 ) { |
135 | return EMAIL; |
136 | } |
137 | err = GENERAL_ERROR; |
138 | if ( status == GPGME_STATUS_GET_LINE && |
139 | strcmp( args, "keygen.name" ) == 0 ) { |
140 | err = INV_NAME_ERROR; |
141 | } |
142 | return ERROR; |
143 | case EMAIL: |
144 | if ( status == GPGME_STATUS_GET_LINE && |
145 | strcmp( args, "keygen.comment" ) == 0 ) { |
146 | return COMMENT; |
147 | } |
148 | err = GENERAL_ERROR; |
149 | if ( status == GPGME_STATUS_GET_LINE && |
150 | strcmp( args, "keygen.email" ) == 0 ) { |
151 | err = INV_EMAIL_ERROR; |
152 | } |
153 | return ERROR; |
154 | case COMMENT: |
155 | if ( status == GPGME_STATUS_GET_LINE && |
156 | strcmp( args, "keyedit.prompt" ) == 0 ) { |
157 | return QUIT; |
158 | } |
159 | err = GENERAL_ERROR; |
160 | if ( status == GPGME_STATUS_GET_LINE && |
161 | strcmp( args, "keygen.comment" ) == 0 ) { |
162 | err = INV_COMMENT_ERROR; |
163 | } |
164 | return ERROR; |
165 | case QUIT: |
166 | if ( status == GPGME_STATUS_GET_BOOL && |
167 | strcmp( args, "keyedit.save.okay" ) == 0 ) { |
168 | return SAVE; |
169 | } |
170 | err = GENERAL_ERROR; |
171 | return ERROR; |
172 | case ERROR: |
173 | if ( status == GPGME_STATUS_GET_LINE && |
174 | strcmp( args, "keyedit.prompt" ) == 0 ) { |
175 | return QUIT; |
176 | } |
177 | err = lastError(); |
178 | return ERROR; |
179 | default: |
180 | err = GENERAL_ERROR; |
181 | return ERROR; |
182 | } |
183 | } |
184 | |