* create function to parse the output of goto into our folder structure
[citadel.git] / webcit / roomlist.c
index 818b02d3a9ba2e3b02f34c768f382a0f67e2ab72..c440085f09caf9a4f5d69f770de4a50558a0ae30 100644 (file)
@@ -31,7 +31,7 @@ int SortFloorsByNameOrder(const void *vfloor1, const void *vfloor2)
 
 HashList *GetFloorListHash(StrBuf *Target, WCTemplputParams *TP) 
 {
-
+       int Done = 0;
        const char *Err;
        StrBuf *Buf;
        HashList *floors;
@@ -47,7 +47,7 @@ HashList *GetFloorListHash(StrBuf *Target, WCTemplputParams *TP)
 
        if (WCC->Floors != NULL)
                return WCC->Floors;
-       WCC->Floors = floors = NewHash(1, NULL);
+       WCC->Floors = floors = NewHash(1, Flathash);
        Buf = NewStrBuf();
 
        Floor = malloc(sizeof(floor));
@@ -61,19 +61,25 @@ HashList *GetFloorListHash(StrBuf *Target, WCTemplputParams *TP)
        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")) 
-               {
+               while(!Done && StrBuf_ServGetln(Buf))
+                       if ( (StrLength(Buf)==3) && 
+                            !strcmp(ChrPtr(Buf), "000")) 
+                       {
+                               Done = 1;
+                       }
+                       else
+                       {
                        
-                       Pos = NULL;
+                               Pos = NULL;
 
-                       Floor = malloc(sizeof(floor));
-                       Floor->ID = StrBufExtractNext_long(Buf, &Pos, '|');
-                       Floor->Name = NewStrBufPlain(NULL, StrLength(Buf));
-                       StrBufExtract_NextToken(Floor->Name, Buf, &Pos, '|');
-                       Floor->NRooms = StrBufExtractNext_long(Buf, &Pos, '|');
+                               Floor = malloc(sizeof(floor));
+                               Floor->ID = StrBufExtractNext_int(Buf, &Pos, '|');
+                               Floor->Name = NewStrBufPlain(NULL, StrLength(Buf));
+                               StrBufExtract_NextToken(Floor->Name, Buf, &Pos, '|');
+                               Floor->NRooms = StrBufExtractNext_long(Buf, &Pos, '|');
 
-                       Put(floors, IKEY(Floor->ID), Floor, DeleteFloor);
-               }
+                               Put(floors, IKEY(Floor->ID), Floor, DeleteFloor);
+                       }
        }
        FreeStrBuf(&Buf);
        
@@ -119,26 +125,33 @@ HashList *GetRoomListHashLKRA(StrBuf *Target, WCTemplputParams *TP)
        return GetRoomListHash(Target, TP);
 }
 
