gnu indented
Showing
2 changed files
with
374 additions
and
238 deletions
... | @@ -37,7 +37,7 @@ char from[256]; | ... | @@ -37,7 +37,7 @@ char from[256]; |
37 | char subject[256]; | 37 | char subject[256]; |
38 | 38 | ||
39 | int | 39 | int |
40 | main(int argc, char **argv) | 40 | main (int argc, char **argv) |
41 | { | 41 | { |
42 | mailbox_t mbox = NULL; | 42 | mailbox_t mbox = NULL; |
43 | int ret; | 43 | int ret; |
... | @@ -59,64 +59,73 @@ main(int argc, char **argv) | ... | @@ -59,64 +59,73 @@ main(int argc, char **argv) |
59 | list_append (bookie, imap_record); | 59 | list_append (bookie, imap_record); |
60 | } | 60 | } |
61 | 61 | ||
62 | if ( ( ret = mailbox_create_default (&mbox, mailbox_name) ) != 0) { | 62 | if ((ret = mailbox_create_default (&mbox, mailbox_name)) != 0) |
63 | fprintf (stderr, "could not create - %s\n", strerror(ret)); | 63 | { |
64 | fprintf (stderr, "could not create - %s\n", strerror (ret)); | ||
64 | exit (2); | 65 | exit (2); |
65 | } | 66 | } |
66 | 67 | ||
67 | /* Debuging Trace. */ | 68 | /* Debuging Trace. */ |
68 | if ( 0 ) { | 69 | if (0) |
70 | { | ||
69 | mu_debug_t debug; | 71 | mu_debug_t debug; |
70 | mailbox_get_debug (mbox, &debug); | 72 | mailbox_get_debug (mbox, &debug); |
71 | mu_debug_set_level (debug, MU_DEBUG_TRACE|MU_DEBUG_PROT); | 73 | mu_debug_set_level (debug, MU_DEBUG_TRACE | MU_DEBUG_PROT); |
72 | } | 74 | } |
73 | 75 | ||
74 | /* Open the mailbox for reading only. */ | 76 | /* Open the mailbox for reading only. */ |
75 | if ( ( ret = mailbox_open (mbox, MU_STREAM_RDWR) ) != 0) { | 77 | if ((ret = mailbox_open (mbox, MU_STREAM_RDWR)) != 0) |
76 | fprintf (stderr, "could not open - %s\n", strerror(ret)); | 78 | { |
79 | fprintf (stderr, "could not open - %s\n", strerror (ret)); | ||
77 | exit (2); | 80 | exit (2); |
78 | } | 81 | } |
79 | 82 | ||
80 | /* Iterator through the entire message set. */ | 83 | /* Iterator through the entire message set. */ |
81 | mailbox_messages_count (mbox, &count); | 84 | mailbox_messages_count (mbox, &count); |
82 | for(i = 1; i <= count; ++i) { | 85 | for (i = 1; i <= count; ++i) |
86 | { | ||
83 | message_t msg; | 87 | message_t msg; |
84 | header_t hdr; | 88 | header_t hdr; |
85 | size_t nparts; | 89 | size_t nparts; |
86 | size_t msize; | 90 | size_t msize; |
87 | 91 | ||
88 | if (( ret = mailbox_get_message (mbox, i, &msg) ) != 0) { | 92 | if ((ret = mailbox_get_message (mbox, i, &msg)) != 0) |
89 | fprintf (stderr, "mailbox_get_message - %s\n", strerror(ret)); | 93 | { |
90 | exit(2); | 94 | fprintf (stderr, "mailbox_get_message - %s\n", strerror (ret)); |
95 | exit (2); | ||
91 | } | 96 | } |
92 | if (( ret = message_size (msg, &msize) ) != 0) { | 97 | if ((ret = message_size (msg, &msize)) != 0) |
93 | fprintf (stderr, "message_size - %s\n", strerror(ret)); | 98 | { |
94 | exit(2); | 99 | fprintf (stderr, "message_size - %s\n", strerror (ret)); |
100 | exit (2); | ||
95 | } | 101 | } |
96 | if (( ret = message_get_header (msg, &hdr) ) != 0) { | 102 | if ((ret = message_get_header (msg, &hdr)) != 0) |
97 | fprintf (stderr, "message_get_header - %s\n", strerror(ret)); | 103 | { |
98 | exit(2); | 104 | fprintf (stderr, "message_get_header - %s\n", strerror (ret)); |
105 | exit (2); | ||
99 | } | 106 | } |
100 | header_get_value (hdr, MU_HEADER_FROM, from, sizeof (from), NULL); | 107 | header_get_value (hdr, MU_HEADER_FROM, from, sizeof (from), NULL); |
101 | header_get_value (hdr, MU_HEADER_SUBJECT, subject, sizeof (subject), NULL); | 108 | header_get_value (hdr, MU_HEADER_SUBJECT, subject, sizeof (subject), |
102 | printf("From: %s\tSubject: %s\n", from, subject); | 109 | NULL); |
103 | if ( ( ret = message_get_num_parts(msg, &nparts) ) != 0 ) { | 110 | printf ("From: %s\tSubject: %s\n", from, subject); |
104 | fprintf (stderr, "message_get_num_parts - %s\n", strerror(ret)); | 111 | if ((ret = message_get_num_parts (msg, &nparts)) != 0) |
105 | exit(2); | 112 | { |
106 | } | 113 | fprintf (stderr, "message_get_num_parts - %s\n", strerror (ret)); |
107 | printf("-- Number of parts in message - %d\n",nparts); | 114 | exit (2); |
108 | printf("-- Total message size - %d\n",msize); | 115 | } |
109 | message_display_parts(msg, "\t"); | 116 | printf ("-- Number of parts in message - %d\n", nparts); |
110 | } | 117 | printf ("-- Total message size - %d\n", msize); |
111 | mailbox_close(mbox); | 118 | message_display_parts (msg, "\t"); |
112 | mailbox_destroy(&mbox); | 119 | } |
120 | mailbox_close (mbox); | ||
121 | mailbox_destroy (&mbox); | ||
113 | return 0; | 122 | return 0; |
114 | } | 123 | } |
115 | 124 | ||
116 | char buf[2048]; | 125 | char buf[2048]; |
117 | 126 | ||
118 | void | 127 | void |
119 | message_display_parts(message_t msg, char *indent) | 128 | message_display_parts (message_t msg, char *indent) |
120 | { | 129 | { |
121 | int ret, j; | 130 | int ret, j; |
122 | size_t msize, nparts, nsubparts; | 131 | size_t msize, nparts, nsubparts; |
... | @@ -130,91 +139,123 @@ message_display_parts(message_t msg, char *indent) | ... | @@ -130,91 +139,123 @@ message_display_parts(message_t msg, char *indent) |
130 | size_t nbytes; | 139 | size_t nbytes; |
131 | 140 | ||
132 | /* How many part those the message has? */ | 141 | /* How many part those the message has? */ |
133 | if ( ( ret = message_get_num_parts(msg, &nparts) ) != 0 ) { | 142 | if ((ret = message_get_num_parts (msg, &nparts)) != 0) |
134 | fprintf (stderr, "message_get_num_parts - %s\n", strerror(ret)); | 143 | { |
135 | exit(2); | 144 | fprintf (stderr, "message_get_num_parts - %s\n", strerror (ret)); |
145 | exit (2); | ||
136 | } | 146 | } |
137 | 147 | ||
138 | /* Iterate through all the parts. | 148 | /* Iterate through all the parts. |
139 | Treat type "message/rfc822" differently, since it is a message of is own | 149 | Treat type "message/rfc822" differently, since it is a message of is own |
140 | that can have other subparts(recursive). */ | 150 | that can have other subparts(recursive). */ |
141 | for( j = 1; j <= nparts; j++) { | 151 | for (j = 1; j <= nparts; j++) |
142 | if (( ret = message_get_part(msg, j, &part ) ) != 0 ) { | 152 | { |
143 | fprintf (stderr, "mime_get_part - %s\n", strerror(ret)); | 153 | if ((ret = message_get_part (msg, j, &part)) != 0) |
144 | exit(2); | 154 | { |
145 | } | 155 | fprintf (stderr, "mime_get_part - %s\n", strerror (ret)); |
146 | if (( ret = message_size (part, &msize) ) != 0) { | 156 | exit (2); |
147 | fprintf (stderr, "message_size - %s\n", strerror(ret)); | 157 | } |
148 | exit(2); | 158 | if ((ret = message_size (part, &msize)) != 0) |
149 | } | 159 | { |
150 | if (( ret = message_get_header (part, &hdr) ) != 0) { | 160 | fprintf (stderr, "message_size - %s\n", strerror (ret)); |
151 | fprintf (stderr, "message_get_header - %s\n", strerror(ret)); | 161 | exit (2); |
152 | exit(2); | 162 | } |
153 | } | 163 | if ((ret = message_get_header (part, &hdr)) != 0) |
154 | header_get_value (hdr, MU_HEADER_CONTENT_TYPE, type, sizeof (type), NULL); | 164 | { |
155 | printf("%sType of part %d = %s\n", indent, j, type); | 165 | fprintf (stderr, "message_get_header - %s\n", strerror (ret)); |
156 | printf("%sMessage part size - %d\n",indent, msize); | 166 | exit (2); |
167 | } | ||
168 | header_get_value (hdr, MU_HEADER_CONTENT_TYPE, type, sizeof (type), | ||
169 | NULL); | ||
170 | printf ("%sType of part %d = %s\n", indent, j, type); | ||
171 | printf ("%sMessage part size - %d\n", indent, msize); | ||
157 | encoding[0] = '\0'; | 172 | encoding[0] = '\0'; |
158 | header_get_value (hdr, MU_HEADER_CONTENT_TRANSFER_ENCODING, encoding, sizeof (encoding), NULL); | 173 | header_get_value (hdr, MU_HEADER_CONTENT_TRANSFER_ENCODING, encoding, |
174 | sizeof (encoding), NULL); | ||
159 | ismulti = 0; | 175 | ismulti = 0; |
160 | if ( (type[0] && strncasecmp( type, "message/rfc822", strlen(type)) == 0 ) || | 176 | if ((type[0] |
161 | ( message_is_multipart (part, &ismulti) == 0 && ismulti ) ) { | 177 | && strncasecmp (type, "message/rfc822", strlen (type)) == 0) |
178 | || (message_is_multipart (part, &ismulti) == 0 && ismulti)) | ||
179 | { | ||
162 | char tmp[10]; | 180 | char tmp[10]; |
163 | 181 | ||
164 | if ( !ismulti ) { | 182 | if (!ismulti) |
165 | ret = message_unencapsulate(part, &part, NULL); | 183 | { |
184 | ret = message_unencapsulate (part, &part, NULL); | ||
166 | if (ret != 0) | 185 | if (ret != 0) |
167 | fprintf (stderr, "message_unencapsulate - %s\n", strerror(ret)); | 186 | fprintf (stderr, "message_unencapsulate - %s\n", |
187 | strerror (ret)); | ||
168 | break; | 188 | break; |
169 | } | 189 | } |
170 | if (( ret = message_get_header (part, &hdr) ) != 0) { | 190 | if ((ret = message_get_header (part, &hdr)) != 0) |
171 | fprintf (stderr, "message_get_header - %s\n", strerror(ret)); | 191 | { |
172 | exit(2); | 192 | fprintf (stderr, "message_get_header - %s\n", strerror (ret)); |
193 | exit (2); | ||
173 | } | 194 | } |
174 | header_get_value (hdr, MU_HEADER_FROM, from, sizeof (from), NULL); | 195 | header_get_value (hdr, MU_HEADER_FROM, from, sizeof (from), NULL); |
175 | header_get_value (hdr, MU_HEADER_SUBJECT, subject, sizeof (subject), NULL); | 196 | header_get_value (hdr, MU_HEADER_SUBJECT, subject, sizeof (subject), |
176 | printf("%sEncapsulated message : %s\t%s\n", indent, from, subject); | 197 | NULL); |
177 | printf("%s-------------------------------------------------------------------\n", indent); | 198 | printf ("%sEncapsulated message : %s\t%s\n", indent, from, subject); |
178 | if ( ( ret = message_get_num_parts(part, &nsubparts) ) != 0 ) { | 199 | printf |
179 | fprintf (stderr, "mime_get_num_parts - %s\n", strerror(ret)); | 200 | ("%s-------------------------------------------------------------------\n", |
180 | exit(2); | 201 | indent); |
181 | } | 202 | if ((ret = message_get_num_parts (part, &nsubparts)) != 0) |
182 | strcpy(tmp, indent); | 203 | { |
183 | strcat(tmp,"\t"); | 204 | fprintf (stderr, "mime_get_num_parts - %s\n", strerror (ret)); |
184 | message_display_parts(part, tmp); | 205 | exit (2); |
206 | } | ||
207 | strcpy (tmp, indent); | ||
208 | strcat (tmp, "\t"); | ||
209 | message_display_parts (part, tmp); | ||
185 | message_destroy (&part, NULL); | 210 | message_destroy (&part, NULL); |
186 | } | 211 | } |
187 | else if ( type[0] == '\0' || (strncasecmp( type, "text/plain", strlen("text/plain")) == 0) || (strncasecmp( type, "text/html", strlen("text/html")) == 0)) { | 212 | else if (type[0] == '\0' |
188 | printf("%sText Message\n",indent); | 213 | || (strncasecmp (type, "text/plain", strlen ("text/plain")) == |
189 | printf("%s-------------------------------------------------------------------\n", indent); | 214 | 0) |
190 | message_get_body(part, &body); | 215 | || (strncasecmp (type, "text/html", strlen ("text/html")) == |
191 | body_get_stream(body, &str); | 216 | 0)) |
192 | filter_create(&str, str, encoding, 0, 0); | 217 | { |
218 | printf ("%sText Message\n", indent); | ||
219 | printf | ||
220 | ("%s-------------------------------------------------------------------\n", | ||
221 | indent); | ||
222 | message_get_body (part, &body); | ||
223 | body_get_stream (body, &str); | ||
224 | filter_create (&str, str, encoding, 0, 0); | ||
193 | offset = 0; | 225 | offset = 0; |
194 | while( stream_readline( str, buf, sizeof(buf), offset, &nbytes ) == 0 && nbytes ) { | 226 | while (stream_readline (str, buf, sizeof (buf), offset, &nbytes) == |
195 | printf("%s%s", indent, buf); | 227 | 0 && nbytes) |
228 | { | ||
229 | printf ("%s%s", indent, buf); | ||
196 | offset += nbytes; | 230 | offset += nbytes; |
197 | } | 231 | } |
198 | stream_destroy(&str, NULL); | 232 | stream_destroy (&str, NULL); |
199 | } | 233 | } |
200 | else { | 234 | else |
235 | { | ||
201 | #if 1 | 236 | #if 1 |
202 | 237 | ||
203 | /* Save the attachements. */ | 238 | /* Save the attachements. */ |
204 | char *fname; | 239 | char *fname; |
205 | message_attachment_filename( part, &fname); | 240 | message_attachment_filename (part, &fname); |
206 | if ( fname == NULL ) { | 241 | if (fname == NULL) |
207 | char buffer[PATH_MAX+1]; | 242 | { |
208 | fname = tempnam(getcwd(buffer, PATH_MAX), "msg-" ); | 243 | char buffer[PATH_MAX + 1]; |
244 | fname = tempnam (getcwd (buffer, PATH_MAX), "msg-"); | ||
209 | } | 245 | } |
210 | printf("%sAttachment - saving [%s]\n",indent, fname); | 246 | printf ("%sAttachment - saving [%s]\n", indent, fname); |
211 | printf("%s-------------------------------------------------------------------\n", indent); | 247 | printf |
248 | ("%s-------------------------------------------------------------------\n", | ||
249 | indent); | ||
212 | /* FIXME: Somethings is not quite correct with this function. | 250 | /* FIXME: Somethings is not quite correct with this function. |
213 | Please fix. */ | 251 | Please fix. */ |
214 | message_save_attachment(part, fname, NULL); | 252 | message_save_attachment (part, fname, NULL); |
215 | free(fname); | 253 | free (fname); |
216 | #endif | 254 | #endif |
217 | } | 255 | } |
218 | printf("\n%s End -------------------------------------------------------------------\n", indent); | 256 | printf |
257 | ("\n%s End -------------------------------------------------------------------\n", | ||
258 | indent); | ||
219 | } | 259 | } |
220 | } | 260 | } |
261 | ... | ... |
... | @@ -48,7 +48,8 @@ | ... | @@ -48,7 +48,8 @@ |
48 | /* FIXME: this should be in a public header. */ | 48 | /* FIXME: this should be in a public header. */ |
49 | extern int message_attachment_filename __P ((message_t, const char **filename)); | 49 | extern int message_attachment_filename __P ((message_t, const char **filename)); |
50 | 50 | ||
51 | struct _msg_info { | 51 | struct _msg_info |
52 | { | ||
52 | char *buf; | 53 | char *buf; |
53 | size_t nbytes; | 54 | size_t nbytes; |
54 | char *header_buf; | 55 | char *header_buf; |
... | @@ -64,7 +65,9 @@ struct _msg_info { | ... | @@ -64,7 +65,9 @@ struct _msg_info { |
64 | 65 | ||
65 | #define MSG_HDR "Content-Type: %s; name=%s\nContent-Transfer-Encoding: %s\nContent-Disposition: attachment; filename=%s\n\n" | 66 | #define MSG_HDR "Content-Type: %s; name=%s\nContent-Transfer-Encoding: %s\nContent-Disposition: attachment; filename=%s\n\n" |
66 | 67 | ||
67 | int message_create_attachment(const char *content_type, const char *encoding, const char *filename, message_t *newmsg) | 68 | int |
69 | message_create_attachment (const char *content_type, const char *encoding, | ||
70 | const char *filename, message_t * newmsg) | ||
68 | { | 71 | { |
69 | header_t hdr; | 72 | header_t hdr; |
70 | body_t body; | 73 | body_t body; |
... | @@ -72,88 +75,112 @@ int message_create_attachment(const char *content_type, const char *encoding, co | ... | @@ -72,88 +75,112 @@ int message_create_attachment(const char *content_type, const char *encoding, co |
72 | char *header, *name = NULL, *fname = NULL; | 75 | char *header, *name = NULL, *fname = NULL; |
73 | int ret; | 76 | int ret; |
74 | 77 | ||
75 | if ( filename == NULL || newmsg == NULL ) | 78 | if (filename == NULL || newmsg == NULL) |
76 | return EINVAL; | 79 | return EINVAL; |
77 | 80 | ||
78 | if ( ( ret = message_create(newmsg, NULL) ) == 0 ) { | 81 | if ((ret = message_create (newmsg, NULL)) == 0) |
79 | if ( content_type == NULL ) | 82 | { |
83 | if (content_type == NULL) | ||
80 | content_type = "text/plain"; | 84 | content_type = "text/plain"; |
81 | if ( encoding == NULL ) | 85 | if (encoding == NULL) |
82 | encoding = "7bit"; | 86 | encoding = "7bit"; |
83 | if ( ( fname = strdup(filename) ) != NULL ) { | 87 | if ((fname = strdup (filename)) != NULL) |
88 | { | ||
84 | name = strrchr (fname, '/'); | 89 | name = strrchr (fname, '/'); |
85 | if (name) | 90 | if (name) |
86 | name++; | 91 | name++; |
87 | else | 92 | else |
88 | name = fname; | 93 | name = fname; |
89 | if ( ( header = alloca(strlen(MSG_HDR) + strlen(content_type) + strlen(name) * 2 + strlen(encoding) + 1) ) == NULL ) | 94 | if ((header = |
95 | alloca (strlen (MSG_HDR) + strlen (content_type) + | ||
96 | strlen (name) * 2 + strlen (encoding) + 1)) == NULL) | ||
90 | ret = ENOMEM; | 97 | ret = ENOMEM; |
91 | else { | 98 | else |
92 | sprintf(header, MSG_HDR, content_type, name, encoding, name); | 99 | { |
93 | if ( ( ret = header_create( &hdr, header, strlen(header), *newmsg ) ) == 0 ) { | 100 | sprintf (header, MSG_HDR, content_type, name, encoding, name); |
94 | message_get_body(*newmsg, &body); | 101 | if ((ret = |
95 | if ( ( ret = file_stream_create(&fstream, filename, MU_STREAM_READ) ) == 0 ) { | 102 | header_create (&hdr, header, strlen (header), |
96 | if ( ( ret = stream_open(fstream) ) == 0 ) { | 103 | *newmsg)) == 0) |
97 | if ( ( ret = filter_create(&tstream, fstream, encoding, MU_FILTER_ENCODE, MU_STREAM_READ) ) == 0 ) { | 104 | { |
98 | body_set_stream(body, tstream, *newmsg); | 105 | message_get_body (*newmsg, &body); |
99 | message_set_header(*newmsg, hdr, NULL); | 106 | if ((ret = |
100 | } | 107 | file_stream_create (&fstream, filename, |
101 | } | 108 | MU_STREAM_READ)) == 0) |
102 | } | 109 | { |
103 | } | 110 | if ((ret = stream_open (fstream)) == 0) |
104 | } | 111 | { |
105 | } | 112 | if ((ret = |
106 | } | 113 | filter_create (&tstream, fstream, encoding, |
107 | if ( ret ) { | 114 | MU_FILTER_ENCODE, |
108 | if ( *newmsg ) | 115 | MU_STREAM_READ)) == 0) |
109 | message_destroy(newmsg, NULL); | 116 | { |
110 | if ( hdr ) | 117 | body_set_stream (body, tstream, *newmsg); |
111 | header_destroy(&hdr, NULL); | 118 | message_set_header (*newmsg, hdr, NULL); |
112 | if ( fstream ) | 119 | } |
113 | stream_destroy(&fstream, NULL); | 120 | } |
114 | if ( fname ) | 121 | } |
115 | free(fname); | 122 | } |
123 | } | ||
124 | } | ||
125 | } | ||
126 | if (ret) | ||
127 | { | ||
128 | if (*newmsg) | ||
129 | message_destroy (newmsg, NULL); | ||
130 | if (hdr) | ||
131 | header_destroy (&hdr, NULL); | ||
132 | if (fstream) | ||
133 | stream_destroy (&fstream, NULL); | ||
134 | if (fname) | ||
135 | free (fname); | ||
116 | } | 136 | } |
117 | return ret; | 137 | return ret; |
118 | } | 138 | } |
119 | 139 | ||
120 | 140 | ||
121 | static int _attachment_setup(struct _msg_info **info, message_t msg, stream_t *stream, void **data) | 141 | static int |
142 | _attachment_setup (struct _msg_info **info, message_t msg, stream_t * stream, | ||
143 | void **data) | ||
122 | { | 144 | { |
123 | int sfl, ret; | 145 | int sfl, ret; |
124 | body_t body; | 146 | body_t body; |
125 | 147 | ||
126 | if ( ( ret = message_get_body(msg, &body) ) != 0 || | 148 | if ((ret = message_get_body (msg, &body)) != 0 || |
127 | ( ret = body_get_stream(body, stream) ) != 0 ) | 149 | (ret = body_get_stream (body, stream)) != 0) |
128 | return ret; | 150 | return ret; |
129 | stream_get_flags(*stream, &sfl); | 151 | stream_get_flags (*stream, &sfl); |
130 | if ( data == NULL && (sfl & MU_STREAM_NONBLOCK) ) | 152 | if (data == NULL && (sfl & MU_STREAM_NONBLOCK)) |
131 | return EINVAL; | 153 | return EINVAL; |
132 | if ( data ) | 154 | if (data) |
133 | *info = *data; | 155 | *info = *data; |
134 | if ( *info == NULL ) { | 156 | if (*info == NULL) |
135 | if ( ( *info = calloc(1, sizeof(struct _msg_info)) ) == NULL ) | 157 | { |
158 | if ((*info = calloc (1, sizeof (struct _msg_info))) == NULL) | ||
136 | return ENOMEM; | 159 | return ENOMEM; |
137 | } | 160 | } |
138 | if ( ( (*info)->buf = malloc(BUF_SIZE) ) == NULL ) { | 161 | if (((*info)->buf = malloc (BUF_SIZE)) == NULL) |
139 | free(*info); | 162 | { |
163 | free (*info); | ||
140 | return ENOMEM; | 164 | return ENOMEM; |
141 | } | 165 | } |
142 | return 0; | 166 | return 0; |
143 | } | 167 | } |
144 | 168 | ||
145 | static void _attachment_free(struct _msg_info *info, int free_message) { | 169 | static void |
146 | if ( info->buf ) | 170 | _attachment_free (struct _msg_info *info, int free_message) |
147 | free(info->buf); | 171 | { |
148 | if ( info->header_buf ) | 172 | if (info->buf) |
149 | free(info->header_buf); | 173 | free (info->buf); |
150 | if ( free_message ) { | 174 | if (info->header_buf) |
151 | if ( info->msg ) | 175 | free (info->header_buf); |
152 | message_destroy(&(info->msg), NULL); | 176 | if (free_message) |
153 | else if ( info->hdr ) | 177 | { |
154 | header_destroy(&(info->hdr), NULL); | 178 | if (info->msg) |
155 | } | 179 | message_destroy (&(info->msg), NULL); |
156 | free(info); | 180 | else if (info->hdr) |
181 | header_destroy (&(info->hdr), NULL); | ||
182 | } | ||
183 | free (info); | ||
157 | } | 184 | } |
158 | 185 | ||
159 | #define _ISSPECIAL(c) ( \ | 186 | #define _ISSPECIAL(c) ( \ |
... | @@ -162,32 +189,36 @@ static void _attachment_free(struct _msg_info *info, int free_message) { | ... | @@ -162,32 +189,36 @@ static void _attachment_free(struct _msg_info *info, int free_message) { |
162 | || ((c) == '\\') || ((c) == '.') || ((c) == '[') \ | 189 | || ((c) == '\\') || ((c) == '.') || ((c) == '[') \ |
163 | || ((c) == ']') ) | 190 | || ((c) == ']') ) |
164 | 191 | ||
165 | static char *_header_get_param(char *field_body, const char *param, size_t *len) | 192 | static char * |
193 | _header_get_param (char *field_body, const char *param, size_t * len) | ||
166 | { | 194 | { |
167 | char *str, *p, *v, *e; | 195 | char *str, *p, *v, *e; |
168 | int quoted = 0, was_quoted = 0; | 196 | int quoted = 0, was_quoted = 0; |
169 | 197 | ||
170 | if ( len == NULL || ( str = field_body ) == NULL ) | 198 | if (len == NULL || (str = field_body) == NULL) |
171 | return NULL; | 199 | return NULL; |
172 | 200 | ||
173 | p = strchr(str, ';' ); | 201 | p = strchr (str, ';'); |
174 | while ( p ) { | 202 | while (p) |
203 | { | ||
175 | p++; | 204 | p++; |
176 | while( isspace((unsigned)*p) ) /* walk upto start of param */ | 205 | while (isspace ((unsigned) *p)) /* walk upto start of param */ |
177 | p++; | 206 | p++; |
178 | if ( ( v = strchr(p, '=' ) ) == NULL ) | 207 | if ((v = strchr (p, '=')) == NULL) |
179 | break; | 208 | break; |
180 | *len = 0; | 209 | *len = 0; |
181 | v = e = v + 1; | 210 | v = e = v + 1; |
182 | while ( *e && (quoted || ( !_ISSPECIAL(*e) && !isspace((unsigned)*e) ) ) ) { /* skip pass value and calc len */ | 211 | while (*e && (quoted || (!_ISSPECIAL (*e) && !isspace ((unsigned) *e)))) |
183 | if ( *e == '\"' ) | 212 | { /* skip pass value and calc len */ |
213 | if (*e == '\"') | ||
184 | quoted = ~quoted, was_quoted = 1; | 214 | quoted = ~quoted, was_quoted = 1; |
185 | else | 215 | else |
186 | (*len)++; | 216 | (*len)++; |
187 | e++; | 217 | e++; |
188 | } | 218 | } |
189 | if ( strncasecmp(p, param, strlen(param)) ) { /* no match jump to next */ | 219 | if (strncasecmp (p, param, strlen (param))) |
190 | p = strchr(e, ';' ); | 220 | { /* no match jump to next */ |
221 | p = strchr (e, ';'); | ||
191 | continue; | 222 | continue; |
192 | } | 223 | } |
193 | else | 224 | else |
... | @@ -196,44 +227,52 @@ static char *_header_get_param(char *field_body, const char *param, size_t *len) | ... | @@ -196,44 +227,52 @@ static char *_header_get_param(char *field_body, const char *param, size_t *len) |
196 | return NULL; | 227 | return NULL; |
197 | } | 228 | } |
198 | 229 | ||
199 | int message_attachment_filename(message_t msg, const char **filename) | 230 | int |
231 | message_attachment_filename (message_t msg, const char **filename) | ||
200 | { | 232 | { |
201 | char *pTmp, *fname = NULL; | 233 | char *pTmp, *fname = NULL; |
202 | header_t hdr; | 234 | header_t hdr; |
203 | int ret = EINVAL; | 235 | int ret = EINVAL; |
204 | size_t size = 0; | 236 | size_t size = 0; |
205 | 237 | ||
206 | if ( filename != NULL && ( ret = message_get_header(msg, &hdr) ) == 0 ) { | 238 | if (filename != NULL && (ret = message_get_header (msg, &hdr)) == 0) |
239 | { | ||
207 | *filename = NULL; | 240 | *filename = NULL; |
208 | header_get_value(hdr, "Content-Disposition", NULL, 0, &size); | 241 | header_get_value (hdr, "Content-Disposition", NULL, 0, &size); |
209 | if ( size ) { | 242 | if (size) |
210 | if ( ( pTmp = alloca(size+1) ) == NULL ) | 243 | { |
244 | if ((pTmp = alloca (size + 1)) == NULL) | ||
211 | ret = ENOMEM; | 245 | ret = ENOMEM; |
212 | header_get_value(hdr, "Content-Disposition", pTmp, size+1, 0); | 246 | header_get_value (hdr, "Content-Disposition", pTmp, size + 1, 0); |
213 | if ( strstr( pTmp, "attachment" ) != NULL ) | 247 | if (strstr (pTmp, "attachment") != NULL) |
214 | fname = _header_get_param(pTmp, "filename", &size); | 248 | fname = _header_get_param (pTmp, "filename", &size); |
215 | } | 249 | } |
216 | if ( fname == NULL ) { | 250 | if (fname == NULL) |
251 | { | ||
217 | size = 0; | 252 | size = 0; |
218 | header_get_value(hdr, "Content-Type", NULL, 0, &size); | 253 | header_get_value (hdr, "Content-Type", NULL, 0, &size); |
219 | if ( size ) { | 254 | if (size) |
220 | if ( ( pTmp = alloca(size+1) ) == NULL ) | 255 | { |
256 | if ((pTmp = alloca (size + 1)) == NULL) | ||
221 | ret = ENOMEM; | 257 | ret = ENOMEM; |
222 | header_get_value(hdr, "Content-Type", pTmp, size+1, 0); | 258 | header_get_value (hdr, "Content-Type", pTmp, size + 1, 0); |
223 | fname = _header_get_param(pTmp, "name", &size); | 259 | fname = _header_get_param (pTmp, "name", &size); |
224 | } | 260 | } |
225 | } | 261 | } |
226 | if ( fname ) { | 262 | if (fname) |
263 | { | ||
227 | fname[size] = '\0'; | 264 | fname[size] = '\0'; |
228 | if ( ( *filename = strdup(fname) ) == NULL ) | 265 | if ((*filename = strdup (fname)) == NULL) |
229 | ret = ENOMEM; | 266 | ret = ENOMEM; |
230 | } else | 267 | } |
268 | else | ||
231 | ret = ENOENT; | 269 | ret = ENOENT; |
232 | } | 270 | } |
233 | return ret; | 271 | return ret; |
234 | } | 272 | } |
235 | 273 | ||
236 | int message_save_attachment(message_t msg, const char *filename, void **data) | 274 | int |
275 | message_save_attachment (message_t msg, const char *filename, void **data) | ||
237 | { | 276 | { |
238 | stream_t istream; | 277 | stream_t istream; |
239 | struct _msg_info *info = NULL; | 278 | struct _msg_info *info = NULL; |
... | @@ -244,53 +283,77 @@ int message_save_attachment(message_t msg, const char *filename, void **data) | ... | @@ -244,53 +283,77 @@ int message_save_attachment(message_t msg, const char *filename, void **data) |
244 | char *content_encoding; | 283 | char *content_encoding; |
245 | const char *fname = NULL; | 284 | const char *fname = NULL; |
246 | 285 | ||
247 | if ( msg == NULL || filename == NULL) | 286 | if (msg == NULL || filename == NULL) |
248 | return EINVAL; | 287 | return EINVAL; |
249 | 288 | ||
250 | if ( ( ret = _attachment_setup( &info, msg, &istream, data) ) != 0 ) | 289 | if ((ret = _attachment_setup (&info, msg, &istream, data)) != 0) |
251 | return ret; | 290 | return ret; |
252 | 291 | ||
253 | if ( ret == 0 && ( ret = message_get_header(msg, &hdr) ) == 0 ) { | 292 | if (ret == 0 && (ret = message_get_header (msg, &hdr)) == 0) |
254 | if ( filename == NULL ) | 293 | { |
255 | ret = message_attachment_filename(msg, &fname); | 294 | if (filename == NULL) |
295 | ret = message_attachment_filename (msg, &fname); | ||
256 | else | 296 | else |
257 | fname = filename; | 297 | fname = filename; |
258 | if ( fname && ( ret = file_stream_create(&info->fstream, fname, MU_STREAM_WRITE|MU_STREAM_CREAT) ) == 0 ) { | 298 | if (fname |
259 | if ( ( ret = stream_open(info->fstream) ) == 0 ) { | 299 | && (ret = |
260 | header_get_value(hdr, "Content-Transfer-Encoding", NULL, 0, &size); | 300 | file_stream_create (&info->fstream, fname, |
261 | if ( size ) { | 301 | MU_STREAM_WRITE | MU_STREAM_CREAT)) == 0) |
262 | if ( ( content_encoding = alloca(size+1) ) == NULL ) | 302 | { |
303 | if ((ret = stream_open (info->fstream)) == 0) | ||
304 | { | ||
305 | header_get_value (hdr, "Content-Transfer-Encoding", NULL, 0, | ||
306 | &size); | ||
307 | if (size) | ||
308 | { | ||
309 | if ((content_encoding = alloca (size + 1)) == NULL) | ||
263 | ret = ENOMEM; | 310 | ret = ENOMEM; |
264 | header_get_value(hdr, "Content-Transfer-Encoding", content_encoding, size+1, 0); | 311 | header_get_value (hdr, "Content-Transfer-Encoding", |
265 | } else | 312 | content_encoding, size + 1, 0); |
266 | content_encoding = (char *)"7bit"; | ||
267 | ret = filter_create(&info->stream, istream, content_encoding, MU_FILTER_DECODE, MU_STREAM_READ); | ||
268 | } | ||
269 | } | ||
270 | } | 313 | } |
271 | if ( info->stream && istream ) { | 314 | else |
272 | if ( info->nbytes ) | 315 | content_encoding = (char *) "7bit"; |
273 | memmove( info->buf, info->buf + (BUF_SIZE - info->nbytes), info->nbytes); | 316 | ret = |
274 | while ( (ret == 0 && info->nbytes) || ( ( ret = stream_read(info->stream, info->buf, BUF_SIZE, info->ioffset, &info->nbytes) ) == 0 && info->nbytes ) ) { | 317 | filter_create (&info->stream, istream, content_encoding, |
318 | MU_FILTER_DECODE, MU_STREAM_READ); | ||
319 | } | ||
320 | } | ||
321 | } | ||
322 | if (info->stream && istream) | ||
323 | { | ||
324 | if (info->nbytes) | ||
325 | memmove (info->buf, info->buf + (BUF_SIZE - info->nbytes), | ||
326 | info->nbytes); | ||
327 | while ((ret == 0 && info->nbytes) | ||
328 | || | ||
329 | ((ret = | ||
330 | stream_read (info->stream, info->buf, BUF_SIZE, info->ioffset, | ||
331 | &info->nbytes)) == 0 && info->nbytes)) | ||
332 | { | ||
275 | info->ioffset += info->nbytes; | 333 | info->ioffset += info->nbytes; |
276 | while( info->nbytes ) { | 334 | while (info->nbytes) |
277 | if ( ( ret = stream_write(info->fstream, info->buf, info->nbytes, info->ooffset, &nbytes ) ) != 0 ) | 335 | { |
336 | if ((ret = | ||
337 | stream_write (info->fstream, info->buf, info->nbytes, | ||
338 | info->ooffset, &nbytes)) != 0) | ||
278 | break; | 339 | break; |
279 | info->nbytes -= nbytes; | 340 | info->nbytes -= nbytes; |
280 | info->ooffset += nbytes; | 341 | info->ooffset += nbytes; |
281 | } | 342 | } |
282 | } | 343 | } |
283 | } | 344 | } |
284 | if ( ret != EAGAIN && info ) { | 345 | if (ret != EAGAIN && info) |
285 | stream_close(info->fstream); | 346 | { |
286 | stream_destroy(&info->stream, NULL); | 347 | stream_close (info->fstream); |
287 | stream_destroy(&info->fstream, NULL); | 348 | stream_destroy (&info->stream, NULL); |
288 | _attachment_free(info, ret); | 349 | stream_destroy (&info->fstream, NULL); |
350 | _attachment_free (info, ret); | ||
289 | } | 351 | } |
290 | return ret; | 352 | return ret; |
291 | } | 353 | } |
292 | 354 | ||
293 | int message_encapsulate(message_t msg, message_t *newmsg, void **data) | 355 | int |
356 | message_encapsulate (message_t msg, message_t * newmsg, void **data) | ||
294 | { | 357 | { |
295 | stream_t istream, ostream; | 358 | stream_t istream, ostream; |
296 | const char *header; | 359 | const char *header; |
... | @@ -299,26 +362,40 @@ int message_encapsulate(message_t msg, message_t *newmsg, void **data) | ... | @@ -299,26 +362,40 @@ int message_encapsulate(message_t msg, message_t *newmsg, void **data) |
299 | size_t nbytes; | 362 | size_t nbytes; |
300 | body_t body; | 363 | body_t body; |
301 | 364 | ||
302 | if ( msg == NULL || newmsg == NULL) | 365 | if (msg == NULL || newmsg == NULL) |
303 | return EINVAL; | 366 | return EINVAL; |
304 | 367 | ||
305 | if ( ( ret = _attachment_setup( &info, msg, &ostream, data) ) != 0 ) | 368 | if ((ret = _attachment_setup (&info, msg, &ostream, data)) != 0) |
306 | return ret; | 369 | return ret; |
307 | 370 | ||
308 | if ( info->msg == NULL && ( ret = message_create(&(info->msg), NULL) ) == 0 ) { | 371 | if (info->msg == NULL && (ret = message_create (&(info->msg), NULL)) == 0) |
309 | header = "Content-Type: message/rfc822\nContent-Transfer-Encoding: 7bit\n\n"; | 372 | { |
310 | if ( ( ret = header_create( &(info->hdr), header, strlen(header), msg ) ) == 0 ) | 373 | header = |
311 | ret = message_set_header(info->msg, info->hdr, NULL); | 374 | "Content-Type: message/rfc822\nContent-Transfer-Encoding: 7bit\n\n"; |
312 | } | 375 | if ((ret = |
313 | if ( ret == 0 && ( ret = message_get_stream(msg, &istream ) ) == 0 ) { | 376 | header_create (&(info->hdr), header, strlen (header), msg)) == 0) |
314 | if ( ( ret = message_get_body(info->msg, &body) ) == 0 && | 377 | ret = message_set_header (info->msg, info->hdr, NULL); |
315 | ( ret = body_get_stream(body, &ostream) ) == 0 ) { | 378 | } |
316 | if ( info->nbytes ) | 379 | if (ret == 0 && (ret = message_get_stream (msg, &istream)) == 0) |
317 | memmove( info->buf, info->buf + (BUF_SIZE - info->nbytes), info->nbytes); | 380 | { |
318 | while ( (ret == 0 && info->nbytes) || ( ( ret = stream_read(istream, info->buf, BUF_SIZE, info->ioffset, &info->nbytes) ) == 0 && info->nbytes ) ) { | 381 | if ((ret = message_get_body (info->msg, &body)) == 0 && |
382 | (ret = body_get_stream (body, &ostream)) == 0) | ||
383 | { | ||
384 | if (info->nbytes) | ||
385 | memmove (info->buf, info->buf + (BUF_SIZE - info->nbytes), | ||
386 | info->nbytes); | ||
387 | while ((ret == 0 && info->nbytes) | ||
388 | || | ||
389 | ((ret = | ||
390 | stream_read (istream, info->buf, BUF_SIZE, info->ioffset, | ||
391 | &info->nbytes)) == 0 && info->nbytes)) | ||
392 | { | ||
319 | info->ioffset += info->nbytes; | 393 | info->ioffset += info->nbytes; |
320 | while( info->nbytes ) { | 394 | while (info->nbytes) |
321 | if ( ( ret = stream_write(ostream, info->buf, info->nbytes, info->ooffset, &nbytes ) ) != 0 ) | 395 | { |
396 | if ((ret = | ||
397 | stream_write (ostream, info->buf, info->nbytes, | ||
398 | info->ooffset, &nbytes)) != 0) | ||
322 | break; | 399 | break; |
323 | info->nbytes -= nbytes; | 400 | info->nbytes -= nbytes; |
324 | info->ooffset += nbytes; | 401 | info->ooffset += nbytes; |
... | @@ -326,14 +403,15 @@ int message_encapsulate(message_t msg, message_t *newmsg, void **data) | ... | @@ -326,14 +403,15 @@ int message_encapsulate(message_t msg, message_t *newmsg, void **data) |
326 | } | 403 | } |
327 | } | 404 | } |
328 | } | 405 | } |
329 | if ( ret == 0 ) | 406 | if (ret == 0) |
330 | *newmsg = info->msg; | 407 | *newmsg = info->msg; |
331 | if ( ret != EAGAIN && info ) | 408 | if (ret != EAGAIN && info) |
332 | _attachment_free(info, ret); | 409 | _attachment_free (info, ret); |
333 | return ret; | 410 | return ret; |
334 | } | 411 | } |
335 | 412 | ||
336 | int message_unencapsulate(message_t msg, message_t *newmsg, void **data) | 413 | int |
414 | message_unencapsulate (message_t msg, message_t * newmsg, void **data) | ||
337 | { | 415 | { |
338 | size_t size, nbytes; | 416 | size_t size, nbytes; |
339 | int ret = 0; | 417 | int ret = 0; |
... | @@ -342,41 +420,58 @@ int message_unencapsulate(message_t msg, message_t *newmsg, void **data) | ... | @@ -342,41 +420,58 @@ int message_unencapsulate(message_t msg, message_t *newmsg, void **data) |
342 | stream_t istream, ostream; | 420 | stream_t istream, ostream; |
343 | struct _msg_info *info = NULL; | 421 | struct _msg_info *info = NULL; |
344 | 422 | ||
345 | if ( msg == NULL || newmsg == NULL) | 423 | if (msg == NULL || newmsg == NULL) |
346 | return EINVAL; | 424 | return EINVAL; |
347 | 425 | ||
348 | if ( (data == NULL || *data == NULL ) && ( ret = message_get_header(msg, &hdr) ) == 0 ) { | 426 | if ((data == NULL || *data == NULL) |
349 | header_get_value(hdr, "Content-Type", NULL, 0, &size); | 427 | && (ret = message_get_header (msg, &hdr)) == 0) |
350 | if ( size ) { | 428 | { |
351 | if ( ( content_type = alloca(size+1) ) == NULL ) | 429 | header_get_value (hdr, "Content-Type", NULL, 0, &size); |
430 | if (size) | ||
431 | { | ||
432 | if ((content_type = alloca (size + 1)) == NULL) | ||
352 | return ENOMEM; | 433 | return ENOMEM; |
353 | header_get_value(hdr, "Content-Type", content_type, size+1, 0); | 434 | header_get_value (hdr, "Content-Type", content_type, size + 1, 0); |
354 | if ( strncasecmp(content_type, "message/rfc822", strlen("message/rfc822")) != 0 ) | 435 | if (strncasecmp |
436 | (content_type, "message/rfc822", | ||
437 | strlen ("message/rfc822")) != 0) | ||
355 | return EINVAL; | 438 | return EINVAL; |
356 | } else | 439 | } |
440 | else | ||
357 | return EINVAL; | 441 | return EINVAL; |
358 | } | 442 | } |
359 | if ( ( ret = _attachment_setup( &info, msg, &istream, data) ) != 0 ) | 443 | if ((ret = _attachment_setup (&info, msg, &istream, data)) != 0) |
360 | return ret; | 444 | return ret; |
361 | if ( info->msg == NULL ) | 445 | if (info->msg == NULL) |
362 | ret = message_create(&(info->msg), NULL); | 446 | ret = message_create (&(info->msg), NULL); |
363 | if ( ret == 0 ) { | 447 | if (ret == 0) |
364 | message_get_stream(info->msg, &ostream); | 448 | { |
365 | if ( info->nbytes ) | 449 | message_get_stream (info->msg, &ostream); |
366 | memmove( info->buf, info->buf + (BUF_SIZE - info->nbytes), info->nbytes); | 450 | if (info->nbytes) |
367 | while ( (ret == 0 && info->nbytes) || ( ( ret = stream_read(istream, info->buf, BUF_SIZE, info->ioffset, &info->nbytes) ) == 0 && info->nbytes ) ) { | 451 | memmove (info->buf, info->buf + (BUF_SIZE - info->nbytes), |
452 | info->nbytes); | ||
453 | while ((ret == 0 && info->nbytes) | ||
454 | || | ||
455 | ((ret = | ||
456 | stream_read (istream, info->buf, BUF_SIZE, info->ioffset, | ||
457 | &info->nbytes)) == 0 && info->nbytes)) | ||
458 | { | ||
368 | info->ioffset += info->nbytes; | 459 | info->ioffset += info->nbytes; |
369 | while( info->nbytes ) { | 460 | while (info->nbytes) |
370 | if ( ( ret = stream_write(ostream, info->buf, info->nbytes, info->ooffset, &nbytes ) ) != 0 ) | 461 | { |
462 | if ((ret = | ||
463 | stream_write (ostream, info->buf, info->nbytes, | ||
464 | info->ooffset, &nbytes)) != 0) | ||
371 | break; | 465 | break; |
372 | info->nbytes -= nbytes; | 466 | info->nbytes -= nbytes; |
373 | info->ooffset += nbytes; | 467 | info->ooffset += nbytes; |
374 | } | 468 | } |
375 | } | 469 | } |
376 | } | 470 | } |
377 | if ( ret == 0 ) | 471 | if (ret == 0) |
378 | *newmsg = info->msg; | 472 | *newmsg = info->msg; |
379 | if ( ret != EAGAIN && info ) | 473 | if (ret != EAGAIN && info) |
380 | _attachment_free(info, ret); | 474 | _attachment_free (info, ret); |
381 | return ret; | 475 | return ret; |
382 | } | 476 | } |
477 | ... | ... |
-
Please register or sign in to post a comment