NeoMutt  2023-05-17-56-ga67199
Teaching an old dog new tricks
DOXYGEN
dlg_pgp.c File Reference

PGP Key Selection Dialog. More...

#include "config.h"
#include <ctype.h>
#include <locale.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.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 "lib.h"
#include "menu/lib.h"
#include "format_flags.h"
#include "keymap.h"
#include "mutt_logging.h"
#include "muttlib.h"
#include "opcodes.h"
#include "pgp.h"
#include "pgp_functions.h"
#include "pgpkey.h"
#include "pgplib.h"
+ Include dependency graph for dlg_pgp.c:

Go to the source code of this file.

Data Structures

struct  PgpEntry
 An entry in a PGP key menu. More...
 

Functions

static int pgp_compare_key_address (const void *a, const void *b)
 Compare Key addresses and IDs for sorting. More...
 
static int pgp_compare_address_qsort (const void *a, const void *b)
 Compare the addresses of two PGP keys. More...
 
static int pgp_compare_key_date (const void *a, const void *b)
 Compare Key dates for sorting. More...
 
static int pgp_compare_date_qsort (const void *a, const void *b)
 Compare the dates of two PGP keys. More...
 
static int pgp_compare_keyid (const void *a, const void *b)
 Compare Key IDs and addresses for sorting. More...
 
static int pgp_compare_keyid_qsort (const void *a, const void *b)
 Compare key IDs. More...
 
static int pgp_compare_key_trust (const void *a, const void *b)
 Compare the trust of keys for sorting. More...
 
static int pgp_compare_trust_qsort (const void *a, const void *b)
 Compare the trust levels of two PGP keys. More...
 
static char * pgp_key_abilities (KeyFlags flags)
 Turn PGP key abilities into a string. More...
 
static char pgp_flags (KeyFlags flags)
 Turn PGP key flags into a string. More...
 
static const char * pgp_entry_format_str (char *buf, size_t buflen, size_t col, int cols, char op, const char *src, const char *prec, const char *if_str, const char *else_str, intptr_t data, MuttFormatFlags flags)
 Format an entry on the PGP key selection menu - Implements format_t -. More...
 
static void pgp_make_entry (struct Menu *menu, char *buf, size_t buflen, int line)
 Format a menu item for the pgp key list - Implements Menu::make_entry() -. More...
 
static void pgp_key_table_free (struct Menu *menu, void **ptr)
 Free the key table - Implements Menu::mdata_free() -. More...
 
static int pgp_key_config_observer (struct NotifyCallback *nc)
 Notification that a Config Variable has changed - Implements observer_t -. More...
 
static int pgp_key_window_observer (struct NotifyCallback *nc)
 Notification that a Window has changed - Implements observer_t -. More...
 
struct PgpKeyInfodlg_select_pgp_key (struct PgpKeyInfo *keys, struct Address *p, const char *s)
 Let the user select a key to use. More...
 

Variables

static const struct Mapping PgpHelp []
 Help Bar for the PGP key selection dialog. More...
 
static const char TrustFlags [] = "?- +"
 Characters used to show the trust level for PGP keys. More...
 

Detailed Description

PGP Key Selection Dialog.

Authors
  • Richard Russon

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/.

Definition in file dlg_pgp.c.

Function Documentation

◆ pgp_compare_key_address()

static int pgp_compare_key_address ( const void *  a,
const void *  b 
)
static

Compare Key addresses and IDs for sorting.

Parameters
aFirst key
bSecond key
Return values
-1a precedes b
0a and b are identical
1b precedes a

Definition at line 124 of file dlg_pgp.c.

125{
126 struct PgpUid const *const *s = (struct PgpUid const *const *) a;
127 struct PgpUid const *const *t = (struct PgpUid const *const *) b;
128
129 int r = mutt_istr_cmp((*s)->addr, (*t)->addr);
130 if (r != 0)
131 return (r > 0);
132
133 return mutt_istr_cmp(pgp_fpr_or_lkeyid((*s)->parent), pgp_fpr_or_lkeyid((*t)->parent)) > 0;
134}
int mutt_istr_cmp(const char *a, const char *b)
Compare two strings ignoring case, safely.
Definition: string.c:484
char * pgp_fpr_or_lkeyid(struct PgpKeyInfo *k)
Get the fingerprint or long keyid.
Definition: pgp.c:235
PGP User ID.
Definition: pgplib.h:35
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ pgp_compare_address_qsort()

