* give all commands their own function
[citadel.git] / citadel / file_ops.c
index d9c3ab149926b5e41880d82440cb855b1de86397..8fcc068bfd88bf2e1a555c839a6e9de920995603 100644 (file)
@@ -45,6 +45,8 @@
 #include "snprintf.h"
 #endif
 
+#include "ctdl_module.h"
+
 /*
  * network_talking_to()  --  concurrency checker
  */
@@ -97,7 +99,7 @@ int network_talking_to(char *nodename, int operation) {
                        break;
        }
 
-       if (nttlist != NULL) lprintf(CTDL_DEBUG, "nttlist=<%s>\n", nttlist);
+       if (nttlist != NULL) CtdlLogPrintf(CTDL_DEBUG, "nttlist=<%s>\n", nttlist);
        end_critical_section(S_NTTLIST);
        return(retval);
 }
@@ -128,7 +130,7 @@ void cmd_delf(char *filename)
                return;
        }
        for (a = 0; !IsEmptyStr(&filename[a]); ++a) {
-               if (filename[a] == '/') {
+               if ( (filename[a] == '/') || (filename[a] == '\\') ) {
                        filename[a] = '_';
                }
        }
@@ -180,7 +182,7 @@ void cmd_movf(char *cmdbuf)
        }
 
        for (a = 0; !IsEmptyStr(&filename[a]); ++a) {
-               if (filename[a] == '/') {
+               if ( (filename[a] == '/') || (filename[a] == '\\') ) {
                        filename[a] = '_';
                }
        }
@@ -219,102 +221,6 @@ void cmd_movf(char *cmdbuf)
 }
 
 
-/*
- * send a file over the net
- */
-void cmd_netf(char *cmdbuf)
-{
-       char pathname[256], filename[256], destsys[256], buf[256];
-       char outfile[256];
-       int a, e;
-       time_t now;
-       FILE *ofp;
-       static int seq = 1;
-
-       extract_token(filename, cmdbuf, 0, '|', sizeof filename);
-       extract_token(destsys, cmdbuf, 1, '|', sizeof destsys);
-
-       if (CtdlAccessCheck(ac_room_aide)) return;
-
-       if ((CC->room.QRflags & QR_DIRECTORY) == 0) {
-               cprintf("%d No directory in this room.\n",
-                       ERROR + NOT_HERE);
-               return;
-       }
-
-       if (IsEmptyStr(filename)) {
-               cprintf("%d You must specify a file name.\n",
-                       ERROR + FILE_NOT_FOUND);
-               return;
-       }
-
-       for (a = 0; !IsEmptyStr(&filename[a]); ++a) {
-               if (filename[a] == '/') {
-                       filename[a] = '_';
-               }
-       }
-       snprintf(pathname, sizeof pathname, "./files/%s/%s",
-                CC->room.QRdirname, filename);
-       if (access(pathname, 0) != 0) {
-               cprintf("%d File '%s' not found.\n",
-                       ERROR + FILE_NOT_FOUND, pathname);
-               return;
-       }
-       snprintf(buf, sizeof buf, "sysop@%s", destsys);
-       e = alias(buf);
-       if (e != MES_IGNET) {
-               cprintf("%d No such system: '%s'\n",
-                       ERROR + NO_SUCH_SYSTEM, destsys);
-               return;
-       }
-       snprintf(outfile, sizeof outfile,
-                        "%s/nsf.%04lx.%04x",
-                        ctdl_netin_dir,
-                        (long)getpid(), ++seq);
-       ofp = fopen(outfile, "a");
-       if (ofp == NULL) {
-               cprintf("%d internal error\n", ERROR + INTERNAL_ERROR);
-               return;
-       }
-
-       putc(255, ofp);
-       putc(MES_NORMAL, ofp);
-       putc(0, ofp);
-       fprintf(ofp, "P%s", CC->user.fullname);
-       putc(0, ofp);
-       time(&now);
-       fprintf(ofp, "T%ld", (long) now);
-       putc(0, ofp);
-       fprintf(ofp, "A%s", CC->user.fullname);
-       putc(0, ofp);
-       fprintf(ofp, "O%s", CC->room.QRname);
-       putc(0, ofp);
-       fprintf(ofp, "N%s", NODENAME);
-       putc(0, ofp);
-       fprintf(ofp, "D%s", destsys);
-       putc(0, ofp);
-       fprintf(ofp, "SFILE");
-       putc(0, ofp);
-       putc('M', ofp);
-       fclose(ofp);
-
-       snprintf(buf, sizeof buf,
-                        "cd %s/%s; uuencode %s <%s 2>/dev/null >>%s",
-                        ctdl_file_dir,
-                        /* FIXME: detect uuencode while installation? or inline */
-                        CC->room.QRdirname, filename, filename, outfile);
-       system(buf);
-
-       ofp = fopen(outfile, "a");
-       putc(0, ofp);
-       fclose(ofp);
-
-       cprintf("%d File '%s' has been sent to %s.\n", CIT_OK, filename,
-               destsys);
-       /* FIXME start a network run here. */
-       return;
-}
-
 /*
  * This code is common to all commands which open a file for downloading,
  * regardless of whether it's a file from the directory, an image, a network
@@ -322,7 +228,7 @@ void cmd_netf(char *cmdbuf)
  * It examines the file and displays the OK result code and some information
  * about the file.  NOTE: this stuff is Unix dependent.
  */
