NeoMutt  2024-11-14-138-ge5ca67
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
sort_gpgme.c
Go to the documentation of this file.
1
30#include "config.h"
31#include <locale.h>
32#include <stdbool.h>
33#include "mutt/lib.h"
34#include "config/lib.h"
35#include "core/lib.h"
36#include "lib.h"
37#include "crypt_gpgme.h"
38#include "sort.h"
39
43static int crypt_sort_address(const void *a, const void *b, void *sdata)
44{
45 struct CryptKeyInfo *s = *(struct CryptKeyInfo **) a;
46 struct CryptKeyInfo *t = *(struct CryptKeyInfo **) b;
47 const bool sort_reverse = *(bool *) sdata;
48
49 int rc = mutt_istr_cmp(s->uid, t->uid);
50 if (rc != 0)
51 goto done;
52
54
55done:
56 return sort_reverse ? -rc : rc;
57}
58
62static int crypt_sort_keyid(const void *a, const void *b, void *sdata)
63{
64 struct CryptKeyInfo *s = *(struct CryptKeyInfo **) a;
65 struct CryptKeyInfo *t = *(struct CryptKeyInfo **) b;
66 const bool sort_reverse = *(bool *) sdata;
67
69 if (rc != 0)
70 goto done;
71
72 rc = mutt_istr_cmp(s->uid, t->uid);
73
74done:
75 return sort_reverse ? -rc : rc;
76}
77
81static int crypt_sort_date(const void *a, const void *b, void *sdata)
82{
83 struct CryptKeyInfo *s = *(struct CryptKeyInfo **) a;
84 struct CryptKeyInfo *t = *(struct CryptKeyInfo **) b;
85 const bool sort_reverse = *(bool *) sdata;
86
87 unsigned long ts = 0;
88 unsigned long tt = 0;
89 int rc = 0;
90
91 if (s->kobj->subkeys && (s->kobj->subkeys->timestamp > 0))
92 ts = s->kobj->subkeys->timestamp;
93 if (t->kobj->subkeys && (t->kobj->subkeys->timestamp > 0))
94 tt = t->kobj->subkeys->timestamp;
95
96 if (ts > tt)
97 {
98 rc = 1;
99 goto done;
100 }
101
102 if (ts < tt)
103 {
104 rc = -1;
105 goto done;
106 }
107
108 rc = mutt_istr_cmp(s->uid, t->uid);
109
110done:
111 return sort_reverse ? -rc : rc;
112}
113
117static int crypt_sort_trust(const void *a, const void *b, void *sdata)
118{
119 struct CryptKeyInfo *s = *(struct CryptKeyInfo **) a;
120 struct CryptKeyInfo *t = *(struct CryptKeyInfo **) b;
121 const bool sort_reverse = *(bool *) sdata;
122
123 unsigned long ts = 0;
124 unsigned long tt = 0;
125
127 if (rc != 0)
128 goto done;
129
130 // Note: reversed
132 if (rc != 0)
133 return rc;
134
135 ts = 0;
136 tt = 0;
137 if (s->kobj->subkeys)
138 ts = s->kobj->subkeys->length;
139 if (t->kobj->subkeys)
140 tt = t->kobj->subkeys->length;
141
142 // Note: reversed
143 rc = mutt_numeric_cmp(tt, ts);
144 if (rc != 0)
145 goto done;
146
147 ts = 0;
148 tt = 0;
149 if (s->kobj->subkeys && (s->kobj->subkeys->timestamp > 0))
150 ts = s->kobj->subkeys->timestamp;
151 if (t->kobj->subkeys && (t->kobj->subkeys->timestamp > 0))
152 tt = t->kobj->subkeys->timestamp;
153
154 // Note: reversed
155 rc = mutt_numeric_cmp(tt, ts);
156 if (rc != 0)
157 goto done;
158
159 rc = mutt_istr_cmp(s->uid, t->uid);
160 if (rc != 0)
161 goto done;
162
164
165done:
166 return sort_reverse ? -rc : rc;
167}
168
175void gpgme_sort_keys(struct CryptKeyInfoArray *ckia)
176{
177 const short c_pgp_sort_keys = cs_subset_sort(NeoMutt->sub, "pgp_key_sort");
178 sort_t fn = NULL;
179 switch (c_pgp_sort_keys & SORT_MASK)
180 {
181 case KEY_SORT_ADDRESS:
183 break;
184 case KEY_SORT_DATE:
185 fn = crypt_sort_date;
186 break;
187 case KEY_SORT_KEYID:
188 fn = crypt_sort_keyid;
189 break;
190 case KEY_SORT_TRUST:
191 default:
192 fn = crypt_sort_trust;
193 break;
194 }
195
196 if (ARRAY_SIZE(ckia) > 1)
197 {
198 bool sort_reverse = c_pgp_sort_keys & SORT_REVERSE;
199 ARRAY_SORT(ckia, fn, &sort_reverse);
200 }
201}
#define ARRAY_SORT(head, fn, sdata)
Sort an array.
Definition: array.h:279
#define ARRAY_SIZE(head)
The number of elements stored.
Definition: array.h:87
short cs_subset_sort(const struct ConfigSubset *sub, const char *name)
Get a sort config item by name.
Definition: helpers.c:266
Convenience wrapper for the config headers.
#define SORT_MASK
Mask for the sort id.
Definition: sort.h:38
#define mutt_numeric_cmp(a, b)
Definition: sort.h:26
#define SORT_REVERSE
Reverse the order of the sort.
Definition: sort.h:39
Convenience wrapper for the core headers.
const char * crypt_fpr_or_lkeyid(struct CryptKeyInfo *k)
Find the fingerprint of a key.
Definition: crypt_gpgme.c:214
Wrapper for PGP/SMIME calls to GPGME.
static int crypt_sort_trust(const void *a, const void *b, void *sdata)
Compare two keys by their trust levels - Implements sort_t -.
Definition: sort_gpgme.c:117
static int crypt_sort_address(const void *a, const void *b, void *sdata)
Compare two keys by their addresses - Implements sort_t -.
Definition: sort_gpgme.c:43
static int crypt_sort_date(const void *a, const void *b, void *sdata)
Compare two keys by their dates - Implements sort_t -.
Definition: sort_gpgme.c:81
static int crypt_sort_keyid(const void *a, const void *b, void *sdata)
Compare two keys by their IDs - Implements sort_t -.
Definition: sort_gpgme.c:62
Convenience wrapper for the library headers.
int mutt_istr_cmp(const char *a, const char *b)
Compare two strings ignoring case, safely.
Definition: string.c:412
#define KEYFLAG_RESTRICTIONS
Definition: lib.h:146
@ KEY_SORT_ADDRESS
Sort by address.
Definition: sort.h:34
@ KEY_SORT_DATE
Sort by date.
Definition: sort.h:35
@ KEY_SORT_TRUST
Sort by trust level.
Definition: sort.h:37
@ KEY_SORT_KEYID
Sort by key id.
Definition: sort.h:36
int(* sort_t)(const void *a, const void *b, void *sdata)
Definition: qsort_r.h:41
Sidebar sorting functions.
void gpgme_sort_keys(struct CryptKeyInfoArray *ckia)
Sort an array of GPGME keys.
Definition: sort_gpgme.c:175
Key value store.
A stored PGP key.
Definition: crypt_gpgme.h:44
gpgme_validity_t validity
uid validity (cached for convenience)
Definition: crypt_gpgme.h:50
KeyFlags flags
global and per uid flags (for convenience)
Definition: crypt_gpgme.h:49
const char * uid
and for convenience point to this user ID
Definition: crypt_gpgme.h:48
gpgme_key_t kobj
Definition: crypt_gpgme.h:46
Container for Accounts, Notifications.
Definition: neomutt.h:42
struct ConfigSubset * sub
Inherited config items.
Definition: neomutt.h:46