static int pgp_compare_address_qsort ( const void *  a,
const void *  b 
)
static

Compare the addresses of two PGP keys.

Parameters
aFirst address
bSecond address
Return values
-1a precedes b
0a and b are identical
1b precedes a

Definition at line 144 of file dlg_pgp.c.

145{
146 const short c_pgp_sort_keys = cs_subset_sort(NeoMutt->sub, "pgp_sort_keys");
147 return (c_pgp_sort_keys & SORT_REVERSE) ? !pgp_compare_key_address(a, b) :
149}
short cs_subset_sort(const struct ConfigSubset *sub, const char *name)
Get a sort config item by name.
Definition: helpers.c:292
static int pgp_compare_key_address(const void *a, const void *b)
Compare Key addresses and IDs for sorting.
Definition: dlg_pgp.c:124
#define SORT_REVERSE
Reverse the order of the sort.
Definition: sort2.h:75
Container for Accounts, Notifications.
Definition: neomutt.h:37
struct ConfigSubset * sub
Inherited config items.
Definition: neomutt.h:39
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ pgp_compare_key_date()

static int pgp_compare_key_date ( const void *  a,
const void *  b 
)
static

Compare Key dates for sorting.

Parameters
aFirst key ID
bSecond key ID
Return values
-1a precedes b
0a and b are identical
1b precedes a

Definition at line 159 of file dlg_pgp.c.

160{
161 struct PgpUid const *const *s = (struct PgpUid const *const *) a;
162 struct PgpUid const *const *t = (struct PgpUid const *const *) b;
163
164 int r = ((*s)->parent->gen_time - (*t)->parent->gen_time);
165 if (r != 0)
166 return r > 0;
167 return mutt_istr_cmp((*s)->addr, (*t)->addr) > 0;
168}
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ pgp_compare_date_qsort()

static int pgp_compare_date_qsort ( const void *  a,
const void *  b 
)
static

Compare the dates of two PGP keys.

Parameters
aFirst key
bSecond key
Return values
-1a precedes b
0a and b are identical
1b precedes a

Definition at line 178 of file dlg_pgp.c.

179{
180 const short c_pgp_sort_keys = cs_subset_sort(NeoMutt->sub, "pgp_sort_keys");
181 return (c_pgp_sort_keys & SORT_REVERSE) ? !pgp_compare_key_date(a, b) :
183}
static int pgp_compare_key_date(const void *a, const void *b)
Compare Key dates for sorting.
Definition: dlg_pgp.c:159
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ pgp_compare_keyid()

static int pgp_compare_keyid ( const void *  a,
const void *  b 
)
static

Compare Key IDs and addresses for sorting.

Parameters
aFirst key ID
bSecond key ID
Return values
-1a precedes b
0a and b are identical
1b precedes a

Definition at line 193 of file dlg_pgp.c.

194{
195 struct PgpUid const *const *s = (struct PgpUid const *const *) a;
196 struct PgpUid const *const *t = (struct PgpUid const *const *) b;
197
198 int r = mutt_istr_cmp(pgp_fpr_or_lkeyid((*s)->parent), pgp_fpr_or_lkeyid((*t)->parent));
199 if (r != 0)
200 return (r > 0);
201 return mutt_istr_cmp((*s)->addr, (*t)->addr) > 0;
202}
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ pgp_compare_keyid_qsort()

static int pgp_compare_keyid_qsort ( const void *  a,
const void *  b 
)
static

Compare key IDs.

Parameters
aFirst key ID
bSecond key ID
Return values
-1a precedes b
0a and b are identical
1b precedes a

Definition at line 212 of file dlg_pgp.c.

