added.
Showing
1 changed file
with
220 additions
and
0 deletions
examples/mimetest.c
0 → 100644
1 | /* GNU mailutils - a suite of utilities for electronic mail | ||
2 | Copyright (C) 1999, 2000, 2001 Free Software Foundation, Inc. | ||
3 | |||
4 | This program is free software; you can redistribute it and/or modify | ||
5 | it under the terms of the GNU General Public License as published by | ||
6 | the Free Software Foundation; either version 2, or (at your option) | ||
7 | any later version. | ||
8 | |||
9 | This program is distributed in the hope that it will be useful, | ||
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
12 | GNU General Public License for more details. | ||
13 | |||
14 | You should have received a copy of the GNU General Public License | ||
15 | along with this program; if not, write to the Free Software | ||
16 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ | ||
17 | |||
18 | #ifdef HAVE_CONFIG_H | ||
19 | # include <config.h> | ||
20 | #endif | ||
21 | |||
22 | #include <sys/types.h> | ||
23 | #include <stdio.h> | ||
24 | #include <stdlib.h> | ||
25 | #include <string.h> | ||
26 | #include <unistd.h> | ||
27 | #include <limits.h> | ||
28 | |||
29 | #include <mailutils/mailbox.h> | ||
30 | #include <mailutils/header.h> | ||
31 | #include <mailutils/filter.h> | ||
32 | #include <mailutils/registrar.h> | ||
33 | |||
34 | void message_display_parts(message_t msg, char *indent); | ||
35 | |||
36 | char from[256]; | ||
37 | char subject[256]; | ||
38 | |||
39 | int | ||
40 | main(int argc, char **argv) | ||
41 | { | ||
42 | mailbox_t mbox = NULL; | ||
43 | int ret; | ||
44 | size_t i; | ||
45 | size_t count = 0; | ||
46 | char *mailbox_name = NULL; | ||
47 | |||
48 | /* have an argument */ | ||
49 | if (argc > 1) | ||
50 | mailbox_name = argv[1]; | ||
51 | |||
52 | /* Registration. */ | ||
53 | { | ||
54 | list_t bookie; | ||
55 | registrar_get_list (&bookie); | ||
56 | list_append (bookie, mbox_record); | ||
57 | list_append (bookie, path_record); | ||
58 | list_append (bookie, pop_record); | ||
59 | list_append (bookie, imap_record); | ||
60 | } | ||
61 | |||
62 | if ( ( ret = mailbox_create_default (&mbox, mailbox_name) ) != 0) { | ||
63 | fprintf (stderr, "could not create - %s\n", strerror(ret)); | ||
64 | exit (2); | ||
65 | } | ||
66 | |||
67 | /* Debuging Trace. */ | ||
68 | if ( 0 ) { | ||
69 | mu_debug_t debug; | ||
70 | mailbox_get_debug (mbox, &debug); | ||
71 | mu_debug_set_level (debug, MU_DEBUG_TRACE|MU_DEBUG_PROT); | ||
72 | } | ||
73 | |||
74 | /* Open the mailbox for reading only. */ | ||
75 | if ( ( ret = mailbox_open (mbox, MU_STREAM_RDWR) ) != 0) { | ||
76 | fprintf (stderr, "could not open - %s\n", strerror(ret)); | ||
77 | exit (2); | ||
78 | } | ||
79 | |||
80 | /* Iterator through the entire message set. */ | ||
81 | mailbox_messages_count (mbox, &count); | ||
82 | for(i = 1; i <= count; ++i) { | ||
83 | message_t msg; | ||
84 | header_t hdr; | ||
85 | size_t nparts; | ||
86 | size_t msize; | ||
87 | |||
88 | if (( ret = mailbox_get_message (mbox, i, &msg) ) != 0) { | ||
89 | fprintf (stderr, "mailbox_get_message - %s\n", strerror(ret)); | ||
90 | exit(2); | ||
91 | } | ||
92 | if (( ret = message_size (msg, &msize) ) != 0) { | ||
93 | fprintf (stderr, "message_size - %s\n", strerror(ret)); | ||
94 | exit(2); | ||
95 | } | ||
96 | if (( ret = message_get_header (msg, &hdr) ) != 0) { | ||
97 | fprintf (stderr, "message_get_header - %s\n", strerror(ret)); | ||
98 | exit(2); | ||
99 | } | ||
100 | header_get_value (hdr, MU_HEADER_FROM, from, sizeof (from), NULL); | ||
101 | header_get_value (hdr, MU_HEADER_SUBJECT, subject, sizeof (subject), NULL); | ||
102 | printf("From: %s\tSubject: %s\n", from, subject); | ||
103 | if ( ( ret = message_get_num_parts(msg, &nparts) ) != 0 ) { | ||
104 | fprintf (stderr, "message_get_num_parts - %s\n", strerror(ret)); | ||
105 | exit(2); | ||
106 | } | ||
107 | printf("-- Number of parts in message - %d\n",nparts); | ||
108 | printf("-- Total message size - %d\n",msize); | ||
109 | message_display_parts(msg, "\t"); | ||
110 | } | ||
111 | mailbox_close(mbox); | ||
112 | mailbox_destroy(&mbox); | ||
113 | return 0; | ||
114 | } | ||
115 | |||
116 | char buf[2048]; | ||
117 | |||
118 | void | ||
119 | message_display_parts(message_t msg, char *indent) | ||
120 | { | ||
121 | int ret, j; | ||
122 | size_t msize, nparts, nsubparts; | ||
123 | message_t part; | ||
124 | header_t hdr; | ||
125 | char type[256]; | ||
126 | char encoding[256]; | ||
127 | stream_t str; | ||
128 | body_t body; | ||
129 | int offset, ismulti; | ||
130 | size_t nbytes; | ||
131 | |||
132 | /* How many part those the message has? */ | ||
133 | if ( ( ret = message_get_num_parts(msg, &nparts) ) != 0 ) { | ||
134 | fprintf (stderr, "message_get_num_parts - %s\n", strerror(ret)); | ||
135 | exit(2); | ||
136 | } | ||
137 | |||
138 | /* Iterate through all the parts. | ||
139 | Treat type "message/rfc822" differently, since it is a message of is own | ||
140 | that can have other subparts(recursive). */ | ||
141 | for( j = 1; j <= nparts; j++) { | ||
142 | if (( ret = message_get_part(msg, j, &part ) ) != 0 ) { | ||
143 | fprintf (stderr, "mime_get_part - %s\n", strerror(ret)); | ||
144 | exit(2); | ||
145 | } | ||
146 | if (( ret = message_size (part, &msize) ) != 0) { | ||
147 | fprintf (stderr, "message_size - %s\n", strerror(ret)); | ||
148 | exit(2); | ||
149 | } | ||
150 | if (( ret = message_get_header (part, &hdr) ) != 0) { | ||
151 | fprintf (stderr, "message_get_header - %s\n", strerror(ret)); | ||
152 | exit(2); | ||
153 | } | ||
154 | header_get_value (hdr, MU_HEADER_CONTENT_TYPE, type, sizeof (type), NULL); | ||
155 | printf("%sType of part %d = %s\n", indent, j, type); | ||
156 | printf("%sMessage part size - %d\n",indent, msize); | ||
157 | encoding[0] = '\0'; | ||
158 | header_get_value (hdr, MU_HEADER_CONTENT_TRANSFER_ENCODING, encoding, sizeof (encoding), NULL); | ||
159 | ismulti = 0; | ||
160 | if ( (type[0] && strncasecmp( type, "message/rfc822", strlen(type)) == 0 ) || | ||
161 | ( message_is_multipart (part, &ismulti) == 0 && ismulti ) ) { | ||
162 | char tmp[10]; | ||
163 | |||
164 | if ( !ismulti ) { | ||
165 | ret = message_unencapsulate(part, &part, NULL); | ||
166 | if (ret != 0) | ||
167 | fprintf (stderr, "message_unencapsulate - %s\n", strerror(ret)); | ||
168 | break; | ||
169 | } | ||
170 | if (( ret = message_get_header (part, &hdr) ) != 0) { | ||
171 | fprintf (stderr, "message_get_header - %s\n", strerror(ret)); | ||
172 | exit(2); | ||
173 | } | ||
174 | header_get_value (hdr, MU_HEADER_FROM, from, sizeof (from), NULL); | ||
175 | header_get_value (hdr, MU_HEADER_SUBJECT, subject, sizeof (subject), NULL); | ||
176 | printf("%sEncapsulated message : %s\t%s\n", indent, from, subject); | ||
177 | printf("%s-------------------------------------------------------------------\n", indent); | ||
178 | if ( ( ret = message_get_num_parts(part, &nsubparts) ) != 0 ) { | ||
179 | fprintf (stderr, "mime_get_num_parts - %s\n", strerror(ret)); | ||
180 | exit(2); | ||
181 | } | ||
182 | strcpy(tmp, indent); | ||
183 | strcat(tmp,"\t"); | ||
184 | message_display_parts(part, tmp); | ||
185 | message_destroy (&part, NULL); | ||
186 | } | ||
187 | else if ( type[0] == '\0' || (strncasecmp( type, "text/plain", strlen("text/plain")) == 0) || (strncasecmp( type, "text/html", strlen("text/html")) == 0)) { | ||
188 | printf("%sText Message\n",indent); | ||
189 | printf("%s-------------------------------------------------------------------\n", indent); | ||
190 | message_get_body(part, &body); | ||
191 | body_get_stream(body, &str); | ||
192 | filter_create(&str, str, encoding, 0, 0); | ||
193 | offset = 0; | ||
194 | while( stream_readline( str, buf, sizeof(buf), offset, &nbytes ) == 0 && nbytes ) { | ||
195 | printf("%s%s", indent, buf); | ||
196 | offset += nbytes; | ||
197 | } | ||
198 | stream_destroy(&str, NULL); | ||
199 | } | ||
200 | else { | ||
201 | #if 1 | ||
202 | |||
203 | /* Save the attachements. */ | ||
204 | char *fname; | ||
205 | message_attachment_filename( part, &fname); | ||
206 | if ( fname == NULL ) { | ||
207 | char buffer[PATH_MAX+1]; | ||
208 | fname = tempnam(getcwd(buffer, PATH_MAX), "msg-" ); | ||
209 | } | ||
210 | printf("%sAttachment - saving [%s]\n",indent, fname); | ||
211 | printf("%s-------------------------------------------------------------------\n", indent); | ||
212 | /* FIXME: Somethings is not quite correct with this function. | ||
213 | Please fix. */ | ||
214 | message_save_attachment(part, fname, NULL); | ||
215 | free(fname); | ||
216 | #endif | ||
217 | } | ||
218 | printf("\n%s End -------------------------------------------------------------------\n", indent); | ||
219 | } | ||
220 | } |
-
Please register or sign in to post a comment