* Holy war on strlen: use IsEmptyStr where apropriate.
[citadel.git] / citadel / rooms.c
index 971ce88a4a98361c8b759dee2d2b085c73debc3a..1226455c3afd25202991f9f98060dbba4870e8f4 100644 (file)
 #include "snprintf.h"
 #endif
 #include "screen.h"
+#include "citadel_dirs.h"
 
 #define IFNEXPERT if ((userflags&US_EXPERT)==0)
 
 
-void sttybbs(int cmd);
-void hit_any_key(void);
+void stty_ctdl(int cmd);
 void dotgoto(CtdlIPC *ipc, char *towhere, int display_name, int fromungoto);
-void progress(unsigned long curr, unsigned long cmax);
+void progress(CtdlIPC* ipc, unsigned long curr, unsigned long cmax);
 int pattern(char *search, char *patn);
 int file_checksum(char *filename);
 int nukedir(char *dirname);
@@ -79,16 +79,16 @@ void load_floorlist(CtdlIPC *ipc)
                strcpy(floorlist[0], "Main Floor");
                return;
        }
-       while (*listing && strlen(listing)) {
-               extract_token(buf, listing, 0, '\n');
+       while (*listing && !IsEmptyStr(listing)) {
+               extract_token(buf, listing, 0, '\n', sizeof buf);
                remove_token(listing, 0, '\n');
-               extract(floorlist[extract_int(buf, 0)], buf, 1);
+               extract_token(floorlist[extract_int(buf, 0)], buf, 1, '|', SIZ);
        }
        free(listing);
 }
 
 
