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