unofficial mirror of notmuch@notmuchmail.org
 help / color / mirror / code / Atom feed
blob b3f91c55f40319d824a6512004e3516f8b1c3704 7298 bytes (raw)
name: util/gmime-extra.c 	 # note: path name is non-authoritative(*)

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
 
#include "gmime-extra.h"
#include <string.h>

static
GMimeStream *
_gzfile_maybe_filter (GMimeStream *file_stream) {
    char buf[4];
    int bytes_read;

    if ((bytes_read = g_mime_stream_read (file_stream, buf, sizeof (buf))) < 0)
	return NULL;

    if (g_mime_stream_reset (file_stream))
	return NULL;

    /* check for gzipped input */
    if (bytes_read >= 2 && buf[0] == 0x1f && (unsigned char)buf[1] == 0x8b) {
	GMimeStream *gzstream;
	GMimeFilter *gzfilter;

	gzfilter = g_mime_filter_gzip_new (GMIME_FILTER_GZIP_MODE_UNZIP, 0);
	if (! gzfilter)
	    return NULL;

	gzstream = g_mime_stream_filter_new (file_stream);
	if (! gzstream)
	    return NULL;

	g_mime_stream_filter_add ((GMimeStreamFilter *)gzstream, gzfilter);
	return gzstream;
    } else {
	return file_stream;
    }
}

GMimeStream *
g_mime_stream_gzfile_new (int fd)
{
    GMimeStream *file_stream;

    file_stream = g_mime_stream_fs_new (fd);
    if (! file_stream)
	return NULL;

    return _gzfile_maybe_filter (file_stream);
}

GMimeStream *
g_mime_stream_gzfile_open (const char *filename)
{
    GMimeStream *file_stream;

    file_stream = g_mime_stream_fs_open (filename, 0, 0, NULL);
    if (! file_stream)
	return NULL;

    return _gzfile_maybe_filter (file_stream);
}

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)

const char *
g_mime_certificate_get_valid_userid (GMimeCertificate *cert)
{
    /* output user id only if validity is FULL or ULTIMATE. */
    /* note that gmime 2.6 is using the term "trust" here, which
     * is WRONG.  It's actually user id "validity". */
    const char *name = g_mime_certificate_get_name (cert);
    if (name == NULL)
	return name;
    GMimeCertificateTrust trust = g_mime_certificate_get_trust (cert);
    if (trust == GMIME_CERTIFICATE_TRUST_FULLY || trust == GMIME_CERTIFICATE_TRUST_ULTIMATE)
	return name;
    return NULL;
}

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_valid_userid (GMimeCertificate *cert)
{
    /* output user id only if validity is FULL or ULTIMATE. */
    const char *uid = g_mime_certificate_get_user_id (cert);
    if (uid == NULL)
	return uid;
    GMimeValidity validity = g_mime_certificate_get_id_validity (cert);
    if (validity == GMIME_VALIDITY_FULL || validity == GMIME_VALIDITY_ULTIMATE)
	return uid;
    return NULL;
}

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

debug log:

solving b3f91c55 ...
found b3f91c55 in https://yhetil.org/notmuch/20190330140633.30422-2-david@tethera.net/ ||
	https://yhetil.org/notmuch/87pnp3ieyh.fsf@tethera.net/
found bc1e3c4d in https://yhetil.org/notmuch.git/
preparing index
index prepared:
100644 bc1e3c4d94bb75b04c7b24c8e8816770a7e83434	util/gmime-extra.c

applying [1/1] https://yhetil.org/notmuch/20190330140633.30422-2-david@tethera.net/
diff --git a/util/gmime-extra.c b/util/gmime-extra.c
index bc1e3c4d..b3f91c55 100644

Checking patch util/gmime-extra.c...
Applied patch util/gmime-extra.c cleanly.

skipping https://yhetil.org/notmuch/87pnp3ieyh.fsf@tethera.net/ for b3f91c55
index at:
100644 b3f91c55f40319d824a6512004e3516f8b1c3704	util/gmime-extra.c

(*) Git path names are given by the tree(s) the blob belongs to.
    Blobs themselves have no identifier aside from the hash of its contents.^

Code repositories for project(s) associated with this public inbox

	https://yhetil.org/notmuch.git/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).