-void DeleteFolder(void *vFolder)
+void FlushFolder(folder *room)
 {
        int i;
-       folder *room;
-       room = (folder*) vFolder;
 
        FreeStrBuf(&room->name);
-
        if (room->RoomNameParts != NULL)
        {
                for (i=0; i < room->nRoomNameParts; i++)
                        FreeStrBuf(&room->RoomNameParts[i]);
                free(room->RoomNameParts);
        }
+}
+
+void vDeleteFolder(void *vFolder)
+{
+       folder *room;
+
+       room = (folder*) vFolder;
+       FlushFolder(room);
+
        free(room);
 }
 
 
 HashList *GetRoomListHash(StrBuf *Target, WCTemplputParams *TP) 
 {
+       int Done = 0;
        HashList *rooms;
        folder *room;
        StrBuf *Buf;
@@ -154,75 +167,79 @@ HashList *GetRoomListHash(StrBuf *Target, WCTemplputParams *TP)
        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")) 
-               {
-
-                       Pos = NULL;
-                       room = (folder*) malloc (sizeof(folder));
-                       memset(room, 0, sizeof(folder));
+               while(!Done && StrBuf_ServGetln(Buf))
+                       if ( (StrLength(Buf)==3) && 
+                            !strcmp(ChrPtr(Buf), "000")) 
+                       {
+                               Done = 1;
+                       }
+                       else
+                       {                               
+                               Pos = NULL;
+                               room = (folder*) malloc (sizeof(folder));
+                               memset(room, 0, sizeof(folder));
 
-                       /* Load the base data from the server reply */
-                       room->name = NewStrBufPlain(NULL, StrLength(Buf));
-                       StrBufExtract_NextToken(room->name, Buf, &Pos, '|');
+                               /* Load the base data from the server reply */
+                               room->name = NewStrBufPlain(NULL, StrLength(Buf));
+                               StrBufExtract_NextToken(room->name, Buf, &Pos, '|');
 
-                       room->QRFlags = StrBufExtractNext_long(Buf, &Pos, '|');
-                       room->floorid = StrBufExtractNext_long(Buf, &Pos, '|');
-                       room->listorder = StrBufExtractNext_long(Buf, &Pos, '|');
-                       room->QRFlags2 = StrBufExtractNext_long(Buf, &Pos, '|');
+                               room->QRFlags = StrBufExtractNext_long(Buf, &Pos, '|');
+                               room->floorid = StrBufExtractNext_int(Buf, &Pos, '|');
+                               room->listorder = StrBufExtractNext_long(Buf, &Pos, '|');
+                               room->QRFlags2 = StrBufExtractNext_long(Buf, &Pos, '|');
 
-                       room->RAFlags = StrBufExtractNext_long(Buf, &Pos, '|');
+                               room->RAFlags = StrBufExtractNext_long(Buf, &Pos, '|');
 
 /*
- ACWHUT?
-                       room->ACL = NewStrBufPlain(NULL, StrLength(Buf));
-                       StrBufExtract_NextToken(room->ACL, Buf, &Pos, '|');
 ACWHUT?
+  room->ACL = NewStrBufPlain(NULL, StrLength(Buf));
+  StrBufExtract_NextToken(room->ACL, Buf, &Pos, '|');
 */
 
-                       room->view = StrBufExtractNext_long(Buf, &Pos, '|');
-                       room->defview = StrBufExtractNext_long(Buf, &Pos, '|');
-                       room->lastchange = StrBufExtractNext_long(Buf, &Pos, '|');
-
-                       /* Evaluate the Server sent data for later use */
-                       /* find out, whether we are in a sub-room */
-                       room->nRoomNameParts = StrBufNum_tokens(room->name, '\\');
-                       if (room->nRoomNameParts > 1)
-                       {
-                               int i;
+                               room->view = StrBufExtractNext_long(Buf, &Pos, '|');
+                               room->defview = StrBufExtractNext_long(Buf, &Pos, '|');
+                               room->lastchange = StrBufExtractNext_long(Buf, &Pos, '|');
 
-                               Pos = NULL;
-                               room->RoomNameParts = malloc(sizeof(StrBuf*) * (room->nRoomNameParts + 1));
-                               memset(room->RoomNameParts, 0, sizeof(StrBuf*) * (room->nRoomNameParts + 1));
-                               for (i=0; i < room->nRoomNameParts; i++)
+                               /* Evaluate the Server sent data for later use */
+                               /* find out, whether we are in a sub-room */
+                               room->nRoomNameParts = StrBufNum_tokens(room->name, '\\');
+                               if (room->nRoomNameParts > 1)
                                {
-                                       room->RoomNameParts[i] = NewStrBuf();
-                                       StrBufExtract_NextToken(room->RoomNameParts[i],
-                                                               room->name, &Pos, '\\');
+                                       int i;
+
+                                       Pos = NULL;
+                                       room->RoomNameParts = malloc(sizeof(StrBuf*) * (room->nRoomNameParts + 1));
+                                       memset(room->RoomNameParts, 0, sizeof(StrBuf*) * (room->nRoomNameParts + 1));
+                                       for (i=0; i < room->nRoomNameParts; i++)
+                                       {
+                                               room->RoomNameParts[i] = NewStrBuf();
+                                               StrBufExtract_NextToken(room->RoomNameParts[i],
+                                                                       room->name, &Pos, '\\');
+                                       }
                                }
-                       }
 
-                       /* Private mailboxes on the main floor get remapped to the personal folder */
-                       if ((room->QRFlags & QR_MAILBOX) && 
-                           (room->floorid == 0))
-                       {
-                               room->floorid = VIRTUAL_MY_FLOOR;
-                               if ((room->nRoomNameParts == 1) && 
-                                   (StrLength(room->name) == 4) && 
-                                   (strcmp(ChrPtr(room->name), "Mail") == 0))
+                               /* Private mailboxes on the main floor get remapped to the personal folder */
+                               if ((room->QRFlags & QR_MAILBOX) && 
+                                   (room->floorid == 0))
                                {
-                                       room->is_inbox = 1;
-                               }
+                                       room->floorid = VIRTUAL_MY_FLOOR;
+                                       if ((room->nRoomNameParts == 1) && 
+                                           (StrLength(room->name) == 4) && 
+                                           (strcmp(ChrPtr(room->name), "Mail") == 0))
+                                       {
+                                               room->is_inbox = 1;
+                                       }
 
-                       }
-                       /* get a pointer to the floor we're on: */
-                       GetHash(WCC->Floors, IKEY(room->floorid), &vFloor);
-                       room->Floor = (const floor*) vFloor;
+                               }
+                               /* get a pointer to the floor we're on: */
+                               GetHash(WCC->Floors, IKEY(room->floorid), &vFloor);
+                               room->Floor = (const floor*) vFloor;
 
 
 
-                       /* now we know everything, remember it... */
-                       Put(rooms, SKEY(room->name), room, DeleteFolder);
-               }
+                               /* now we know everything, remember it... */
+                               Put(rooms, SKEY(room->name), room, vDeleteFolder);
+                       }
        }
 
        SubTP.Filter.ContextType = CTX_ROOMS;
