* move room listing related stuff into its own file
[citadel.git] / webcit / roomops.c
index 48497c9f6885f87bdfaa568380a284837b2425f4..f6347c660fc165d6fef89d0260ff975c3065c6d0 100644 (file)
@@ -5,6 +5,7 @@
 
 #include "webcit.h"
 #include "webserver.h"
+#include "roomops.h"
 #define MAX_FLOORS 128
 
 char floorlist[MAX_FLOORS][SIZ];       /* list of our floor names */
@@ -15,22 +16,6 @@ char *viewdefs[9];                   /* the different kinds of available views */
  * Basically we pull LFLR/LKRA etc. and set up a room HashList with these keys.
  */
 
-#define FLOOR_PARAM_LEN 3
-const ConstStr FLOOR_PARAM_NAMES[] = {{HKEY("ID")},
-                                     {HKEY("NAME")}, 
-                                     {HKEY("ROOMS")}};
-
-#define ROOM_PARAM_LEN 8
-const ConstStr ROOM_PARAM_NAMES[] = {{HKEY("NAME")},
-                                    {HKEY("FLAG")},
-                                    {HKEY("FLOOR")},
-                                    {HKEY("LISTORDER")},
-                                    {HKEY("ACL")},
-                                    {HKEY("CURVIEW")},
-                                    {HKEY("DEFVIEW")},
-                                    {HKEY("LASTCHANGE")}};
-
-
 void display_whok(void);
 
 /*
@@ -102,52 +87,6 @@ void load_floorlist(StrBuf *Buf)
 }
 
 
-/*
- * Free a session's march list
- */
-void free_march_list(wcsession *wcf)
-{
-       struct march *mptr;
-
-       while (wcf->march != NULL) {
-               mptr = wcf->march->next;
-               free(wcf->march);
-               wcf->march = mptr;
-       }
-
-}
-
-
-
-/*
- * remove a room from the march list
- */
-void remove_march(const StrBuf *aaa)
-{
-       struct march *mptr, *mptr2;
-
-       if (WC->march == NULL)
-               return;
-
-       if (!strcasecmp(WC->march->march_name, ChrPtr(aaa))) {
-               mptr = WC->march->next;
-               free(WC->march);
-               WC->march = mptr;
-               return;
-       }
-       mptr2 = WC->march;
-       for (mptr = WC->march; mptr != NULL; mptr = mptr->next) {
-               if (!strcasecmp(mptr->march_name, ChrPtr(aaa))) {
-                       mptr2->next = mptr->next;
-                       free(mptr);
-                       mptr = mptr2;
-               } else {
-                       mptr2 = mptr;
-               }
-       }
-}
-
-
 
 
 /*
@@ -856,138 +795,6 @@ long gotoroom(const StrBuf *gname)
 }
 
 
-/**
- * \brief Locate the room on the march list which we most want to go to.  
- * Each room
- * is measured given a "weight" of preference based on various factors.
- * \param desired_floor the room number on the citadel server
- * \return the roomname
- */
-char *pop_march(int desired_floor)
-{
-       static char TheRoom[128];
-       int TheFloor = 0;
-       int TheOrder = 32767;
-       int TheWeight = 0;
-       int weight;
-       struct march *mptr = NULL;
-
-       strcpy(TheRoom, "_BASEROOM_");
-       if (WC->march == NULL)
-               return (TheRoom);
-
-       for (mptr = WC->march; mptr != NULL; mptr = mptr->next) {
-               weight = 0;
-               if ((strcasecmp(mptr->march_name, "_BASEROOM_")))
-                       weight = weight + 10000;
-               if (mptr->march_floor == desired_floor)
-                       weight = weight + 5000;
-
-               weight = weight + ((128 - (mptr->march_floor)) * 128);
-               weight = weight + (128 - (mptr->march_order));
-
-               if (weight > TheWeight) {
-                       TheWeight = weight;
-                       strcpy(TheRoom, mptr->march_name);
-                       TheFloor = mptr->march_floor;
-                       TheOrder = mptr->march_order;
-               }
-       }
-       return (TheRoom);
-}
-
-
-
-/*
- * Goto next room having unread messages.
- *
- * We want to skip over rooms that the user has already been to, and take the
- * user back to the lobby when done.  The room we end up in is placed in
- * newroom - which is set to 0 (the lobby) initially.
- * We start the search in the current room rather than the beginning to prevent
- * two or more concurrent users from dragging each other back to the same room.
- */
-void gotonext(void)
-{
-       char buf[256];
-       struct march *mptr = NULL;
-       struct march *mptr2 = NULL;
-       char room_name[128];
-       StrBuf *next_room;
-       int ELoop = 0;
-
-       /*
-        * First check to see if the march-mode list is already allocated.
-        * If it is, pop the first room off the list and go there.
-        */
-       if (havebstr("startmsg")) {
-                readloop(readnew);
-                return;
-       }
-
-       if (WC->march == NULL) {
-               serv_puts("LKRN");
-               serv_getln(buf, sizeof buf);
-               if (buf[0] == '1')
-                       while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
-                               if (IsEmptyStr(buf)) {
-                                       if (ELoop > 10000)
-                                               return;
-                                       if (ELoop % 100 == 0)
-                                               sleeeeeeeeeep(1);
-                                       ELoop ++;
-                                       continue;                                       
-                               }
-                               extract_token(room_name, buf, 0, '|', sizeof room_name);
-                               if (strcasecmp(room_name, ChrPtr(WC->wc_roomname))) {
-                                       mptr = (struct march *) malloc(sizeof(struct march));
-                                       mptr->next = NULL;
-                                       safestrncpy(mptr->march_name, room_name, sizeof mptr->march_name);
-                                       mptr->march_floor = extract_int(buf, 2);
-                                       mptr->march_order = extract_int(buf, 3);
-                                       if (WC->march == NULL) 
-                                               WC->march = mptr;
-                                       else 
-                                               mptr2->next = mptr;
-                                       mptr2 = mptr;
-                               }
-                               buf[0] = '\0';
-                       }
-               /*
-                * add _BASEROOM_ to the end of the march list, so the user will end up
-                * in the system base room (usually the Lobby>) at the end of the loop
-                */
-               mptr = (struct march *) malloc(sizeof(struct march));
-               mptr->next = NULL;
-               mptr->march_order = 0;
-               mptr->march_floor = 0;
-               strcpy(mptr->march_name, "_BASEROOM_");
-               if (WC->march == NULL) {
-                       WC->march = mptr;
-               } else {
-                       mptr2 = WC->march;
-                       while (mptr2->next != NULL)
-                               mptr2 = mptr2->next;
-                       mptr2->next = mptr;
-               }
-               /*
-                * ...and remove the room we're currently in, so a <G>oto doesn't make us
-                * walk around in circles
-                */
-               remove_march(WC->wc_roomname);
-       }
-       if (WC->march != NULL) {
-               next_room = NewStrBufPlain(pop_march(-1), -1);/*TODO: migrate march to strbuf */
-       } else {
-               next_room = NewStrBufPlain(HKEY("_BASEROOM_"));
-       }
-
-
-       smart_goto(next_room);
-       FreeStrBuf(&next_room);
-}
-
-
 /*
  * goto next room
  */
