NeoMutt  2024-04-16-36-g75b6fb
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
dlg_smime.c
Go to the documentation of this file.
1
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 "mutt_logging.h"
68#include "smime.h"
69#include "smime_functions.h"
70
72static const struct Mapping SmimeHelp[] = {
73 // clang-format off
74 { N_("Exit"), OP_EXIT },
75 { N_("Select"), OP_GENERIC_SELECT_ENTRY },
76 { N_("Help"), OP_HELP },
77 { NULL, 0 },
78 // clang-format on
79};
80
88static char *smime_key_flags(KeyFlags flags)
89{
90 static char buf[3];
91
92 if (!(flags & KEYFLAG_CANENCRYPT))
93 buf[0] = '-';
94 else
95 buf[0] = 'e';
96
97 if (!(flags & KEYFLAG_CANSIGN))
98 buf[1] = '-';
99 else
100 buf[1] = 's';
101
102 buf[2] = '\0';
103
104 return buf;
105}
106
110static int smime_make_entry(struct Menu *menu, int line, int max_cols, struct Buffer *buf)
111{
112 struct SmimeKey **table = menu->mdata;
113 struct SmimeKey *key = table[line];
114 char *truststate = NULL;
115 switch (key->trust)
116 {
117 case 'e':
118 /* L10N: Describes the trust state of a S/MIME key.
119 This translation must be padded with spaces to the right such that it
120 has the same length as the other translations.
121 The translation strings which need to be padded are:
122 Expired, Invalid, Revoked, Trusted, Unverified, Verified, and Unknown. */
123 truststate = _("Expired ");
124 break;
125 case 'i':
126 /* L10N: Describes the trust state of a S/MIME key.
127 This translation must be padded with spaces to the right such that it
128 has the same length as the other translations.
129 The translation strings which need to be padded are:
130 Expired, Invalid, Revoked, Trusted, Unverified, Verified, and Unknown. */
131 truststate = _("Invalid ");
132 break;
133 case 'r':
134 /* L10N: Describes the trust state of a S/MIME key.
135 This translation must be padded with spaces to the right such that it
136 has the same length as the other translations.
137 The translation strings which need to be padded are:
138 Expired, Invalid, Revoked, Trusted, Unverified, Verified, and Unknown. */
139 truststate = _("Revoked ");
140 break;
141 case 't':
142 /* L10N: Describes the trust state of a S/MIME key.
143 This translation must be padded with spaces to the right such that it
144 has the same length as the other translations.
145 The translation strings which need to be padded are:
146 Expired, Invalid, Revoked, Trusted, Unverified, Verified, and Unknown. */
147 truststate = _("Trusted ");
148 break;
149 case 'u':
150 /* L10N: Describes the trust state of a S/MIME key.
151 This translation must be padded with spaces to the right such that it
152 has the same length as the other translations.
153 The translation strings which need to be padded are:
154 Expired, Invalid, Revoked, Trusted, Unverified, Verified, and Unknown. */
155 truststate = _("Unverified");
156 break;
157 case 'v':
158 /* L10N: Describes the trust state of a S/MIME key.
159 This translation must be padded with spaces to the right such that it
160 has the same length as the other translations.
161 The translation strings which need to be padded are:
162 Expired, Invalid, Revoked, Trusted, Unverified, Verified, and Unknown. */
163 truststate = _("Verified ");
164 break;
165 default:
166 /* L10N: Describes the trust state of a S/MIME key.
167 This translation must be padded with spaces to the right such that it
168 has the same length as the other translations.
169 The translation strings which need to be padded are:
170 Expired, Invalid, Revoked, Trusted, Unverified, Verified, and Unknown. */
171 truststate = _("Unknown ");
172 }
173
174 int bytes = buf_printf(buf, " 0x%s %s %s %-35.35s %s", key->hash,
175 smime_key_flags(key->flags), truststate, key->email, key->label);
176 if (bytes < 0)
177 bytes = 0;
178
179 return mutt_strnwidth(buf_string(buf), bytes);
180}
181
187static void smime_key_table_free(struct Menu *menu, void **ptr)
188{
189 FREE(ptr);
190}
191
200struct SmimeKey *dlg_smime(struct SmimeKey *keys, const char *query)
201{
202 struct SmimeKey **table = NULL;
203 int table_size = 0;
204 int table_index = 0;
205 struct SmimeKey *key = NULL;
206
207 for (table_index = 0, key = keys; key; key = key->next)
208 {
209 if (table_index == table_size)
210 {
211 table_size += 5;
212 mutt_mem_realloc(&table, sizeof(struct SmimeKey *) * table_size);
213 }
214
215 table[table_index++] = key;
216 }
217
219
220 struct Menu *menu = dlg->wdata;
221 menu->max = table_index;
223 menu->mdata = table;
225 /* sorting keys might be done later - TODO */
226
227 struct SmimeData sd = { false, menu, table, NULL };
228 dlg->wdata = &sd;
229
230 char title[256] = { 0 };
231 struct MuttWindow *sbar = window_find_child(dlg, WT_STATUS_BAR);
232 snprintf(title, sizeof(title), _("S/MIME certificates matching \"%s\""), query);
233 sbar_set_title(sbar, title);
234
236
237 struct MuttWindow *old_focus = window_set_focus(menu->win);
238 // ---------------------------------------------------------------------------
239 // Event Loop
240 int op = OP_NULL;
241 do
242 {
243 menu_tagging_dispatcher(menu->win, op);
244 window_redraw(NULL);
245
247 mutt_debug(LL_DEBUG1, "Got op %s (%d)\n", opcodes_get_name(op), op);
248 if (op < 0)
249 continue;
250 if (op == OP_NULL)
251 {
253 continue;
254 }
256
257 int rc = smime_function_dispatcher(dlg, op);
258
259 if (rc == FR_UNKNOWN)
260 rc = menu_function_dispatcher(menu->win, op);
261 if (rc == FR_UNKNOWN)
262 rc = global_function_dispatcher(NULL, op);
263 } while (!sd.done);
264
265 window_set_focus(old_focus);
266 simple_dialog_free(&dlg);
267 return sd.key;
268}
int buf_printf(struct Buffer *buf, const char *fmt,...)
Format a string overwriting a Buffer.
Definition: buffer.c:160
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:456
@ FR_UNKNOWN
Unknown function.
Definition: dispatcher.h:33
static const struct Mapping SmimeHelp[]
Help Bar for the Smime key selection dialog.
Definition: dlg_smime.c:72
static char * smime_key_flags(KeyFlags flags)
Turn SMIME key flags into a string.
Definition: dlg_smime.c:88
int km_dokey(enum MenuType mtype, GetChFlags flags)
Determine what a keypress should do.
Definition: get.c:463
void km_error_key(enum MenuType mtype)
Handle an unbound key sequence.
Definition: get.c:293
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
int smime_function_dispatcher(struct MuttWindow *win, int op)
Perform a Smime function - Implements function_dispatcher_t -.
struct SmimeKey * dlg_smime(struct SmimeKey *keys, const char *query)
Get the user to select a key -.
Definition: dlg_smime.c:200
#define mutt_debug(LEVEL,...)
Definition: logging2.h:89
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:110
static void smime_key_table_free(struct Menu *menu, void **ptr)
Free the key table - Implements Menu::mdata_free() -.
Definition: dlg_smime.c:187
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_NO_FLAGS
No flags are set.
Definition: lib.h:51
@ LL_DEBUG1
Log at debug level 1.
Definition: logging2.h:43
void mutt_mem_realloc(void *ptr, size_t size)
Resize a block of memory on the heap.
Definition: memory.c:114
#define FREE(x)
Definition: memory.h:45
GUI present the user with a selectable list.
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)
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
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_SMIME
Smime Dialog, dlg_smime()
Definition: mutt_window.h:92
@ WT_STATUS_BAR
Status Bar containing extra info about the Index/Pager/etc.
Definition: mutt_window.h:102
uint16_t KeyFlags
Flags describing PGP/SMIME keys, e.g. KEYFLAG_CANSIGN.
Definition: lib.h:125
#define KEYFLAG_CANENCRYPT
Key is suitable for encryption.
Definition: lib.h:128
#define KEYFLAG_CANSIGN
Key is suitable for signing.
Definition: lib.h:127
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
GUI display the mailboxes in a side panel.
SMIME helper routines.
Smime functions.
Key value store.
String manipulation buffer.
Definition: buffer.h:36
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
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
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 SmimeKey ** table
Array of Keys.
An SIME key.
Definition: smime.h:43
KeyFlags flags
Definition: smime.h:49
char * hash
Definition: smime.h:45
struct SmimeKey * next
Definition: smime.h:50
char * email
Definition: smime.h:44
char * label
Definition: smime.h:46
char trust
i=Invalid r=revoked e=expired u=unverified v=verified t=trusted
Definition: smime.h:48
@ MENU_SMIME
SMIME encryption menu.
Definition: type.h:59