fa0ada03d4d012007b95f2ec723865c98fa764af
[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         char buf[1024];
26
27         ctdl_printf(c, "EUID %s", uid);
28         ctdl_readline(c, buf, sizeof buf);
29         if (buf[0] == '2') {
30                 return(atol(&buf[4]));
31                 
32         }
33
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);
40                 if (buf[0] == '2') {
41                         return(atol(&buf[4]));
42                         
43                 }
44         }
45
46         return(-1);
47 }
48
49
50 /*
51  * DAV delete an object in a room.
52  */
53 void dav_delete_message(struct http_transaction *h, struct ctdlsession *c, long msgnum)
54 {
55         ctdl_delete_msgs(c, &msgnum, 1);
56         h->response_code = 204;
57         h->response_string = strdup("no content");
58 }
59
60
61 /*
62  * GET method directly on a message in a room
63  */
64 void dav_get_message(struct http_transaction *h, struct ctdlsession *c, long msgnum)
65 {
66         char buf[1024];
67         int in_body = 0;
68         int encoding = 0;
69         StrBuf *Body = NULL;
70
71         ctdl_printf(c, "MSG2 %ld", msgnum);
72         ctdl_readline(c, buf, sizeof buf);
73         if (buf[0] != '1') {
74                 do_404(h);
75                 return;
76         }
77
78         char *etag = malloc(20);
79         if (etag != NULL) {
80                 sprintf(etag, "%ld", msgnum);
81                 add_response_header(h, strdup("ETag"), etag);                   // http_transaction now owns this memory
82         }
83
84         while (ctdl_readline(c, buf, sizeof buf), strcmp(buf,"000"))
85         {
86                 if (IsEmptyStr(buf) && (in_body == 0)) {
87                         in_body = 1;
88                         Body = NewStrBuf();
89                 }
90                 else if (in_body == 0) {
91                         char *k = buf;
92                         char *v = strchr(buf, ':');
93                         if (v) {
94                                 *v = 0;
95                                 ++v;
96                                 striplt(v);                                     // we now have a key (k) and a value (v)
97                                 if (
98                                         (!strcasecmp(k, "content-type"))        // fields which can be passed from RFC822 to HTTP as-is
99                                         || (!strcasecmp(k, "date"))
100                                 ) {
101                                         add_response_header(h, strdup(k), strdup(v));
102                                 }
103                                 else if (!strcasecmp(k, "content-transfer-encoding")) {
104                                         if (!strcasecmp(v, "base64")) {
105                                                 encoding = 'b';
106                                         }
107                                         else if (!strcasecmp(v, "quoted-printable")) {
108                                                 encoding = 'q';
109                                         }
110                                 }
111                         }
112                 }
113                 else if ( (in_body == 1) && (Body != NULL) ) {
114                         StrBufAppendPrintf(Body, "%s\n", buf);
115                 }
116         }
117
118         h->response_code = 200;
119         h->response_string = strdup("OK");
120
121         if (Body != NULL) {
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));
126                         }
127                         FreeStrBuf(&Body);
128                 }
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));
133                         }
134                         FreeStrBuf(&Body);
135                 }
136                 else {
137                         h->response_body_length = StrLength(Body);
138                         h->response_body = SmashStrBuf(&Body);
139                 }
140         }
141 }
142
143
144 /*
145  * PUT a message into a room
146  */
147 void dav_put_message(struct http_transaction *h, struct ctdlsession *c, char *euid, long old_msgnum)
148 {
149         char buf[1024];
150         char *content_type = NULL;
151         int n;
152         long new_msgnum;
153         char new_euid[1024];
154         char response_string[1024];
155
156         if ( (h->request_body == NULL) || (h->request_body_length < 1) ) {
157                 do_404(h);                              // Refuse to post a null message
158                 return;
159         }
160
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);
163         if (buf[0] != '8') {
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);
169                 return;
170         }
171
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");
178         }
179         ctdl_printf(c, "000");
180
181         /*
182          * Now handle the response from the Citadel server.
183          */
184         
185         n = 0;
186         new_msgnum = 0;
187         strcpy(new_euid, "");
188         strcpy(response_string, "");
189
190         while (ctdl_readline(c, buf, sizeof buf), strcmp(buf, "000")) switch(n++) {
191                 case 0:
192                         new_msgnum = atol(buf);
193                         break;
194                 case 1:
195                         safestrncpy(response_string, buf, sizeof response_string);
196                         syslog(LOG_DEBUG, "new_msgnum=%ld (%s)\n", new_msgnum, buf);
197                         break;
198                 case 2:
199                         safestrncpy(new_euid, buf, sizeof new_euid);
200                         break;
201                 default:
202                         break;
203         }
204
205         /* Tell the client what happened. */
206
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);
215                 return;
216         }
217
218         char *etag = malloc(20);
219         if (etag != NULL) {
220                 sprintf(etag, "%ld", new_msgnum);
221                 add_response_header(h, strdup("ETag"), etag);                   // http_transaction now owns this memory
222         }
223
224         char esc_room[1024];
225         char esc_euid[1024];
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
230
231         if (old_msgnum <= 0) {
232                 h->response_code = 201;                                         // We created this item for the first time.
233                 h->response_string = strdup("created");
234         }
235         else {
236                 h->response_code = 204;                                         // We modified an existing item.
237                 h->response_string = strdup("no content");
238         
239                 /* The item we replaced has probably already been deleted by
240                  * the Citadel server, but we'll do this anyway, just in case.
241                  */
242                 ctdl_delete_msgs(c, &old_msgnum, 1);
243         }
244
245 }
246
247
248 /*
249  * Download a single component of a MIME-encoded message
250  */
251 void download_mime_component(struct http_transaction *h, struct ctdlsession *c, long msgnum, char *partnum)
252 {
253         char buf[1024];
254         char content_type[1024];
255
256         ctdl_printf(c, "DLAT %ld|%s", msgnum, partnum);
257         ctdl_readline(c, buf, sizeof buf);
258         if (buf[0] != '6') {
259                 do_404(h);                                                      // too bad, so sad, go away
260         }
261
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);
265
266         h->response_body = malloc(h->response_body_length + 1);
267         int bytes = 0;
268         int thisblock;
269         do {
270                 thisblock = read(c->sock, &h->response_body[bytes], (h->response_body_length - bytes));
271                 bytes += thisblock;
272                 syslog(LOG_DEBUG, "Bytes read: %d of %d", (int)bytes, (int)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);
276
277         add_response_header(h, strdup("Content-type"), strdup(content_type));
278         h->response_code = 200;
279         h->response_string = strdup("OK");
280 }