@@ -1010,41 +817,6 @@ void slrp_highest(void)
 }
 
 
-/*
- * un-goto the previous room
- */
-void ungoto(void)
-{
-       StrBuf *Buf;
-
-       if (havebstr("startmsg")) {
-               readloop(readnew);
-               return;
-       }
-
-       if (!strcmp(WC->ugname, "")) {
-               smart_goto(WC->wc_roomname);
-               return;
-       }
-       serv_printf("GOTO %s", WC->ugname);
-       Buf = NewStrBuf();
-       StrBuf_ServGetln(Buf);
-       if (GetServerStatus(Buf, NULL) != 2) {
-               smart_goto(WC->wc_roomname);
-               FreeStrBuf(&Buf);
-               return;
-       }
-       if (WC->uglsn >= 0L) {
-               serv_printf("SLRP %ld", WC->uglsn);
-               StrBuf_ServGetln(Buf);
-       }
-       FlushStrBuf(Buf);
-       StrBufAppendBufPlain(Buf, WC->ugname, -1, 0);
-       strcpy(WC->ugname, "");
-       smart_goto(Buf);
-       FreeStrBuf(&Buf);
-}
-
 typedef struct __room_states {
        char password[SIZ];
        char dirname[SIZ];
@@ -3107,7 +2879,7 @@ void change_view(void) {
  * \param max_folders how many folders???
  * \param num_floors hom many floors???
  */
-void do_folder_view(struct folder *fold, int max_folders, int num_floors) {
+void do_folder_view(struct __ofolder *fold, int max_folders, int num_floors) {
        char buf[SIZ];
        int levels;
        int i;
@@ -3226,7 +2998,7 @@ void do_folder_view(struct folder *fold, int max_folders, int num_floors) {
  * \param max_folders how many folders???
  * \param num_floors hom many floors???
  */
-void do_rooms_view(struct folder *fold, int max_folders, int num_floors) {
+void do_rooms_view(struct __ofolder *fold, int max_folders, int num_floors) {
        char buf[256];
        char floor_name[256];
        char old_floor_name[256];
@@ -3347,7 +3119,7 @@ void set_floordiv_expanded(void) {
  * \param max_folders how many folders???
  * \param num_floors hom many floors???
  */
-void do_iconbar_view(struct folder *fold, int max_folders, int num_floors) {
+void do_iconbar_view(struct __ofolder *fold, int max_folders, int num_floors) {
        char buf[256];
        char floor_name[256];
        char old_floor_name[256];
@@ -3487,8 +3259,8 @@ void list_all_rooms_by_floor(const char *viewpref) {
        StrBuf *Buf;
        char buf[SIZ];
        int swap = 0;
-       struct folder *fold = NULL;
-       struct folder ftmp;
+       struct __ofolder *fold = NULL;
+       struct __ofolder ftmp;
        int max_folders = 0;
        int alloc_folders = 0;
        int *floor_mapping;
@@ -3516,8 +3288,8 @@ void list_all_rooms_by_floor(const char *viewpref) {
        /** Start with the mailboxes */
        max_folders = 1;
        alloc_folders = 1;
-       fold = malloc(sizeof(struct folder));
-       memset(fold, 0, sizeof(struct folder));
+       fold = malloc(sizeof(struct __ofolder));
+       memset(fold, 0, sizeof(struct __ofolder));
        strcpy(fold[0].name, "My folders");
        fold[0].is_mailbox = 1;
 
@@ -3528,9 +3300,9 @@ void list_all_rooms_by_floor(const char *viewpref) {
                        if (max_folders >= alloc_folders) {
                                alloc_folders = max_folders + 100;
                                fold = realloc(fold,
-                                              alloc_folders * sizeof(struct folder));
+                                              alloc_folders * sizeof(struct __ofolder));
                        }
-                       memset(&fold[max_folders], 0, sizeof(struct folder));
+                       memset(&fold[max_folders], 0, sizeof(struct __ofolder));
                        extract_token(fold[max_folders].name, buf, 1, '|', sizeof fold[max_folders].name);
                        extract_token(buf3, buf, 0, '|', SIZ);
                        fold[max_folders].floor = atol (buf3);
@@ -3556,9 +3328,9 @@ void list_all_rooms_by_floor(const char *viewpref) {
                        if (max_folders >= alloc_folders) {
                                alloc_folders = max_folders + 100;
                                fold = realloc(fold,
-                                              alloc_folders * sizeof(struct folder));
+                                              alloc_folders * sizeof(struct __ofolder));
                        }
-                       memset(&fold[max_folders], 0, sizeof(struct folder));
+                       memset(&fold[max_folders], 0, sizeof(struct __ofolder));
                        extract_token(fold[max_folders].room, buf, 0, '|', sizeof fold[max_folders].room);
                        ra_flags = extract_int(buf, 5);
                        flags = extract_int(buf, 1);
@@ -3595,7 +3367,7 @@ void list_all_rooms_by_floor(const char *viewpref) {
                {
                        if (fold[i].num_rooms == 0) {
                                for (j=i; j<max_folders; j++) {
-                                       memcpy(&fold[j], &fold[j+1], sizeof(struct folder));
+                                       memcpy(&fold[j], &fold[j+1], sizeof(struct __ofolder));
                                }
                                max_folders--;
                                num_floors--;
@@ -3620,11 +3392,11 @@ void list_all_rooms_by_floor(const char *viewpref) {
                                }
                        }
                        if (swap > 0) {
-                               memcpy(&ftmp, &fold[j], sizeof(struct folder));
+                               memcpy(&ftmp, &fold[j], sizeof(struct __ofolder));
                                memcpy(&fold[j], &fold[j+1],
-                                      sizeof(struct folder));
+                                      sizeof(struct __ofolder));
                                memcpy(&fold[j+1], &ftmp,
-                                      sizeof(struct folder));
+                                      sizeof(struct __ofolder));
                        }
                }
        }
@@ -3766,136 +3538,11 @@ void set_room_policy(void) {
        display_editroom();
 }
 
-HashList *GetFloorListHash(StrBuf *Target, WCTemplputParams *TP) {
-       /* todo: check context */
-       const char *Err;
-       StrBuf *Buf;
-       StrBuf *Buf2;
-       HashList *floors;
-       HashList *floor;
-       floors = NewHash(1, NULL);
-       Buf = NewStrBuf();
-       serv_puts("LFLR"); /* get floors */
-       StrBufTCP_read_line(Buf, &WC->serv_sock, 0, &Err); /* '100', we hope */
-       if (GetServerStatus(Buf, NULL) == 1) 
-               while(StrBufTCP_read_line(Buf, &WC->serv_sock, 0, &Err), strcmp(ChrPtr(Buf), "000")) {
-                       int a;
-                       const StrBuf *floorNum = NULL;
-                       floor = NewHash(1, NULL);
-                       for(a=0; a<FLOOR_PARAM_LEN; a++) {
-                               Buf2 = NewStrBuf();
-                               StrBufExtract_token(Buf2, Buf, a, '|');
-                               if (a==0) {
-                                       floorNum = Buf2; /* hmm, should we copy Buf2 first? */
-                                       
-                               }
-                               Put(floor, CKEY(FLOOR_PARAM_NAMES[a]), Buf2, HFreeStrBuf);
-                       }
-                       Put(floors, SKEY(floorNum), floor, HDeleteHash);
-               }
-       FreeStrBuf(&Buf);
-       return floors;
-}
-
-void tmplput_FLOOR_Value(StrBuf *TemplBuffer, WCTemplputParams *TP) 
-{
-       StrBuf *val;
-       HashList *floor = (HashList *)(TP->Context);
-       void *value;
-       GetHash(floor, TKEY(0), &value);
-       val = (StrBuf *)value;
-       StrECMAEscAppend(TemplBuffer, val, 0);
-}
-HashList *GetRoomListHashLKRA(StrBuf *Target, WCTemplputParams *TP) 
-{
-       serv_puts("LKRA");
-       return GetRoomListHash(Target, TP);
-}
-HashList *GetRoomListHash(StrBuf *Target, WCTemplputParams *TP) 
-{
-       /* TODO: Check context */
-       HashList *rooms;
-       HashList *room;
-       StrBuf *buf;
-       StrBuf *buf2;
-       const char *Err;
-       buf = NewStrBuf();
-       rooms = NewHash(1, NULL);
-       StrBufTCP_read_line(buf, &WC->serv_sock, 0, &Err);
-       if (GetServerStatus(buf, NULL) == 1) 
-               while(StrBufTCP_read_line(buf, &WC->serv_sock, 0, &Err), strcmp(ChrPtr(buf), "000")) {
-                       int i;
-                       StrBuf *rmName = NULL;
-                       room = NewHash(1, NULL);
-                       for(i=0; i<ROOM_PARAM_LEN; i++) {
-                               buf2 = NewStrBuf();
-                               StrBufExtract_token(buf2, buf, i, '|');
-                               if (i==0) {
-                                       rmName = buf2;
-                               }
-                               Put(room, CKEY(ROOM_PARAM_NAMES[i]), buf2, HFreeStrBuf);
-                       }
-                       Put(rooms, SKEY(rmName), room, HDeleteHash);
-               }
-       SortByHashKey(rooms, 1);
-       /*SortByPayload(rooms, SortRoomsByListOrder);  */
-       FreeStrBuf(&buf);
-       return rooms;
-}
-/** Unused function that orders rooms by the listorder flag */
-int SortRoomsByListOrder(const void *room1, const void *room2) 
-{
-       int l1;
-       int l2;
-       HashList *r1 = (HashList *)GetSearchPayload(room1);
-       HashList *r2 = (HashList *)GetSearchPayload(room2);
-       StrBuf *listOrderBuf1;
-       StrBuf *listOrderBuf2;
-  
-       GetHash(r1, CKEY(ROOM_PARAM_NAMES[3]), (void *)&listOrderBuf1);
-       GetHash(r2, CKEY(ROOM_PARAM_NAMES[3]), (void *)&listOrderBuf2);
-       l1 = atoi(ChrPtr(listOrderBuf1));
-       l2 = atoi(ChrPtr(listOrderBuf2));
-       if (l1 < l2) return -1;
-       else if (l1 > l2) return +1;
-       else return 0;
-}
-void tmplput_ROOM_Value(StrBuf *TemplBuffer, WCTemplputParams *TP) 
-{
-       void *value;
-       StrBuf *val;
-       HashList *room = (HashList *)(TP->Context);
-
-       GetHash(room, TKEY(0), &value);
-       val = (StrBuf *)value;
-       StrECMAEscAppend(TemplBuffer, val, 0);
-}
-void jsonRoomFlr(void) 
-{
-       /* Send as our own (application/json) content type */
-       hprintf("HTTP/1.1 200 OK\r\n");
-       hprintf("Content-type: application/json; charset=utf-8\r\n");
-       hprintf("Server: %s / %s\r\n", PACKAGE_STRING, ChrPtr(WC->serv_info->serv_software));
-       hprintf("Connection: close\r\n");
-       hprintf("Pragma: no-cache\r\nCache-Control: no-store\r\nExpires:-1\r\n");
-       begin_burst();
-       DoTemplate(HKEY("json_roomflr"),NULL,&NoCtx);
-       end_burst(); 
-}
-
 void tmplput_RoomName(StrBuf *Target, WCTemplputParams *TP)
 {
        StrBufAppendTemplate(Target, TP, WC->wc_roomname, 0);
 }
 
-void _gotonext(void) {
-       slrp_highest();
-       gotonext();
-}
-
-void dotskip(void) {
-       smart_goto(sbstr("room"));
-}
 
 void _display_private(void) {
        display_private("", 0);
@@ -4117,9 +3764,6 @@ InitModule_ROOMOPS
        RegisterNamespace("ROOMNAME", 0, 1, tmplput_RoomName, 0);
 
        WebcitAddUrlHandler(HKEY("knrooms"), knrooms, 0);
-       WebcitAddUrlHandler(HKEY("gotonext"), _gotonext, NEED_URL);
-       WebcitAddUrlHandler(HKEY("skip"), gotonext, NEED_URL);
-       WebcitAddUrlHandler(HKEY("ungoto"), ungoto, NEED_URL);
        WebcitAddUrlHandler(HKEY("dotgoto"), dotgoto, NEED_URL);
        WebcitAddUrlHandler(HKEY("dotskip"), dotskip, NEED_URL);
        WebcitAddUrlHandler(HKEY("display_private"), _display_private, 0);
@@ -4139,7 +3783,6 @@ InitModule_ROOMOPS
        WebcitAddUrlHandler(HKEY("set_floordiv_expanded"), set_floordiv_expanded, NEED_URL|AJAX);
        WebcitAddUrlHandler(HKEY("changeview"), change_view, 0);
        WebcitAddUrlHandler(HKEY("toggle_self_service"), toggle_self_service, 0);
-       WebcitAddUrlHandler(HKEY("json_roomflr"), jsonRoomFlr, 0);
        RegisterNamespace("ROOMBANNER", 0, 1, tmplput_roombanner, 0);
 
        RegisterConditional(HKEY("COND:ROOM:TYPE_IS"), 0, ConditionalIsRoomtype, CTX_NONE);
@@ -4163,12 +3806,10 @@ InitModule_ROOMOPS
        RegisterConditional(HKEY("COND:ROOM:EDITACCESS"), 0, ConditionalHaveRoomeditRights, CTX_NONE);
 
        RegisterNamespace("ROOM:UNGOTO", 0, 0, tmplput_ungoto, 0);
-       RegisterIterator("FLOORS", 0, NULL, GetFloorListHash, NULL, DeleteHash, CTX_FLOORS, CTX_NONE, IT_NOFLAG);
-       RegisterNamespace("FLOOR:INFO", 1, 2, tmplput_FLOOR_Value, CTX_FLOORS);
-       RegisterIterator("LKRA", 0, NULL, GetRoomListHashLKRA, NULL, DeleteHash, CTX_ROOMS, CTX_NONE, IT_NOFLAG);
-       RegisterNamespace("ROOM:INFO", 1, 2, tmplput_ROOM_Value, CTX_ROOMS);
-}
+       RegisterIterator("FLOORS", 0, NULL, GetFloorListHash, NULL, NULL, CTX_FLOORS, CTX_NONE, IT_NOFLAG);
+
 
+}
 
 
 void 
@@ -4180,5 +3821,6 @@ SessionDestroyModule_ROOMOPS
        }
        
        free_march_list(sess);
+       DeleteHash(&sess->Floors);
 }
 /*@}*/