summaryrefslogtreecommitdiffstats
path: root/autocrypt/autocrypt_acct_menu.c
blob: 76bab1d2d4f0a1222110241cf8f4617b178572e9 (plain)
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
/*
 * Copyright (C) 2019 Kevin J. McCarthy <kevin@8t8.us>
 *
 *     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, write to the Free Software
 *     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

#if HAVE_CONFIG_H
# include "config.h"
#endif

#include "mutt.h"
#include "mutt_menu.h"
#include "mutt_idna.h"

#include "autocrypt.h"
#include "autocrypt_private.h"

typedef struct entry
{
  int tagged;    /* TODO */
  int num;
  AUTOCRYPT_ACCOUNT *account;
  ADDRESS *addr;
} ENTRY;

static const struct mapping_t AutocryptAcctHelp[] = {
  { N_("Exit"),   OP_EXIT },
  /* L10N: Autocrypt Account Menu Help line:
     create new account
  */
  { N_("Create"),   OP_AUTOCRYPT_CREATE_ACCT },
  /* L10N: Autocrypt Account Menu Help line:
     delete account
  */
  { N_("Delete"),   OP_AUTOCRYPT_DELETE_ACCT },
  /* L10N: Autocrypt Account Menu Help line:
     toggle an account active/inactive
     The words here are abbreviated to keep the help line compact.
     It currently has the content:
     q:Exit  c:Create  D:Delete  a:Tgl Active  p:Prf Encr  ?:Help
  */
  { N_("Tgl Active"),  OP_AUTOCRYPT_TOGGLE_ACTIVE },
  /* L10N: Autocrypt Account Menu Help line:
     toggle "prefer-encrypt" on an account
     The words here are abbreviated to keep the help line compact.
     It currently has the content:
     q:Exit  c:Create  D:Delete  a:Tgl Active  p:Prf Encr  ?:Help
  */
  { N_("Prf Encr"), OP_AUTOCRYPT_TOGGLE_PREFER },
  { N_("Help"),   OP_HELP },
  { NULL,	  0 }
};

static const char *account_format_str (char *dest, size_t destlen, size_t col,
                                       int cols, char op, const char *src,
                                       const char *fmt, const char *ifstring,
                                       const char *elsestring,
                                       void *data, format_flag flags)
{
  ENTRY *entry = (ENTRY *)data;
  char tmp[SHORT_STRING];

  switch (op)
  {
    case 'a':
      mutt_format_s (dest, destlen, fmt, entry->addr->mailbox);
      break;
    case 'k':
      mutt_format_s (dest, destlen, fmt, entry->account->keyid);
      break;
    case 'n':
      snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
      snprintf (dest, destlen, tmp, entry->num);
      break;
    case 'p':
      if (entry->account->prefer_encrypt)
        /* L10N:
           Autocrypt Account menu.
           flag that an account has prefer-encrypt set
        */
        mutt_format_s (dest, destlen, fmt, _("prefer encrypt"));
      else
        /* L10N:
           Autocrypt Account menu.
           flag that an account has prefer-encrypt unset;
           thus encryption will need to be manually enabled.
        */
        mutt_format_s (dest, destlen, fmt, _("manual encrypt"));
      break;
    case 's':
      if (entry->account->enabled)
        /* L10N:
           Autocrypt Account menu.
           flag that an account is enabled/active
        */
        mutt_format_s (dest, destlen, fmt, _("active"));
      else
        /* L10N:
           Autocrypt Account menu.
           flag that an account is disabled/inactive
        */
        mutt_format_s (dest, destlen, fmt, _("inactive"));
      break;
  }

  return (src);
}

static void account_entry (char *s, size_t slen, MUTTMENU *m, int num)
{
  ENTRY *entry = &((ENTRY *) m->data)[num];

  mutt_FormatString (s, slen, 0, MuttIndexWindow->cols,
                     NONULL (AutocryptAcctFormat), account_format_str,
		     entry, MUTT_FORMAT_ARROWCURSOR);
}

