2 * Message base functions
4 * Copyright (c) 1996-2016 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) {
27 ctdl_printf(c, "EUID %s", uid);
28 ctdl_readline(c, buf, sizeof buf);
30 return(atol(&buf[4]));
34 /* Ugly hack to handle Mozilla Thunderbird, try stripping ".ics" if present */
35 if (!strcasecmp(&uid[strlen(uid)-4], ".ics")) {
36 safestrncpy(buf, uid, sizeof buf);
37 buf[strlen(buf)-4] = 0;
38 ctdl_printf(c, "EUID %s", buf);
39 ctdl_readline(c, buf, sizeof buf);
41 return(atol(&buf[4]));
51 * DAV delete an object in a room.
53 void dav_delete_message(struct http_transaction *h, struct ctdlsession *c, long msgnum)
55 ctdl_delete_msgs(c, &msgnum, 1);
56 h->response_code = 204;
57 h->response_string = strdup("no content");
62 * GET method directly on a message in a room
64 void dav_get_message(struct http_transaction *h, struct ctdlsession *c, long msgnum)
71 ctdl_printf(c, "MSG2 %ld", msgnum);
72 ctdl_readline(c, buf, sizeof buf);
78 char *etag = malloc(20);
80 sprintf(etag, "%ld", msgnum);
81 add_response_header(h, strdup("ETag"), etag); // http_transaction now owns this memory
84 while (ctdl_readline(c, buf, sizeof buf), strcmp(buf,"000"))
86 if (IsEmptyStr(buf) && (in_body == 0)) {
90 else if (in_body == 0) {
92 char *v = strchr(buf, ':');
96 striplt(v); // we now have a key (k) and a value (v)
98 (!strcasecmp(k, "content-type")) // fields which can be passed from RFC822 to HTTP as-is
99 || (!strcasecmp(k, "date"))
101 add_response_header(h, strdup(k), strdup(v));
103 else if (!strcasecmp(k, "content-transfer-encoding")) {
104 if (!strcasecmp(v, "base64")) {
107 else if (!strcasecmp(v, "quoted-printable")) {
113 else if ( (in_body == 1) && (Body != NULL) ) {
114 StrBufAppendPrintf(Body, "%s\n", buf);
118 h->response_code = 200;
119 h->response_string = strdup("OK");
122 if (encoding == 'q') {
123 h->response_body = malloc(StrLength(Body));
124 if (h->response_body != NULL) {
125 h->response_body_length = CtdlDecodeQuotedPrintable(h->response_body, (char *)ChrPtr(Body), StrLength(Body));
129 else if (encoding == 'b') {
130 h->response_body = malloc(StrLength(Body));
131 if (h->response_body != NULL) {
132 h->response_body_length = CtdlDecodeBase64(h->response_body, ChrPtr(Body), StrLength(Body));
137 h->response_body_length = StrLength(Body);
138 h->response_body = SmashStrBuf(&Body);
145 * PUT a message into a room
147 void dav_put_message(struct http_transaction *h, struct ctdlsession *c, char *euid, long old_msgnum)
150 char *content_type = NULL;
154 char response_string[1024];
156 if ( (h->request_body == NULL) || (h->request_body_length < 1) ) {
157 do_404(h); // Refuse to post a null message
161 ctdl_printf(c, "ENT0 1|||4|||1|"); // This protocol syntax will give us metadata back after upload
162 ctdl_readline(c, buf, sizeof buf);
164 h->response_code = 502;
165 h->response_string = strdup("bad gateway");
166 add_response_header(h, strdup("Content-type"), strdup("text/plain"));
167 h->response_body = strdup(buf);
168 h->response_body_length = strlen(h->response_body);
172 content_type = header_val(h, "Content-type");
173 ctdl_printf(c, "Content-type: %s\r\n", (content_type ? content_type : "application/octet-stream"));
174 ctdl_printf(c, "\r\n");
175 ctdl_write(c, h->request_body, h->request_body_length);
176 if (h->request_body[h->request_body_length] != '\n') {
177 ctdl_printf(c, "\r\n");
179 ctdl_printf(c, "000");
182 * Now handle the response from the Citadel server.
187 strcpy(new_euid, "");
188 strcpy(response_string, "");
190 while (ctdl_readline(c, buf, sizeof buf), strcmp(buf, "000")) switch(n++) {
192 new_msgnum = atol(buf);
195 safestrncpy(response_string, buf, sizeof response_string);
196 syslog(LOG_DEBUG, "new_msgnum=%ld (%s)\n", new_msgnum, buf);
199 safestrncpy(new_euid, buf, sizeof new_euid);
205 /* Tell the client what happened. */
207 /* Citadel failed in some way? */
208 char *new_location = malloc(1024);
209 if ( (new_msgnum < 0L) || (new_location == NULL) ) {
210 h->response_code = 502;
211 h->response_string = strdup("bad gateway");
212 add_response_header(h, strdup("Content-type"), strdup("text/plain"));
213 h->response_body = strdup(response_string);
214 h->response_body_length = strlen(h->response_body);
218 char *etag = malloc(20);
220 sprintf(etag, "%ld", new_msgnum);
221 add_response_header(h, strdup("ETag"), etag); // http_transaction now owns this memory
226 urlesc(esc_room, sizeof esc_room, c->room);
227 urlesc(esc_euid, sizeof esc_euid, new_euid);
228 snprintf(new_location, 1024, "/ctdl/r/%s/%s", esc_room, esc_euid);
229 add_response_header(h, strdup("Location"), new_location); // http_transaction now owns this memory
231 if (old_msgnum <= 0) {
232 h->response_code = 201; // We created this item for the first time.
233 h->response_string = strdup("created");
236 h->response_code = 204; // We modified an existing item.
237 h->response_string = strdup("no content");
239 /* The item we replaced has probably already been deleted by
240 * the Citadel server, but we'll do this anyway, just in case.
242 ctdl_delete_msgs(c, &old_msgnum, 1);
249 * Download a single component of a MIME-encoded message
251 void download_mime_component(struct http_transaction *h, struct ctdlsession *c, long msgnum, char *partnum)
254 char content_type[1024];
256 ctdl_printf(c, "DLAT %ld|%s", msgnum, partnum);
257 ctdl_readline(c, buf, sizeof buf);
259 do_404(h); // too bad, so sad, go away
262 // Server response is going to be: 6XX length|-1|filename|content-type|charset
263 h->response_body_length = extract_int(&buf[4], 0);
264 extract_token(content_type, buf, 3, '|', sizeof content_type);
266 h->response_body = malloc(h->response_body_length + 1);
270 thisblock = read(c->sock, &h->response_body[bytes], (h->response_body_length - bytes));
272 syslog(LOG_DEBUG, "Bytes read: %d of %d", bytes, h->response_body_length);
273 } while ((bytes < h->response_body_length) && (thisblock >= 0));
274 h->response_body[h->response_body_length] = 0; // null terminate it just for good measure
275 syslog(LOG_DEBUG, "content type: %s", content_type);
277 add_response_header(h, strdup("Content-type"), strdup(content_type));
278 h->response_code = 200;
279 h->response_string = strdup("OK");