diff options
| author | David Bremner <david@tethera.net> | 2017-08-27 15:54:50 -0300 |
|---|---|---|
| committer | David Bremner <david@tethera.net> | 2017-08-27 15:54:50 -0300 |
| commit | a565f71e1c160431ff99f088bc8fc08d367603a2 (patch) | |
| tree | 2f7c33d7296e15b2c33f6ede436d412c812c3f15 /util | |
| parent | 00f87faf4bc19e90e19b8b27c13845efb6a68152 (diff) | |
| parent | 6354745dcd6505c5f12c185a29c25a8d1c240595 (diff) | |
Merge tag 'debian/0.25-6' into debian/stretch-backports
Diffstat (limited to 'util')
| -rw-r--r-- | util/Makefile.local | 12 | ||||
| -rw-r--r-- | util/gmime-extra.c | 199 | ||||
| -rw-r--r-- | util/gmime-extra.h | 110 |
3 files changed, 315 insertions, 6 deletions
diff --git a/util/Makefile.local b/util/Makefile.local index 905f2376..3027880b 100644 --- a/util/Makefile.local +++ b/util/Makefile.local @@ -3,14 +3,14 @@ dir := util extra_cflags += -I$(srcdir)/$(dir) -libutil_c_srcs := $(dir)/xutil.c $(dir)/error_util.c $(dir)/hex-escape.c \ +libnotmuch_util_c_srcs := $(dir)/xutil.c $(dir)/error_util.c $(dir)/hex-escape.c \ $(dir)/string-util.c $(dir)/talloc-extra.c $(dir)/zlib-extra.c \ - $(dir)/util.c + $(dir)/util.c $(dir)/gmime-extra.c -libutil_modules := $(libutil_c_srcs:.c=.o) +libnotmuch_util_modules := $(libnotmuch_util_c_srcs:.c=.o) -$(dir)/libutil.a: $(libutil_modules) +$(dir)/libnotmuch_util.a: $(libnotmuch_util_modules) $(call quiet,AR) rcs $@ $^ -SRCS := $(SRCS) $(libutil_c_srcs) -CLEAN := $(CLEAN) $(libutil_modules) $(dir)/libutil.a +SRCS := $(SRCS) $(libnotmuch_util_c_srcs) +CLEAN := $(CLEAN) $(libnotmuch_util_modules) $(dir)/libnotmuch_util.a diff --git a/util/gmime-extra.c b/util/gmime-extra.c new file mode 100644 index 00000000..901d4d56 --- /dev/null +++ b/util/gmime-extra.c @@ -0,0 +1,199 @@ +#include "gmime-extra.h" +#include <string.h> + +GMimeStream * +g_mime_stream_stdout_new() +{ + GMimeStream *stream_stdout = NULL; + GMimeStream *stream_buffered = NULL; + + stream_stdout = g_mime_stream_pipe_new (STDOUT_FILENO); + if (!stream_stdout) + return NULL; + + g_mime_stream_pipe_set_owner (GMIME_STREAM_PIPE (stream_stdout), FALSE); + + stream_buffered = g_mime_stream_buffer_new (stream_stdout, GMIME_STREAM_BUFFER_BLOCK_WRITE); + + g_object_unref (stream_stdout); + + return stream_buffered; +} + +/** + * copy a glib string into a talloc context, and free it. + */ +static char* +g_string_talloc_strdup (void *ctx, char *g_string) +{ + char *new_str = talloc_strdup (ctx, g_string); + g_free (g_string); + return new_str; +} + +#if (GMIME_MAJOR_VERSION < 3) + +char * +g_mime_message_get_address_string (GMimeMessage *message, GMimeRecipientType type) +{ + InternetAddressList *list = g_mime_message_get_recipients (message, type); + return internet_address_list_to_string (list, 0); +} + +inline InternetAddressList * +g_mime_message_get_addresses (GMimeMessage *message, GMimeRecipientType type) +{ + return g_mime_message_get_recipients (message, type); +} + +char * +g_mime_message_get_date_string (void *ctx, GMimeMessage *message) +{ + char *date = g_mime_message_get_date_as_string (message); + return g_string_talloc_strdup (ctx, date); +} + +InternetAddressList * +g_mime_message_get_from (GMimeMessage *message) +{ + return internet_address_list_parse_string (g_mime_message_get_sender (message)); +} + +const char * +g_mime_message_get_from_string (GMimeMessage *message) { + return g_mime_message_get_sender (message); +} + +InternetAddressList * +g_mime_message_get_reply_to_list (GMimeMessage *message) +{ + const char *reply_to; + + reply_to = g_mime_message_get_reply_to (message); + if (reply_to && *reply_to) + return internet_address_list_parse_string (reply_to); + else + return NULL; +} + +/** + * return talloc allocated reply-to string + */ +char * +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); +} + +gint64 +g_mime_utils_header_decode_date_unix (const char *date) { + return (gint64) g_mime_utils_header_decode_date (date, NULL); +} + +#else /* GMime >= 3.0 */ + +const char* +g_mime_certificate_get_fpr16 (GMimeCertificate *cert) { + const char *fpr = g_mime_certificate_get_fingerprint (cert); + if (!fpr || strlen (fpr) < 16) + return fpr; + + return fpr + (strlen (fpr) - 16); +} + +char * +g_mime_message_get_address_string (GMimeMessage *message, GMimeAddressType type) +{ + InternetAddressList *list = g_mime_message_get_addresses (message, type); + return internet_address_list_to_string (list, NULL, 0); +} + +char * +g_mime_message_get_date_string (void *ctx, GMimeMessage *message) +{ + GDateTime* parsed_date = g_mime_message_get_date (message); + if (parsed_date) { + char *date = g_mime_utils_header_format_date (parsed_date); + return g_string_talloc_strdup (ctx, date); + } else { + return talloc_strdup(ctx, "Thu, 01 Jan 1970 00:00:00 +0000"); + } +} + +InternetAddressList * +g_mime_message_get_reply_to_list(GMimeMessage *message) +{ + return g_mime_message_get_reply_to (message); +} + +const char * +g_mime_message_get_from_string (GMimeMessage *message) +{ + return g_mime_object_get_header (GMIME_OBJECT (message), "From"); +} + +char * +g_mime_message_get_reply_to_string (void *ctx, GMimeMessage *message) +{ + InternetAddressList *list = g_mime_message_get_reply_to (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); +} + +gint64 +g_mime_utils_header_decode_date_unix (const char *date) { + GDateTime* parsed_date = g_mime_utils_header_decode_date (date); + time_t ret; + + if (parsed_date) { + ret = g_date_time_to_unix (parsed_date); + g_date_time_unref (parsed_date); + } else { + ret = 0; + } + + return ret; +} + +#endif diff --git a/util/gmime-extra.h b/util/gmime-extra.h new file mode 100644 index 00000000..de275bc1 --- /dev/null +++ b/util/gmime-extra.h @@ -0,0 +1,110 @@ +#ifndef _GMIME_EXTRA_H +#define _GMIME_EXTRA_H +#include <gmime/gmime.h> + +GMimeStream *g_mime_stream_stdout_new(void); + +#include <talloc.h> + + +#if (GMIME_MAJOR_VERSION < 3) + +#define GMIME_ADDRESS_TYPE_TO GMIME_RECIPIENT_TYPE_TO +#define GMIME_ADDRESS_TYPE_CC GMIME_RECIPIENT_TYPE_CC +#define GMIME_ADDRESS_TYPE_BCC GMIME_RECIPIENT_TYPE_BCC + +#define g_mime_2_6_unref(obj) g_object_unref (obj) +#define g_mime_3_unused(arg) arg +#define g_mime_certificate_get_fpr16(cert) g_mime_certificate_get_key_id (cert) +#define g_mime_certificate_get_uid(cert) g_mime_certificate_get_name (cert); +#else /* GMime >= 3.0 */ +typedef GMimeAddressType GMimeRecipientType; + +#define GMIME_ENABLE_RFC_2047_WORKAROUNDS 0xdeadbeef +#define g_mime_certificate_get_uid(cert) g_mime_certificate_get_key_id (cert); +#define g_mime_content_type_to_string(c) g_mime_content_type_get_mime_type (c) +#define g_mime_filter_crlf_new(encode,dots) g_mime_filter_dos2unix_new (FALSE) +#define g_mime_gpg_context_new(func,path) g_mime_gpg_context_new () +#define g_mime_gpg_context_set_use_agent(ctx,val) /*ignore*/ +#define g_mime_gpg_context_set_always_trust(ctx,val) /*ignore*/ +#define g_mime_init(flags) g_mime_init() +#define g_mime_message_add_recipient(m,t,n,a) g_mime_message_add_mailbox (m,t,n,a) +#define g_mime_message_set_subject(m,s) g_mime_message_set_subject(m,s,NULL) +#define g_mime_multipart_encrypted_decrypt(mpe,ctx,out,err) g_mime_multipart_encrypted_decrypt(mpe, GMIME_DECRYPT_NONE, NULL, out, err) +#define g_mime_multipart_signed_verify(mps,ctx,err) g_mime_multipart_signed_verify(mps, GMIME_ENCRYPT_NONE, err) +#define g_mime_object_write_to_stream(o,s) g_mime_object_write_to_stream (o,NULL,s) +#define g_mime_object_set_header(o,h,v) g_mime_object_set_header (o,h,v,NULL) +#define g_mime_parser_construct_message(p) g_mime_parser_construct_message (p, g_mime_parser_options_get_default ()) +#define g_mime_part_get_content_object(p) g_mime_part_get_content (p) +#define g_mime_pkcs7_context_new(arg) g_mime_pkcs7_context_new() +#define g_mime_pkcs7_context_set_always_trust(ctx,val) /*ignore*/ +#define g_mime_signature_get_errors(sig) g_mime_signature_get_status (sig) +#define g_mime_utils_header_decode_text(txt) g_mime_utils_header_decode_text (NULL, txt) +#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; + +typedef GMimeTrust GMimeCertificateTrust; + +#define GMIME_CERTIFICATE_TRUST_UNKNOWN GMIME_TRUST_UNKNOWN +#define GMIME_CERTIFICATE_TRUST_UNDEFINED GMIME_TRUST_UNDEFINED +#define GMIME_CERTIFICATE_TRUST_NEVER GMIME_TRUST_NEVER +#define GMIME_CERTIFICATE_TRUST_MARGINAL GMIME_TRUST_MARGINAL +#define GMIME_CERTIFICATE_TRUST_FULLY GMIME_TRUST_FULL +#define GMIME_CERTIFICATE_TRUST_ULTIMATE GMIME_TRUST_ULTIMATE + +#define g_mime_2_6_unref(obj) /*ignore*/ +#define g_mime_3_unused(arg) unused(arg) +#endif + +/** + * Get last 16 hex digits of fingerprint ("keyid") + */ +const char *g_mime_certificate_get_fpr16 (GMimeCertificate *cert); +/** + * Return the contents of the appropriate address header as a string + * Should be freed using g_free + */ +char *g_mime_message_get_address_string (GMimeMessage *message, GMimeRecipientType type); + +InternetAddressList * g_mime_message_get_addresses (GMimeMessage *message, GMimeRecipientType type); + +/** + * return talloc allocated date string + */ + +char *g_mime_message_get_date_string (void *ctx, GMimeMessage *message); + +/** + * glib allocated list of From: addresses + */ + +InternetAddressList * g_mime_message_get_from (GMimeMessage *message); + + +/** + * return string for From: address + * (owned by gmime) + */ +const char * g_mime_message_get_from_string (GMimeMessage *message); + +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); + +gint64 g_mime_utils_header_decode_date_unix (const char *date); +#endif |