static MUTTMENU *create_menu (void)
{
  MUTTMENU *menu = NULL;
  AUTOCRYPT_ACCOUNT **accounts = NULL;
  ENTRY *entries = NULL;
  int num_accounts = 0, i;
  char *helpstr;

  if (mutt_autocrypt_db_account_get_all (&accounts, &num_accounts) < 0)
    return NULL;

  menu = mutt_new_menu (MENU_AUTOCRYPT_ACCT);
  menu->make_entry = account_entry;
  /* menu->tag = account_tag; */
  /* L10N:
     Autocrypt Account Management Menu title
  */
  menu->title = _("Autocrypt Accounts");
  helpstr = safe_malloc (STRING);
  menu->help = mutt_compile_help (helpstr, STRING, MENU_AUTOCRYPT_ACCT,
                                  AutocryptAcctHelp);

  menu->data = entries = safe_calloc (num_accounts, sizeof(ENTRY));
  menu->max = num_accounts;

  for (i = 0; i < num_accounts; i++)
  {
    entries[i].num = i + 1;
    /* note: we are transfering the account pointer to the entries
     * array, and freeing the accounts array below.  the account
     * will be freed in free_menu().
     */
    entries[i].account = accounts[i];

    entries[i].addr = rfc822_new_address ();
    entries[i].addr->mailbox = safe_strdup (accounts[i]->email_addr);
    mutt_addrlist_to_local (entries[i].addr);
  }
  FREE (&accounts);

  mutt_push_current_menu (menu);

  return menu;
}

static void free_menu (MUTTMENU **menu)
{
  int i;
  ENTRY *entries;

  entries = (ENTRY *)(*menu)->data;
  for (i = 0; i < (*menu)->max; i++)
  {
    mutt_autocrypt_db_account_free (&entries[i].account);
    rfc822_free_address (&entries[i].addr);
  }
  FREE (&(*menu)->data);

  mutt_pop_current_menu (*menu);
  FREE (&(*menu)->help);
  mutt_menuDestroy (menu);
}

static void toggle_active (ENTRY *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");
  }
}

static void toggle_prefer_encrypt (ENTRY *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");
  }
}

void mutt_autocrypt_account_menu (void)
{
  MUTTMENU *menu;
  int done = 0, op;
  ENTRY *entry;
  char msg[SHORT_STRING];

  if (!option (OPTAUTOCRYPT))
    return;

  if (mutt_autocrypt_init (0))
    return;

  menu = create_menu ();
  if (!menu)
    return;

  while (!done)
  {
    switch ((op = mutt_menuLoop (menu)))
    {
      case OP_EXIT:
        done = 1;
        break;

      case OP_AUTOCRYPT_CREATE_ACCT:
        if (!mutt_autocrypt_account_init (0))
        {
          free_menu (&menu);
          menu = create_menu ();
        }
        break;

      case OP_AUTOCRYPT_DELETE_ACCT:
        if (menu->data)
        {
          entry = (ENTRY *)(menu->data) + menu->current;
          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)
            break;

          if (!mutt_autocrypt_db_account_delete (entry->account))
          {
            free_menu (&menu);
            menu = create_menu ();
          }
        }
        break;

      case OP_AUTOCRYPT_TOGGLE_ACTIVE:
        if (menu->data)
        {
          entry = (ENTRY *)(menu->data) + menu->current;
          toggle_active (entry);
          menu->redraw |= REDRAW_FULL;
        }
        break;

      case OP_AUTOCRYPT_TOGGLE_PREFER:
        if (menu->data)
        {
          entry = (ENTRY *)(menu->data) + menu->current;
          toggle_prefer_encrypt (entry);
          menu->redraw |= REDRAW_FULL;
        }
        break;
    }
  }

  free_menu (&menu);
}