NeoMutt  2024-10-02-37-gfa9146
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 "functions.h"
45#endif
46
47// clang-format off
48#ifdef USE_AUTOCRYPT
52const 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#endif
61
62#ifdef USE_AUTOCRYPT
66const struct MenuOpSeq AutocryptDefaultBindings[] = { /* map: autocrypt account */
67 { OP_AUTOCRYPT_CREATE_ACCT, "c" },
68 { OP_AUTOCRYPT_DELETE_ACCT, "D" },
69 { OP_AUTOCRYPT_TOGGLE_ACTIVE, "a" },
70 { OP_AUTOCRYPT_TOGGLE_PREFER, "p" },
71 { OP_EXIT, "q" },
72 { 0, NULL }
73};
74#endif
75// clang-format on
76
81static void toggle_active(struct AccountEntry *entry)
82{
83 entry->account->enabled = !entry->account->enabled;
85 {
86 entry->account->enabled = !entry->account->enabled;
87 /* L10N: This error message is displayed if a database update of an
88 account record fails for some odd reason. */
89 mutt_error(_("Error updating account record"));
90 }
91}
92
97static void toggle_prefer_encrypt(struct AccountEntry *entry)
98{
101 {
102 entry->account->prefer_encrypt = !entry->account->prefer_encrypt;
103 mutt_error(_("Error updating account record"));
104 }
105}
106
107// -----------------------------------------------------------------------------
108
112static int op_autocrypt_create_acct(struct AutocryptData *ad, int op)
113{
114 if (mutt_autocrypt_account_init(false) == 0)
115 populate_menu(ad->menu);
116
117 return FR_SUCCESS;
118}
119
123static int op_autocrypt_delete_acct(struct AutocryptData *ad, int op)
124{
125 if (!ad->menu->mdata)
126 return FR_ERROR;
127
128 const int index = menu_get_index(ad->menu);
129 struct AccountEntry *entry = ((struct AccountEntry *) ad->menu->mdata) + index;
130 char msg[128] = { 0 };
131 snprintf(msg, sizeof(msg),
132 // L10N: Confirmation message when deleting an autocrypt account
133 _("Really delete account \"%s\"?"), buf_string(entry->addr->mailbox));
134 if (query_yesorno(msg, MUTT_NO) != MUTT_YES)
135 return FR_NO_ACTION;
136
138 populate_menu(ad->menu);
139
140 return FR_SUCCESS;
141}
142
146static int op_autocrypt_toggle_active(struct AutocryptData *ad, int op)
147{
148 if (!ad->menu->mdata)
149 return FR_ERROR;
150
151 const int index = menu_get_index(ad->menu);
152 struct AccountEntry *entry = ((struct AccountEntry *) ad->menu->mdata) + index;
153 toggle_active(entry);
155
156 return FR_SUCCESS;
157}
158
162static int op_autocrypt_toggle_prefer(struct AutocryptData *ad, int op)
163{
164 if (!ad->menu->mdata)
165 return FR_ERROR;
166
167 const int index = menu_get_index(ad->menu);
168 struct AccountEntry *entry = (struct AccountEntry *) (ad->menu->mdata) + index;
171
172 return FR_SUCCESS;
173}
174
178static int op_exit(struct AutocryptData *ad, int op)
179{
180 ad->done = true;
181 return FR_SUCCESS;
182}
183
184// -----------------------------------------------------------------------------
185
189static const struct AutocryptFunction AutocryptFunctions[] = {
190 // clang-format off
191 { OP_AUTOCRYPT_CREATE_ACCT, op_autocrypt_create_acct },
192 { OP_AUTOCRYPT_DELETE_ACCT, op_autocrypt_delete_acct },
193 { OP_AUTOCRYPT_TOGGLE_ACTIVE, op_autocrypt_toggle_active },
194 { OP_AUTOCRYPT_TOGGLE_PREFER, op_autocrypt_toggle_prefer },
195 { OP_EXIT, op_exit },
196 { 0, NULL },
197 // clang-format on
198};
199
204{
205 if (!win || !win->wdata)
206 return FR_UNKNOWN;
207
208 struct MuttWindow *dlg = dialog_find(win);
209 if (!dlg)
210 return FR_ERROR;
211
212 struct AutocryptData *ad = dlg->wdata;
213
214 int rc = FR_UNKNOWN;
215 for (size_t i = 0; AutocryptFunctions[i].op != OP_NULL; i++)
216 {
217 const struct AutocryptFunction *fn = &AutocryptFunctions[i];
218 if (fn->op == op)
219 {
220 rc = fn->function(ad, op);
221 break;
222 }
223 }
224
225 if (rc == FR_UNKNOWN) // Not our function
226 return rc;
227
228 const char *result = dispatcher_get_retval_name(rc);
229 mutt_debug(LL_DEBUG1, "Handled %s (%d) -> %s\n", opcodes_get_name(op), op, NONULL(result));
230
231 return rc;
232}
Email Address Handling.
int mutt_autocrypt_db_account_delete(struct AutocryptAccount *acct)
Delete an Account from the Autocrypt database.
Definition: db.c:428
int mutt_autocrypt_db_account_update(struct AutocryptAccount *acct)
Update Account info in the Autocrypt database.
Definition: db.c:382
static void toggle_active(struct AccountEntry *entry)
Toggle whether an Autocrypt account is active.
Definition: functions.c:81
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:189
const struct MenuOpSeq AutocryptDefaultBindings[]
Key bindings for the Autocrypt Account.
Definition: functions.c:66
static void toggle_prefer_encrypt(struct AccountEntry *entry)
Toggle whether an Autocrypt account prefers encryption.
Definition: functions.c:97
int mutt_autocrypt_account_init(bool prompt)
Create a new Autocrypt account.
Definition: autocrypt.c:143
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:191
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:162
static int op_autocrypt_delete_acct(struct AutocryptData *ad, int op)
Delete the current account - Implements autocrypt_function_t -.
Definition: functions.c:123
static int op_autocrypt_toggle_active(struct AutocryptData *ad, int op)
Toggle the current account active/inactive - Implements autocrypt_function_t -.
Definition: functions.c:146
static int op_autocrypt_create_acct(struct AutocryptData *ad, int op)
Create a new autocrypt account - Implements autocrypt_function_t -.
Definition: functions.c:112
int autocrypt_function_dispatcher(struct MuttWindow *win, int op)
Perform a Autocrypt function - Implements function_dispatcher_t -.
Definition: functions.c:203
#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:45
struct Address * addr
Email address associated with the account.
Definition: private.h:48
struct AutocryptAccount * account
Account details.
Definition: private.h:47
struct Buffer * mailbox
Mailbox and host address.
Definition: address.h:38
bool enabled
Is this account enabled.
Definition: lib.h:112
bool prefer_encrypt
false = nopref, true = mutual
Definition: lib.h:111
Data to pass to the Autocrypt Functions.
Definition: functions.h:34
bool done
Should we close the Dialog?
Definition: functions.h:35
struct Menu * menu
Autocrypt Menu.
Definition: functions.h:36
A NeoMutt function.
Definition: functions.h:55
autocrypt_function_t function
Function to call.
Definition: functions.h:57
int op
Op code, e.g. OP_AUTOCRYPT_CREATE_ACCT.
Definition: functions.h:56
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
void * mdata
Private data.
Definition: lib.h:147
void * wdata
Private data.
Definition: mutt_window.h:144