213{
214 const short c_pgp_sort_keys = cs_subset_sort(NeoMutt->sub, "pgp_sort_keys");
215 return (c_pgp_sort_keys & SORT_REVERSE) ? !pgp_compare_keyid(a, b) :
216 pgp_compare_keyid(a, b);
217}
static int pgp_compare_keyid(const void *a, const void *b)
Compare Key IDs and addresses for sorting.
Definition: dlg_pgp.c:193
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ pgp_compare_key_trust()

static int pgp_compare_key_trust ( const void *  a,
const void *  b 
)
static

Compare the trust of keys for sorting.

Parameters
aFirst key
bSecond key
Return values
-1a precedes b
0a and b are identical
1b precedes a

Compare two trust values, the key length, the creation dates. the addresses and the key IDs.

Definition at line 230 of file dlg_pgp.c.

231{
232 struct PgpUid const *const *s = (struct PgpUid const *const *) a;
233 struct PgpUid const *const *t = (struct PgpUid const *const *) b;
234
235 int r = (((*s)->parent->flags & KEYFLAG_RESTRICTIONS) -
236 ((*t)->parent->flags & KEYFLAG_RESTRICTIONS));
237 if (r != 0)
238 return r > 0;
239 r = ((*s)->trust - (*t)->trust);
240 if (r != 0)
241 return r < 0;
242 r = ((*s)->parent->keylen - (*t)->parent->keylen);
243 if (r != 0)
244 return r < 0;
245 r = ((*s)->parent->gen_time - (*t)->parent->gen_time);
246 if (r != 0)
247 return r < 0;
248 r = mutt_istr_cmp((*s)->addr, (*t)->addr);
249 if (r != 0)
250 return r > 0;
251 return mutt_istr_cmp(pgp_fpr_or_lkeyid((*s)->parent), pgp_fpr_or_lkeyid((*t)->parent)) > 0;
252}
#define KEYFLAG_RESTRICTIONS
Definition: lib.h:140
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ pgp_compare_trust_qsort()

static int pgp_compare_trust_qsort ( const void *  a,
const void *  b 
)
static

Compare the trust levels of two PGP keys.

Parameters
aFirst key
bSecond key
Return values
-1a precedes b
0a and b are identical
1b precedes a

Definition at line 262 of file dlg_pgp.c.

263{
264 const short c_pgp_sort_keys = cs_subset_sort(NeoMutt->sub, "pgp_sort_keys");
265 return (c_pgp_sort_keys & SORT_REVERSE) ? !pgp_compare_key_trust(a, b) :
267}
static int pgp_compare_key_trust(const void *a, const void *b)
Compare the trust of keys for sorting.
Definition: dlg_pgp.c:230
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ pgp_key_abilities()

static char * pgp_key_abilities ( KeyFlags  flags)
static

Turn PGP key abilities into a string.

Parameters
flagsFlags, see KeyFlags
Return values
ptrAbilities string
Note
This returns a pointer to a static buffer

Definition at line 276 of file dlg_pgp.c.

277{
278 static char buf[3];
279
280 if (!(flags & KEYFLAG_CANENCRYPT))
281 buf[0] = '-';
282 else if (flags & KEYFLAG_PREFER_SIGNING)
283 buf[0] = '.';
284 else
285 buf[0] = 'e';
286
287 if (!(flags & KEYFLAG_CANSIGN))
288 buf[1] = '-';
289 else if (flags & KEYFLAG_PREFER_ENCRYPTION)
290 buf[1] = '.';
291 else
292 buf[1] = 's';
293
294 buf[2] = '\0';
295
296 return buf;
297}
#define KEYFLAG_CANENCRYPT
Key is suitable for encryption.
Definition: lib.h:128
#define KEYFLAG_PREFER_SIGNING
Key's owner prefers signing.
Definition: lib.h:137
#define KEYFLAG_PREFER_ENCRYPTION
Key's owner prefers encryption.
Definition: lib.h:136
#define KEYFLAG_CANSIGN
Key is suitable for signing.
Definition: lib.h:127
+ Here is the caller graph for this function:

