* added some missing license declarations
[citadel.git] / webcit / downloads.c
1 /*
2  * $Id$
3  *
4  * Copyright (c) 1996-2010 by the citadel.org team
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 3 of the License, or
9  * (at your option) any later version.
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  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20 #include "webcit.h"
21 #include "webserver.h"
22
23 extern char* static_dirs[];
24
25 typedef struct _FileListStruct {
26         StrBuf *Filename;
27         long FileSize;
28         StrBuf *MimeType;
29         StrBuf *Comment;
30         int IsPic;
31         int Sequence;
32 } FileListStruct;
33
34 void FreeFiles(void *vFile)
35 {
36         FileListStruct *F = (FileListStruct*) vFile;
37         FreeStrBuf(&F->Filename);
38         FreeStrBuf(&F->MimeType);
39         FreeStrBuf(&F->Comment);
40         free(F);
41 }
42
43 /* -------------------------------------------------------------------------------- */
44 void tmplput_FILE_NAME(StrBuf *Target, WCTemplputParams *TP)
45 {
46         FileListStruct *F = (FileListStruct*) CTX;
47         StrBufAppendTemplate(Target, TP, F->Filename, 0);
48 }
49 void tmplput_FILE_SIZE(StrBuf *Target, WCTemplputParams *TP)
50 {
51         FileListStruct *F = (FileListStruct*) CTX;
52         StrBufAppendPrintf(Target, "%ld", F->FileSize);
53 }
54 void tmplput_FILEMIMETYPE(StrBuf *Target, WCTemplputParams *TP)
55 {
56         FileListStruct *F = (FileListStruct*) CTX;
57         StrBufAppendTemplate(Target, TP, F->MimeType, 0);
58 }
59 void tmplput_FILE_COMMENT(StrBuf *Target, WCTemplputParams *TP)
60 {
61         FileListStruct *F = (FileListStruct*) CTX;
62         StrBufAppendTemplate(Target, TP, F->Comment, 0);
63 }
64
65 /* -------------------------------------------------------------------------------- */
66
67 int Conditional_FILE_ISPIC(StrBuf *Target, WCTemplputParams *TP)
68 {
69         FileListStruct *F = (FileListStruct*) CTX;
70         return F->IsPic;
71 }
72
73 /* -------------------------------------------------------------------------------- */
74 int CompareFilelistByMime(const void *vFile1, const void *vFile2)
75 {
76         FileListStruct *File1 = (FileListStruct*) GetSearchPayload(vFile1);
77         FileListStruct *File2 = (FileListStruct*) GetSearchPayload(vFile2);
78
79         if (File1->IsPic != File2->IsPic)
80                 return File1->IsPic > File2->IsPic;
81         return strcasecmp(ChrPtr(File1->MimeType), ChrPtr(File2->MimeType));
82 }
83 int CompareFilelistByMimeRev(const void *vFile1, const void *vFile2)
84 {
85         FileListStruct *File1 = (FileListStruct*) GetSearchPayload(vFile1);
86         FileListStruct *File2 = (FileListStruct*) GetSearchPayload(vFile2);
87         if (File1->IsPic != File2->IsPic)
88                 return File1->IsPic < File2->IsPic;
89         return strcasecmp(ChrPtr(File2->MimeType), ChrPtr(File1->MimeType));
90 }
91 int GroupchangeFilelistByMime(const void *vFile1, const void *vFile2)
92 {
93         FileListStruct *File1 = (FileListStruct*) vFile1;
94         FileListStruct *File2 = (FileListStruct*) vFile2;
95
96         if (File1->IsPic != File2->IsPic)
97                 return File1->IsPic > File2->IsPic;
98         return strcasecmp(ChrPtr(File1->MimeType), ChrPtr(File2->MimeType)) != 0;
99 }
100
101
102 int CompareFilelistByName(const void *vFile1, const void *vFile2)
103 {
104         FileListStruct *File1 = (FileListStruct*) GetSearchPayload(vFile1);
105         FileListStruct *File2 = (FileListStruct*) GetSearchPayload(vFile2);
106
107         if (File1->IsPic != File2->IsPic)
108                 return File1->IsPic > File2->IsPic;
109         return strcasecmp(ChrPtr(File1->Filename), ChrPtr(File2->Filename));
110 }
111 int CompareFilelistByNameRev(const void *vFile1, const void *vFile2)
112 {
113         FileListStruct *File1 = (FileListStruct*) GetSearchPayload(vFile1);
114         FileListStruct *File2 = (FileListStruct*) GetSearchPayload(vFile2);
115         if (File1->IsPic != File2->IsPic)
116                 return File1->IsPic < File2->IsPic;
117         return strcasecmp(ChrPtr(File2->Filename), ChrPtr(File1->Filename));
118 }
119 int GroupchangeFilelistByName(const void *vFile1, const void *vFile2)
120 {
121         FileListStruct *File1 = (FileListStruct*) vFile1;
122         FileListStruct *File2 = (FileListStruct*) vFile2;
123
124         return ChrPtr(File1->Filename)[0] != ChrPtr(File2->Filename)[0];
125 }
126
127
128 int CompareFilelistBySize(const void *vFile1, const void *vFile2)
129 {
130         FileListStruct *File1 = (FileListStruct*) GetSearchPayload(vFile1);
131         FileListStruct *File2 = (FileListStruct*) GetSearchPayload(vFile2);
132         if (File1->FileSize == File2->FileSize)
133                 return 0;
134         return (File1->FileSize > File2->FileSize);
135 }
136 int CompareFilelistBySizeRev(const void *vFile1, const void *vFile2)
137 {
138         FileListStruct *File1 = (FileListStruct*) GetSearchPayload(vFile1);
139         FileListStruct *File2 = (FileListStruct*) GetSearchPayload(vFile2);
140         if (File1->FileSize == File2->FileSize)
141                 return 0;
142         return (File1->FileSize < File2->FileSize);
143 }
144 int GroupchangeFilelistBySize(const void *vFile1, const void *vFile2)
145 {
146         return 0;
147 }
148
149
150 int CompareFilelistByComment(const void *vFile1, const void *vFile2)
151 {
152         FileListStruct *File1 = (FileListStruct*) GetSearchPayload(vFile1);
153         FileListStruct *File2 = (FileListStruct*) GetSearchPayload(vFile2);
154         return strcasecmp(ChrPtr(File1->Comment), ChrPtr(File2->Comment));
155 }
156 int CompareFilelistByCommentRev(const void *vFile1, const void *vFile2)
157 {
158         FileListStruct *File1 = (FileListStruct*) GetSearchPayload(vFile1);
159         FileListStruct *File2 = (FileListStruct*) GetSearchPayload(vFile2);
160         return strcasecmp(ChrPtr(File2->Comment), ChrPtr(File1->Comment));
161 }
162 int GroupchangeFilelistByComment(const void *vFile1, const void *vFile2)
163 {
164         FileListStruct *File1 = (FileListStruct*) vFile1;
165         FileListStruct *File2 = (FileListStruct*) vFile2;
166         return ChrPtr(File1->Comment)[9] != ChrPtr(File2->Comment)[0];
167 }
168
169
170 int CompareFilelistBySequence(const void *vFile1, const void *vFile2)
171 {
172         FileListStruct *File1 = (FileListStruct*) GetSearchPayload(vFile1);
173         FileListStruct *File2 = (FileListStruct*) GetSearchPayload(vFile2);
174         return (File2->Sequence >  File1->Sequence);
175 }
176 int GroupchangeFilelistBySequence(const void *vFile1, const void *vFile2)
177 {
178         return 0;
179 }
180
181 /* -------------------------------------------------------------------------------- */
182 HashList* LoadFileList(StrBuf *Target, WCTemplputParams *TP)
183 {
184         FileListStruct *Entry;
185         StrBuf *Buf;
186         HashList *Files;
187         int Done = 0;
188         int sequence = 0;
189         char buf[1024];
190         CompareFunc SortIt;
191         int HavePic;
192         WCTemplputParams SubTP;
193
194         memset(&SubTP, 0, sizeof(WCTemplputParams));
195         serv_puts("RDIR");
196         serv_getln(buf, sizeof buf);
197         if (buf[0] != '1') return NULL;
198
199         Buf = NewStrBuf();             
200         Files = NewHash(1, NULL);
201         while (!Done && (StrBuf_ServGetln(Buf)>=0)) {
202                 if ( (StrLength(Buf)==3) && 
203                     !strcmp(ChrPtr(Buf), "000")) 
204                 {
205                         Done = 1;
206                         continue;
207                 }
208
209                 Entry = (FileListStruct*) malloc(sizeof (FileListStruct));
210                 Entry->Filename = NewStrBufPlain(NULL, StrLength(Buf));
211                 Entry->MimeType = NewStrBufPlain(NULL, StrLength(Buf));
212                 Entry->Comment = NewStrBufPlain(NULL, StrLength(Buf));
213
214                 Entry->Sequence = sequence++;
215
216                 StrBufExtract_token(Entry->Filename, Buf, 0, '|');
217                 Entry->FileSize = StrBufExtract_long(Buf, 1, '|');
218                 StrBufExtract_token(Entry->MimeType, Buf, 2, '|');
219                 StrBufExtract_token(Entry->Comment, Buf, 3, '|');
220
221
222
223                 Entry->IsPic = (strstr(ChrPtr(Entry->MimeType), "image") != NULL);
224                 if (Entry->IsPic) {
225                         HavePic = 1;
226                 }
227                 Put(Files, SKEY(Entry->Filename), Entry, FreeFiles);
228         }
229         SubTP.Filter.ContextType = CTX_FILELIST;
230         SortIt = RetrieveSort(&SubTP, NULL, 0, HKEY("fileunsorted"), 0);
231         if (SortIt != NULL)
232                 SortByPayload(Files, SortIt);
233         else 
234                 SortByPayload(Files, CompareFilelistBySequence);
235         FreeStrBuf(&Buf);
236         svputlong("FILE:HAVEPICS", HavePic);
237         return Files;
238 }
239
240 void display_mime_icon(void)
241 {
242         char FileBuf[SIZ];
243         const char *FileName;
244         char *MimeType;
245         size_t tlen;
246
247         MimeType = xbstr("type", &tlen);
248         FileName = GetIconFilename(MimeType, tlen);
249
250         if (FileName == NULL)
251                 snprintf (FileBuf, SIZ, "%s%s", static_dirs[0], "/diskette_24x.gif");
252         else
253                 snprintf (FileBuf, SIZ, "%s%s", static_dirs[3], FileName);
254         output_static(FileBuf);
255 }
256
257 void download_file(void)
258 {
259         wcsession *WCC = WC;
260         StrBuf *Buf;
261         off_t bytes;
262         StrBuf *ContentType = NewStrBufPlain(HKEY("application/octet-stream"));
263
264         /* Setting to nonzero forces a MIME type of application/octet-stream */
265         int force_download = 1;
266         
267         Buf = NewStrBuf();
268         StrBufExtract_token(Buf, WCC->Hdr->HR.ReqLine, 0, '/');
269         StrBufUnescape(Buf, 1);
270         serv_printf("OPEN %s", ChrPtr(Buf));
271         StrBuf_ServGetln(Buf);
272         if (GetServerStatus(Buf, NULL) == 2) {
273                 StrBufCutLeft(Buf, 4);
274                 bytes = StrBufExtract_long(Buf, 0, '|');
275                 if (!force_download) {
276                         StrBufExtract_token(ContentType, Buf, 3, '|');
277                 }
278                 serv_read_binary(WCC->WBuf, bytes, Buf);
279                 serv_puts("CLOS");
280                 StrBuf_ServGetln(Buf);
281                 http_transmit_thing(ChrPtr(ContentType), 0);
282         } else {
283                 StrBufCutLeft(Buf, 4);
284                 hprintf("HTTP/1.1 404 %s\n", ChrPtr(Buf));
285                 output_headers(0, 0, 0, 0, 0, 0);
286                 hprintf("Content-Type: text/plain\r\n");
287                 wc_printf(_("An error occurred while retrieving this file: %s\n"), 
288                         ChrPtr(Buf));
289                 end_burst();
290         }
291         FreeStrBuf(&ContentType);
292         FreeStrBuf(&Buf);
293 }
294
295
296
297 void delete_file(void)
298 {
299         const StrBuf *MimeType;
300         StrBuf *Buf;
301         char buf[256];
302         
303         safestrncpy(buf, bstr("file"), sizeof buf);
304         unescape_input(buf);
305         serv_printf("DELF %s", buf);
306         Buf = NewStrBuf();
307         StrBuf_ServGetln(Buf);
308         GetServerStatus(Buf, NULL);
309         StrBufCutLeft(Buf, 4);
310         strcpy(WC->ImportantMessage, ChrPtr(Buf));
311         MimeType = DoTemplate(HKEY("files"), NULL, &NoCtx);
312         http_transmit_thing(ChrPtr(MimeType), 0);
313         FreeStrBuf(&Buf);
314 }
315
316
317
318 void upload_file(void)
319 {
320         const StrBuf *RetMimeType;
321         const char *MimeType;
322         char buf[1024];
323         long bytes_transmitted = 0;
324         long blocksize;
325         wcsession *WCC = WC;     /* stack this for faster access (WC is a function) */
326
327         MimeType = GuessMimeType(ChrPtr(WCC->upload), WCC->upload_length); 
328         serv_printf("UOPN %s|%s|%s", WCC->upload_filename, MimeType, bstr("description"));
329         serv_getln(buf, sizeof buf);
330         if (buf[0] != '2')
331         {
332                 strcpy(WCC->ImportantMessage, &buf[4]);
333                 RetMimeType = DoTemplate(HKEY("files"), NULL, &NoCtx);
334                 http_transmit_thing(ChrPtr(RetMimeType), 0);
335                 return;
336         }
337
338         while (bytes_transmitted < WCC->upload_length)
339         {
340                 blocksize = 4096;
341                 if (blocksize > (WCC->upload_length - bytes_transmitted))
342                 {
343                         blocksize = (WCC->upload_length - bytes_transmitted);
344                 }
345                 serv_printf("WRIT %ld", blocksize);
346                 serv_getln(buf, sizeof buf);
347                 if (buf[0] == '7')
348                 {
349                         blocksize = atoi(&buf[4]);
350                         serv_write(&ChrPtr(WCC->upload)[bytes_transmitted], blocksize);
351                         bytes_transmitted += blocksize;
352                 }
353         }
354
355         serv_puts("UCLS 1");
356         serv_getln(buf, sizeof buf);
357         strcpy(WCC->ImportantMessage, &buf[4]);
358         RetMimeType = DoTemplate(HKEY("files"), NULL, &NoCtx);
359         http_transmit_thing(ChrPtr(RetMimeType), 0);
360 }
361
362
363
364 /*
365  * When the browser requests an image file from the Citadel server,
366  * this function is called to transmit it.
367  */
368 void output_image(void)
369 {
370         StrBuf *Buf;
371         wcsession *WCC = WC;
372         off_t bytes;
373         const char *MimeType;
374         
375         Buf = NewStrBuf();
376         serv_printf("OIMG %s|%s", bstr("name"), bstr("parm"));
377         StrBuf_ServGetln(Buf);
378         if (GetServerStatus(Buf, NULL) == 2) {
379                 int rc;
380                 StrBufCutLeft(Buf, 4);
381                 bytes = StrBufExtract_long(Buf, 0, '|');
382
383                 /** Read it from the server */
384                 
385                 rc = serv_read_binary(WCC->WBuf, bytes, Buf);
386                 serv_puts("CLOS");
387                 StrBuf_ServGetln(Buf);
388                 
389                 if (rc > 0) {
390                         MimeType = GuessMimeType (ChrPtr(WCC->WBuf), StrLength(WCC->WBuf));
391                         /** Write it to the browser */
392                         if (!IsEmptyStr(MimeType))
393                         {
394                                 http_transmit_thing(MimeType, 0);
395                                 FreeStrBuf(&Buf);
396                                 return;
397                         }
398                 }
399                 /* hm... unknown mimetype? fallback to blank gif */
400         } 
401
402         
403         /*
404          * Instead of an ugly 404, send a 1x1 transparent GIF
405          * when there's no such image on the server.
406          */
407         StrBufPrintf (Buf, "%s%s", static_dirs[0], "/blank.gif");
408         output_static(ChrPtr(Buf));
409         FreeStrBuf(&Buf);
410 }
411
412 void 
413 InitModule_DOWNLOAD
414 (void)
415 {
416
417         RegisterIterator("ROOM:FILES", 0, NULL, LoadFileList,
418                          NULL, DeleteHash, CTX_FILELIST, CTX_NONE, 
419                          IT_FLAG_DETECT_GROUPCHANGE);
420
421         RegisterSortFunc(HKEY("filemime"),
422                          NULL, 0,
423                          CompareFilelistByMime,
424                          CompareFilelistByMimeRev,
425                          GroupchangeFilelistByMime,
426                          CTX_FILELIST);
427         RegisterSortFunc(HKEY("filename"),
428                          NULL, 0,
429                          CompareFilelistByName,
430                          CompareFilelistByNameRev,
431                          GroupchangeFilelistByName,
432                          CTX_FILELIST);
433         RegisterSortFunc(HKEY("filesize"),
434                          NULL, 0,
435                          CompareFilelistBySize,
436                          CompareFilelistBySizeRev,
437                          GroupchangeFilelistBySize,
438                          CTX_FILELIST);
439         RegisterSortFunc(HKEY("filesubject"),
440                          NULL, 0,
441                          CompareFilelistByComment,
442                          CompareFilelistByCommentRev,
443                          GroupchangeFilelistByComment,
444                          CTX_FILELIST);
445         RegisterSortFunc(HKEY("fileunsorted"),
446                          NULL, 0,
447                          CompareFilelistBySequence,
448                          CompareFilelistBySequence,
449                          GroupchangeFilelistBySequence,
450                          CTX_FILELIST);
451
452         RegisterNamespace("FILE:NAME", 0, 2, tmplput_FILE_NAME, NULL, CTX_FILELIST);
453         RegisterNamespace("FILE:SIZE", 0, 1, tmplput_FILE_SIZE, NULL, CTX_FILELIST);
454         RegisterNamespace("FILE:MIMETYPE", 0, 2, tmplput_FILEMIMETYPE, NULL, CTX_FILELIST);
455         RegisterNamespace("FILE:COMMENT", 0, 2, tmplput_FILE_COMMENT, NULL, CTX_FILELIST);
456
457         RegisterConditional(HKEY("COND:FILE:ISPIC"), 0, Conditional_FILE_ISPIC, CTX_FILELIST);
458
459         WebcitAddUrlHandler(HKEY("image"), "", 0, output_image, ANONYMOUS);
460         WebcitAddUrlHandler(HKEY("display_mime_icon"), "", 0, display_mime_icon , ANONYMOUS);
461         WebcitAddUrlHandler(HKEY("download_file"), "", 0, download_file, NEED_URL);
462         WebcitAddUrlHandler(HKEY("delete_file"), "", 0, delete_file, NEED_URL);
463         WebcitAddUrlHandler(HKEY("upload_file"), "", 0, upload_file, 0);
464 }