NeoMutt  2024-04-16-36-g75b6fb
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
dlg_autocrypt.c
Go to the documentation of this file.
1
69#include "config.h"
70#include <stdbool.h>
71#include <stdio.h>
72#include "private.h"
73#include "mutt/lib.h"
74#include "address/lib.h"
75#include "config/lib.h"
76#include "core/lib.h"
77#include "gui/lib.h"
78#include "lib.h"
79#include "expando/lib.h"
80#include "key/lib.h"
81#include "menu/lib.h"
82#include "functions.h"
83#include "mutt_logging.h"
84
86
88static const struct Mapping AutocryptHelp[] = {
89 // clang-format off
90 { N_("Exit"), OP_EXIT },
91 /* L10N: Autocrypt Account Menu Help line:
92 create new account */
93 { N_("Create"), OP_AUTOCRYPT_CREATE_ACCT },
94 /* L10N: Autocrypt Account Menu Help line:
95 delete account */
96 { N_("Delete"), OP_AUTOCRYPT_DELETE_ACCT },
97 /* L10N: Autocrypt Account Menu Help line:
98 toggle an account active/inactive
99 The words here are abbreviated to keep the help line compact.
100 It currently has the content:
101 q:Exit c:Create D:Delete a:Tgl Active p:Prf Encr ?:Help */
102 { N_("Tgl Active"), OP_AUTOCRYPT_TOGGLE_ACTIVE },
103 /* L10N: Autocrypt Account Menu Help line:
104 toggle "prefer-encrypt" on an account
105 The words here are abbreviated to keep the help line compact.
106 It currently has the content:
107 q:Exit c:Create D:Delete a:Tgl Active p:Prf Encr ?:Help */
108 { N_("Prf Encr"), OP_AUTOCRYPT_TOGGLE_PREFER },
109 { N_("Help"), OP_HELP },
110 { NULL, 0 }
111 // clang-format on
112};
113
117void autocrypt_a(const struct ExpandoNode *node, void *data,
118 MuttFormatFlags flags, int max_cols, struct Buffer *buf)
119{
120 const struct AccountEntry *entry = data;
121
122 buf_copy(buf, entry->addr->mailbox);
123}
124
128void autocrypt_k(const struct ExpandoNode *node, void *data,
129 MuttFormatFlags flags, int max_cols, struct Buffer *buf)
130{
131 const struct AccountEntry *entry = data;
132
133 const char *s = entry->account->keyid;
134 buf_strcpy(buf, s);
135}
136
140long autocrypt_n_num(const struct ExpandoNode *node, void *data, MuttFormatFlags flags)
141{
142 const struct AccountEntry *entry = data;
143
144 return entry->num;
145}
146
150void autocrypt_p(const struct ExpandoNode *node, void *data,
151 MuttFormatFlags flags, int max_cols, struct Buffer *buf)
152{
153 const struct AccountEntry *entry = data;
154
155 if (entry->account->prefer_encrypt)
156 {
157 /* L10N: Autocrypt Account menu.
158 flag that an account has prefer-encrypt set */
159 buf_addstr(buf, _("prefer encrypt"));
160 }
161 else
162 {
163 /* L10N: Autocrypt Account menu.
164 flag that an account has prefer-encrypt unset;
165 thus encryption will need to be manually enabled. */
166 buf_addstr(buf, _("manual encrypt"));
167 }
168}
169
173void autocrypt_s(const struct ExpandoNode *node, void *data,
174 MuttFormatFlags flags, int max_cols, struct Buffer *buf)
175{
176 const struct AccountEntry *entry = data;
177
178 if (entry->account->enabled)
179 {
180 /* L10N: Autocrypt Account menu.
181 flag that an account is enabled/active */
182 buf_addstr(buf, _("active"));
183 }
184 else
185 {
186 /* L10N: Autocrypt Account menu.
187 flag that an account is disabled/inactive */
188 buf_addstr(buf, _("inactive"));
189 }
190}
191
197static int autocrypt_make_entry(struct Menu *menu, int line, int max_cols, struct Buffer *buf)
198{
199 struct AccountEntry *entry = &((struct AccountEntry *) menu->mdata)[line];
200
201 const bool c_arrow_cursor = cs_subset_bool(menu->sub, "arrow_cursor");
202 if (c_arrow_cursor)
203 {
204 const char *const c_arrow_string = cs_subset_string(menu->sub, "arrow_string");
205 max_cols -= (mutt_strwidth(c_arrow_string) + 1);
206 }
207
208 const struct Expando *c_autocrypt_acct_format = cs_subset_expando(NeoMutt->sub, "autocrypt_acct_format");
209 return expando_render(c_autocrypt_acct_format, AutocryptRenderData, entry,
210 MUTT_FORMAT_ARROWCURSOR, max_cols, buf);
211}
212
216static void autocrypt_menu_free(struct Menu *menu, void **ptr)
217{
218 struct AccountEntry *entries = *ptr;
219
220 for (size_t i = 0; i < menu->max; i++)
221 {
223 mutt_addr_free(&entries[i].addr);
224 }
225
226 FREE(ptr);
227}
228
234bool populate_menu(struct Menu *menu)
235{
236 // Clear out any existing data
237 autocrypt_menu_free(menu, &menu->mdata);
238 menu->max = 0;
239
240 struct AutocryptAccount **accounts = NULL;
241 int num_accounts = 0;
242
243 if (mutt_autocrypt_db_account_get_all(&accounts, &num_accounts) < 0)
244 return false;
245
246 struct AccountEntry *entries = mutt_mem_calloc(num_accounts, sizeof(struct AccountEntry));
247 menu->mdata = entries;
249 menu->max = num_accounts;
250
251 for (int i = 0; i < num_accounts; i++)
252 {
253 entries[i].num = i + 1;
254 /* note: we are transferring the account pointer to the entries
255 * array, and freeing the accounts array below. the account
256 * will be freed in autocrypt_menu_free(). */
257 entries[i].account = accounts[i];
258
259 entries[i].addr = mutt_addr_new();
260 entries[i].addr->mailbox = buf_new(accounts[i]->email_addr);
261 mutt_addr_to_local(entries[i].addr);
262 }
263 FREE(&accounts);
264
266 return true;
267}
268
275{
276 if (nc->event_type != NT_CONFIG)
277 return 0;
278 if (!nc->global_data || !nc->event_data)
279 return -1;
280
281 struct EventConfig *ev_c = nc->event_data;
282
283 if (!mutt_str_equal(ev_c->name, "autocrypt_acct_format"))
284 return 0;
285
286 struct Menu *menu = nc->global_data;
288 mutt_debug(LL_DEBUG5, "config done, request WA_RECALC, MENU_REDRAW_FULL\n");
289
290 return 0;
291}
292
301{
302 if (nc->event_type != NT_WINDOW)
303 return 0;
304 if (!nc->global_data || !nc->event_data)
305 return -1;
307 return 0;
308
309 struct MuttWindow *win_menu = nc->global_data;
310 struct EventWindow *ev_w = nc->event_data;
311 if (ev_w->win != win_menu)
312 return 0;
313
314 struct Menu *menu = win_menu->wdata;
315
318
319 mutt_debug(LL_DEBUG5, "window delete done\n");
320 return 0;
321}
322
329{
330 const bool c_autocrypt = cs_subset_bool(NeoMutt->sub, "autocrypt");
331 if (!c_autocrypt)
332 return;
333
334 if (mutt_autocrypt_init(false))
335 return;
336
338
339 struct Menu *menu = dlg->wdata;
341
342 populate_menu(menu);
343
344 struct AutocryptData ad = { false, menu };
345 dlg->wdata = &ad;
346
347 struct MuttWindow *sbar = window_find_child(dlg, WT_STATUS_BAR);
348 // L10N: Autocrypt Account Management Menu title
349 sbar_set_title(sbar, _("Autocrypt Accounts"));
350
351 // NT_COLOR is handled by the SimpleDialog
354
355 struct MuttWindow *old_focus = window_set_focus(menu->win);
356 // ---------------------------------------------------------------------------
357 // Event Loop
358 int op = OP_NULL;
359 do
360 {
361 menu_tagging_dispatcher(menu->win, op);
362 window_redraw(NULL);
363
365 mutt_debug(LL_DEBUG1, "Got op %s (%d)\n", opcodes_get_name(op), op);
366 if (op < 0)
367 continue;
368 if (op == OP_NULL)
369 {
371 continue;
372 }
374
375 int rc = autocrypt_function_dispatcher(dlg, op);
376
377 if (rc == FR_UNKNOWN)
378 rc = menu_function_dispatcher(menu->win, op);
379 if (rc == FR_UNKNOWN)
380 rc = global_function_dispatcher(NULL, op);
381 } while (!ad.done);
382 // ---------------------------------------------------------------------------
383
384 window_set_focus(old_focus);
385 simple_dialog_free(&dlg);
386}
387
393const struct ExpandoRenderData AutocryptRenderData[] = {
394 // clang-format off
400 { -1, -1, NULL, NULL },
401 // clang-format on
402};
void mutt_addr_free(struct Address **ptr)
Free a single Address.
Definition: address.c:462
struct Address * mutt_addr_new(void)
Create a new Address.
Definition: address.c:401
bool mutt_addr_to_local(struct Address *a)
Convert an Address from Punycode.
Definition: address.c:1340
Email Address Handling.
void mutt_autocrypt_db_account_free(struct AutocryptAccount **ptr)
Free an AutocryptAccount.
Definition: db.c:247
int mutt_autocrypt_db_account_get_all(struct AutocryptAccount ***accounts, int *num_accounts)
Get all accounts from an Autocrypt database.
Definition: db.c:463
@ ED_AUT_ADDRESS
AccountEntry.addr.
Definition: private.h:60
@ ED_AUT_KEYID
AutocryptAccount.keyid.
Definition: private.h:59
@ ED_AUT_NUMBER
AccountEntry.num.
Definition: private.h:61
@ ED_AUT_ENABLED
AutocryptAccount.enabled.
Definition: private.h:58
@ ED_AUT_PREFER_ENCRYPT
AutocryptAccount.prefer_encrypt.
Definition: private.h:62
int mutt_autocrypt_init(bool can_create)
Initialise Autocrypt.
Definition: autocrypt.c:99
struct Buffer * buf_new(const char *str)
Allocate a new Buffer.
Definition: buffer.c:303
size_t buf_addstr(struct Buffer *buf, const char *s)
Add a string to a Buffer.
Definition: buffer.c:225
size_t buf_strcpy(struct Buffer *buf, const char *s)
Copy a string into a Buffer.
Definition: buffer.c:394
size_t buf_copy(struct Buffer *dst, const struct Buffer *src)
Copy a Buffer's contents to another Buffer.
Definition: buffer.c:600
const char * cs_subset_string(const struct ConfigSubset *sub, const char *name)
Get a string config item by name.
Definition: helpers.c:292
bool cs_subset_bool(const struct ConfigSubset *sub, const char *name)
Get a boolean config item by name.
Definition: helpers.c:48
const struct Expando * cs_subset_expando(const struct ConfigSubset *sub, const char *name)
Get an Expando config item by name.
Definition: config_type.c:358
Convenience wrapper for the config headers.
Convenience wrapper for the core headers.
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 AutocryptHelp[]
Help Bar for the Autocrypt Account selection dialog.
Definition: dlg_autocrypt.c:88
const struct ExpandoRenderData AutocryptRenderData[]
Callbacks for Autocrypt Expandos.
Definition: dlg_autocrypt.c:85
bool populate_menu(struct Menu *menu)
Add the Autocrypt data to a Menu.
@ ED_AUTOCRYPT
Autocrypt ED_AUT_ ExpandoDataAutocrypt.
Definition: domain.h:37
Parse Expando string.
int expando_render(const struct Expando *exp, const struct ExpandoRenderData *rdata, void *data, MuttFormatFlags flags, int max_cols, struct Buffer *buf)
Render an Expando + data into a string.
Definition: expando.c:109
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 autocrypt_function_dispatcher(struct MuttWindow *win, int op)
Perform a Autocrypt function - Implements function_dispatcher_t -.
Definition: functions.c:203
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
long autocrypt_n_num(const struct ExpandoNode *node, void *data, MuttFormatFlags flags)
Autocrypt: Index number - Implements ExpandoRenderData::get_number() -.
void autocrypt_p(const struct ExpandoNode *node, void *data, MuttFormatFlags flags, int max_cols, struct Buffer *buf)
Autocrypt: Prefer-encrypt flag - Implements ExpandoRenderData::get_string() -.
void autocrypt_k(const struct ExpandoNode *node, void *data, MuttFormatFlags flags, int max_cols, struct Buffer *buf)
Autocrypt: GPG Key - Implements ExpandoRenderData::get_string() -.
void autocrypt_s(const struct ExpandoNode *node, void *data, MuttFormatFlags flags, int max_cols, struct Buffer *buf)
Autocrypt: Status flag - Implements ExpandoRenderData::get_string() -.
void autocrypt_a(const struct ExpandoNode *node, void *data, MuttFormatFlags flags, int max_cols, struct Buffer *buf)
Autocrypt: Address - Implements ExpandoRenderData::get_string() -.
void dlg_autocrypt(void)
Display the Autocrypt account Menu -.
#define mutt_debug(LEVEL,...)
Definition: logging2.h:89
static int autocrypt_make_entry(struct Menu *menu, int line, int max_cols, struct Buffer *buf)
Format an Autocrypt Account for the Menu - Implements Menu::make_entry() -.
static void autocrypt_menu_free(struct Menu *menu, void **ptr)
Free the Autocrypt account Menu - Implements Menu::mdata_free() -.
static int autocrypt_window_observer(struct NotifyCallback *nc)
Notification that a Window has changed - Implements observer_t -.
static int autocrypt_config_observer(struct NotifyCallback *nc)
Notification that a Config Variable has changed - Implements observer_t -.
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_DEBUG5
Log at debug level 5.
Definition: logging2.h:47
@ LL_DEBUG1
Log at debug level 1.
Definition: logging2.h:43
void * mutt_mem_calloc(size_t nmemb, size_t size)
Allocate zeroed memory on the heap.
Definition: memory.c:50
#define FREE(x)
Definition: memory.h:45
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:654
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_STATUS_BAR
Status Bar containing extra info about the Index/Pager/etc.
Definition: mutt_window.h:102
@ WT_DLG_AUTOCRYPT
Autocrypt Dialog, dlg_autocrypt()
Definition: mutt_window.h:79
@ NT_WINDOW_DELETE
Window is about to be deleted.
Definition: mutt_window.h:229
@ 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
uint8_t MuttFormatFlags
Flags for expando_render(), e.g. MUTT_FORMAT_FORCESUBJ.
Definition: render.h:32
void sbar_set_title(struct MuttWindow *win, const char *title)
Set the title for the Simple Bar.
Definition: sbar.c:227
Sidebar functions.
GUI display the mailboxes in a side panel.
Key value store.
An entry in the Autocrypt account Menu.
Definition: private.h:45
struct Address * addr
Email address associated with the account.
Definition: private.h:48
struct AutocryptAccount * account
Account details.
Definition: private.h:47
int num
Number in the index.
Definition: private.h:46
struct Buffer * mailbox
Mailbox and host address.
Definition: address.h:38
Autocrypt account.
Definition: lib.h:107
char * keyid
PGP Key id.
Definition: lib.h:109
bool enabled
Is this account enabled.
Definition: lib.h:112
bool prefer_encrypt
false = nopref, true = mutual
Definition: lib.h:111
Data to pass to the Autocrypt Functions.
Definition: functions.h:34
bool done
Should we close the Dialog?
Definition: functions.h:35
struct Menu * menu
Autocrypt Menu.
Definition: functions.h:36
String manipulation buffer.
Definition: buffer.h:36
struct Notify * notify
Notifications: NotifyConfig, EventConfig.
Definition: subset.h:52
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
Basic Expando Node.
Definition: node.h:67
Parsed Expando trees.
Definition: expando.h:41
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:41
struct ConfigSubset * sub
Inherited config items.
Definition: neomutt.h:45
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
@ MENU_AUTOCRYPT
Autocrypt Account menu.
Definition: type.h:40