From c7b9bdb81e49321170dc6f0a522301f28e791521 Mon Sep 17 00:00:00 2001 From: David Bremner Date: Sun, 14 May 2017 14:49:31 -0300 Subject: [PATCH] cli: generalize use of GMIME_SIGNATURE_{ERROR,STATUS} to gmime-3 The ERROR enum is merged into to the STATUS enum, and the set of flags is different. --- notmuch-show.c | 46 +++++++++++++++++++++++++++++++++++----------- util/gmime-extra.c | 41 +++++++++++++++++++++++++++++++++++++++++ util/gmime-extra.h | 13 ++++++++++++- 3 files changed, 88 insertions(+), 12 deletions(-) diff --git a/notmuch-show.c b/notmuch-show.c index 1f359a91..311ac6ac 100644 --- a/notmuch-show.c +++ b/notmuch-show.c @@ -320,22 +320,21 @@ show_text_part_content (GMimeObject *part, GMimeStream *stream_out, g_object_unref(stream_filter); } -/* Get signature status string (GMime 2.6) */ static const char* -signature_status_to_string (GMimeSignatureStatus x) +signature_status_to_string (GMimeSignatureStatus status) { - switch (x) { - case GMIME_SIGNATURE_STATUS_GOOD: - return "good"; - case GMIME_SIGNATURE_STATUS_BAD: + if (g_mime_signature_status_bad (status)) return "bad"; - case GMIME_SIGNATURE_STATUS_ERROR: + + if (g_mime_signature_status_error (status)) return "error"; - } + + if (g_mime_signature_status_good (status)) + return "good"; + return "unknown"; } - /* Print signature flags */ struct key_map_struct { GMimeSignatureError bit; @@ -358,6 +357,7 @@ do_format_signature_errors (sprinter_t *sp, struct key_map_struct *key_map, sp->end (sp); } +#if (GMIME_MAJOR_VERSION < 3) static void format_signature_errors (sprinter_t *sp, GMimeSignature *signature) { @@ -376,6 +376,30 @@ format_signature_errors (sprinter_t *sp, GMimeSignature *signature) do_format_signature_errors (sp, key_map, ARRAY_SIZE(key_map), errors); } +#else +static void +format_signature_errors (sprinter_t *sp, GMimeSignature *signature) +{ + GMimeSignatureError errors = g_mime_signature_get_errors (signature); + + if (!(errors & GMIME_SIGNATURE_STATUS_ERROR_MASK)) + return; + + struct key_map_struct key_map[] = { + { GMIME_SIGNATURE_STATUS_KEY_REVOKED, "key-revoked"}, + { GMIME_SIGNATURE_STATUS_KEY_EXPIRED, "key-expired"}, + { GMIME_SIGNATURE_STATUS_SIG_EXPIRED, "sig-expired" }, + { GMIME_SIGNATURE_STATUS_KEY_MISSING, "key-missing"}, + { GMIME_SIGNATURE_STATUS_CRL_MISSING, "crl-missing"}, + { GMIME_SIGNATURE_STATUS_CRL_TOO_OLD, "crl-too-old"}, + { GMIME_SIGNATURE_STATUS_BAD_POLICY, "bad-policy"}, + { GMIME_SIGNATURE_STATUS_SYS_ERROR, "sys-error"}, + { GMIME_SIGNATURE_STATUS_TOFU_CONFLICT, "tofu-conflict"}, + }; + + do_format_signature_errors (sp, key_map, ARRAY_SIZE(key_map), errors); +} +#endif /* Signature status sprinter (GMime 2.6) */ static void @@ -405,7 +429,7 @@ format_part_sigstatus_sprinter (sprinter_t *sp, mime_node_t *node) sp->string (sp, signature_status_to_string (status)); GMimeCertificate *certificate = g_mime_signature_get_certificate (signature); - if (status == GMIME_SIGNATURE_STATUS_GOOD) { + if (g_mime_signature_status_good (status)) { if (certificate) { sp->map_key (sp, "fingerprint"); sp->string (sp, g_mime_certificate_get_fingerprint (certificate)); @@ -443,7 +467,7 @@ format_part_sigstatus_sprinter (sprinter_t *sp, mime_node_t *node) if (notmuch_format_version <= 3) { GMimeSignatureError errors = g_mime_signature_get_errors (signature); - if (errors != GMIME_SIGNATURE_ERROR_NONE) { + if (g_mime_signature_status_error (errors)) { sp->map_key (sp, "errors"); sp->integer (sp, errors); } diff --git a/util/gmime-extra.c b/util/gmime-extra.c index 8f9e77a1..350f75ea 100644 --- a/util/gmime-extra.c +++ b/util/gmime-extra.c @@ -84,6 +84,20 @@ g_mime_message_get_reply_to_string (void *ctx, GMimeMessage *message) return talloc_strdup(ctx, g_mime_message_get_reply_to (message)); } +gboolean +g_mime_signature_status_good (GMimeSignatureStatus status) { + return (status == GMIME_SIGNATURE_STATUS_GOOD); +} + +gboolean +g_mime_signature_status_bad (GMimeSignatureStatus status) { + return (status == GMIME_SIGNATURE_STATUS_BAD); +} + +gboolean +g_mime_signature_status_error (GMimeSignatureError error) { + return (error != GMIME_SIGNATURE_ERROR_NONE); +} #else /* GMime >= 3.0 */ @@ -125,5 +139,32 @@ g_mime_message_get_reply_to_string (void *ctx, GMimeMessage *message) return g_string_talloc_strdup (ctx, internet_address_list_to_string (list, NULL, 0)); } +void +g_mime_parser_set_scan_from (GMimeParser *parser, gboolean flag) +{ + g_mime_parser_set_format (parser, flag ? GMIME_FORMAT_MBOX : GMIME_FORMAT_MESSAGE); +} + +/* In GMime 3.0, status GOOD and VALID both imply something about the + * validity of the UIDs attached to the signing key. This forces us to + * use following somewhat relaxed definition of a "good" signature to + * preserve current notmuch semantics. + */ + +gboolean +g_mime_signature_status_good (GMimeSignatureStatus status) { + return ((status & (GMIME_SIGNATURE_STATUS_RED | GMIME_SIGNATURE_STATUS_ERROR_MASK)) == 0); +} + +gboolean +g_mime_signature_status_bad (GMimeSignatureStatus status) { + return (status & GMIME_SIGNATURE_STATUS_RED); +} + +gboolean +g_mime_signature_status_error (GMimeSignatureStatus status) { + return (status & GMIME_SIGNATURE_STATUS_ERROR_MASK); +} + #endif diff --git a/util/gmime-extra.h b/util/gmime-extra.h index 3c16e46e..90f230c1 100644 --- a/util/gmime-extra.h +++ b/util/gmime-extra.h @@ -13,7 +13,6 @@ GMimeStream *g_mime_stream_stdout_new(void); #define GMIME_ADDRESS_TYPE_CC GMIME_RECIPIENT_TYPE_CC #define GMIME_ADDRESS_TYPE_BCC GMIME_RECIPIENT_TYPE_BCC - #else /* GMime >= 3.0 */ typedef GMimeAddressType GMimeRecipientType; @@ -39,6 +38,9 @@ typedef GMimeAddressType GMimeRecipientType; #define internet_address_to_string(ia,encode) internet_address_to_string (ia,NULL,encode) #define internet_address_list_parse_string(str) internet_address_list_parse (NULL,str) typedef GMimeAddressType GMimeRecipientType; + +typedef GMimeSignatureStatus GMimeSignatureError; + #endif /** @@ -61,6 +63,7 @@ char *g_mime_message_get_date_string (void *ctx, GMimeMessage *message); InternetAddressList * g_mime_message_get_from (GMimeMessage *message); + /** * return string for From: address * (owned by gmime) @@ -73,4 +76,12 @@ InternetAddressList * g_mime_message_get_reply_to_list (GMimeMessage *message); * return talloc allocated reply-to string */ char * g_mime_message_get_reply_to_string (void *ctx, GMimeMessage *message); + +void g_mime_parser_set_scan_from (GMimeParser *parser, gboolean flag); + +gboolean g_mime_signature_status_good (GMimeSignatureStatus status); + +gboolean g_mime_signature_status_bad (GMimeSignatureStatus status); + +gboolean g_mime_signature_status_error (GMimeSignatureError status); #endif