@@ -318,11 +335,11 @@ int CompareRoomListByFloorRoomPrivFirstRev(const void *room1, const void *room2)
 {
        folder *r1 = (folder*) GetSearchPayload(room1);
        folder *r2 = (folder*) GetSearchPayload(room2);
-  
+
        if ((r1->Floor == NULL)  ||
            (r2->Floor == NULL))
                return 0;
-               
+
        /**
         * are we on the same floor? else sort by floor.
         */
@@ -471,6 +488,11 @@ void tmplput_ROOM_QRFLAGS(StrBuf *Target, WCTemplputParams *TP)
        StrBufAppendPrintf(Target, "%d", Folder->QRFlags);
 }
 
+void tmplput_ROOM_RAFLAGS(StrBuf *Target, WCTemplputParams *TP) 
+{
+       folder *Folder = (folder *)(TP->Context);
+       StrBufAppendPrintf(Target, "%d", Folder->RAFlags);
+}
 
 
 void tmplput_ROOM_FLOORID(StrBuf *Target, WCTemplputParams *TP) 
@@ -693,6 +715,12 @@ void jsonRoomFlr(void)
        end_burst(); 
 }
 
+void 
+SessionDetachModule_ROOMLIST
+(wcsession *sess)
+{
+       DeleteHash(&sess->Floors);
+}
 
 
 void 
@@ -719,6 +747,7 @@ InitModule_ROOMLIST
 
        RegisterNamespace("ROOM:INFO:ACL", 0, 1, tmplput_ROOM_ACL, NULL, CTX_ROOMS);
        RegisterNamespace("ROOM:INFO:QRFLAGS", 0, 1, tmplput_ROOM_QRFLAGS, NULL, CTX_ROOMS);
+       RegisterNamespace("ROOM:INFO:RAFLAGS", 0, 1, tmplput_ROOM_RAFLAGS, NULL, CTX_ROOMS);
        RegisterNamespace("ROOM:INFO:LISTORDER", 0, 1, tmplput_ROOM_LISTORDER, NULL, CTX_ROOMS);
        RegisterNamespace("ROOM:INFO:VIEW", 0, 1, tmplput_ROOM_VIEW, NULL, CTX_ROOMS);
        RegisterNamespace("ROOM:INFO:DEFVIEW", 0, 1, tmplput_ROOM_DEFVIEW, NULL, CTX_ROOMS);