◆ pgp_flags()

static char pgp_flags ( KeyFlags  flags)
static

Turn PGP key flags into a string.

Parameters
flagsFlags, see KeyFlags
Return values
charFlag character

Definition at line 304 of file dlg_pgp.c.

305{
306 if (flags & KEYFLAG_REVOKED)
307 return 'R';
308 if (flags & KEYFLAG_EXPIRED)
309 return 'X';
310 if (flags & KEYFLAG_DISABLED)
311 return 'd';
312 if (flags & KEYFLAG_CRITICAL)
313 return 'c';
314
315 return ' ';
316}
#define KEYFLAG_EXPIRED
Key is expired.
Definition: lib.h:131
#define KEYFLAG_CRITICAL
Key is marked critical.
Definition: lib.h:135
#define KEYFLAG_DISABLED
Key is marked disabled.
Definition: lib.h:133
#define KEYFLAG_REVOKED
Key is revoked.
Definition: lib.h:132
+ Here is the caller graph for this function:

◆ dlg_select_pgp_key()

struct PgpKeyInfo * dlg_select_pgp_key ( struct PgpKeyInfo keys,
struct Address p,
const char *  s 
)

Let the user select a key to use.

Parameters
keysList of PGP keys
pAddress to match
sString to match
Return values
ptrSelected PGP key

Definition at line 592 of file dlg_pgp.c.

