Commit 42a59a2f 42a59a2fb37dd5f7b01e2d1d08405163d17c10ab by Sergey Poznyakoff

(build_mime): Split into three smaller

functions, each for its own task.
Add Content-Type,Content-Transfer-Encoding headers to the
DSN. Proposed by Kostas Zorbadelos.
1 parent ccc5e5a2
...@@ -132,105 +132,128 @@ mu_sieve_get_message_sender (mu_message_t msg, char **ptext) ...@@ -132,105 +132,128 @@ mu_sieve_get_message_sender (mu_message_t msg, char **ptext)
132 return rc; 132 return rc;
133 } 133 }
134 134
135 static void
136 mime_create_reason (mu_mime_t mime, mu_message_t msg, const char *text)
137 {
138 mu_message_t newmsg;
139 mu_stream_t stream;
140 time_t t;
141 struct tm *tm;
142 char *sender;
143 size_t off = 0;
144 mu_body_t body;
145 mu_header_t hdr;
146 char datestr[80];
147 static char *content_header = "Content-Type: text/plain;charset=" MU_SIEVE_CHARSET "\n"
148 "Content-Transfer-Encoding: 8bit\n";
149
150 mu_message_create (&newmsg, NULL);
151 mu_message_get_body (newmsg, &body);
152 mu_body_get_stream (body, &stream);
153
154 time (&t);
155 tm = localtime (&t);
156 strftime (datestr, sizeof datestr, "%a, %b %d %H:%M:%S %Y %Z", tm);
157
158 mu_sieve_get_message_sender (msg, &sender);
159
160 mu_stream_printf (stream, &off,
161 "\nThe original message was received at %s from %s.\n",
162 datestr, sender);
163 free (sender);
164 mu_stream_printf (stream, &off,
165 "Message was refused by recipient's mail filtering program.\n");
166 mu_stream_printf (stream, &off, "Reason given was as follows:\n\n");
167 mu_stream_printf (stream, &off, "%s", text);
168 mu_stream_close (stream);
169 mu_header_create (&hdr, content_header, strlen (content_header), newmsg);
170 mu_message_set_header (newmsg, hdr, NULL);
171 mu_mime_add_part (mime, newmsg);
172 mu_message_unref (newmsg);
173 }
174
175 static void
176 mime_create_ds (mu_mime_t mime)
177 {
178 mu_message_t newmsg;
179 mu_stream_t stream;
180 mu_header_t hdr;
181 size_t off = 0;
182 mu_body_t body;
183 char *email;
184 char datestr[80];
185
186 mu_message_create (&newmsg, NULL);
187 mu_message_get_header (newmsg, &hdr);
188 mu_header_set_value (hdr, "Content-Type", "message/delivery-status", 1);
189 mu_message_get_body (newmsg, &body);
190 mu_body_get_stream (body, &stream);
191 mu_stream_printf (stream, &off, "Reporting-UA: sieve; %s\n", PACKAGE_STRING);
192 mu_stream_printf (stream, &off, "Arrival-Date: %s\n", datestr);
193 email = mu_get_user_email (NULL);
194 mu_stream_printf (stream, &off, "Final-Recipient: RFC822; %s\n",
195 email ? email : "unknown");
196 free (email);
197 mu_stream_printf (stream, &off, "Action: deleted\n");
198 mu_stream_printf (stream, &off,
199 "Disposition: automatic-action/MDN-sent-automatically;deleted\n");
200 mu_stream_printf (stream, &off, "Last-Attempt-Date: %s\n", datestr);
201 mu_stream_close (stream);
202 mu_mime_add_part(mime, newmsg);
203 mu_message_unref (newmsg);
204 }
205
206
207 /* Quote original message */
208 static int
209 mime_create_quote (mu_mime_t mime, mu_message_t msg)
210 {
211 mu_message_t newmsg;
212 mu_stream_t istream, ostream;
213 mu_header_t hdr;
214 size_t ioff = 0, ooff = 0, n;
215 char buffer[512];
216 mu_body_t body;
217
218 mu_message_create (&newmsg, NULL);
219 mu_message_get_header (newmsg, &hdr);
220 mu_header_set_value (hdr, "Content-Type", "message/rfc822", 1);
221 mu_message_get_body (newmsg, &body);
222 mu_body_get_stream (body, &ostream);
223 mu_message_get_stream (msg, &istream);
224
225 while (mu_stream_read (istream, buffer, sizeof buffer - 1, ioff, &n) == 0
226 && n != 0)
227 {
228 size_t sz;
229 mu_stream_write (ostream, buffer, n, ooff, &sz);
230 if (sz != n)
231 return EIO;
232 ooff += n;
233 ioff += n;
234 }
235 mu_stream_close (ostream);
236 mu_mime_add_part (mime, newmsg);
237 mu_message_unref (newmsg);
238 return 0;
239 }
240
135 static int 241 static int
136 build_mime (mu_mime_t *pmime, mu_message_t msg, const char *text) 242 build_mime (mu_mime_t *pmime, mu_message_t msg, const char *text)
137 { 243 {
138 mu_mime_t mime = NULL; 244 mu_mime_t mime = NULL;
139 char datestr[80]; 245 int status;
140 246
141 mu_mime_create (&mime, NULL, 0); 247 mu_mime_create (&mime, NULL, 0);
142 { 248 mime_create_reason (mime, msg, text);
143 mu_message_t newmsg; 249 mime_create_ds (mime);
144 mu_stream_t stream; 250 status = mime_create_quote (mime, msg);
145 time_t t; 251 if (status)
146 struct tm *tm; 252 {
147 char *sender; 253 mu_mime_destroy (&mime);
148 size_t off = 0; 254 return status;
149 mu_body_t body; 255 }
150
151 mu_message_create (&newmsg, NULL);
152 mu_message_get_body (newmsg, &body);
153 mu_body_get_stream (body, &stream);
154
155 time (&t);
156 tm = localtime (&t);
157 strftime (datestr, sizeof datestr, "%a, %b %d %H:%M:%S %Y %Z", tm);
158
159 mu_sieve_get_message_sender (msg, &sender);
160
161 mu_stream_printf (stream, &off,
162 "\nThe original message was received at %s from %s.\n",
163 datestr, sender);
164 free (sender);
165 mu_stream_printf (stream, &off,
166 "Message was refused by recipient's mail filtering program.\n");
167 mu_stream_printf (stream, &off, "Reason given was as follows:\n\n");
168 mu_stream_printf (stream, &off, "%s", text);
169 mu_stream_close (stream);
170 mu_mime_add_part (mime, newmsg);
171 mu_message_unref (newmsg);
172 }
173
174 /* message/delivery-status */
175 {
176 mu_message_t newmsg;
177 mu_stream_t stream;
178 mu_header_t hdr;
179 size_t off = 0;
180 mu_body_t body;
181 char *email;
182
183 mu_message_create (&newmsg, NULL);
184 mu_message_get_header (newmsg, &hdr);
185 mu_header_set_value (hdr, "Content-Type", "message/delivery-status", 1);
186 mu_message_get_body (newmsg, &body);
187 mu_body_get_stream (body, &stream);
188 mu_stream_printf (stream, &off, "Reporting-UA: sieve; %s\n", PACKAGE_STRING);
189 mu_stream_printf (stream, &off, "Arrival-Date: %s\n", datestr);
190 email = mu_get_user_email (NULL);
191 mu_stream_printf (stream, &off, "Final-Recipient: RFC822; %s\n",
192 email ? email : "unknown");
193 free (email);
194 mu_stream_printf (stream, &off, "Action: deleted\n");
195 mu_stream_printf (stream, &off,
196 "Disposition: automatic-action/MDN-sent-automatically;deleted\n");
197 mu_stream_printf (stream, &off, "Last-Attempt-Date: %s\n", datestr);
198 mu_stream_close (stream);
199 mu_mime_add_part(mime, newmsg);
200 mu_message_unref (newmsg);
201 }
202
203 /* Quote original message */
204 {
205 mu_message_t newmsg;
206 mu_stream_t istream, ostream;
207 mu_header_t hdr;
208 size_t ioff = 0, ooff = 0, n;
209 char buffer[512];
210 mu_body_t body;
211
212 mu_message_create (&newmsg, NULL);
213 mu_message_get_header (newmsg, &hdr);
214 mu_header_set_value (hdr, "Content-Type", "message/rfc822", 1);
215 mu_message_get_body (newmsg, &body);
216 mu_body_get_stream (body, &ostream);
217 mu_message_get_stream (msg, &istream);
218 256
219 while (mu_stream_read (istream, buffer, sizeof buffer - 1, ioff, &n) == 0
220 && n != 0)
221 {
222 size_t sz;
223 mu_stream_write (ostream, buffer, n, ooff, &sz);
224 if (sz != n)
225 return EIO;
226 ooff += n;
227 ioff += n;
228 }
229 mu_stream_close (ostream);
230 mu_mime_add_part (mime, newmsg);
231 mu_message_unref (newmsg);
232 }
233
234 *pmime = mime; 257 *pmime = mime;
235 258
236 return 0; 259 return 0;
......