Handle posting errors a little more elegantly.
[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.  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
9 // License version 3.
10 //
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.
15
16 #include "webcit.h"
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 long locate_message_by_uid(struct ctdlsession *c, char *uid) {
23         char buf[1024];
24
25         ctdl_printf(c, "EUID %s", uid);
26         ctdl_readline(c, buf, sizeof buf);
27         if (buf[0] == '2') {
28                 return (atol(&buf[4]));
29
30         }
31
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);
38                 if (buf[0] == '2') {
39                         return (atol(&buf[4]));
40
41                 }
42         }
43
44         return (-1);
45 }
46
47
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");
53 }
54
55
56 // GET method directly on a message in a room
57 void dav_get_message(struct http_transaction *h, struct ctdlsession *c, long msgnum) {
58         char buf[1024];
59         int in_body = 0;
60         int encoding = 0;
61         StrBuf *Body = NULL;
62
63         ctdl_printf(c, "MSG2 %ld", msgnum);
64         ctdl_readline(c, buf, sizeof buf);
65         if (buf[0] != '1') {
66                 do_404(h);
67                 return;
68         }
69
70         char *etag = malloc(20);
71         if (etag != NULL) {
72                 sprintf(etag, "%ld", msgnum);
73                 add_response_header(h, strdup("ETag"), etag);   // http_transaction now owns this memory
74         }
75
76         while (ctdl_readline(c, buf, sizeof buf), strcmp(buf, "000")) {
77                 if (IsEmptyStr(buf) && (in_body == 0)) {
78                         in_body = 1;
79                         Body = NewStrBuf();
80                 } else if (in_body == 0) {
81                         char *k = buf;
82                         char *v = strchr(buf, ':');
83                         if (v) {
84                                 *v = 0;
85                                 ++v;
86                                 striplt(v);     // we now have a key (k) and a value (v)
87                                 if ((!strcasecmp(k, "content-type"))    // fields which can be passed from RFC822 to HTTP as-is
88                                     || (!strcasecmp(k, "date"))
89                                     ) {
90                                         add_response_header(h, strdup(k), strdup(v));
91                                 } else if (!strcasecmp(k, "content-transfer-encoding")) {
92                                         if (!strcasecmp(v, "base64")) {
93                                                 encoding = 'b';
94                                         } else if (!strcasecmp(v, "quoted-printable")) {
95                                                 encoding = 'q';
96                                         }
97                                 }
98                         }
99                 } else if ((in_body == 1) && (Body != NULL)) {
100                         StrBufAppendPrintf(Body, "%s\n", buf);
101                 }
102         }
103
104         h->response_code = 200;
105         h->response_string = strdup("OK");
106
107         if (Body != NULL) {
108                 if (encoding == 'q') {
109                         h->response_body = malloc(StrLength(Body));
110                         if (h->response_body != NULL) {
111                                 h->response_body_length =
112                                     CtdlDecodeQuotedPrintable(h->response_body, (char *) ChrPtr(Body), StrLength(Body));
113                         }
114                         FreeStrBuf(&Body);
115                 } else if (encoding == 'b') {
116                         h->response_body = malloc(StrLength(Body));
117                         if (h->response_body != NULL) {
118                                 h->response_body_length = CtdlDecodeBase64(h->response_body, ChrPtr(Body), StrLength(Body));
119                         }
120                         FreeStrBuf(&Body);
121                 } else {
122                         h->response_body_length = StrLength(Body);
123                         h->response_body = SmashStrBuf(&Body);
124                 }
125         }
126 }
127
128
129 // PUT a message into a room
130 void dav_put_message(struct http_transaction *h, struct ctdlsession *c, char *euid, long old_msgnum) {
131         char buf[1024];
132         char *content_type = NULL;
133         int n;
134         long new_msgnum;
135         char new_euid[1024];
136         char response_string[1024];
137
138         if ((h->request_body == NULL) || (h->request_body_length < 1)) {
139                 do_404(h);                      // Refuse to post a null message
140                 return;
141         }
142
143         ctdl_printf(c, "ENT0 1|||4|||1|");      // This protocol syntax will give us metadata back after upload
144         ctdl_readline(c, buf, sizeof buf);
145         if (buf[0] != '8') {
146                 h->response_code = 502;
147                 h->response_string = strdup("bad gateway");
148                 add_response_header(h, strdup("Content-type"), strdup("text/plain"));
149                 h->response_body = strdup(buf);
150                 h->response_body_length = strlen(h->response_body);
151                 return;
152         }
153
154         content_type = header_val(h, "Content-type");
155         ctdl_printf(c, "Content-type: %s\r\n", (content_type ? content_type : "application/octet-stream"));
156         ctdl_printf(c, "\r\n");
157         ctdl_write(c, h->request_body, h->request_body_length);
158         if (h->request_body[h->request_body_length] != '\n') {
159                 ctdl_printf(c, "\r\n");
160         }
161         ctdl_printf(c, "000");
162
163         // Now handle the response from the Citadel server.
164
165         n = 0;
166         new_msgnum = 0;
167         strcpy(new_euid, "");
168         strcpy(response_string, "");
169
170         while (ctdl_readline(c, buf, sizeof buf), strcmp(buf, "000"))
171                 switch (n++) {
172                 case 0:
173                         new_msgnum = atol(buf);
174                         break;
175                 case 1:
176                         safestrncpy(response_string, buf, sizeof response_string);
177                         syslog(LOG_DEBUG, "new_msgnum=%ld (%s)\n", new_msgnum, buf);
178                         break;
179                 case 2:
180                         safestrncpy(new_euid, buf, sizeof new_euid);
181                         break;
182                 default:
183                         break;
184                 }
185
186         // Tell the client what happened.
187
188         // Citadel failed in some way?
189         char *new_location = malloc(1024);
190         if ((new_msgnum < 0L) || (new_location == NULL)) {
191                 h->response_code = 502;
192                 h->response_string = strdup("bad gateway");
193                 add_response_header(h, strdup("Content-type"), strdup("text/plain"));
194                 h->response_body = strdup(response_string);
195                 h->response_body_length = strlen(h->response_body);
196                 return;
197         }
198
199         char *etag = malloc(20);
200         if (etag != NULL) {
201                 sprintf(etag, "%ld", new_msgnum);
202                 add_response_header(h, strdup("ETag"), etag);   // http_transaction now owns this memory
203         }
204
205         char esc_room[1024];
206         char esc_euid[1024];
207         urlesc(esc_room, sizeof esc_room, c->room);
208         urlesc(esc_euid, sizeof esc_euid, new_euid);
209         snprintf(new_location, 1024, "/ctdl/r/%s/%s", esc_room, esc_euid);
210         add_response_header(h, strdup("Location"), new_location);       // http_transaction now owns this memory
211
212         if (old_msgnum <= 0) {
213                 h->response_code = 201; // We created this item for the first time.
214                 h->response_string = strdup("created");
215         } else {
216                 h->response_code = 204; // We modified an existing item.
217                 h->response_string = strdup("no content");
218
219                 // The item we replaced has probably already been deleted by
220                 // the Citadel server, but we'll do this anyway, just in case.
221                 ctdl_delete_msgs(c, &old_msgnum, 1);
222         }
223
224 }
225
226
227 // Download a single component of a MIME-encoded message
228 void download_mime_component(struct http_transaction *h, struct ctdlsession *c, long msgnum, char *partnum) {
229         char buf[1024];
230         char content_type[1024];
231
232         ctdl_printf(c, "DLAT %ld|%s", msgnum, partnum);
233         ctdl_readline(c, buf, sizeof buf);
234         if (buf[0] != '6') {
235                 do_404(h);      // too bad, so sad, go away
236         }
237         // Server response is going to be: 6XX length|-1|filename|content-type|charset
238         h->response_body_length = extract_int(&buf[4], 0);
239         extract_token(content_type, buf, 3, '|', sizeof content_type);
240
241         h->response_body = malloc(h->response_body_length + 1);
242         int bytes = 0;
243         int thisblock;
244         do {
245                 thisblock = read(c->sock, &h->response_body[bytes], (h->response_body_length - bytes));
246                 bytes += thisblock;
247                 syslog(LOG_DEBUG, "Bytes read: %d of %d", (int) bytes, (int) h->response_body_length);
248         } while ((bytes < h->response_body_length) && (thisblock >= 0));
249         h->response_body[h->response_body_length] = 0;  // null terminate it just for good measure
250         syslog(LOG_DEBUG, "content type: %s", content_type);
251
252         add_response_header(h, strdup("Content-type"), strdup(content_type));
253         h->response_code = 200;
254         h->response_string = strdup("OK");
255 }