NeoMutt
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
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 "key/lib.h"
79#include "menu/lib.h"
80#include "format_flags.h"
81#include "functions.h"
82#include "mutt_logging.h"
83#include "muttlib.h"
84
86static const struct Mapping AutocryptHelp[] = {
87 // clang-format off
88 { N_("Exit"), OP_EXIT },
89 /* L10N: Autocrypt Account Menu Help line:
90 create new account */
91 { N_("Create"), OP_AUTOCRYPT_CREATE_ACCT },
92 /* L10N: Autocrypt Account Menu Help line:
93 delete account */
94 { N_("Delete"), OP_AUTOCRYPT_DELETE_ACCT },
95 /* L10N: Autocrypt Account Menu Help line:
96 toggle an account active/inactive
97 The words here are abbreviated to keep the help line compact.
98 It currently has the content:
99 q:Exit c:Create D:Delete a:Tgl Active p:Prf Encr ?:Help */
100 { N_("Tgl Active"), OP_AUTOCRYPT_TOGGLE_ACTIVE },
101 /* L10N: Autocrypt Account Menu Help line:
102 toggle "prefer-encrypt" on an account
103 The words here are abbreviated to keep the help line compact.
104 It currently has the content:
105 q:Exit c:Create D:Delete a:Tgl Active p:Prf Encr ?:Help */
106 { N_("Prf Encr"), OP_AUTOCRYPT_TOGGLE_PREFER },
107 { N_("Help"), OP_HELP },
108 { NULL, 0 }
109 // clang-format on
110};
111
123static const char *autocrypt_format_str(char *buf, size_t buflen, size_t col, int cols,
124 char op, const char *src, const char *prec,
125 const char *if_str, const char *else_str,
126 intptr_t data, MuttFormatFlags flags)
127{
128 struct AccountEntry *entry = (struct AccountEntry *) data;
129 char tmp[128] = { 0 };
130
131 switch (op)
132 {
133 case 'a':
134 mutt_format_s(buf, buflen, prec, buf_string(entry->addr->mailbox));
135 break;
136 case 'k':
137 mutt_format_s(buf, buflen, prec, entry->account->keyid);
138 break;
139 case 'n':
140 snprintf(tmp, sizeof(tmp), "%%%sd", prec);
141 snprintf(buf, buflen, tmp, entry->num);
142 break;
143 case 'p':
144 if (entry->account->prefer_encrypt)
145 {
146 /* L10N: Autocrypt Account menu.
147 flag that an account has prefer-encrypt set */
148 mutt_format_s(buf, buflen, prec, _("prefer encrypt"));
149 }
150 else
151 {
152 /* L10N: Autocrypt Account menu.
153 flag that an account has prefer-encrypt unset;
154 thus encryption will need to be manually enabled. */
155 mutt_format_s(buf, buflen, prec, _("manual encrypt"));
156 }
157 break;
158 case 's':
159 if (entry->account->enabled)
160 {
161 /* L10N: Autocrypt Account menu.
162 flag that an account is enabled/active */
163 mutt_format_s(buf, buflen, prec, _("active"));
164 }
165 else
166 {
167 /* L10N: Autocrypt Account menu.
168 flag that an account is disabled/inactive */
169 mutt_format_s(buf, buflen, prec, _("inactive"));
170 }
171 break;
172 }
173
174 return (src);
175}
176
182static void autocrypt_make_entry(struct Menu *menu, char *buf, size_t buflen, int num)
183{
184 struct AccountEntry *entry = &((struct AccountEntry *) menu->mdata)[num];
185
186 const char *const c_autocrypt_acct_format = cs_subset_string(NeoMutt->sub, "autocrypt_acct_format");
187 mutt_expando_format(buf, buflen, 0, menu->win->state.cols,
188 NONULL(c_autocrypt_acct_format), autocrypt_format_str,
189 (intptr_t) entry, MUTT_FORMAT_ARROWCURSOR);
190}
191
195static void autocrypt_menu_free(struct Menu *menu, void **ptr)
196{
197 struct AccountEntry *entries = *ptr;
198
199 for (size_t i = 0; i < menu->max; i++)
200 {
202 mutt_addr_free(&entries[i].addr);
203 }
204
205 FREE(ptr);
206}
207
213bool populate_menu(struct Menu *menu)
214{
215 // Clear out any existing data
216 autocrypt_menu_free(menu, &menu->mdata);
217 menu->max = 0;
218
219 struct AutocryptAccount **accounts = NULL;
220 int num_accounts = 0;
221
222 if (mutt_autocrypt_db_account_get_all(&accounts, &num_accounts) < 0)
223 return false;
224
225 struct AccountEntry *entries = mutt_mem_calloc(num_accounts, sizeof(struct AccountEntry));
226 menu->mdata = entries;
228 menu->max = num_accounts;
229
230 for (int i = 0; i < num_accounts; i++)
231 {
232 entries[i].num = i + 1;
233 /* note: we are transferring the account pointer to the entries
234 * array, and freeing the accounts array below. the account
235 * will be freed in autocrypt_menu_free(). */
236 entries[i].account = accounts[i];
237
238 entries[i].addr = mutt_addr_new();
239 entries[i].addr->mailbox = buf_new(accounts[i]->email_addr);
240 mutt_addr_to_local(entries[i].addr);
241 }
242 FREE(&accounts);
243
245 return true;
246}
247
254{
255 if (nc->event_type != NT_CONFIG)
256 return 0;
257 if (!nc->global_data || !nc->event_data)
258 return -1;
259
260 struct EventConfig *ev_c = nc->event_data;
261
262 if (!mutt_str_equal(ev_c->name, "autocrypt_acct_format"))
263 return 0;
264
265 struct Menu *menu = nc->global_data;
267 mutt_debug(LL_DEBUG5, "config done, request WA_RECALC, MENU_REDRAW_FULL\n");
268
269 return 0;
270}
271
280{
281 if (nc->event_type != NT_WINDOW)
282 return 0;
283 if (!nc->global_data || !nc->event_data)
284 return -1;
286 return 0;
287
288 struct MuttWindow *win_menu = nc->global_data;
289 struct EventWindow *ev_w = nc->event_data;
290 if (ev_w->win != win_menu)
291 return 0;
292
293 struct Menu *menu = win_menu->wdata;
294
297
298 mutt_debug(LL_DEBUG5, "window delete done\n");
299 return 0;
300}
301
308{
309 const bool c_autocrypt = cs_subset_bool(NeoMutt->sub, "autocrypt");
310 if (!c_autocrypt)
311 return;
312
313 if (mutt_autocrypt_init(false))
314 return;
315
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 struct MuttWindow *old_focus = window_set_focus(menu->win);
335 // ---------------------------------------------------------------------------
336 // Event Loop
337 int op = OP_NULL;
338 do
339 {
340 menu_tagging_dispatcher(menu->win, op);
341 window_redraw(NULL);
342
344 mutt_debug(LL_DEBUG1, "Got op %s (%d)\n", opcodes_get_name(op), op);
345 if (op < 0)
346 continue;
347 if (op == OP_NULL)
348 {
350 continue;
351 }
353
354 int rc = autocrypt_function_dispatcher(dlg, op);
355
356 if (rc == FR_UNKNOWN)
357 rc = menu_function_dispatcher(menu->win, op);
358 if (rc == FR_UNKNOWN)
359 rc = global_function_dispatcher(NULL, op);
360 } while (!ad.done);
361 // ---------------------------------------------------------------------------
362
363 window_set_focus(old_focus);
364 simple_dialog_free(&dlg);
365}
void mutt_addr_free(struct Address **ptr)
Free a single Address.
Definition: address.c:460
struct Address * mutt_addr_new(void)
Create a new Address.
Definition: address.c:399
bool mutt_addr_to_local(struct Address *a)
Convert an Address from Punycode.
Definition: address.c:1341
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:460
int mutt_autocrypt_init(bool can_create)
Initialise Autocrypt.
Definition: autocrypt.c:97
struct Buffer * buf_new(const char *str)
Allocate a new Buffer.
Definition: buffer.c:316
static const char * buf_string(const struct Buffer *buf)
Convert a buffer to a const char * "string".
Definition: buffer.h:93
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
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:522
@ 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:86
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 km_dokey(enum MenuType mtype, GetChFlags flags)
Determine what a keypress should do.
Definition: get.c:475
void km_error_key(enum MenuType mtype)
Handle an unbound key sequence.
Definition: get.c:305
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:166
int menu_function_dispatcher(struct MuttWindow *win, int op)
Perform a Menu function - Implements function_dispatcher_t -.
Definition: functions.c:317
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:745
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 -.
void dlg_autocrypt(void)
Display the Autocrypt account Menu -.
#define mutt_debug(LEVEL,...)
Definition: logging2.h:89
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: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:52
@ 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:60
void menu_queue_redraw(struct Menu *menu, MenuRedrawFlags redraw)
Queue a request for a redraw.
Definition: menu.c:180
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:798
void mutt_clear_error(void)
Clear the message line (bottom line of screen)
Definition: mutt_logging.c:73
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
Some miscellaneous functions.
@ 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
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.
#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
struct Buffer * 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
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
Mapping between user-readable string and a constant.
Definition: mapping.h:32
Definition: lib.h:70
struct MuttWindow * win
Window holding the Menu.
Definition: lib.h:77
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:151
void * mdata
Private data.
Definition: lib.h:137
int max
Number of entries in the menu.
Definition: lib.h:72
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: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
short cols
Number of columns, can be MUTT_WIN_SIZE_UNLIMITED.
Definition: mutt_window.h:60
@ MENU_AUTOCRYPT
Autocrypt Account menu.
Definition: type.h:40