Mailing list header changes (fuck you Google)
[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         StrBuf *RoomName;
431         HashList *PossibleAliases = NULL;
432         
433         const char *pComma;
434         const char *pAt;
435         long aliaslen;
436         long locallen;
437         long State;
438         long n = 0;
439
440         Domains = GetValidDomainNames(Target, TP);
441         if (Domains == NULL)
442                 return NULL;
443         if (GetCount(Domains) == 0) {
444                 DeleteHash(&Domains);
445                 return NULL;
446         }
447         PossibleAliases = NewHash(1, NULL);
448         Line = NewStrBuf();
449         RoomName = NewStrBufDup(WCC->CurRoom.name);
450         StrBufAsciify(RoomName, '_');
451         StrBufReplaceChars(RoomName, ' ', '_');
452
453         AppendPossibleAliasWithDomain(PossibleAliases,
454                                       &n,
455                                       Domains,
456                                       HKEY("room_"),
457                                       SKEY(RoomName));
458
459
460         serv_puts("GNET "FILE_MAILALIAS);
461         StrBuf_ServGetln(Line);
462         if (GetServerStatus(Line, &State) == 1) 
463         {
464                 int Done = 0;
465
466                 while(!Done && (StrBuf_ServGetln(Line) >= 0))
467                         if ( (StrLength(Line)==3) && 
468                              !strcmp(ChrPtr(Line), "000"))
469                         {
470                                 Done = 1;
471                         }
472                         else
473                         {
474                                 pComma = strchr(ChrPtr(Line), ',');
475                                 if (pComma == NULL)
476                                         continue;
477                                 aliaslen = pComma - ChrPtr(Line);
478                                 locallen = StrLength(Line) - 1 - aliaslen;
479                                 if (locallen - 5 != StrLength(WCC->CurRoom.name))
480                                         continue;
481                                 if (strncmp(pComma + 1, "room_", 5) != 0)
482                                         continue;
483
484                                 if (strcasecmp(pComma + 6, ChrPtr(WCC->CurRoom.name)) != 0)
485                                         continue;
486                                 pAt = strchr(ChrPtr(Line), '@');
487                                 if ((pAt == NULL) || (pAt > pComma))
488                                 {
489                                         AppendPossibleAliasWithDomain(PossibleAliases,
490                                                                       &n,
491                                                                       Domains,
492                                                                       HKEY(""),
493                                                                       ChrPtr(Line),
494                                                                       aliaslen);
495                                         n++;
496                                 }
497                                 else
498                                 {
499                                         
500                                         Token = NewStrBufPlain(ChrPtr(Line), aliaslen);
501                                         Put(PossibleAliases,
502                                             IKEY(n),
503                                             Token,
504                                             HFreeStrBuf);
505                                         n++;
506                                 }
507                         }
508         }
509         else if (State == 550)
510                 AppendImportantMessage(_("Higher access is required to access this function."), -1);
511         DeleteHash(&Domains);
512         FreeStrBuf(&Line);
513         FreeStrBuf(&RoomName);
514         return PossibleAliases;
515 }
516
517
518 HashList *GetNetConfigHash(StrBuf *Target, WCTemplputParams *TP) 
519 {
520         wcsession *WCC = WC;
521         StrBuf *Line;
522         StrBuf *Token;
523         StrBuf *Content;
524         long WantThisOne;
525         long PutTo;
526         long State;
527         
528         WantThisOne = GetTemplateTokenNumber(Target, TP, 5, -1);
529         if ((WantThisOne < 0) || (WantThisOne > maxRoomNetCfg))
530                 return NULL;
531         if (WCC->CurRoom.IgnetCfgs[maxRoomNetCfg] == (HashList*) StrBufNOTNULL)
532                 return WCC->CurRoom.IgnetCfgs[WantThisOne];
533
534         WCC->CurRoom.IgnetCfgs[maxRoomNetCfg] = (HashList*) StrBufNOTNULL;
535         serv_puts("GNET");
536         Line = NewStrBuf();
537         Token = NewStrBuf();
538         StrBuf_ServGetln(Line);
539         if (GetServerStatus(Line, &State) == 1) 
540         {
541                 const char *Pos = NULL;
542                 int Done = 0;
543                 int HaveRoomMailAlias = 0;
544
545                 while(!Done && (StrBuf_ServGetln(Line) >= 0))
546                 {
547                         if (StrLength(Line) == 0)
548                                 continue;
549                         if ( (StrLength(Line)==3) && 
550                              !strcmp(ChrPtr(Line), "000"))
551                         {
552                                 Done = 1;
553                         }
554                         else
555                         {
556                                 StrBufExtract_NextToken(Token, Line, &Pos, '|');
557                                 PutTo = GetTokenDefine(SKEY(Token), -1);
558                                 if (PutTo == roommailalias)
559                                 {
560                                         if (HaveRoomMailAlias > 0)
561                                                 continue; /* Only ONE alias possible! */
562                                         HaveRoomMailAlias++;
563                                 }
564                                 if ((PutTo >= 0) && 
565                                     (PutTo < maxRoomNetCfg) &&
566                                     (Pos != StrBufNOTNULL))
567                                 {
568                                         int n;
569                                         HashList *SubH;
570                                         
571                                         if (WCC->CurRoom.IgnetCfgs[PutTo] == NULL)
572                                         {
573                                                 n = 0;
574                                                 WCC->CurRoom.IgnetCfgs[PutTo] = NewHash(1, NULL);
575                                         }
576                                         else 
577                                         {
578                                                 n = GetCount(WCC->CurRoom.IgnetCfgs[PutTo]);
579                                         }
580                                         SubH = NewHash(1, NULL);
581                                         Put(WCC->CurRoom.IgnetCfgs[PutTo], 
582                                             IKEY(n),
583                                             SubH, 
584                                             HDeleteHash);
585                                         n = 1; /* #0 is the type... */
586                                         while (Pos != StrBufNOTNULL) {
587                                                 Content = NewStrBuf();
588                                                 StrBufExtract_NextToken(Content, Line, &Pos, '|');
589
590                                                 if ((PutTo == roommailalias) && n == 1)
591                                                         WCC->CurRoom.RoomAlias = Content;
592
593                                                 Put(SubH, 
594                                                     IKEY(n),
595                                                     Content, 
596                                                     HFreeStrBuf);
597                                                 n++;
598                                         }
599                                 }
600                                 Pos = NULL;
601                         }
602                 }
603         }
604         else if (State == 550)
605                 AppendImportantMessage(_("Higher access is required to access this function."), -1);
606
607         FreeStrBuf(&Line);
608         FreeStrBuf(&Token);
609
610         return WCC->CurRoom.IgnetCfgs[WantThisOne];
611 }
612
613 /** Unused function that orders rooms by the listorder flag */
614 int SortRoomsByListOrder(const void *room1, const void *room2) 
615 {
616         folder *r1 = (folder*) GetSearchPayload(room1);
617         folder *r2 = (folder*) GetSearchPayload(room2);
618   
619         if (r1->Order == r2->Order) return 0;
620         if (r1->Order > r2->Order) return 1;
621         return -1;
622 }
623
624 int CompareRoomListByFloorRoomPrivFirst(const void *room1, const void *room2) 
625 {
626         folder *r1 = (folder*) GetSearchPayload(room1);
627         folder *r2 = (folder*) GetSearchPayload(room2);
628   
629         if ((r1->Floor == NULL)  ||
630             (r2->Floor == NULL))
631                 return 0;
632                 
633         /**
634          * are we on the same floor? else sort by floor.
635          */
636         if (r1->Floor != r2->Floor)
637         {
638                 /**
639                  * the private rooms are first in any case.
640                  */
641                 if (r1->Floor->ID == VIRTUAL_MY_FLOOR)
642                         return -1;
643                 if (r2->Floor->ID == VIRTUAL_MY_FLOOR)
644                         return 1;
645                 /**
646                  * else decide alpaheticaly by floorname
647                  */
648                 return (r1->Floor->AlphaN > r2->Floor->AlphaN)? 1 : -1;
649         }
650
651         /**
652          * if we have different levels of subdirectories, 
653          * we want the toplevel to be first, regardless of sort
654          * sequence.
655          */
656         if (((r1->nRoomNameParts > 1) || 
657             (r2->nRoomNameParts > 1)    )&&
658             (r1->nRoomNameParts != r2->nRoomNameParts))
659         {
660                 int i, ret;
661                 int nparts = (r1->nRoomNameParts > r2->nRoomNameParts)?
662                         r2->nRoomNameParts : r1->nRoomNameParts;
663
664                 for (i=0; i < nparts; i++)
665                 {
666                         ret = strcmp (ChrPtr(r1->name), 
667                                       ChrPtr(r2->name));
668                         /**
669                          * Deltas in common parts? exit here.
670                          */
671                         if (ret != 0) 
672                                 return ret;
673                 }
674
675                 /**
676                  * who's a subdirectory of whom?
677                  */
678                 if (r1->nRoomNameParts > r2->nRoomNameParts)
679                         return 1;
680                 else
681                         return -1;
682
683         }
684
685         /**
686          * else just sort alphabeticaly.
687          */
688         return strcmp (ChrPtr(r1->name), 
689                        ChrPtr(r2->name));
690 }
691
692 int CompareRoomListByFloorRoomPrivFirstRev(const void *room1, const void *room2) 
693 {
694         folder *r1 = (folder*) GetSearchPayload(room1);
695         folder *r2 = (folder*) GetSearchPayload(room2);
696
697         if ((r1->Floor == NULL)  ||
698             (r2->Floor == NULL))
699                 return 0;
700
701         /**
702          * are we on the same floor? else sort by floor.
703          */
704         if (r2->Floor != r1->Floor)
705         {
706                 /**
707                  * the private rooms are first in any case.
708                  */
709                 if (r1->Floor->ID == VIRTUAL_MY_FLOOR)
710                         return -1;
711                 if (r2->Floor->ID == VIRTUAL_MY_FLOOR)
712                         return 1;
713                 /**
714                  * else decide alpaheticaly by floorname
715                  */
716
717                 return (r1->Floor->AlphaN < r2->Floor->AlphaN)? 1 : -1;
718         }
719
720         /**
721          * if we have different levels of subdirectories, 
722          * we want the toplevel to be first, regardless of sort
723          * sequence.
724          */
725         if (((r1->nRoomNameParts > 1) || 
726             (r2->nRoomNameParts > 1)    )&&
727             (r1->nRoomNameParts != r2->nRoomNameParts))
728         {
729                 int i, ret;
730                 int nparts = (r1->nRoomNameParts > r2->nRoomNameParts)?
731                         r2->nRoomNameParts : r1->nRoomNameParts;
732
733                 for (i=0; i < nparts; i++)
734                 {
735                         /**
736                          * special cases if one room is top-level...
737                          */
738                         if (r2->nRoomNameParts == 1)
739                                 ret = strcmp (ChrPtr(r2->name), 
740                                               ChrPtr(r1->RoomNameParts[i]));
741                         else if (r1->nRoomNameParts == 1)
742                                 ret = strcmp (ChrPtr(r2->RoomNameParts[i]),
743                                               ChrPtr(r1->name));
744                         else 
745                                 ret = strcmp (ChrPtr(r2->RoomNameParts[i]), 
746                                               ChrPtr(r1->RoomNameParts[i]));
747                         /**
748                          * Deltas in common parts? exit here.
749                          */
750                         if (ret != 0) 
751                                 return ret;
752                 }
753
754                 /**
755                  * who's a subdirectory of whom?
756                  */
757                 if (r1->nRoomNameParts > r2->nRoomNameParts)
758                         return 1;
759                 else
760                         return -1;
761         }
762
763         return strcmp (ChrPtr(r2->name), 
764                        ChrPtr(r1->name));
765 }
766
767 int GroupchangeRoomListByFloorRoomPrivFirst(const void *room1, const void *room2) 
768 {
769         folder *r1 = (folder*) room1;
770         folder *r2 = (folder*) room2;
771   
772
773         if ((r1->Floor == NULL)  ||
774             (r2->Floor == NULL))
775                 return 0;
776                 
777         if (r1->Floor == r2->Floor)
778                 return 0;
779         else 
780         {
781                 wcsession *WCC = WC;
782                 static int columns = 3;
783                 int boxes_per_column = 0;
784                 int nf;
785
786                 nf = GetCount(WCC->Floors);
787                 while (nf % columns != 0) ++nf;
788                 boxes_per_column = (nf / columns);
789                 if (boxes_per_column < 1)
790                         boxes_per_column = 1;
791                 if (r1->Floor->AlphaN % boxes_per_column == 0)
792                         return 2;
793                 else 
794                         return 1;
795         }
796 }
797
798
799 int CompareRooms(const folder *room1, const folder *room2) 
800 {
801         if ((room1 == NULL) || (room2 == NULL))
802                 return -1;
803         return CompareRoomListByFloorRoomPrivFirst(room1, room2);
804 }
805
806 int ConditionalThisRoomIsStrBufContextAlias(StrBuf *Target, WCTemplputParams *TP)
807 {
808         wcsession       *WCC = WC;
809         const char      *pVal;
810         long             len;
811         eRoomParamType   ParamType;
812
813         ParamType = GetTemplateTokenNumber(Target, TP, 2, eNotSet);
814         GetTemplateTokenString(Target, TP, 3, &pVal, &len);
815
816         if (ParamType == eNotSet)
817         {
818                 return StrLength(WCC->CurRoom.RoomAlias) == 0;
819         }
820         else if (ParamType == eDomain)
821         {
822                 const StrBuf *CtxStr = (const StrBuf*) CTX(CTX_STRBUF);
823                 const char *pAt;
824
825                 if (CtxStr == NULL) 
826                         return 0;
827                 
828                 if (StrLength(WCC->CurRoom.RoomAlias) == 0)
829                         return 0;
830
831                 if (strncmp(ChrPtr(WCC->CurRoom.RoomAlias), "room_", 5) != 0)
832                         return 0;
833
834                 pAt = strchr(ChrPtr(WCC->CurRoom.RoomAlias), '@');
835                 if (pAt == NULL)
836                         return 0;
837                 return strcmp(pAt + 1, ChrPtr(CtxStr)) == 0;
838         }
839         else if (ParamType == eAlias)
840         {
841                 const StrBuf *CtxStr = (const StrBuf*) CTX(CTX_STRBUF);
842
843                 if (CtxStr == NULL) 
844                         return 0;
845                 
846                 if (StrLength(WCC->CurRoom.RoomAlias) == 0)
847                         return 0;
848
849                 return strcmp(ChrPtr(WCC->CurRoom.RoomAlias), ChrPtr(CtxStr)) == 0;
850         }
851         else
852         {
853                 LogTemplateError(Target, "TokenParameter", 2, TP, 
854                                  "Invalid paramtype; need one of [eNotSet|eDomain|eAlias]");
855                 return 0;
856         }
857
858 }
859
860 int ConditionalRoomIsRESTSubRoom(StrBuf *Target, WCTemplputParams *TP)
861 {
862         wcsession  *WCC = WC;
863         folder     *Folder = (folder *)CTX(CTX_ROOMS);
864         HashPos    *it;
865         StrBuf     * Dir;
866         void       *vDir;
867         long        len;
868         const char *Key;
869         int i, j, urlp;
870         int delta;
871
872
873         /* list only folders relative to the current floor... */
874         if (Folder->Floor != WCC->CurrentFloor)
875                 return 0;
876
877         urlp = GetCount(WCC->Directory);
878         delta = Folder->nRoomNameParts - urlp + 1;
879
880         syslog(LOG_DEBUG, "\n->%s: %d - %ld ", 
881                ChrPtr(Folder->name), 
882                urlp, 
883                Folder->nRoomNameParts);
884         /* list only the floors which are in relation to the dav_depth header */
885         if (WCC->Hdr->HR.dav_depth != delta) {
886                 syslog(LOG_DEBUG, "1\n");
887                 return 0;
888         }
889
890
891         it = GetNewHashPos(WCC->Directory, 0);
892         /* Fast forward the floorname we checked above... */
893         GetNextHashPos(WCC->Directory, it, &len, &Key, &vDir);
894
895         if (Folder->nRoomNameParts > 1) {               
896                 for (i = 0, j = 1; 
897                      (i > Folder->nRoomNameParts) && (j > urlp); 
898                      i++, j++)
899                 {
900                         if (!GetNextHashPos(WCC->Directory, 
901                                             it, &len, &Key, &vDir) ||
902                             (vDir == NULL))
903                         {
904                                 DeleteHashPos(&it);
905
906                                 syslog(LOG_DEBUG, "3\n");
907                                 return 0;
908                         }
909                         Dir = (StrBuf*) vDir;
910                         if (strcmp(ChrPtr(Folder->RoomNameParts[i]), 
911                                    ChrPtr(Dir)) != 0)
912                         {
913                                 DeleteHashPos(&it);
914                                 syslog(LOG_DEBUG, "4\n");
915                                 return 0;
916                         }
917                 }
918                 DeleteHashPos(&it);
919                 return 1;
920         }
921         else {
922                 if (!GetNextHashPos(WCC->Directory, 
923                                     it, &len, &Key, &vDir) ||
924                     (vDir == NULL))
925                 {
926                         DeleteHashPos(&it);
927                         
928                         syslog(LOG_DEBUG, "5\n");
929                         return WCC->Hdr->HR.dav_depth == 1;
930                 }
931                 DeleteHashPos(&it);
932                 Dir = (StrBuf*) vDir;
933                 if (WCC->Hdr->HR.dav_depth == 0) {
934                         return (strcmp(ChrPtr(Folder->name), 
935                                        ChrPtr(Dir))
936                                 == 0);
937
938                 }
939                 return 0;
940         }
941 }
942
943
944 void 
945 InitModule_ROOMLIST
946 (void)
947 {
948         /* we duplicate this, just to be shure its already done. */
949         RegisterCTX(CTX_ROOMS);
950         RegisterCTX(CTX_FLOORS);
951
952         RegisterIterator("ITERATE:THISROOM:WHO_KNOWS", 0, NULL, GetWhoKnowsHash, NULL, DeleteHash, CTX_STRBUF, CTX_NONE, IT_NOFLAG);
953         RegisterIterator("ITERATE:THISROOM:GNET", 1, NULL, GetNetConfigHash, NULL, NULL, CTX_STRBUFARR, CTX_NONE, IT_NOFLAG);
954         RegisterIterator("ITERATE:THISROOM:MALIAS", 1, NULL, GetThisRoomMAlias, NULL, DeleteHash, CTX_STRBUF, CTX_NONE, IT_NOFLAG);
955         RegisterIterator("ITERATE:THISROOM:POSSIBLE:MALIAS", 1, NULL, GetThisRoomPossibleMAlias, NULL, DeleteHash, CTX_STRBUF, CTX_NONE, IT_NOFLAG);
956
957         RegisterIterator("LFLR", 0, NULL, GetFloorListHash, NULL, NULL, CTX_FLOORS, CTX_NONE, IT_FLAG_DETECT_GROUPCHANGE);
958         RegisterIterator("LKRA", 0, NULL, GetRoomListHashLKRA, NULL, NULL, CTX_ROOMS, CTX_NONE, IT_FLAG_DETECT_GROUPCHANGE);
959         RegisterIterator("LZRM", 0, NULL, GetZappedRoomListHash, NULL, DeleteHash, CTX_ROOMS, CTX_NONE, IT_FLAG_DETECT_GROUPCHANGE);
960         RegisterIterator("LPRM", 0, NULL, GetRoomListHashLPRM, NULL, DeleteHash, CTX_ROOMS, CTX_NONE, IT_FLAG_DETECT_GROUPCHANGE);
961
962
963         REGISTERTokenParamDefine(eNotSet);
964         REGISTERTokenParamDefine(eDomain);
965         REGISTERTokenParamDefine(eAlias);
966
967
968         RegisterConditional("COND:ROOM:REST:ISSUBROOM", 0, ConditionalRoomIsRESTSubRoom, CTX_ROOMS);
969
970         RegisterConditional("COND:THISROOM:ISALIAS:CONTEXTSTR", 0, ConditionalThisRoomIsStrBufContextAlias, CTX_NONE);
971
972         RegisterSortFunc(HKEY("byfloorroom"),
973                          NULL, 0,
974                          CompareRoomListByFloorRoomPrivFirst,
975                          CompareRoomListByFloorRoomPrivFirstRev,
976                          GroupchangeRoomListByFloorRoomPrivFirst,
977                          CTX_ROOMS);
978
979 }