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