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