forked from neomutt/neomutt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.c
198 lines (171 loc) · 5.77 KB
/
functions.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/**
* @file
* Autocrypt functions
*
* @authors
* Copyright (C) 2022 Richard Russon <[email protected]>
*
* @copyright
* This program is free software: you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation, either version 2 of the License, or (at your option) any later
* version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* @page autocrypt_functions Autocrypt functions
*
* Autocrypt functions
*/
#include "config.h"
#include <stdio.h>
#include "private.h"
#include "mutt/lib.h"
#include "address/lib.h"
#include "config/lib.h"
#include "core/lib.h"
#include "gui/lib.h"
#include "functions.h"
#include "lib.h"
#include "menu/lib.h"
#include "question/lib.h"
#include "opcodes.h"
/**
* toggle_active - Toggle whether an Autocrypt account is active
* @param entry Menu Entry for the account
*/
static void toggle_active(struct AccountEntry *entry)
{
entry->account->enabled = !entry->account->enabled;
if (mutt_autocrypt_db_account_update(entry->account) != 0)
{
entry->account->enabled = !entry->account->enabled;
/* L10N: This error message is displayed if a database update of an
account record fails for some odd reason. */
mutt_error(_("Error updating account record"));
}
}
/**
* toggle_prefer_encrypt - Toggle whether an Autocrypt account prefers encryption
* @param entry Menu Entry for the account
*/
static void toggle_prefer_encrypt(struct AccountEntry *entry)
{
entry->account->prefer_encrypt = !entry->account->prefer_encrypt;
if (mutt_autocrypt_db_account_update(entry->account))
{
entry->account->prefer_encrypt = !entry->account->prefer_encrypt;
mutt_error(_("Error updating account record"));
}
}
// -----------------------------------------------------------------------------
/**
* op_autocrypt_create_acct - Create a new autocrypt account - Implements ::autocrypt_function_t - @ingroup autocrypt_function_api
*/
static int op_autocrypt_create_acct(struct AutocryptData *ad, int op)
{
if (mutt_autocrypt_account_init(false) == 0)
populate_menu(ad->menu);
return FR_SUCCESS;
}
/**
* op_autocrypt_delete_acct - Delete the current account - Implements ::autocrypt_function_t - @ingroup autocrypt_function_api
*/
static int op_autocrypt_delete_acct(struct AutocryptData *ad, int op)
{
if (!ad->menu->mdata)
return FR_ERROR;
const int index = menu_get_index(ad->menu);
struct AccountEntry *entry = ((struct AccountEntry *) ad->menu->mdata) + index;
char msg[128] = { 0 };
snprintf(msg, sizeof(msg),
// L10N: Confirmation message when deleting an autocrypt account
_("Really delete account \"%s\"?"), entry->addr->mailbox);
if (mutt_yesorno(msg, MUTT_NO) != MUTT_YES)
return FR_NO_ACTION;
if (mutt_autocrypt_db_account_delete(entry->account) == 0)
populate_menu(ad->menu);
return FR_SUCCESS;
}
/**
* op_autocrypt_toggle_active - Toggle the current account active/inactive - Implements ::autocrypt_function_t - @ingroup autocrypt_function_api
*/
static int op_autocrypt_toggle_active(struct AutocryptData *ad, int op)
{
if (!ad->menu->mdata)
return FR_ERROR;
const int index = menu_get_index(ad->menu);
struct AccountEntry *entry = ((struct AccountEntry *) ad->menu->mdata) + index;
toggle_active(entry);
menu_queue_redraw(ad->menu, MENU_REDRAW_FULL);
return FR_SUCCESS;
}
/**
* op_autocrypt_toggle_prefer - Toggle the current account prefer-encrypt flag - Implements ::autocrypt_function_t - @ingroup autocrypt_function_api
*/
static int op_autocrypt_toggle_prefer(struct AutocryptData *ad, int op)
{
if (!ad->menu->mdata)
return FR_ERROR;
const int index = menu_get_index(ad->menu);
struct AccountEntry *entry = (struct AccountEntry *) (ad->menu->mdata) + index;
toggle_prefer_encrypt(entry);
menu_queue_redraw(ad->menu, MENU_REDRAW_FULL);
return FR_SUCCESS;
}
/**
* op_exit - Exit this menu - Implements ::autocrypt_function_t - @ingroup autocrypt_function_api
*/
static int op_exit(struct AutocryptData *ad, int op)
{
ad->done = true;
return FR_SUCCESS;
}
// -----------------------------------------------------------------------------
/**
* AutocryptFunctions - All the NeoMutt functions that the Autocrypt supports
*/
struct AutocryptFunction AutocryptFunctions[] = {
// clang-format off
{ OP_AUTOCRYPT_CREATE_ACCT, op_autocrypt_create_acct },
{ OP_AUTOCRYPT_DELETE_ACCT, op_autocrypt_delete_acct },
{ OP_AUTOCRYPT_TOGGLE_ACTIVE, op_autocrypt_toggle_active },
{ OP_AUTOCRYPT_TOGGLE_PREFER, op_autocrypt_toggle_prefer },
{ OP_EXIT, op_exit },
{ 0, NULL },
// clang-format on
};
/**
* autocrypt_function_dispatcher - Perform a Autocrypt function - Implements ::function_dispatcher_t - @ingroup dispatcher_api
*/
int autocrypt_function_dispatcher(struct MuttWindow *win, int op)
{
if (!win || !win->wdata)
return FR_UNKNOWN;
struct MuttWindow *dlg = dialog_find(win);
if (!dlg)
return FR_ERROR;
struct AutocryptData *ad = dlg->wdata;
int rc = FR_UNKNOWN;
for (size_t i = 0; AutocryptFunctions[i].op != OP_NULL; i++)
{
const struct AutocryptFunction *fn = &AutocryptFunctions[i];
if (fn->op == op)
{
rc = fn->function(ad, op);
break;
}
}
if (rc == FR_UNKNOWN) // Not our function
return rc;
const char *result = dispacher_get_retval_name(rc);
mutt_debug(LL_DEBUG1, "Handled %s (%d) -> %s\n", opcodes_get_name(op), op, NONULL(result));
return rc;
}