NeoMutt  2023-11-03-85-g512e01
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
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 "key/lib.h"
61#include "menu/lib.h"
62#include "ssl.h"
63
65static const struct Mapping VerifyHelp[] = {
66 // clang-format off
67 { N_("Exit"), OP_EXIT },
68 { N_("Help"), OP_HELP },
69 { NULL, 0 },
70 // clang-format on
71};
72
80static int menu_dialog_dokey(struct Menu *menu, int *id)
81{
83
84 if ((event.op == OP_TIMEOUT) || (event.op == OP_ABORT))
85 {
86 *id = event.op;
87 return 0;
88 }
89
90 struct CertMenuData *mdata = menu->mdata;
91 char *p = NULL;
92 if ((event.ch != 0) && (p = strchr(mdata->keys, event.ch)))
93 {
94 *id = OP_MAX + (p - mdata->keys + 1);
95 return 0;
96 }
97
98 if (event.op == OP_NULL)
99 mutt_unget_ch(event.ch);
100 else
101 mutt_unget_op(event.op);
102 return -1;
103}
104
110static int menu_dialog_translate_op(int op)
111{
112 switch (op)
113 {
114 case OP_NEXT_ENTRY:
115 return OP_NEXT_LINE;
116 case OP_PREV_ENTRY:
117 return OP_PREV_LINE;
118 case OP_CURRENT_TOP:
119 return OP_TOP_PAGE;
120 case OP_CURRENT_BOTTOM:
121 return OP_BOTTOM_PAGE;
122 case OP_CURRENT_MIDDLE:
123 return OP_MIDDLE_PAGE;
124 }
125
126 return op;
127}
128
132static void cert_make_entry(struct Menu *menu, char *buf, size_t buflen, int line)
133{
134 struct CertMenuData *mdata = menu->mdata;
135
136 menu->current = -1; /* hide menubar */
137
138 const char **line_ptr = ARRAY_GET(mdata->carr, line);
139 if (!line_ptr)
140 {
141 buf[0] = '\0';
142 return;
143 }
144
145 mutt_str_copy(buf, *line_ptr, buflen);
146}
147
154void cert_array_clear(struct CertArray *carr)
155{
156 const char **line = NULL;
157 ARRAY_FOREACH(line, carr)
158 {
159 FREE(line);
160 }
161}
162
181int dlg_certificate(const char *title, struct CertArray *carr, bool allow_always, bool allow_skip)
182{
184
185 struct CertMenuData mdata = { carr };
186
187 struct Menu *menu = dlg->wdata;
188 menu->mdata = &mdata;
189 menu->mdata_free = NULL; // Menu doesn't own the data
191 menu->max = ARRAY_SIZE(carr);
192
193 struct MuttWindow *sbar = window_find_child(dlg, WT_STATUS_BAR);
194 sbar_set_title(sbar, title);
195
196 if (allow_always)
197 {
198 if (allow_skip)
199 {
200 mdata.prompt = _("(r)eject, accept (o)nce, (a)ccept always, (s)kip");
201 /* L10N: The letters correspond to the choices in the string:
202 "(r)eject, accept (o)nce, (a)ccept always, (s)kip"
203 This is an interactive certificate confirmation prompt for an SSL connection. */
204 mdata.keys = _("roas");
205 }
206 else
207 {
208 mdata.prompt = _("(r)eject, accept (o)nce, (a)ccept always");
209 /* L10N: The letters correspond to the choices in the string:
210 "(r)eject, accept (o)nce, (a)ccept always"
211 This is an interactive certificate confirmation prompt for an SSL connection. */
212 mdata.keys = _("roa");
213 }
214 }
215 else
216 {
217 if (allow_skip)
218 {
219 mdata.prompt = _("(r)eject, accept (o)nce, (s)kip");
220 /* L10N: The letters correspond to the choices in the string:
221 "(r)eject, accept (o)nce, (s)kip"
222 This is an interactive certificate confirmation prompt for an SSL connection. */
223 mdata.keys = _("ros");
224 }
225 else
226 {
227 mdata.prompt = _("(r)eject, accept (o)nce");
228 /* L10N: The letters correspond to the choices in the string:
229 "(r)eject, accept (o)nce"
230 This is an interactive certificate confirmation prompt for an SSL connection. */
231 mdata.keys = _("ro");
232 }
233 }
235
236 struct MuttWindow *old_focus = window_set_focus(menu->win);
237 // ---------------------------------------------------------------------------
238 // Event Loop
239 int choice = 0;
240 int op = OP_NULL;
241 do
242 {
243 window_redraw(NULL);
245
246 // Try to catch dialog keys before ops
247 if (menu_dialog_dokey(menu, &op) != 0)
248 {
250 }
251
252 if (op == OP_TIMEOUT)
253 continue;
254
255 // Convert menubar movement to scrolling
257
258 if (op <= OP_MAX)
259 mutt_debug(LL_DEBUG1, "Got op %s (%d)\n", opcodes_get_name(op), op);
260 else
261 mutt_debug(LL_DEBUG1, "Got choice %d\n", op - OP_MAX);
262
263 switch (op)
264 {
265 case -1: // Abort: Ctrl-G
266 case OP_QUIT: // Q)uit
267 case OP_MAX + 1: // R)eject
268 choice = 1;
269 break;
270 case OP_MAX + 2: // O)nce
271 choice = 2;
272 break;
273 case OP_MAX + 3: // A)lways / S)kip
274 choice = 3;
275 break;
276 case OP_MAX + 4: // S)kip
277 choice = 4;
278 break;
279
280 case OP_JUMP:
281 case OP_JUMP_1:
282 case OP_JUMP_2:
283 case OP_JUMP_3:
284 case OP_JUMP_4:
285 case OP_JUMP_5:
286 case OP_JUMP_6:
287 case OP_JUMP_7:
288 case OP_JUMP_8:
289 case OP_JUMP_9:
290 mutt_error(_("Jumping is not implemented for dialogs"));
291 continue;
292
293 case OP_SEARCH:
294 case OP_SEARCH_NEXT:
295 case OP_SEARCH_OPPOSITE:
296 case OP_SEARCH_REVERSE:
297 mutt_error(_("Search is not implemented for this menu"));
298 continue;
299 }
300
301 (void) menu_function_dispatcher(menu->win, op);
302 } while (choice == 0);
303 // ---------------------------------------------------------------------------
304
305 window_set_focus(old_focus);
306 simple_dialog_free(&dlg);
307
308 return choice;
309}
#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:61
struct KeyEvent mutt_getch(GetChFlags flags)
Read a character from the input buffer.
Definition: get.c:221
void mutt_unget_op(int op)
Return an operation to the input buffer.
Definition: get.c:125
void mutt_unget_ch(int ch)
Return a keystroke to the input buffer.
Definition: get.c:114
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 km_dokey(enum MenuType mtype, GetChFlags flags)
Determine what a keypress should do.
Definition: get.c:475
int menu_function_dispatcher(struct MuttWindow *win, int op)
Perform a Menu function - Implements function_dispatcher_t -.
Definition: functions.c:317
int dlg_certificate(const char *title, struct CertArray *carr, bool allow_always, bool allow_skip)
Ask the user to validate the certificate -.
#define mutt_error(...)
Definition: logging2.h:92
#define mutt_debug(LEVEL,...)
Definition: logging2.h:89
static void cert_make_entry(struct Menu *menu, char *buf, size_t buflen, int line)
Create a Certificate for the 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:168
struct MuttWindow * simple_dialog_new(enum MenuType mtype, enum WindowType wtype, const struct Mapping *help_data)
Create a simple index Dialog.
Definition: simple.c:132
Manage keymappings.
#define GETCH_IGNORE_MACRO
Don't use MacroEvents.
Definition: lib.h:53
@ LL_DEBUG1
Log at debug level 1.
Definition: logging2.h:43
#define FREE(x)
Definition: memory.h:45
GUI present the user with a selectable list.
void msgwin_set_text(struct MuttWindow *win, const char *text, enum ColorId color)
Set the text for the Message Window.
Definition: msgwin.c:484
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:653
void window_redraw(struct MuttWindow *win)
Reflow, recalc and repaint a tree of Windows.
Definition: mutt_window.c:634
struct MuttWindow * window_set_focus(struct MuttWindow *win)
Set the Window focus.
Definition: mutt_window.c:684
struct MuttWindow * window_find_child(struct MuttWindow *win, enum WindowType type)
Recursively find a child Window of a given type.
Definition: mutt_window.c:533
@ WT_DLG_CERTIFICATE
Certificate Dialog, dlg_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:48
#define OP_TIMEOUT
1 second with no events
Definition: opcodes.h:34
#define OP_ABORT
$abort_key pressed (Ctrl-G)
Definition: opcodes.h:35
@ OP_MAX
Definition: opcodes.h:380
void sbar_set_title(struct MuttWindow *win, const char *title)
Set the title for the Simple Bar.
Definition: sbar.c:227
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 mw_multi_choice.
Definition: ssl.h:44
char * keys
Keys used in the prompt.
Definition: ssl.h:45
An event such as a keypress.
Definition: lib.h:82
int op
Function opcode, e.g. OP_HELP.
Definition: lib.h:84
int ch
Raw key pressed.
Definition: lib.h:83
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:96
int current
Current entry.
Definition: lib.h:71
void(* mdata_free)(struct Menu *menu, void **ptr)
Definition: lib.h:151
void * mdata
Private data.
Definition: lib.h:137
int max
Number of entries in the menu.
Definition: lib.h:72
void * wdata
Private data.
Definition: mutt_window.h:145
@ MENU_DIALOG
Simple Dialog.
Definition: type.h:43
@ MENU_GENERIC
Generic selection list.
Definition: type.h:46