594{
595 struct PgpUid **key_table = NULL;
596 struct Menu *menu = NULL;
597 char buf[1024] = { 0 };
598 struct PgpUid *a = NULL;
599 bool unusable = false;
600 int keymax = 0;
601
602 const bool c_pgp_show_unusable = cs_subset_bool(NeoMutt->sub, "pgp_show_unusable");
603 int i = 0;
604 for (struct PgpKeyInfo *kp = keys; kp; kp = kp->next)
605 {
606 if (!c_pgp_show_unusable && (kp->flags & KEYFLAG_CANTUSE))
607 {
608 unusable = true;
609 continue;
610 }
611
612 for (a = kp->address; a; a = a->next)
613 {
614 if (!c_pgp_show_unusable && (a->flags & KEYFLAG_CANTUSE))
615 {
616 unusable = true;
617 continue;
618 }
619
620 if (i == keymax)
621 {
622 keymax += 5;
623 mutt_mem_realloc(&key_table, sizeof(struct PgpUid *) * keymax);
624 }
625
626 key_table[i++] = a;
627 }
628 }
629
630 if ((i == 0) && unusable)
631 {
632 mutt_error(_("All matching keys are expired, revoked, or disabled"));
633 return NULL;
634 }
635
636 int (*f)(const void *, const void *);
637 const short c_pgp_sort_keys = cs_subset_sort(NeoMutt->sub, "pgp_sort_keys");
638 switch (c_pgp_sort_keys & SORT_MASK)
639 {
640 case SORT_ADDRESS:
642 break;
643 case SORT_DATE:
645 break;
646 case SORT_KEYID:
648 break;
649 case SORT_TRUST:
650 default:
652 break;
653 }
654
655 if (key_table)
656 qsort(key_table, i, sizeof(struct PgpUid *), f);
657
659
660 menu = dlg->wdata;
661 menu->max = i;
663 menu->mdata = key_table;
665
666 struct PgpData pd = { false, menu, key_table, NULL };
667 dlg->wdata = &pd;
668
669 // NT_COLOR is handled by the SimpleDialog
672
673 if (p)
674 snprintf(buf, sizeof(buf), _("PGP keys matching <%s>"), buf_string(p->mailbox));
675 else
676 snprintf(buf, sizeof(buf), _("PGP keys matching \"%s\""), s);
677
678 struct MuttWindow *sbar = window_find_child(dlg, WT_STATUS_BAR);
679 sbar_set_title(sbar, buf);
680
682
683 // ---------------------------------------------------------------------------
684 // Event Loop
685 int op = OP_NULL;
686 do
687 {
688 menu_tagging_dispatcher(menu->win, op);
689 window_redraw(NULL);
690
691 op = km_dokey(MENU_PGP);
692 mutt_debug(LL_DEBUG1, "Got op %s (%d)\n", opcodes_get_name(op), op);
693 if (op < 0)
694 continue;
695 if (op == OP_NULL)
696 {
698 continue;
699 }
701
702 int rc = pgp_function_dispatcher(dlg, op);
703
704 if (rc == FR_UNKNOWN)
705 rc = menu_function_dispatcher(menu->win, op);
706 if (rc == FR_UNKNOWN)
707 rc = global_function_dispatcher(NULL, op);
708 } while (!pd.done);
709 // ---------------------------------------------------------------------------
710
711 simple_dialog_free(&dlg);
712 return pd.key;
713}
static const char * buf_string(const struct Buffer *buf)
Convert a buffer to a const char * "string".
Definition: buffer.h:90
bool cs_subset_bool(const struct ConfigSubset *sub, const char *name)
Get a boolean config item by name.
Definition: helpers.c:73
@ FR_UNKNOWN
Unknown function.
Definition: dispatcher.h:33
static int pgp_compare_address_qsort(const void *a, const void *b)
Compare the addresses of two PGP keys.
Definition: dlg_pgp.c:144
static int pgp_compare_keyid_qsort(const void *a, const void *b)
Compare key IDs.
Definition: dlg_pgp.c:212
static int pgp_compare_date_qsort(const void *a, const void *b)
Compare the dates of two PGP keys.
Definition: dlg_pgp.c:178
static int pgp_compare_trust_qsort(const void *a, const void *b)
Compare the trust levels of two PGP keys.
Definition: dlg_pgp.c:262
static const struct Mapping PgpHelp[]
Help Bar for the PGP key selection dialog.
Definition: dlg_pgp.c:94
int menu_tagging_dispatcher(struct MuttWindow *win, int op)
Perform tagging operations on the Menu - Implements function_dispatcher_t -.
Definition: tagging.c:223
int pgp_function_dispatcher(struct MuttWindow *win, int op)
Perform a Pgp function - Implements function_dispatcher_t -.
int global_function_dispatcher(struct MuttWindow *win, int op)
Perform a Global function - Implements function_dispatcher_t -.
Definition: global.c:164
int menu_function_dispatcher(struct MuttWindow *win, int op)
Perform a Menu function - Implements function_dispatcher_t -.
Definition: functions.c:317
#define mutt_error(...)
Definition: logging2.h:90
#define mutt_debug(LEVEL,...)
Definition: logging2.h:87
static void pgp_make_entry(struct Menu *menu, char *buf, size_t buflen, int line)
Format a menu item for the pgp key list - Implements Menu::make_entry() -.
Definition: dlg_pgp.c:506
static void pgp_key_table_free(struct Menu *menu, void **ptr)
Free the key table - Implements Menu::mdata_free() -.
Definition: dlg_pgp.c:525
static int pgp_key_window_observer(struct NotifyCallback *nc)
Notification that a Window has changed - Implements observer_t -.
Definition: dlg_pgp.c:562
static int pgp_key_config_observer(struct NotifyCallback *nc)
Notification that a Config Variable has changed - Implements observer_t -.
Definition: dlg_pgp.c:533
void simple_dialog_free(struct MuttWindow **ptr)
Destroy a simple index Dialog.
Definition: simple.c:166
struct MuttWindow * simple_dialog_new(enum MenuType mtype, enum WindowType wtype, const struct Mapping *help_data)
Create a simple index Dialog.
Definition: simple.c:129
int km_dokey(enum MenuType mtype)
Determine what a keypress should do.
Definition: keymap.c:803
void km_error_key(enum MenuType mtype)
Handle an unbound key sequence.
Definition: keymap.c:1077
@ LL_DEBUG1
Log at debug level 1.
Definition: logging2.h:43
void mutt_mem_realloc(void *ptr, size_t size)
Resize a block of memory on the heap.
Definition: memory.c:114
#define _(a)
Definition: message.h:28
bool notify_observer_add(struct Notify *notify, enum NotifyType type, observer_t callback, void *global_data)
Add an observer to an object.
Definition: notify.c:189
void mutt_clear_error(void)
Clear the message line (bottom line of screen)
Definition: mutt_logging.c:73
void window_redraw(struct MuttWindow *win)
Reflow, recalc and repaint a tree of Windows.
Definition: mutt_window.c:605
struct MuttWindow * window_find_child(struct MuttWindow *win, enum WindowType type)
Recursively find a child Window of a given type.
Definition: mutt_window.c:523
@ WT_DLG_PGP
Pgp Dialog, dlg_select_pgp_key()
Definition: mutt_window.h:88
@ WT_STATUS_BAR
Status Bar containing extra info about the Index/Pager/etc.
Definition: mutt_window.h:102
#define KEYFLAG_CANTUSE
Definition: lib.h:139
@ NT_WINDOW
MuttWindow has changed, NotifyWindow, EventWindow.
Definition: notify_type.h:55
@ NT_CONFIG
Config has changed, NotifyConfig, EventConfig.
Definition: notify_type.h:43
const char * opcodes_get_name(int op)
Get the name of an opcode.
Definition: opcodes.c:48
void sbar_set_title(struct MuttWindow *win, const char *title)
Set the title for the Simple Bar.
Definition: sbar.c:224
#define SORT_MASK
Mask for the sort id.
Definition: sort2.h:74
@ SORT_TRUST
Sort by encryption key's trust level.
Definition: sort2.h:52
@ SORT_KEYID
Sort by the encryption key's ID.
Definition: sort2.h:51
@ SORT_DATE
Sort by the date the email was sent.
Definition: sort2.h:39
@ SORT_ADDRESS
Sort by email address.
Definition: sort2.h:50
struct Buffer * mailbox
Mailbox and host address.
Definition: address.h:38
Definition: lib.h:70
struct MuttWindow * win
Window holding the Menu.
Definition: lib.h:77
void(* make_entry)(struct Menu *menu, char *buf, size_t buflen, int line)
Definition: lib.h:97
void(* mdata_free)(struct Menu *menu, void **ptr)
Definition: lib.h:152
void * mdata
Private data.
Definition: lib.h:138
int max
Number of entries in the menu.
Definition: lib.h:72
void * wdata
Private data.
Definition: mutt_window.h:145
struct Notify * notify
Notifications: NotifyWindow, EventWindow.
Definition: mutt_window.h:138
struct Notify * notify
Notifications handler.
Definition: neomutt.h:38
Data to pass to the Pgp Functions.
Definition: pgp_functions.h:34
struct PgpUid ** key_table
Array of Keys.
Definition: pgp_functions.h:37
struct Menu * menu
Pgp Menu.
Definition: pgp_functions.h:36
bool done
Should we close the Dialog?
Definition: pgp_functions.h:35
struct PgpKeyInfo * key
Selected Key.
Definition: pgp_functions.h:38
Information about a PGP key.
Definition: pgplib.h:47
struct PgpKeyInfo * next
Definition: pgplib.h:57
int flags
Definition: pgplib.h:38
struct PgpUid * next
Linked list.
Definition: pgplib.h:40
@ MENU_PGP
PGP encryption menu.
Definition: type.h:55
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

Variable Documentation

◆ PgpHelp

const struct Mapping PgpHelp[]
static
Initial value:
= {
{ N_("Exit"), OP_EXIT },
{ N_("Select"), OP_GENERIC_SELECT_ENTRY },
{ N_("Check key"), OP_VERIFY_KEY },
{ N_("Help"), OP_HELP },
{ NULL, 0 },
}
#define N_(a)
Definition: message.h:32

Help Bar for the PGP key selection dialog.

Definition at line 94 of file dlg_pgp.c.

◆ TrustFlags

const char TrustFlags[] = "?- +"
static

Characters used to show the trust level for PGP keys.

Definition at line 114 of file dlg_pgp.c.