NeoMutt  2025-12-11-694-ga89709
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
47
48// clang-format off
52static const struct MenuFuncOp OpAutocrypt[] = { /* map: autocrypt account */
53 { "create-account", OP_AUTOCRYPT_CREATE_ACCT },
54 { "delete-account", OP_AUTOCRYPT_DELETE_ACCT },
55 { "exit", OP_EXIT },
56 { "toggle-active", OP_AUTOCRYPT_TOGGLE_ACTIVE },
57 { "toggle-prefer-encrypt", OP_AUTOCRYPT_TOGGLE_PREFER },
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_ACTIVE, "a" },
68 { OP_AUTOCRYPT_TOGGLE_PREFER, "p" },
69 { OP_EXIT, "q" },
70 { 0, NULL }
71};
72// clang-format on
73
77void autocrypt_init_keys(struct SubMenu *sm_generic)
78{
79 struct MenuDefinition *md = NULL;
80 struct SubMenu *sm = NULL;
81
83 md = km_register_menu(MENU_AUTOCRYPT, "autocrypt");
84 km_menu_add_submenu(md, sm);
85 km_menu_add_submenu(md, sm_generic);
87
88 MdAutocrypt = md;
89}
90
95static void toggle_active(struct AccountEntry *entry)
96{
97 entry->account->enabled = !entry->account->enabled;
99 {
100 entry->account->enabled = !entry->account->enabled;
101 /* L10N: This error message is displayed if a database update of an
102 account record fails for some odd reason. */
103 mutt_error(_("Error updating account record"));
104 }
105}
106
111static void toggle_prefer_encrypt(struct AccountEntry *entry)
112{
113 entry->account->prefer_encrypt = !entry->account->prefer_encrypt;
115 {
116 entry->account->prefer_encrypt = !entry->account->prefer_encrypt;
117 mutt_error(_("Error updating account record"));
118 }
119}
120
121// -----------------------------------------------------------------------------
122
126static int op_autocrypt_create_acct(struct AutocryptData *ad, const struct KeyEvent *event)
127{
128 if (mutt_autocrypt_account_init(false) == 0)
129 populate_menu(ad->menu);
130
131 return FR_SUCCESS;
132}
133
137static int op_autocrypt_delete_acct(struct AutocryptData *ad, const struct KeyEvent *event)
138{
139 if (!ad->menu->mdata)
140 return FR_ERROR;
141
142 const int index = menu_get_index(ad->menu);
143 struct AccountEntry **pentry = ARRAY_GET(&ad->entries, index);
144 if (!pentry)
145 return 0;
146
147 char msg[128] = { 0 };
148 snprintf(msg, sizeof(msg),
149 // L10N: Confirmation message when deleting an autocrypt account
150 _("Really delete account \"%s\"?"), buf_string((*pentry)->addr->mailbox));
151 if (query_yesorno(msg, MUTT_NO) != MUTT_YES)
152 return FR_NO_ACTION;
153
154 if (mutt_autocrypt_db_account_delete((*pentry)->account) == 0)
155 populate_menu(ad->menu);
156
157 return FR_SUCCESS;
158}
159
163static int op_autocrypt_toggle_active(struct AutocryptData *ad, const struct KeyEvent *event)
164{
165 if (!ad->menu->mdata)
166 return FR_ERROR;
167
168 const int index = menu_get_index(ad->menu);
169 struct AccountEntry **pentry = ARRAY_GET(&ad->entries, index);
170 if (!pentry)
171 return 0;
172
173 toggle_active((*pentry));
175
176 return FR_SUCCESS;
177}
178
182static int op_autocrypt_toggle_prefer(struct AutocryptData *ad, const struct KeyEvent *event)
183{
184 if (!ad->menu->mdata)
185 return FR_ERROR;
186
187 const int index = menu_get_index(ad->menu);
188 struct AccountEntry **pentry = ARRAY_GET(&ad->entries, index);
189 if (!pentry)
190 return 0;
191
192 toggle_prefer_encrypt((*pentry));
194
195 return FR_SUCCESS;
196}
197
201static int op_exit(struct AutocryptData *ad, const struct KeyEvent *event)
202{
203 ad->done = true;
204 return FR_SUCCESS;
205}
206
207// -----------------------------------------------------------------------------
208
212static const struct AutocryptFunction AutocryptFunctions[] = {
213 // clang-format off
214 { OP_AUTOCRYPT_CREATE_ACCT, op_autocrypt_create_acct },
215 { OP_AUTOCRYPT_DELETE_ACCT, op_autocrypt_delete_acct },
216 { OP_AUTOCRYPT_TOGGLE_ACTIVE, op_autocrypt_toggle_active },
217 { OP_AUTOCRYPT_TOGGLE_PREFER, op_autocrypt_toggle_prefer },
218 { OP_EXIT, op_exit },
219 { 0, NULL },
220 // clang-format on
221};
222
226int autocrypt_function_dispatcher(struct MuttWindow *win, const struct KeyEvent *event)
227{
228 // The Dispatcher may be called on any Window in the Dialog
229 struct MuttWindow *dlg = dialog_find(win);
230 if (!event || !dlg || !dlg->wdata)
231 return FR_ERROR;
232
233 const int op = event->op;
234 struct Menu *menu = dlg->wdata;
235 struct AutocryptData *ad = menu->mdata;
236 if (!ad)
237 return FR_ERROR;
238
239 int rc = FR_UNKNOWN;
240 for (size_t i = 0; AutocryptFunctions[i].op != OP_NULL; i++)
241 {
242 const struct AutocryptFunction *fn = &AutocryptFunctions[i];
243 if (fn->op == op)
244 {
245 rc = fn->function(ad, event);
246 break;
247 }
248 }
249
250 if (rc == FR_UNKNOWN) // Not our function
251 return rc;
252
253 const char *result = dispatcher_get_retval_name(rc);
254 mutt_debug(LL_DEBUG1, "Handled %s (%d) -> %s\n", opcodes_get_name(op), op, NONULL(result));
255
256 return rc;
257}
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:427
int mutt_autocrypt_db_account_update(struct AutocryptAccount *acct)
Update Account info in the Autocrypt database.
Definition db.c:381
static void toggle_active(struct AccountEntry *entry)
Toggle whether an Autocrypt account is active.
Definition functions.c:95
static const struct MenuFuncOp OpAutocrypt[]
Functions for the Autocrypt Account.
Definition functions.c:52
static const struct AutocryptFunction AutocryptFunctions[]
All the NeoMutt functions that the Autocrypt supports.
Definition functions.c:212
struct MenuDefinition * MdAutocrypt
Autocrypt Menu Definition.
Definition functions.c:46
void autocrypt_init_keys(struct SubMenu *sm_generic)
Initialise the Autocrypt Keybindings - Implements ::init_keys_api.
Definition functions.c:77
static const struct MenuOpSeq AutocryptDefaultBindings[]
Key bindings for the Autocrypt Account.
Definition functions.c:64
static void toggle_prefer_encrypt(struct AccountEntry *entry)
Toggle whether an Autocrypt account prefers encryption.
Definition functions.c:111
Autocrypt functions.
Autocrypt end-to-end encryption.
Shared constants/structs that are private to Autocrypt.
int mutt_autocrypt_account_init(bool prompt)
Create a new Autocrypt account.
Definition autocrypt.c:147
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:54
@ 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 AliasMenuData *mdata, const struct KeyEvent *event)
exit this menu - Implements alias_function_t -
Definition functions.c:234
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:163
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:126
static int op_autocrypt_delete_acct(struct AutocryptData *ad, const struct KeyEvent *event)
Delete the current account - Implements autocrypt_function_t -.
Definition functions.c:137
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:182
int autocrypt_function_dispatcher(struct MuttWindow *win, const struct KeyEvent *event)
Perform a Autocrypt function - Implements function_dispatcher_t -.
Definition functions.c:226
#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:91
struct MenuDefinition * km_register_menu(int menu, const char *name)
Register a menu.
Definition init.c:107
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.
#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:188
int menu_get_index(struct Menu *menu)
Get the current selection in the Menu.
Definition menu.c:164
Convenience wrapper for the library headers.
#define _(a)
Definition message.h:28
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 NONULL(x)
Definition string2.h:44
An entry in the Autocrypt account Menu.
Definition private.h:46
struct AutocryptAccount * account
Account details.
Definition private.h:48
bool enabled
Is this account enabled.
Definition lib.h:118
bool prefer_encrypt
false = nopref, true = mutual
Definition lib.h:117
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:51
autocrypt_function_t function
Function to call.
Definition functions.h:53
int op
Op code, e.g. OP_AUTOCRYPT_CREATE_ACCT.
Definition functions.h:52
An event such as a keypress.
Definition get.h:50
Functions for a Dialog or Window.
Definition menu.h:80
Mapping between a function and an operation.
Definition menu.h:38
Mapping between an operation and a key sequence.
Definition menu.h:48
Definition lib.h:80
void * mdata
Private data.
Definition lib.h:149
void * wdata
Private data.
Collection of related functions.
Definition menu.h:68
@ MENU_AUTOCRYPT
Autocrypt Account menu.
Definition type.h:37