-void OpenCmdResult(char *filename, char *mime_type)
+void OpenCmdResult(char *filename, const char *mime_type)
 {
        struct stat statbuf;
        time_t modtime;
@@ -369,7 +275,7 @@ void cmd_open(char *cmdbuf)
        }
 
        for (a = 0; !IsEmptyStr(&filename[a]); ++a) {
-               if (filename[a] == '/') {
+               if ( (filename[a] == '/') || (filename[a] == '\\') ) {
                        filename[a] = '_';
                }
        }
@@ -396,6 +302,7 @@ void cmd_oimg(char *cmdbuf)
 {
        char filename[256];
        char pathname[PATH_MAX];
+       char MimeTestBuf[32];
        struct ctdluser usbuf;
        char which_user[USERNAME_SIZE];
        int which_floor;
@@ -423,37 +330,42 @@ void cmd_oimg(char *cmdbuf)
                        return;
                }
                snprintf(pathname, sizeof pathname, 
-                                "%s/%ld.gif",
+                                "%s/%ld",
                                 ctdl_usrpic_dir,
                                 usbuf.usernum);
        } else if (!strcasecmp(filename, "_floorpic_")) {
                which_floor = extract_int(cmdbuf, 1);
                snprintf(pathname, sizeof pathname,
-                                "%s/floor.%d.gif",
+                                "%s/floor.%d",
                                 ctdl_image_dir, which_floor);
        } else if (!strcasecmp(filename, "_roompic_")) {
                assoc_file_name(pathname, sizeof pathname, &CC->room, ctdl_image_dir);
        } else {
                for (a = 0; !IsEmptyStr(&filename[a]); ++a) {
                        filename[a] = tolower(filename[a]);
-                       if (filename[a] == '/') {
+                       if ( (filename[a] == '/') || (filename[a] == '\\') ) {
                                filename[a] = '_';
                        }
                }
                snprintf(pathname, sizeof pathname,
-                                "%s/%s.gif",
+                                "%s/%s",
                                 ctdl_image_dir,
                                 filename);
        }
 
        CC->download_fp = fopen(pathname, "rb");
+       if (CC->download_fp == NULL) {
+               strcat(pathname, ".gif");
+               CC->download_fp = fopen(pathname, "rb");
+       }
        if (CC->download_fp == NULL) {
                cprintf("%d Cannot open %s: %s\n",
                        ERROR + FILE_NOT_FOUND, pathname, strerror(errno));
                return;
        }
-
-       OpenCmdResult(pathname, "image/gif");
+       fread(&MimeTestBuf[0], 1, 32, CC->download_fp);
+       rewind (CC->download_fp);
+       OpenCmdResult(pathname, GuessMimeType(&MimeTestBuf[0], 32));
 }
 
 /*
@@ -464,7 +376,8 @@ void cmd_uopn(char *cmdbuf)
        int a;
 
        extract_token(CC->upl_file, cmdbuf, 0, '|', sizeof CC->upl_file);
-       extract_token(CC->upl_comment, cmdbuf, 1, '|', sizeof CC->upl_comment);
+       extract_token(CC->upl_mimetype, cmdbuf, 1, '|', sizeof CC->upl_mimetype);
+       extract_token(CC->upl_comment, cmdbuf, 2, '|', sizeof CC->upl_comment);
 
        if (CtdlAccessCheck(ac_logged_in)) return;
 
@@ -487,7 +400,7 @@ void cmd_uopn(char *cmdbuf)
        }
 
        for (a = 0; !IsEmptyStr(&CC->upl_file[a]); ++a) {
-               if (CC->upl_file[a] == '/') {
+               if ( (CC->upl_file[a] == '/') || (CC->upl_file[a] == '\\') ) {
                        CC->upl_file[a] = '_';
                }
        }
@@ -536,7 +449,8 @@ void cmd_uimg(char *cmdbuf)
        }
 
        is_this_for_real = extract_int(cmdbuf, 0);
-       extract_token(basenm, cmdbuf, 1, '|', sizeof basenm);
+       extract_token(CC->upl_mimetype, cmdbuf, 1, '|', sizeof CC->upl_mimetype);
+       extract_token(basenm, cmdbuf, 2, '|', sizeof basenm);
        if (CC->upload_fp != NULL) {
                cprintf("%d You already have an upload file open.\n",
                        ERROR + RESOURCE_BUSY);
@@ -547,7 +461,7 @@ void cmd_uimg(char *cmdbuf)
 
        for (a = 0; !IsEmptyStr(&basenm[a]); ++a) {
                basenm[a] = tolower(basenm[a]);
-               if (basenm[a] == '/') {
+               if ( (basenm[a] == '/') || (basenm[a] == '\\') ) {
                        basenm[a] = '_';
                }
        }
@@ -604,7 +518,7 @@ void cmd_uimg(char *cmdbuf)
 /*
  * close the download file
  */
