NeoMutt  2024-12-12-19-ge4b57e
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
functions.c
Go to the documentation of this file.
1
29#include "config.h"
30#ifdef _MAKEDOC
31#include "docs/makedoc_defs.h"
32#else
33#include <stdbool.h>
34#include <stdio.h>
35#include "private.h"
36#include "mutt/lib.h"
37#include "address/lib.h"
38#include "config/lib.h"
39#include "core/lib.h"
40#include "gui/lib.h"
41#include "lib.h"
42#include "key/lib.h"
43#include "menu/lib.h"
44#include "question/lib.h"
45#include "autocrypt_data.h"
46#include "functions.h"
47#endif
48
49// clang-format off
50#ifdef USE_AUTOCRYPT
54const struct MenuFuncOp OpAutocrypt[] = { /* map: autocrypt account */
55 { "create-account", OP_AUTOCRYPT_CREATE_ACCT },
56 { "delete-account", OP_AUTOCRYPT_DELETE_ACCT },
57 { "exit", OP_EXIT },
58 { "toggle-active", OP_AUTOCRYPT_TOGGLE_ACTIVE },
59 { "toggle-prefer-encrypt", OP_AUTOCRYPT_TOGGLE_PREFER },
60 { NULL, 0 }
61};
62#endif
63
64#ifdef USE_AUTOCRYPT
68const struct MenuOpSeq AutocryptDefaultBindings[] = { /* map: autocrypt account */
69 { OP_AUTOCRYPT_CREATE_ACCT, "c" },
70 { OP_AUTOCRYPT_DELETE_ACCT, "D" },
71 { OP_AUTOCRYPT_TOGGLE_ACTIVE, "a" },
72 { OP_AUTOCRYPT_TOGGLE_PREFER, "p" },
73 { OP_EXIT, "q" },
74 { 0, NULL }
75};
76#endif
77// clang-format on
78
83static void toggle_active(struct AccountEntry *entry)
84{
85 entry->account->enabled = !entry->account->enabled;
87 {
88 entry->account->enabled = !entry->account->enabled;
89 /* L10N: This error message is displayed if a database update of an
90 account record fails for some odd reason. */
91 mutt_error(_("Error updating account record"));
92 }
93}
94
99static void toggle_prefer_encrypt(struct AccountEntry *entry)
100{
101 entry->account->prefer_encrypt = !entry->account->prefer_encrypt;
103 {
104 entry->account->prefer_encrypt = !entry->account->prefer_encrypt;
105 mutt_error(_("Error updating account record"));
106 }
107}
108
109// -----------------------------------------------------------------------------
110
114static int op_autocrypt_create_acct(struct AutocryptData *ad, int op)
115{
116 if (mutt_autocrypt_account_init(false) == 0)
117 populate_menu(ad->menu);
118
119 return FR_SUCCESS;
120}
121
125static int op_autocrypt_delete_acct(struct AutocryptData *ad, int op)
126{
127 if (!ad->menu->mdata)
128 return FR_ERROR;
129
130 const int index = menu_get_index(ad->menu);
131 struct AccountEntry **pentry = ARRAY_GET(&ad->entries, index);
132 if (!pentry)
133 return 0;
134
135 char msg[128] = { 0 };
136 snprintf(msg, sizeof(msg),
137 // L10N: Confirmation message when deleting an autocrypt account
138 _("Really delete account \"%s\"?"), buf_string((*pentry)->addr->mailbox));
139 if (query_yesorno(msg, MUTT_NO) != MUTT_YES)
140 return FR_NO_ACTION;
141
142 if (mutt_autocrypt_db_account_delete((*pentry)->account) == 0)
143 populate_menu(ad->menu);
144
145 return FR_SUCCESS;
146}
147
151static int op_autocrypt_toggle_active(struct AutocryptData *ad, int op)
152{
153 if (!ad->menu->mdata)
154 return FR_ERROR;
155
156 const int index = menu_get_index(ad->menu);
157 struct AccountEntry **pentry = ARRAY_GET(&ad->entries, index);
158 if (!pentry)
159 return 0;
160
161 toggle_active((*pentry));
163
164 return FR_SUCCESS;
165}
166
170static int op_autocrypt_toggle_prefer(struct AutocryptData *ad, int op)
171{
172 if (!ad->menu->mdata)
173 return FR_ERROR;
174
175 const int index = menu_get_index(ad->menu);
176 struct AccountEntry **pentry = ARRAY_GET(&ad->entries, index);
177 if (!pentry)
178 return 0;
179
180 toggle_prefer_encrypt((*pentry));
182
183 return FR_SUCCESS;
184}
185
189static int op_exit(struct AutocryptData *ad, int op)
190{
191 ad->done = true;
192 return FR_SUCCESS;
193}
194
195// -----------------------------------------------------------------------------
196
200static const struct AutocryptFunction AutocryptFunctions[] = {
201 // clang-format off
202 { OP_AUTOCRYPT_CREATE_ACCT, op_autocrypt_create_acct },
203 { OP_AUTOCRYPT_DELETE_ACCT, op_autocrypt_delete_acct },
204 { OP_AUTOCRYPT_TOGGLE_ACTIVE, op_autocrypt_toggle_active },
205 { OP_AUTOCRYPT_TOGGLE_PREFER, op_autocrypt_toggle_prefer },
206 { OP_EXIT, op_exit },
207 { 0, NULL },
208 // clang-format on
209};
210
215{
216 // The Dispatcher may be called on any Window in the Dialog
217 struct MuttWindow *dlg = dialog_find(win);
218 if (!dlg || !dlg->wdata)
219 return FR_ERROR;
220
221 struct Menu *menu = dlg->wdata;
222 struct AutocryptData *ad = menu->mdata;
223
224 int rc = FR_UNKNOWN;
225 for (size_t i = 0; AutocryptFunctions[i].op != OP_NULL; i++)
226 {
227 const struct AutocryptFunction *fn = &AutocryptFunctions[i];
228 if (fn->op == op)
229 {
230 rc = fn->function(ad, op);
231 break;
232 }
233 }
234
235 if (rc == FR_UNKNOWN) // Not our function
236 return rc;
237
238 const char *result = dispatcher_get_retval_name(rc);
239 mutt_debug(LL_DEBUG1, "Handled %s (%d) -> %s\n", opcodes_get_name(op), op, NONULL(result));
240
241 return rc;
242}
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:83
const struct MenuFuncOp OpAutocrypt[]
Functions for the Autocrypt Account.
Definition: functions.c:54
static const struct AutocryptFunction AutocryptFunctions[]
All the NeoMutt functions that the Autocrypt supports.
Definition: functions.c:200
const struct MenuOpSeq AutocryptDefaultBindings[]
Key bindings for the Autocrypt Account.
Definition: functions.c:68
static void toggle_prefer_encrypt(struct AccountEntry *entry)
Toggle whether an Autocrypt account prefers encryption.
Definition: functions.c:99
int mutt_autocrypt_account_init(bool prompt)
Create a new Autocrypt account.
Definition: autocrypt.c:143
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:39
@ FR_UNKNOWN
Unknown function.
Definition: dispatcher.h:33
@ FR_ERROR
Valid function - error occurred.
Definition: dispatcher.h:38
@ FR_NO_ACTION
Valid function - no action performed.
Definition: dispatcher.h:37
bool populate_menu(struct Menu *menu)
Add the Autocrypt data to a Menu.
static int op_exit(struct AliasMenuData *mdata, int op)
exit this menu - Implements alias_function_t -
Definition: functions.c:201
static int op_autocrypt_toggle_prefer(struct AutocryptData *ad, int op)
Toggle the current account prefer-encrypt flag - Implements autocrypt_function_t -.
Definition: functions.c:170
static int op_autocrypt_delete_acct(struct AutocryptData *ad, int op)
Delete the current account - Implements autocrypt_function_t -.
Definition: functions.c:125
static int op_autocrypt_toggle_active(struct AutocryptData *ad, int op)
Toggle the current account active/inactive - Implements autocrypt_function_t -.
Definition: functions.c:151
static int op_autocrypt_create_acct(struct AutocryptData *ad, int op)
Create a new autocrypt account - Implements autocrypt_function_t -.
Definition: functions.c:114
int autocrypt_function_dispatcher(struct MuttWindow *win, int op)
Perform a Autocrypt function - Implements function_dispatcher_t -.
Definition: functions.c:214
#define mutt_error(...)
Definition: logging2.h:92
#define mutt_debug(LEVEL,...)
Definition: logging2.h:89
Convenience wrapper for the gui headers.
Manage keymappings.
@ LL_DEBUG1
Log at debug level 1.
Definition: logging2.h:43
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
int menu_get_index(struct Menu *menu)
Get the current selection in the Menu.
Definition: menu.c:160
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:327
Sidebar functions.
GUI display the mailboxes in a side panel.
Key value store.
#define NONULL(x)
Definition: string2.h:37
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:115
bool prefer_encrypt
false = nopref, true = mutual
Definition: lib.h:114
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:45
autocrypt_function_t function
Function to call.
Definition: functions.h:47
int op
Op code, e.g. OP_AUTOCRYPT_CREATE_ACCT.
Definition: functions.h:46
Mapping between a function and an operation.
Definition: lib.h:101
Mapping between an operation and a key sequence.
Definition: lib.h:110
int op
Operation, e.g. OP_DELETE.
Definition: lib.h:111
Definition: lib.h:79
void * mdata
Private data.
Definition: lib.h:147
void * wdata
Private data.
Definition: mutt_window.h:145