* implement ITERATE:THISROOM:GNET
[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         
275         WantThisOne = GetTemplateTokenNumber(Target, TP, 6, 0);
276         if (WantThisOne == 0)
277                 return NULL;
278         if (WCC->CurRoom.IgnetCfgs[0] == (HashList*) StrBufNOTNULL)
279                 return WCC->CurRoom.IgnetCfgs[WantThisOne];
280
281         WCC->CurRoom.IgnetCfgs[0] = (HashList*) StrBufNOTNULL;
282         serv_puts("GNET");
283         Line = NewStrBuf();
284         Token = NewStrBuf();
285         StrBuf_ServGetln(Line);
286         if (GetServerStatus(Line, NULL) == 1) 
287         {
288                 const char *Pos = NULL;
289                 StrBuf_ServGetln(Line);
290                 StrBufExtract_NextToken(Token, Line, &Pos, '|');
291                 PutTo = GetTokenDefine(SKEY(Token), -1);
292                 if ((PutTo > 0) && (PutTo < maxRoomNetCfg))
293                 {
294                         int n;
295
296                         if (WCC->CurRoom.IgnetCfgs[PutTo] == NULL)
297                                 WCC->CurRoom.IgnetCfgs[PutTo] = NewHash(1, NULL);
298                         Content = NewStrBuf();
299                         StrBufExtract_NextToken(Content, Line, &Pos, '|');
300                         n = GetCount(WCC->CurRoom.IgnetCfgs[PutTo]) + 1;
301                         Put(WCC->CurRoom.IgnetCfgs[PutTo], 
302                             IKEY(n),
303                             Content, 
304                             HFreeStrBuf);
305                 }
306         }
307
308         return WCC->CurRoom.IgnetCfgs[WantThisOne];
309 }
310
311 /** Unused function that orders rooms by the listorder flag */
312 int SortRoomsByListOrder(const void *room1, const void *room2) 
313 {
314         folder *r1 = (folder*) GetSearchPayload(room1);
315         folder *r2 = (folder*) GetSearchPayload(room2);
316   
317         if (r1->listorder == r2->listorder) return 0;
318         if (r1->listorder > r2->listorder) return 1;
319         return -1;
320 }
321
322 int CompareRoomListByFloorRoomPrivFirst(const void *room1, const void *room2) 
323 {
324         folder *r1 = (folder*) GetSearchPayload(room1);
325         folder *r2 = (folder*) GetSearchPayload(room2);
326   
327         if ((r1->Floor == NULL)  ||
328             (r2->Floor == NULL))
329                 return 0;
330                 
331         /**
332          * are we on the same floor? else sort by floor.
333          */
334         if (r1->Floor != r2->Floor)
335         {
336                 /**
337                  * the private rooms are first in any case.
338                  */
339                 if (r1->Floor->ID == VIRTUAL_MY_FLOOR)
340                         return -1;
341                 if (r2->Floor->ID == VIRTUAL_MY_FLOOR)
342                         return 1;
343                 /**
344                  * else decide alpaheticaly by floorname
345                  */
346                 return (r1->Floor->AlphaN > r2->Floor->AlphaN)? 1 : -1;
347         }
348
349         /**
350          * if we have different levels of subdirectories, 
351          * we want the toplevel to be first, regardless of sort
352          * sequence.
353          */
354         if (((r1->nRoomNameParts > 1) || 
355             (r2->nRoomNameParts > 1)    )&&
356             (r1->nRoomNameParts != r2->nRoomNameParts))
357         {
358                 int i, ret;
359                 int nparts = (r1->nRoomNameParts > r2->nRoomNameParts)?
360                         r2->nRoomNameParts : r1->nRoomNameParts;
361
362                 for (i=0; i < nparts; i++)
363                 {
364                         ret = strcmp (ChrPtr(r1->name), 
365                                       ChrPtr(r2->name));
366                         /**
367                          * Deltas in common parts? exit here.
368                          */
369                         if (ret != 0) 
370                                 return ret;
371                 }
372
373                 /**
374                  * who's a subdirectory of whom?
375                  */
376                 if (r1->nRoomNameParts > r2->nRoomNameParts)
377                         return 1;
378                 else
379                         return -1;
380
381         }
382
383         /**
384          * else just sort alphabeticaly.
385          */
386         return strcmp (ChrPtr(r1->name), 
387                        ChrPtr(r2->name));
388 }
389
390 int CompareRoomListByFloorRoomPrivFirstRev(const void *room1, const void *room2) 
391 {
392         folder *r1 = (folder*) GetSearchPayload(room1);
393         folder *r2 = (folder*) GetSearchPayload(room2);
394
395         if ((r1->Floor == NULL)  ||
396             (r2->Floor == NULL))
397                 return 0;
398
399         /**
400          * are we on the same floor? else sort by floor.
401          */
402         if (r2->Floor != r1->Floor)
403         {
404                 /**
405                  * the private rooms are first in any case.
406                  */
407                 if (r1->Floor->ID == VIRTUAL_MY_FLOOR)
408                         return -1;
409                 if (r2->Floor->ID == VIRTUAL_MY_FLOOR)
410                         return 1;
411                 /**
412                  * else decide alpaheticaly by floorname
413                  */
414
415                 return (r1->Floor->AlphaN < r2->Floor->AlphaN)? 1 : -1;
416         }
417
418         /**
419          * if we have different levels of subdirectories, 
420          * we want the toplevel to be first, regardless of sort
421          * sequence.
422          */
423         if (((r1->nRoomNameParts > 1) || 
424             (r2->nRoomNameParts > 1)    )&&
425             (r1->nRoomNameParts != r2->nRoomNameParts))
426         {
427                 int i, ret;
428                 int nparts = (r1->nRoomNameParts > r2->nRoomNameParts)?
429                         r2->nRoomNameParts : r1->nRoomNameParts;
430
431                 for (i=0; i < nparts; i++)
432                 {
433                         /**
434                          * special cases if one room is top-level...
435                          */
436                         if (r2->nRoomNameParts == 1)
437                                 ret = strcmp (ChrPtr(r2->name), 
438                                               ChrPtr(r1->RoomNameParts[i]));
439                         else if (r1->nRoomNameParts == 1)
440                                 ret = strcmp (ChrPtr(r2->RoomNameParts[i]),
441                                               ChrPtr(r1->name));
442                         else 
443                                 ret = strcmp (ChrPtr(r2->RoomNameParts[i]), 
444                                               ChrPtr(r1->RoomNameParts[i]));
445                         /**
446                          * Deltas in common parts? exit here.
447                          */
448                         if (ret != 0) 
449                                 return ret;
450                 }
451
452                 /**
453                  * who's a subdirectory of whom?
454                  */
455                 if (r1->nRoomNameParts > r2->nRoomNameParts)
456                         return 1;
457                 else
458                         return -1;
459         }
460
461         return strcmp (ChrPtr(r2->name), 
462                        ChrPtr(r1->name));
463 }
464
465 int GroupchangeRoomListByFloorRoomPrivFirst(const void *room1, const void *room2) 
466 {
467         folder *r1 = (folder*) room1;
468         folder *r2 = (folder*) room2;
469   
470
471         if ((r1->Floor == NULL)  ||
472             (r2->Floor == NULL))
473                 return 0;
474                 
475         if (r1->Floor == r2->Floor)
476                 return 0;
477         else 
478         {
479                 wcsession *WCC = WC;
480                 static int columns = 3;
481                 int boxes_per_column = 0;
482                 int nf;
483
484                 nf = GetCount(WCC->Floors);
485                 while (nf % columns != 0) ++nf;
486                 boxes_per_column = (nf / columns);
487                 if (boxes_per_column < 1)
488                         boxes_per_column = 1;
489                 if (r1->Floor->AlphaN % boxes_per_column == 0)
490                         return 2;
491                 else 
492                         return 1;
493         }
494 }
495
496
497
498
499
500
501 void tmplput_ROOM_NAME(StrBuf *Target, WCTemplputParams *TP) 
502 {
503         folder *Folder = (folder *)CTX;
504
505         StrBufAppendTemplate(Target, TP, Folder->name, 0);
506 }
507 void tmplput_ROOM_BASENAME(StrBuf *Target, WCTemplputParams *TP) 
508 {
509         folder *room = (folder *)CTX;
510
511         if (room->nRoomNameParts > 1)
512                 StrBufAppendTemplate(Target, TP, 
513                                       room->RoomNameParts[room->nRoomNameParts - 1], 0);
514         else 
515                 StrBufAppendTemplate(Target, TP, room->name, 0);
516 }
517 void tmplput_ROOM_LEVEL_N_TIMES(StrBuf *Target, WCTemplputParams *TP) 
518 {
519         folder *room = (folder *)CTX;
520         int i;
521         const char *AppendMe;
522         long AppendMeLen;
523
524
525         if (room->nRoomNameParts > 1)
526         {
527                 GetTemplateTokenString(Target, TP, 0, &AppendMe, &AppendMeLen);
528                 for (i = 0; i < room->nRoomNameParts; i++)
529                         StrBufAppendBufPlain(Target, AppendMe, AppendMeLen, 0);
530         }
531 }
532
533 void tmplput_ROOM_ACL(StrBuf *Target, WCTemplputParams *TP) 
534 {
535         folder *Folder = (folder *)CTX;
536
537         StrBufAppendPrintf(Target, "%ld", Folder->RAFlags, 0);
538 }
539
540
541 void tmplput_ROOM_QRFLAGS(StrBuf *Target, WCTemplputParams *TP) 
542 {
543         folder *Folder = (folder *)CTX;
544         StrBufAppendPrintf(Target, "%d", Folder->QRFlags);
545 }
546
547 void tmplput_ROOM_RAFLAGS(StrBuf *Target, WCTemplputParams *TP) 
548 {
549         folder *Folder = (folder *)(TP->Context);
550         StrBufAppendPrintf(Target, "%d", Folder->RAFlags);
551 }
552
553
554 void tmplput_ROOM_FLOORID(StrBuf *Target, WCTemplputParams *TP) 
555 {
556         folder *Folder = (folder *)CTX;
557         StrBufAppendPrintf(Target, "%d", Folder->floorid);
558 }
559
560 void tmplput_ROOM_LISTORDER(StrBuf *Target, WCTemplputParams *TP) 
561 {
562         folder *Folder = (folder *)CTX;
563         StrBufAppendPrintf(Target, "%d", Folder->listorder);
564 }
565 void tmplput_ROOM_VIEW(StrBuf *Target, WCTemplputParams *TP) 
566 {
567         folder *Folder = (folder *)CTX;
568         StrBufAppendPrintf(Target, "%d", Folder->view);
569 }
570 void tmplput_ROOM_DEFVIEW(StrBuf *Target, WCTemplputParams *TP) 
571 {
572         folder *Folder = (folder *)CTX;
573         StrBufAppendPrintf(Target, "%d", Folder->defview);
574 }
575 void tmplput_ROOM_LASTCHANGE(StrBuf *Target, WCTemplputParams *TP) 
576 {
577         folder *Folder = (folder *)CTX;
578         StrBufAppendPrintf(Target, "%d", Folder->lastchange);
579 }
580 void tmplput_ROOM_FLOOR_ID(StrBuf *Target, WCTemplputParams *TP) 
581 {
582         folder *Folder = (folder *)CTX;
583         const Floor *pFloor = Folder->Floor;
584
585         if (pFloor == NULL)
586                 return;
587
588         StrBufAppendPrintf(Target, "%d", pFloor->ID);
589 }
590
591 void tmplput_ROOM_FLOOR_NAME(StrBuf *Target, WCTemplputParams *TP) 
592 {
593         folder *Folder = (folder *)CTX;
594         const Floor *pFloor = Folder->Floor;
595
596         if (pFloor == NULL)
597                 return;
598
599         StrBufAppendTemplate(Target, TP, pFloor->Name, 0);
600 }
601
602 void tmplput_ROOM_FLOOR_NROOMS(StrBuf *Target, WCTemplputParams *TP) 
603 {
604         folder *Folder = (folder *)CTX;
605         const Floor *pFloor = Folder->Floor;
606
607         if (pFloor == NULL)
608                 return;
609         StrBufAppendPrintf(Target, "%d", pFloor->NRooms);
610 }
611
612
613
614 int ConditionalRoomHas_UA_KNOWN(StrBuf *Target, WCTemplputParams *TP)
615 {
616         folder *Folder = (folder *)CTX;
617         return (Folder->RAFlags & UA_KNOWN) != 0;
618 }
619
620 int ConditionalRoomHas_UA_GOTOALLOWED(StrBuf *Target, WCTemplputParams *TP)
621 {
622         folder *Folder = (folder *)CTX;
623         return (Folder->RAFlags & UA_GOTOALLOWED) != 0;
624 }
625
626 int ConditionalRoomHas_UA_HASNEWMSGS(StrBuf *Target, WCTemplputParams *TP)
627 {
628         folder *Folder = (folder *)CTX;
629         return (Folder->RAFlags & UA_HASNEWMSGS) != 0;
630 }
631
632 int ConditionalRoomHas_UA_ZAPPED(StrBuf *Target, WCTemplputParams *TP)
633 {
634         folder *Folder = (folder *)CTX;
635         return (Folder->RAFlags & UA_ZAPPED) != 0;
636 }
637
638 int ConditionalRoomHas_UA_POSTALLOWED(StrBuf *Target, WCTemplputParams *TP)
639 {
640         folder *Folder = (folder *)CTX;
641         return (Folder->RAFlags & UA_POSTALLOWED) != 0;
642 }
643
644 int ConditionalRoomHas_UA_ADMINALLOWED(StrBuf *Target, WCTemplputParams *TP)
645 {
646         folder *Folder = (folder *)CTX;
647         return (Folder->RAFlags & UA_ADMINALLOWED) != 0;
648 }
649
650 int ConditionalRoomHas_UA_DELETEALLOWED(StrBuf *Target, WCTemplputParams *TP)
651 {
652         folder *Folder = (folder *)CTX;
653         return (Folder->RAFlags & UA_DELETEALLOWED) != 0;
654 }
655
656
657 int ConditionalRoomIsInbox(StrBuf *Target, WCTemplputParams *TP)
658 {
659         folder *Folder = (folder *)CTX;
660         return Folder->is_inbox;
661 }
662
663 void tmplput_ROOM_COLLECTIONTYPE(StrBuf *Target, WCTemplputParams *TP) 
664 {
665         folder *Folder = (folder *)CTX;
666         
667         switch(Folder->view) {
668         case VIEW_CALENDAR:
669                 StrBufAppendBufPlain(Target, HKEY("vevent"), 0);
670                 break;
671         case VIEW_TASKS:
672                 StrBufAppendBufPlain(Target, HKEY("vtodo"), 0);
673                 break;
674         case VIEW_ADDRESSBOOK:
675                 StrBufAppendBufPlain(Target, HKEY("vcard"), 0);
676                 break;
677         case VIEW_NOTES:
678                 StrBufAppendBufPlain(Target, HKEY("vnotes"), 0);
679                 break;
680         case VIEW_JOURNAL:
681                 StrBufAppendBufPlain(Target, HKEY("vjournal"), 0);
682                 break;
683         case VIEW_WIKI:
684                 StrBufAppendBufPlain(Target, HKEY("wiki"), 0);
685                 break;
686         }
687 }
688
689
690
691
692 int ConditionalRoomHasGroupdavContent(StrBuf *Target, WCTemplputParams *TP)
693 {
694         folder *Folder = (folder *)CTX;
695
696         lprintf(0, "-> %s: %ld\n", ChrPtr(Folder->name), Folder->view);
697
698         return ((Folder->view == VIEW_CALENDAR) || 
699                 (Folder->view == VIEW_TASKS) || 
700                 (Folder->view == VIEW_ADDRESSBOOK) ||
701                 (Folder->view == VIEW_NOTES) ||
702                 (Folder->view == VIEW_JOURNAL) );
703 }
704
705
706
707 int ConditionalFloorIsRESTSubFloor(StrBuf *Target, WCTemplputParams *TP)
708 {
709         wcsession  *WCC = WC;
710         Floor *MyFloor = (Floor *)CTX;
711         /** if we have dav_depth the client just wants the subfloors */
712         if ((WCC->Hdr->HR.dav_depth == 1) && 
713             (GetCount(WCC->Directory) == 0))
714                 return 1;
715         return WCC->CurrentFloor == MyFloor;
716 }
717
718
719 int ConditionalRoomIsRESTSubRoom(StrBuf *Target, WCTemplputParams *TP)
720 {
721         wcsession  *WCC = WC;
722         folder     *Folder = (folder *)CTX;
723         HashPos    *it;
724         StrBuf     * Dir;
725         void       *vDir;
726         long        len;
727         const char *Key;
728         int i, j, urlp;
729         int delta;
730
731
732         /* list only folders relative to the current floor... */
733         if (Folder->Floor != WCC->CurrentFloor)
734                 return 0;
735
736         urlp = GetCount(WCC->Directory);
737         delta = Folder->nRoomNameParts - urlp + 1;
738
739         lprintf(0, "\n->%s: %ld - %ld ", ChrPtr(Folder->name), urlp, 
740                 Folder->nRoomNameParts);
741         /* list only the floors which are in relation to the dav_depth header */
742         if (WCC->Hdr->HR.dav_depth != delta) {
743                 lprintf(0, "1\n");
744                 return 0;
745         }
746
747
748         it = GetNewHashPos(WCC->Directory, 0);
749         /* Fast forward the floorname we checked above... */
750         GetNextHashPos(WCC->Directory, it, &len, &Key, &vDir);
751
752         if (Folder->nRoomNameParts > 1) {               
753                 for (i = 0, j = 1; 
754                      (i > Folder->nRoomNameParts) && (j > urlp); 
755                      i++, j++)
756                 {
757                         if (!GetNextHashPos(WCC->Directory, 
758                                             it, &len, &Key, &vDir) ||
759                             (vDir == NULL))
760                         {
761                                 DeleteHashPos(&it);
762
763                                 lprintf(0, "3\n");
764                                 return 0;
765                         }
766                         Dir = (StrBuf*) vDir;
767                         if (strcmp(ChrPtr(Folder->RoomNameParts[i]), 
768                                    ChrPtr(Dir)) != 0)
769                         {
770                                 DeleteHashPos(&it);
771                                 lprintf(0, "4\n");
772                                 return 0;
773                         }
774                 }
775                 DeleteHashPos(&it);
776                 return 1;
777         }
778         else {
779                 if (!GetNextHashPos(WCC->Directory, 
780                                     it, &len, &Key, &vDir) ||
781                     (vDir == NULL))
782                 {
783                         DeleteHashPos(&it);
784                         
785                         lprintf(0, "5\n");
786                         return WCC->Hdr->HR.dav_depth == 1;
787                 }
788                 DeleteHashPos(&it);
789                 Dir = (StrBuf*) vDir;
790                 if (WCC->Hdr->HR.dav_depth == 0) {
791                         return (strcmp(ChrPtr(Folder->name), 
792                                        ChrPtr(Dir))
793                                 == 0);
794
795                 }
796                 return 0;
797         }
798 }
799
800
801 void jsonRoomFlr(void) 
802 {
803         /* Send as our own (application/json) content type */
804         hprintf("HTTP/1.1 200 OK\r\n");
805         hprintf("Content-type: application/json; charset=utf-8\r\n");
806         hprintf("Server: %s / %s\r\n", PACKAGE_STRING, ChrPtr(WC->serv_info->serv_software));
807         hprintf("Connection: close\r\n");
808         hprintf("Pragma: no-cache\r\nCache-Control: no-store\r\nExpires:-1\r\n");
809         begin_burst();
810         DoTemplate(HKEY("json_roomflr"),NULL,&NoCtx);
811         end_burst(); 
812 }
813
814
815 void 
816 SessionDetachModule_ROOMLIST
817 (wcsession *sess)
818 {
819         DeleteHash(&sess->Floors);
820         DeleteHash(&sess->Rooms);
821         DeleteHash(&sess->FloorsByName);
822 }
823
824 void 
825 InitModule_ROOMLIST
826 (void)
827 {
828         WebcitAddUrlHandler(HKEY("json_roomflr"), "", 0, jsonRoomFlr, 0);
829
830
831         RegisterNamespace("FLOOR:ID", 0, 0, tmplput_FLOOR_ID, NULL, CTX_FLOORS);
832         RegisterNamespace("FLOOR:NAME", 0, 1, tmplput_FLOOR_NAME, NULL, CTX_FLOORS);
833         RegisterNamespace("FLOOR:NROOMS", 0, 0, tmplput_FLOOR_NROOMS, NULL, CTX_FLOORS);
834         RegisterConditional(HKEY("COND:ROOM:REST:ISSUBFLOOR"), 0, ConditionalFloorIsRESTSubFloor, CTX_FLOORS);
835
836         RegisterIterator("ITERATE:THISROOM:GNET", 0, NULL, GetNetConfigHash, NULL, NULL, CTX_STRBUF, CTX_NONE, IT_NOFLAG);
837
838         RegisterIterator("LFLR", 0, NULL, GetFloorListHash, NULL, NULL, CTX_FLOORS, CTX_NONE, IT_FLAG_DETECT_GROUPCHANGE);
839
840         RegisterIterator("LKRA", 0, NULL, GetRoomListHashLKRA, NULL, NULL, CTX_ROOMS, CTX_NONE, IT_FLAG_DETECT_GROUPCHANGE);
841
842         RegisterNamespace("ROOM:INFO:FLOORID", 0, 1, tmplput_ROOM_FLOORID, NULL, CTX_ROOMS);
843         RegisterNamespace("ROOM:INFO:NAME", 0, 1, tmplput_ROOM_NAME, NULL, CTX_ROOMS);
844         RegisterNamespace("ROOM:INFO:PRINT_NAME", 0, 1, tmplput_ROOM_NAME, NULL, CTX_ROOMS);/// TODO!
845         RegisterNamespace("ROOM:INFO:BASENAME", 0, 1, tmplput_ROOM_BASENAME, NULL, CTX_ROOMS);
846         RegisterNamespace("ROOM:INFO:LEVELNTIMES", 1, 2, tmplput_ROOM_LEVEL_N_TIMES, NULL, CTX_ROOMS);
847
848         RegisterNamespace("ROOM:INFO:ACL", 0, 1, tmplput_ROOM_ACL, NULL, CTX_ROOMS);
849         RegisterNamespace("ROOM:INFO:QRFLAGS", 0, 1, tmplput_ROOM_QRFLAGS, NULL, CTX_ROOMS);
850         RegisterNamespace("ROOM:INFO:RAFLAGS", 0, 1, tmplput_ROOM_RAFLAGS, NULL, CTX_ROOMS);
851         RegisterNamespace("ROOM:INFO:LISTORDER", 0, 1, tmplput_ROOM_LISTORDER, NULL, CTX_ROOMS);
852         RegisterNamespace("ROOM:INFO:VIEW", 0, 1, tmplput_ROOM_VIEW, NULL, CTX_ROOMS);
853         RegisterNamespace("ROOM:INFO:DEFVIEW", 0, 1, tmplput_ROOM_DEFVIEW, NULL, CTX_ROOMS);
854         RegisterNamespace("ROOM:INFO:LASTCHANGE", 0, 1, tmplput_ROOM_LASTCHANGE, NULL, CTX_ROOMS);
855         RegisterNamespace("ROOM:INFO:COLLECTIONTYPE", 0, 1, tmplput_ROOM_COLLECTIONTYPE, NULL, CTX_ROOMS);
856         RegisterNamespace("ROOM:INFO:FLOOR:ID", 0, 0, tmplput_ROOM_FLOOR_ID, NULL, CTX_ROOMS);
857         RegisterNamespace("ROOM:INFO:FLOOR:NAME", 0, 1, tmplput_ROOM_FLOOR_NAME, NULL, CTX_ROOMS);
858         RegisterNamespace("ROOM:INFO:FLOOR:NROOMS", 0, 0, tmplput_ROOM_FLOOR_NROOMS, NULL, CTX_ROOMS);
859
860         RegisterConditional(HKEY("COND:ROOM:REST:ISSUBROOM"), 0, ConditionalRoomIsRESTSubRoom, CTX_ROOMS);
861
862         RegisterConditional(HKEY("COND:ROOM:INFO:IS_INBOX"), 0, ConditionalRoomIsInbox, CTX_ROOMS);
863         RegisterConditional(HKEY("COND:ROOM:FLAGS:UA_KNOWN"), 0, ConditionalRoomHas_UA_KNOWN, CTX_ROOMS);
864         RegisterConditional(HKEY("COND:ROOM:FLAGS:UA_GOTOALLOWED"), 0, ConditionalRoomHas_UA_GOTOALLOWED, CTX_ROOMS);
865         RegisterConditional(HKEY("COND:ROOM:FLAGS:UA_HASNEWMSGS"), 0, ConditionalRoomHas_UA_HASNEWMSGS, CTX_ROOMS);
866         RegisterConditional(HKEY("COND:ROOM:FLAGS:UA_ZAPPED"), 0, ConditionalRoomHas_UA_ZAPPED, CTX_ROOMS);
867         RegisterConditional(HKEY("COND:ROOM:FLAGS:UA_POSTALLOWED"), 0, ConditionalRoomHas_UA_POSTALLOWED, CTX_ROOMS);
868         RegisterConditional(HKEY("COND:ROOM:FLAGS:UA_ADMINALLOWED"), 0, ConditionalRoomHas_UA_ADMINALLOWED, CTX_ROOMS);
869         RegisterConditional(HKEY("COND:ROOM:FLAGS:UA_DELETEALLOWED"), 0, ConditionalRoomHas_UA_DELETEALLOWED, CTX_ROOMS);
870         RegisterConditional(HKEY("COND:ROOM:GROUPDAV_CONTENT"), 0, ConditionalRoomHasGroupdavContent, CTX_ROOMS);
871
872
873
874         RegisterSortFunc(HKEY("byfloorroom"),
875                          NULL, 0,
876                          CompareRoomListByFloorRoomPrivFirst,
877                          CompareRoomListByFloorRoomPrivFirstRev,
878                          GroupchangeRoomListByFloorRoomPrivFirst,
879                          CTX_ROOMS);
880
881 }