dav_move_or_copy_message() skeleton
[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         do_404(h);
52 }
53
54
55 // GET method directly on a message in a room
56 void dav_get_message(struct http_transaction *h, struct ctdlsession *c, long msgnum) {
57         char buf[1024];
58         int in_body = 0;
59         int encoding = 0;
60         StrBuf *Body = NULL;
61
62         ctdl_printf(c, "MSG2 %ld", msgnum);
63         ctdl_readline(c, buf, sizeof buf);
64         if (buf[0] != '1') {
65                 do_404(h);
66                 return;
67         }
68
69         char *etag = malloc(20);
70         if (etag != NULL) {
71                 sprintf(etag, "%ld", msgnum);
72                 add_response_header(h, strdup("ETag"), etag);   // http_transaction now owns this memory
73         }
74
75         while (ctdl_readline(c, buf, sizeof buf), strcmp(buf, "000")) {
76                 if (IsEmptyStr(buf) && (in_body == 0)) {
77                         in_body = 1;
78                         Body = NewStrBuf();
79                 }
80                 else if (in_body == 0) {
81                         char *k = buf;
82                         char *v = strchr(buf, ':');
83                         if (v) {
84                                 *v = 0;
85                                 ++v;
86                                 string_trim(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                                 }
92                                 else if (!strcasecmp(k, "content-transfer-encoding")) {
93                                         if (!strcasecmp(v, "base64")) {
94                                                 encoding = 'b';
95                                         }
96                                         else if (!strcasecmp(v, "quoted-printable")) {
97                                                 encoding = 'q';
98                                         }
99                                 }
100                         }
101                 }
102                 else if ((in_body == 1) && (Body != NULL)) {
103                         StrBufAppendPrintf(Body, "%s\n", buf);
104                 }
105         }
106
107         h->response_code = 200;
108         h->response_string = strdup("OK");
109
110         if (Body != NULL) {
111                 if (encoding == 'q') {
112                         h->response_body = malloc(StrLength(Body));
113                         if (h->response_body != NULL) {
114                                 h->response_body_length =
115                                     CtdlDecodeQuotedPrintable(h->response_body, (char *) ChrPtr(Body), StrLength(Body));
116                         }
117                         FreeStrBuf(&Body);
118                 }
119                 else if (encoding == 'b') {
120                         h->response_body = malloc(StrLength(Body));
121                         if (h->response_body != NULL) {
122                                 h->response_body_length = CtdlDecodeBase64(h->response_body, ChrPtr(Body), StrLength(Body));
123                         }
124                         FreeStrBuf(&Body);
125                 }
126                 else {
127                         h->response_body_length = StrLength(Body);
128                         h->response_body = SmashStrBuf(&Body);
129                 }
130         }
131 }
132
133
134 // PUT a message into a room
135 void dav_put_message(struct http_transaction *h, struct ctdlsession *c, char *euid, long old_msgnum) {
136         char buf[1024];
137         char *content_type = NULL;
138         int n;
139         long new_msgnum;
140         char new_euid[1024];
141         char response_string[1024];
142
143         if ((h->request_body == NULL) || (h->request_body_length < 1)) {
144                 do_404(h);                              // Refuse to post a null message
145                 return;
146         }
147
148         // Extract metadata from the URL
149         char *wefw = get_url_param(h, "wefw");          // References:
150         if (!wefw) wefw = "";
151         char *subj = get_url_param(h, "subj");          // Subject:
152         if (!subj) subj = "";
153         char *mailto = get_url_param(h, "mailto");      // To:
154         if (!mailto) mailto = "";
155         char *mailcc = get_url_param(h, "mailcc");      // Cc:
156         if (!mailcc) mailcc = "";
157         char *mailbcc = get_url_param(h, "mailbcc");    // Bcc:
158         if (!mailbcc) mailbcc = "";
159
160         // Mode 4 will give us metadata back after upload
161         ctdl_printf(c, "ENT0 1|%s||4|%s||1|%s|%s|||%s|", mailto, subj, mailcc, mailbcc, wefw);
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         // Remember, ctdl_printf() appends \n on its own, so when adding a CRLF newline, only use \r
173         // Or for a blank line, use ctdl_write() with \r\n
174
175         content_type = header_val(h, "Content-type");
176         ctdl_printf(c, "Content-type: %s\r", (content_type ? content_type : "application/octet-stream"));
177         ctdl_write(c, HKEY("\r\n"));
178         ctdl_write(c, h->request_body, h->request_body_length);
179         if (h->request_body[h->request_body_length] != '\n') {
180                 ctdl_write(c, HKEY("\r\n"));
181         }
182         ctdl_printf(c, "000");
183
184         // Now handle the response from the Citadel server.
185
186         n = 0;
187         new_msgnum = 0;
188         strcpy(new_euid, "");
189         strcpy(response_string, "");
190
191         while (ctdl_readline(c, buf, sizeof buf), strcmp(buf, "000"))
192                 switch (n++) {
193                 case 0:
194                         new_msgnum = atol(buf);
195                         break;
196                 case 1:
197                         safestrncpy(response_string, buf, sizeof response_string);
198                         syslog(LOG_DEBUG, "new_msgnum=%ld (%s)\n", new_msgnum, buf);
199                         break;
200                 case 2:
201                         safestrncpy(new_euid, buf, sizeof new_euid);
202                         break;
203                 default:
204                         break;
205                 }
206
207         // Tell the client what happened.
208
209         // Citadel failed in some way?
210         char *new_location = malloc(1024);
211         if ((new_msgnum < 0L) || (new_location == NULL)) {
212                 h->response_code = 502;
213                 h->response_string = strdup("bad gateway");
214                 add_response_header(h, strdup("Content-type"), strdup("text/plain"));
215                 h->response_body = strdup(response_string);
216                 h->response_body_length = strlen(h->response_body);
217                 return;
218         }
219
220         char *etag = malloc(20);
221         if (etag != NULL) {
222                 sprintf(etag, "%ld", new_msgnum);
223                 add_response_header(h, strdup("ETag"), etag);   // http_transaction now owns this memory
224         }
225
226         char esc_room[1024];
227         char esc_euid[1024];
228         urlesc(esc_room, sizeof esc_room, c->room);
229         urlesc(esc_euid, sizeof esc_euid, new_euid);
230         snprintf(new_location, 1024, "/ctdl/r/%s/%s", esc_room, esc_euid);
231         add_response_header(h, strdup("Location"), new_location);       // http_transaction now owns this memory
232
233         if (old_msgnum <= 0) {
234                 h->response_code = 201; // We created this item for the first time.
235                 h->response_string = strdup("created");
236         }
237         else {
238                 h->response_code = 204; // We modified an existing item.
239                 h->response_string = strdup("no content");
240
241                 // The item we replaced has probably already been deleted by
242                 // the Citadel server, but we'll do this anyway, just in case.
243                 ctdl_delete_msgs(c, &old_msgnum, 1);
244         }
245
246 }
247
248
249 // Download a single component of a MIME-encoded message
250 void download_mime_component(struct http_transaction *h, struct ctdlsession *c, long msgnum, char *partnum) {
251         char buf[1024];
252         char content_type[1024];
253
254         ctdl_printf(c, "DLAT %ld|%s", msgnum, partnum);
255         ctdl_readline(c, buf, sizeof buf);
256         if (buf[0] != '6') {
257                 do_404(h);      // too bad, so sad, go away
258         }
259         // Server response is going to be: 6XX length|-1|filename|content-type|charset
260         h->response_body_length = extract_int(&buf[4], 0);
261         extract_token(content_type, buf, 3, '|', sizeof content_type);
262
263         h->response_body = malloc(h->response_body_length + 1);
264         int bytes = 0;
265         int thisblock;
266         do {
267                 thisblock = read(c->sock, &h->response_body[bytes], (h->response_body_length - bytes));
268                 bytes += thisblock;
269                 syslog(LOG_DEBUG, "Bytes read: %d of %d", (int) bytes, (int) h->response_body_length);
270         } while ((bytes < h->response_body_length) && (thisblock >= 0));
271         h->response_body[h->response_body_length] = 0;  // null terminate it just for good measure
272         syslog(LOG_DEBUG, "content type: %s", content_type);
273
274         add_response_header(h, strdup("Content-type"), strdup(content_type));
275         h->response_code = 200;
276         h->response_string = strdup("OK");
277 }