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