NeoMutt  2024-03-23-142-g2b2e76
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
dlg_verifycert.c
Go to the documentation of this file.
1
54#include "config.h"
55#include <stdbool.h>
56#include <stdio.h>
57#include <string.h>
58#include "mutt/lib.h"
59#include "gui/lib.h"
60#include "color/lib.h"
61#include "key/lib.h"
62#include "menu/lib.h"
63#include "ssl.h"
64
66static const struct Mapping VerifyHelp[] = {
67 // clang-format off
68 { N_("Exit"), OP_EXIT },
69 { N_("Help"), OP_HELP },
70 { NULL, 0 },
71 // clang-format on
72};
73
81static int menu_dialog_dokey(struct Menu *menu, int *id)
82{
84
85 if ((event.op == OP_TIMEOUT) || (event.op == OP_ABORT))
86 {
87 *id = event.op;
88 return 0;
89 }
90
91 struct CertMenuData *mdata = menu->mdata;
92 char *p = NULL;
93 if ((event.ch != 0) && (p = strchr(mdata->keys, event.ch)))
94 {
95 *id = OP_MAX + (p - mdata->keys + 1);
96 return 0;
97 }
98
99 if (event.op == OP_NULL)
100 mutt_unget_ch(event.ch);
101 else
102 mutt_unget_op(event.op);
103 return -1;
104}
105
111static int menu_dialog_translate_op(int op)
112{
113 switch (op)
114 {
115 case OP_NEXT_ENTRY:
116 return OP_NEXT_LINE;
117 case OP_PREV_ENTRY:
118 return OP_PREV_LINE;
119 case OP_CURRENT_TOP:
120 return OP_TOP_PAGE;
121 case OP_CURRENT_BOTTOM:
122 return OP_BOTTOM_PAGE;
123 case OP_CURRENT_MIDDLE:
124 return OP_MIDDLE_PAGE;
125 }
126
127 return op;
128}
129
133static int cert_make_entry(struct Menu *menu, int line, int max_cols, struct Buffer *buf)
134{
135 struct CertMenuData *mdata = menu->mdata;
136
137 menu->current = -1; /* hide menubar */
138
139 int total_cols = 0;
140
141 const char **line_ptr = ARRAY_GET(mdata->carr, line);
142 if (line_ptr)
143 {
144 const int bytes = buf_addstr(buf, *line_ptr);
145 total_cols = mutt_strnwidth(buf_string(buf), bytes);
146 }
147
148 return total_cols;
149}
150
157void cert_array_clear(struct CertArray *carr)
158{
159 const char **line = NULL;
160 ARRAY_FOREACH(line, carr)
161 {
162 FREE(line);
163 }
164}
165
184int dlg_certificate(const char *title, struct CertArray *carr, bool allow_always, bool allow_skip)
185{
187
188 struct CertMenuData mdata = { carr };
189
190 struct Menu *menu = dlg->wdata;
191 menu->mdata = &mdata;
192 menu->mdata_free = NULL; // Menu doesn't own the data
194 menu->max = ARRAY_SIZE(carr);
195
196 struct MuttWindow *sbar = window_find_child(dlg, WT_STATUS_BAR);
197 sbar_set_title(sbar, title);
198
199 if (allow_always)
200 {
201 if (allow_skip)
202 {
203 mdata.prompt = _("(r)eject, accept (o)nce, (a)ccept always, (s)kip");
204 /* L10N: The letters correspond to the choices in the string:
205 "(r)eject, accept (o)nce, (a)ccept always, (s)kip"
206 This is an interactive certificate confirmation prompt for an SSL connection. */
207 mdata.keys = _("roas");
208 }
209 else
210 {
211 mdata.prompt = _("(r)eject, accept (o)nce, (a)ccept always");
212 /* L10N: The letters correspond to the choices in the string:
213 "(r)eject, accept (o)nce, (a)ccept always"
214 This is an interactive certificate confirmation prompt for an SSL connection. */
215 mdata.keys = _("roa");
216 }
217 }
218 else
219 {
220 if (allow_skip)
221 {
222 mdata.prompt = _("(r)eject, accept (o)nce, (s)kip");
223 /* L10N: The letters correspond to the choices in the string:
224 "(r)eject, accept (o)nce, (s)kip"
225 This is an interactive certificate confirmation prompt for an SSL connection. */
226 mdata.keys = _("ros");
227 }
228 else
229 {
230 mdata.prompt = _("(r)eject, accept (o)nce");
231 /* L10N: The letters correspond to the choices in the string:
232 "(r)eject, accept (o)nce"
233 This is an interactive certificate confirmation prompt for an SSL connection. */
234 mdata.keys = _("ro");
235 }
236 }
238
239 struct MuttWindow *old_focus = window_set_focus(menu->win);
240 // ---------------------------------------------------------------------------
241 // Event Loop
242 int choice = 0;
243 int op = OP_NULL;
244 do
245 {
246 window_redraw(NULL);
248
249 // Try to catch dialog keys before ops
250 if (menu_dialog_dokey(menu, &op) != 0)
251 {
253 }
254
255 if (op == OP_TIMEOUT)
256 continue;
257
258 // Convert menubar movement to scrolling
260
261 if (op <= OP_MAX)
262 mutt_debug(LL_DEBUG1, "Got op %s (%d)\n", opcodes_get_name(op), op);
263 else
264 mutt_debug(LL_DEBUG1, "Got choice %d\n", op - OP_MAX);
265
266 switch (op)
267 {
268 case -1: // Abort: Ctrl-G
269 case OP_QUIT: // Q)uit
270 case OP_MAX + 1: // R)eject
271 choice = 1;
272 break;
273 case OP_MAX + 2: // O)nce
274 choice = 2;
275 break;
276 case OP_MAX + 3: // A)lways / S)kip
277 choice = 3;
278 break;
279 case OP_MAX + 4: // S)kip
280 choice = 4;
281 break;
282
283 case OP_JUMP:
284 case OP_JUMP_1:
285 case OP_JUMP_2:
286 case OP_JUMP_3:
287 case OP_JUMP_4:
288 case OP_JUMP_5:
289 case OP_JUMP_6:
290 case OP_JUMP_7:
291 case OP_JUMP_8:
292 case OP_JUMP_9:
293 mutt_error(_("Jumping is not implemented for dialogs"));
294 continue;
295
296 case OP_SEARCH:
297 case OP_SEARCH_NEXT:
298 case OP_SEARCH_OPPOSITE:
299 case OP_SEARCH_REVERSE:
300 mutt_error(_("Search is not implemented for this menu"));
301 continue;
302 }
303
304 (void) menu_function_dispatcher(menu->win, op);
305 } while (choice == 0);
306 // ---------------------------------------------------------------------------
307
308 window_set_focus(old_focus);
309 simple_dialog_free(&dlg);
310
311 return choice;
312}
#define ARRAY_FOREACH(elem, head)
Iterate over all elements of the array.
Definition: array.h:212
#define ARRAY_SIZE(head)
The number of elements stored.
Definition: array.h:87
#define ARRAY_GET(head, idx)
Return the element at index.
Definition: array.h:109
size_t buf_addstr(struct Buffer *buf, const char *s)
Add a string to a Buffer.
Definition: buffer.c:225
static const char * buf_string(const struct Buffer *buf)
Convert a buffer to a const char * "string".
Definition: buffer.h:96
Color and attribute parsing.
@ MT_COLOR_PROMPT
Question/user input.
Definition: color.h:62
size_t mutt_strnwidth(const char *s, size_t n)
Measure a string's width in screen cells.
Definition: curs_lib.c:456
struct KeyEvent mutt_getch(GetChFlags flags)
Read a character from the input buffer.
Definition: get.c:209
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:463
int menu_function_dispatcher(struct MuttWindow *win, int op)
Perform a Menu function - Implements function_dispatcher_t -.
Definition: functions.c:318
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 int cert_make_entry(struct Menu *menu, int line, int max_cols, struct Buffer *buf)
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:52
@ 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
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:36
#define OP_ABORT
$abort_key pressed (Ctrl-G)
Definition: opcodes.h:37
@ OP_MAX
Definition: opcodes.h:1030
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.
String manipulation buffer.
Definition: buffer.h:36
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:81
int op
Function opcode, e.g. OP_HELP.
Definition: lib.h:83
int ch
Raw key pressed.
Definition: lib.h:82
Mapping between user-readable string and a constant.
Definition: mapping.h:33
Definition: lib.h:79
struct MuttWindow * win
Window holding the Menu.
Definition: lib.h:86
int current
Current entry.
Definition: lib.h:80
void(* mdata_free)(struct Menu *menu, void **ptr)
Definition: lib.h:161
int(* make_entry)(struct Menu *menu, int line, int max_cols, struct Buffer *buf)
Definition: lib.h:106
void * mdata
Private data.
Definition: lib.h:147
int max
Number of entries in the menu.
Definition: lib.h:81
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