-void cmd_clos(void)
+void cmd_clos(char *cmdbuf)
 {
        char buf[256];
 
@@ -676,15 +590,18 @@ void cmd_ucls(char *cmd)
                        fp = fopen(CC->upl_filedir, "w");
                }
                if (fp != NULL) {
-                       fprintf(fp, "%s %s\n", CC->upl_file,
+                       fprintf(fp, "%s %s %s\n", CC->upl_file,
+                               CC->upl_mimetype,
                                CC->upl_comment);
                        fclose(fp);
                }
 
                /* put together an upload notice */
                snprintf(upload_notice, sizeof upload_notice,
-                       "NEW UPLOAD: '%s'\n %s\n",
-                       CC->upl_file, CC->upl_comment);
+                       "NEW UPLOAD: '%s'\n %s\n%s\n",
+                        CC->upl_file, 
+                        CC->upl_comment, 
+                        CC->upl_mimetype);
                quickie_message(CC->curr_user, NULL, NULL, CC->room.QRname,
                                upload_notice, 0, NULL);
        } else {
@@ -854,3 +771,26 @@ void cmd_nuop(char *cmdbuf)
        CC->upload_type = UPL_NET;
        cprintf("%d Ok\n", CIT_OK);
 }
+
+
+/*****************************************************************************/
+/*                      MODULE INITIALIZATION STUFF                          */
+/*****************************************************************************/
+
+CTDL_MODULE_INIT(file_ops)
+{
+       CtdlRegisterProtoHook(cmd_delf, "DELF", "Autoconverted. TODO: document me.");
+       CtdlRegisterProtoHook(cmd_movf, "MOVF", "Autoconverted. TODO: document me.");
+       CtdlRegisterProtoHook(cmd_open, "OPEN", "Autoconverted. TODO: document me.");
+       CtdlRegisterProtoHook(cmd_clos, "CLOS", "Autoconverted. TODO: document me.");
+       CtdlRegisterProtoHook(cmd_uopn, "UOPN", "Autoconverted. TODO: document me.");
+       CtdlRegisterProtoHook(cmd_ucls, "UCLS", "Autoconverted. TODO: document me.");
+       CtdlRegisterProtoHook(cmd_read, "READ", "Autoconverted. TODO: document me.");
+       CtdlRegisterProtoHook(cmd_writ, "WRIT", "Autoconverted. TODO: document me.");
+       CtdlRegisterProtoHook(cmd_oimg, "OIMG", "Autoconverted. TODO: document me.");
+       CtdlRegisterProtoHook(cmd_ndop, "NDOP", "Autoconverted. TODO: document me.");
+       CtdlRegisterProtoHook(cmd_nuop, "NUOP", "Autoconverted. TODO: document me.");
+       CtdlRegisterProtoHook(cmd_uimg, "UIMG", "Autoconverted. TODO: document me.");
+        /* return our Subversion id for the Log */
+       return "$Id$";
+}