NeoMutt  2023-03-22-27-g3cb248
Teaching an old dog new tricks
DOXYGEN
dlg_verifycert.c
Go to the documentation of this file.
1
53#include "config.h"
54#include <stdbool.h>
55#include <stdio.h>
56#include <string.h>
57#include "mutt/lib.h"
58#include "gui/lib.h"
59#include "color/lib.h"
60#include "menu/lib.h"
61#include "globals.h"
62#include "keymap.h"
63#include "opcodes.h"
64#include "ssl.h"
65
67static const struct Mapping VerifyHelp[] = {
68 // clang-format off
69 { N_("Exit"), OP_EXIT },
70 { N_("Help"), OP_HELP },
71 { NULL, 0 },
72 // clang-format on
73};
74
82static int menu_dialog_dokey(struct Menu *menu, int *id)
83{
84 // enum MuttCursorState cursor = mutt_curses_set_cursor(MUTT_CURSOR_VISIBLE);
85 struct KeyEvent ch = mutt_getch_timeout(5000);
86 // mutt_curses_set_cursor(cursor);
87
88 if ((ch.op == OP_TIMEOUT) || (ch.op == OP_ABORT))
89 {
90 *id = ch.op;
91 return 0;
92 }
93
94 struct CertMenuData *mdata = menu->mdata;
95 char *p = NULL;
96 if ((ch.ch != 0) && (p = strchr(mdata->keys, ch.ch)))
97 {
98 *id = OP_MAX + (p - mdata->keys + 1);
99 return 0;
100 }
101
102 if (ch.op == OP_NULL)
103 mutt_unget_ch(ch.ch);
104 else
105 mutt_unget_op(ch.op);
106 return -1;
107}
108
114static int menu_dialog_translate_op(int op)
115{
116 switch (op)
117 {
118 case OP_NEXT_ENTRY:
119 return OP_NEXT_LINE;
120 case OP_PREV_ENTRY:
121 return OP_PREV_LINE;
122 case OP_CURRENT_TOP:
123 return OP_TOP_PAGE;
124 case OP_CURRENT_BOTTOM:
125 return OP_BOTTOM_PAGE;
126 case OP_CURRENT_MIDDLE:
127 return OP_MIDDLE_PAGE;
128 }
129
130 return op;
131}
132
136static void cert_make_entry(struct Menu *menu, char *buf, size_t buflen, int line)
137{
138 struct CertMenuData *mdata = menu->mdata;
139
140 menu->current = -1; /* hide menubar */
141
142 const char **line_ptr = ARRAY_GET(mdata->carr, line);
143 if (!line_ptr)
144 {
145 buf[0] = '\0';
146 return;
147 }
148
149 mutt_str_copy(buf, *line_ptr, buflen);
150}
151
158void cert_array_clear(struct CertArray *carr)
159{
160 const char **line = NULL;
161 ARRAY_FOREACH(line, carr)
162 {
163 FREE(line);
164 }
165}
166
182int dlg_verify_certificate(const char *title, struct CertArray *carr,
183 bool allow_always, bool allow_skip)
184{
186
187 struct CertMenuData mdata = { carr };
188
189 struct Menu *menu = dlg->wdata;
190 menu->mdata = &mdata;
191 menu->mdata_free = NULL; // Menu doesn't own the data
193 menu->max = ARRAY_SIZE(carr);
194
195 struct MuttWindow *sbar = window_find_child(dlg, WT_STATUS_BAR);
196 sbar_set_title(sbar, title);
197
198 if (allow_always)
199 {
200 if (allow_skip)
201 {
202 mdata.prompt = _("(r)eject, accept (o)nce, (a)ccept always, (s)kip");
203 /* L10N: The letters correspond to the choices in the string:
204 "(r)eject, accept (o)nce, (a)ccept always, (s)kip"
205 This is an interactive certificate confirmation prompt for an SSL connection. */
206 mdata.keys = _("roas");
207 }
208 else
209 {
210 mdata.prompt = _("(r)eject, accept (o)nce, (a)ccept always");
211 /* L10N: The letters correspond to the choices in the string:
212 "(r)eject, accept (o)nce, (a)ccept always"
213 This is an interactive certificate confirmation prompt for an SSL connection. */
214 mdata.keys = _("roa");
215 }
216 }
217 else
218 {
219 if (allow_skip)
220 {
221 mdata.prompt = _("(r)eject, accept (o)nce, (s)kip");
222 /* L10N: The letters correspond to the choices in the string:
223 "(r)eject, accept (o)nce, (s)kip"
224 This is an interactive certificate confirmation prompt for an SSL connection. */
225 mdata.keys = _("ros");
226 }
227 else
228 {
229 mdata.prompt = _("(r)eject, accept (o)nce");
230 /* L10N: The letters correspond to the choices in the string:
231 "(r)eject, accept (o)nce"
232 This is an interactive certificate confirmation prompt for an SSL connection. */
233 mdata.keys = _("ro");
234 }
235 }
237
238 bool old_ime = OptIgnoreMacroEvents;
240
241 // ---------------------------------------------------------------------------
242 // Event Loop
243 int choice = 0;
244 int op = OP_NULL;
245 do
246 {
247 window_redraw(NULL);
249
250 // Try to catch dialog keys before ops
251 if (menu_dialog_dokey(menu, &op) != 0)
252 {
253 struct KeyEvent event = km_dokey_event(MENU_GENERIC);
254 if (event.ch == 'q')
255 op = OP_EXIT;
256 else
257 op = event.op;
258 }
259
260 if (op == OP_TIMEOUT)
261 continue;
262
263 // Convert menubar movement to scrolling
265
266 if (op <= OP_MAX)
267 mutt_debug(LL_DEBUG1, "Got op %s (%d)\n", opcodes_get_name(op), op);
268 else
269 mutt_debug(LL_DEBUG1, "Got choice %d\n", op - OP_MAX);
270
271 switch (op)
272 {
273 case -1: // Abort: Ctrl-G
274 case OP_EXIT: // Q)uit
275 case OP_MAX + 1: // R)eject
276 choice = 1;
277 break;
278 case OP_MAX + 2: // O)nce
279 choice = 2;
280 break;
281 case OP_MAX + 3: // A)lways / S)kip
282 choice = 3;
283 break;
284 case OP_MAX + 4: // S)kip
285 choice = 4;
286 break;
287
288 case OP_JUMP:
289 case OP_JUMP_1:
290 case OP_JUMP_2:
291 case OP_JUMP_3:
292 case OP_JUMP_4:
293 case OP_JUMP_5:
294 case OP_JUMP_6:
295 case OP_JUMP_7:
296 case OP_JUMP_8:
297 case OP_JUMP_9:
298 mutt_error(_("Jumping is not implemented for dialogs"));
299 continue;
300
301 case OP_SEARCH:
302 case OP_SEARCH_NEXT:
303 case OP_SEARCH_OPPOSITE:
304 case OP_SEARCH_REVERSE:
305 mutt_error(_("Search is not implemented for this menu"));
306 continue;
307 }
308
309 (void) menu_function_dispatcher(menu->win, op);
310 } while (choice == 0);
311 // ---------------------------------------------------------------------------
312
313 OptIgnoreMacroEvents = old_ime;
314
315 simple_dialog_free(&dlg);
316
317 return choice;
318}
#define ARRAY_FOREACH(elem, head)
Iterate over all elements of the array.
Definition: array.h:211
#define ARRAY_SIZE(head)
The number of elements stored.
Definition: array.h:86
#define ARRAY_GET(head, idx)
Return the element at index.
Definition: array.h:108
Color and attribute parsing.
@ MT_COLOR_PROMPT
Question/user input.
Definition: color.h:60
struct KeyEvent mutt_getch_timeout(int delay)
Get an event with a timeout.
Definition: curs_lib.c:196
void mutt_unget_op(int op)
Return an operation to the input buffer.
Definition: curs_lib.c:531
void mutt_unget_ch(int ch)
Return a keystroke to the input buffer.
Definition: curs_lib.c:520
static int menu_dialog_dokey(struct Menu *menu, int *id)
Check if there are any menu key events to process.
void cert_array_clear(struct CertArray *carr)
Free all memory of a CertArray.
static int menu_dialog_translate_op(int op)
Convert menubar movement to scrolling.
static const struct Mapping VerifyHelp[]
Help Bar for the Certificate Verification dialog.
int dlg_verify_certificate(const char *title, struct CertArray *carr, bool allow_always, bool allow_skip)
Ask the user to validate the certificate.
bool OptIgnoreMacroEvents
(pseudo) don't process macro/push/exec events while set
Definition: globals.c:71
int menu_function_dispatcher(struct MuttWindow *win, int op)
Perform a Menu function - Implements function_dispatcher_t -.
Definition: functions.c:320
#define mutt_error(...)
Definition: logging.h:87
#define mutt_debug(LEVEL,...)
Definition: logging.h:84
static void cert_make_entry(struct Menu *menu, char *buf, size_t buflen, int line)
Create a string to display in a Menu - Implements Menu::make_entry() -.
Convenience wrapper for the gui headers.
void simple_dialog_free(struct MuttWindow **ptr)
Destroy a simple index Dialog.
Definition: simple.c:166
struct MuttWindow * simple_dialog_new(enum MenuType mtype, enum WindowType wtype, const struct Mapping *help_data)
Create a simple index Dialog.
Definition: simple.c:129
struct KeyEvent km_dokey_event(enum MenuType mtype)
Determine what a keypress should do.
Definition: keymap.c:637
Manage keymappings.
@ LL_DEBUG1
Log at debug level 1.
Definition: logging.h:40
#define FREE(x)
Definition: memory.h:43
GUI present the user with a selectable list.
void msgwin_set_text(enum ColorId cid, const char *text)
Set the text for the Message Window.
Definition: msgwin.c:233
Convenience wrapper for the library headers.
#define N_(a)
Definition: message.h:32
#define _(a)
Definition: message.h:28
size_t mutt_str_copy(char *dest, const char *src, size_t dsize)
Copy a string into a buffer (guaranteeing NUL-termination)
Definition: string.c:652
void window_redraw(struct MuttWindow *win)
Reflow, recalc and repaint a tree of Windows.
Definition: mutt_window.c:605
struct MuttWindow * window_find_child(struct MuttWindow *win, enum WindowType type)
Recursively find a child Window of a given type.
Definition: mutt_window.c:523
@ WT_DLG_CERTIFICATE
Certificate Dialog, dlg_verify_certificate()
Definition: mutt_window.h:81
@ WT_STATUS_BAR
Status Bar containing extra info about the Index/Pager/etc.
Definition: mutt_window.h:102
const char * opcodes_get_name(int op)
Get the name of an opcode.
Definition: opcodes.c:46
All user-callable functions.
#define OP_TIMEOUT
Definition: opcodes.h:32
#define OP_ABORT
Definition: opcodes.h:33
@ OP_MAX
Definition: opcodes.h:378
void sbar_set_title(struct MuttWindow *win, const char *title)
Set the title for the Simple Bar.
Definition: sbar.c:224
Handling of SSL encryption.
Certificate data to use in the Menu.
Definition: ssl.h:42
struct CertArray * carr
Lines of the Certificate.
Definition: ssl.h:43
char * prompt
Prompt for user, similar to mutt_multi_choice.
Definition: ssl.h:44
char * keys
Keys used in the prompt.
Definition: ssl.h:45
An event such as a keypress.
Definition: keymap.h:65
int op
Function opcode, e.g. OP_HELP.
Definition: keymap.h:67
int ch
Raw key pressed.
Definition: keymap.h:66
Mapping between user-readable string and a constant.
Definition: mapping.h:32
Definition: lib.h:70
struct MuttWindow * win
Window holding the Menu.
Definition: lib.h:77
void(* make_entry)(struct Menu *menu, char *buf, size_t buflen, int line)
Definition: lib.h:97
int current
Current entry.
Definition: lib.h:71
void(* mdata_free)(struct Menu *menu, void **ptr)
Definition: lib.h:152
void * mdata
Private data.
Definition: lib.h:138
int max
Number of entries in the menu.
Definition: lib.h:72
void * wdata
Private data.
Definition: mutt_window.h:145
@ MENU_GENERIC
Generic selection list.
Definition: type.h:45