NeoMutt  2024-11-14-34-g5aaf0d
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 "autocrypt_data.h"
83#include "functions.h"
84#include "mutt_logging.h"
85
87
89static const struct Mapping AutocryptHelp[] = {
90 // clang-format off
91 { N_("Exit"), OP_EXIT },
92 /* L10N: Autocrypt Account Menu Help line:
93 create new account */
94 { N_("Create"), OP_AUTOCRYPT_CREATE_ACCT },
95 /* L10N: Autocrypt Account Menu Help line:
96 delete account */
97 { N_("Delete"), OP_AUTOCRYPT_DELETE_ACCT },
98 /* L10N: Autocrypt Account Menu Help line:
99 toggle an account active/inactive
100 The words here are abbreviated to keep the help line compact.
101 It currently has the content:
102 q:Exit c:Create D:Delete a:Tgl Active p:Prf Encr ?:Help */
103 { N_("Tgl Active"), OP_AUTOCRYPT_TOGGLE_ACTIVE },
104 /* L10N: Autocrypt Account Menu Help line:
105 toggle "prefer-encrypt" on an account
106 The words here are abbreviated to keep the help line compact.
107 It currently has the content:
108 q:Exit c:Create D:Delete a:Tgl Active p:Prf Encr ?:Help */
109 { N_("Prf Encr"), OP_AUTOCRYPT_TOGGLE_PREFER },
110 { N_("Help"), OP_HELP },
111 { NULL, 0 }
112 // clang-format on
113};
114
118void autocrypt_a(const struct ExpandoNode *node, void *data,
119 MuttFormatFlags flags, struct Buffer *buf)
120{
121 const struct AccountEntry *entry = data;
122
123 buf_copy(buf, entry->addr->mailbox);
124}
125
129void autocrypt_k(const struct ExpandoNode *node, void *data,
130 MuttFormatFlags flags, struct Buffer *buf)
131{
132 const struct AccountEntry *entry = data;
133
134 const char *s = entry->account->keyid;
135 buf_strcpy(buf, s);
136}
137
141long autocrypt_n_num(const struct ExpandoNode *node, void *data, MuttFormatFlags flags)
142{
143 const struct AccountEntry *entry = data;
144
145 return entry->num;
146}
147
151void autocrypt_p(const struct ExpandoNode *node, void *data,
152 MuttFormatFlags flags, struct Buffer *buf)
153{
154 const struct AccountEntry *entry = data;
155
156 if (entry->account->prefer_encrypt)
157 {
158 /* L10N: Autocrypt Account menu.
159 flag that an account has prefer-encrypt set */
160 buf_addstr(buf, _("prefer encrypt"));
161 }
162 else
163 {
164 /* L10N: Autocrypt Account menu.
165 flag that an account has prefer-encrypt unset;
166 thus encryption will need to be manually enabled. */
167 buf_addstr(buf, _("manual encrypt"));
168 }
169}
170
174void autocrypt_s(const struct ExpandoNode *node, void *data,
175 MuttFormatFlags flags, struct Buffer *buf)
176{
177 const struct AccountEntry *entry = data;
178
179 if (entry->account->enabled)
180 {
181 /* L10N: Autocrypt Account menu.
182 flag that an account is enabled/active */
183 buf_addstr(buf, _("active"));
184 }
185 else
186 {
187 /* L10N: Autocrypt Account menu.
188 flag that an account is disabled/inactive */
189 buf_addstr(buf, _("inactive"));
190 }
191}
192
198static int autocrypt_make_entry(struct Menu *menu, int line, int max_cols, struct Buffer *buf)
199{
200 struct AutocryptData *ad = menu->mdata;
201 struct AccountEntry **pentry = ARRAY_GET(&ad->entries, line);
202 if (!pentry)
203 return 0;
204
205 const bool c_arrow_cursor = cs_subset_bool(menu->sub, "arrow_cursor");
206 if (c_arrow_cursor)
207 {
208 const char *const c_arrow_string = cs_subset_string(menu->sub, "arrow_string");
209 if (max_cols > 0)
210 max_cols -= (mutt_strwidth(c_arrow_string) + 1);
211 }
212
213 const struct Expando *c_autocrypt_acct_format = cs_subset_expando(NeoMutt->sub, "autocrypt_acct_format");
214 return expando_filter(c_autocrypt_acct_format, AutocryptRenderData, *pentry,
215 MUTT_FORMAT_ARROWCURSOR, max_cols, buf);
216}
217
223bool populate_menu(struct Menu *menu)
224{
225 struct AutocryptData *ad = menu->mdata;
226
227 // Clear out any existing data
229 menu->max = 0;
230
231 struct AutocryptAccountArray accounts = ARRAY_HEAD_INITIALIZER;
232
233 if (mutt_autocrypt_db_account_get_all(&accounts) < 0)
234 return false;
235
236 menu->max = ARRAY_SIZE(&accounts);
237
238 struct AutocryptAccount **pac = NULL;
239 ARRAY_FOREACH(pac, &accounts)
240 {
241 struct AccountEntry *entry = MUTT_MEM_CALLOC(1, struct AccountEntry);
242
243 entry->num = ARRAY_FOREACH_IDX + 1;
244 /* note: we are transferring the account pointer to the entries
245 * array, and freeing the accounts array below. the account
246 * will be freed in autocrypt_menu_free(). */
247 entry->account = *pac;
248
249 entry->addr = mutt_addr_new();
250 entry->addr->mailbox = buf_new((*pac)->email_addr);
251 mutt_addr_to_local(entry->addr);
252 ARRAY_ADD(&ad->entries, entry);
253 }
254 ARRAY_FREE(&accounts);
255
257 return true;
258}
259
266{
267 if (nc->event_type != NT_CONFIG)
268 return 0;
269 if (!nc->global_data || !nc->event_data)
270 return -1;
271
272 struct EventConfig *ev_c = nc->event_data;
273
274 if (!mutt_str_equal(ev_c->name, "autocrypt_acct_format"))
275 return 0;
276
277 struct Menu *menu = nc->global_data;
279 mutt_debug(LL_DEBUG5, "config done, request WA_RECALC, MENU_REDRAW_FULL\n");
280
281 return 0;
282}
283
292{
293 if (nc->event_type != NT_WINDOW)
294 return 0;
295 if (!nc->global_data || !nc->event_data)
296 return -1;
298 return 0;
299
300 struct MuttWindow *win_menu = nc->global_data;
301 struct EventWindow *ev_w = nc->event_data;
302 if (ev_w->win != win_menu)
303 return 0;
304
305 struct Menu *menu = win_menu->wdata;
306
309
310 mutt_debug(LL_DEBUG5, "window delete done\n");
311 return 0;
312}
313
320{
321 const bool c_autocrypt = cs_subset_bool(NeoMutt->sub, "autocrypt");
322 if (!c_autocrypt)
323 return;
324
325 if (mutt_autocrypt_init(false))
326 return;
327
330
331 struct Menu *menu = sdw.menu;
332
333 struct AutocryptData *ad = autocrypt_data_new();
334 ad->menu = menu;
335
337 menu->mdata = ad;
339
341
342 // L10N: Autocrypt Account Management Menu title
343 sbar_set_title(sdw.sbar, _("Autocrypt Accounts"));
344
345 // NT_COLOR is handled by the SimpleDialog
348
349 struct MuttWindow *old_focus = window_set_focus(menu->win);
350 // ---------------------------------------------------------------------------
351 // Event Loop
352 int op = OP_NULL;
353 do
354 {
355 menu_tagging_dispatcher(menu->win, op);
356 window_redraw(NULL);
357
359 mutt_debug(LL_DEBUG1, "Got op %s (%d)\n", opcodes_get_name(op), op);
360 if (op < 0)
361 continue;
362 if (op == OP_NULL)
363 {
365 continue;
366 }
368
369 int rc = autocrypt_function_dispatcher(sdw.dlg, op);
370
371 if (rc == FR_UNKNOWN)
372 rc = menu_function_dispatcher(menu->win, op);
373 if (rc == FR_UNKNOWN)
374 rc = global_function_dispatcher(NULL, op);
375 } while (!ad->done);
376 // ---------------------------------------------------------------------------
377
378 window_set_focus(old_focus);
380}
381
387const struct ExpandoRenderData AutocryptRenderData[] = {
388 // clang-format off
394 { -1, -1, NULL, NULL },
395 // clang-format on
396};
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.
#define ARRAY_ADD(head, elem)
Add an element at the end of the array.
Definition: array.h:156
#define ARRAY_FOREACH(elem, head)
Iterate over all elements of the array.
Definition: array.h:212
#define ARRAY_SIZE(head)
The number of elements stored.
Definition: array.h:87
#define ARRAY_FREE(head)
Release all memory.
Definition: array.h:204
#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 mutt_autocrypt_db_account_get_all(struct AutocryptAccountArray *aaa)
Get all accounts from an Autocrypt database.
Definition: db.c:461
@ ED_AUT_ADDRESS
AccountEntry.addr.
Definition: private.h:63
@ ED_AUT_KEYID
AutocryptAccount.keyid.
Definition: private.h:62
@ ED_AUT_NUMBER
AccountEntry.num.
Definition: private.h:64
@ ED_AUT_ENABLED
AutocryptAccount.enabled.
Definition: private.h:61
@ ED_AUT_PREFER_ENCRYPT
AutocryptAccount.prefer_encrypt.
Definition: private.h:65
int mutt_autocrypt_init(bool can_create)
Initialise Autocrypt.
Definition: autocrypt.c:99
struct AutocryptData * autocrypt_data_new(void)
Create new Autocrypt Data.
void account_entry_array_clear(struct AccountEntryArray *entries)
Clear an AccountEntry array.
Private Autocrypt Data.
struct Buffer * buf_new(const char *str)
Allocate a new Buffer.
Definition: buffer.c:304
size_t buf_addstr(struct Buffer *buf, const char *s)
Add a string to a Buffer.
Definition: buffer.c:226
size_t buf_strcpy(struct Buffer *buf, const char *s)
Copy a string into a Buffer.
Definition: buffer.c:395
size_t buf_copy(struct Buffer *dst, const struct Buffer *src)
Copy a Buffer's contents to another Buffer.
Definition: buffer.c:601
const char * cs_subset_string(const struct ConfigSubset *sub, const char *name)
Get a string config item by name.
Definition: helpers.c:291
bool cs_subset_bool(const struct ConfigSubset *sub, const char *name)
Get a boolean config item by name.
Definition: helpers.c:47
const struct Expando * cs_subset_expando(const struct ConfigSubset *sub, const char *name)
Get an Expando config item by name.
Definition: config_type.c:357
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:89
const struct ExpandoRenderData AutocryptRenderData[]
Callbacks for Autocrypt Expandos.
Definition: dlg_autocrypt.c:86
bool populate_menu(struct Menu *menu)
Add the Autocrypt data to a Menu.
@ ED_AUTOCRYPT
Autocrypt ED_AUT_ ExpandoDataAutocrypt.
Definition: domain.h:37
int expando_filter(const struct Expando *exp, const struct ExpandoRenderData *rdata, void *data, MuttFormatFlags flags, int max_cols, struct Buffer *buf)
Render an Expando and run the result through a filter.
Definition: filter.c:138
Parse Expando string.
int km_dokey(enum MenuType mtype, GetChFlags flags)
Determine what a keypress should do.
Definition: get.c:464
void km_error_key(enum MenuType mtype)
Handle an unbound key sequence.
Definition: get.c:294
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:213
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_a(const struct ExpandoNode *node, void *data, MuttFormatFlags flags, struct Buffer *buf)
Autocrypt: Address - Implements ExpandoRenderData::get_string() -.
void autocrypt_p(const struct ExpandoNode *node, void *data, MuttFormatFlags flags, struct Buffer *buf)
Autocrypt: Prefer-encrypt flag - Implements ExpandoRenderData::get_string() -.
void autocrypt_s(const struct ExpandoNode *node, void *data, MuttFormatFlags flags, struct Buffer *buf)
Autocrypt: Status flag - Implements ExpandoRenderData::get_string() -.
void autocrypt_k(const struct ExpandoNode *node, void *data, MuttFormatFlags flags, struct Buffer *buf)
Autocrypt: GPG Key - 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() -.
void autocrypt_data_free(struct Menu *menu, void **ptr)
Free Autocrypt Data - 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 SimpleDialogWindows 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
#define MUTT_MEM_CALLOC(n, type)
Definition: memory.h:40
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:660
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:633
struct MuttWindow * window_set_focus(struct MuttWindow *win)
Set the Window focus.
Definition: mutt_window.c:683
@ 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:228
@ 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:47
struct Address * addr
Email address associated with the account.
Definition: private.h:50
struct AutocryptAccount * account
Account details.
Definition: private.h:49
int num
Number in the index.
Definition: private.h:48
struct Buffer * mailbox
Mailbox and host address.
Definition: address.h:38
Autocrypt account.
Definition: lib.h:108
char * keyid
PGP Key id.
Definition: lib.h:110
bool enabled
Is this account enabled.
Definition: lib.h:113
bool prefer_encrypt
false = nopref, true = mutual
Definition: lib.h:112
Data to pass to the Autocrypt Functions.
bool done
Should we close the Dialog?
struct Menu * menu
Autocrypt Menu.
struct AccountEntryArray entries
Account Entries.
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:238
struct MuttWindow * win
Window that changed.
Definition: mutt_window.h:239
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:144
struct Notify * notify
Notifications: NotifyWindow, EventWindow.
Definition: mutt_window.h:137
Container for Accounts, Notifications.
Definition: neomutt.h:42
struct ConfigSubset * sub
Inherited config items.
Definition: neomutt.h:46
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
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
@ MENU_AUTOCRYPT
Autocrypt Account menu.
Definition: type.h:40