2 * Message base functions
4 * Copyright (c) 1996-2018 by the citadel.org team
6 * This program is open source software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 3.
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.
19 * Given an encoded UID, translate that to an unencoded Citadel EUID and
20 * then search for it in the current room. Return a message number or -1
24 long locate_message_by_uid(struct ctdlsession *c, char *uid)
28 ctdl_printf(c, "EUID %s", uid);
29 ctdl_readline(c, buf, sizeof buf);
31 return (atol(&buf[4]));
35 /* Ugly hack to handle Mozilla Thunderbird, try stripping ".ics" if present */
36 if (!strcasecmp(&uid[strlen(uid) - 4], ".ics")) {
37 safestrncpy(buf, uid, sizeof buf);
38 buf[strlen(buf) - 4] = 0;
39 ctdl_printf(c, "EUID %s", buf);
40 ctdl_readline(c, buf, sizeof buf);
42 return (atol(&buf[4]));
52 * DAV delete an object in a room.
54 void dav_delete_message(struct http_transaction *h, struct ctdlsession *c, long msgnum)
56 ctdl_delete_msgs(c, &msgnum, 1);
57 h->response_code = 204;
58 h->response_string = strdup("no content");
63 * GET method directly on a message in a room
65 void dav_get_message(struct http_transaction *h, struct ctdlsession *c, long msgnum)
72 ctdl_printf(c, "MSG2 %ld", msgnum);
73 ctdl_readline(c, buf, sizeof buf);
79 char *etag = malloc(20);
81 sprintf(etag, "%ld", msgnum);
82 add_response_header(h, strdup("ETag"), etag); // http_transaction now owns this memory
85 while (ctdl_readline(c, buf, sizeof buf), strcmp(buf, "000")) {
86 if (IsEmptyStr(buf) && (in_body == 0)) {
89 } else if (in_body == 0) {
91 char *v = strchr(buf, ':');
95 striplt(v); // we now have a key (k) and a value (v)
96 if ((!strcasecmp(k, "content-type")) // fields which can be passed from RFC822 to HTTP as-is
97 || (!strcasecmp(k, "date"))
99 add_response_header(h, strdup(k), strdup(v));
100 } else if (!strcasecmp(k, "content-transfer-encoding")) {
101 if (!strcasecmp(v, "base64")) {
103 } else if (!strcasecmp(v, "quoted-printable")) {
108 } else if ((in_body == 1) && (Body != NULL)) {
109 StrBufAppendPrintf(Body, "%s\n", buf);
113 h->response_code = 200;
114 h->response_string = strdup("OK");
117 if (encoding == 'q') {
118 h->response_body = malloc(StrLength(Body));
119 if (h->response_body != NULL) {
120 h->response_body_length =
121 CtdlDecodeQuotedPrintable(h->response_body, (char *) ChrPtr(Body), StrLength(Body));
124 } else if (encoding == 'b') {
125 h->response_body = malloc(StrLength(Body));
126 if (h->response_body != NULL) {
127 h->response_body_length = CtdlDecodeBase64(h->response_body, ChrPtr(Body), StrLength(Body));
131 h->response_body_length = StrLength(Body);
132 h->response_body = SmashStrBuf(&Body);
139 * PUT a message into a room
141 void dav_put_message(struct http_transaction *h, struct ctdlsession *c, char *euid, long old_msgnum)
144 char *content_type = NULL;
148 char response_string[1024];
150 if ((h->request_body == NULL) || (h->request_body_length < 1)) {
151 do_404(h); // Refuse to post a null message
155 ctdl_printf(c, "ENT0 1|||4|||1|"); // This protocol syntax will give us metadata back after upload
156 ctdl_readline(c, buf, sizeof buf);
158 h->response_code = 502;
159 h->response_string = strdup("bad gateway");
160 add_response_header(h, strdup("Content-type"), strdup("text/plain"));
161 h->response_body = strdup(buf);
162 h->response_body_length = strlen(h->response_body);
166 content_type = header_val(h, "Content-type");
167 ctdl_printf(c, "Content-type: %s\r\n", (content_type ? content_type : "application/octet-stream"));
168 ctdl_printf(c, "\r\n");
169 ctdl_write(c, h->request_body, h->request_body_length);
170 if (h->request_body[h->request_body_length] != '\n') {
171 ctdl_printf(c, "\r\n");
173 ctdl_printf(c, "000");
176 * Now handle the response from the Citadel server.
181 strcpy(new_euid, "");
182 strcpy(response_string, "");
184 while (ctdl_readline(c, buf, sizeof buf), strcmp(buf, "000"))
187 new_msgnum = atol(buf);
190 safestrncpy(response_string, buf, sizeof response_string);
191 syslog(LOG_DEBUG, "new_msgnum=%ld (%s)\n", new_msgnum, buf);
194 safestrncpy(new_euid, buf, sizeof new_euid);
200 /* Tell the client what happened. */
202 /* Citadel failed in some way? */
203 char *new_location = malloc(1024);
204 if ((new_msgnum < 0L) || (new_location == NULL)) {
205 h->response_code = 502;
206 h->response_string = strdup("bad gateway");
207 add_response_header(h, strdup("Content-type"), strdup("text/plain"));
208 h->response_body = strdup(response_string);
209 h->response_body_length = strlen(h->response_body);
213 char *etag = malloc(20);
215 sprintf(etag, "%ld", new_msgnum);
216 add_response_header(h, strdup("ETag"), etag); // http_transaction now owns this memory
221 urlesc(esc_room, sizeof esc_room, c->room);
222 urlesc(esc_euid, sizeof esc_euid, new_euid);
223 snprintf(new_location, 1024, "/ctdl/r/%s/%s", esc_room, esc_euid);
224 add_response_header(h, strdup("Location"), new_location); // http_transaction now owns this memory
226 if (old_msgnum <= 0) {
227 h->response_code = 201; // We created this item for the first time.
228 h->response_string = strdup("created");
230 h->response_code = 204; // We modified an existing item.
231 h->response_string = strdup("no content");
233 /* The item we replaced has probably already been deleted by
234 * the Citadel server, but we'll do this anyway, just in case.
236 ctdl_delete_msgs(c, &old_msgnum, 1);
243 * Download a single component of a MIME-encoded message
245 void download_mime_component(struct http_transaction *h, struct ctdlsession *c, long msgnum, char *partnum)
248 char content_type[1024];
250 ctdl_printf(c, "DLAT %ld|%s", msgnum, partnum);
251 ctdl_readline(c, buf, sizeof buf);
253 do_404(h); // too bad, so sad, go away
255 // Server response is going to be: 6XX length|-1|filename|content-type|charset
256 h->response_body_length = extract_int(&buf[4], 0);
257 extract_token(content_type, buf, 3, '|', sizeof content_type);
259 h->response_body = malloc(h->response_body_length + 1);
263 thisblock = read(c->sock, &h->response_body[bytes], (h->response_body_length - bytes));
265 syslog(LOG_DEBUG, "Bytes read: %d of %d", (int) bytes, (int) h->response_body_length);
266 } while ((bytes < h->response_body_length) && (thisblock >= 0));
267 h->response_body[h->response_body_length] = 0; // null terminate it just for good measure
268 syslog(LOG_DEBUG, "content type: %s", content_type);
270 add_response_header(h, strdup("Content-type"), strdup(content_type));
271 h->response_code = 200;
272 h->response_string = strdup("OK");