NeoMutt  2025-12-11-694-ga89709
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
dlg_gpgme.c
Go to the documentation of this file.
1
25
70
71#include "config.h"
72#include <stdbool.h>
73#include <stdio.h>
74#include "private.h"
75#include "mutt/lib.h"
76#include "address/lib.h"
77#include "config/lib.h"
78#include "core/lib.h"
79#include "gui/lib.h"
80#include "lib.h"
81#include "expando/lib.h"
82#include "key/lib.h"
83#include "menu/lib.h"
84#include "crypt_gpgme.h"
85#include "expando_gpgme.h"
86#include "gpgme_functions.h"
87#include "mutt_logging.h"
88#include "pgp_functions.h"
89#include "smime_functions.h"
90#include "sort.h"
91
93static const struct Mapping GpgmeHelp[] = {
94 // clang-format off
95 { N_("Exit"), OP_EXIT },
96 { N_("Select"), OP_GENERIC_SELECT_ENTRY },
97 { N_("Check key"), OP_VERIFY_KEY },
98 { N_("Help"), OP_HELP },
99 { NULL, 0 },
100 // clang-format on
101};
102
108static int crypt_make_entry(struct Menu *menu, int line, int max_cols, struct Buffer *buf)
109{
110 struct GpgmeData *gd = menu->mdata;
111 struct CryptKeyInfo **pinfo = ARRAY_GET(gd->key_table, line);
112 if (!pinfo)
113 return 0;
114
115 struct CryptEntry entry = { line + 1, *pinfo };
116
117 const bool c_arrow_cursor = cs_subset_bool(menu->sub, "arrow_cursor");
118 if (c_arrow_cursor)
119 {
120 const char *const c_arrow_string = cs_subset_string(menu->sub, "arrow_string");
121 if (max_cols > 0)
122 max_cols -= (mutt_strwidth(c_arrow_string) + 1);
123 }
124
125 const struct Expando *c_pgp_entry_format = cs_subset_expando(NeoMutt->sub, "pgp_entry_format");
126 return expando_filter(c_pgp_entry_format, PgpEntryGpgmeRenderCallbacks, &entry,
127 MUTT_FORMAT_ARROWCURSOR, max_cols, NeoMutt->env, buf);
128}
129
134{
135 if (nc->event_type != NT_CONFIG)
136 return 0;
137 if (!nc->global_data || !nc->event_data)
138 return -1;
139
140 struct EventConfig *ev_c = nc->event_data;
141
142 if (!mutt_str_equal(ev_c->name, "pgp_entry_format") &&
143 !mutt_str_equal(ev_c->name, "pgp_key_sort"))
144 {
145 return 0;
146 }
147
148 struct Menu *menu = nc->global_data;
150 mutt_debug(LL_DEBUG5, "config done, request WA_RECALC, MENU_REDRAW_FULL\n");
151
152 return 0;
153}
154
163{
164 if (nc->event_type != NT_WINDOW)
165 return 0;
166 if (!nc->global_data || !nc->event_data)
167 return -1;
169 return 0;
170
171 struct MuttWindow *win_menu = nc->global_data;
172 struct EventWindow *ev_w = nc->event_data;
173 if (ev_w->win != win_menu)
174 return 0;
175
176 struct Menu *menu = win_menu->wdata;
177
180
181 mutt_debug(LL_DEBUG5, "window delete done\n");
182 return 0;
183}
184
196struct CryptKeyInfo *dlg_gpgme(struct CryptKeyInfo *keys, struct Address *p,
197 const char *s, unsigned int app, bool *forced_valid)
198{
199 /* build the key table */
200 struct CryptKeyInfoArray ckia = ARRAY_HEAD_INITIALIZER;
201 const bool c_pgp_show_unusable = cs_subset_bool(NeoMutt->sub, "pgp_show_unusable");
202 bool unusable = false;
203 for (struct CryptKeyInfo *k = keys; k; k = k->next)
204 {
205 if (!c_pgp_show_unusable && (k->flags & KEYFLAG_CANTUSE))
206 {
207 unusable = true;
208 continue;
209 }
210
211 ARRAY_ADD(&ckia, k);
212 }
213
214 if ((ARRAY_SIZE(&ckia) == 0) && unusable)
215 {
216 mutt_error(_("All matching keys are marked expired/revoked"));
217 return NULL;
218 }
219
220 gpgme_sort_keys(&ckia);
221
222 struct MenuDefinition *md = NULL;
223 if (app & APPLICATION_PGP)
224 md = MdPgp;
225 else if (app & APPLICATION_SMIME)
226 md = MdSmime;
227
229
230 struct Menu *menu = sdw.menu;
231 struct GpgmeData gd = { false, menu, &ckia, NULL, forced_valid };
232
233 menu->max = ARRAY_SIZE(&ckia);
235 menu->mdata = &gd;
236 menu->mdata_free = NULL; // Menu doesn't own the data
237
238 // NT_COLOR is handled by the SimpleDialog
241
242 const char *ts = NULL;
243
244 if ((app & APPLICATION_PGP) && (app & APPLICATION_SMIME))
245 ts = _("PGP and S/MIME keys matching");
246 else if ((app & APPLICATION_PGP))
247 ts = _("PGP keys matching");
248 else if ((app & APPLICATION_SMIME))
249 ts = _("S/MIME keys matching");
250 else
251 ts = _("keys matching");
252
253 char buf[1024] = { 0 };
254 if (p)
255 {
256 /* L10N: 1$s is one of the previous four entries.
257 %2$s is an address.
258 e.g. "S/MIME keys matching <john.doe@example.com>" */
259 snprintf(buf, sizeof(buf), _("%s <%s>"), ts, buf_string(p->mailbox));
260 }
261 else
262 {
263 /* L10N: e.g. 'S/MIME keys matching "John Doe".' */
264 snprintf(buf, sizeof(buf), _("%s \"%s\""), ts, s);
265 }
266
267 sbar_set_title(sdw.sbar, buf);
268
270
271 struct MuttWindow *old_focus = window_set_focus(menu->win);
272 // ---------------------------------------------------------------------------
273 // Event Loop
274 int op = OP_NULL;
275 struct KeyEvent event = { 0, OP_NULL };
276 do
277 {
278 menu_tagging_dispatcher(menu->win, &event);
279 window_redraw(NULL);
280
281 event = km_dokey(md, GETCH_NO_FLAGS);
282 op = event.op;
283 mutt_debug(LL_DEBUG1, "Got op %s (%d)\n", opcodes_get_name(op), op);
284 if (op < 0)
285 continue;
286 if (op == OP_NULL)
287 {
288 km_error_key(md);
289 continue;
290 }
292
293 int rc = gpgme_function_dispatcher(sdw.dlg, &event);
294
295 if (rc == FR_UNKNOWN)
296 rc = menu_function_dispatcher(menu->win, &event);
297 if (rc == FR_UNKNOWN)
298 rc = global_function_dispatcher(menu->win, &event);
299 } while (!gd.done);
300 // ---------------------------------------------------------------------------
301
302 ARRAY_FREE(&ckia);
303 window_set_focus(old_focus);
305 return gd.key;
306}
Email Address Handling.
#define ARRAY_ADD(head, elem)
Add an element at the end of the array.
Definition array.h:157
#define ARRAY_SIZE(head)
The number of elements stored.
Definition array.h:87
#define ARRAY_FREE(head)
Release all memory.
Definition array.h:209
#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.
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:444
@ FR_UNKNOWN
Unknown function.
Definition dispatcher.h:34
static const struct Mapping GpgmeHelp[]
Help Bar for the GPGME key selection dialog.
Definition dlg_gpgme.c:93
int expando_filter(const struct Expando *exp, const struct ExpandoRenderCallback *erc, void *data, MuttFormatFlags flags, int max_cols, char **env_list, struct Buffer *buf)
Render an Expando and run the result through a filter.
Definition filter.c:139
Parse Expando string.
const struct ExpandoRenderCallback PgpEntryGpgmeRenderCallbacks[]
Callbacks for GPGME Key Expandos.
Ncrypt GPGME Expando definitions.
struct KeyEvent km_dokey(const struct MenuDefinition *md, GetChFlags flags)
Determine what a keypress should do.
Definition get.c:434
void km_error_key(const struct MenuDefinition *md)
Handle an unbound key sequence.
Definition get.c:285
#define GETCH_NO_FLAGS
No flags are set.
Definition get.h:34
Gpgme functions.
int gpgme_function_dispatcher(struct MuttWindow *win, const struct KeyEvent *event)
Perform a Gpgme function - Implements function_dispatcher_t -.
int menu_tagging_dispatcher(struct MuttWindow *win, const struct KeyEvent *event)
Perform tagging operations on the Menu - Implements function_dispatcher_t -.
Definition tagging.c:230
int global_function_dispatcher(struct MuttWindow *win, const struct KeyEvent *event)
Perform a Global function - Implements function_dispatcher_t -.
Definition global.c:182
int menu_function_dispatcher(struct MuttWindow *win, const struct KeyEvent *event)
Perform a Menu function - Implements function_dispatcher_t -.
Definition functions.c:320
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:196
#define mutt_error(...)
Definition logging2.h:94
#define mutt_debug(LEVEL,...)
Definition logging2.h:91
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:108
static int gpgme_key_config_observer(struct NotifyCallback *nc)
Notification that a Config Variable has changed - Implements observer_t -.
Definition dlg_gpgme.c:133
static int gpgme_key_window_observer(struct NotifyCallback *nc)
Notification that a Window has changed - Implements observer_t -.
Definition dlg_gpgme.c:162
Convenience wrapper for the gui headers.
void simple_dialog_free(struct MuttWindow **ptr)
Destroy a simple index Dialog.
Definition simple.c:169
struct SimpleDialogWindows simple_dialog_new(const struct MenuDefinition *md, enum WindowType wtype, const struct Mapping *help_data)
Create a simple index Dialog.
Definition simple.c:132
Manage keymappings.
@ LL_DEBUG5
Log at debug level 5.
Definition logging2.h:49
@ LL_DEBUG1
Log at debug level 1.
Definition logging2.h:45
GUI present the user with a selectable list.
#define MENU_REDRAW_FULL
Redraw everything.
Definition lib.h:60
void menu_queue_redraw(struct Menu *menu, MenuRedrawFlags redraw)
Queue a request for a redraw.
Definition menu.c:188
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:665
void mutt_clear_error(void)
Clear the message line (bottom line of screen)
NeoMutt Logging.
void window_redraw(struct MuttWindow *win)
Reflow, recalc and repaint a tree of Windows.
struct MuttWindow * window_set_focus(struct MuttWindow *win)
Set the Window focus.
@ WT_DLG_GPGME
GPGME Dialog, dlg_gpgme()
Definition mutt_window.h:83
@ NT_WINDOW_DELETE
Window is about to be deleted.
struct MenuDefinition * MdPgp
Pgp Menu Definition.
Definition functions.c:37
struct MenuDefinition * MdSmime
Smime Menu Definition.
Definition functions.c:40
API for encryption/signing of emails.
#define APPLICATION_PGP
Use PGP to encrypt/sign.
Definition lib.h:98
#define KEYFLAG_CANTUSE
Definition lib.h:147
#define APPLICATION_SMIME
Use SMIME to encrypt/sign.
Definition lib.h:99
Shared constants/structs that are private to libconn.
Crypto Key sorting functions.
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
Pgp functions.
#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
Smime functions.
An email address.
Definition address.h:35
struct Buffer * mailbox
Mailbox and host address.
Definition address.h:37
String manipulation buffer.
Definition buffer.h:36
struct Notify * notify
Notifications: NotifyConfig, EventConfig.
Definition subset.h:51
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:70
const char * name
Name of config item that changed.
Definition subset.h:72
An Event that happened to a Window.
struct MuttWindow * win
Window that changed.
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.
An event such as a keypress.
Definition get.h:50
int op
Function opcode, e.g. OP_HELP.
Definition get.h:52
Mapping between user-readable string and a constant.
Definition mapping.h:33
Functions for a Dialog or Window.
Definition menu.h:80
Definition lib.h:80
struct MuttWindow * win
Window holding the Menu.
Definition lib.h:88
void(* mdata_free)(struct Menu *menu, void **ptr)
Definition lib.h:163
int(* make_entry)(struct Menu *menu, int line, int max_cols, struct Buffer *buf)
Definition lib.h:108
struct ConfigSubset * sub
Inherited config items.
Definition lib.h:89
void * mdata
Private data.
Definition lib.h:149
int max
Number of entries in the menu.
Definition lib.h:82
void * wdata
Private data.
struct Notify * notify
Notifications: NotifyWindow, EventWindow.
Container for Accounts, Notifications.
Definition neomutt.h:41
char ** env
Private copy of the environment variables.
Definition neomutt.h:58
struct ConfigSubset * sub
Inherited config items.
Definition neomutt.h:49
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