#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>
#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"
struct floor *floorcache[MAXFLOORS];
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 */
+/*
+ * Message pointer compare function for sort_msglist()
+ */
+int sort_msglist_cmp(long *m1, long *m2) {
+ if (*m1 > *m2) return(1);
+ if (*m1 < *m2) return(-1);
+ return(0);
+}
+
/*
* sort message pointers
*/
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)) {
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
);
((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));
void cmd_rdir(void)
{
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);
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, "Command RDIR found something that is not a useable file. It should be cleaned up.\n RDIR found this non regular file:\n%s\n", buf);
+ aide_message(buf2, "RDIR found bad file");
+ 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");
}
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) {
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!" */
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);
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);
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);
}
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));