unofficial mirror of notmuch@notmuchmail.org
 help / color / mirror / code / Atom feed
blob daeb1defe252145fb925561ea18d7895a97e49e7 5130 bytes (raw)
name: show-message.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
 
/* notmuch - Not much of an email program, (just index and search)
 *
 * Copyright © 2009 Carl Worth
 * Copyright © 2009 Keith Packard
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * 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/ .
 *
 * Authors: Carl Worth <cworth@cworth.org>
 *	    Keith Packard <keithp@keithp.com>
 */

#include "notmuch-client.h"

static void
show_message_part (GMimeObject *part, int *part_count,
		   void (*show_part) (GMimeObject *part, int *part_count))
{
    if (GMIME_IS_MULTIPART (part)) {
	GMimeMultipart *multipart = GMIME_MULTIPART (part);
	int i;

       //*part_count = *part_count + 1;
       //(*show_part) (part, part_count);

	for (i = 0; i < g_mime_multipart_get_count (multipart); i++) {
	    show_message_part (g_mime_multipart_get_part (multipart, i),
			       part_count, show_part);
	}
	return;
    }

    if (GMIME_IS_MESSAGE_PART (part)) {
	GMimeMessage *mime_message;

	mime_message = g_mime_message_part_get_message (GMIME_MESSAGE_PART (part));

	show_message_part (g_mime_message_get_mime_part (mime_message),
			   part_count, show_part);

	return;
    }

    if (! (GMIME_IS_PART (part))) {
	fprintf (stderr, "Warning: Not displaying unknown mime part: %s.\n",
		 g_type_name (G_OBJECT_TYPE (part)));
	return;
    }

    *part_count = *part_count + 1;

    (*show_part) (part, part_count);
}

notmuch_status_t
show_message_body (const char *filename,
		   void (*show_part) (GMimeObject *part, int *part_count))
{
    GMimeStream *stream = NULL;
    GMimeParser *parser = NULL;
    GMimeMessage *mime_message = NULL;
    notmuch_status_t ret = NOTMUCH_STATUS_SUCCESS;
    FILE *file = NULL;
    int part_count = 0;

    file = fopen (filename, "r");
    if (! file) {
	fprintf (stderr, "Error opening %s: %s\n", filename, strerror (errno));
	ret = NOTMUCH_STATUS_FILE_ERROR;
	goto DONE;
    }

    stream = g_mime_stream_file_new (file);
    g_mime_stream_file_set_owner (GMIME_STREAM_FILE (stream), FALSE);

    parser = g_mime_parser_new_with_stream (stream);

    mime_message = g_mime_parser_construct_message (parser);

    show_message_part (g_mime_message_get_mime_part (mime_message),
		       &part_count, show_part);

  DONE:
    if (mime_message)
	g_object_unref (mime_message);

    if (parser)
	g_object_unref (parser);

    if (stream)
	g_object_unref (stream);

    if (file)
	fclose (file);

    return ret;
}

static void
show_one_part_output (GMimeObject *part)
{
    GMimeStream *stream_filter = NULL;
    GMimeDataWrapper *wrapper;
    GMimeStream *stream_stdout = g_mime_stream_file_new (stdout);

    stream_filter = g_mime_stream_filter_new(stream_stdout);
    wrapper = g_mime_part_get_content_object (GMIME_PART (part));
    if (wrapper && stream_filter)
	g_mime_data_wrapper_write_to_stream (wrapper, stream_filter);
    if (stream_filter)
	g_object_unref(stream_filter);
}

static void
show_one_part_worker (GMimeObject *part, int *part_count, int desired_part)
{
    if (GMIME_IS_MULTIPART (part)) {
	GMimeMultipart *multipart = GMIME_MULTIPART (part);
	int i;

	for (i = 0; i < g_mime_multipart_get_count (multipart); i++) {
		show_one_part_worker (g_mime_multipart_get_part (multipart, i),
				      part_count, desired_part);
	}
	return;
    }

    if (GMIME_IS_MESSAGE_PART (part)) {
	GMimeMessage *mime_message;

	mime_message = g_mime_message_part_get_message (GMIME_MESSAGE_PART (part));

	show_one_part_worker (g_mime_message_get_mime_part (mime_message),
			      part_count, desired_part);

	return;
    }

    if (! (GMIME_IS_PART (part)))
	return;

    *part_count = *part_count + 1;

    if (*part_count == desired_part)
	    show_one_part_output (part);
}

notmuch_status_t
show_one_part (const char *filename, int part)
{
	GMimeStream *stream = NULL;
	GMimeParser *parser = NULL;
	GMimeMessage *mime_message = NULL;
	notmuch_status_t ret = NOTMUCH_STATUS_SUCCESS;
	FILE *file = NULL;
	int part_count = 0;

	file = fopen (filename, "r");
	if (! file) {
		fprintf (stderr, "Error opening %s: %s\n", filename, strerror (errno));
		ret = NOTMUCH_STATUS_FILE_ERROR;
		goto DONE;
	}

	stream = g_mime_stream_file_new (file);
	g_mime_stream_file_set_owner (GMIME_STREAM_FILE (stream), FALSE);

	parser = g_mime_parser_new_with_stream (stream);

	mime_message = g_mime_parser_construct_message (parser);

	show_one_part_worker (g_mime_message_get_mime_part (mime_message),
			      &part_count, part);

 DONE:
	if (mime_message)
		g_object_unref (mime_message);

	if (parser)
		g_object_unref (parser);

	if (stream)
		g_object_unref (stream);

	if (file)
		fclose (file);

	return ret;
}

debug log:

solving 6913d7f ...
found 6913d7f in https://yhetil.org/notmuch/877h9hmt8k.fsf@pc44es141.cs.uni-magdeburg.de/
found ff9146e in https://yhetil.org/notmuch.git/
preparing index
index prepared:
100644 ff9146e233124b81a110481b58ff4e76a57525a5	show-message.c

applying [1/1] https://yhetil.org/notmuch/877h9hmt8k.fsf@pc44es141.cs.uni-magdeburg.de/
diff --git a/show-message.c b/show-message.c
index ff9146e..6913d7f 100644

Checking patch show-message.c...
Applied patch show-message.c cleanly.

index at:
100644 daeb1defe252145fb925561ea18d7895a97e49e7	show-message.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).