]> code.citadel.org Git - citadel.git/blobdiff - citadel/room_ops.c
* Clarify confusing error message reported in bug #476.
[citadel.git] / citadel / room_ops.c
index 340b99a38dd2b786383766152f32383c8b1fc4d4..0e4eed746b01aa07ec60cb8458502749486adbd5 100644 (file)
@@ -12,6 +12,7 @@
 #include <sys/stat.h>
 #include <ctype.h>
 #include <string.h>
+#include <dirent.h>    /* for cmd_rdir to read contents of the directory */
 
 #if TIME_WITH_SYS_TIME
 # include <sys/time.h>
@@ -27,6 +28,7 @@
 #include <limits.h>
 #include <errno.h>
 #include "citadel.h"
+#include <libcitadel.h>
 #include "server.h"
 #include "database.h"
 #include "config.h"
 #include "msgbase.h"
 #include "citserver.h"
 #include "control.h"
-#include "tools.h"
 #include "citadel_dirs.h"
+#include "threads.h"
+
+#include "ctdl_module.h"
 
 struct floor *floorcache[MAXFLOORS];
 
@@ -179,7 +183,7 @@ void CtdlRoomAccess(struct ctdlroom *roombuf, struct ctdluser *userbuf,
        if ( (userbuf->axlevel >= 6)
           || (userbuf->usernum == roombuf->QRroomaide)
           ) {
-               retval = retval | UA_ADMINALLOWED | UA_DELETEALLOWED;
+               retval = retval | UA_ADMINALLOWED | UA_DELETEALLOWED | UA_POSTALLOWED;
        }
 
 NEWMSG:        /* By the way, we also check for the presence of new messages */
@@ -225,12 +229,16 @@ int getroom(struct ctdlroom *qrbuf, char *room_name)
        struct cdbdata *cdbqr;
        char lowercase_name[ROOMNAMELEN];
        char personal_lowercase_name[ROOMNAMELEN];
-       int a;
+       char *dptr, *sptr, *eptr;
 
-       for (a = 0; room_name[a] && a < sizeof lowercase_name - 1; ++a) {
-               lowercase_name[a] = tolower(room_name[a]);
+       dptr = lowercase_name;
+       sptr = room_name;
+       eptr = (dptr + (sizeof lowercase_name - 1));
+       while (!IsEmptyStr(sptr) && (dptr < eptr)){
+               *dptr = tolower(*sptr);
+               sptr++; dptr++;
        }
-       lowercase_name[a] = 0;
+       *dptr = '\0';
 
        memset(qrbuf, 0, sizeof(struct ctdlroom));
 
@@ -280,19 +288,27 @@ int lgetroom(struct ctdlroom *qrbuf, char *room_name)
 void b_putroom(struct ctdlroom *qrbuf, char *room_name)
 {
        char lowercase_name[ROOMNAMELEN];
-       int a;
+       char *aptr, *bptr;
+       long len;
 
-       for (a = 0; a <= strlen(room_name); ++a) {
-               lowercase_name[a] = tolower(room_name[a]);
+       aptr = room_name;
+       bptr = lowercase_name;
+       while (!IsEmptyStr(aptr))
+       {
+               *bptr = tolower(*aptr);
+               aptr++;
+               bptr++;
        }
+       *bptr='\0';
 
+       len = bptr - lowercase_name;
        if (qrbuf == NULL) {
                cdb_delete(CDB_ROOMS,
-                          lowercase_name, strlen(lowercase_name));
+                          lowercase_name, len);
        } else {
                time(&qrbuf->QRmtime);
                cdb_store(CDB_ROOMS,
-                         lowercase_name, strlen(lowercase_name),
+                         lowercase_name, len,
                          qrbuf, sizeof(struct ctdlroom));
        }
 }
@@ -479,6 +495,15 @@ void delete_msglist(struct ctdlroom *whichroom)
 
 
 
+/*
+ * Message pointer compare function for sort_msglist()
+ */
+int sort_msglist_cmp(const void *m1, const void *m2) {
+       if ((*(const long *)m1) > (*(const long *)m2)) return(1);
+       if ((*(const long *)m1) < (*(const long *)m2)) return(-1);
+       return(0);
+}
+
 
 /*
  * sort message pointers
@@ -486,29 +511,19 @@ void delete_msglist(struct ctdlroom *whichroom)
  */
 int sort_msglist(long listptrs[], int oldcount)
 {
-       int a, b;
-       long hold1, hold2;
        int numitems;
 
        numitems = oldcount;
-       if (numitems < 2)
+       if (numitems < 2) {
                return (oldcount);
+       }
 
        /* do the sort */
-       for (a = numitems - 2; a >= 0; --a) {
-               for (b = 0; b <= a; ++b) {
-                       if (listptrs[b] > (listptrs[b + 1])) {
-                               hold1 = listptrs[b];
-                               hold2 = listptrs[b + 1];
-                               listptrs[b] = hold2;
-                               listptrs[b + 1] = hold1;
-                       }
-               }
-       }
+       qsort(listptrs, numitems, sizeof(long), sort_msglist_cmp);
 
        /* and yank any nulls */
        while ((numitems > 0) && (listptrs[0] == 0L)) {
-               memcpy(&listptrs[0], &listptrs[1],
+               memmove(&listptrs[0], &listptrs[1],
                       (sizeof(long) * (numitems - 1)));
                --numitems;
        }
@@ -919,7 +934,7 @@ void usergoto(char *where, int display_result, int transiently,
 
        if (retmsgs != NULL) *retmsgs = total_messages;
        if (retnew != NULL) *retnew = new_messages;
-       lprintf(CTDL_DEBUG, "<%s> %d new of %d total messages\n",
+       CtdlLogPrintf(CTDL_DEBUG, "<%s> %d new of %d total messages\n",
                CC->room.QRname,
                new_messages, total_messages
        );
@@ -962,6 +977,9 @@ void convert_room_name_macros(char *towhere, size_t maxlen) {
        else if (!strcasecmp(towhere, "_TRASH_")) {
                safestrncpy(towhere, USERTRASHROOM, maxlen);
        }
+       else if (!strcasecmp(towhere, "_DRAFTS_")) {
+               safestrncpy(towhere, USERDRAFTROOM, maxlen);
+       }
        else if (!strcasecmp(towhere, "_BITBUCKET_")) {
                safestrncpy(towhere, config.c_twitroom, maxlen);
        }
@@ -1059,7 +1077,7 @@ void cmd_goto(char *gargs)
                                   ((ra & UA_KNOWN) == 0) &&
                                   (CC->user.axlevel < 6)
                                   ) {
-                               lprintf(CTDL_DEBUG, "Failed to acquire private room\n");
+                               CtdlLogPrintf(CTDL_DEBUG, "Failed to acquire private room\n");
                        } else {
                                memcpy(&CC->room, &QRscratch,
                                        sizeof(struct ctdlroom));
@@ -1073,7 +1091,7 @@ void cmd_goto(char *gargs)
 }
 
 
-void cmd_whok(void)
+void cmd_whok(char *cmdbuf)
 {
        struct ctdluser temp;
        struct cdbdata *cdbus;
@@ -1099,17 +1117,20 @@ void cmd_whok(void)
 /*
  * RDIR command for room directory
  */
-void cmd_rdir(void)
+void cmd_rdir(char *cmdbuf)
 {
        char buf[256];
-       char flnm[256];
        char comment[256];
-       FILE *ls, *fd;
+       FILE *fd;
        struct stat statbuf;
-       char tempfilename[PATH_MAX];
-
+       DIR *filedir = NULL;
+       struct dirent *filedir_entry;
+       int d_namelen;
+       char buf2[SIZ];
+       char mimebuf[64];
+       long len;
+       
        if (CtdlAccessCheck(ac_logged_in)) return;
-       CtdlMakeTempFileName(tempfilename, sizeof tempfilename);
        
        getroom(&CC->room, CC->room.QRname);
        getuser(&CC->user, CC->curr_user);
@@ -1124,62 +1145,72 @@ void cmd_rdir(void)
                cprintf("%d not here.\n", ERROR + HIGHER_ACCESS_REQUIRED);
                return;
        }
-       cprintf("%d %s|%s/%s\n",
-                       LISTING_FOLLOWS, 
-                       config.c_fqdn,
-                       ctdl_file_dir, 
-                       CC->room.QRdirname);
-
-       snprintf(buf, sizeof buf, 
-                        "ls %s/%s >%s 2>/dev/null",
-                        ctdl_file_dir,
-                        CC->room.QRdirname, 
-                        tempfilename);
-       system(buf);
-
-       snprintf(buf, sizeof buf, 
-                        "%s/%s/filedir",
-                        ctdl_file_dir,
-                        CC->room.QRdirname);
+
+       snprintf(buf, sizeof buf, "%s/%s", ctdl_file_dir, CC->room.QRdirname);
+       filedir = opendir (buf);
+       
+       if (filedir == NULL) {
+               cprintf("%d not here.\n", ERROR + HIGHER_ACCESS_REQUIRED);
+               return;
+       }
+       cprintf("%d %s|%s/%s\n", LISTING_FOLLOWS, config.c_fqdn, ctdl_file_dir, CC->room.QRdirname);
+       
+       snprintf(buf, sizeof buf, "%s/%s/filedir", ctdl_file_dir, CC->room.QRdirname);
        fd = fopen(buf, "r");
        if (fd == NULL)
                fd = fopen("/dev/null", "r");
-
-       ls = fopen(tempfilename, "r");
-       while (fgets(flnm, sizeof flnm, ls) != NULL) {
-               flnm[strlen(flnm) - 1] = 0;
-               if (strcasecmp(flnm, "filedir")) {
-                       snprintf(buf, sizeof buf, 
-                                        "%s/%s/%s",
-                                        ctdl_file_dir,
-                                        CC->room.QRdirname,
-                                        flnm);
-                       stat(buf, &statbuf);
+       while ((filedir_entry = readdir(filedir)))
+       {
+               if (strcasecmp(filedir_entry->d_name, "filedir") && filedir_entry->d_name[0] != '.')
+               {
+#ifdef _DIRENT_HAVE_D_NAMELEN
+                       d_namelen = filedir_entry->d_namelen;
+#else
+                       d_namelen = strlen(filedir_entry->d_name);
+#endif
+                       snprintf(buf, sizeof buf, "%s/%s/%s", ctdl_file_dir, CC->room.QRdirname, filedir_entry->d_name);
+                       stat(buf, &statbuf);    /* stat the file */
+                       if (!(statbuf.st_mode & S_IFREG))
+                       {
+                               snprintf(buf2, sizeof buf2,
+                                       "\"%s\" appears in the file directory for room \"%s\" but is not a regular file.  Directories, named pipes, sockets, etc. are not usable in Citadel room directories.\n",
+                                       buf, CC->room.QRname
+                               );
+                               aide_message(buf2, "Unusable data found in room directory");
+                               continue;       /* not a useable file type so don't show it */
+                       }
                        safestrncpy(comment, "", sizeof comment);
-                       fseek(fd, 0L, 0);
-                       while ((fgets(buf, sizeof buf, fd) != NULL)
-                              && (IsEmptyStr(comment))) {
+                       fseek(fd, 0L, 0);       /* rewind descriptions file */
+                       /* Get the description from the descriptions file */
+                       while ((fgets(buf, sizeof buf, fd) != NULL) && (IsEmptyStr(comment))) 
+                       {
                                buf[strlen(buf) - 1] = 0;
-                               if ((!strncasecmp(buf, flnm, strlen(flnm)))
-                                   && (buf[strlen(flnm)] == ' '))
-                                       safestrncpy(comment,
-                                           &buf[strlen(flnm) + 1],
-                                           sizeof comment);
+                               if ((!strncasecmp(buf, filedir_entry->d_name, d_namelen)) && (buf[d_namelen] == ' '))
+                                       safestrncpy(comment, &buf[d_namelen + 1], sizeof comment);
                        }
-                       cprintf("%s|%ld|%s\n", flnm, (long)statbuf.st_size, comment);
+                       len = extract_token (mimebuf, comment, 0,' ', 64);
+                       if ((len <0) || strchr(mimebuf, '/') == NULL)
+                       {
+                               snprintf (mimebuf, 64, "application/octetstream");
+                               len = 0;
+                       }
+                       cprintf("%s|%ld|%s|%s\n", 
+                               filedir_entry->d_name, 
+                               (long)statbuf.st_size, 
+                               mimebuf, 
+                               &comment[len]);
                }
        }
-       fclose(ls);
        fclose(fd);
-       unlink(tempfilename);
-
+       closedir(filedir);
+       
        cprintf("000\n");
 }
 
 /*
  * get room parameters (aide or room aide command)
  */
-void cmd_getr(void)
+void cmd_getr(char *cmdbuf)
 {
        if (CtdlAccessCheck(ac_room_aide)) return;
 
@@ -1225,7 +1256,7 @@ int CtdlRenameRoom(char *old_name, char *new_name, int new_floor) {
        long owner = 0L;
        char actual_old_name[ROOMNAMELEN];
 
-       lprintf(CTDL_DEBUG, "CtdlRenameRoom(%s, %s, %d)\n",
+       CtdlLogPrintf(CTDL_DEBUG, "CtdlRenameRoom(%s, %s, %d)\n",
                old_name, new_name, new_floor);
 
        if (new_floor >= 0) {
@@ -1316,11 +1347,11 @@ int CtdlRenameRoom(char *old_name, char *new_name, int new_floor) {
                lgetfloor(&flbuf, old_floor);
                --flbuf.f_ref_count;
                lputfloor(&flbuf, old_floor);
-               lprintf(CTDL_DEBUG, "Reference count for floor %d is now %d\n", old_floor, flbuf.f_ref_count);
+               CtdlLogPrintf(CTDL_DEBUG, "Reference count for floor %d is now %d\n", old_floor, flbuf.f_ref_count);
                lgetfloor(&flbuf, new_floor);
                ++flbuf.f_ref_count;
                lputfloor(&flbuf, new_floor);
-               lprintf(CTDL_DEBUG, "Reference count for floor %d is now %d\n", new_floor, flbuf.f_ref_count);
+               CtdlLogPrintf(CTDL_DEBUG, "Reference count for floor %d is now %d\n", new_floor, flbuf.f_ref_count);
        }
 
        /* ...and everybody say "YATTA!" */     
@@ -1475,7 +1506,7 @@ void cmd_setr(char *args)
 /* 
  * get the name of the room aide for this room
  */
-void cmd_geta(void)
+void cmd_geta(char *cmdbuf)
 {
        struct ctdluser usbuf;
 
@@ -1536,7 +1567,7 @@ void cmd_seta(char *new_ra)
 /* 
  * retrieve info file for this room
  */
-void cmd_rinf(void)
+void cmd_rinf(char *gargs)
 {
        char filename[128];
        char buf[SIZ];
@@ -1569,7 +1600,7 @@ void schedule_room_for_deletion(struct ctdlroom *qrbuf)
        char old_name[ROOMNAMELEN];
        static int seq = 0;
 
-       lprintf(CTDL_NOTICE, "Scheduling room <%s> for deletion\n",
+       CtdlLogPrintf(CTDL_NOTICE, "Scheduling room <%s> for deletion\n",
                qrbuf->QRname);
 
        safestrncpy(old_name, qrbuf->QRname, sizeof old_name);
@@ -1607,7 +1638,7 @@ void delete_room(struct ctdlroom *qrbuf)
        char filename[100];
        /* TODO: filename magic? does this realy work? */
 
-       lprintf(CTDL_NOTICE, "Deleting room <%s>\n", qrbuf->QRname);
+       CtdlLogPrintf(CTDL_NOTICE, "Deleting room <%s>\n", qrbuf->QRname);
 
        /* Delete the info file */
        assoc_file_name(filename, sizeof filename, qrbuf, ctdl_info_dir);
@@ -1738,11 +1769,11 @@ unsigned create_room(char *new_room_name,
        struct floor flbuf;
        struct visit vbuf;
 
-       lprintf(CTDL_DEBUG, "create_room(name=%s, type=%d, view=%d)\n",
+       CtdlLogPrintf(CTDL_DEBUG, "create_room(name=%s, type=%d, view=%d)\n",
                new_room_name, new_room_type, new_room_view);
 
        if (getroom(&qrbuf, new_room_name) == 0) {
-               lprintf(CTDL_DEBUG, "%s already exists.\n", new_room_name);
+               CtdlLogPrintf(CTDL_DEBUG, "%s already exists.\n", new_room_name);
                return(0);
        }
 
@@ -1870,7 +1901,7 @@ void cmd_cre8(char *args)
 
        if (CtdlAccessCheck(ac_logged_in)) return;
 
-       if (CC->user.axlevel < config.c_createax || CC->internal_pgm) {
+       if (CC->user.axlevel < config.c_createax && !CC->internal_pgm) {
                cprintf("%d You need higher access to create rooms.\n",
                        ERROR + HIGHER_ACCESS_REQUIRED);
                return;
@@ -1950,9 +1981,9 @@ void cmd_einf(char *ok)
                return;
        }
        assoc_file_name(infofilename, sizeof infofilename, &CC->room, ctdl_info_dir);
-       lprintf(CTDL_DEBUG, "opening\n");
+       CtdlLogPrintf(CTDL_DEBUG, "opening\n");
        fp = fopen(infofilename, "w");
-       lprintf(CTDL_DEBUG, "checking\n");
+       CtdlLogPrintf(CTDL_DEBUG, "checking\n");
        if (fp == NULL) {
                cprintf("%d Cannot open %s: %s\n",
                  ERROR + INTERNAL_ERROR, infofilename, strerror(errno));
@@ -1977,7 +2008,7 @@ void cmd_einf(char *ok)
 /* 
  * cmd_lflr()   -  List all known floors
  */
-void cmd_lflr(void)
+void cmd_lflr(char *gargs)
 {
        int a;
        struct floor flbuf;
@@ -2134,3 +2165,35 @@ void cmd_eflr(char *argbuf)
 
        cprintf("%d Ok\n", CIT_OK);
 }
+
+
+/*****************************************************************************/
+/*                      MODULE INITIALIZATION STUFF                          */
+/*****************************************************************************/
+
+CTDL_MODULE_INIT(room_ops)
+{
+       CtdlRegisterProtoHook(cmd_lrms, "LRMS", "Autoconverted. TODO: document me.");
+       CtdlRegisterProtoHook(cmd_lkra, "LKRA", "Autoconverted. TODO: document me.");
+       CtdlRegisterProtoHook(cmd_lkrn, "LKRN", "Autoconverted. TODO: document me.");
+       CtdlRegisterProtoHook(cmd_lkro, "LKRO", "Autoconverted. TODO: document me.");
+       CtdlRegisterProtoHook(cmd_lzrm, "LZRM", "Autoconverted. TODO: document me.");
+       CtdlRegisterProtoHook(cmd_lprm, "LPRM", "Autoconverted. TODO: document me.");
+       CtdlRegisterProtoHook(cmd_goto, "GOTO", "Autoconverted. TODO: document me.");
+       CtdlRegisterProtoHook(cmd_whok, "WHOK", "Autoconverted. TODO: document me.");
+       CtdlRegisterProtoHook(cmd_rdir, "RDIR", "Autoconverted. TODO: document me.");
+       CtdlRegisterProtoHook(cmd_getr, "GETR", "Autoconverted. TODO: document me.");
+       CtdlRegisterProtoHook(cmd_setr, "SETR", "Autoconverted. TODO: document me.");
+       CtdlRegisterProtoHook(cmd_geta, "GETA", "Autoconverted. TODO: document me.");
+       CtdlRegisterProtoHook(cmd_seta, "SETA", "Autoconverted. TODO: document me.");
+       CtdlRegisterProtoHook(cmd_rinf, "RINF", "Autoconverted. TODO: document me.");
+       CtdlRegisterProtoHook(cmd_kill, "KILL", "Autoconverted. TODO: document me.");
+       CtdlRegisterProtoHook(cmd_cre8, "CRE8", "Autoconverted. TODO: document me.");
+       CtdlRegisterProtoHook(cmd_einf, "EINF", "Autoconverted. TODO: document me.");
+       CtdlRegisterProtoHook(cmd_lflr, "LFLR", "Autoconverted. TODO: document me.");
+       CtdlRegisterProtoHook(cmd_cflr, "CFLR", "Autoconverted. TODO: document me.");
+       CtdlRegisterProtoHook(cmd_kflr, "KFLR", "Autoconverted. TODO: document me.");
+       CtdlRegisterProtoHook(cmd_eflr, "EFLR", "Autoconverted. TODO: document me.");
+        /* return our Subversion id for the Log */
+       return "$Id$";
+}