NeoMutt  2025-12-11-860-g80c9cc
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
functions.c
Go to the documentation of this file.
1
22
28
29#include "config.h"
30#include <stdbool.h>
31#include <stdio.h>
32#include "private.h"
33#include "mutt/lib.h"
34#include "address/lib.h"
35#include "config/lib.h"
36#include "core/lib.h"
37#include "gui/lib.h"
38#include "functions.h"
39#include "lib.h"
40#include "key/lib.h"
41#include "menu/lib.h"
42#include "question/lib.h"
43#include "autocrypt_data.h"
44#include "module_data.h"
45
46// clang-format off
50static const struct MenuFuncOp OpAutocrypt[] = { /* map: autocrypt account */
51 { "create-account", OP_AUTOCRYPT_CREATE_ACCT },
52 { "delete-account", OP_AUTOCRYPT_DELETE_ACCT },
53 { "exit", OP_EXIT },
54 { "toggle-active", OP_AUTOCRYPT_TOGGLE_ACTIVE },
55 { "toggle-prefer-encrypt", OP_AUTOCRYPT_TOGGLE_PREFER },
56 { NULL, 0 }
57};
58
62static const struct MenuOpSeq AutocryptDefaultBindings[] = { /* map: autocrypt account */
63 { OP_AUTOCRYPT_CREATE_ACCT, "c" },
64 { OP_AUTOCRYPT_DELETE_ACCT, "D" },
65 { OP_AUTOCRYPT_TOGGLE_ACTIVE, "a" },
66 { OP_AUTOCRYPT_TOGGLE_PREFER, "p" },
67 { OP_EXIT, "q" },
68 { 0, NULL }
69};
70// clang-format on
71
75void autocrypt_init_keys(struct NeoMutt *n, struct SubMenu *sm_generic)
76{
78 ASSERT(mod_data);
79
80 struct MenuDefinition *md = NULL;
81 struct SubMenu *sm = NULL;
82
84 md = km_register_menu(MENU_AUTOCRYPT, "autocrypt");
85 km_menu_add_submenu(md, sm);
86 km_menu_add_submenu(md, sm_generic);
88
89 mod_data->menu_autocrypt = md;
90}
91
98static bool toggle_active(struct AccountEntry *entry)
99{
100 entry->account->enabled = !entry->account->enabled;
102 {
103 entry->account->enabled = !entry->account->enabled;
104 /* L10N: This error message is displayed if a database update of an
105 account record fails for some odd reason. */
106 mutt_error(_("Error updating account record"));
107 return false;
108 }
109
110 return true;
111}
112
119static bool toggle_prefer_encrypt(struct AccountEntry *entry)
120{
121 entry->account->prefer_encrypt = !entry->account->prefer_encrypt;
123 {
124 entry->account->prefer_encrypt = !entry->account->prefer_encrypt;
125 mutt_error(_("Error updating account record"));
126 return false;
127 }
128
129 return true;
130}
131
132// -----------------------------------------------------------------------------
133
137static int op_autocrypt_create_acct(struct AutocryptData *ad, const struct KeyEvent *event)
138{
139 if (mutt_autocrypt_account_init(false) == 0)
140 populate_menu(ad->menu);
141
142 return FR_SUCCESS;
143}
144
148static int op_autocrypt_delete_acct(struct AutocryptData *ad, const struct KeyEvent *event)
149{
150 if (!ad->menu->mdata)
151 return FR_ERROR;
152
153 const int index = menu_get_index(ad->menu);
154 struct AccountEntry **pentry = ARRAY_GET(&ad->entries, index);
155 if (!pentry)
156 return 0;
157
158 char msg[128] = { 0 };
159 snprintf(msg, sizeof(msg),
160 // L10N: Confirmation message when deleting an autocrypt account
161 _("Really delete account \"%s\"?"), buf_string((*pentry)->addr->mailbox));
162 if (query_yesorno(msg, MUTT_NO) != MUTT_YES)
163 return FR_NO_ACTION;
164
165 if (mutt_autocrypt_db_account_delete((*pentry)->account) == 0)
166 populate_menu(ad->menu);
167
168 return FR_SUCCESS;
169}
170
174static int op_autocrypt_toggle_active(struct AutocryptData *ad, const struct KeyEvent *event)
175{
176 if (!ad->menu->mdata)
177 return FR_ERROR;
178
179 const int index = menu_get_index(ad->menu);
180 struct AccountEntry **pentry = ARRAY_GET(&ad->entries, index);
181 if (!pentry)
182 return 0;
183
184 if (!toggle_active((*pentry)))
185 return FR_ERROR;
186
188 return FR_SUCCESS;
189}
190
194static int op_autocrypt_toggle_prefer(struct AutocryptData *ad, const struct KeyEvent *event)
195{
196 if (!ad->menu->mdata)
197 return FR_ERROR;
198
199 const int index = menu_get_index(ad->menu);
200 struct AccountEntry **pentry = ARRAY_GET(&ad->entries, index);
201 if (!pentry)
202 return 0;
203
204 if (!toggle_prefer_encrypt((*pentry)))
205 return FR_ERROR;
206
208 return FR_SUCCESS;
209}
210
214static int op_exit(struct AutocryptData *ad, const struct KeyEvent *event)
215{
216 ad->done = true;
217 return FR_SUCCESS;
218}
219
220// -----------------------------------------------------------------------------
221
225static const struct AutocryptFunction AutocryptFunctions[] = {
226 // clang-format off
227 { OP_AUTOCRYPT_CREATE_ACCT, op_autocrypt_create_acct },
228 { OP_AUTOCRYPT_DELETE_ACCT, op_autocrypt_delete_acct },
229 { OP_AUTOCRYPT_TOGGLE_ACTIVE, op_autocrypt_toggle_active },
230 { OP_AUTOCRYPT_TOGGLE_PREFER, op_autocrypt_toggle_prefer },
231 { OP_EXIT, op_exit },
232 { 0, NULL },
233 // clang-format on
234};
235
239int autocrypt_function_dispatcher(struct MuttWindow *win, const struct KeyEvent *event)
240{
241 // The Dispatcher may be called on any Window in the Dialog
242 struct MuttWindow *dlg = dialog_find(win);
243 if (!event || !dlg || !dlg->wdata)
244 {
246 return FR_ERROR;
247 }
248
249 const int op = event->op;
250 struct Menu *menu = dlg->wdata;
251 struct AutocryptData *ad = menu->mdata;
252 if (!ad)
253 {
255 return FR_ERROR;
256 }
257
258 int rc = FR_UNKNOWN;
259 for (size_t i = 0; AutocryptFunctions[i].op != OP_NULL; i++)
260 {
261 const struct AutocryptFunction *fn = &AutocryptFunctions[i];
262 if (fn->op == op)
263 {
264 rc = fn->function(ad, event);
265 break;
266 }
267 }
268
269 if (rc == FR_UNKNOWN) // Not our function
270 return rc;
271
272 const char *result = dispatcher_get_retval_name(rc);
273 mutt_debug(LL_DEBUG1, "Handled %s (%d) -> %s\n", opcodes_get_name(op), op, NONULL(result));
274
276 return rc;
277}
Email Address Handling.
#define ARRAY_GET(head, idx)
Return the element at index.
Definition array.h:109
int mutt_autocrypt_db_account_delete(struct AutocryptAccount *acct)
Delete an Account from the Autocrypt database.
Definition db.c:434
int mutt_autocrypt_db_account_update(struct AutocryptAccount *acct)
Update Account info in the Autocrypt database.
Definition db.c:383
static const struct MenuFuncOp OpAutocrypt[]
Functions for the Autocrypt Account.
Definition functions.c:50
void autocrypt_init_keys(struct NeoMutt *n, struct SubMenu *sm_generic)
Initialise the Autocrypt Keybindings - Implements ::init_keys_api.
Definition functions.c:75
static const struct AutocryptFunction AutocryptFunctions[]
All the NeoMutt functions that the Autocrypt supports.
Definition functions.c:225
static bool toggle_active(struct AccountEntry *entry)
Toggle whether an Autocrypt account is active.
Definition functions.c:98
static const struct MenuOpSeq AutocryptDefaultBindings[]
Key bindings for the Autocrypt Account.
Definition functions.c:62
static bool toggle_prefer_encrypt(struct AccountEntry *entry)
Toggle whether an Autocrypt account prefers encryption.
Definition functions.c:119
Autocrypt functions.
Autocrypt end-to-end encryption.
Autocrypt private Module data.
Shared constants/structs that are private to Autocrypt.
int mutt_autocrypt_account_init(bool prompt)
Create a new Autocrypt account.
Definition autocrypt.c:150
Private Autocrypt Data.
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 config headers.
Convenience wrapper for the core headers.
struct MuttWindow * dialog_find(struct MuttWindow *win)
Find the parent Dialog of a Window.
Definition dialog.c:89
const char * dispatcher_get_retval_name(int rv)
Get the name of a return value.
Definition dispatcher.c:55
void dispatcher_flush_on_error(int rv)
Flush pending keys after a dispatch error.
Definition dispatcher.c:65
@ FR_SUCCESS
Valid function - successfully performed.
Definition dispatcher.h:40
@ FR_UNKNOWN
Unknown function.
Definition dispatcher.h:34
@ FR_ERROR
Valid function - error occurred.
Definition dispatcher.h:39
@ FR_NO_ACTION
Valid function - no action performed.
Definition dispatcher.h:38
bool populate_menu(struct Menu *menu)
Add the Autocrypt data to a Menu.
static int op_exit(struct AliasFunctionData *fdata, const struct KeyEvent *event)
exit this menu - Implements alias_function_t -
Definition functions.c:312
static int op_autocrypt_toggle_active(struct AutocryptData *ad, const struct KeyEvent *event)
Toggle the current account active/inactive - Implements autocrypt_function_t -.
Definition functions.c:174
static int op_autocrypt_create_acct(struct AutocryptData *ad, const struct KeyEvent *event)
Create a new autocrypt account - Implements autocrypt_function_t -.
Definition functions.c:137
static int op_autocrypt_delete_acct(struct AutocryptData *ad, const struct KeyEvent *event)
Delete the current account - Implements autocrypt_function_t -.
Definition functions.c:148
static int op_autocrypt_toggle_prefer(struct AutocryptData *ad, const struct KeyEvent *event)
Toggle the current account prefer-encrypt flag - Implements autocrypt_function_t -.
Definition functions.c:194
int autocrypt_function_dispatcher(struct MuttWindow *win, const struct KeyEvent *event)
Perform a Autocrypt function - Implements function_dispatcher_t -.
Definition functions.c:239
#define mutt_error(...)
Definition logging2.h:94
#define mutt_debug(LEVEL,...)
Definition logging2.h:91
Convenience wrapper for the gui headers.
void km_menu_add_submenu(struct MenuDefinition *md, struct SubMenu *sm)
Add a SubMenu to a Menu Definition.
Definition init.c:121
struct SubMenu * km_register_submenu(const struct MenuFuncOp functions[])
Register a submenu.
Definition init.c:87
struct MenuDefinition * km_register_menu(int menu, const char *name)
Register a menu.
Definition init.c:104
void km_menu_add_bindings(struct MenuDefinition *md, const struct MenuOpSeq bindings[])
Add Keybindings to a Menu.
Definition init.c:134
Manage keymappings.
@ LL_DEBUG1
Log at debug level 1.
Definition logging2.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:179
int menu_get_index(struct Menu *menu)
Get the current selection in the Menu.
Definition menu.c:155
@ MODULE_ID_AUTOCRYPT
ModuleAutocrypt, Autocrypt
Definition module_api.h:50
Convenience wrapper for the library headers.
#define _(a)
Definition message.h:28
void * neomutt_get_module_data(struct NeoMutt *n, enum ModuleId id)
Get the private data for a Module.
Definition neomutt.c:663
const char * opcodes_get_name(int op)
Get the name of an opcode.
Definition opcodes.c:48
@ MUTT_NO
User answered 'No', or assume 'No'.
Definition quad.h:38
@ MUTT_YES
User answered 'Yes', or assume 'Yes'.
Definition quad.h:39
Ask the user a question.
enum QuadOption query_yesorno(const char *prompt, enum QuadOption def)
Ask the user a Yes/No question.
Definition question.c:329
#define ASSERT(COND)
Definition signal2.h:59
#define NONULL(x)
Definition string2.h:44
An entry in the Autocrypt account Menu.
Definition private.h:45
struct AutocryptAccount * account
Account details.
Definition private.h:47
bool enabled
Is this account enabled.
Definition lib.h:119
bool prefer_encrypt
false = nopref, true = mutual
Definition lib.h:118
Data to pass to the Autocrypt Functions.
bool done
Should we close the Dialog?
struct Menu * menu
Autocrypt Menu.
struct AccountEntryArray entries
Account Entries.
A NeoMutt function.
Definition functions.h:49
autocrypt_function_t function
Function to call.
Definition functions.h:51
int op
Op code, e.g. OP_AUTOCRYPT_CREATE_ACCT.
Definition functions.h:50
Autocrypt private Module data.
Definition module_data.h:32
struct MenuDefinition * menu_autocrypt
Autocrypt menu definition.
Definition module_data.h:34
An event such as a keypress.
Definition get.h:57
Functions for a Dialog or Window.
Definition menu.h:77
Mapping between a function and an operation.
Definition menu.h:35
Mapping between an operation and a key sequence.
Definition menu.h:45
Definition lib.h:80
void * mdata
Private data.
Definition lib.h:149
void * wdata
Private data.
Container for Accounts, Notifications.
Definition neomutt.h:41
Collection of related functions.
Definition menu.h:65
@ MENU_AUTOCRYPT
Autocrypt Account menu.
Definition type.h:37