NeoMutt  2022-04-29-249-gaae397
Teaching an old dog new tricks
DOXYGEN
dlg_autocrypt.c
Go to the documentation of this file.
1
67#include "config.h"
68#include <stdbool.h>
69#include <stdint.h>
70#include <stdio.h>
71#include "private.h"
72#include "mutt/lib.h"
73#include "address/lib.h"
74#include "config/lib.h"
75#include "core/lib.h"
76#include "gui/lib.h"
77#include "lib.h"
78#include "menu/lib.h"
79#include "format_flags.h"
80#include "functions.h"
81#include "keymap.h"
82#include "mutt_logging.h"
83#include "muttlib.h"
84#include "opcodes.h"
85
87static const struct Mapping AutocryptAcctHelp[] = {
88 // clang-format off
89 { N_("Exit"), OP_EXIT },
90 /* L10N: Autocrypt Account Menu Help line:
91 create new account */
92 { N_("Create"), OP_AUTOCRYPT_CREATE_ACCT },
93 /* L10N: Autocrypt Account Menu Help line:
94 delete account */
95 { N_("Delete"), OP_AUTOCRYPT_DELETE_ACCT },
96 /* L10N: Autocrypt Account Menu Help line:
97 toggle an account active/inactive
98 The words here are abbreviated to keep the help line compact.
99 It currently has the content:
100 q:Exit c:Create D:Delete a:Tgl Active p:Prf Encr ?:Help */
101 { N_("Tgl Active"), OP_AUTOCRYPT_TOGGLE_ACTIVE },
102 /* L10N: Autocrypt Account Menu Help line:
103 toggle "prefer-encrypt" on an account
104 The words here are abbreviated to keep the help line compact.
105 It currently has the content:
106 q:Exit c:Create D:Delete a:Tgl Active p:Prf Encr ?:Help */
107 { N_("Prf Encr"), OP_AUTOCRYPT_TOGGLE_PREFER },
108 { N_("Help"), OP_HELP },
109 { NULL, 0 }
110 // clang-format on
111};
112
124static const char *autocrypt_format_str(char *buf, size_t buflen, size_t col, int cols,
125 char op, const char *src, const char *prec,
126 const char *if_str, const char *else_str,
127 intptr_t data, MuttFormatFlags flags)
128{
129 struct AccountEntry *entry = (struct AccountEntry *) data;
130 char tmp[128] = { 0 };
131
132 switch (op)
133 {
134 case 'a':
135 mutt_format_s(buf, buflen, prec, entry->addr->mailbox);
136 break;
137 case 'k':
138 mutt_format_s(buf, buflen, prec, entry->account->keyid);
139 break;
140 case 'n':
141 snprintf(tmp, sizeof(tmp), "%%%sd", prec);
142 snprintf(buf, buflen, tmp, entry->num);
143 break;
144 case 'p':
145 if (entry->account->prefer_encrypt)
146 {
147 /* L10N: Autocrypt Account menu.
148 flag that an account has prefer-encrypt set */
149 mutt_format_s(buf, buflen, prec, _("prefer encrypt"));
150 }
151 else
152 {
153 /* L10N: Autocrypt Account menu.
154 flag that an account has prefer-encrypt unset;
155 thus encryption will need to be manually enabled. */
156 mutt_format_s(buf, buflen, prec, _("manual encrypt"));
157 }
158 break;
159 case 's':
160 if (entry->account->enabled)
161 {
162 /* L10N: Autocrypt Account menu.
163 flag that an account is enabled/active */
164 mutt_format_s(buf, buflen, prec, _("active"));
165 }
166 else
167 {
168 /* L10N: Autocrypt Account menu.
169 flag that an account is disabled/inactive */
170 mutt_format_s(buf, buflen, prec, _("inactive"));
171 }
172 break;
173 }
174
175 return (src);
176}
177
183static void autocrypt_make_entry(struct Menu *menu, char *buf, size_t buflen, int num)
184{
185 struct AccountEntry *entry = &((struct AccountEntry *) menu->mdata)[num];
186
187 const char *const c_autocrypt_acct_format = cs_subset_string(NeoMutt->sub, "autocrypt_acct_format");
188 mutt_expando_format(buf, buflen, 0, menu->win->state.cols,
189 NONULL(c_autocrypt_acct_format), autocrypt_format_str,
190 (intptr_t) entry, MUTT_FORMAT_ARROWCURSOR);
191}
192
196static void autocrypt_menu_free(struct Menu *menu, void **ptr)
197{
198 struct AccountEntry *entries = *ptr;
199
200 for (size_t i = 0; i < menu->max; i++)
201 {
203 mutt_addr_free(&entries[i].addr);
204 }
205
206 FREE(ptr);
207}
208
214bool populate_menu(struct Menu *menu)
215{
216 // Clear out any existing data
217 autocrypt_menu_free(menu, &menu->mdata);
218 menu->max = 0;
219
220 struct AutocryptAccount **accounts = NULL;
221 int num_accounts = 0;
222
223 if (mutt_autocrypt_db_account_get_all(&accounts, &num_accounts) < 0)
224 return false;
225
226 struct AccountEntry *entries = mutt_mem_calloc(num_accounts, sizeof(struct AccountEntry));
227 menu->mdata = entries;
229 menu->max = num_accounts;
230
231 for (int i = 0; i < num_accounts; i++)
232 {
233 entries[i].num = i + 1;
234 /* note: we are transferring the account pointer to the entries
235 * array, and freeing the accounts array below. the account
236 * will be freed in autocrypt_menu_free(). */
237 entries[i].account = accounts[i];
238
239 entries[i].addr = mutt_addr_new();
240 entries[i].addr->mailbox = mutt_str_dup(accounts[i]->email_addr);
241 mutt_addr_to_local(entries[i].addr);
242 }
243 FREE(&accounts);
244
246 return true;
247}
248
255{
256 if (nc->event_type != NT_CONFIG)
257 return 0;
258 if (!nc->global_data || !nc->event_data)
259 return -1;
260
261 struct EventConfig *ev_c = nc->event_data;
262
263 if (!mutt_str_equal(ev_c->name, "autocrypt_acct_format"))
264 return 0;
265
266 struct Menu *menu = nc->global_data;
268 mutt_debug(LL_DEBUG5, "config done, request WA_RECALC, MENU_REDRAW_FULL\n");
269
270 return 0;
271}
272
281{
282 if (nc->event_type != NT_WINDOW)
283 return 0;
284 if (!nc->global_data || !nc->event_data)
285 return -1;
287 return 0;
288
289 struct MuttWindow *win_menu = nc->global_data;
290 struct EventWindow *ev_w = nc->event_data;
291 if (ev_w->win != win_menu)
292 return 0;
293
294 struct Menu *menu = win_menu->wdata;
295
298
299 mutt_debug(LL_DEBUG5, "window delete done\n");
300 return 0;
301}
302
307{
308 const bool c_autocrypt = cs_subset_bool(NeoMutt->sub, "autocrypt");
309 if (!c_autocrypt)
310 return;
311
312 if (mutt_autocrypt_init(false))
313 return;
314
317
318 struct Menu *menu = dlg->wdata;
320
321 populate_menu(menu);
322
323 struct AutocryptData ad = { false, menu };
324 dlg->wdata = &ad;
325
326 struct MuttWindow *sbar = window_find_child(dlg, WT_STATUS_BAR);
327 // L10N: Autocrypt Account Management Menu title
328 sbar_set_title(sbar, _("Autocrypt Accounts"));
329
330 // NT_COLOR is handled by the SimpleDialog
333
334 // ---------------------------------------------------------------------------
335 // Event Loop
336 int op = OP_NULL;
337 do
338 {
339 menu_tagging_dispatcher(menu->win, op);
340 window_redraw(NULL);
341
343 mutt_debug(LL_DEBUG1, "Got op %s (%d)\n", opcodes_get_name(op), op);
344 if (op < 0)
345 continue;
346 if (op == OP_NULL)
347 {
349 continue;
350 }
352
353 int rc = autocrypt_function_dispatcher(dlg, op);
354
355 if (rc == FR_UNKNOWN)
356 rc = menu_function_dispatcher(menu->win, op);
357 if (rc == FR_UNKNOWN)
358 rc = global_function_dispatcher(NULL, op);
359 } while (!ad.done);
360 // ---------------------------------------------------------------------------
361
362 simple_dialog_free(&dlg);
363}
void mutt_addr_free(struct Address **ptr)
Free a single Address.
Definition: address.c:440
struct Address * mutt_addr_new(void)
Create a new Address.
Definition: address.c:385
bool mutt_addr_to_local(struct Address *a)
Convert an Address from Punycode.
Definition: address.c:1351
Email Address Handling.
void mutt_autocrypt_db_account_free(struct AutocryptAccount **ptr)
Free an AutocryptAccount.
Definition: db.c:244
int mutt_autocrypt_db_account_get_all(struct AutocryptAccount ***accounts, int *num_accounts)
Get all accounts from an Autocrypt database.
Definition: db.c:454
int mutt_autocrypt_init(bool can_create)
Initialise Autocrypt.
Definition: autocrypt.c:97
const char * cs_subset_string(const struct ConfigSubset *sub, const char *name)
Get a string config item by name.
Definition: helpers.c:317
bool cs_subset_bool(const struct ConfigSubset *sub, const char *name)
Get a boolean config item by name.
Definition: helpers.c:73
Convenience wrapper for the config headers.
Convenience wrapper for the core headers.
void mutt_format_s(char *buf, size_t buflen, const char *prec, const char *s)
Format a simple string.
Definition: curs_lib.c:793
@ FR_UNKNOWN
Unknown function.
Definition: dispatcher.h:33
static const struct Mapping AutocryptAcctHelp[]
Help Bar for the Autocrypt Account selection dialog.
Definition: dlg_autocrypt.c:87
void dlg_select_autocrypt_account(void)
Display the Autocrypt account Menu.
bool populate_menu(struct Menu *menu)
Add the Autocrypt data to a Menu.
Flags to control mutt_expando_format()
#define MUTT_FORMAT_ARROWCURSOR
Reserve space for arrow_cursor.
Definition: format_flags.h:35
uint8_t MuttFormatFlags
Flags for mutt_expando_format(), e.g. MUTT_FORMAT_FORCESUBJ.
Definition: format_flags.h:29
int menu_tagging_dispatcher(struct MuttWindow *win, int op)
Perform tagging operations on the Menu - Implements function_dispatcher_t -.
Definition: tagging.c:223
int autocrypt_function_dispatcher(struct MuttWindow *win, int op)
Perform a Autocrypt function - Implements function_dispatcher_t -.
Definition: functions.c:169
int global_function_dispatcher(struct MuttWindow *win, int op)
Perform a Global function - Implements function_dispatcher_t -.
Definition: global.c:164
int menu_function_dispatcher(struct MuttWindow *win, int op)
Perform a Menu function - Implements function_dispatcher_t -.
Definition: functions.c:320
void mutt_expando_format(char *buf, size_t buflen, size_t col, int cols, const char *src, format_t callback, intptr_t data, MuttFormatFlags flags)
Expand expandos (x) in a string -.
Definition: muttlib.c:777
static const char * autocrypt_format_str(char *buf, size_t buflen, size_t col, int cols, char op, const char *src, const char *prec, const char *if_str, const char *else_str, intptr_t data, MuttFormatFlags flags)
Format a string for the Autocrypt account list - Implements format_t -.
#define mutt_debug(LEVEL,...)
Definition: logging.h:84
static void autocrypt_make_entry(struct Menu *menu, char *buf, size_t buflen, int num)
Create a line for the Autocrypt account 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:166
struct MuttWindow * simple_dialog_new(enum MenuType mtype, enum WindowType wtype, const struct Mapping *help_data)
Create a simple index Dialog.
Definition: simple.c:129
int km_dokey(enum MenuType mtype)
Determine what a keypress should do.
Definition: keymap.c:795
void km_error_key(enum MenuType mtype)
Handle an unbound key sequence.
Definition: keymap.c:1061
Manage keymappings.
@ LL_DEBUG5
Log at debug level 5.
Definition: logging.h:44
@ LL_DEBUG1
Log at debug level 1.
Definition: logging.h:40
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: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:178
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:228
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:189
char * mutt_str_dup(const char *str)
Copy a string, safely.
Definition: string.c:250
bool mutt_str_equal(const char *a, const char *b)
Compare two strings.
Definition: string.c:807
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:604
struct MuttWindow * window_find_child(struct MuttWindow *win, enum WindowType type)
Recursively find a child Window of a given type.
Definition: mutt_window.c:522
@ WT_STATUS_BAR
Status Bar containing extra info about the Index/Pager/etc.
Definition: mutt_window.h:102
@ WT_DLG_AUTOCRYPT
Autocrypt Dialog, dlg_select_autocrypt_account()
Definition: mutt_window.h:79
@ NT_WINDOW_DELETE
Window is about to be deleted.
Definition: mutt_window.h:206
Some miscellaneous functions.
@ NT_WINDOW
MuttWindow has changed, NotifyWindow, EventWindow.
Definition: notify_type.h:55
@ 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:46
All user-callable functions.
void sbar_set_title(struct MuttWindow *win, const char *title)
Set the title for the Simple Bar.
Definition: sbar.c:224
Sidebar functions.
GUI display the mailboxes in a side panel.
Key value store.
#define NONULL(x)
Definition: string2.h:37
An entry in the Autocrypt account Menu.
Definition: private.h:44
struct Address * addr
Email address associated with the account.
Definition: private.h:47
struct AutocryptAccount * account
Account details.
Definition: private.h:46
int num
Number in the index.
Definition: private.h:45
char * mailbox
Mailbox and host address.
Definition: address.h:38
Autocrypt account.
Definition: lib.h:106
char * keyid
PGP Key id.
Definition: lib.h:108
bool enabled
Is this account enabled.
Definition: lib.h:111
bool prefer_encrypt
false = nopref, true = mutual
Definition: lib.h:110
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
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.
Definition: mutt_window.h:216
struct MuttWindow * win
Window that changed.
Definition: mutt_window.h:217
Mapping between user-readable string and a constant.
Definition: mapping.h:32
Definition: lib.h:69
struct MuttWindow * win
Window holding the Menu.
Definition: lib.h:76
void(* make_entry)(struct Menu *menu, char *buf, size_t buflen, int line)
Definition: lib.h:96
void(* mdata_free)(struct Menu *menu, void **ptr)
Definition: lib.h:152
void * mdata
Private data.
Definition: lib.h:137
int max
Number of entries in the menu.
Definition: lib.h:71
struct WindowState state
Current state of the Window.
Definition: mutt_window.h:127
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:37
struct Notify * notify
Notifications handler.
Definition: neomutt.h:38
struct ConfigSubset * sub
Inherited config items.
Definition: neomutt.h:39
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
short cols
Number of columns, can be MUTT_WIN_SIZE_UNLIMITED.
Definition: mutt_window.h:60
@ MENU_AUTOCRYPT_ACCT
Autocrypt Account menu.
Definition: type.h:40