Got multipart/mixed being transmitted
[citadel.git] / webcit-ng / server / messages.c
1 // Message base functions
2 //
3 // Copyright (c) 1996-2022 by the citadel.org team
4 //
5 // This program is open source software.  Use, duplication, or
6 // disclosure are subject to the GNU General Public License v3.
7
8 #include "webcit.h"
9
10
11 // Given an encoded UID, translate that to an unencoded Citadel EUID and
12 // then search for it in the current room.  Return a message number or -1
13 // if not found.
14 long locate_message_by_uid(struct ctdlsession *c, char *uid) {
15         char buf[1024];
16
17         ctdl_printf(c, "EUID %s", uid);
18         ctdl_readline(c, buf, sizeof buf);
19         if (buf[0] == '2') {
20                 return (atol(&buf[4]));
21
22         }
23
24         // Ugly hack to handle Mozilla Thunderbird, try stripping ".ics" if present
25         if (!strcasecmp(&uid[strlen(uid) - 4], ".ics")) {
26                 safestrncpy(buf, uid, sizeof buf);
27                 buf[strlen(buf) - 4] = 0;
28                 ctdl_printf(c, "EUID %s", buf);
29                 ctdl_readline(c, buf, sizeof buf);
30                 if (buf[0] == '2') {
31                         return (atol(&buf[4]));
32
33                 }
34         }
35
36         return (-1);
37 }
38
39
40 // DAV delete an object in a room.
41 void dav_delete_message(struct http_transaction *h, struct ctdlsession *c, long msgnum) {
42         ctdl_delete_msgs(c, &msgnum, 1);
43         do_204(h);
44 }
45
46
47 // DAV move or copy an object in a room.
48 void dav_move_or_copy_message(struct http_transaction *h, struct ctdlsession *c, long msgnum, int move_or_copy) {
49         char target_room[ROOMNAMELEN];
50         char buf[1024];
51
52         // HTTP "Destination" header will tell us the target collection
53         char *target_collection = header_val(h, "Destination");
54         syslog(LOG_DEBUG, "dest coll: \"%s\"", target_collection);
55
56         // Translate the target WebDAV Collection name to a Citadel Room name.
57         // Note that some clients will supply a fully-qualified URL such as "http://example.com/ctdl/r/roomname/999"
58         // so we're just going to search for "/ctdl/r/" and work from there.
59         char *ctdlr = strstr(target_collection, "/ctdl/r/");
60         if (ctdlr == NULL) {    
61                 do_412(h);              // badly formed target collection; fail out.
62                 return;
63         }
64         safestrncpy(target_room, ctdlr+8, sizeof target_room);
65         char *slash = strchr(target_room, '/');
66         if (slash) {
67                 *slash = 0;             // lop off the "filename" we don't need it
68         }
69         unescape_input(target_room);
70         syslog(LOG_DEBUG, "dest room: \"%s\"", target_room);
71
72         // Perform the move or copy operation
73         ctdl_printf(c, "MOVE %ld|%s|%d", msgnum, target_room, move_or_copy);    // Citadel Server: 0=move, 1=copy
74         ctdl_readline(c, buf, sizeof buf);
75         if (buf[0] == '2') {
76                 do_204(h);              // succeed (no content)
77                 return;
78         }
79         do_412(h);                      // fail (precondition failed)
80 }
81
82
83 // GET method directly on a message in a room
84 void dav_get_message(struct http_transaction *h, struct ctdlsession *c, long msgnum) {
85         char buf[1024];
86         int in_body = 0;
87         int encoding = 0;
88         StrBuf *Body = NULL;
89
90         ctdl_printf(c, "MSG2 %ld", msgnum);
91         ctdl_readline(c, buf, sizeof buf);
92         if (buf[0] != '1') {
93                 do_404(h);
94                 return;
95         }
96
97         char *etag = malloc(20);
98         if (etag != NULL) {
99                 sprintf(etag, "%ld", msgnum);
100                 add_response_header(h, strdup("ETag"), etag);   // http_transaction now owns this memory
101         }
102
103         while (ctdl_readline(c, buf, sizeof buf), strcmp(buf, "000")) {
104                 if (IsEmptyStr(buf) && (in_body == 0)) {
105                         in_body = 1;
106                         Body = NewStrBuf();
107                 }
108                 else if (in_body == 0) {
109                         char *k = buf;
110                         char *v = strchr(buf, ':');
111                         if (v) {
112                                 *v = 0;
113                                 ++v;
114                                 string_trim(v);                         // we now have a key (k) and a value (v)
115                                 if ((!strcasecmp(k, "content-type"))    // fields which can be passed from RFC822 to HTTP as-is
116                                     || (!strcasecmp(k, "date"))
117                                 ) {
118                                         add_response_header(h, strdup(k), strdup(v));
119                                 }
120                                 else if (!strcasecmp(k, "content-transfer-encoding")) {
121                                         if (!strcasecmp(v, "base64")) {
122                                                 encoding = 'b';
123                                         }
124                                         else if (!strcasecmp(v, "quoted-printable")) {
125                                                 encoding = 'q';
126                                         }
127                                 }
128                         }
129                 }
130                 else if ((in_body == 1) && (Body != NULL)) {
131                         StrBufAppendPrintf(Body, "%s\n", buf);
132                 }
133         }
134
135         h->response_code = 200;
136         h->response_string = strdup("OK");
137
138         if (Body != NULL) {
139                 if (encoding == 'q') {
140                         h->response_body = malloc(StrLength(Body));
141                         if (h->response_body != NULL) {
142                                 h->response_body_length =
143                                     CtdlDecodeQuotedPrintable(h->response_body, (char *) ChrPtr(Body), StrLength(Body));
144                         }
145                         FreeStrBuf(&Body);
146                 }
147                 else if (encoding == 'b') {
148                         h->response_body = malloc(StrLength(Body));
149                         if (h->response_body != NULL) {
150                                 h->response_body_length = CtdlDecodeBase64(h->response_body, ChrPtr(Body), StrLength(Body));
151                         }
152                         FreeStrBuf(&Body);
153                 }
154                 else {
155                         h->response_body_length = StrLength(Body);
156                         h->response_body = SmashStrBuf(&Body);
157                 }
158         }
159 }
160
161
162 // PUT a message into a room
163 void dav_put_message(struct http_transaction *h, struct ctdlsession *c, char *euid, long old_msgnum) {
164         char buf[1024];
165         char *content_type = NULL;
166         int n;
167         long new_msgnum;
168         char new_euid[1024];
169         char response_string[1024];
170         char mime_boundary[80];
171
172         if ((h->request_body == NULL) || (h->request_body_length < 1)) {
173                 do_404(h);                              // Refuse to post a null message
174                 return;
175         }
176
177         // Extract metadata from the URL
178         char *wefw = get_url_param(h, "wefw");          // References:
179         if (!wefw) wefw = "";
180         char *subj = get_url_param(h, "subj");          // Subject:
181         if (!subj) subj = "";
182         char *mailto = get_url_param(h, "mailto");      // To:
183         if (!mailto) mailto = "";
184         char *mailcc = get_url_param(h, "mailcc");      // Cc:
185         if (!mailcc) mailcc = "";
186         char *mailbcc = get_url_param(h, "mailbcc");    // Bcc:
187         if (!mailbcc) mailbcc = "";
188
189         // If there are attachments, we have to merge them into the message text.
190         char *att = get_url_param(h, "att");
191         if (att) {
192                 syslog(LOG_DEBUG, "💥 There are attachments.  Going multipart/mixed. <%s>", att);
193         }
194
195         // Mode 4 will give us metadata back after upload
196         ctdl_printf(c, "ENT0 1|%s||4|%s||1|%s|%s|||%s|", mailto, subj, mailcc, mailbcc, wefw);
197         ctdl_readline(c, buf, sizeof buf);
198         if (buf[0] != '8') {
199                 h->response_code = 502;
200                 h->response_string = strdup("bad gateway");
201                 add_response_header(h, strdup("Content-type"), strdup("text/plain"));
202                 h->response_body = strdup(buf);
203                 h->response_body_length = strlen(h->response_body);
204                 return;
205         }
206
207         // Remember, ctdl_printf() appends \n on its own, so when adding a CRLF newline, only use \r
208         // Or for a blank line, use ctdl_write() with \r\n
209
210
211         // If there are attachments, open up a multipart/mixed MIME container.
212         if (att) {
213                 snprintf(mime_boundary, sizeof(mime_boundary), "citadel-multipart-%x-%x", time(NULL), rand());
214                 ctdl_printf(c, "MIME-Version: 1.0\r");
215                 ctdl_printf(c, "Content-Type: multipart/mixed; boundary=\"%s\"\r", mime_boundary);
216                 ctdl_write(c, HKEY("\r\n"));
217                 ctdl_write(c, HKEY("\r\n"));
218                 ctdl_printf(c, "--%s\r", mime_boundary);        // start of message body
219         }
220
221         // This section
222         content_type = header_val(h, "Content-type");
223         ctdl_printf(c, "Content-type: %s\r", (content_type ? content_type : "application/octet-stream"));
224         ctdl_write(c, HKEY("\r\n"));
225         ctdl_write(c, h->request_body, h->request_body_length);
226         if (h->request_body[h->request_body_length] != '\n') {
227                 ctdl_write(c, HKEY("\r\n"));
228         }
229
230         // If there are attachments, close the multipart/mixed MIME container.
231         if (att) {
232                 // FIXME actually attach the attachments here.
233                 ctdl_printf(c, "--%s--\r", mime_boundary);
234         }
235
236         // Done writing to the Citadel Server.
237         ctdl_printf(c, "000");
238
239         // Now handle the response from the Citadel Server.
240         n = 0;
241         new_msgnum = 0;
242         strcpy(new_euid, "");
243         strcpy(response_string, "");
244
245         while (ctdl_readline(c, buf, sizeof buf), strcmp(buf, "000"))
246                 switch (n++) {
247                 case 0:
248                         new_msgnum = atol(buf);
249                         break;
250                 case 1:
251                         safestrncpy(response_string, buf, sizeof response_string);
252                         syslog(LOG_DEBUG, "new_msgnum=%ld (%s)\n", new_msgnum, buf);
253                         break;
254                 case 2:
255                         safestrncpy(new_euid, buf, sizeof new_euid);
256                         break;
257                 default:
258                         break;
259                 }
260
261         // Tell the client what happened.
262
263         // Citadel failed in some way?
264         char *new_location = malloc(1024);
265         if ((new_msgnum < 0L) || (new_location == NULL)) {
266                 h->response_code = 502;
267                 h->response_string = strdup("bad gateway");
268                 add_response_header(h, strdup("Content-type"), strdup("text/plain"));
269                 h->response_body = strdup(response_string);
270                 h->response_body_length = strlen(h->response_body);
271                 return;
272         }
273
274         char *etag = malloc(20);
275         if (etag != NULL) {
276                 sprintf(etag, "%ld", new_msgnum);
277                 add_response_header(h, strdup("ETag"), etag);   // http_transaction now owns this memory
278         }
279
280         char esc_room[1024];
281         char esc_euid[1024];
282         urlesc(esc_room, sizeof esc_room, c->room);
283         urlesc(esc_euid, sizeof esc_euid, new_euid);
284         snprintf(new_location, 1024, "/ctdl/r/%s/%s", esc_room, esc_euid);
285         add_response_header(h, strdup("Location"), new_location);       // http_transaction now owns this memory
286
287         if (old_msgnum <= 0) {
288                 h->response_code = 201; // We created this item for the first time.
289                 h->response_string = strdup("created");
290         }
291         else {
292                 h->response_code = 204; // We modified an existing item.
293                 h->response_string = strdup("no content");
294
295                 // The item we replaced has probably already been deleted by
296                 // the Citadel server, but we'll do this anyway, just in case.
297                 ctdl_delete_msgs(c, &old_msgnum, 1);
298         }
299
300 }
301
302
303 // Download a single component of a MIME-encoded message
304 void download_mime_component(struct http_transaction *h, struct ctdlsession *c, long msgnum, char *partnum) {
305         char buf[1024];
306         char content_type[1024];
307
308         ctdl_printf(c, "DLAT %ld|%s", msgnum, partnum);
309         ctdl_readline(c, buf, sizeof buf);
310         if (buf[0] != '6') {
311                 do_404(h);      // too bad, so sad, go away
312         }
313         // Server response is going to be: 6XX length|-1|filename|content-type|charset
314         h->response_body_length = extract_int(&buf[4], 0);
315         extract_token(content_type, buf, 3, '|', sizeof content_type);
316
317         h->response_body = malloc(h->response_body_length + 1);
318         int bytes = 0;
319         int thisblock;
320         do {
321                 thisblock = read(c->sock, &h->response_body[bytes], (h->response_body_length - bytes));
322                 bytes += thisblock;
323                 syslog(LOG_DEBUG, "Bytes read: %d of %d", (int) bytes, (int) h->response_body_length);
324         } while ((bytes < h->response_body_length) && (thisblock >= 0));
325         h->response_body[h->response_body_length] = 0;  // null terminate it just for good measure
326         syslog(LOG_DEBUG, "content type: %s", content_type);
327
328         add_response_header(h, strdup("Content-type"), strdup(content_type));
329         h->response_code = 200;
330         h->response_string = strdup("OK");
331 }