-void room_tree_list(struct roomlisting *rp)
+void room_tree_list(struct ctdlroomlisting *rp)
 {
        static int c = 0;
        char rmname[ROOMNAMELEN];
@@ -142,7 +142,7 @@ void room_tree_list(struct roomlisting *rp)
 /* 
  * Room ordering stuff (compare first by floor, then by order)
  */
-int rordercmp(struct roomlisting *r1, struct roomlisting *r2)
+int rordercmp(struct ctdlroomlisting *r1, struct ctdlroomlisting *r2)
 {
        if ((r1 == NULL) && (r2 == NULL))
                return (0);
@@ -165,12 +165,12 @@ int rordercmp(struct roomlisting *r1, struct roomlisting *r2)
 /*
  * Common code for all room listings
  */
-static void listrms(struct march *listing, int new_only, int floor_only)
+static void listrms(struct march *listing, int new_only, int floor_only, unsigned int flags, char *match)
 {
        struct march *mptr;
-       struct roomlisting *rl = NULL;
-       struct roomlisting *rp;
-       struct roomlisting *rs;
+       struct ctdlroomlisting *rl = NULL;
+       struct ctdlroomlisting *rp;
+       struct ctdlroomlisting *rs;
        int list_it;
 
        for (mptr = listing; mptr != NULL; mptr = mptr->next) {
@@ -188,8 +188,14 @@ static void listrms(struct march *listing, int new_only, int floor_only)
                   && (mptr->march_floor != floor_only))
                        list_it = 0;
 
+               if (flags && (mptr->march_flags & flags) == 0)
+                   list_it = 0;
+
+           if (match && (pattern(mptr->march_name, match) == -1))
+                       list_it = 0;
+
                if (list_it) {
-                       rp = malloc(sizeof(struct roomlisting));
+                       rp = malloc(sizeof(struct ctdlroomlisting));
                        strncpy(rp->rlname, mptr->march_name, ROOMNAMELEN);
                        rp->rlflags = mptr->march_flags;
                        rp->rlfloor = mptr->march_floor;
@@ -248,7 +254,7 @@ void list_other_floors(void)
 
 /*
  * List known rooms.  kn_floor_mode should be set to 0 for a 'flat' listing,
- * 1 to list rooms on the current floor, or 1 to list rooms on all floors.
+ * 1 to list rooms on the current floor, or 2 to list rooms on all floors.
  */
 void knrooms(CtdlIPC *ipc, int kn_floor_mode)
 {
@@ -271,10 +277,10 @@ void knrooms(CtdlIPC *ipc, int kn_floor_mode)
        if (kn_floor_mode == 0) {
                color(BRIGHT_CYAN);
                pprintf("\n   Rooms with unread messages:\n");
-               listrms(listing, LISTRMS_NEW_ONLY, -1);
+               listrms(listing, LISTRMS_NEW_ONLY, -1, 0, NULL);
                color(BRIGHT_CYAN);
                pprintf("\n\n   No unseen messages in:\n");
-               listrms(listing, LISTRMS_OLD_ONLY, -1);
+               listrms(listing, LISTRMS_OLD_ONLY, -1, 0, NULL);
                pprintf("\n");
        }
 
@@ -282,11 +288,11 @@ void knrooms(CtdlIPC *ipc, int kn_floor_mode)
                color(BRIGHT_CYAN);
                pprintf("\n   Rooms with unread messages on %s:\n",
                        floorlist[(int) curr_floor]);
-               listrms(listing, LISTRMS_NEW_ONLY, curr_floor);
+               listrms(listing, LISTRMS_NEW_ONLY, curr_floor, 0, NULL);
                color(BRIGHT_CYAN);
                pprintf("\n\n   Rooms with no new messages on %s:\n",
                        floorlist[(int) curr_floor]);
-               listrms(listing, LISTRMS_OLD_ONLY, curr_floor);
+               listrms(listing, LISTRMS_OLD_ONLY, curr_floor, 0, NULL);
                color(BRIGHT_CYAN);
                pprintf("\n\n   Other floors:\n");
                list_other_floors();
@@ -299,7 +305,7 @@ void knrooms(CtdlIPC *ipc, int kn_floor_mode)
                                color(BRIGHT_CYAN);
                                pprintf("\n   Rooms on %s:\n",
                                        floorlist[a]);
-                               listrms(listing, LISTRMS_ALL, a);
+                               listrms(listing, LISTRMS_ALL, a, 0, NULL);
                                pprintf("\n");
                        }
                }
@@ -313,7 +319,7 @@ void knrooms(CtdlIPC *ipc, int kn_floor_mode)
        };
 
        color(DIM_WHITE);
-       IFNEXPERT hit_any_key();
+       IFNEXPERT hit_any_key(ipc);
 }
 
 
@@ -326,14 +332,14 @@ void listzrooms(CtdlIPC *ipc)
 
 
        /* Ask the server for a room list */
-       r = CtdlIPCKnownRooms(ipc, UnsubscribedRooms, 1, &listing, buf);
+       r = CtdlIPCKnownRooms(ipc, UnsubscribedRooms, (-1), &listing, buf);
        if (r / 100 != 1) {
                listing = NULL;
        }
 
        color(BRIGHT_CYAN);
        pprintf("\n   Forgotten public rooms:\n");
-       listrms(listing, LISTRMS_ALL, -1);
+       listrms(listing, LISTRMS_ALL, -1, 0, NULL);
        pprintf("\n");
 
        /* Free the room list */
@@ -344,7 +350,71 @@ void listzrooms(CtdlIPC *ipc)
        };
 
        color(DIM_WHITE);
