NeoMutt  2024-12-12-14-g7b49f7
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
dlg_gpgme.c
Go to the documentation of this file.
1
71#include "config.h"
72#include <locale.h>
73#include <stdbool.h>
74#include <stdio.h>
75#include "private.h"
76#include "mutt/lib.h"
77#include "address/lib.h"
78#include "config/lib.h"
79#include "core/lib.h"
80#include "gui/lib.h"
81#include "lib.h"
82#include "expando/lib.h"
83#include "key/lib.h"
84#include "menu/lib.h"
85#include "crypt_gpgme.h"
86#include "expando_gpgme.h"
87#include "gpgme_functions.h"
88#include "mutt_logging.h"
89#include "sort.h"
90
92static const struct Mapping GpgmeHelp[] = {
93 // clang-format off
94 { N_("Exit"), OP_EXIT },
95 { N_("Select"), OP_GENERIC_SELECT_ENTRY },
96 { N_("Check key"), OP_VERIFY_KEY },
97 { N_("Help"), OP_HELP },
98 { NULL, 0 },
99 // clang-format on
100};
101
107static int crypt_make_entry(struct Menu *menu, int line, int max_cols, struct Buffer *buf)
108{
109 struct GpgmeData *gd = menu->mdata;
110 struct CryptKeyInfo **pinfo = ARRAY_GET(gd->key_table, line);
111 if (!pinfo)
112 return 0;
113
114 struct CryptEntry entry = { line + 1, *pinfo };
115
116 const bool c_arrow_cursor = cs_subset_bool(menu->sub, "arrow_cursor");
117 if (c_arrow_cursor)
118 {
119 const char *const c_arrow_string = cs_subset_string(menu->sub, "arrow_string");
120 if (max_cols > 0)
121 max_cols -= (mutt_strwidth(c_arrow_string) + 1);
122 }
123
124 const struct Expando *c_pgp_entry_format = cs_subset_expando(NeoMutt->sub, "pgp_entry_format");
125 return expando_filter(c_pgp_entry_format, PgpEntryGpgmeRenderCallbacks,
126 &entry, MUTT_FORMAT_ARROWCURSOR, max_cols, buf);
127}
128
133{
134 if (nc->event_type != NT_CONFIG)
135 return 0;
136 if (!nc->global_data || !nc->event_data)
137 return -1;
138
139 struct EventConfig *ev_c = nc->event_data;
140
141 if (!mutt_str_equal(ev_c->name, "pgp_entry_format") &&
142 !mutt_str_equal(ev_c->name, "pgp_key_sort"))
143 {
144 return 0;
145 }
146
147 struct Menu *menu = nc->global_data;
149 mutt_debug(LL_DEBUG5, "config done, request WA_RECALC, MENU_REDRAW_FULL\n");
150
151 return 0;
152}
153
162{
163 if (nc->event_type != NT_WINDOW)
164 return 0;
165 if (!nc->global_data || !nc->event_data)
166 return -1;
168 return 0;
169
170 struct MuttWindow *win_menu = nc->global_data;
171 struct EventWindow *ev_w = nc->event_data;
172 if (ev_w->win != win_menu)
173 return 0;
174
175 struct Menu *menu = win_menu->wdata;
176
179
180 mutt_debug(LL_DEBUG5, "window delete done\n");
181 return 0;
182}
183
195struct CryptKeyInfo *dlg_gpgme(struct CryptKeyInfo *keys, struct Address *p,
196 const char *s, unsigned int app, bool *forced_valid)
197{
198 /* build the key table */
199 struct CryptKeyInfoArray ckia = ARRAY_HEAD_INITIALIZER;
200 const bool c_pgp_show_unusable = cs_subset_bool(NeoMutt->sub, "pgp_show_unusable");
201 bool unusable = false;
202 for (struct CryptKeyInfo *k = keys; k; k = k->next)
203 {
204 if (!c_pgp_show_unusable && (k->flags & KEYFLAG_CANTUSE))
205 {
206 unusable = true;
207 continue;
208 }
209
210 ARRAY_ADD(&ckia, k);
211 }
212
213 if ((ARRAY_SIZE(&ckia) == 0) && unusable)
214 {
215 mutt_error(_("All matching keys are marked expired/revoked"));
216 return NULL;
217 }
218
219 gpgme_sort_keys(&ckia);
220
221 enum MenuType menu_to_use = MENU_GENERIC;
222 if (app & APPLICATION_PGP)
223 menu_to_use = MENU_KEY_SELECT_PGP;
224 else if (app & APPLICATION_SMIME)
225 menu_to_use = MENU_KEY_SELECT_SMIME;
226
228
229 struct Menu *menu = sdw.menu;
230 struct GpgmeData gd = { false, menu, &ckia, NULL, forced_valid };
231
232 menu->max = ARRAY_SIZE(&ckia);
234 menu->mdata = &gd;
235 menu->mdata_free = NULL; // Menu doesn't own the data
236
237 // NT_COLOR is handled by the SimpleDialog
240
241 const char *ts = NULL;
242
243 if ((app & APPLICATION_PGP) && (app & APPLICATION_SMIME))
244 ts = _("PGP and S/MIME keys matching");
245 else if ((app & APPLICATION_PGP))
246 ts = _("PGP keys matching");
247 else if ((app & APPLICATION_SMIME))
248 ts = _("S/MIME keys matching");
249 else
250 ts = _("keys matching");
251
252 char buf[1024] = { 0 };
253 if (p)
254 {
255 /* L10N: 1$s is one of the previous four entries.
256 %2$s is an address.
257 e.g. "S/MIME keys matching <john.doe@example.com>" */
258 snprintf(buf, sizeof(buf), _("%s <%s>"), ts, buf_string(p->mailbox));
259 }
260 else
261 {
262 /* L10N: e.g. 'S/MIME keys matching "John Doe".' */
263 snprintf(buf, sizeof(buf), _("%s \"%s\""), ts, s);
264 }
265
266 sbar_set_title(sdw.sbar, buf);
267
269
270 struct MuttWindow *old_focus = window_set_focus(menu->win);
271 // ---------------------------------------------------------------------------
272 // Event Loop
273 int op = OP_NULL;
274 do
275 {
276 menu_tagging_dispatcher(menu->win, op);
277 window_redraw(NULL);
278
279 op = km_dokey(menu_to_use, GETCH_NO_FLAGS);
280 mutt_debug(LL_DEBUG1, "Got op %s (%d)\n", opcodes_get_name(op), op);
281 if (op < 0)
282 continue;
283 if (op == OP_NULL)
284 {
285 km_error_key(menu_to_use);
286 continue;
287 }
289
290 int rc = gpgme_function_dispatcher(sdw.dlg, op);
291
292 if (rc == FR_UNKNOWN)
293 rc = menu_function_dispatcher(menu->win, op);
294 if (rc == FR_UNKNOWN)
295 rc = global_function_dispatcher(NULL, op);
296 } while (!gd.done);
297 // ---------------------------------------------------------------------------
298
299 ARRAY_FREE(&ckia);
300 window_set_focus(old_focus);
302 return gd.key;
303}
Email Address Handling.
#define ARRAY_ADD(head, elem)
Add an element at the end of the array.
Definition: array.h:156
#define ARRAY_SIZE(head)
The number of elements stored.
Definition: array.h:87
#define ARRAY_FREE(head)
Release all memory.
Definition: array.h:204
#define ARRAY_GET(head, idx)
Return the element at index.
Definition: array.h:109
#define ARRAY_HEAD_INITIALIZER
Static initializer for arrays.
Definition: array.h:58
static const char * buf_string(const struct Buffer *buf)
Convert a buffer to a const char * "string".
Definition: buffer.h:96
const char * cs_subset_string(const struct ConfigSubset *sub, const char *name)
Get a string config item by name.
Definition: helpers.c:291
bool cs_subset_bool(const struct ConfigSubset *sub, const char *name)
Get a boolean config item by name.
Definition: helpers.c:47
const struct Expando * cs_subset_expando(const struct ConfigSubset *sub, const char *name)
Get an Expando config item by name.
Definition: config_type.c:357
Convenience wrapper for the config headers.
Convenience wrapper for the core headers.
Wrapper for PGP/SMIME calls to GPGME.
size_t mutt_strwidth(const char *s)
Measure a string's width in screen cells.
Definition: curs_lib.c:443
@ FR_UNKNOWN
Unknown function.
Definition: dispatcher.h:33
static const struct Mapping GpgmeHelp[]
Help Bar for the GPGME key selection dialog.
Definition: dlg_gpgme.c:92
int expando_filter(const struct Expando *exp, const struct ExpandoRenderCallback *erc, void *data, MuttFormatFlags flags, int max_cols, struct Buffer *buf)
Render an Expando and run the result through a filter.
Definition: filter.c:138
Parse Expando string.
const struct ExpandoRenderCallback PgpEntryGpgmeRenderCallbacks[]
Callbacks for GPGME Key Expandos.
Ncrypt GPGME Expando definitions.
int km_dokey(enum MenuType mtype, GetChFlags flags)
Determine what a keypress should do.
Definition: get.c:464
void km_error_key(enum MenuType mtype)
Handle an unbound key sequence.
Definition: get.c:294
Gpgme functions.
int gpgme_function_dispatcher(struct MuttWindow *win, int op)
Perform a Gpgme function - Implements function_dispatcher_t -.
int menu_tagging_dispatcher(struct MuttWindow *win, int op)
Perform tagging operations on the Menu - Implements function_dispatcher_t -.
Definition: tagging.c:230
int global_function_dispatcher(struct MuttWindow *win, int op)
Perform a Global function - Implements function_dispatcher_t -.
Definition: global.c:172
int menu_function_dispatcher(struct MuttWindow *win, int op)
Perform a Menu function - Implements function_dispatcher_t -.
Definition: functions.c:318
struct CryptKeyInfo * dlg_gpgme(struct CryptKeyInfo *keys, struct Address *p, const char *s, unsigned int app, bool *forced_valid)
Get the user to select a key -.
Definition: dlg_gpgme.c:195
#define mutt_error(...)
Definition: logging2.h:92
#define mutt_debug(LEVEL,...)
Definition: logging2.h:89
static int crypt_make_entry(struct Menu *menu, int line, int max_cols, struct Buffer *buf)
Format a PGP Key for the Menu - Implements Menu::make_entry() -.
Definition: dlg_gpgme.c:107
static int gpgme_key_config_observer(struct NotifyCallback *nc)
Notification that a Config Variable has changed - Implements observer_t -.
Definition: dlg_gpgme.c:132
static int gpgme_key_window_observer(struct NotifyCallback *nc)
Notification that a Window has changed - Implements observer_t -.
Definition: dlg_gpgme.c:161
Convenience wrapper for the gui headers.
void simple_dialog_free(struct MuttWindow **ptr)
Destroy a simple index Dialog.
Definition: simple.c:168
struct SimpleDialogWindows 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_NO_FLAGS
No flags are set.
Definition: lib.h:51
@ LL_DEBUG5
Log at debug level 5.
Definition: logging2.h:47
@ LL_DEBUG1
Log at debug level 1.
Definition: logging2.h:43
GUI present the user with a selectable list.
#define MENU_REDRAW_FULL
Redraw everything.
Definition: lib.h:59
void menu_queue_redraw(struct Menu *menu, MenuRedrawFlags redraw)
Queue a request for a redraw.
Definition: menu.c:184
Convenience wrapper for the library headers.
#define N_(a)
Definition: message.h:32
#define _(a)
Definition: message.h:28
bool notify_observer_remove(struct Notify *notify, const observer_t callback, const void *global_data)
Remove an observer from an object.
Definition: notify.c:230
bool notify_observer_add(struct Notify *notify, enum NotifyType type, observer_t callback, void *global_data)
Add an observer to an object.
Definition: notify.c:191
bool mutt_str_equal(const char *a, const char *b)
Compare two strings.
Definition: string.c:660
void mutt_clear_error(void)
Clear the message line (bottom line of screen)
Definition: mutt_logging.c:74
NeoMutt Logging.
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
@ WT_DLG_GPGME
GPGME Dialog, dlg_gpgme()
Definition: mutt_window.h:84
@ NT_WINDOW_DELETE
Window is about to be deleted.
Definition: mutt_window.h:229
#define APPLICATION_PGP
Use PGP to encrypt/sign.
Definition: lib.h:96
#define KEYFLAG_CANTUSE
Definition: lib.h:145
#define APPLICATION_SMIME
Use SMIME to encrypt/sign.
Definition: lib.h:97
void gpgme_sort_keys(struct CryptKeyInfoArray *ckia)
Sort an array of GPGME keys.
Definition: sort_gpgme.c:175
@ NT_WINDOW
MuttWindow has changed, NotifyWindow, EventWindow.
Definition: notify_type.h:57
@ NT_CONFIG
Config has changed, NotifyConfig, EventConfig.
Definition: notify_type.h:43
const char * opcodes_get_name(int op)
Get the name of an opcode.
Definition: opcodes.c:48
#define MUTT_FORMAT_ARROWCURSOR
Reserve space for arrow_cursor.
Definition: render.h:37
void sbar_set_title(struct MuttWindow *win, const char *title)
Set the title for the Simple Bar.
Definition: sbar.c:227
GUI display the mailboxes in a side panel.
Sidebar sorting functions.
Key value store.
An email address.
Definition: address.h:36
struct Buffer * mailbox
Mailbox and host address.
Definition: address.h:38
String manipulation buffer.
Definition: buffer.h:36
struct Notify * notify
Notifications: NotifyConfig, EventConfig.
Definition: subset.h:52
An entry in the Select-Key menu.
Definition: crypt_gpgme.h:86
A stored PGP key.
Definition: crypt_gpgme.h:44
struct CryptKeyInfo * next
Linked list.
Definition: crypt_gpgme.h:45
A config-change event.
Definition: subset.h:71
const char * name
Name of config item that changed.
Definition: subset.h:73
An Event that happened to a Window.
Definition: mutt_window.h:239
struct MuttWindow * win
Window that changed.
Definition: mutt_window.h:240
Parsed Expando trees.
Definition: expando.h:41
Data to pass to the Gpgme Functions.
struct CryptKeyInfoArray * key_table
Array of Keys.
bool * forced_valid
User insists on out-of-date key.
struct CryptKeyInfo * key
Selected Key.
bool done
Should we close the Dialog?
struct Menu * menu
Gpgme Menu.
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
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
struct ConfigSubset * sub
Inherited config items.
Definition: lib.h:87
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
struct Notify * notify
Notifications: NotifyWindow, EventWindow.
Definition: mutt_window.h:138
Container for Accounts, Notifications.
Definition: neomutt.h:42
struct ConfigSubset * sub
Inherited config items.
Definition: neomutt.h:46
Data passed to a notification function.
Definition: observer.h:34
void * event_data
Data from notify_send()
Definition: observer.h:38
enum NotifyType event_type
Send: Event type, e.g. NT_ACCOUNT.
Definition: observer.h:36
int event_subtype
Send: Event subtype, e.g. NT_ACCOUNT_ADD.
Definition: observer.h:37
void * global_data
Data from notify_observer_add()
Definition: observer.h:39
Tuple for the results of simple_dialog_new()
Definition: simple.h:35
struct MuttWindow * sbar
Simple Bar.
Definition: simple.h:37
struct Menu * menu
Menu.
Definition: simple.h:38
struct MuttWindow * dlg
Main Dialog Window.
Definition: simple.h:36
MenuType
Types of GUI selections.
Definition: type.h:36
@ MENU_KEY_SELECT_PGP
Select a PGP key.
Definition: type.h:48
@ MENU_KEY_SELECT_SMIME
Select a SMIME key.
Definition: type.h:49
@ MENU_GENERIC
Generic selection list.
Definition: type.h:46