NeoMutt  2025-12-11-980-ge38c27
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-active", OP_AUTOCRYPT_TOGGLE_ACTIVE },
54 { "toggle-prefer-encrypt", OP_AUTOCRYPT_TOGGLE_PREFER },
55 { NULL, 0 }
56};
57
61static const struct MenuOpSeq AutocryptDefaultBindings[] = { /* map: autocrypt account */
62 { OP_AUTOCRYPT_CREATE_ACCT, "c" },
63 { OP_AUTOCRYPT_DELETE_ACCT, "D" },
64 { OP_AUTOCRYPT_TOGGLE_ACTIVE, "a" },
65 { OP_AUTOCRYPT_TOGGLE_PREFER, "p" },
66 { 0, NULL }
67};
68// clang-format on
69
73void autocrypt_init_keys(struct NeoMutt *n, struct SubMenu *sm_generic)
74{
76 ASSERT(mod_data);
77
78 struct MenuDefinition *md = NULL;
79 struct SubMenu *sm = NULL;
80
82 md = km_register_menu(MENU_AUTOCRYPT, "autocrypt");
83 km_menu_add_submenu(md, sm);
84 km_menu_add_submenu(md, sm_generic);
86
87 mod_data->menu_autocrypt = md;
88}
89
96static bool toggle_active(struct AccountEntry *entry)
97{
98 entry->account->enabled = !entry->account->enabled;
100 {
101 entry->account->enabled = !entry->account->enabled;
102 /* L10N: This error message is displayed if a database update of an
103 account record fails for some odd reason. */
104 mutt_error(_("Error updating account record"));
105 return false;
106 }
107
108 return true;
109}
110
117static bool toggle_prefer_encrypt(struct AccountEntry *entry)
118{
119 entry->account->prefer_encrypt = !entry->account->prefer_encrypt;
121 {
122 entry->account->prefer_encrypt = !entry->account->prefer_encrypt;
123 mutt_error(_("Error updating account record"));
124 return false;
125 }
126
127 return true;
128}
129
130// -----------------------------------------------------------------------------
131
135static int op_autocrypt_create_acct(struct AutocryptData *ad, const struct KeyEvent *event)
136{
137 if (mutt_autocrypt_account_init(false) == 0)
138 populate_menu(ad->menu);
139
140 return FR_SUCCESS;
141}
142
146static int op_autocrypt_delete_acct(struct AutocryptData *ad, const struct KeyEvent *event)
147{
148 if (!ad->menu->mdata)
149 return FR_ERROR;
150
151 const int index = menu_get_index(ad->menu);
152 struct AccountEntry **pentry = ARRAY_GET(&ad->entries, index);
153 if (!pentry)
154 return 0;
155
156 char msg[128] = { 0 };
157 snprintf(msg, sizeof(msg),
158 // L10N: Confirmation message when deleting an autocrypt account
159 _("Really delete account \"%s\"?"), buf_string((*pentry)->addr->mailbox));
160 if (query_yesorno(msg, MUTT_NO) != MUTT_YES)
161 return FR_NO_ACTION;
162
163 if (mutt_autocrypt_db_account_delete((*pentry)->account) == 0)
164 populate_menu(ad->menu);
165
166 return FR_SUCCESS;
167}
168
172static int op_autocrypt_toggle_active(struct AutocryptData *ad, const struct KeyEvent *event)
173{
174 if (!ad->menu->mdata)
175 return FR_ERROR;
176
177 const int index = menu_get_index(ad->menu);
178 struct AccountEntry **pentry = ARRAY_GET(&ad->entries, index);
179 if (!pentry)
180 return 0;
181
182 if (!toggle_active((*pentry)))
183 return FR_ERROR;
184
186 return FR_SUCCESS;
187}
188
192static int op_autocrypt_toggle_prefer(struct AutocryptData *ad, const struct KeyEvent *event)
193{
194 if (!ad->menu->mdata)
195 return FR_ERROR;
196
197 const int index = menu_get_index(ad->menu);
198 struct AccountEntry **pentry = ARRAY_GET(&ad->entries, index);
199 if (!pentry)
200 return 0;
201
202 if (!toggle_prefer_encrypt((*pentry)))
203 return FR_ERROR;
204
206 return FR_SUCCESS;
207}
208
212static int op_quit(struct AutocryptData *ad, const struct KeyEvent *event)
213{
214 ad->done = true;
215 return FR_SUCCESS;
216}
217
218// -----------------------------------------------------------------------------
219
223static const struct AutocryptFunction AutocryptFunctions[] = {
224 // clang-format off
225 { OP_AUTOCRYPT_CREATE_ACCT, op_autocrypt_create_acct },
226 { OP_AUTOCRYPT_DELETE_ACCT, op_autocrypt_delete_acct },
227 { OP_AUTOCRYPT_TOGGLE_ACTIVE, op_autocrypt_toggle_active },
228 { OP_AUTOCRYPT_TOGGLE_PREFER, op_autocrypt_toggle_prefer },
229 { OP_EXIT, op_quit },
230 { OP_QUIT, op_quit },
231 { 0, NULL },
232 // clang-format on
233};
234
238int autocrypt_function_dispatcher(struct MuttWindow *win, const struct KeyEvent *event)
239{
240 // The Dispatcher may be called on any Window in the Dialog
241 struct MuttWindow *dlg = dialog_find(win);
242 if (!event || !dlg || !dlg->wdata)
243 {
245 return FR_ERROR;
246 }
247
248 const int op = event->op;
249 struct Menu *menu = dlg->wdata;
250 struct AutocryptData *ad = menu->mdata;
251 if (!ad)
252 {
254 return FR_ERROR;
255 }
256
257 int rc = FR_UNKNOWN;
258 for (size_t i = 0; AutocryptFunctions[i].op != OP_NULL; i++)
259 {
260 const struct AutocryptFunction *fn = &AutocryptFunctions[i];
261 if (fn->op == op)
262 {
263 rc = fn->function(ad, event);
264 break;
265 }
266 }
267
268 if (rc == FR_UNKNOWN) // Not our function
269 return rc;
270
271 const char *result = dispatcher_get_retval_name(rc);
272 mutt_debug(LL_DEBUG1, "Handled %s (%d) -> %s\n", opcodes_get_name(op), op, NONULL(result));
273
275 return rc;
276}
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:73
static const struct AutocryptFunction AutocryptFunctions[]
All the NeoMutt functions that the Autocrypt supports.
Definition functions.c:223
static bool toggle_active(struct AccountEntry *entry)
Toggle whether an Autocrypt account is active.
Definition functions.c:96
static const struct MenuOpSeq AutocryptDefaultBindings[]
Key bindings for the Autocrypt Account.
Definition functions.c:61
static bool toggle_prefer_encrypt(struct AccountEntry *entry)
Toggle whether an Autocrypt account prefers encryption.
Definition functions.c:117
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_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_toggle_active(struct AutocryptData *ad, const struct KeyEvent *event)
Toggle the current account active/inactive - Implements autocrypt_function_t -.
Definition functions.c:172
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:135
static int op_autocrypt_delete_acct(struct AutocryptData *ad, const struct KeyEvent *event)
Delete the current account - Implements autocrypt_function_t -.
Definition functions.c:146
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:192
int autocrypt_function_dispatcher(struct MuttWindow *win, const struct KeyEvent *event)
Perform a Autocrypt function - Implements function_dispatcher_t -.
Definition functions.c:238
#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