-       IFNEXPERT hit_any_key();
+       IFNEXPERT hit_any_key(ipc);
+}
+
+void dotknown(CtdlIPC *ipc, int what, char *match)
+{                              /* list rooms according to attribute */
+       struct march *listing = NULL;
+       struct march *mptr;
+       int r;          /* IPC response code */
+       char buf[SIZ];
+
+       /* Ask the server for a room list */
+       r = CtdlIPCKnownRooms(ipc, AllAccessibleRooms, (-1), &listing, buf);
+       if (r / 100 != 1) {
+               listing = NULL;
+       }
+
+       color(BRIGHT_CYAN);
+
+       switch (what) {
+    case 0:
+       pprintf("\n   Anonymous rooms:\n");
+           listrms(listing, LISTRMS_ALL, -1, QR_ANONONLY|QR_ANONOPT, NULL);
+       pprintf("\n");
+               break;
+    case 1:
+       pprintf("\n   Directory rooms:\n");
+           listrms(listing, LISTRMS_ALL, -1, QR_DIRECTORY, NULL);
+       pprintf("\n");
+               break;
+    case 2:
+       pprintf("\n   Matching \"%s\" rooms:\n", match);
+           listrms(listing, LISTRMS_ALL, -1, 0, match);
+       pprintf("\n");
+               break;
+    case 3:
+       pprintf("\n   Preferred only rooms:\n");
+           listrms(listing, LISTRMS_ALL, -1, QR_PREFONLY, NULL);
+       pprintf("\n");
+               break;
+    case 4:
+       pprintf("\n   Private rooms:\n");
+           listrms(listing, LISTRMS_ALL, -1, QR_PRIVATE, NULL);
+       pprintf("\n");
+               break;
+    case 5:
+       pprintf("\n   Read only rooms:\n");
+           listrms(listing, LISTRMS_ALL, -1, QR_READONLY, NULL);
+       pprintf("\n");
+               break;
+    case 6:
+       pprintf("\n   Shared rooms:\n");
+           listrms(listing, LISTRMS_ALL, -1, QR_NETWORK, NULL);
+       pprintf("\n");
+               break;
+       }
+
+       /* Free the room list */
+       while (listing) {
+               mptr = listing->next;
+               free(listing);
+               listing = mptr;
+       };
+
+       color(DIM_WHITE);
+       IFNEXPERT hit_any_key(ipc);
 }
 
 
@@ -429,7 +499,7 @@ void editthisroom(CtdlIPC *ipc)
        int rbump = 0;
        char raide[USERNAME_SIZE];
        char buf[SIZ];
-       struct quickroom *attr = NULL;
+       struct ctdlroom *attr = NULL;
        struct ExpirePolicy *eptr = NULL;
        int r;                          /* IPC response code */
 
@@ -448,7 +518,7 @@ void editthisroom(CtdlIPC *ipc)
        } else {
                strcpy(raide, "");
        }
-       if (strlen(raide) == 0) {
+       if (IsEmptyStr(raide)) {
                strcpy(raide, "none");
        }
 
@@ -498,10 +568,17 @@ void editthisroom(CtdlIPC *ipc)
                                        "Preferred users only", QR_PREFONLY);
        attr->QRflags = set_room_attr(ipc, attr->QRflags,
                                        "Read-only room", QR_READONLY);
-       attr->QRflags = set_room_attr(ipc, attr->QRflags,
-                                       "Directory room", QR_DIRECTORY);
+       attr->QRflags2 = set_room_attr(ipc, attr->QRflags2,
+                               "Allow message deletion by anyone who can post",
+                               QR2_COLLABDEL);
        attr->QRflags = set_room_attr(ipc, attr->QRflags,
                                        "Permanent room", QR_PERMANENT);
+       attr->QRflags2 = set_room_attr(ipc, attr->QRflags2,
+                                                                  "Subject Required (Force "
+                                                                  "users to specify a message "
+                                   "subject)", QR2_SUBJECTREQ);
+       attr->QRflags = set_room_attr(ipc, attr->QRflags,
+                                       "Directory room", QR_DIRECTORY);
        if (attr->QRflags & QR_DIRECTORY) {
                strprompt("Directory name", attr->QRdirname, 14);
                attr->QRflags =
@@ -527,7 +604,7 @@ void editthisroom(CtdlIPC *ipc)
                                       "Ask users whether to make messages anonymous",
                                       QR_ANONOPT);
        }
-       attr->QRorder = intprompt("Listing order", attr->QRorder, 1, 127);
+       attr->QRorder = intprompt("Listing order", attr->QRorder, 0, 127);
 
        /* Ask about the room aide */
        do {
@@ -624,7 +701,7 @@ void dotungoto(CtdlIPC *ipc, char *towhere)
                scr_printf("Must specify a room to ungoto.\n");
                return;
       }
