NeoMutt  2025-12-11-1039-g550ac6
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 { "toggle-enabled", OP_AUTOCRYPT_TOGGLE_ENABLED },
54 { "toggle-prefer-encrypt", OP_AUTOCRYPT_TOGGLE_PREFER },
55
56 // Deprecated
57 { "toggle-active", OP_AUTOCRYPT_TOGGLE_ENABLED, MFF_DEPRECATED },
58 { NULL, 0 }
59};
60
64static const struct MenuOpSeq AutocryptDefaultBindings[] = { /* map: autocrypt account */
65 { OP_AUTOCRYPT_CREATE_ACCT, "c" },
66 { OP_AUTOCRYPT_DELETE_ACCT, "D" },
67 { OP_AUTOCRYPT_TOGGLE_ENABLED, "a" },
68 { OP_AUTOCRYPT_TOGGLE_PREFER, "p" },
69 { 0, NULL }
70};
71// clang-format on
72
76void autocrypt_init_keys(struct NeoMutt *n, struct SubMenu *sm_generic)
77{
79 ASSERT(mod_data);
80
81 struct MenuDefinition *md = NULL;
82 struct SubMenu *sm = NULL;
83
85 md = km_register_menu(MENU_AUTOCRYPT, "autocrypt");
86 km_menu_add_submenu(md, sm);
87 km_menu_add_submenu(md, sm_generic);
89
90 mod_data->menu_autocrypt = md;
91}
92
99static bool toggle_enabled(struct AccountEntry *entry)
100{
101 entry->account->enabled = !entry->account->enabled;
103 {
104 entry->account->enabled = !entry->account->enabled;
105 /* L10N: This error message is displayed if a database update of an
106 account record fails for some odd reason. */
107 mutt_error(_("Error updating account record"));
108 return false;
109 }
110
111 return true;
112}
113
120static bool toggle_prefer_encrypt(struct AccountEntry *entry)
121{
122 entry->account->prefer_encrypt = !entry->account->prefer_encrypt;
124 {
125 entry->account->prefer_encrypt = !entry->account->prefer_encrypt;
126 mutt_error(_("Error updating account record"));
127 return false;
128 }
129
130 return true;
131}
132
133// -----------------------------------------------------------------------------
134
138static int op_autocrypt_create_acct(struct AutocryptData *ad, const struct KeyEvent *event)
139{
140 if (mutt_autocrypt_account_init(false) == 0)
141 populate_menu(ad->menu);
142
143 return FR_SUCCESS;
144}
145
149static int op_autocrypt_delete_acct(struct AutocryptData *ad, const struct KeyEvent *event)
150{
151 if (!ad->menu->mdata)
152 return FR_ERROR;
153
154 const int index = menu_get_index(ad->menu);
155 struct AccountEntry **pentry = ARRAY_GET(&ad->entries, index);
156 if (!pentry)
157 return 0;
158
159 char msg[128] = { 0 };
160 snprintf(msg, sizeof(msg),
161 // L10N: Confirmation message when deleting an autocrypt account
162 _("Really delete account \"%s\"?"), buf_string((*pentry)->addr->mailbox));
163 if (query_yesorno(msg, MUTT_NO) != MUTT_YES)
164 return FR_NO_ACTION;
165
166 if (mutt_autocrypt_db_account_delete((*pentry)->account) == 0)
167 populate_menu(ad->menu);
168
169 return FR_SUCCESS;
170}
171
175static int op_autocrypt_toggle_enabled(struct AutocryptData *ad, const struct KeyEvent *event)
176{
177 if (!ad->menu->mdata)
178 return FR_ERROR;
179
180 const int index = menu_get_index(ad->menu);
181 struct AccountEntry **pentry = ARRAY_GET(&ad->entries, index);
182 if (!pentry)
183 return 0;
184
185 if (!toggle_enabled((*pentry)))
186 return FR_ERROR;
187
189 return FR_SUCCESS;
190}
191
195static int op_autocrypt_toggle_prefer(struct AutocryptData *ad, const struct KeyEvent *event)
196{
197 if (!ad->menu->mdata)
198 return FR_ERROR;
199
200 const int index = menu_get_index(ad->menu);
201 struct AccountEntry **pentry = ARRAY_GET(&ad->entries, index);
202 if (!pentry)
203 return 0;
204
205 if (!toggle_prefer_encrypt((*pentry)))
206 return FR_ERROR;
207
209 return FR_SUCCESS;
210}
211
215static int op_quit(struct AutocryptData *ad, const struct KeyEvent *event)
216{
217 ad->done = true;
218 return FR_SUCCESS;
219}
220
221// -----------------------------------------------------------------------------
222
226static const struct AutocryptFunction AutocryptFunctions[] = {
227 // clang-format off
228 { OP_AUTOCRYPT_CREATE_ACCT, op_autocrypt_create_acct },
229 { OP_AUTOCRYPT_DELETE_ACCT, op_autocrypt_delete_acct },
230 { OP_AUTOCRYPT_TOGGLE_ENABLED, op_autocrypt_toggle_enabled },
231 { OP_AUTOCRYPT_TOGGLE_PREFER, op_autocrypt_toggle_prefer },
232 { OP_EXIT, op_quit },
233 { OP_QUIT, op_quit },
234 { 0, NULL },
235 // clang-format on
236};
237
241int autocrypt_function_dispatcher(struct MuttWindow *win, const struct KeyEvent *event)
242{
243 // The Dispatcher may be called on any Window in the Dialog
244 struct MuttWindow *dlg = dialog_find(win);
245 if (!event || !dlg || !dlg->wdata)
246 {
248 return FR_ERROR;
249 }
250
251 const int op = event->op;
252 struct Menu *menu = dlg->wdata;
253 struct AutocryptData *ad = menu->mdata;
254 if (!ad)
255 {
257 return FR_ERROR;
258 }
259
260 int rc = FR_UNKNOWN;
261 for (size_t i = 0; AutocryptFunctions[i].op != OP_NULL; i++)
262 {
263 const struct AutocryptFunction *fn = &AutocryptFunctions[i];
264 if (fn->op == op)
265 {
266 rc = fn->function(ad, event);
267 break;
268 }
269 }
270
271 if (rc == FR_UNKNOWN) // Not our function
272 return rc;
273
274 const char *result = dispatcher_get_retval_name(rc);
275 mutt_debug(LL_DEBUG1, "Handled %s (%d) -> %s\n", opcodes_get_name(op), op, NONULL(result));
276
278 return rc;
279}
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:76
static const struct AutocryptFunction AutocryptFunctions[]
All the NeoMutt functions that the Autocrypt supports.
Definition functions.c:226
static const struct MenuOpSeq AutocryptDefaultBindings[]
Key bindings for the Autocrypt Account.
Definition functions.c:64
static bool toggle_prefer_encrypt(struct AccountEntry *entry)
Toggle whether an Autocrypt account prefers encryption.
Definition functions.c:120
static bool toggle_enabled(struct AccountEntry *entry)
Toggle whether an Autocrypt account is enabled.
Definition functions.c:99
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.
@ MFF_DEPRECATED
Function is deprecated.
Definition get.h:67
static int op_quit(struct AliasFunctionData *fdata, const struct KeyEvent *event)
Save changes and exit this dialog - Implements alias_function_t -.
Definition functions.c:308
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:138
static int op_autocrypt_delete_acct(struct AutocryptData *ad, const struct KeyEvent *event)
Delete the current account - Implements autocrypt_function_t -.
Definition functions.c:149
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:195
static int op_autocrypt_toggle_enabled(struct AutocryptData *ad, const struct KeyEvent *event)
Toggle the current account enabled/disabled - Implements autocrypt_function_t -.
Definition functions.c:175
int autocrypt_function_dispatcher(struct MuttWindow *win, const struct KeyEvent *event)
Perform a Autocrypt function - Implements function_dispatcher_t -.
Definition functions.c:241
#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:123
struct SubMenu * km_register_submenu(const struct MenuFuncOp functions[])
Register a submenu.
Definition init.c:88
struct MenuDefinition * km_register_menu(int menu, const char *name)
Register a menu.
Definition init.c:105
void km_menu_add_bindings(struct MenuDefinition *md, const struct MenuOpSeq bindings[])
Add Keybindings to a Menu.
Definition init.c:136
Manage keymappings.
@ LL_DEBUG1
Log at debug level 1.
Definition logging2.h:45
GUI present the user with a selectable list.
void menu_queue_redraw(struct Menu *menu, MenuRedrawFlags redraw)
Queue a request for a redraw.
Definition menu.c:177
int menu_get_index(struct Menu *menu)
Get the current selection in the Menu.
Definition menu.c:153
@ MENU_REDRAW_FULL
Redraw everything.
Definition lib.h:64
@ 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:666
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:75
Functions for a Dialog or Window.
Definition menudef.h:44
Mapping between a function and an operation.
Definition menu.h:37
Mapping between an operation and a key sequence.
Definition menu.h:47
Definition lib.h:86
void * mdata
Private data.
Definition lib.h:155
void * wdata
Private data.
Container for Accounts, Notifications.
Definition neomutt.h:41
Collection of related functions.
Definition menudef.h:33
@ MENU_AUTOCRYPT
Autocrypt Account menu.
Definition type.h:37