indent -kr -i8 -l132 on everything in webcit-ng
[citadel.git] / webcit-ng / messages.c
1 /*
2  * Message base functions
3  *
4  * Copyright (c) 1996-2018 by the citadel.org team
5  *
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.
8  *
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.
13  */
14
15 #include "webcit.h"
16
17
18 /*
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
21  * if not found.
22  *
23  */
24 long locate_message_by_uid(struct ctdlsession *c, char *uid)
25 {
26         char buf[1024];
27
28         ctdl_printf(c, "EUID %s", uid);
29         ctdl_readline(c, buf, sizeof buf);
30         if (buf[0] == '2') {
31                 return (atol(&buf[4]));
32
33         }
34
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);
41                 if (buf[0] == '2') {
42                         return (atol(&buf[4]));
43
44                 }
45         }
46
47         return (-1);
48 }
49
50
51 /*
52  * DAV delete an object in a room.
53  */
54 void dav_delete_message(struct http_transaction *h, struct ctdlsession *c, long msgnum)
55 {
56         ctdl_delete_msgs(c, &msgnum, 1);
57         h->response_code = 204;
58         h->response_string = strdup("no content");
59 }
60
61
62 /*
63  * GET method directly on a message in a room
64  */
65 void dav_get_message(struct http_transaction *h, struct ctdlsession *c, long msgnum)
66 {
67         char buf[1024];
68         int in_body = 0;
69         int encoding = 0;
70         StrBuf *Body = NULL;
71
72         ctdl_printf(c, "MSG2 %ld", msgnum);
73         ctdl_readline(c, buf, sizeof buf);
74         if (buf[0] != '1') {
75                 do_404(h);
76                 return;
77         }
78
79         char *etag = malloc(20);
80         if (etag != NULL) {
81                 sprintf(etag, "%ld", msgnum);
82                 add_response_header(h, strdup("ETag"), etag);   // http_transaction now owns this memory
83         }
84
85         while (ctdl_readline(c, buf, sizeof buf), strcmp(buf, "000")) {
86                 if (IsEmptyStr(buf) && (in_body == 0)) {
87                         in_body = 1;
88                         Body = NewStrBuf();
89                 } else if (in_body == 0) {
90                         char *k = buf;
91                         char *v = strchr(buf, ':');
92                         if (v) {
93                                 *v = 0;
94                                 ++v;
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"))
98                                     ) {
99                                         add_response_header(h, strdup(k), strdup(v));
100                                 } else if (!strcasecmp(k, "content-transfer-encoding")) {
101                                         if (!strcasecmp(v, "base64")) {
102                                                 encoding = 'b';
103                                         } else if (!strcasecmp(v, "quoted-printable")) {
104                                                 encoding = 'q';
105                                         }
106                                 }
107                         }
108                 } else if ((in_body == 1) && (Body != NULL)) {
109                         StrBufAppendPrintf(Body, "%s\n", buf);
110                 }
111         }
112
113         h->response_code = 200;
114         h->response_string = strdup("OK");
115
116         if (Body != NULL) {
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));
122                         }
123                         FreeStrBuf(&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));
128                         }
129                         FreeStrBuf(&Body);
130                 } else {
131                         h->response_body_length = StrLength(Body);
132                         h->response_body = SmashStrBuf(&Body);
133                 }
134         }
135 }
136
137
138 /*
139  * PUT a message into a room
140  */
141 void dav_put_message(struct http_transaction *h, struct ctdlsession *c, char *euid, long old_msgnum)
142 {
143         char buf[1024];
144         char *content_type = NULL;
145         int n;
146         long new_msgnum;
147         char new_euid[1024];
148         char response_string[1024];
149
150         if ((h->request_body == NULL) || (h->request_body_length < 1)) {
151                 do_404(h);      // Refuse to post a null message
152                 return;
153         }
154
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);
157         if (buf[0] != '8') {
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);
163                 return;
164         }
165
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");
172         }
173         ctdl_printf(c, "000");
174
175         /*
176          * Now handle the response from the Citadel server.
177          */
178
179         n = 0;
180         new_msgnum = 0;
181         strcpy(new_euid, "");
182         strcpy(response_string, "");
183
184         while (ctdl_readline(c, buf, sizeof buf), strcmp(buf, "000"))
185                 switch (n++) {
186                 case 0:
187                         new_msgnum = atol(buf);
188                         break;
189                 case 1:
190                         safestrncpy(response_string, buf, sizeof response_string);
191                         syslog(LOG_DEBUG, "new_msgnum=%ld (%s)\n", new_msgnum, buf);
192                         break;
193                 case 2:
194                         safestrncpy(new_euid, buf, sizeof new_euid);
195                         break;
196                 default:
197                         break;
198                 }
199
200         /* Tell the client what happened. */
201
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);
210                 return;
211         }
212
213         char *etag = malloc(20);
214         if (etag != NULL) {
215                 sprintf(etag, "%ld", new_msgnum);
216                 add_response_header(h, strdup("ETag"), etag);   // http_transaction now owns this memory
217         }
218
219         char esc_room[1024];
220         char esc_euid[1024];
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
225
226         if (old_msgnum <= 0) {
227                 h->response_code = 201; // We created this item for the first time.
228                 h->response_string = strdup("created");
229         } else {
230                 h->response_code = 204; // We modified an existing item.
231                 h->response_string = strdup("no content");
232
233                 /* The item we replaced has probably already been deleted by
234                  * the Citadel server, but we'll do this anyway, just in case.
235                  */
236                 ctdl_delete_msgs(c, &old_msgnum, 1);
237         }
238
239 }
240
241
242 /*
243  * Download a single component of a MIME-encoded message
244  */
245 void download_mime_component(struct http_transaction *h, struct ctdlsession *c, long msgnum, char *partnum)
246 {
247         char buf[1024];
248         char content_type[1024];
249
250         ctdl_printf(c, "DLAT %ld|%s", msgnum, partnum);
251         ctdl_readline(c, buf, sizeof buf);
252         if (buf[0] != '6') {
253                 do_404(h);      // too bad, so sad, go away
254         }
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);
258
259         h->response_body = malloc(h->response_body_length + 1);
260         int bytes = 0;
261         int thisblock;
262         do {
263                 thisblock = read(c->sock, &h->response_body[bytes], (h->response_body_length - bytes));
264                 bytes += thisblock;
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);
269
270         add_response_header(h, strdup("Content-type"), strdup(content_type));
271         h->response_code = 200;
272         h->response_string = strdup("OK");
273 }