NeoMutt  2025-12-11-911-gd8d604
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
dlg_smime.c
Go to the documentation of this file.
1
22
56
57#include "config.h"
58#include <stdbool.h>
59#include <stdio.h>
60#include "private.h"
61#include "mutt/lib.h"
62#include "core/lib.h"
63#include "gui/lib.h"
64#include "lib.h"
65#include "key/lib.h"
66#include "menu/lib.h"
67#include "module_data.h"
68#include "mutt_logging.h"
69#include "smime.h"
70#include "smime_functions.h"
71
73static const struct Mapping SmimeHelp[] = {
74 // clang-format off
75 { N_("Exit"), OP_EXIT },
76 { N_("Select"), OP_GENERIC_SELECT_ENTRY },
77 { N_("Help"), OP_HELP },
78 { NULL, 0 },
79 // clang-format on
80};
81
89static char *smime_key_flags(KeyFlags flags)
90{
91 static char buf[3];
92
93 if (!(flags & KEYFLAG_CANENCRYPT))
94 buf[0] = '-';
95 else
96 buf[0] = 'e';
97
98 if (!(flags & KEYFLAG_CANSIGN))
99 buf[1] = '-';
100 else
101 buf[1] = 's';
102
103 buf[2] = '\0';
104
105 return buf;
106}
107
111static int smime_make_entry(struct Menu *menu, int line, int max_cols, struct Buffer *buf)
112{
113 struct SmimeData *sd = menu->mdata;
114 struct SmimeKey **pkey = ARRAY_GET(sd->ska, line);
115 if (!pkey)
116 return 0;
117
118 char *truststate = NULL;
119 switch ((*pkey)->trust)
120 {
121 case 'e':
122 /* L10N: Describes the trust state of a S/MIME key.
123 This translation must be padded with spaces to the right such that it
124 has the same length as the other translations.
125 The translation strings which need to be padded are:
126 Expired, Invalid, Revoked, Trusted, Unverified, Verified, and Unknown. */
127 truststate = _("Expired ");
128 break;
129 case 'i':
130 /* L10N: Describes the trust state of a S/MIME key.
131 This translation must be padded with spaces to the right such that it
132 has the same length as the other translations.
133 The translation strings which need to be padded are:
134 Expired, Invalid, Revoked, Trusted, Unverified, Verified, and Unknown. */
135 truststate = _("Invalid ");
136 break;
137 case 'r':
138 /* L10N: Describes the trust state of a S/MIME key.
139 This translation must be padded with spaces to the right such that it
140 has the same length as the other translations.
141 The translation strings which need to be padded are:
142 Expired, Invalid, Revoked, Trusted, Unverified, Verified, and Unknown. */
143 truststate = _("Revoked ");
144 break;
145 case 't':
146 /* L10N: Describes the trust state of a S/MIME key.
147 This translation must be padded with spaces to the right such that it
148 has the same length as the other translations.
149 The translation strings which need to be padded are:
150 Expired, Invalid, Revoked, Trusted, Unverified, Verified, and Unknown. */
151 truststate = _("Trusted ");
152 break;
153 case 'u':
154 /* L10N: Describes the trust state of a S/MIME key.
155 This translation must be padded with spaces to the right such that it
156 has the same length as the other translations.
157 The translation strings which need to be padded are:
158 Expired, Invalid, Revoked, Trusted, Unverified, Verified, and Unknown. */
159 truststate = _("Unverified");
160 break;
161 case 'v':
162 /* L10N: Describes the trust state of a S/MIME key.
163 This translation must be padded with spaces to the right such that it
164 has the same length as the other translations.
165 The translation strings which need to be padded are:
166 Expired, Invalid, Revoked, Trusted, Unverified, Verified, and Unknown. */
167 truststate = _("Verified ");
168 break;
169 default:
170 /* L10N: Describes the trust state of a S/MIME key.
171 This translation must be padded with spaces to the right such that it
172 has the same length as the other translations.
173 The translation strings which need to be padded are:
174 Expired, Invalid, Revoked, Trusted, Unverified, Verified, and Unknown. */
175 truststate = _("Unknown ");
176 }
177
178 int bytes = buf_printf(buf, " 0x%s %s %s %-35.35s %s", (*pkey)->hash,
179 smime_key_flags((*pkey)->flags), truststate,
180 (*pkey)->email, (*pkey)->label);
181 if (bytes < 0)
182 bytes = 0;
183
184 return mutt_strnwidth(buf_string(buf), bytes);
185}
186
195struct SmimeKey *dlg_smime(struct SmimeKey *keys, const char *query)
196{
197 struct SmimeKeyArray ska = ARRAY_HEAD_INITIALIZER;
198 for (struct SmimeKey *key = keys; key; key = key->next)
199 {
200 ARRAY_ADD(&ska, key);
201 }
202 /* sorting keys might be done later - TODO */
203
205 ASSERT(mod_data);
206
207 struct SimpleDialogWindows sdw = simple_dialog_new(mod_data->menu_smime,
209 struct Menu *menu = sdw.menu;
210
211 struct SmimeData sd = { false, menu, &ska, NULL };
212
213 menu->max = ARRAY_SIZE(&ska);
215 menu->mdata = &sd;
216 menu->mdata_free = NULL; // Menu doesn't own the data
217
218 char title[256] = { 0 };
219 snprintf(title, sizeof(title), _("S/MIME certificates matching \"%s\""), query);
220 sbar_set_title(sdw.sbar, title);
221
223
224 struct MuttWindow *old_focus = window_set_focus(menu->win);
225 // ---------------------------------------------------------------------------
226 // Event Loop
227 int op = OP_NULL;
228 struct KeyEvent event = { 0, OP_NULL };
229 do
230 {
231 menu_tagging_dispatcher(menu->win, &event);
232 window_redraw(NULL);
233
234 event = km_dokey(mod_data->menu_smime, GETCH_NONE);
235 op = event.op;
236 mutt_debug(LL_DEBUG1, "Got op %s (%d)\n", opcodes_get_name(op), op);
237 if (op < 0)
238 continue;
239 if (op == OP_NULL)
240 {
241 km_error_key(mod_data->menu_smime);
242 continue;
243 }
245
246 int rc = smime_function_dispatcher(sdw.dlg, &event);
247
248 if (rc == FR_UNKNOWN)
249 rc = menu_function_dispatcher(menu->win, &event);
250 if (rc == FR_UNKNOWN)
251 rc = global_function_dispatcher(menu->win, &event);
252 } while (!sd.done);
253
254 window_set_focus(old_focus);
256 return sd.key;
257}
#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_GET(head, idx)
Return the element at index.
Definition array.h:109
#define ARRAY_HEAD_INITIALIZER
Static initializer for arrays.
Definition array.h:58
int buf_printf(struct Buffer *buf, const char *fmt,...)
Format a string overwriting a Buffer.
Definition buffer.c:161
static const char * buf_string(const struct Buffer *buf)
Convert a buffer to a const char * "string".
Definition buffer.h:96
Convenience wrapper for the core headers.
size_t mutt_strnwidth(const char *s, size_t n)
Measure a string's width in screen cells.
Definition curs_lib.c:459
@ FR_UNKNOWN
Unknown function.
Definition dispatcher.h:34
static const struct Mapping SmimeHelp[]
Help Bar for the Smime key selection dialog.
Definition dlg_smime.c:73
static char * smime_key_flags(KeyFlags flags)
Turn SMIME key flags into a string.
Definition dlg_smime.c:89
struct KeyEvent km_dokey(const struct MenuDefinition *md, GetChFlags flags)
Determine what a keypress should do.
Definition get.c:518
void km_error_key(const struct MenuDefinition *md)
Handle an unbound key sequence.
Definition get.c:328
@ GETCH_NONE
No flags are set.
Definition get.h:38
int smime_function_dispatcher(struct MuttWindow *win, const struct KeyEvent *event)
Perform a Smime 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:239
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:366
struct SmimeKey * dlg_smime(struct SmimeKey *keys, const char *query)
Get the user to select a key -.
Definition dlg_smime.c:195
#define mutt_debug(LEVEL,...)
Definition logging2.h:91
static int smime_make_entry(struct Menu *menu, int line, int max_cols, struct Buffer *buf)
Format an S/MIME Key for the Menu - Implements Menu::make_entry() -.
Definition dlg_smime.c:111
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_DEBUG1
Log at debug level 1.
Definition logging2.h:45
GUI present the user with a selectable list.
@ MODULE_ID_NCRYPT
ModuleNcrypt, Ncrypt
Definition module_api.h:80
Convenience wrapper for the library headers.
#define N_(a)
Definition message.h:32
#define _(a)
Definition message.h:28
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_SMIME
Smime Dialog, dlg_smime()
Definition mutt_window.h:92
API for encryption/signing of emails.
uint16_t KeyFlags
Definition lib.h:159
@ KEYFLAG_CANSIGN
Key is suitable for signing.
Definition lib.h:147
@ KEYFLAG_CANENCRYPT
Key is suitable for encryption.
Definition lib.h:148
Ncrypt private Module data.
Shared constants/structs that are private to libconn.
void * neomutt_get_module_data(struct NeoMutt *n, enum ModuleId id)
Get the private data for a Module.
Definition neomutt.c:663
const char * opcodes_get_name(int op)
Get the name of an opcode.
Definition opcodes.c:48
void sbar_set_title(struct MuttWindow *win, const char *title)
Set the title for the Simple Bar.
Definition sbar.c:227
#define ASSERT(COND)
Definition signal2.h:59
SMIME helper routines.
Smime functions.
String manipulation buffer.
Definition buffer.h:36
An event such as a keypress.
Definition get.h:75
int op
Function opcode, e.g. OP_HELP.
Definition get.h:77
Mapping between user-readable string and a constant.
Definition mapping.h:33
Definition lib.h:86
struct MuttWindow * win
Window holding the Menu.
Definition lib.h:94
void(* mdata_free)(struct Menu *menu, void **ptr)
Definition lib.h:169
int(* make_entry)(struct Menu *menu, int line, int max_cols, struct Buffer *buf)
Definition lib.h:114
void * mdata
Private data.
Definition lib.h:155
int max
Number of entries in the menu.
Definition lib.h:88
Ncrypt private Module data.
Definition module_data.h:38
struct MenuDefinition * menu_smime
S/MIME menu definition.
Definition module_data.h:41
Container for Accounts, Notifications.
Definition neomutt.h:41
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
Data to pass to the Smime Functions.
struct SmimeKey * key
Selected Key.
bool done
Should we close the Dialog?
struct Menu * menu
Smime Menu.
struct SmimeKeyArray * ska
Array of Keys.
An SIME key.
Definition smime.h:43
struct SmimeKey * next
Linked list.
Definition smime.h:50