-       if (strlen(towhere) == 0)
+       if (IsEmptyStr(towhere))
       {
                scr_printf("Must specify a room to ungoto.\n");
                return;
@@ -649,7 +726,7 @@ void dotungoto(CtdlIPC *ipc, char *towhere)
                scr_printf("%s\n", buf);
                return;
        }
-       r = CtdlIPCSetLastRead(ipc, uglistlsn[found], buf);
+       r = CtdlIPCSetLastRead(ipc, uglistlsn[found] ? uglistlsn[found] : 1, buf);
        if (r / 100 != 2) {
                scr_printf("%s\n", buf);
        }
@@ -675,7 +752,7 @@ void ungoto(CtdlIPC *ipc)
                scr_printf("%s\n", buf);
                return;
        }
-       r = CtdlIPCSetLastRead(ipc, uglistlsn[uglistsize-1], buf);
+       r = CtdlIPCSetLastRead(ipc, uglistlsn[uglistsize-1] ? uglistlsn[uglistsize-1] : 1, buf);
        if (r / 100 != 2) {
                scr_printf("%s\n", buf);
        }
@@ -702,7 +779,7 @@ int save_buffer(void *file, size_t filelen, const char *pathname)
                return 0;
        }
        do {
-               block = fwrite(file + bytes_written, 1,
+               block = fwrite((char *)file + bytes_written, 1,
                                filelen - bytes_written, fp);
                bytes_written += block;
        } while (errno == EINTR && bytes_written < filelen);
@@ -722,16 +799,34 @@ int save_buffer(void *file, size_t filelen, const char *pathname)
  */
 void destination_directory(char *dest, const char *supplied_filename)
 {
-       scr_printf("Enter the name of the directory to save '%s'\n"
-               "to, or press return for the current directory.\n",
-               supplied_filename);
-       newprompt("Directory: ", dest, PATH_MAX);
-       if (strlen(dest) == 0) {
-               dest[0] = '.';
-               dest[1] = 0;
-       }
-       strcat(dest, "/");
-       strcat(dest, supplied_filename);
+       static char save_dir[SIZ] = { 0 };
+
+       if (IsEmptyStr(save_dir)) {
+               if (getenv("HOME") == NULL) {
+                       strcpy(save_dir, ".");
+               }
+               else {
+                       sprintf(save_dir, "%s/Desktop", getenv("HOME"));
+                       if (access(save_dir, W_OK) != 0) {
+                               sprintf(save_dir, "%s", getenv("HOME"));
+                               if (access(save_dir, W_OK) != 0) {
+                                       sprintf(save_dir, ".");
+                               }
+                       }
+               }
+       }
+
+       sprintf(dest, "%s/%s", save_dir, supplied_filename);
+       strprompt("Save as", dest, PATH_MAX);
+
+       /* Remember the directory for next time */
+       strcpy(save_dir, dest);
+       if (strrchr(save_dir, '/') != NULL) {
+               strcpy(strrchr(save_dir, '/'), "");
+       }
+       else {
+               strcpy(save_dir, ".");
+       }
 }
 
 
@@ -808,9 +903,9 @@ void download(CtdlIPC *ipc, int proto)
                snprintf(transmit_cmd, sizeof transmit_cmd, "exec cat %s", tempname);
 
        screen_reset();
-       sttybbs(SB_RESTORE);
+       stty_ctdl(SB_RESTORE);
        system(transmit_cmd);
-       sttybbs(SB_NO_INTR);
+       stty_ctdl(SB_NO_INTR);
        screen_set();
 
        /* clean up the temporary directory */
@@ -824,32 +919,37 @@ void download(CtdlIPC *ipc, int proto)
  */
 void roomdir(CtdlIPC *ipc)
 {
-       char flnm[SIZ];
+       char flnm[256];
        char flsz[32];
-       char comment[SIZ];
-       char buf[SIZ];
+       char comment[256];
+       char buf[256];
+       char *listing = NULL;   /* Returned directory listing */
+       int r;
 
-       CtdlIPC_putline(ipc, "RDIR");
-       CtdlIPC_getline(ipc, buf);
-       if (buf[0] != '1') {
-               pprintf("%s\n", &buf[4]);
+       r = CtdlIPCReadDirectory(ipc, &listing, buf);
+       if (r / 100 != 1) {
+               pprintf("%s\n", buf);
                return;
        }
 
-       extract(comment, &buf[4], 0);
-       extract(flnm, &buf[4], 1);
+       extract_token(comment, buf, 0, '|', sizeof comment);
+       extract_token(flnm, buf, 1, '|', sizeof flnm);
        pprintf("\nDirectory of %s on %s\n", flnm, comment);
        pprintf("-----------------------\n");
-       while (CtdlIPC_getline(ipc, buf), strcmp(buf, "000")) {
-               extract(flnm, buf, 0);
-               extract(flsz, buf, 1);
-               extract(comment, buf, 2);
+       while (listing && *listing && !IsEmptyStr(listing)) {
+               extract_token(buf, listing, 0, '\n', sizeof buf);
+               remove_token(listing, 0, '\n');
+
+               extract_token(flnm, buf, 0, '|', sizeof flnm);
+               extract_token(flsz, buf, 1, '|', sizeof flsz);
+               extract_token(comment, buf, 2, '|', sizeof comment);
                if (strlen(flnm) <= 14)
                        pprintf("%-14s %8s %s\n", flnm, flsz, comment);
                else
                        pprintf("%s\n%14s %8s %s\n", flnm, "", flsz,
                                comment);
        }
+       if (listing) free(listing);
 }
 
 
@@ -922,6 +1022,7 @@ void forget(CtdlIPC *ipc)
        if (yesno() == 0)
                return;
 
+       remove_march(room_name, 0);
        if (CtdlIPCForgetRoom(ipc, buf) / 100 != 2) {
                scr_printf("%s\n", buf);
                return;
@@ -953,7 +1054,7 @@ void entroom(CtdlIPC *ipc)
        }
 
        newprompt("Name for new room? ", new_room_name, ROOMNAMELEN - 1);
-       if (strlen(new_room_name) == 0) {
+       if (IsEmptyStr(new_room_name)) {
                return;
        }
        for (a = 0; a < strlen(new_room_name); ++a) {
@@ -1033,7 +1134,7 @@ void readinfo(CtdlIPC *ipc)
        else
                strcpy(raide, "");
 
-       if (strlen(raide) > 0)
+       if (!IsEmptyStr(raide))
                scr_printf("Room aide is %s.\n\n", raide);
 
        r = CtdlIPCRoomInfo(ipc, &text, buf);
@@ -1054,7 +1155,7 @@ void readinfo(CtdlIPC *ipc)
  */
 void whoknows(CtdlIPC *ipc)
 {
-       char buf[SIZ];
+       char buf[256];
        char *listing = NULL;
        int r;
 
@@ -1063,8 +1164,8 @@ void whoknows(CtdlIPC *ipc)
                pprintf("%s\n", buf);
                return;
        }
-       while (strlen(listing) > 0) {
-               extract_token(buf, listing, 0, '\n');
+       while (!IsEmptyStr(listing)) {
+               extract_token(buf, listing, 0, '\n', sizeof buf);
                remove_token(listing, 0, '\n');
                if (sigcaught == 0)
                        pprintf("%s\n", buf);
@@ -1080,7 +1181,7 @@ void do_edit(CtdlIPC *ipc,
        char cmd[SIZ];
        int b, cksum, editor_exit;
 
-       if (strlen(editor_paths[0]) == 0) {
+       if (IsEmptyStr(editor_paths[0])) {
                scr_printf("Do you wish to re-enter %s? ", desc);
                if (yesno() == 0)
                        return;
@@ -1089,19 +1190,19 @@ void do_edit(CtdlIPC *ipc,
        fp = fopen(temp, "w");
        fclose(fp);
 
-       CtdlIPC_putline(ipc, check_cmd);
-       CtdlIPC_getline(ipc, cmd);
+       CtdlIPC_chat_send(ipc, check_cmd);
+       CtdlIPC_chat_recv(ipc, cmd);
        if (cmd[0] != '2') {
                scr_printf("%s\n", &cmd[4]);
                return;
        }
 
-       if (strlen(editor_paths[0]) > 0) {
-               CtdlIPC_putline(ipc, read_cmd);
-               CtdlIPC_getline(ipc, cmd);
+       if (!IsEmptyStr(editor_paths[0])) {
+               CtdlIPC_chat_send(ipc, read_cmd);
+               CtdlIPC_chat_recv(ipc, cmd);
                if (cmd[0] == '1') {
                        fp = fopen(temp, "w");
-                       while (CtdlIPC_getline(ipc, cmd), strcmp(cmd, "000")) {
+                       while (CtdlIPC_chat_recv(ipc, cmd), strcmp(cmd, "000")) {
                                fprintf(fp, "%s\n", cmd);
                        }
                        fclose(fp);
@@ -1110,13 +1211,13 @@ void do_edit(CtdlIPC *ipc,
 
        cksum = file_checksum(temp);
 
-       if (strlen(editor_paths[0]) > 0) {
+       if (!IsEmptyStr(editor_paths[0])) {
                char tmp[SIZ];
 
                snprintf(tmp, sizeof tmp, "WINDOW_TITLE=%s", desc);
                putenv(tmp);
                screen_reset();
-               sttybbs(SB_RESTORE);
+               stty_ctdl(SB_RESTORE);
                editor_pid = fork();
                if (editor_pid == 0) {
                        chmod(temp, 0600);
@@ -1130,7 +1231,7 @@ void do_edit(CtdlIPC *ipc,
                        } while ((b != editor_pid) && (b >= 0));
                editor_pid = (-1);
                scr_printf("Executed %s\n", editor_paths[0]);
-               sttybbs(0);
+               stty_ctdl(0);
                screen_set();
        } else {
                scr_printf("Entering %s.  "
@@ -1145,8 +1246,8 @@ void do_edit(CtdlIPC *ipc,
        }
 
        else {
-               CtdlIPC_putline(ipc, write_cmd);
-               CtdlIPC_getline(ipc, cmd);
+               CtdlIPC_chat_send(ipc, write_cmd);
+               CtdlIPC_chat_recv(ipc, cmd);
                if (cmd[0] != '4') {
                        scr_printf("%s\n", &cmd[4]);
                        return;
@@ -1155,10 +1256,10 @@ void do_edit(CtdlIPC *ipc,
                fp = fopen(temp, "r");
                while (fgets(cmd, SIZ - 1, fp) != NULL) {
                        cmd[strlen(cmd) - 1] = 0;
-                       CtdlIPC_putline(ipc, cmd);
+                       CtdlIPC_chat_send(ipc, cmd);
                }
                fclose(fp);
-               CtdlIPC_putline(ipc, "000");
+               CtdlIPC_chat_send(ipc, "000");
        }
 
        unlink(temp);
@@ -1195,7 +1296,7 @@ void create_floor(CtdlIPC *ipc)
        }
 
        newprompt("Name for new floor: ", newfloorname, 255);
-       if (!*newprompt) return;
+       if (!*newfloorname) return;
        r = CtdlIPCCreateFloor(ipc, 1, newfloorname, buf);
        if (r / 100 == 2) {
                scr_printf("Floor has been created.\n");
@@ -1279,7 +1380,7 @@ void kill_floor(CtdlIPC *ipc)
                floornum_to_delete = (-1);
                scr_printf("(Press return to abort)\n");
                newprompt("Delete which floor? ", buf, 255);
-               if (strlen(buf) == 0)
+               if (IsEmptyStr(buf))
                        return;
                for (a = 0; a < 128; ++a)
                        if (!strcasecmp(&floorlist[a][0], buf))