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