striplt() is now string_trim()
[citadel.git] / webcit-ng / server / messages.c
1 //
2 // Message base functions
3 //
4 // Copyright (c) 1996-2022 by the citadel.org team
5 //
6 // This program is open source software.  Use, duplication, or
7 // disclosure are subject to the GNU General Public License v3.
8
9 #include "webcit.h"
10
11
12 // Given an encoded UID, translate that to an unencoded Citadel EUID and
13 // then search for it in the current room.  Return a message number or -1
14 // if not found.
15 long locate_message_by_uid(struct ctdlsession *c, char *uid) {
16         char buf[1024];
17
18         ctdl_printf(c, "EUID %s", uid);
19         ctdl_readline(c, buf, sizeof buf);
20         if (buf[0] == '2') {
21                 return (atol(&buf[4]));
22
23         }
24
25         // Ugly hack to handle Mozilla Thunderbird, try stripping ".ics" if present
26         if (!strcasecmp(&uid[strlen(uid) - 4], ".ics")) {
27                 safestrncpy(buf, uid, sizeof buf);
28                 buf[strlen(buf) - 4] = 0;
29                 ctdl_printf(c, "EUID %s", buf);
30                 ctdl_readline(c, buf, sizeof buf);
31                 if (buf[0] == '2') {
32                         return (atol(&buf[4]));
33
34                 }
35         }
36
37         return (-1);
38 }
39
40
41 // DAV delete an object in a room.
42 void dav_delete_message(struct http_transaction *h, struct ctdlsession *c, long msgnum) {
43         ctdl_delete_msgs(c, &msgnum, 1);
44         h->response_code = 204;
45         h->response_string = strdup("no content");
46 }
47
48
49 // GET method directly on a message in a room
50 void dav_get_message(struct http_transaction *h, struct ctdlsession *c, long msgnum) {
51         char buf[1024];
52         int in_body = 0;
53         int encoding = 0;
54         StrBuf *Body = NULL;
55
56         ctdl_printf(c, "MSG2 %ld", msgnum);
57         ctdl_readline(c, buf, sizeof buf);
58         if (buf[0] != '1') {
59                 do_404(h);
60                 return;
61         }
62
63         char *etag = malloc(20);
64         if (etag != NULL) {
65                 sprintf(etag, "%ld", msgnum);
66                 add_response_header(h, strdup("ETag"), etag);   // http_transaction now owns this memory
67         }
68
69         while (ctdl_readline(c, buf, sizeof buf), strcmp(buf, "000")) {
70                 if (IsEmptyStr(buf) && (in_body == 0)) {
71                         in_body = 1;
72                         Body = NewStrBuf();
73                 }
74                 else if (in_body == 0) {
75                         char *k = buf;
76                         char *v = strchr(buf, ':');
77                         if (v) {
78                                 *v = 0;
79                                 ++v;
80                                 string_trim(v); // we now have a key (k) and a value (v)
81                                 if ((!strcasecmp(k, "content-type"))    // fields which can be passed from RFC822 to HTTP as-is
82                                     || (!strcasecmp(k, "date"))
83                                 ) {
84                                         add_response_header(h, strdup(k), strdup(v));
85                                 }
86                                 else if (!strcasecmp(k, "content-transfer-encoding")) {
87                                         if (!strcasecmp(v, "base64")) {
88                                                 encoding = 'b';
89                                         }
90                                         else if (!strcasecmp(v, "quoted-printable")) {
91                                                 encoding = 'q';
92                                         }
93                                 }
94                         }
95                 }
96                 else if ((in_body == 1) && (Body != NULL)) {
97                         StrBufAppendPrintf(Body, "%s\n", buf);
98                 }
99         }
100
101         h->response_code = 200;
102         h->response_string = strdup("OK");
103
104         if (Body != NULL) {
105                 if (encoding == 'q') {
106                         h->response_body = malloc(StrLength(Body));
107                         if (h->response_body != NULL) {
108                                 h->response_body_length =
109                                     CtdlDecodeQuotedPrintable(h->response_body, (char *) ChrPtr(Body), StrLength(Body));
110                         }
111                         FreeStrBuf(&Body);
112                 }
113                 else if (encoding == 'b') {
114                         h->response_body = malloc(StrLength(Body));
115                         if (h->response_body != NULL) {
116                                 h->response_body_length = CtdlDecodeBase64(h->response_body, ChrPtr(Body), StrLength(Body));
117                         }
118                         FreeStrBuf(&Body);
119                 }
120                 else {
121                         h->response_body_length = StrLength(Body);
122                         h->response_body = SmashStrBuf(&Body);
123                 }
124         }
125 }
126
127
128 // PUT a message into a room
129 void dav_put_message(struct http_transaction *h, struct ctdlsession *c, char *euid, long old_msgnum) {
130         char buf[1024];
131         char *content_type = NULL;
132         int n;
133         long new_msgnum;
134         char new_euid[1024];
135         char response_string[1024];
136
137         if ((h->request_body == NULL) || (h->request_body_length < 1)) {
138                 do_404(h);                              // Refuse to post a null message
139                 return;
140         }
141
142         // Extract metadata from the URL
143         char *wefw = get_url_param(h, "wefw");          // References:
144         if (!wefw) wefw = "";
145         char *subj = get_url_param(h, "subj");          // Subject:
146         if (!subj) subj = "";
147         char *mailto = get_url_param(h, "mailto");      // To:
148         if (!mailto) mailto = "";
149         char *mailcc = get_url_param(h, "mailcc");      // Cc:
150         if (!mailcc) mailcc = "";
151         char *mailbcc = get_url_param(h, "mailbcc");    // Bcc:
152         if (!mailbcc) mailbcc = "";
153
154         // Mode 4 will give us metadata back after upload
155         ctdl_printf(c, "ENT0 1|%s||4|%s||1|%s|%s|||%s|", mailto, subj, mailcc, mailbcc, wefw);
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         // Remember, ctdl_printf() appends \n on its own, so when adding a CRLF newline, only use \r
167         // Or for a blank line, use ctdl_write() with \r\n
168
169         content_type = header_val(h, "Content-type");
170         ctdl_printf(c, "Content-type: %s\r", (content_type ? content_type : "application/octet-stream"));
171         ctdl_write(c, HKEY("\r\n"));
172         ctdl_write(c, h->request_body, h->request_body_length);
173         if (h->request_body[h->request_body_length] != '\n') {
174                 ctdl_write(c, HKEY("\r\n"));
175         }
176         ctdl_printf(c, "000");
177
178         // Now handle the response from the Citadel server.
179
180         n = 0;
181         new_msgnum = 0;
182         strcpy(new_euid, "");
183         strcpy(response_string, "");
184
185         while (ctdl_readline(c, buf, sizeof buf), strcmp(buf, "000"))
186                 switch (n++) {
187                 case 0:
188                         new_msgnum = atol(buf);
189                         break;
190                 case 1:
191                         safestrncpy(response_string, buf, sizeof response_string);
192                         syslog(LOG_DEBUG, "new_msgnum=%ld (%s)\n", new_msgnum, buf);
193                         break;
194                 case 2:
195                         safestrncpy(new_euid, buf, sizeof new_euid);
196                         break;
197                 default:
198                         break;
199                 }
200
201         // Tell the client what happened.
202
203         // Citadel failed in some way?
204         char *new_location = malloc(1024);
205         if ((new_msgnum < 0L) || (new_location == NULL)) {
206                 h->response_code = 502;
207                 h->response_string = strdup("bad gateway");
208                 add_response_header(h, strdup("Content-type"), strdup("text/plain"));
209                 h->response_body = strdup(response_string);
210                 h->response_body_length = strlen(h->response_body);
211                 return;
212         }
213
214         char *etag = malloc(20);
215         if (etag != NULL) {
216                 sprintf(etag, "%ld", new_msgnum);
217                 add_response_header(h, strdup("ETag"), etag);   // http_transaction now owns this memory
218         }
219
220         char esc_room[1024];
221         char esc_euid[1024];
222         urlesc(esc_room, sizeof esc_room, c->room);
223         urlesc(esc_euid, sizeof esc_euid, new_euid);
224         snprintf(new_location, 1024, "/ctdl/r/%s/%s", esc_room, esc_euid);
225         add_response_header(h, strdup("Location"), new_location);       // http_transaction now owns this memory
226
227         if (old_msgnum <= 0) {
228                 h->response_code = 201; // We created this item for the first time.
229                 h->response_string = strdup("created");
230         }
231         else {
232                 h->response_code = 204; // We modified an existing item.
233                 h->response_string = strdup("no content");
234
235                 // The item we replaced has probably already been deleted by
236                 // the Citadel server, but we'll do this anyway, just in case.
237                 ctdl_delete_msgs(c, &old_msgnum, 1);
238         }
239
240 }
241
242
243 // Download a single component of a MIME-encoded message
244 void download_mime_component(struct http_transaction *h, struct ctdlsession *c, long msgnum, char *partnum) {
245         char buf[1024];
246         char content_type[1024];
247
248         ctdl_printf(c, "DLAT %ld|%s", msgnum, partnum);
249         ctdl_readline(c, buf, sizeof buf);
250         if (buf[0] != '6') {
251                 do_404(h);      // too bad, so sad, go away
252         }
253         // Server response is going to be: 6XX length|-1|filename|content-type|charset
254         h->response_body_length = extract_int(&buf[4], 0);
255         extract_token(content_type, buf, 3, '|', sizeof content_type);
256
257         h->response_body = malloc(h->response_body_length + 1);
258         int bytes = 0;
259         int thisblock;
260         do {
261                 thisblock = read(c->sock, &h->response_body[bytes], (h->response_body_length - bytes));
262                 bytes += thisblock;
263                 syslog(LOG_DEBUG, "Bytes read: %d of %d", (int) bytes, (int) h->response_body_length);
264         } while ((bytes < h->response_body_length) && (thisblock >= 0));
265         h->response_body[h->response_body_length] = 0;  // null terminate it just for good measure
266         syslog(LOG_DEBUG, "content type: %s", content_type);
267
268         add_response_header(h, strdup("Content-type"), strdup(content_type));
269         h->response_code = 200;
270         h->response_string = strdup("OK");
271 }