* add proper error handling to room netconfig loading
[citadel.git] / webcit / roomlist.c
1 /*
2  * $Id$
3  * room listings and filters.
4  */
5
6 #include "webcit.h"
7 #include "webserver.h"
8
9
10 void DeleteFloor(void *vFloor)
11 {
12         Floor *pFloor;
13         pFloor = (Floor*) vFloor;
14         FreeStrBuf(&pFloor->Name);
15         free(pFloor);
16 }
17
18 int SortFloorsByNameOrder(const void *vfloor1, const void *vfloor2) 
19 {
20         Floor *f1 = (Floor*) GetSearchPayload(vfloor1);
21         Floor *f2 = (Floor*) GetSearchPayload(vfloor2);
22         
23         /* prefer My floor over alpabetical sort */
24         if (f1->ID == VIRTUAL_MY_FLOOR)
25                 return 1;
26         if (f2->ID == VIRTUAL_MY_FLOOR)
27                 return -1;
28
29         return strcmp(ChrPtr(f1->Name), ChrPtr(f2->Name));
30 }
31
32 HashList *GetFloorListHash(StrBuf *Target, WCTemplputParams *TP) 
33 {
34         int Done = 0;
35         const char *Err;
36         StrBuf *Buf;
37         HashList *floors;
38         HashList *floorsbyname;
39         HashPos *it;
40         Floor *pFloor;
41         void *vFloor;
42         const char *Pos;
43         int i;
44         wcsession *WCC = WC;
45         const char *HashKey;
46         long HKLen;
47
48
49         if (WCC->Floors != NULL)
50                 return WCC->Floors;
51         WCC->Floors = floors = NewHash(1, Flathash);
52         WCC->FloorsByName = floorsbyname = NewHash(1, NULL);
53         Buf = NewStrBuf();
54
55         pFloor = (Floor*) malloc(sizeof(Floor));
56         pFloor->ID = VIRTUAL_MY_FLOOR;
57         pFloor->Name = NewStrBufPlain(_("My Folders"), -1);
58         pFloor->NRooms = 0;
59         
60         Put(floors, IKEY(pFloor->ID), pFloor, DeleteFloor);
61         Put(floorsbyname, SKEY(pFloor->Name), pFloor, reference_free_handler);
62
63         serv_puts("LFLR"); /* get floors */
64         StrBufTCP_read_line(Buf, &WC->serv_sock, 0, &Err); /* '100', we hope */
65         if (GetServerStatus(Buf, NULL) == 1) 
66         {
67                 while(!Done && StrBuf_ServGetln(Buf))
68                         if ( (StrLength(Buf)==3) && 
69                              !strcmp(ChrPtr(Buf), "000")) 
70                         {
71                                 Done = 1;
72                         }
73                         else
74                         {
75                         
76                                 Pos = NULL;
77
78                                 pFloor = (Floor*) malloc(sizeof(Floor));
79                                 pFloor->ID = StrBufExtractNext_int(Buf, &Pos, '|');
80                                 pFloor->Name = NewStrBufPlain(NULL, StrLength(Buf));
81                                 StrBufExtract_NextToken(pFloor->Name, Buf, &Pos, '|');
82                                 pFloor->NRooms = StrBufExtractNext_long(Buf, &Pos, '|');
83
84                                 Put(floors, IKEY(pFloor->ID), pFloor, DeleteFloor);
85                                 Put(floorsbyname, SKEY(pFloor->Name), pFloor, reference_free_handler);
86                         }
87         }
88         FreeStrBuf(&Buf);
89         
90         /* now lets pre-sort them alphabeticaly. */
91         i = 1;
92         SortByPayload(floors, SortFloorsByNameOrder);
93         it = GetNewHashPos(floors, 0);
94         while ( GetNextHashPos(floors, it, &HKLen, &HashKey, &vFloor)) 
95                 ((Floor*) vFloor)->AlphaN = i++;
96         DeleteHashPos(&it);
97         SortByHashKeyStr(floors);
98
99         return floors;
100 }
101
102 void tmplput_FLOOR_ID(StrBuf *Target, WCTemplputParams *TP) 
103 {
104         Floor *myFloor = (Floor *)CTX;
105
106         StrBufAppendPrintf(Target, "%d", myFloor->ID);
107 }
108
109 void tmplput_FLOOR_NAME(StrBuf *Target, WCTemplputParams *TP) 
110 {
111         Floor *myFloor = (Floor *)CTX;
112
113         StrBufAppendTemplate(Target, TP, myFloor->Name, 0);
114 }
115
116 void tmplput_FLOOR_NROOMS(StrBuf *Target, WCTemplputParams *TP) 
117 {
118         Floor *myFloor = (Floor *)CTX;
119
120         StrBufAppendPrintf(Target, "%d", myFloor->NRooms);
121 }
122 HashList *GetRoomListHashLKRA(StrBuf *Target, WCTemplputParams *TP) 
123 {
124         wcsession *WCC = WC;
125
126         if (WCC->Floors == NULL)
127                 GetFloorListHash(Target, TP);
128         serv_puts("LKRA");
129         if (WCC->Rooms == NULL) 
130                 WCC->Rooms =  GetRoomListHash(Target, TP);
131         return WCC->Rooms;
132 }
133
134 void FlushFolder(folder *room)
135 {
136         int i;
137
138         FreeStrBuf(&room->name);
139         if (room->IgnetCfgs[0] == (HashList*) StrBufNOTNULL)
140         {
141                 room->IgnetCfgs[0] = NULL;
142                 for (i = ignet_push_share; i < maxRoomNetCfg; i++)
143                         DeleteHash(&room->IgnetCfgs[i]);
144         }
145         if (room->RoomNameParts != NULL)
146         {
147                 for (i=0; i < room->nRoomNameParts; i++)
148                         FreeStrBuf(&room->RoomNameParts[i]);
149                 free(room->RoomNameParts);
150         }
151 }
152
153 void vDeleteFolder(void *vFolder)
154 {
155         folder *room;
156
157         room = (folder*) vFolder;
158         FlushFolder(room);
159
160         free(room);
161 }
162
163
164 HashList *GetRoomListHash(StrBuf *Target, WCTemplputParams *TP) 
165 {
166         int Done = 0;
167         HashList *rooms;
168         folder *room;
169         StrBuf *Buf;
170         const char *Pos;
171         void *vFloor;
172         wcsession *WCC = WC;
173         CompareFunc SortIt;
174         WCTemplputParams SubTP;
175
176         Buf = NewStrBuf();
177         rooms = NewHash(1, NULL);
178         StrBuf_ServGetln(Buf);
179         if (GetServerStatus(Buf, NULL) == 1) 
180         {
181                 while(!Done && StrBuf_ServGetln(Buf))
182                         if ( (StrLength(Buf)==3) && 
183                              !strcmp(ChrPtr(Buf), "000")) 
184                         {
185                                 Done = 1;
186                         }
187                         else
188                         {                               
189                                 Pos = NULL;
190                                 room = (folder*) malloc (sizeof(folder));
191                                 memset(room, 0, sizeof(folder));
192
193                                 /* Load the base data from the server reply */
194                                 room->name = NewStrBufPlain(NULL, StrLength(Buf));
195                                 StrBufExtract_NextToken(room->name, Buf, &Pos, '|');
196
197                                 room->QRFlags = StrBufExtractNext_long(Buf, &Pos, '|');
198                                 room->floorid = StrBufExtractNext_int(Buf, &Pos, '|');
199                                 room->listorder = StrBufExtractNext_long(Buf, &Pos, '|');
200                                 room->QRFlags2 = StrBufExtractNext_long(Buf, &Pos, '|');
201
202                                 room->RAFlags = StrBufExtractNext_long(Buf, &Pos, '|');
203
204 /*
205   ACWHUT?
206   room->ACL = NewStrBufPlain(NULL, StrLength(Buf));
207   StrBufExtract_NextToken(room->ACL, Buf, &Pos, '|');
208 */
209
210                                 room->view = StrBufExtractNext_long(Buf, &Pos, '|');
211                                 room->defview = StrBufExtractNext_long(Buf, &Pos, '|');
212                                 room->lastchange = StrBufExtractNext_long(Buf, &Pos, '|');
213
214                                 /* Evaluate the Server sent data for later use */
215                                 /* find out, whether we are in a sub-room */
216                                 room->nRoomNameParts = StrBufNum_tokens(room->name, '\\');
217                                 if (room->nRoomNameParts > 1)
218                                 {
219                                         int i;
220
221                                         Pos = NULL;
222                                         room->RoomNameParts = malloc(sizeof(StrBuf*) * (room->nRoomNameParts + 1));
223                                         memset(room->RoomNameParts, 0, sizeof(StrBuf*) * (room->nRoomNameParts + 1));
224                                         for (i=0; i < room->nRoomNameParts; i++)
225                                         {
226                                                 room->RoomNameParts[i] = NewStrBuf();
227                                                 StrBufExtract_NextToken(room->RoomNameParts[i],
228                                                                         room->name, &Pos, '\\');
229                                         }
230                                 }
231
232                                 /* Private mailboxes on the main floor get remapped to the personal folder */
233                                 if ((room->QRFlags & QR_MAILBOX) && 
234                                     (room->floorid == 0))
235                                 {
236                                         room->floorid = VIRTUAL_MY_FLOOR;
237                                         if ((room->nRoomNameParts == 1) && 
238                                             (StrLength(room->name) == 4) && 
239                                             (strcmp(ChrPtr(room->name), "Mail") == 0))
240                                         {
241                                                 room->is_inbox = 1;
242                                         }
243
244                                 }
245                                 /* get a pointer to the floor we're on: */
246                                 GetHash(WCC->Floors, IKEY(room->floorid), &vFloor);
247                                 room->Floor = (const Floor*) vFloor;
248
249
250
251                                 /* now we know everything, remember it... */
252                                 Put(rooms, SKEY(room->name), room, vDeleteFolder);
253                         }
254         }
255
256         SubTP.Filter.ContextType = CTX_ROOMS;
257         SortIt = RetrieveSort(&SubTP, NULL, 0, HKEY("fileunsorted"), 0);
258         if (SortIt != NULL)
259                 SortByPayload(rooms, SortIt);
260         else 
261                 SortByPayload(rooms, SortRoomsByListOrder);
262         FreeStrBuf(&Buf);
263         return rooms;
264 }
265
266 HashList *GetNetConfigHash(StrBuf *Target, WCTemplputParams *TP) 
267 {
268         wcsession *WCC = WC;
269         StrBuf *Line;
270         StrBuf *Token;
271         StrBuf *Content;
272         long WantThisOne;
273         long PutTo;
274         long State;
275         
276         WantThisOne = GetTemplateTokenNumber(Target, TP, 5, 0);
277         if (WantThisOne == 0)
278                 return NULL;
279         if (WCC->CurRoom.IgnetCfgs[0] == (HashList*) StrBufNOTNULL)
280                 return WCC->CurRoom.IgnetCfgs[WantThisOne];
281
282         WCC->CurRoom.IgnetCfgs[0] = (HashList*) StrBufNOTNULL;
283         serv_puts("GNET");
284         Line = NewStrBuf();
285         Token = NewStrBuf();
286         StrBuf_ServGetln(Line);
287         if (GetServerStatus(Line, &State) == 1) 
288         {
289                 const char *Pos = NULL;
290                 StrBuf_ServGetln(Line);
291                 StrBufExtract_NextToken(Token, Line, &Pos, '|');
292                 PutTo = GetTokenDefine(SKEY(Token), -1);
293                 if ((PutTo > 0) && (PutTo < maxRoomNetCfg))
294                 {
295                         int n;
296
297                         if (WCC->CurRoom.IgnetCfgs[PutTo] == NULL)
298                                 WCC->CurRoom.IgnetCfgs[PutTo] = NewHash(1, NULL);
299                         Content = NewStrBuf();
300                         StrBufExtract_NextToken(Content, Line, &Pos, '|');
301                         n = GetCount(WCC->CurRoom.IgnetCfgs[PutTo]) + 1;
302                         Put(WCC->CurRoom.IgnetCfgs[PutTo], 
303                             IKEY(n),
304                             Content, 
305                             HFreeStrBuf);
306                 }
307         }
308         else if (State == 550)
309                 StrBufAppendBufPlain(WCC->ImportantMsg,
310                                      _("Higher access is required to access this function."), -1, 0);
311
312
313         return WCC->CurRoom.IgnetCfgs[WantThisOne];
314 }
315
316 /** Unused function that orders rooms by the listorder flag */
317 int SortRoomsByListOrder(const void *room1, const void *room2) 
318 {
319         folder *r1 = (folder*) GetSearchPayload(room1);
320         folder *r2 = (folder*) GetSearchPayload(room2);
321   
322         if (r1->listorder == r2->listorder) return 0;
323         if (r1->listorder > r2->listorder) return 1;
324         return -1;
325 }
326
327 int CompareRoomListByFloorRoomPrivFirst(const void *room1, const void *room2) 
328 {
329         folder *r1 = (folder*) GetSearchPayload(room1);
330         folder *r2 = (folder*) GetSearchPayload(room2);
331   
332         if ((r1->Floor == NULL)  ||
333             (r2->Floor == NULL))
334                 return 0;
335                 
336         /**
337          * are we on the same floor? else sort by floor.
338          */
339         if (r1->Floor != r2->Floor)
340         {
341                 /**
342                  * the private rooms are first in any case.
343                  */
344                 if (r1->Floor->ID == VIRTUAL_MY_FLOOR)
345                         return -1;
346                 if (r2->Floor->ID == VIRTUAL_MY_FLOOR)
347                         return 1;
348                 /**
349                  * else decide alpaheticaly by floorname
350                  */
351                 return (r1->Floor->AlphaN > r2->Floor->AlphaN)? 1 : -1;
352         }
353
354         /**
355          * if we have different levels of subdirectories, 
356          * we want the toplevel to be first, regardless of sort
357          * sequence.
358          */
359         if (((r1->nRoomNameParts > 1) || 
360             (r2->nRoomNameParts > 1)    )&&
361             (r1->nRoomNameParts != r2->nRoomNameParts))
362         {
363                 int i, ret;
364                 int nparts = (r1->nRoomNameParts > r2->nRoomNameParts)?
365                         r2->nRoomNameParts : r1->nRoomNameParts;
366
367                 for (i=0; i < nparts; i++)
368                 {
369                         ret = strcmp (ChrPtr(r1->name), 
370                                       ChrPtr(r2->name));
371                         /**
372                          * Deltas in common parts? exit here.
373                          */
374                         if (ret != 0) 
375                                 return ret;
376                 }
377
378                 /**
379                  * who's a subdirectory of whom?
380                  */
381                 if (r1->nRoomNameParts > r2->nRoomNameParts)
382                         return 1;
383                 else
384                         return -1;
385
386         }
387
388         /**
389          * else just sort alphabeticaly.
390          */
391         return strcmp (ChrPtr(r1->name), 
392                        ChrPtr(r2->name));
393 }
394
395 int CompareRoomListByFloorRoomPrivFirstRev(const void *room1, const void *room2) 
396 {
397         folder *r1 = (folder*) GetSearchPayload(room1);
398         folder *r2 = (folder*) GetSearchPayload(room2);
399
400         if ((r1->Floor == NULL)  ||
401             (r2->Floor == NULL))
402                 return 0;
403
404         /**
405          * are we on the same floor? else sort by floor.
406          */
407         if (r2->Floor != r1->Floor)
408         {
409                 /**
410                  * the private rooms are first in any case.
411                  */
412                 if (r1->Floor->ID == VIRTUAL_MY_FLOOR)
413                         return -1;
414                 if (r2->Floor->ID == VIRTUAL_MY_FLOOR)
415                         return 1;
416                 /**
417                  * else decide alpaheticaly by floorname
418                  */
419
420                 return (r1->Floor->AlphaN < r2->Floor->AlphaN)? 1 : -1;
421         }
422
423         /**
424          * if we have different levels of subdirectories, 
425          * we want the toplevel to be first, regardless of sort
426          * sequence.
427          */
428         if (((r1->nRoomNameParts > 1) || 
429             (r2->nRoomNameParts > 1)    )&&
430             (r1->nRoomNameParts != r2->nRoomNameParts))
431         {
432                 int i, ret;
433                 int nparts = (r1->nRoomNameParts > r2->nRoomNameParts)?
434                         r2->nRoomNameParts : r1->nRoomNameParts;
435
436                 for (i=0; i < nparts; i++)
437                 {
438                         /**
439                          * special cases if one room is top-level...
440                          */
441                         if (r2->nRoomNameParts == 1)
442                                 ret = strcmp (ChrPtr(r2->name), 
443                                               ChrPtr(r1->RoomNameParts[i]));
444                         else if (r1->nRoomNameParts == 1)
445                                 ret = strcmp (ChrPtr(r2->RoomNameParts[i]),
446                                               ChrPtr(r1->name));
447                         else 
448                                 ret = strcmp (ChrPtr(r2->RoomNameParts[i]), 
449                                               ChrPtr(r1->RoomNameParts[i]));
450                         /**
451                          * Deltas in common parts? exit here.
452                          */
453                         if (ret != 0) 
454                                 return ret;
455                 }
456
457                 /**
458                  * who's a subdirectory of whom?
459                  */
460                 if (r1->nRoomNameParts > r2->nRoomNameParts)
461                         return 1;
462                 else
463                         return -1;
464         }
465
466         return strcmp (ChrPtr(r2->name), 
467                        ChrPtr(r1->name));
468 }
469
470 int GroupchangeRoomListByFloorRoomPrivFirst(const void *room1, const void *room2) 
471 {
472         folder *r1 = (folder*) room1;
473         folder *r2 = (folder*) room2;
474   
475
476         if ((r1->Floor == NULL)  ||
477             (r2->Floor == NULL))
478                 return 0;
479                 
480         if (r1->Floor == r2->Floor)
481                 return 0;
482         else 
483         {
484                 wcsession *WCC = WC;
485                 static int columns = 3;
486                 int boxes_per_column = 0;
487                 int nf;
488
489                 nf = GetCount(WCC->Floors);
490                 while (nf % columns != 0) ++nf;
491                 boxes_per_column = (nf / columns);
492                 if (boxes_per_column < 1)
493                         boxes_per_column = 1;
494                 if (r1->Floor->AlphaN % boxes_per_column == 0)
495                         return 2;
496                 else 
497                         return 1;
498         }
499 }
500
501
502
503
504
505
506 void tmplput_ROOM_NAME(StrBuf *Target, WCTemplputParams *TP) 
507 {
508         folder *Folder = (folder *)CTX;
509
510         StrBufAppendTemplate(Target, TP, Folder->name, 0);
511 }
512 void tmplput_ROOM_BASENAME(StrBuf *Target, WCTemplputParams *TP) 
513 {
514         folder *room = (folder *)CTX;
515
516         if (room->nRoomNameParts > 1)
517                 StrBufAppendTemplate(Target, TP, 
518                                       room->RoomNameParts[room->nRoomNameParts - 1], 0);
519         else 
520                 StrBufAppendTemplate(Target, TP, room->name, 0);
521 }
522 void tmplput_ROOM_LEVEL_N_TIMES(StrBuf *Target, WCTemplputParams *TP) 
523 {
524         folder *room = (folder *)CTX;
525         int i;
526         const char *AppendMe;
527         long AppendMeLen;
528
529
530         if (room->nRoomNameParts > 1)
531         {
532                 GetTemplateTokenString(Target, TP, 0, &AppendMe, &AppendMeLen);
533                 for (i = 0; i < room->nRoomNameParts; i++)
534                         StrBufAppendBufPlain(Target, AppendMe, AppendMeLen, 0);
535         }
536 }
537
538 void tmplput_ROOM_ACL(StrBuf *Target, WCTemplputParams *TP) 
539 {
540         folder *Folder = (folder *)CTX;
541
542         StrBufAppendPrintf(Target, "%ld", Folder->RAFlags, 0);
543 }
544
545
546 void tmplput_ROOM_QRFLAGS(StrBuf *Target, WCTemplputParams *TP) 
547 {
548         folder *Folder = (folder *)CTX;
549         StrBufAppendPrintf(Target, "%d", Folder->QRFlags);
550 }
551
552 void tmplput_ROOM_RAFLAGS(StrBuf *Target, WCTemplputParams *TP) 
553 {
554         folder *Folder = (folder *)(TP->Context);
555         StrBufAppendPrintf(Target, "%d", Folder->RAFlags);
556 }
557
558
559 void tmplput_ROOM_FLOORID(StrBuf *Target, WCTemplputParams *TP) 
560 {
561         folder *Folder = (folder *)CTX;
562         StrBufAppendPrintf(Target, "%d", Folder->floorid);
563 }
564
565 void tmplput_ROOM_LISTORDER(StrBuf *Target, WCTemplputParams *TP) 
566 {
567         folder *Folder = (folder *)CTX;
568         StrBufAppendPrintf(Target, "%d", Folder->listorder);
569 }
570 void tmplput_ROOM_VIEW(StrBuf *Target, WCTemplputParams *TP) 
571 {
572         folder *Folder = (folder *)CTX;
573         StrBufAppendPrintf(Target, "%d", Folder->view);
574 }
575 void tmplput_ROOM_DEFVIEW(StrBuf *Target, WCTemplputParams *TP) 
576 {
577         folder *Folder = (folder *)CTX;
578         StrBufAppendPrintf(Target, "%d", Folder->defview);
579 }
580 void tmplput_ROOM_LASTCHANGE(StrBuf *Target, WCTemplputParams *TP) 
581 {
582         folder *Folder = (folder *)CTX;
583         StrBufAppendPrintf(Target, "%d", Folder->lastchange);
584 }
585 void tmplput_ROOM_FLOOR_ID(StrBuf *Target, WCTemplputParams *TP) 
586 {
587         folder *Folder = (folder *)CTX;
588         const Floor *pFloor = Folder->Floor;
589
590         if (pFloor == NULL)
591                 return;
592
593         StrBufAppendPrintf(Target, "%d", pFloor->ID);
594 }
595
596 void tmplput_ROOM_FLOOR_NAME(StrBuf *Target, WCTemplputParams *TP) 
597 {
598         folder *Folder = (folder *)CTX;
599         const Floor *pFloor = Folder->Floor;
600
601         if (pFloor == NULL)
602                 return;
603
604         StrBufAppendTemplate(Target, TP, pFloor->Name, 0);
605 }
606
607 void tmplput_ROOM_FLOOR_NROOMS(StrBuf *Target, WCTemplputParams *TP) 
608 {
609         folder *Folder = (folder *)CTX;
610         const Floor *pFloor = Folder->Floor;
611
612         if (pFloor == NULL)
613                 return;
614         StrBufAppendPrintf(Target, "%d", pFloor->NRooms);
615 }
616
617
618
619 int ConditionalRoomHas_UA_KNOWN(StrBuf *Target, WCTemplputParams *TP)
620 {
621         folder *Folder = (folder *)CTX;
622         return (Folder->RAFlags & UA_KNOWN) != 0;
623 }
624
625 int ConditionalRoomHas_UA_GOTOALLOWED(StrBuf *Target, WCTemplputParams *TP)
626 {
627         folder *Folder = (folder *)CTX;
628         return (Folder->RAFlags & UA_GOTOALLOWED) != 0;
629 }
630
631 int ConditionalRoomHas_UA_HASNEWMSGS(StrBuf *Target, WCTemplputParams *TP)
632 {
633         folder *Folder = (folder *)CTX;
634         return (Folder->RAFlags & UA_HASNEWMSGS) != 0;
635 }
636
637 int ConditionalRoomHas_UA_ZAPPED(StrBuf *Target, WCTemplputParams *TP)
638 {
639         folder *Folder = (folder *)CTX;
640         return (Folder->RAFlags & UA_ZAPPED) != 0;
641 }
642
643 int ConditionalRoomHas_UA_POSTALLOWED(StrBuf *Target, WCTemplputParams *TP)
644 {
645         folder *Folder = (folder *)CTX;
646         return (Folder->RAFlags & UA_POSTALLOWED) != 0;
647 }
648
649 int ConditionalRoomHas_UA_ADMINALLOWED(StrBuf *Target, WCTemplputParams *TP)
650 {
651         folder *Folder = (folder *)CTX;
652         return (Folder->RAFlags & UA_ADMINALLOWED) != 0;
653 }
654
655 int ConditionalRoomHas_UA_DELETEALLOWED(StrBuf *Target, WCTemplputParams *TP)
656 {
657         folder *Folder = (folder *)CTX;
658         return (Folder->RAFlags & UA_DELETEALLOWED) != 0;
659 }
660
661
662 int ConditionalRoomIsInbox(StrBuf *Target, WCTemplputParams *TP)
663 {
664         folder *Folder = (folder *)CTX;
665         return Folder->is_inbox;
666 }
667
668 void tmplput_ROOM_COLLECTIONTYPE(StrBuf *Target, WCTemplputParams *TP) 
669 {
670         folder *Folder = (folder *)CTX;
671         
672         switch(Folder->view) {
673         case VIEW_CALENDAR:
674                 StrBufAppendBufPlain(Target, HKEY("vevent"), 0);
675                 break;
676         case VIEW_TASKS:
677                 StrBufAppendBufPlain(Target, HKEY("vtodo"), 0);
678                 break;
679         case VIEW_ADDRESSBOOK:
680                 StrBufAppendBufPlain(Target, HKEY("vcard"), 0);
681                 break;
682         case VIEW_NOTES:
683                 StrBufAppendBufPlain(Target, HKEY("vnotes"), 0);
684                 break;
685         case VIEW_JOURNAL:
686                 StrBufAppendBufPlain(Target, HKEY("vjournal"), 0);
687                 break;
688         case VIEW_WIKI:
689                 StrBufAppendBufPlain(Target, HKEY("wiki"), 0);
690                 break;
691         }
692 }
693
694
695
696
697 int ConditionalRoomHasGroupdavContent(StrBuf *Target, WCTemplputParams *TP)
698 {
699         folder *Folder = (folder *)CTX;
700
701         lprintf(0, "-> %s: %ld\n", ChrPtr(Folder->name), Folder->view);
702
703         return ((Folder->view == VIEW_CALENDAR) || 
704                 (Folder->view == VIEW_TASKS) || 
705                 (Folder->view == VIEW_ADDRESSBOOK) ||
706                 (Folder->view == VIEW_NOTES) ||
707                 (Folder->view == VIEW_JOURNAL) );
708 }
709
710
711
712 int ConditionalFloorIsRESTSubFloor(StrBuf *Target, WCTemplputParams *TP)
713 {
714         wcsession  *WCC = WC;
715         Floor *MyFloor = (Floor *)CTX;
716         /** if we have dav_depth the client just wants the subfloors */
717         if ((WCC->Hdr->HR.dav_depth == 1) && 
718             (GetCount(WCC->Directory) == 0))
719                 return 1;
720         return WCC->CurrentFloor == MyFloor;
721 }
722
723
724 int ConditionalFloorIsSUBROOM(StrBuf *Target, WCTemplputParams *TP)
725 {
726         wcsession  *WCC = WC;
727         Floor *MyFloor = (Floor *)CTX;
728
729         return WCC->CurRoom.floorid == MyFloor->ID;
730 }
731
732
733 int ConditionalRoomIsRESTSubRoom(StrBuf *Target, WCTemplputParams *TP)
734 {
735         wcsession  *WCC = WC;
736         folder     *Folder = (folder *)CTX;
737         HashPos    *it;
738         StrBuf     * Dir;
739         void       *vDir;
740         long        len;
741         const char *Key;
742         int i, j, urlp;
743         int delta;
744
745
746         /* list only folders relative to the current floor... */
747         if (Folder->Floor != WCC->CurrentFloor)
748                 return 0;
749
750         urlp = GetCount(WCC->Directory);
751         delta = Folder->nRoomNameParts - urlp + 1;
752
753         lprintf(0, "\n->%s: %ld - %ld ", ChrPtr(Folder->name), urlp, 
754                 Folder->nRoomNameParts);
755         /* list only the floors which are in relation to the dav_depth header */
756         if (WCC->Hdr->HR.dav_depth != delta) {
757                 lprintf(0, "1\n");
758                 return 0;
759         }
760
761
762         it = GetNewHashPos(WCC->Directory, 0);
763         /* Fast forward the floorname we checked above... */
764         GetNextHashPos(WCC->Directory, it, &len, &Key, &vDir);
765
766         if (Folder->nRoomNameParts > 1) {               
767                 for (i = 0, j = 1; 
768                      (i > Folder->nRoomNameParts) && (j > urlp); 
769                      i++, j++)
770                 {
771                         if (!GetNextHashPos(WCC->Directory, 
772                                             it, &len, &Key, &vDir) ||
773                             (vDir == NULL))
774                         {
775                                 DeleteHashPos(&it);
776
777                                 lprintf(0, "3\n");
778                                 return 0;
779                         }
780                         Dir = (StrBuf*) vDir;
781                         if (strcmp(ChrPtr(Folder->RoomNameParts[i]), 
782                                    ChrPtr(Dir)) != 0)
783                         {
784                                 DeleteHashPos(&it);
785                                 lprintf(0, "4\n");
786                                 return 0;
787                         }
788                 }
789                 DeleteHashPos(&it);
790                 return 1;
791         }
792         else {
793                 if (!GetNextHashPos(WCC->Directory, 
794                                     it, &len, &Key, &vDir) ||
795                     (vDir == NULL))
796                 {
797                         DeleteHashPos(&it);
798                         
799                         lprintf(0, "5\n");
800                         return WCC->Hdr->HR.dav_depth == 1;
801                 }
802                 DeleteHashPos(&it);
803                 Dir = (StrBuf*) vDir;
804                 if (WCC->Hdr->HR.dav_depth == 0) {
805                         return (strcmp(ChrPtr(Folder->name), 
806                                        ChrPtr(Dir))
807                                 == 0);
808
809                 }
810                 return 0;
811         }
812 }
813
814
815 void jsonRoomFlr(void) 
816 {
817         /* Send as our own (application/json) content type */
818         hprintf("HTTP/1.1 200 OK\r\n");
819         hprintf("Content-type: application/json; charset=utf-8\r\n");
820         hprintf("Server: %s / %s\r\n", PACKAGE_STRING, ChrPtr(WC->serv_info->serv_software));
821         hprintf("Connection: close\r\n");
822         hprintf("Pragma: no-cache\r\nCache-Control: no-store\r\nExpires:-1\r\n");
823         begin_burst();
824         DoTemplate(HKEY("json_roomflr"),NULL,&NoCtx);
825         end_burst(); 
826 }
827
828
829 void 
830 SessionDetachModule_ROOMLIST
831 (wcsession *sess)
832 {
833         DeleteHash(&sess->Floors);
834         DeleteHash(&sess->Rooms);
835         DeleteHash(&sess->FloorsByName);
836 }
837
838 void 
839 InitModule_ROOMLIST
840 (void)
841 {
842         WebcitAddUrlHandler(HKEY("json_roomflr"), "", 0, jsonRoomFlr, 0);
843
844
845         RegisterNamespace("FLOOR:ID", 0, 0, tmplput_FLOOR_ID, NULL, CTX_FLOORS);
846         RegisterNamespace("FLOOR:NAME", 0, 1, tmplput_FLOOR_NAME, NULL, CTX_FLOORS);
847         RegisterNamespace("FLOOR:NROOMS", 0, 0, tmplput_FLOOR_NROOMS, NULL, CTX_FLOORS);
848         RegisterConditional(HKEY("COND:FLOOR:ISSUBROOM"), 0, ConditionalFloorIsSUBROOM, CTX_FLOORS);
849         RegisterConditional(HKEY("COND:ROOM:REST:ISSUBFLOOR"), 0, ConditionalFloorIsRESTSubFloor, CTX_FLOORS);
850
851         RegisterIterator("ITERATE:THISROOM:GNET", 1, NULL, GetNetConfigHash, NULL, NULL, CTX_STRBUF, CTX_NONE, IT_NOFLAG);
852
853         RegisterIterator("LFLR", 0, NULL, GetFloorListHash, NULL, NULL, CTX_FLOORS, CTX_NONE, IT_FLAG_DETECT_GROUPCHANGE);
854
855         RegisterIterator("LKRA", 0, NULL, GetRoomListHashLKRA, NULL, NULL, CTX_ROOMS, CTX_NONE, IT_FLAG_DETECT_GROUPCHANGE);
856
857         RegisterNamespace("ROOM:INFO:FLOORID", 0, 1, tmplput_ROOM_FLOORID, NULL, CTX_ROOMS);
858         RegisterNamespace("ROOM:INFO:NAME", 0, 1, tmplput_ROOM_NAME, NULL, CTX_ROOMS);
859         RegisterNamespace("ROOM:INFO:PRINT_NAME", 0, 1, tmplput_ROOM_NAME, NULL, CTX_ROOMS);/// TODO!
860         RegisterNamespace("ROOM:INFO:BASENAME", 0, 1, tmplput_ROOM_BASENAME, NULL, CTX_ROOMS);
861         RegisterNamespace("ROOM:INFO:LEVELNTIMES", 1, 2, tmplput_ROOM_LEVEL_N_TIMES, NULL, CTX_ROOMS);
862
863         RegisterNamespace("ROOM:INFO:ACL", 0, 1, tmplput_ROOM_ACL, NULL, CTX_ROOMS);
864         RegisterNamespace("ROOM:INFO:QRFLAGS", 0, 1, tmplput_ROOM_QRFLAGS, NULL, CTX_ROOMS);
865         RegisterNamespace("ROOM:INFO:RAFLAGS", 0, 1, tmplput_ROOM_RAFLAGS, NULL, CTX_ROOMS);
866         RegisterNamespace("ROOM:INFO:LISTORDER", 0, 1, tmplput_ROOM_LISTORDER, NULL, CTX_ROOMS);
867         RegisterNamespace("ROOM:INFO:VIEW", 0, 1, tmplput_ROOM_VIEW, NULL, CTX_ROOMS);
868         RegisterNamespace("ROOM:INFO:DEFVIEW", 0, 1, tmplput_ROOM_DEFVIEW, NULL, CTX_ROOMS);
869         RegisterNamespace("ROOM:INFO:LASTCHANGE", 0, 1, tmplput_ROOM_LASTCHANGE, NULL, CTX_ROOMS);
870         RegisterNamespace("ROOM:INFO:COLLECTIONTYPE", 0, 1, tmplput_ROOM_COLLECTIONTYPE, NULL, CTX_ROOMS);
871         RegisterNamespace("ROOM:INFO:FLOOR:ID", 0, 0, tmplput_ROOM_FLOOR_ID, NULL, CTX_ROOMS);
872         RegisterNamespace("ROOM:INFO:FLOOR:NAME", 0, 1, tmplput_ROOM_FLOOR_NAME, NULL, CTX_ROOMS);
873         RegisterNamespace("ROOM:INFO:FLOOR:NROOMS", 0, 0, tmplput_ROOM_FLOOR_NROOMS, NULL, CTX_ROOMS);
874
875         RegisterConditional(HKEY("COND:ROOM:REST:ISSUBROOM"), 0, ConditionalRoomIsRESTSubRoom, CTX_ROOMS);
876
877         RegisterConditional(HKEY("COND:ROOM:INFO:IS_INBOX"), 0, ConditionalRoomIsInbox, CTX_ROOMS);
878         RegisterConditional(HKEY("COND:ROOM:FLAGS:UA_KNOWN"), 0, ConditionalRoomHas_UA_KNOWN, CTX_ROOMS);
879         RegisterConditional(HKEY("COND:ROOM:FLAGS:UA_GOTOALLOWED"), 0, ConditionalRoomHas_UA_GOTOALLOWED, CTX_ROOMS);
880         RegisterConditional(HKEY("COND:ROOM:FLAGS:UA_HASNEWMSGS"), 0, ConditionalRoomHas_UA_HASNEWMSGS, CTX_ROOMS);
881         RegisterConditional(HKEY("COND:ROOM:FLAGS:UA_ZAPPED"), 0, ConditionalRoomHas_UA_ZAPPED, CTX_ROOMS);
882         RegisterConditional(HKEY("COND:ROOM:FLAGS:UA_POSTALLOWED"), 0, ConditionalRoomHas_UA_POSTALLOWED, CTX_ROOMS);
883         RegisterConditional(HKEY("COND:ROOM:FLAGS:UA_ADMINALLOWED"), 0, ConditionalRoomHas_UA_ADMINALLOWED, CTX_ROOMS);
884         RegisterConditional(HKEY("COND:ROOM:FLAGS:UA_DELETEALLOWED"), 0, ConditionalRoomHas_UA_DELETEALLOWED, CTX_ROOMS);
885         RegisterConditional(HKEY("COND:ROOM:GROUPDAV_CONTENT"), 0, ConditionalRoomHasGroupdavContent, CTX_ROOMS);
886
887
888
889         RegisterSortFunc(HKEY("byfloorroom"),
890                          NULL, 0,
891                          CompareRoomListByFloorRoomPrivFirst,
892                          CompareRoomListByFloorRoomPrivFirstRev,
893                          GroupchangeRoomListByFloorRoomPrivFirst,
894                          CTX_ROOMS);
895
896 }