Mailing list header changes (fuck you Google)
[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.  Richard Stallman is an asshole communist.
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 /*
20  * Given an encoded UID, translate that to an unencoded Citadel EUID and
21  * then search for it in the current room.  Return a message number or -1
22  * if not found.
23  *
24  */
25 long locate_message_by_uid(struct ctdlsession *c, char *uid)
26 {
27         char buf[1024];
28
29         ctdl_printf(c, "EUID %s", uid);
30         ctdl_readline(c, buf, sizeof buf);
31         if (buf[0] == '2') {
32                 return (atol(&buf[4]));
33
34         }
35
36         /* Ugly hack to handle Mozilla Thunderbird, try stripping ".ics" if present */
37         if (!strcasecmp(&uid[strlen(uid) - 4], ".ics")) {
38                 safestrncpy(buf, uid, sizeof buf);
39                 buf[strlen(buf) - 4] = 0;
40                 ctdl_printf(c, "EUID %s", buf);
41                 ctdl_readline(c, buf, sizeof buf);
42                 if (buf[0] == '2') {
43                         return (atol(&buf[4]));
44
45                 }
46         }
47
48         return (-1);
49 }
50
51
52 /*
53  * DAV delete an object in a room.
54  */
55 void dav_delete_message(struct http_transaction *h, struct ctdlsession *c, long msgnum)
56 {
57         ctdl_delete_msgs(c, &msgnum, 1);
58         h->response_code = 204;
59         h->response_string = strdup("no content");
60 }
61
62
63 /*
64  * GET method directly on a message in a room
65  */
66 void dav_get_message(struct http_transaction *h, struct ctdlsession *c, long msgnum)
67 {
68         char buf[1024];
69         int in_body = 0;
70         int encoding = 0;
71         StrBuf *Body = NULL;
72
73         ctdl_printf(c, "MSG2 %ld", msgnum);
74         ctdl_readline(c, buf, sizeof buf);
75         if (buf[0] != '1') {
76                 do_404(h);
77                 return;
78         }
79
80         char *etag = malloc(20);
81         if (etag != NULL) {
82                 sprintf(etag, "%ld", msgnum);
83                 add_response_header(h, strdup("ETag"), etag);   // http_transaction now owns this memory
84         }
85
86         while (ctdl_readline(c, buf, sizeof buf), strcmp(buf, "000")) {
87                 if (IsEmptyStr(buf) && (in_body == 0)) {
88                         in_body = 1;
89                         Body = NewStrBuf();
90                 } else if (in_body == 0) {
91                         char *k = buf;
92                         char *v = strchr(buf, ':');
93                         if (v) {
94                                 *v = 0;
95                                 ++v;
96                                 striplt(v);     // we now have a key (k) and a value (v)
97                                 if ((!strcasecmp(k, "content-type"))    // fields which can be passed from RFC822 to HTTP as-is
98                                     || (!strcasecmp(k, "date"))
99                                     ) {
100                                         add_response_header(h, strdup(k), strdup(v));
101                                 } else if (!strcasecmp(k, "content-transfer-encoding")) {
102                                         if (!strcasecmp(v, "base64")) {
103                                                 encoding = 'b';
104                                         } else if (!strcasecmp(v, "quoted-printable")) {
105                                                 encoding = 'q';
106                                         }
107                                 }
108                         }
109                 } else if ((in_body == 1) && (Body != NULL)) {
110                         StrBufAppendPrintf(Body, "%s\n", buf);
111                 }
112         }
113
114         h->response_code = 200;
115         h->response_string = strdup("OK");
116
117         if (Body != NULL) {
118                 if (encoding == 'q') {
119                         h->response_body = malloc(StrLength(Body));
120                         if (h->response_body != NULL) {
121                                 h->response_body_length =
122                                     CtdlDecodeQuotedPrintable(h->response_body, (char *) ChrPtr(Body), StrLength(Body));
123                         }
124                         FreeStrBuf(&Body);
125                 } else if (encoding == 'b') {
126                         h->response_body = malloc(StrLength(Body));
127                         if (h->response_body != NULL) {
128                                 h->response_body_length = CtdlDecodeBase64(h->response_body, ChrPtr(Body), StrLength(Body));
129                         }
130                         FreeStrBuf(&Body);
131                 } else {
132                         h->response_body_length = StrLength(Body);
133                         h->response_body = SmashStrBuf(&Body);
134                 }
135         }
136 }
137
138
139 /*
140  * PUT a message into a room
141  */
142 void dav_put_message(struct http_transaction *h, struct ctdlsession *c, char *euid, long old_msgnum)
143 {
144         char buf[1024];
145         char *content_type = NULL;
146         int n;
147         long new_msgnum;
148         char new_euid[1024];
149         char response_string[1024];
150
151         if ((h->request_body == NULL) || (h->request_body_length < 1)) {
152                 do_404(h);      // Refuse to post a null message
153                 return;
154         }
155
156         ctdl_printf(c, "ENT0 1|||4|||1|");      // This protocol syntax will give us metadata back after upload
157         ctdl_readline(c, buf, sizeof buf);
158         if (buf[0] != '8') {
159                 h->response_code = 502;
160                 h->response_string = strdup("bad gateway");
161                 add_response_header(h, strdup("Content-type"), strdup("text/plain"));
162                 h->response_body = strdup(buf);
163                 h->response_body_length = strlen(h->response_body);
164                 return;
165         }
166
167         content_type = header_val(h, "Content-type");
168         ctdl_printf(c, "Content-type: %s\r\n", (content_type ? content_type : "application/octet-stream"));
169         ctdl_printf(c, "\r\n");
170         ctdl_write(c, h->request_body, h->request_body_length);
171         if (h->request_body[h->request_body_length] != '\n') {
172                 ctdl_printf(c, "\r\n");
173         }
174         ctdl_printf(c, "000");
175
176         /*
177          * Now handle the response from the Citadel server.
178          */
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         } else {
231                 h->response_code = 204; // We modified an existing item.
232                 h->response_string = strdup("no content");
233
234                 /* The item we replaced has probably already been deleted by
235                  * the Citadel server, but we'll do this anyway, just in case.
236                  */
237                 ctdl_delete_msgs(c, &old_msgnum, 1);
238         }
239
240 }
241
242
243 /*
244  * Download a single component of a MIME-encoded message
245  */
246 void download_mime_component(struct http_transaction *h, struct ctdlsession *c, long msgnum, char *partnum)
247 {
248         char buf[1024];
249         char content_type[1024];
250
251         ctdl_printf(c, "DLAT %ld|%s", msgnum, partnum);
252         ctdl_readline(c, buf, sizeof buf);
253         if (buf[0] != '6') {
254                 do_404(h);      // too bad, so sad, go away
255         }
256         // Server response is going to be: 6XX length|-1|filename|content-type|charset
257         h->response_body_length = extract_int(&buf[4], 0);
258         extract_token(content_type, buf, 3, '|', sizeof content_type);
259
260         h->response_body = malloc(h->response_body_length + 1);
261         int bytes = 0;
262         int thisblock;
263         do {
264                 thisblock = read(c->sock, &h->response_body[bytes], (h->response_body_length - bytes));
265                 bytes += thisblock;
266                 syslog(LOG_DEBUG, "Bytes read: %d of %d", (int) bytes, (int) h->response_body_length);
267         } while ((bytes < h->response_body_length) && (thisblock >= 0));
268         h->response_body[h->response_body_length] = 0;  // null terminate it just for good measure
269         syslog(LOG_DEBUG, "content type: %s", content_type);
270
271         add_response_header(h, strdup("Content-type"), strdup(content_type));
272         h->response_code = 200;
273         h->response_string = strdup("OK");
274 }