NeoMutt  2024-11-14-34-g5aaf0d
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 <stdio.h>
34#include "private.h"
35#include "mutt/lib.h"
36#include "address/lib.h"
37#include "config/lib.h"
38#include "core/lib.h"
39#include "gui/lib.h"
40#include "lib.h"
41#include "key/lib.h"
42#include "menu/lib.h"
43#include "question/lib.h"
44#include "autocrypt_data.h"
45#include "functions.h"
46#endif
47
48// clang-format off
49#ifdef USE_AUTOCRYPT
53const struct MenuFuncOp OpAutocrypt[] = { /* map: autocrypt account */
54 { "create-account", OP_AUTOCRYPT_CREATE_ACCT },
55 { "delete-account", OP_AUTOCRYPT_DELETE_ACCT },
56 { "exit", OP_EXIT },
57 { "toggle-active", OP_AUTOCRYPT_TOGGLE_ACTIVE },
58 { "toggle-prefer-encrypt", OP_AUTOCRYPT_TOGGLE_PREFER },
59 { NULL, 0 }
60};
61#endif
62
63#ifdef USE_AUTOCRYPT
67const struct MenuOpSeq AutocryptDefaultBindings[] = { /* map: autocrypt account */
68 { OP_AUTOCRYPT_CREATE_ACCT, "c" },
69 { OP_AUTOCRYPT_DELETE_ACCT, "D" },
70 { OP_AUTOCRYPT_TOGGLE_ACTIVE, "a" },
71 { OP_AUTOCRYPT_TOGGLE_PREFER, "p" },
72 { OP_EXIT, "q" },
73 { 0, NULL }
74};
75#endif
76// clang-format on
77
82static void toggle_active(struct AccountEntry *entry)
83{
84 entry->account->enabled = !entry->account->enabled;
86 {
87 entry->account->enabled = !entry->account->enabled;
88 /* L10N: This error message is displayed if a database update of an
89 account record fails for some odd reason. */
90 mutt_error(_("Error updating account record"));
91 }
92}
93
98static void toggle_prefer_encrypt(struct AccountEntry *entry)
99{
100 entry->account->prefer_encrypt = !entry->account->prefer_encrypt;
102 {
103 entry->account->prefer_encrypt = !entry->account->prefer_encrypt;
104 mutt_error(_("Error updating account record"));
105 }
106}
107
108// -----------------------------------------------------------------------------
109
113static int op_autocrypt_create_acct(struct AutocryptData *ad, int op)
114{
115 if (mutt_autocrypt_account_init(false) == 0)
116 populate_menu(ad->menu);
117
118 return FR_SUCCESS;
119}
120
124static int op_autocrypt_delete_acct(struct AutocryptData *ad, int op)
125{
126 if (!ad->menu->mdata)
127 return FR_ERROR;
128
129 const int index = menu_get_index(ad->menu);
130 struct AccountEntry **pentry = ARRAY_GET(&ad->entries, index);
131 if (!pentry)
132 return 0;
133
134 char msg[128] = { 0 };
135 snprintf(msg, sizeof(msg),
136 // L10N: Confirmation message when deleting an autocrypt account
137 _("Really delete account \"%s\"?"), buf_string((*pentry)->addr->mailbox));
138 if (query_yesorno(msg, MUTT_NO) != MUTT_YES)
139 return FR_NO_ACTION;
140
141 if (mutt_autocrypt_db_account_delete((*pentry)->account) == 0)
142 populate_menu(ad->menu);
143
144 return FR_SUCCESS;
145}
146
150static int op_autocrypt_toggle_active(struct AutocryptData *ad, int op)
151{
152 if (!ad->menu->mdata)
153 return FR_ERROR;
154
155 const int index = menu_get_index(ad->menu);
156 struct AccountEntry **pentry = ARRAY_GET(&ad->entries, index);
157 if (!pentry)
158 return 0;
159
160 toggle_active((*pentry));
162
163 return FR_SUCCESS;
164}
165
169static int op_autocrypt_toggle_prefer(struct AutocryptData *ad, int op)
170{
171 if (!ad->menu->mdata)
172 return FR_ERROR;
173
174 const int index = menu_get_index(ad->menu);
175 struct AccountEntry **pentry = ARRAY_GET(&ad->entries, index);
176 if (!pentry)
177 return 0;
178
179 toggle_prefer_encrypt((*pentry));
181
182 return FR_SUCCESS;
183}
184
188static int op_exit(struct AutocryptData *ad, int op)
189{
190 ad->done = true;
191 return FR_SUCCESS;
192}
193
194// -----------------------------------------------------------------------------
195
199static const struct AutocryptFunction AutocryptFunctions[] = {
200 // clang-format off
201 { OP_AUTOCRYPT_CREATE_ACCT, op_autocrypt_create_acct },
202 { OP_AUTOCRYPT_DELETE_ACCT, op_autocrypt_delete_acct },
203 { OP_AUTOCRYPT_TOGGLE_ACTIVE, op_autocrypt_toggle_active },
204 { OP_AUTOCRYPT_TOGGLE_PREFER, op_autocrypt_toggle_prefer },
205 { OP_EXIT, op_exit },
206 { 0, NULL },
207 // clang-format on
208};
209
214{
215 // The Dispatcher may be called on any Window in the Dialog
216 struct MuttWindow *dlg = dialog_find(win);
217 if (!dlg || !dlg->wdata)
218 return FR_ERROR;
219
220 struct Menu *menu = dlg->wdata;
221 struct AutocryptData *ad = menu->mdata;
222
223 int rc = FR_UNKNOWN;
224 for (size_t i = 0; AutocryptFunctions[i].op != OP_NULL; i++)
225 {
226 const struct AutocryptFunction *fn = &AutocryptFunctions[i];
227 if (fn->op == op)
228 {
229 rc = fn->function(ad, op);
230 break;
231 }
232 }
233
234 if (rc == FR_UNKNOWN) // Not our function
235 return rc;
236
237 const char *result = dispatcher_get_retval_name(rc);
238 mutt_debug(LL_DEBUG1, "Handled %s (%d) -> %s\n", opcodes_get_name(op), op, NONULL(result));
239
240 return rc;
241}
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:82
const struct MenuFuncOp OpAutocrypt[]
Functions for the Autocrypt Account.
Definition: functions.c:53
static const struct AutocryptFunction AutocryptFunctions[]
All the NeoMutt functions that the Autocrypt supports.
Definition: functions.c:199
const struct MenuOpSeq AutocryptDefaultBindings[]
Key bindings for the Autocrypt Account.
Definition: functions.c:67
static void toggle_prefer_encrypt(struct AccountEntry *entry)
Toggle whether an Autocrypt account prefers encryption.
Definition: functions.c:98
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:200
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:169
static int op_autocrypt_delete_acct(struct AutocryptData *ad, int op)
Delete the current account - Implements autocrypt_function_t -.
Definition: functions.c:124
static int op_autocrypt_toggle_active(struct AutocryptData *ad, int op)
Toggle the current account active/inactive - Implements autocrypt_function_t -.
Definition: functions.c:150
static int op_autocrypt_create_acct(struct AutocryptData *ad, int op)
Create a new autocrypt account - Implements autocrypt_function_t -.
Definition: functions.c:113
int autocrypt_function_dispatcher(struct MuttWindow *win, int op)
Perform a Autocrypt function - Implements function_dispatcher_t -.
Definition: functions.c:213
#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:47
struct AutocryptAccount * account
Account details.
Definition: private.h:49
bool enabled
Is this account enabled.
Definition: lib.h:113
bool prefer_encrypt
false = nopref, true = mutual
Definition: lib.h:112
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:47
autocrypt_function_t function
Function to call.
Definition: functions.h:49
int op
Op code, e.g. OP_AUTOCRYPT_CREATE_ACCT.
Definition: functions.h:48
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:144