NeoMutt  2021-02-05-666-ge300cd
Teaching an old dog new tricks
DOXYGEN
dlgsmime.c
Go to the documentation of this file.
1 
59 #include "config.h"
60 #include <stdbool.h>
61 #include <stdio.h>
62 #include "private.h"
63 #include "mutt/lib.h"
64 #include "config/lib.h"
65 #include "gui/lib.h"
66 #include "lib.h"
67 #include "menu/lib.h"
68 #include "question/lib.h"
69 #include "mutt_logging.h"
70 #include "opcodes.h"
71 #include "smime.h"
72 
74 static const struct Mapping SmimeHelp[] = {
75  // clang-format off
76  { N_("Exit"), OP_EXIT },
77  { N_("Select"), OP_GENERIC_SELECT_ENTRY },
78  { N_("Help"), OP_HELP },
79  { NULL, 0 },
80  // clang-format on
81 };
82 
90 static char *smime_key_flags(KeyFlags flags)
91 {
92  static char buf[3];
93 
94  if (!(flags & KEYFLAG_CANENCRYPT))
95  buf[0] = '-';
96  else
97  buf[0] = 'e';
98 
99  if (!(flags & KEYFLAG_CANSIGN))
100  buf[1] = '-';
101  else
102  buf[1] = 's';
103 
104  buf[2] = '\0';
105 
106  return buf;
107 }
108 
112 static void smime_make_entry(struct Menu *menu, char *buf, size_t buflen, int line)
113 {
114  struct SmimeKey **table = menu->mdata;
115  struct SmimeKey *key = table[line];
116  char *truststate = NULL;
117  switch (key->trust)
118  {
119  case 'e':
120  /* L10N: Describes the trust state of a S/MIME key.
121  This translation must be padded with spaces to the right such that it
122  has the same length as the other translations.
123  The translation strings which need to be padded are:
124  Expired, Invalid, Revoked, Trusted, Unverified, Verified, and Unknown. */
125  truststate = _("Expired ");
126  break;
127  case 'i':
128  /* L10N: Describes the trust state of a S/MIME key.
129  This translation must be padded with spaces to the right such that it
130  has the same length as the other translations.
131  The translation strings which need to be padded are:
132  Expired, Invalid, Revoked, Trusted, Unverified, Verified, and Unknown. */
133  truststate = _("Invalid ");
134  break;
135  case 'r':
136  /* L10N: Describes the trust state of a S/MIME key.
137  This translation must be padded with spaces to the right such that it
138  has the same length as the other translations.
139  The translation strings which need to be padded are:
140  Expired, Invalid, Revoked, Trusted, Unverified, Verified, and Unknown. */
141  truststate = _("Revoked ");
142  break;
143  case 't':
144  /* L10N: Describes the trust state of a S/MIME key.
145  This translation must be padded with spaces to the right such that it
146  has the same length as the other translations.
147  The translation strings which need to be padded are:
148  Expired, Invalid, Revoked, Trusted, Unverified, Verified, and Unknown. */
149  truststate = _("Trusted ");
150  break;
151  case 'u':
152  /* L10N: Describes the trust state of a S/MIME key.
153  This translation must be padded with spaces to the right such that it
154  has the same length as the other translations.
155  The translation strings which need to be padded are:
156  Expired, Invalid, Revoked, Trusted, Unverified, Verified, and Unknown. */
157  truststate = _("Unverified");
158  break;
159  case 'v':
160  /* L10N: Describes the trust state of a S/MIME key.
161  This translation must be padded with spaces to the right such that it
162  has the same length as the other translations.
163  The translation strings which need to be padded are:
164  Expired, Invalid, Revoked, Trusted, Unverified, Verified, and Unknown. */
165  truststate = _("Verified ");
166  break;
167  default:
168  /* L10N: Describes the trust state of a S/MIME key.
169  This translation must be padded with spaces to the right such that it
170  has the same length as the other translations.
171  The translation strings which need to be padded are:
172  Expired, Invalid, Revoked, Trusted, Unverified, Verified, and Unknown. */
173  truststate = _("Unknown ");
174  }
175  snprintf(buf, buflen, " 0x%s %s %s %-35.35s %s", key->hash,
176  smime_key_flags(key->flags), truststate, key->email, key->label);
177 }
178 
184 static void smime_key_table_free(struct Menu *menu, void **ptr)
185 {
186  FREE(ptr);
187 }
188 
195 struct SmimeKey *dlg_select_smime_key(struct SmimeKey *keys, char *query)
196 {
197  struct SmimeKey **table = NULL;
198  int table_size = 0;
199  int table_index = 0;
200  struct SmimeKey *key = NULL;
201  struct SmimeKey *selected_key = NULL;
202  char buf[1024];
203  const char *s = "";
204  bool done = false;
205 
206  for (table_index = 0, key = keys; key; key = key->next)
207  {
208  if (table_index == table_size)
209  {
210  table_size += 5;
211  mutt_mem_realloc(&table, sizeof(struct SmimeKey *) * table_size);
212  }
213 
214  table[table_index++] = key;
215  }
216 
217  struct MuttWindow *dlg = simple_dialog_new(MENU_SMIME, WT_DLG_SMIME, SmimeHelp);
218 
219  struct Menu *menu = dlg->wdata;
220  menu->max = table_index;
222  menu->mdata = table;
224  /* sorting keys might be done later - TODO */
225 
226  char title[256];
227  struct MuttWindow *sbar = window_find_child(dlg, WT_STATUS_BAR);
228  snprintf(title, sizeof(title), _("S/MIME certificates matching \"%s\""), query);
229  sbar_set_title(sbar, title);
230 
232 
233  done = false;
234  while (!done)
235  {
236  switch (menu_loop(menu))
237  {
238  case OP_GENERIC_SELECT_ENTRY:
239  {
240  const int index = menu_get_index(menu);
241  struct SmimeKey *cur_key = table[index];
242  if (cur_key->trust != 't')
243  {
244  switch (cur_key->trust)
245  {
246  case 'e':
247  case 'i':
248  case 'r':
249  s = _("ID is expired/disabled/revoked. Do you really want to use "
250  "the key?");
251  break;
252  case 'u':
253  s = _("ID has undefined validity. Do you really want to use the "
254  "key?");
255  break;
256  case 'v':
257  s = _("ID is not trusted. Do you really want to use the key?");
258  break;
259  }
260 
261  snprintf(buf, sizeof(buf), "%s", s);
262 
263  if (mutt_yesorno(buf, MUTT_NO) != MUTT_YES)
264  {
266  break;
267  }
268  }
269 
270  selected_key = cur_key;
271  done = true;
272  break;
273  }
274 
275  case OP_EXIT:
276  done = true;
277  break;
278  }
279  }
280 
281  simple_dialog_free(&dlg);
282  return selected_key;
283 }
Convenience wrapper for the gui headers.
struct MuttWindow * window_find_child(struct MuttWindow *win, enum WindowType type)
Recursively find a child Window of a given type.
Definition: mutt_window.c:550
void simple_dialog_free(struct MuttWindow **ptr)
Destroy a simple index Dialog.
Definition: simple.c:165
Definition: lib.h:67
KeyFlags flags
Definition: smime.h:51
static void smime_key_table_free(struct Menu *menu, void **ptr)
Free the key table - Implements Menu::mdata_free() -.
Definition: dlgsmime.c:184
NeoMutt Logging.
#define _(a)
Definition: message.h:28
char trust
i=Invalid r=revoked e=expired u=unverified v=verified t=trusted
Definition: smime.h:50
An SIME key.
Definition: smime.h:44
Shared constants/structs that are private to libconn.
All user-callable functions.
#define KEYFLAG_CANENCRYPT
Key is suitable for encryption.
Definition: lib.h:125
Status Bar containing extra info about the Index/Pager/etc.
Definition: mutt_window.h:102
Convenience wrapper for the config headers.
void(* make_entry)(struct Menu *menu, char *buf, size_t buflen, int line)
Definition: lib.h:105
const char * title
Title of this menu.
Definition: lib.h:69
void mutt_clear_error(void)
Clear the message line (bottom line of screen)
Definition: mutt_logging.c:112
static char * smime_key_flags(KeyFlags flags)
Turn SMIME key flags into a string.
Definition: dlgsmime.c:90
void mutt_mem_realloc(void *ptr, size_t size)
Resize a block of memory on the heap.
Definition: memory.c:114
void * mdata
Private data.
Definition: lib.h:155
uint16_t KeyFlags
Flags describing PGP/SMIME keys, e.g. KEYFLAG_CANSIGN.
Definition: lib.h:122
struct SmimeKey * next
Definition: smime.h:52
Ask the user a question.
void sbar_set_title(struct MuttWindow *win, const char *title)
Set the title for the Simple Bar.
Definition: sbar.c:221
struct SmimeKey * dlg_select_smime_key(struct SmimeKey *keys, char *query)
Get the user to select a key.
Definition: dlgsmime.c:195
#define KEYFLAG_CANSIGN
Key is suitable for signing.
Definition: lib.h:124
enum QuadOption mutt_yesorno(const char *msg, enum QuadOption def)
Ask the user a Yes/No question.
Definition: question.c:180
int max
Number of entries in the menu.
Definition: lib.h:71
char * email
Definition: smime.h:46
Smime Dialog, dlg_select_smime_key()
Definition: mutt_window.h:92
void(* mdata_free)(struct Menu *menu, void **ptr)
Definition: lib.h:170
char * label
Definition: smime.h:48
SMIME helper routines.
char * hash
Definition: smime.h:47
#define FREE(x)
Definition: memory.h:40
Mapping between user-readable string and a constant.
Definition: mapping.h:31
User answered &#39;No&#39;, or assume &#39;No&#39;.
Definition: quad.h:38
struct MuttWindow * simple_dialog_new(enum MenuType mtype, enum WindowType wtype, const struct Mapping *help_data)
Create a simple index Dialog.
Definition: simple.c:128
SMIME encryption menu.
Definition: type.h:58
Convenience wrapper for the library headers.
void * wdata
Private data.
Definition: mutt_window.h:145
#define N_(a)
Definition: message.h:32
static void smime_make_entry(struct Menu *menu, char *buf, size_t buflen, int line)
Format a menu item for the smime key list - Implements Menu::make_entry() -.
Definition: dlgsmime.c:112
User answered &#39;Yes&#39;, or assume &#39;Yes&#39;.
Definition: quad.h:39