* Added some temporary variables to OpenCmdResult(). Hopefully fixes
[citadel.git] / citadel / file_ops.c
1 /* $Id$ */
2 #include "sysdep.h"
3 #include <stdlib.h>
4 #include <unistd.h>
5 #include <stdio.h>
6 #include <fcntl.h>
7 #include <sys/stat.h>
8 #include <errno.h>
9 #include <string.h>
10 #include <sys/stat.h>
11 #include <time.h>
12 #include <limits.h>
13 #include "citadel.h"
14 #include "server.h"
15 #include "config.h"
16 #include "file_ops.h"
17 #include "sysdep_decls.h"
18 #include "user_ops.h"
19 #include "support.h"
20 #include "room_ops.h"
21 #include "msgbase.h"
22 #include "tools.h"
23
24 void cmd_delf(char *filename)
25 {
26         char pathname[64];
27         int a;
28
29         if (!(CC->logged_in)) {
30                 cprintf("%d Not logged in.\n",ERROR+NOT_LOGGED_IN);
31                 return;
32                 }
33
34         if (!is_room_aide()) {
35                 cprintf("%d Higher access required.\n",
36                         ERROR+HIGHER_ACCESS_REQUIRED);
37                 return;
38                 }
39
40
41         if ((CC->quickroom.QRflags & QR_DIRECTORY) == 0) {
42                 cprintf("%d No directory in this room.\n",ERROR+NOT_HERE);
43                 return;
44                 }
45
46         if (strlen(filename)==0) {
47                 cprintf("%d You must specify a file name.\n",
48                         ERROR+FILE_NOT_FOUND);
49                 return;
50                 }
51         for (a=0; a<strlen(filename); ++a)
52                 if (filename[a]=='/') filename[a] = '_';
53         snprintf(pathname,sizeof pathname,"./files/%s/%s",
54                  CC->quickroom.QRdirname,filename);
55         a=unlink(pathname);
56         if (a==0) cprintf("%d File '%s' deleted.\n",OK,pathname);
57         else cprintf("%d File '%s' not found.\n",ERROR+FILE_NOT_FOUND,pathname);
58         }
59
60
61
62
63 /*
64  * move a file from one room directory to another
65  */
66 void cmd_movf(char *cmdbuf)
67 {
68         char filename[256];
69         char pathname[256];
70         char newpath[256];
71         char newroom[256];
72         char buf[256];
73         int a;
74         struct quickroom qrbuf;
75
76         extract(filename,cmdbuf,0);
77         extract(newroom,cmdbuf,1);
78
79         if (!(CC->logged_in)) {
80                 cprintf("%d Not logged in.\n",ERROR+NOT_LOGGED_IN);
81                 return;
82                 }
83
84         if (!is_room_aide()) {
85                 cprintf("%d Higher access required.\n",
86                         ERROR+HIGHER_ACCESS_REQUIRED);
87                 return;
88                 }
89
90         if ((CC->quickroom.QRflags & QR_DIRECTORY) == 0) {
91                 cprintf("%d No directory in this room.\n",ERROR+NOT_HERE);
92                 return;
93                 }
94
95         if (strlen(filename)==0) {
96                 cprintf("%d You must specify a file name.\n",
97                         ERROR+FILE_NOT_FOUND);
98                 return;
99                 }
100
101         for (a=0; a<strlen(filename); ++a)
102                 if (filename[a]=='/') filename[a] = '_';
103         snprintf(pathname,sizeof pathname,"./files/%s/%s",
104                  CC->quickroom.QRdirname,filename);
105         if (access(pathname,0)!=0) {
106                 cprintf("%d File '%s' not found.\n",
107                         ERROR+FILE_NOT_FOUND,pathname);
108                 return;
109                 }
110
111         if (getroom(&qrbuf, newroom)!=0) {
112                 cprintf("%d '%s' does not exist.\n",
113                         ERROR, newroom);
114                 return;
115                 }
116         if ((qrbuf.QRflags & QR_DIRECTORY) == 0) {
117                 cprintf("%d '%s' is not a directory room.\n",
118                         ERROR+NOT_HERE,qrbuf.QRname);
119                 return;
120                 }
121         snprintf(newpath,sizeof newpath,"./files/%s/%s",qrbuf.QRdirname,
122                  filename);
123         if (link(pathname,newpath)!=0) {
124                 cprintf("%d Couldn't move file: %s\n",ERROR,strerror(errno));
125                 return;
126                 }
127         unlink(pathname);
128
129         /* this is a crude method of copying the file description */
130         snprintf(buf, sizeof buf,
131                 "cat ./files/%s/filedir |grep %s >>./files/%s/filedir",
132                 CC->quickroom.QRdirname,
133                 filename,
134                 qrbuf.QRdirname);
135         system(buf);
136         cprintf("%d File '%s' has been moved.\n",OK,filename);
137         }
138
139
140 /*
141  * send a file over the net
142  */
143 void cmd_netf(char *cmdbuf)
144 {
145         char pathname[256],filename[256],destsys[256],buf[256],outfile[256];
146         int a,e;
147         time_t now;
148         FILE *ofp;
149
150         extract(filename,cmdbuf,0);
151         extract(destsys,cmdbuf,1);
152
153         if (!(CC->logged_in)) {
154                 cprintf("%d Not logged in.\n",ERROR+NOT_LOGGED_IN);
155                 return;
156                 }
157
158         if (!is_room_aide()) {
159                 cprintf("%d Higher access required.\n",
160                         ERROR+HIGHER_ACCESS_REQUIRED);
161                 return;
162                 }
163
164         if ((CC->quickroom.QRflags & QR_DIRECTORY) == 0) {
165                 cprintf("%d No directory in this room.\n",ERROR+NOT_HERE);
166                 return;
167                 }
168
169         if (strlen(filename)==0) {
170                 cprintf("%d You must specify a file name.\n",
171                         ERROR+FILE_NOT_FOUND);
172                 return;
173                 }
174
175         for (a=0; a<strlen(filename); ++a)
176                 if (filename[a]=='/') filename[a] = '_';
177         snprintf(pathname,sizeof pathname,"./files/%s/%s",
178                  CC->quickroom.QRdirname,filename);
179         if (access(pathname,0)!=0) {
180                 cprintf("%d File '%s' not found.\n",
181                         ERROR+FILE_NOT_FOUND,pathname);
182                 return;
183                 }
184         snprintf(buf,sizeof buf,"sysop@%s",destsys);
185         e=alias(buf);
186         if (e!=MES_BINARY) {
187                 cprintf("%d No such system: '%s'\n",
188                         ERROR+NO_SUCH_SYSTEM,destsys);
189                 return;
190                 }
191         snprintf(outfile,sizeof outfile,"%s/network/spoolin/nsf.%d",BBSDIR,
192                  getpid());
193         ofp=fopen(outfile,"a");
194         if (ofp==NULL) {
195                 cprintf("%d internal error\n",ERROR);
196                 return;
197                 }
198
199         putc(255,ofp);
200         putc(MES_NORMAL,ofp);
201         putc(0,ofp);
202         fprintf(ofp,"Pcit%ld",CC->usersupp.usernum); putc(0,ofp);
203         time(&now);
204         fprintf(ofp,"T%ld",(long)now); putc(0,ofp);
205         fprintf(ofp,"A%s",CC->usersupp.fullname); putc(0,ofp);
206         fprintf(ofp,"O%s",CC->quickroom.QRname); putc(0,ofp);
207         fprintf(ofp,"N%s",NODENAME); putc(0,ofp);
208         fprintf(ofp,"D%s",destsys); putc(0,ofp);
209         fprintf(ofp,"SFILE"); putc(0,ofp);
210         putc('M',ofp);
211         fclose(ofp);
212
213         snprintf(buf,sizeof buf,
214                 "cd ./files/%s; uuencode %s <%s 2>/dev/null >>%s",
215                 CC->quickroom.QRdirname,filename,filename,outfile);
216         system(buf);
217
218         ofp = fopen(outfile,"a");
219         putc(0,ofp);
220         fclose(ofp);
221
222         cprintf("%d File '%s' has been sent to %s.\n",OK,filename,destsys);
223         system("nohup ./netproc -i >/dev/null 2>&1 &");
224         return;
225         }
226
227 /*
228  * This code is common to all commands which open a file for downloading.
229  * It examines the file and displays the OK result code and some information
230  * about the file.  NOTE: this stuff is Unix dependent.
231  */
232 void OpenCmdResult(char *filename, char *mime_type) {
233         struct stat statbuf;
234         time_t modtime;
235         long filesize;
236
237         fstat(fileno(CC->download_fp), &statbuf);
238         filesize = (long) statbuf.st_size;
239         modtime = (time_t) statbuf.st_mtime;
240
241         cprintf("%d %ld|%ld|%s|%s\n",
242                 OK, filesize, modtime, filename, mime_type);
243 }
244
245
246 /*
247  * open a file for downloading
248  */
249 void cmd_open(char *cmdbuf)
250 {
251         char filename[256];
252         char pathname[256];
253         int a;
254
255         extract(filename,cmdbuf,0);
256
257         if (!(CC->logged_in)) {
258                 cprintf("%d Not logged in.\n",ERROR+NOT_LOGGED_IN);
259                 return;
260                 }
261
262         if ((CC->quickroom.QRflags & QR_DIRECTORY) == 0) {
263                 cprintf("%d No directory in this room.\n",ERROR+NOT_HERE);
264                 return;
265                 }
266
267         if (strlen(filename)==0) {
268                 cprintf("%d You must specify a file name.\n",
269                         ERROR+FILE_NOT_FOUND);
270                 return;
271                 }
272
273         if (CC->download_fp != NULL) {
274                 cprintf("%d You already have a download file open.\n",ERROR);
275                 return;
276                 }
277
278         for (a=0; a<strlen(filename); ++a)
279                 if (filename[a]=='/') filename[a] = '_';
280
281         snprintf(pathname,sizeof pathname,
282                  "./files/%s/%s",CC->quickroom.QRdirname,filename);
283         CC->download_fp = fopen(pathname,"r");
284
285         if (CC->download_fp==NULL) {
286                 cprintf("%d cannot open %s: %s\n",
287                         ERROR,pathname,strerror(errno));
288                 return;
289                 }
290
291         OpenCmdResult(filename, "application/octet-stream");
292         }
293
294 /*
295  * open an image file
296  */
297 void cmd_oimg(char *cmdbuf)
298 {
299         char filename[256];
300         char pathname[256];
301         struct usersupp usbuf;
302         char which_user[32];
303         int which_floor;
304         int a;
305
306         extract(filename,cmdbuf,0);
307
308         if (strlen(filename)==0) {
309                 cprintf("%d You must specify a file name.\n",
310                         ERROR+FILE_NOT_FOUND);
311                 return;
312                 }
313
314         if (CC->download_fp != NULL) {
315                 cprintf("%d You already have a download file open.\n",ERROR);
316                 return;
317                 }
318
319         if (!strcasecmp(filename, "_userpic_")) {
320                 extract(which_user, cmdbuf, 1);
321                 if (getuser(&usbuf, which_user) != 0) {
322                         cprintf("%d No such user.\n", ERROR+NO_SUCH_USER);
323                         return;
324                         }
325                 snprintf(pathname, sizeof pathname, "./userpics/%ld.gif",
326                          usbuf.usernum);
327                 }
328         else if (!strcasecmp(filename, "_floorpic_")) {
329                 which_floor = extract_int(cmdbuf, 1);
330                 snprintf(pathname, sizeof pathname, "./images/floor.%d.gif",
331                          which_floor);
332                 }
333         else if (!strcasecmp(filename, "_roompic_")) {
334                 assoc_file_name(pathname, &CC->quickroom, "images");
335                 }
336         else {
337                 for (a=0; a<strlen(filename); ++a) {
338                         filename[a] = tolower(filename[a]);
339                         if (filename[a]=='/') filename[a] = '_';
340                         }
341                 snprintf(pathname,sizeof pathname,"./images/%s.gif",filename);
342                 }
343         
344         CC->download_fp = fopen(pathname,"r");
345         if (CC->download_fp == NULL) {
346                 cprintf("%d Cannot open %s: %s\n",
347                         ERROR+FILE_NOT_FOUND,pathname,strerror(errno));
348                 return;
349                 }
350         
351         OpenCmdResult(pathname, "image/gif");
352         }
353
354 /*
355  * open a file for uploading
356  */
357 void cmd_uopn(char *cmdbuf)
358 {
359         int a;
360
361         extract(CC->upl_file,cmdbuf,0);
362         extract(CC->upl_comment,cmdbuf,1);
363
364         if (!(CC->logged_in)) {
365                 cprintf("%d Not logged in.\n",ERROR+NOT_LOGGED_IN);
366                 return;
367                 }
368
369         if ((CC->quickroom.QRflags & QR_DIRECTORY) == 0) {
370                 cprintf("%d No directory in this room.\n",ERROR+NOT_HERE);
371                 return;
372                 }
373
374         if (strlen(CC->upl_file)==0) {
375                 cprintf("%d You must specify a file name.\n",
376                         ERROR+FILE_NOT_FOUND);
377                 return;
378                 }
379
380         if (CC->upload_fp != NULL) {
381                 cprintf("%d You already have a upload file open.\n",ERROR);
382                 return;
383                 }
384
385         for (a=0; a<strlen(CC->upl_file); ++a)
386                 if (CC->upl_file[a]=='/') CC->upl_file[a] = '_';
387         snprintf(CC->upl_path,sizeof CC->upl_path,"./files/%s/%s",
388                  CC->quickroom.QRdirname,CC->upl_file);
389         snprintf(CC->upl_filedir,sizeof CC->upl_filedir,"./files/%s/filedir",
390                  CC->quickroom.QRdirname);
391         
392         CC->upload_fp = fopen(CC->upl_path,"r");
393         if (CC->upload_fp != NULL) {
394                 fclose(CC->upload_fp);
395                 CC->upload_fp = NULL;
396                 cprintf("%d '%s' already exists\n",
397                         ERROR+ALREADY_EXISTS,CC->upl_path);
398                 return;
399                 }
400
401         CC->upload_fp = fopen(CC->upl_path,"wb");
402         if (CC->upload_fp == NULL) {
403                 cprintf("%d Cannot open %s: %s\n",
404                         ERROR,CC->upl_path,strerror(errno));
405                 return;
406                 }
407         cprintf("%d Ok\n",OK);
408         }
409
410
411
412 /*
413  * open an image file for uploading
414  */
415 void cmd_uimg(char *cmdbuf)
416 {
417         int is_this_for_real;
418         char basenm[256];
419         int which_floor;
420         int a;
421
422         if (num_parms(cmdbuf) < 2) {
423                 cprintf("%d Usage error.\n", ERROR);
424                 return;
425                 }
426
427         is_this_for_real = extract_int(cmdbuf,0);       
428         extract(basenm, cmdbuf, 1);
429         if (CC->upload_fp != NULL) {
430                 cprintf("%d You already have an upload file open.\n", ERROR);
431                 return;
432                 }
433
434         strcpy(CC->upl_path, "");
435
436         for (a=0; a<strlen(basenm); ++a) {
437                 basenm[a] = tolower(basenm[a]);
438                 if (basenm[a]=='/') basenm[a] = '_';
439                 }
440
441         if (CC->usersupp.axlevel >= 6) {
442                 snprintf(CC->upl_path, sizeof CC->upl_path, "./images/%s",
443                          basenm);
444                 }
445
446         if (!strcasecmp(basenm, "_userpic_")) {
447                 snprintf(CC->upl_path, sizeof CC->upl_path,
448                          "./userpics/%ld.gif", CC->usersupp.usernum);
449                 }
450
451         if ( (!strcasecmp(basenm, "_floorpic_")) && (CC->usersupp.axlevel >= 6) ) {
452                 which_floor = extract_int(cmdbuf, 2);
453                 snprintf(CC->upl_path, sizeof CC->upl_path,
454                          "./images/floor.%d.gif", which_floor);
455                 }
456
457         if ( (!strcasecmp(basenm, "_roompic_")) && (is_room_aide()) ) {
458                 assoc_file_name(CC->upl_path, &CC->quickroom, "images");
459                 }
460
461         if (strlen(CC->upl_path) == 0) {
462                 cprintf("%d Higher access required.\n",
463                         ERROR+HIGHER_ACCESS_REQUIRED);
464                 return;
465                 }
466
467         if (is_this_for_real == 0) {
468                 cprintf("%d Ok to send image\n", OK);
469                 return;
470                 }
471
472         CC->upload_fp = fopen(CC->upl_path,"wb");
473         if (CC->upload_fp == NULL) {
474                 cprintf("%d Cannot open %s: %s\n",
475                         ERROR,CC->upl_path,strerror(errno));
476                 return;
477                 }
478         cprintf("%d Ok\n",OK);
479         CC->upload_type = UPL_IMAGE;
480         }
481
482
483 /*
484  * close the download file
485  */
486 void cmd_clos(void) {
487         char buf[256];
488         
489         if (CC->download_fp == NULL) {
490                 cprintf("%d You don't have a download file open.\n",ERROR);
491                 return;
492                 }
493
494         fclose(CC->download_fp);
495         CC->download_fp = NULL;
496
497         if (CC->dl_is_net == 1) {
498                 CC->dl_is_net = 0;
499                 snprintf(buf,sizeof buf,"%s/network/spoolout/%s",BBSDIR,
500                          CC->net_node);
501                 unlink(buf);
502                 }
503
504         cprintf("%d Ok\n",OK);
505         }
506
507
508 /*
509  * abort and upload
510  */
511 void abort_upl(struct CitContext *who)
512 {
513         if (who->upload_fp != NULL) {
514                 fclose(who->upload_fp);
515                 who->upload_fp = NULL;
516                 unlink(CC->upl_path);
517                 }
518         }
519
520
521
522 /*
523  * close the upload file
524  */
525 void cmd_ucls(char *cmd)
526 {
527         FILE *fp;
528         char upload_notice[512];
529         
530         if (CC->upload_fp == NULL) {
531                 cprintf("%d You don't have an upload file open.\n",ERROR);
532                 return;
533         }
534
535         fclose(CC->upload_fp);
536         CC->upload_fp = NULL;
537
538         if ((!strcasecmp(cmd,"1")) && (CC->upload_type != UPL_FILE)) {
539                 CC->upload_type = UPL_FILE;
540                 cprintf("%d Upload completed.\n", OK);
541
542                 if (CC->upload_type == UPL_NET) {
543                         if (fork()==0) {
544                                 execlp("./netproc", "netproc", "-i", NULL);
545                                 exit(errno);
546                         }
547                 }
548
549                 return;
550         }
551
552         if (!strcasecmp(cmd,"1")) {
553                 cprintf("%d File '%s' saved.\n",OK,CC->upl_path);
554                 fp = fopen(CC->upl_filedir,"a");
555                 if (fp==NULL) fp=fopen(CC->upl_filedir,"w");
556                 if (fp!=NULL) {
557                         fprintf(fp,"%s %s\n",CC->upl_file,CC->upl_comment);
558                         fclose(fp);
559                 }
560
561                 /* put together an upload notice */
562                 sprintf(upload_notice,
563                         "NEW UPLOAD: '%s'\n %s\n",
564                         CC->upl_file,CC->upl_comment);
565                 quickie_message(CC->curr_user, NULL, CC->quickroom.QRname,
566                                 upload_notice);
567         }
568         else {
569                 abort_upl(CC);
570                 cprintf("%d File '%s' aborted.\n",OK,CC->upl_path);
571         }
572 }
573
574
575
576 /*
577  * read from the download file
578  */
579 void cmd_read(char *cmdbuf)
580 {
581         long start_pos;
582         int bytes;
583         char buf[4096];
584
585         start_pos = extract_long(cmdbuf,0);
586         bytes = extract_int(cmdbuf,1);
587         
588         if (CC->download_fp == NULL) {
589                 cprintf("%d You don't have a download file open.\n",ERROR);
590                 return;
591                 }
592
593         if (bytes > 4096) {
594                 cprintf("%d You may not read more than 4096 bytes.\n",ERROR);
595                 return;
596                 }
597
598         fseek(CC->download_fp,start_pos,0);
599         fread(buf,bytes,1,CC->download_fp);
600         cprintf("%d %d\n",BINARY_FOLLOWS,bytes);
601         client_write(buf, bytes);
602         }
603
604
605
606 /*
607  * write to the upload file
608  */
609 void cmd_writ(char *cmdbuf)
610 {
611         int bytes;
612         char buf[4096];
613
614         bytes = extract_int(cmdbuf,0);
615         
616         if (CC->upload_fp == NULL) {
617                 cprintf("%d You don't have an upload file open.\n",ERROR);
618                 return;
619                 }
620
621         if (bytes > 4096) {
622                 cprintf("%d You may not write more than 4096 bytes.\n",ERROR);
623                 return;
624                 }
625
626         cprintf("%d %d\n",SEND_BINARY,bytes);
627         client_read(buf, bytes);
628         fwrite(buf,bytes,1,CC->upload_fp);
629         }
630
631
632
633 /*
634  * cmd_netp() - identify as network poll session
635  */
636 void cmd_netp(char *cmdbuf)
637 {
638         char buf[256];
639         
640         extract(buf,cmdbuf,1);
641         if (strcasecmp(buf,config.c_net_password)) {
642                 cprintf("%d authentication failed\n",ERROR);
643                 return;
644                 }
645         extract(CC->net_node,cmdbuf,0);
646         cprintf("%d authenticated as network node '%s'\n",OK,CC->net_node);
647         }
648
649 /*
650  * cmd_ndop() - open a network spool file for downloading
651  */
652 void cmd_ndop(char *cmdbuf)
653 {
654         char pathname[256];
655         struct stat statbuf;
656
657         if (strlen(CC->net_node)==0) {
658                 cprintf("%d Not authenticated as a network node.\n",
659                         ERROR+NOT_LOGGED_IN);
660                 return;
661                 }
662
663         if (CC->download_fp != NULL) {
664                 cprintf("%d You already have a download file open.\n",ERROR);
665                 return;
666                 }
667
668         snprintf(pathname,sizeof pathname,"%s/network/spoolout/%s",BBSDIR,
669                  CC->net_node);
670
671         /* first open the file in append mode in order to create a
672          * zero-length file if it doesn't already exist 
673          */
674         CC->download_fp = fopen(pathname,"a");
675         if (CC->download_fp != NULL) fclose(CC->download_fp);
676
677         /* now open it */
678         CC->download_fp = fopen(pathname,"r");
679         if (CC->download_fp==NULL) {
680                 cprintf("%d cannot open %s: %s\n",
681                         ERROR,pathname,strerror(errno));
682                 return;
683                 }
684
685
686         /* set this flag so other routines know that the download file
687          * currently open is a network spool file 
688          */
689         CC->dl_is_net = 1;
690
691         stat(pathname,&statbuf);
692         cprintf("%d %ld\n",OK,statbuf.st_size);
693         }
694
695 /*
696  * cmd_nuop() - open a network spool file for uploading
697  */
698 void cmd_nuop(char *cmdbuf)
699 {
700         if (strlen(CC->net_node)==0) {
701                 cprintf("%d Not authenticated as a network node.\n",
702                         ERROR+NOT_LOGGED_IN);
703                 return;
704                 }
705
706         if (CC->upload_fp != NULL) {
707                 cprintf("%d You already have an upload file open.\n",ERROR);
708                 return;
709                 }
710
711         snprintf(CC->upl_path,sizeof CC->upl_path,"%s/network/spoolin/%s.%d",
712                 BBSDIR,CC->net_node,getpid());
713
714         CC->upload_fp = fopen(CC->upl_path,"r");
715         if (CC->upload_fp != NULL) {
716                 fclose(CC->upload_fp);
717                 CC->upload_fp = NULL;
718                 cprintf("%d '%s' already exists\n",
719                         ERROR+ALREADY_EXISTS,CC->upl_path);
720                 return;
721                 }
722
723         CC->upload_fp = fopen(CC->upl_path,"w");
724         if (CC->upload_fp == NULL) {
725                 cprintf("%d Cannot open %s: %s\n",
726                         ERROR,CC->upl_path,strerror(errno));
727                 return;
728                 }
729
730         CC->upload_type = UPL_NET;
731         cprintf("%d Ok\n",OK);
732         }