2 // Message base functions
4 // Copyright (c) 1996-2021 by the citadel.org team
6 // This program is open source software. It runs great on the
7 // Linux operating system (and probably elsewhere). You can use,
8 // copy, and run it under the terms of the GNU General Public
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // 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
22 long locate_message_by_uid(struct ctdlsession *c, char *uid) {
25 ctdl_printf(c, "EUID %s", uid);
26 ctdl_readline(c, buf, sizeof buf);
28 return (atol(&buf[4]));
32 // Ugly hack to handle Mozilla Thunderbird, try stripping ".ics" if present
33 if (!strcasecmp(&uid[strlen(uid) - 4], ".ics")) {
34 safestrncpy(buf, uid, sizeof buf);
35 buf[strlen(buf) - 4] = 0;
36 ctdl_printf(c, "EUID %s", buf);
37 ctdl_readline(c, buf, sizeof buf);
39 return (atol(&buf[4]));
48 // DAV delete an object in a room.
49 void dav_delete_message(struct http_transaction *h, struct ctdlsession *c, long msgnum) {
50 ctdl_delete_msgs(c, &msgnum, 1);
51 h->response_code = 204;
52 h->response_string = strdup("no content");
56 // GET method directly on a message in a room
57 void dav_get_message(struct http_transaction *h, struct ctdlsession *c, long msgnum) {
63 ctdl_printf(c, "MSG2 %ld", msgnum);
64 ctdl_readline(c, buf, sizeof buf);
70 char *etag = malloc(20);
72 sprintf(etag, "%ld", msgnum);
73 add_response_header(h, strdup("ETag"), etag); // http_transaction now owns this memory
76 while (ctdl_readline(c, buf, sizeof buf), strcmp(buf, "000")) {
77 if (IsEmptyStr(buf) && (in_body == 0)) {
81 else if (in_body == 0) {
83 char *v = strchr(buf, ':');
87 striplt(v); // we now have a key (k) and a value (v)
88 if ((!strcasecmp(k, "content-type")) // fields which can be passed from RFC822 to HTTP as-is
89 || (!strcasecmp(k, "date"))
91 add_response_header(h, strdup(k), strdup(v));
93 else if (!strcasecmp(k, "content-transfer-encoding")) {
94 if (!strcasecmp(v, "base64")) {
97 else if (!strcasecmp(v, "quoted-printable")) {
103 else if ((in_body == 1) && (Body != NULL)) {
104 StrBufAppendPrintf(Body, "%s\n", buf);
108 h->response_code = 200;
109 h->response_string = strdup("OK");
112 if (encoding == 'q') {
113 h->response_body = malloc(StrLength(Body));
114 if (h->response_body != NULL) {
115 h->response_body_length =
116 CtdlDecodeQuotedPrintable(h->response_body, (char *) ChrPtr(Body), StrLength(Body));
120 else if (encoding == 'b') {
121 h->response_body = malloc(StrLength(Body));
122 if (h->response_body != NULL) {
123 h->response_body_length = CtdlDecodeBase64(h->response_body, ChrPtr(Body), StrLength(Body));
128 h->response_body_length = StrLength(Body);
129 h->response_body = SmashStrBuf(&Body);
135 // PUT a message into a room
136 void dav_put_message(struct http_transaction *h, struct ctdlsession *c, char *euid, long old_msgnum) {
138 char *content_type = NULL;
142 char response_string[1024];
144 if ((h->request_body == NULL) || (h->request_body_length < 1)) {
145 do_404(h); // Refuse to post a null message
149 ctdl_printf(c, "ENT0 1|||4|||1|"); // This protocol syntax will give us metadata back after upload
150 ctdl_readline(c, buf, sizeof buf);
152 h->response_code = 502;
153 h->response_string = strdup("bad gateway");
154 add_response_header(h, strdup("Content-type"), strdup("text/plain"));
155 h->response_body = strdup(buf);
156 h->response_body_length = strlen(h->response_body);
160 // Remember, ctdl_printf() appends \n on its own, so we only need \r here.
161 content_type = header_val(h, "Content-type");
162 ctdl_printf(c, "Content-type: %s\r", (content_type ? content_type : "application/octet-stream"));
163 ctdl_printf(c, "\r");
164 ctdl_write(c, h->request_body, h->request_body_length);
165 if (h->request_body[h->request_body_length] != '\n') {
166 ctdl_printf(c, "\r");
168 ctdl_printf(c, "000");
170 // Now handle the response from the Citadel server.
174 strcpy(new_euid, "");
175 strcpy(response_string, "");
177 while (ctdl_readline(c, buf, sizeof buf), strcmp(buf, "000"))
180 new_msgnum = atol(buf);
183 safestrncpy(response_string, buf, sizeof response_string);
184 syslog(LOG_DEBUG, "new_msgnum=%ld (%s)\n", new_msgnum, buf);
187 safestrncpy(new_euid, buf, sizeof new_euid);
193 // Tell the client what happened.
195 // Citadel failed in some way?
196 char *new_location = malloc(1024);
197 if ((new_msgnum < 0L) || (new_location == NULL)) {
198 h->response_code = 502;
199 h->response_string = strdup("bad gateway");
200 add_response_header(h, strdup("Content-type"), strdup("text/plain"));
201 h->response_body = strdup(response_string);
202 h->response_body_length = strlen(h->response_body);
206 char *etag = malloc(20);
208 sprintf(etag, "%ld", new_msgnum);
209 add_response_header(h, strdup("ETag"), etag); // http_transaction now owns this memory
214 urlesc(esc_room, sizeof esc_room, c->room);
215 urlesc(esc_euid, sizeof esc_euid, new_euid);
216 snprintf(new_location, 1024, "/ctdl/r/%s/%s", esc_room, esc_euid);
217 add_response_header(h, strdup("Location"), new_location); // http_transaction now owns this memory
219 if (old_msgnum <= 0) {
220 h->response_code = 201; // We created this item for the first time.
221 h->response_string = strdup("created");
224 h->response_code = 204; // We modified an existing item.
225 h->response_string = strdup("no content");
227 // The item we replaced has probably already been deleted by
228 // the Citadel server, but we'll do this anyway, just in case.
229 ctdl_delete_msgs(c, &old_msgnum, 1);
235 // Download a single component of a MIME-encoded message
236 void download_mime_component(struct http_transaction *h, struct ctdlsession *c, long msgnum, char *partnum) {
238 char content_type[1024];
240 ctdl_printf(c, "DLAT %ld|%s", msgnum, partnum);
241 ctdl_readline(c, buf, sizeof buf);
243 do_404(h); // too bad, so sad, go away
245 // Server response is going to be: 6XX length|-1|filename|content-type|charset
246 h->response_body_length = extract_int(&buf[4], 0);
247 extract_token(content_type, buf, 3, '|', sizeof content_type);
249 h->response_body = malloc(h->response_body_length + 1);
253 thisblock = read(c->sock, &h->response_body[bytes], (h->response_body_length - bytes));
255 syslog(LOG_DEBUG, "Bytes read: %d of %d", (int) bytes, (int) h->response_body_length);
256 } while ((bytes < h->response_body_length) && (thisblock >= 0));
257 h->response_body[h->response_body_length] = 0; // null terminate it just for good measure
258 syslog(LOG_DEBUG, "content type: %s", content_type);
260 add_response_header(h, strdup("Content-type"), strdup(content_type));
261 h->response_code = 200;
262 h->response_string = strdup("OK");