7716506335ba20a36cc141831d1fa7272e7b1001
[citadel.git] / webcit / roomops.c
1 /* $Id$ */
2
3
4 #include <ctype.h>
5 #include <stdlib.h>
6 #include <unistd.h>
7 #include <stdio.h>
8 #include <fcntl.h>
9 #include <signal.h>
10 #include <sys/types.h>
11 #include <sys/wait.h>
12 #include <sys/socket.h>
13 #include <sys/time.h>
14 #include <limits.h>
15 #include <netinet/in.h>
16 #include <netdb.h>
17 #include <string.h>
18 #include <pwd.h>
19 #include <errno.h>
20 #include <stdarg.h>
21 #include <pthread.h>
22 #include <signal.h>
23 #include "webcit.h"
24
25 struct folder {
26         int floor;
27         char room[SIZ];
28         char name[SIZ];
29         int hasnewmsgs;
30         int is_mailbox;
31         int selectable;
32 };
33
34 char *viewdefs[] = {
35         "Messages",
36         "Summary",
37         "Address Book",
38         "Calendar",
39         "Tasks"
40 };
41
42 char floorlist[128][SIZ];
43
44 /*
45  * load the list of floors
46  */
47 void load_floorlist(void)
48 {
49         int a;
50         char buf[SIZ];
51
52         for (a = 0; a < 128; ++a)
53                 floorlist[a][0] = 0;
54
55         serv_puts("LFLR");
56         serv_gets(buf);
57         if (buf[0] != '1') {
58                 strcpy(floorlist[0], "Main Floor");
59                 return;
60         }
61         while (serv_gets(buf), strcmp(buf, "000")) {
62                 extract(floorlist[extract_int(buf, 0)], buf, 1);
63         }
64 }
65
66
67 /*
68  * remove a room from the march list
69  */
70 void remove_march(char *aaa)
71 {
72         struct march *mptr, *mptr2;
73
74         if (WC->march == NULL)
75                 return;
76
77         if (!strcasecmp(WC->march->march_name, aaa)) {
78                 mptr = WC->march->next;
79                 free(WC->march);
80                 WC->march = mptr;
81                 return;
82         }
83         mptr2 = WC->march;
84         for (mptr = WC->march; mptr != NULL; mptr = mptr->next) {
85                 if (!strcasecmp(mptr->march_name, aaa)) {
86                         mptr2->next = mptr->next;
87                         free(mptr);
88                         mptr = mptr2;
89                 } else {
90                         mptr2 = mptr;
91                 }
92         }
93 }
94
95
96
97
98
99 void room_tree_list(struct roomlisting *rp)
100 {
101         char rmname[64];
102         int f;
103
104         if (rp == NULL) {
105                 return;
106         }
107
108         room_tree_list(rp->lnext);
109
110         strcpy(rmname, rp->rlname);
111         f = rp->rlflags;
112
113         wprintf("<A HREF=\"/dotgoto&room=");
114         urlescputs(rmname);
115         wprintf("\"");
116         wprintf(">");
117         escputs1(rmname, 1, 1);
118         if ((f & QR_DIRECTORY) && (f & QR_NETWORK))
119                 wprintf("}");
120         else if (f & QR_DIRECTORY)
121                 wprintf("]");
122         else if (f & QR_NETWORK)
123                 wprintf(")");
124         else
125                 wprintf("&gt;");
126         wprintf("</A><TT> </TT>\n");
127
128         room_tree_list(rp->rnext);
129         free(rp);
130 }
131
132
133 /* 
134  * Room ordering stuff (compare first by floor, then by order)
135  */
136 int rordercmp(struct roomlisting *r1, struct roomlisting *r2)
137 {
138         if ((r1 == NULL) && (r2 == NULL))
139                 return (0);
140         if (r1 == NULL)
141                 return (-1);
142         if (r2 == NULL)
143                 return (1);
144         if (r1->rlfloor < r2->rlfloor)
145                 return (-1);
146         if (r1->rlfloor > r2->rlfloor)
147                 return (1);
148         if (r1->rlorder < r2->rlorder)
149                 return (-1);
150         if (r1->rlorder > r2->rlorder)
151                 return (1);
152         return (0);
153 }
154
155
156 /*
157  * Common code for all room listings
158  */
159 void listrms(char *variety)
160 {
161         char buf[SIZ];
162         int num_rooms = 0;
163
164         struct roomlisting *rl = NULL;
165         struct roomlisting *rp;
166         struct roomlisting *rs;
167
168
169         /* Ask the server for a room list */
170         serv_puts(variety);
171         serv_gets(buf);
172         if (buf[0] != '1') {
173                 wprintf("&nbsp;");
174                 return;
175         }
176         while (serv_gets(buf), strcmp(buf, "000")) {
177                 ++num_rooms;
178                 rp = malloc(sizeof(struct roomlisting));
179                 extract(rp->rlname, buf, 0);
180                 rp->rlflags = extract_int(buf, 1);
181                 rp->rlfloor = extract_int(buf, 2);
182                 rp->rlorder = extract_int(buf, 3);
183                 rp->lnext = NULL;
184                 rp->rnext = NULL;
185
186                 rs = rl;
187                 if (rl == NULL) {
188                         rl = rp;
189                 } else
190                         while (rp != NULL) {
191                                 if (rordercmp(rp, rs) < 0) {
192                                         if (rs->lnext == NULL) {
193                                                 rs->lnext = rp;
194                                                 rp = NULL;
195                                         } else {
196                                                 rs = rs->lnext;
197                                         }
198                                 } else {
199                                         if (rs->rnext == NULL) {
200                                                 rs->rnext = rp;
201                                                 rp = NULL;
202                                         } else {
203                                                 rs = rs->rnext;
204                                         }
205                                 }
206                         }
207         }
208
209         room_tree_list(rl);
210
211         /* If no rooms were listed, print an nbsp to make the cell
212          * borders show up anyway.
213          */
214         if (num_rooms == 0) wprintf("&nbsp;");
215 }
216
217
218 /*
219  * list all rooms by floor (only should get called from knrooms() because
220  * that's where output_headers() is called from)
221  */
222 void tabular_room_list(void)
223 {
224         int a;
225         char buf[SIZ];
226
227         do_template("beginbox_nt");
228         wprintf("<TABLE width=100%% border><TR><TH>Floor</TH>");
229         wprintf("<TH>Rooms with new messages</TH>");
230         wprintf("<TH>Rooms with no new messages</TH></TR>\n");
231
232         for (a = 0; a < 128; ++a)
233                 if (floorlist[a][0] != 0) {
234
235                         /* Floor name column */
236                         wprintf("<TR><TD>");
237
238                         serv_printf("OIMG _floorpic_|%d", a);
239                         serv_gets(buf);
240                         if (buf[0] == '2') {
241                                 serv_puts("CLOS");
242                                 serv_gets(buf);
243                                 wprintf("<IMG SRC=\"/image&name=_floorpic_&parm=%d\" ALT=\"%s\">",
244                                         a, &floorlist[a][0]);
245                         } else {
246                                 escputs(&floorlist[a][0]);
247                         }
248
249                         wprintf("</TD>");
250
251                         /* Rooms with new messages column */
252                         wprintf("<TD>");
253                         sprintf(buf, "LKRN %d", a);
254                         listrms(buf);
255                         wprintf("</TD>\n<TD>");
256
257                         /* Rooms with old messages column */
258                         sprintf(buf, "LKRO %d", a);
259                         listrms(buf);
260                         wprintf("</TD></TR>\n");
261                 }
262         wprintf("</TABLE>\n");
263         do_template("endbox");
264         wDumpContent(1);
265 }
266
267
268
269
270
271
272
273
274
275 /*
276  * list all forgotten rooms
277  */
278 void zapped_list(void)
279 {
280         output_headers(3);
281
282         svprintf("BOXTITLE", WCS_STRING, "Zapped (forgotten) rooms");
283         do_template("beginbox");
284
285         listrms("LZRM -1");
286
287         wprintf("<BR><BR>\n");
288         wprintf("Click on any room to un-zap it and goto that room.\n");
289         do_template("endbox");
290         wDumpContent(1);
291 }
292
293
294 /*
295  * read this room's info file (set v to 1 for verbose mode)
296  */
297 void readinfo(void)
298 {
299         char buf[SIZ];
300
301         serv_puts("RINF");
302         serv_gets(buf);
303         if (buf[0] == '1') {
304                 fmout(NULL, "CENTER");
305         }
306 }
307
308
309
310
311 /* Display room graphic.  The server doesn't actually
312  * need the room name, but we supply it in order to
313  * keep the browser from using a cached graphic from 
314  * another room.
315  */
316 void embed_room_graphic(void) {
317         char buf[SIZ];
318
319         serv_puts("OIMG _roompic_");
320         serv_gets(buf);
321         if (buf[0] == '2') {
322                 wprintf("<TD BGCOLOR=\"#444455\">");
323                 wprintf("<IMG HEIGHT=64 SRC=\"/image&name=_roompic_&room=");
324                 urlescputs(WC->wc_roomname);
325                 wprintf("\"></TD>");
326                 serv_puts("CLOS");
327                 serv_gets(buf);
328         }
329
330 }
331
332
333 /* Let the user know if new mail has arrived 
334  */
335 void embed_newmail_button(void) {
336         if ( (WC->new_mail > WC->remember_new_mail) && (WC->new_mail>0) ) {
337                 wprintf(
338                         "<A HREF=\"/dotgoto?room=_MAIL_\">"
339                         "<IMG SRC=\"/static/mail.gif\" border=0 "
340                         "ALT=\"You have new mail\">"
341                         "<BR><SPAN CLASS=\"youhavemail\">"
342                         "%d new mail</SPAN></A>", WC->new_mail);
343                 WC->remember_new_mail = WC->new_mail;
344         }
345 }
346
347
348
349 /*
350  * Display the current view and offer an option to change it
351  */
352 void embed_view_o_matic(void) {
353         int i;
354
355         wprintf("<FORM NAME=\"viewomatic\">\n"
356                 "<SELECT NAME=\"newview\" SIZE=\"1\" "
357                 "OnChange=\"location.href=viewomatic.newview.options"
358                 "[selectedIndex].value\">\n");
359
360         for (i=0; i<(sizeof viewdefs / sizeof (char *)); ++i) {
361                 /*
362                  * Only offer the views that make sense, given the default
363                  * view for the room.  For example, don't offer a Calendar
364                  * view in a non-Calendar room.
365                  */
366                 if (
367                         (i == WC->wc_view)
368                    ||   (i == WC->wc_default_view)
369                    ||   ( (i == 0) && (WC->wc_default_view == 1) )
370                    ||   ( (i == 1) && (WC->wc_default_view == 0) )
371                 ) {
372
373                         wprintf("<OPTION %s VALUE=\"/changeview?view=%d\">",
374                                 ((i == WC->wc_view) ? "SELECTED" : ""),
375                                 i );
376                         escputs(viewdefs[i]);
377                         wprintf("</OPTION>\n");
378                 }
379         }
380         wprintf("</SELECT></FORM>\n");
381 }
382
383
384
385 void embed_room_banner(char *got) {
386         char fakegot[SIZ];
387
388         /* We need to have the information returned by a GOTO server command.
389          * If it isn't supplied, we fake it by issuing our own GOTO.
390          */
391         if (got == NULL) {
392                 serv_printf("GOTO %s", WC->wc_roomname);
393                 serv_gets(fakegot);
394                 got = fakegot;
395         }
396
397         /* If the user happens to select the "make this my start page" link,
398          * we want it to remember the URL as a "/dotskip" one instead of
399          * a "skip" or "gotonext" or something like that.
400          */
401         snprintf(WC->this_page, sizeof(WC->this_page), "/dotskip&room=%s",
402                 WC->wc_roomname);
403
404         /* Check for new mail. */
405         WC->new_mail = extract_int(&got[4], 9);
406         WC->wc_view = extract_int(&got[4], 11);
407
408         svprintf("ROOMNAME", WCS_STRING, "%s", WC->wc_roomname);
409         svprintf("NEWMSGS", WCS_STRING, "%d", extract_int(&got[4], 1));
410         svprintf("TOTALMSGS", WCS_STRING, "%d", extract_int(&got[4], 2));
411         svcallback("ROOMPIC", embed_room_graphic);
412         svcallback("ROOMINFO", readinfo);
413         svcallback("YOUHAVEMAIL", embed_newmail_button);
414         svcallback("VIEWOMATIC", embed_view_o_matic);
415         svcallback("START", offer_start_page);
416
417         do_template("roombanner");
418         clear_local_substs();
419 }
420
421
422
423
424
425 /*
426  * generic routine to take the session to a new room
427  *
428  * display_name values:  0 = goto only
429  *                       1 = goto and display
430  *                       2 = display only
431  */
432 void gotoroom(char *gname, int display_name)
433 {
434         char buf[SIZ];
435         static long ls = (-1L);
436
437
438         if (display_name) {
439                 output_headers(0);
440                 wprintf("Pragma: no-cache\n");
441                 wprintf("Cache-Control: no-store\n");
442
443                 wprintf("<HTML><HEAD>\n"
444                         "<META HTTP-EQUIV=\"refresh\" CONTENT=\"500363689;\">\n"
445                         "<META HTTP-EQUIV=\"Pragma\" CONTENT=\"no-cache\">\n"
446                         "<META HTTP-EQUIV=\"expired\" CONTENT=\"28-May-1971 18:10:00 GMT\">\n"
447                         "<meta name=\"MSSmartTagsPreventParsing\" content=\"TRUE\">\n"
448                         "</HEAD>\n");
449                 do_template("background");
450         }
451         if (display_name != 2) {
452                 /* store ungoto information */
453                 strcpy(WC->ugname, WC->wc_roomname);
454                 WC->uglsn = ls;
455         }
456         /* move to the new room */
457         serv_printf("GOTO %s", gname);
458         serv_gets(buf);
459         if (buf[0] != '2') {
460                 serv_puts("GOTO _BASEROOM_");
461                 serv_gets(buf);
462         }
463         if (buf[0] != '2') {
464                 if (display_name) {
465                         wprintf("<EM>%s</EM><BR>\n", &buf[4]);
466                         wDumpContent(1);
467                 }
468                 return;
469         }
470         extract(WC->wc_roomname, &buf[4], 0);
471         WC->room_flags = extract_int(&buf[4], 4);
472         /* highest_msg_read = extract_int(&buf[4],6);
473            maxmsgnum = extract_int(&buf[4],5);
474            is_mail = (char) extract_int(&buf[4],7); */
475         ls = extract_long(&buf[4], 6);
476
477         if (WC->is_aide)
478                 WC->is_room_aide = WC->is_aide;
479         else
480                 WC->is_room_aide = (char) extract_int(&buf[4], 8);
481
482         remove_march(WC->wc_roomname);
483         if (!strcasecmp(gname, "_BASEROOM_"))
484                 remove_march(gname);
485
486         /* Display the room banner */
487         if (display_name) {
488                 embed_room_banner(buf);
489                 wDumpContent(1);
490         }
491         strcpy(WC->wc_roomname, WC->wc_roomname);
492         WC->wc_view = extract_int(&buf[4], 11);
493         WC->wc_default_view = extract_int(&buf[4], 12);
494 }
495
496
497 /*
498  * Locate the room on the march list which we most want to go to.  Each room
499  * is measured given a "weight" of preference based on various factors.
500  */
501 char *pop_march(int desired_floor)
502 {
503         static char TheRoom[128];
504         int TheFloor = 0;
505         int TheOrder = 32767;
506         int TheWeight = 0;
507         int weight;
508         struct march *mptr = NULL;
509
510         strcpy(TheRoom, "_BASEROOM_");
511         if (WC->march == NULL)
512                 return (TheRoom);
513
514         for (mptr = WC->march; mptr != NULL; mptr = mptr->next) {
515                 weight = 0;
516                 if ((strcasecmp(mptr->march_name, "_BASEROOM_")))
517                         weight = weight + 10000;
518                 if (mptr->march_floor == desired_floor)
519                         weight = weight + 5000;
520
521                 weight = weight + ((128 - (mptr->march_floor)) * 128);
522                 weight = weight + (128 - (mptr->march_order));
523
524                 if (weight > TheWeight) {
525                         TheWeight = weight;
526                         strcpy(TheRoom, mptr->march_name);
527                         TheFloor = mptr->march_floor;
528                         TheOrder = mptr->march_order;
529                 }
530         }
531         return (TheRoom);
532 }
533
534
535
536 /* Goto next room having unread messages.
537  * We want to skip over rooms that the user has already been to, and take the
538  * user back to the lobby when done.  The room we end up in is placed in
539  * newroom - which is set to 0 (the lobby) initially.
540  * We start the search in the current room rather than the beginning to prevent
541  * two or more concurrent users from dragging each other back to the same room.
542  */
543 void gotonext(void)
544 {
545         char buf[SIZ];
546         struct march *mptr, *mptr2;
547         char next_room[128];
548
549         /* First check to see if the march-mode list is already allocated.
550          * If it is, pop the first room off the list and go there.
551          */
552
553         if (WC->march == NULL) {
554                 serv_puts("LKRN");
555                 serv_gets(buf);
556                 if (buf[0] == '1')
557                         while (serv_gets(buf), strcmp(buf, "000")) {
558                                 mptr = (struct march *) malloc(sizeof(struct march));
559                                 mptr->next = NULL;
560                                 extract(mptr->march_name, buf, 0);
561                                 mptr->march_floor = extract_int(buf, 2);
562                                 mptr->march_order = extract_int(buf, 3);
563                                 if (WC->march == NULL) {
564                                         WC->march = mptr;
565                                 } else {
566                                         mptr2 = WC->march;
567                                         while (mptr2->next != NULL)
568                                                 mptr2 = mptr2->next;
569                                         mptr2->next = mptr;
570                                 }
571                         }
572 /* add _BASEROOM_ to the end of the march list, so the user will end up
573  * in the system base room (usually the Lobby>) at the end of the loop
574  */
575                 mptr = (struct march *) malloc(sizeof(struct march));
576                 mptr->next = NULL;
577                 strcpy(mptr->march_name, "_BASEROOM_");
578                 if (WC->march == NULL) {
579                         WC->march = mptr;
580                 } else {
581                         mptr2 = WC->march;
582                         while (mptr2->next != NULL)
583                                 mptr2 = mptr2->next;
584                         mptr2->next = mptr;
585                 }
586 /*
587  * ...and remove the room we're currently in, so a <G>oto doesn't make us
588  * walk around in circles
589  */
590                 remove_march(WC->wc_roomname);
591         }
592         if (WC->march != NULL) {
593                 strcpy(next_room, pop_march(-1));
594         } else {
595                 strcpy(next_room, "_BASEROOM_");
596         }
597
598
599         smart_goto(next_room);
600 }
601
602
603 void smart_goto(char *next_room) {
604         gotoroom(next_room, 0);
605         readloop("readnew");
606 }
607
608
609
610 /*
611  * mark all messages in current room as having been read
612  */
613 void slrp_highest(void)
614 {
615         char buf[SIZ];
616
617         /* set pointer */
618         serv_puts("SLRP HIGHEST");
619         serv_gets(buf);
620         if (buf[0] != '2') {
621                 wprintf("<EM>%s</EM><BR>\n", &buf[4]);
622                 return;
623         }
624 }
625
626
627 /*
628  * un-goto the previous room
629  */
630 void ungoto(void)
631 {
632         char buf[SIZ];
633
634         if (!strcmp(WC->ugname, "")) {
635                 smart_goto(WC->wc_roomname);
636                 return;
637         }
638         serv_printf("GOTO %s", WC->ugname);
639         serv_gets(buf);
640         if (buf[0] != '2') {
641                 smart_goto(WC->wc_roomname);
642                 return;
643         }
644         if (WC->uglsn >= 0L) {
645                 serv_printf("SLRP %ld", WC->uglsn);
646                 serv_gets(buf);
647         }
648         strcpy(buf, WC->ugname);
649         strcpy(WC->ugname, "");
650         smart_goto(buf);
651 }
652
653
654
655
656
657 /*
658  * Set/clear/read the "self-service list subscribe" flag for a room
659  * 
660  * Set 'newval' to 0 to clear, 1 to set, any other value to leave unchanged.
661  * Always returns the new value.
662  */
663
664 int self_service(int newval) {
665         int current_value = 0;
666         char buf[SIZ];
667         
668         char name[SIZ];
669         char password[SIZ];
670         char dirname[SIZ];
671         int flags, floor, order, view, flags2;
672
673         serv_puts("GETR");
674         serv_gets(buf);
675         if (buf[0] != '2') return(0);
676
677         extract(name, &buf[4], 0);
678         extract(password, &buf[4], 1);
679         extract(dirname, &buf[4], 2);
680         flags = extract_int(&buf[4], 3);
681         floor = extract_int(&buf[4], 4);
682         order = extract_int(&buf[4], 5);
683         view = extract_int(&buf[4], 6);
684         flags2 = extract_int(&buf[4], 7);
685
686         if (flags2 & QR2_SELFLIST) {
687                 current_value = 1;
688         }
689         else {
690                 current_value = 0;
691         }
692
693         if (newval == 1) {
694                 flags2 = flags2 | QR2_SELFLIST;
695         }
696         else if (newval == 0) {
697                 flags2 = flags2 & ~QR2_SELFLIST;
698         }
699         else {
700                 return(current_value);
701         }
702
703         if (newval != current_value) {
704                 serv_printf("SETR %s|%s|%s|%d|0|%d|%d|%d|%d",
705                         name, password, dirname, flags,
706                         floor, order, view, flags2);
707                 serv_gets(buf);
708         }
709
710         return(newval);
711
712 }
713
714
715
716
717
718
719 /*
720  * display the form for editing a room
721  */
722 void display_editroom(void)
723 {
724         char buf[SIZ];
725         char cmd[SIZ];
726         char node[SIZ];
727         char recp[SIZ];
728         char er_name[20];
729         char er_password[10];
730         char er_dirname[15];
731         char er_roomaide[26];
732         unsigned er_flags;
733         int er_floor;
734         int i, j;
735         char *tab;
736         char *shared_with;
737         char *not_shared_with;
738
739         tab = bstr("tab");
740         if (strlen(tab) == 0) tab = "admin";
741
742         serv_puts("GETR");
743         serv_gets(buf);
744
745         if (buf[0] != '2') {
746                 strcpy(WC->ImportantMessage, &buf[4]);
747                 display_main_menu();
748                 return;
749         }
750         extract(er_name, &buf[4], 0);
751         extract(er_password, &buf[4], 1);
752         extract(er_dirname, &buf[4], 2);
753         er_flags = extract_int(&buf[4], 3);
754         er_floor = extract_int(&buf[4], 4);
755
756         output_headers(1);
757
758         /* print the tabbed dialog */
759         wprintf("<TABLE border=0 cellspacing=0 cellpadding=0 width=100%%>"
760                 "<TR ALIGN=CENTER>"
761                 "<TD>&nbsp;</TD>\n");
762
763         if (!strcmp(tab, "admin")) {
764                 wprintf("<TD BGCOLOR=\"#FFFFFF\"><SPAN CLASS=\"tablabel\">");
765         }
766         else {
767                 wprintf("<TD BGCOLOR=\"#AAAAAA\"><A HREF=\"/display_editroom&tab=admin\">");
768         }
769         wprintf("Room administration");
770         if (!strcmp(tab, "admin")) {
771                 wprintf("</SPAN></TD>\n");
772         }
773         else {
774                 wprintf("</A></TD>\n");
775         }
776
777         wprintf("<TD>&nbsp;</TD>\n");
778
779         if (!strcmp(tab, "config")) {
780                 wprintf("<TD BGCOLOR=\"#FFFFFF\"><SPAN CLASS=\"tablabel\">");
781         }
782         else {
783                 wprintf("<TD BGCOLOR=\"#AAAAAA\"><A HREF=\"/display_editroom&tab=config\">");
784         }
785         wprintf("Room configuration");
786         if (!strcmp(tab, "config")) {
787                 wprintf("</SPAN></TD>\n");
788         }
789         else {
790                 wprintf("</A></TD>\n");
791         }
792
793         wprintf("<TD>&nbsp;</TD>\n");
794
795         if (!strcmp(tab, "sharing")) {
796                 wprintf("<TD BGCOLOR=\"#FFFFFF\"><SPAN CLASS=\"tablabel\">");
797         }
798         else {
799                 wprintf("<TD BGCOLOR=\"#AAAAAA\"><A HREF=\"/display_editroom&tab=sharing\">");
800         }
801         wprintf("Sharing");
802         if (!strcmp(tab, "sharing")) {
803                 wprintf("</SPAN></TD>\n");
804         }
805         else {
806                 wprintf("</A></TD>\n");
807         }
808
809         wprintf("<TD>&nbsp;</TD>\n");
810
811         if (!strcmp(tab, "listserv")) {
812                 wprintf("<TD BGCOLOR=\"#FFFFFF\"><SPAN CLASS=\"tablabel\">");
813         }
814         else {
815                 wprintf("<TD BGCOLOR=\"#AAAAAA\"><A HREF=\"/display_editroom&tab=listserv\">");
816         }
817         wprintf("Mailing list service");
818         if (!strcmp(tab, "listserv")) {
819                 wprintf("</SPAN></TD>\n");
820         }
821         else {
822                 wprintf("</A></TD>\n");
823         }
824
825         wprintf("</TR></TABLE>\n");
826         /* end tabbed dialog */ 
827
828         /* begin content of whatever tab is open now */
829         wprintf("<TABLE border=0 width=100%% bgcolor=\"#FFFFFF\">\n"
830                 "<TR><TD>\n");
831
832         if (!strcmp(tab, "admin")) {
833                 wprintf("<UL>"
834                         "<LI><A HREF=\"/confirm_delete_room\">\n"
835                         "Delete this room</A>\n"
836                         "<LI><A HREF=\"/display_editroompic\">\n"
837                         "Set or change the graphic for this room's banner</A>\n"
838                         "<LI><A HREF=\"/display_editinfo\">\n"
839                         "Edit this room's Info file</A>\n"
840                         "</UL>");
841         }
842
843         if (!strcmp(tab, "config")) {
844                 wprintf("<FORM METHOD=\"POST\" ACTION=\"/editroom\">\n");
845         
846                 wprintf("<UL><LI>Name of room: ");
847                 wprintf("<INPUT TYPE=\"text\" NAME=\"er_name\" VALUE=\"%s\" MAXLENGTH=\"19\">\n", er_name);
848         
849                 wprintf("<LI>Resides on floor: ");
850                 load_floorlist();
851                 wprintf("<SELECT NAME=\"er_floor\" SIZE=\"1\">\n");
852                 for (i = 0; i < 128; ++i)
853                         if (strlen(floorlist[i]) > 0) {
854                                 wprintf("<OPTION ");
855                                 if (i == er_floor)
856                                         wprintf("SELECTED ");
857                                 wprintf("VALUE=\"%d\">", i);
858                                 escputs(floorlist[i]);
859                                 wprintf("</OPTION>\n");
860                         }
861                 wprintf("</SELECT>\n");
862         
863                 wprintf("<LI>Type of room:<UL>\n");
864
865                 wprintf("<LI><INPUT TYPE=\"radio\" NAME=\"type\" VALUE=\"public\" ");
866                 if ((er_flags & QR_PRIVATE) == 0)
867                 wprintf("CHECKED ");
868                 wprintf("> Public room\n");
869
870                 wprintf("<LI><INPUT TYPE=\"radio\" NAME=\"type\" VALUE=\"guessname\" ");
871                 if ((er_flags & QR_PRIVATE) &&
872                     (er_flags & QR_GUESSNAME))
873                         wprintf("CHECKED ");
874                 wprintf("> Private - guess name\n");
875         
876                 wprintf("<LI><INPUT TYPE=\"radio\" NAME=\"type\" VALUE=\"passworded\" ");
877                 if ((er_flags & QR_PRIVATE) &&
878                     (er_flags & QR_PASSWORDED))
879                         wprintf("CHECKED ");
880                 wprintf("> Private - require password:\n");
881                 wprintf("<INPUT TYPE=\"text\" NAME=\"er_password\" VALUE=\"%s\" MAXLENGTH=\"9\">\n", er_password);
882         
883                 wprintf("<LI><INPUT TYPE=\"radio\" NAME=\"type\" VALUE=\"invonly\" ");
884                 if ((er_flags & QR_PRIVATE)
885                     && ((er_flags & QR_GUESSNAME) == 0)
886                     && ((er_flags & QR_PASSWORDED) == 0))
887                         wprintf("CHECKED ");
888                 wprintf("> Private - invitation only\n");
889         
890                 wprintf("<LI><INPUT TYPE=\"checkbox\" NAME=\"bump\" VALUE=\"yes\" ");
891                 wprintf("> If private, cause current users to forget room\n");
892         
893                 wprintf("</UL>\n");
894         
895                 wprintf("<LI><INPUT TYPE=\"checkbox\" NAME=\"prefonly\" VALUE=\"yes\" ");
896                 if (er_flags & QR_PREFONLY)
897                         wprintf("CHECKED ");
898                 wprintf("> Preferred users only\n");
899         
900                 wprintf("<LI><INPUT TYPE=\"checkbox\" NAME=\"readonly\" VALUE=\"yes\" ");
901                 if (er_flags & QR_READONLY)
902                         wprintf("CHECKED ");
903                 wprintf("> Read-only room\n");
904         
905         /* directory stuff */
906                 wprintf("<LI><INPUT TYPE=\"checkbox\" NAME=\"directory\" VALUE=\"yes\" ");
907                 if (er_flags & QR_DIRECTORY)
908                         wprintf("CHECKED ");
909                 wprintf("> File directory room\n");
910
911                 wprintf("<UL><LI>Directory name: ");
912                 wprintf("<INPUT TYPE=\"text\" NAME=\"er_dirname\" VALUE=\"%s\" MAXLENGTH=\"14\">\n", er_dirname);
913
914                 wprintf("<LI><INPUT TYPE=\"checkbox\" NAME=\"ulallowed\" VALUE=\"yes\" ");
915                 if (er_flags & QR_UPLOAD)
916                         wprintf("CHECKED ");
917                 wprintf("> Uploading allowed\n");
918         
919                 wprintf("<LI><INPUT TYPE=\"checkbox\" NAME=\"dlallowed\" VALUE=\"yes\" ");
920                 if (er_flags & QR_DOWNLOAD)
921                         wprintf("CHECKED ");
922                 wprintf("> Downloading allowed\n");
923         
924                 wprintf("<LI><INPUT TYPE=\"checkbox\" NAME=\"visdir\" VALUE=\"yes\" ");
925                 if (er_flags & QR_VISDIR)
926                         wprintf("CHECKED ");
927                 wprintf("> Visible directory</UL>\n");
928         
929         /* end of directory stuff */
930         
931                 wprintf("<LI><INPUT TYPE=\"checkbox\" NAME=\"network\" VALUE=\"yes\" ");
932                 if (er_flags & QR_NETWORK)
933                         wprintf("CHECKED ");
934                 wprintf("> Network shared room\n");
935
936                 wprintf("<LI><INPUT TYPE=\"checkbox\" NAME=\"permanent\" VALUE=\"yes\" ");
937                 if (er_flags & QR_PERMANENT)
938                         wprintf("CHECKED ");
939                 wprintf("> Permanent (does not auto-purge)\n");
940
941         /* start of anon options */
942         
943                 wprintf("<LI>Anonymous messages<UL>\n");
944         
945                 wprintf("<LI><INPUT TYPE=\"radio\" NAME=\"anon\" VALUE=\"no\" ");
946                 if (((er_flags & QR_ANONONLY) == 0)
947                     && ((er_flags & QR_ANONOPT) == 0))
948                         wprintf("CHECKED ");
949                 wprintf("> No anonymous messages\n");
950         
951                 wprintf("<LI><INPUT TYPE=\"radio\" NAME=\"anon\" VALUE=\"anononly\" ");
952                 if (er_flags & QR_ANONONLY)
953                         wprintf("CHECKED ");
954                 wprintf("> All messages are anonymous\n");
955         
956                 wprintf("<LI><INPUT TYPE=\"radio\" NAME=\"anon\" VALUE=\"anon2\" ");
957                 if (er_flags & QR_ANONOPT)
958                         wprintf("CHECKED ");
959                 wprintf("> Prompt user when entering messages</UL>\n");
960         
961         /* end of anon options */
962         
963                 wprintf("<LI>Room aide: \n");
964                 serv_puts("GETA");
965                 serv_gets(buf);
966                 if (buf[0] != '2') {
967                         wprintf("<EM>%s</EM>\n", &buf[4]);
968                 } else {
969                         extract(er_roomaide, &buf[4], 0);
970                         wprintf("<INPUT TYPE=\"text\" NAME=\"er_roomaide\" VALUE=\"%s\" MAXLENGTH=\"25\">\n", er_roomaide);
971                 }
972         
973                 wprintf("</UL><CENTER>\n");
974                 wprintf("<INPUT TYPE=\"submit\" NAME=\"sc\" VALUE=\"OK\">");
975                 wprintf("<INPUT TYPE=\"submit\" NAME=\"sc\" VALUE=\"Cancel\">");
976                 wprintf("</CENTER>\n");
977         }
978
979
980         /* Sharing the room with other Citadel nodes... */
981         if (!strcmp(tab, "sharing")) {
982
983                 shared_with = strdup("");
984                 not_shared_with = strdup("");
985
986                 /* Learn the current configuration */
987                 serv_puts("CONF getsys|application/x-citadel-ignet-config");
988                 serv_gets(buf);
989                 if (buf[0]=='1') while (serv_gets(buf), strcmp(buf, "000")) {
990                         extract(node, buf, 0);
991                         not_shared_with = realloc(not_shared_with,
992                                         strlen(not_shared_with) + 32);
993                         strcat(not_shared_with, node);
994                         strcat(not_shared_with, "|");
995                 }
996
997                 serv_puts("GNET");
998                 serv_gets(buf);
999                 if (buf[0]=='1') while (serv_gets(buf), strcmp(buf, "000")) {
1000                         extract(cmd, buf, 0);
1001                         extract(node, buf, 1);
1002                         if (!strcasecmp(cmd, "ignet_push_share")) {
1003                                 shared_with = realloc(shared_with,
1004                                                 strlen(shared_with) + 32);
1005                                 strcat(shared_with, node);
1006                                 strcat(shared_with, "|");
1007                         }
1008                 }
1009
1010                 for (i=0; i<num_tokens(shared_with, '|'); ++i) {
1011                         extract(node, shared_with, i);
1012                         for (j=0; j<num_tokens(not_shared_with, '|'); ++j) {
1013                                 extract(cmd, not_shared_with, j);
1014                                 if (!strcasecmp(node, cmd)) {
1015                                         remove_token(not_shared_with, j, '|');
1016                                 }
1017                         }
1018                 }
1019
1020                 /* Display the stuff */
1021                 wprintf("<CENTER><BR>"
1022                         "<TABLE border=1 cellpadding=5><TR>"
1023                         "<TD><B><I>Shared with</I></B></TD>"
1024                         "<TD><B><I>Not shared with</I></B></TD></TR>\n"
1025                         "<TR><TD>\n");
1026
1027                 for (i=0; i<num_tokens(shared_with, '|'); ++i) {
1028                         extract(node, shared_with, i);
1029                         if (strlen(node) > 0) {
1030                                 wprintf("%s ", node);
1031                                 wprintf("<A HREF=\"/netedit&cmd=remove&line="
1032                                         "ignet_push_share|");
1033                                 urlescputs(node);
1034                                 wprintf("&tab=sharing\">(unshare)</A><BR>");
1035                         }
1036                 }
1037
1038                 wprintf("</TD><TD>\n");
1039
1040                 for (i=0; i<num_tokens(not_shared_with, '|'); ++i) {
1041                         extract(node, not_shared_with, i);
1042                         if (strlen(node) > 0) {
1043                                 wprintf("%s ", node);
1044                                 wprintf("<A HREF=\"/netedit&cmd=add&line="
1045                                         "ignet_push_share|");
1046                                 urlescputs(node);
1047                                 wprintf("&tab=sharing\">(share)</A><BR>");
1048                         }
1049                 }
1050
1051                 wprintf("</TD></TR>"
1052                         "</TABLE><BR>\n"
1053                         "<I><B>Reminder:</B> When sharing a room, "
1054                         "it must be shared from both ends.  Adding a node to "
1055                         "the 'shared' list sends messages out, but in order to"
1056                         " receive messages, the other nodes must be configured"
1057                         " to send messages out to your system as well.</I><BR>"
1058                         "</CENTER>\n");
1059
1060         }
1061
1062         /* Mailing list management */
1063         if (!strcmp(tab, "listserv")) {
1064
1065                 wprintf("<BR><center>"
1066                         "<TABLE BORDER=0 WIDTH=100%% CELLPADDING=5>"
1067                         "<TR><TD VALIGN=TOP>");
1068
1069                 wprintf("<i>The contents of this room are being "
1070                         "mailed <b>as individual messages</b> "
1071                         "to the following list recipients:"
1072                         "</i><br><br>\n");
1073
1074                 serv_puts("GNET");
1075                 serv_gets(buf);
1076                 if (buf[0]=='1') while (serv_gets(buf), strcmp(buf, "000")) {
1077                         extract(cmd, buf, 0);
1078                         if (!strcasecmp(cmd, "listrecp")) {
1079                                 extract(recp, buf, 1);
1080                         
1081                                 escputs(recp);
1082                                 wprintf(" <A HREF=\"/netedit&cmd=remove&line="
1083                                         "listrecp|");
1084                                 urlescputs(recp);
1085                                 wprintf("&tab=listserv\">(remove)</A><BR>");
1086
1087                         }
1088                 }
1089                 wprintf("<BR><FORM METHOD=\"POST\" ACTION=\"/netedit\">\n"
1090                         "<INPUT TYPE=\"hidden\" NAME=\"tab\" VALUE=\"listserv\">\n"
1091                         "<INPUT TYPE=\"hidden\" NAME=\"prefix\" VALUE=\"listrecp|\">\n");
1092                 wprintf("<INPUT TYPE=\"text\" NAME=\"line\">\n");
1093                 wprintf("<INPUT TYPE=\"submit\" NAME=\"cmd\" VALUE=\"Add\">");
1094                 wprintf("</FORM>\n");
1095
1096                 wprintf("</TD><TD VALIGN=TOP>\n");
1097                 
1098                 wprintf("<i>The contents of this room are being "
1099                         "mailed <b>in digest form</b> "
1100                         "to the following list recipients:"
1101                         "</i><br><br>\n");
1102
1103                 serv_puts("GNET");
1104                 serv_gets(buf);
1105                 if (buf[0]=='1') while (serv_gets(buf), strcmp(buf, "000")) {
1106                         extract(cmd, buf, 0);
1107                         if (!strcasecmp(cmd, "digestrecp")) {
1108                                 extract(recp, buf, 1);
1109                         
1110                                 escputs(recp);
1111                                 wprintf(" <A HREF=\"/netedit&cmd=remove&line="
1112                                         "digestrecp|");
1113                                 urlescputs(recp);
1114                                 wprintf("&tab=listserv\">(remove)</A><BR>");
1115
1116                         }
1117                 }
1118                 wprintf("<BR><FORM METHOD=\"POST\" ACTION=\"/netedit\">\n"
1119                         "<INPUT TYPE=\"hidden\" NAME=\"tab\" VALUE=\"listserv\">\n"
1120                         "<INPUT TYPE=\"hidden\" NAME=\"prefix\" VALUE=\"digestrecp|\">\n");
1121                 wprintf("<INPUT TYPE=\"text\" NAME=\"line\">\n");
1122                 wprintf("<INPUT TYPE=\"submit\" NAME=\"cmd\" VALUE=\"Add\">");
1123                 wprintf("</FORM>\n");
1124                 
1125                 wprintf("</TD></TR></TABLE><HR>\n");
1126
1127                 if (self_service(999) == 1) {
1128                         wprintf("This room is configured to allow "
1129                                 "self-service subscribe/unsubscribe requests."
1130                                 " <A HREF=\"/toggle_self_service?newval=0&"
1131                                 "tab=listserv\">"
1132                                 "Click to disable.</A><BR>\n"
1133                                 "The URL for subscribe/unsubscribe is: "
1134                                 "<TT>http://%s/listsub</TT><BR>\n",
1135                                 WC->http_host
1136                         );
1137                 }
1138                 else {
1139                         wprintf("This room is <i>not</i> configured to allow "
1140                                 "self-service subscribe/unsubscribe requests."
1141                                 " <A HREF=\"/toggle_self_service?newval=1&"
1142                                 "tab=listserv\">"
1143                                 "Click to enable.</A><BR>\n"
1144                         );
1145                 }
1146
1147
1148                 wprintf("</CENTER>\n");
1149         }
1150
1151         /* end content of whatever tab is open now */
1152         wprintf("</TD></TR></TABLE>\n");
1153
1154         wDumpContent(1);
1155 }
1156
1157
1158 /* 
1159  * Toggle self-service list subscription
1160  */
1161 void toggle_self_service(void) {
1162         int newval = 0;
1163
1164         newval = atoi(bstr("newval"));
1165         self_service(newval);
1166         display_editroom();
1167 }
1168
1169
1170
1171 /*
1172  * save new parameters for a room
1173  */
1174 void editroom(void)
1175 {
1176         char buf[SIZ];
1177         char er_name[20];
1178         char er_password[10];
1179         char er_dirname[15];
1180         char er_roomaide[26];
1181         int er_floor;
1182         unsigned er_flags;
1183         int bump;
1184
1185
1186         if (strcmp(bstr("sc"), "OK")) {
1187                 strcpy(WC->ImportantMessage,
1188                         "Cancelled.  Changes were not saved.");
1189                 display_main_menu();
1190                 return;
1191         }
1192         serv_puts("GETR");
1193         serv_gets(buf);
1194
1195         if (buf[0] != '2') {
1196                 strcpy(WC->ImportantMessage, &buf[4]);
1197                 display_main_menu();
1198                 return;
1199         }
1200         extract(er_name, &buf[4], 0);
1201         extract(er_password, &buf[4], 1);
1202         extract(er_dirname, &buf[4], 2);
1203         er_flags = extract_int(&buf[4], 3);
1204
1205         strcpy(er_roomaide, bstr("er_roomaide"));
1206         if (strlen(er_roomaide) == 0) {
1207                 serv_puts("GETA");
1208                 serv_gets(buf);
1209                 if (buf[0] != '2') {
1210                         strcpy(er_roomaide, "");
1211                 } else {
1212                         extract(er_roomaide, &buf[4], 0);
1213                 }
1214         }
1215         strcpy(buf, bstr("er_name"));
1216         buf[20] = 0;
1217         if (strlen(buf) > 0)
1218                 strcpy(er_name, buf);
1219
1220         strcpy(buf, bstr("er_password"));
1221         buf[10] = 0;
1222         if (strlen(buf) > 0)
1223                 strcpy(er_password, buf);
1224
1225         strcpy(buf, bstr("er_dirname"));
1226         buf[15] = 0;
1227         if (strlen(buf) > 0)
1228                 strcpy(er_dirname, buf);
1229
1230         strcpy(buf, bstr("type"));
1231         er_flags &= !(QR_PRIVATE | QR_PASSWORDED | QR_GUESSNAME);
1232
1233         if (!strcmp(buf, "invonly")) {
1234                 er_flags |= (QR_PRIVATE);
1235         }
1236         if (!strcmp(buf, "guessname")) {
1237                 er_flags |= (QR_PRIVATE | QR_GUESSNAME);
1238         }
1239         if (!strcmp(buf, "passworded")) {
1240                 er_flags |= (QR_PRIVATE | QR_PASSWORDED);
1241         }
1242         if (!strcmp(bstr("prefonly"), "yes")) {
1243                 er_flags |= QR_PREFONLY;
1244         } else {
1245                 er_flags &= ~QR_PREFONLY;
1246         }
1247
1248         if (!strcmp(bstr("readonly"), "yes")) {
1249                 er_flags |= QR_READONLY;
1250         } else {
1251                 er_flags &= ~QR_READONLY;
1252         }
1253
1254         if (!strcmp(bstr("permanent"), "yes")) {
1255                 er_flags |= QR_PERMANENT;
1256         } else {
1257                 er_flags &= ~QR_PERMANENT;
1258         }
1259
1260         if (!strcmp(bstr("network"), "yes")) {
1261                 er_flags |= QR_NETWORK;
1262         } else {
1263                 er_flags &= ~QR_NETWORK;
1264         }
1265
1266         if (!strcmp(bstr("directory"), "yes")) {
1267                 er_flags |= QR_DIRECTORY;
1268         } else {
1269                 er_flags &= ~QR_DIRECTORY;
1270         }
1271
1272         if (!strcmp(bstr("ulallowed"), "yes")) {
1273                 er_flags |= QR_UPLOAD;
1274         } else {
1275                 er_flags &= ~QR_UPLOAD;
1276         }
1277
1278         if (!strcmp(bstr("dlallowed"), "yes")) {
1279                 er_flags |= QR_DOWNLOAD;
1280         } else {
1281                 er_flags &= ~QR_DOWNLOAD;
1282         }
1283
1284         if (!strcmp(bstr("visdir"), "yes")) {
1285                 er_flags |= QR_VISDIR;
1286         } else {
1287                 er_flags &= ~QR_VISDIR;
1288         }
1289
1290         strcpy(buf, bstr("anon"));
1291
1292         er_flags &= ~(QR_ANONONLY | QR_ANONOPT);
1293         if (!strcmp(buf, "anononly"))
1294                 er_flags |= QR_ANONONLY;
1295         if (!strcmp(buf, "anon2"))
1296                 er_flags |= QR_ANONOPT;
1297
1298         bump = 0;
1299         if (!strcmp(bstr("bump"), "yes"))
1300                 bump = 1;
1301
1302         er_floor = atoi(bstr("er_floor"));
1303
1304         sprintf(buf, "SETR %s|%s|%s|%u|%d|%d",
1305              er_name, er_password, er_dirname, er_flags, bump, er_floor);
1306         serv_puts(buf);
1307         serv_gets(buf);
1308         if (buf[0] != '2') {
1309                 strcpy(WC->ImportantMessage, &buf[4]);
1310                 display_main_menu();
1311                 return;
1312         }
1313         gotoroom(er_name, 0);
1314
1315         if (strlen(er_roomaide) > 0) {
1316                 sprintf(buf, "SETA %s", er_roomaide);
1317                 serv_puts(buf);
1318                 serv_gets(buf);
1319                 if (buf[0] != '2') {
1320                         strcpy(WC->ImportantMessage, &buf[4]);
1321                         display_main_menu();
1322                         return;
1323                 }
1324         }
1325         smart_goto(er_name);
1326 }
1327
1328 /*
1329  * Invite, Kick, and show Who Knows a room
1330  */
1331 void display_whok(void)
1332 {
1333         char buf[SIZ], room[SIZ], username[SIZ];
1334
1335         serv_puts("GETR");
1336         serv_gets(buf);
1337
1338         if (buf[0] != '2') {
1339                 strcpy(WC->ImportantMessage, &buf[4]);
1340                 display_main_menu();
1341                 return;
1342         }
1343         extract(room, &buf[4], 0);
1344
1345         strcpy(username, bstr("username"));
1346
1347         if(!strcmp(bstr("sc"), "Kick")) {
1348                 sprintf(buf, "KICK %s", username);
1349                 serv_puts(buf);
1350                 serv_gets(buf);
1351
1352                 if (buf[0] != '2') {
1353                         strcpy(WC->ImportantMessage, &buf[4]);
1354                 } else {
1355                         sprintf(WC->ImportantMessage,
1356                                 "<B><I>User %s kicked out of room %s.</I></B>\n", 
1357                                 username, room);
1358                 }
1359         } else if(!strcmp(bstr("sc"), "Invite")) {
1360                 sprintf(buf, "INVT %s", username);
1361                 serv_puts(buf);
1362                 serv_gets(buf);
1363
1364                 if (buf[0] != '2') {
1365                         strcpy(WC->ImportantMessage, &buf[4]);
1366                 } else {
1367                         sprintf(WC->ImportantMessage,
1368                                 "<B><I>User %s invited to room %s.</I></B>\n", 
1369                                 username, room);
1370                 }
1371         }
1372         
1373         output_headers(1);
1374         stresc(buf, WC->wc_roomname, 1, 1);
1375         svprintf("BOXTITLE", WCS_STRING, "Access control list for %s", buf);
1376         do_template("beginbox");
1377
1378         wprintf("<TABLE border=0 CELLSPACING=10><TR VALIGN=TOP>"
1379                 "<TD>The users listed below have access to this room.  "
1380                 "To remove a user from the access list, select the user "
1381                 "name from the list and click 'Kick'.<BR><BR>");
1382         
1383         wprintf("<CENTER><FORM METHOD=\"POST\" ACTION=\"/display_whok\">\n");
1384         wprintf("<SELECT NAME=\"username\" SIZE=10>\n");
1385         serv_puts("WHOK");
1386         serv_gets(buf);
1387         if (buf[0] == '1') {
1388                 while (serv_gets(buf), strcmp(buf, "000")) {
1389                         extract(username, buf, 0);
1390                         wprintf("<OPTION>");
1391                         escputs(username);
1392                         wprintf("\n");
1393                 }
1394         }
1395         wprintf("</SELECT><BR>\n");
1396
1397         wprintf("<input type=submit name=sc value=\"Kick\">");
1398         wprintf("</FORM></CENTER>\n");
1399
1400         wprintf("</TD><TD>"
1401                 "To grant another user access to this room, enter the "
1402                 "user name in the box below and click 'Invite'.<BR><BR>");
1403
1404         wprintf("<CENTER><FORM METHOD=\"POST\" ACTION=\"/display_whok\">\n");
1405         wprintf("Invite: ");
1406         wprintf("<input type=text name=username><BR>\n"
1407                 "<input type=hidden name=sc value=\"Invite\">"
1408                 "<input type=submit value=\"Invite\">"
1409                 "</FORM></CENTER>\n");
1410
1411         wprintf("</TD></TR></TABLE>\n");
1412         do_template("endbox");
1413         wDumpContent(1);
1414 }
1415
1416
1417
1418 /*
1419  * display the form for entering a new room
1420  */
1421 void display_entroom(void)
1422 {
1423         int i;
1424         char buf[SIZ];
1425
1426         serv_puts("CRE8 0");
1427         serv_gets(buf);
1428
1429         if (buf[0] != '2') {
1430                 strcpy(WC->ImportantMessage, &buf[4]);
1431                 display_main_menu();
1432                 return;
1433         }
1434         output_headers(3);
1435         svprintf("BOXTITLE", WCS_STRING, "Create a new room");
1436         do_template("beginbox");
1437
1438         wprintf("<FORM METHOD=\"POST\" ACTION=\"/entroom\">\n");
1439
1440         wprintf("<UL><LI>Name of room: ");
1441         wprintf("<INPUT TYPE=\"text\" NAME=\"er_name\" MAXLENGTH=\"127\">\n");
1442
1443         wprintf("<LI>Resides on floor: ");
1444         load_floorlist(); 
1445         wprintf("<SELECT NAME=\"er_floor\" SIZE=\"1\">\n");
1446         for (i = 0; i < 128; ++i)
1447                 if (strlen(floorlist[i]) > 0) {
1448                         wprintf("<OPTION ");
1449                         wprintf("VALUE=\"%d\">", i);
1450                         escputs(floorlist[i]);
1451                         wprintf("</OPTION>\n");
1452                 }
1453         wprintf("</SELECT>\n");
1454
1455         wprintf("<LI>Type of room:<UL>\n");
1456
1457         wprintf("<LI><INPUT TYPE=\"radio\" NAME=\"type\" VALUE=\"public\" ");
1458         wprintf("CHECKED > Public room\n");
1459
1460         wprintf("<LI><INPUT TYPE=\"radio\" NAME=\"type\" VALUE=\"guessname\" ");
1461         wprintf("> Private - guess name\n");
1462
1463         wprintf("<LI><INPUT TYPE=\"radio\" NAME=\"type\" VALUE=\"passworded\" ");
1464         wprintf("> Private - require password:\n");
1465         wprintf("<INPUT TYPE=\"text\" NAME=\"er_password\" MAXLENGTH=\"9\">\n");
1466
1467         wprintf("<LI><INPUT TYPE=\"radio\" NAME=\"type\" VALUE=\"invonly\" ");
1468         wprintf("> Private - invitation only\n");
1469
1470         wprintf("<LI><INPUT TYPE=\"radio\" NAME=\"type\" VALUE=\"personal\" ");
1471         wprintf("> Personal (mailbox for you only)\n");
1472         wprintf("</UL>\n");
1473
1474         wprintf("<LI>Default view for room: "); /* FOO */
1475         wprintf("<SELECT NAME=\"er_view\" SIZE=\"1\">\n");
1476         for (i=0; i<(sizeof viewdefs / sizeof (char *)); ++i) {
1477                 wprintf("<OPTION %s VALUE=\"%d\">",
1478                         ((i == 0) ? "SELECTED" : ""), i );
1479                 escputs(viewdefs[i]);
1480                 wprintf("</OPTION>\n");
1481         }
1482         wprintf("</SELECT>\n");
1483
1484         wprintf("<CENTER>\n");
1485         wprintf("<INPUT TYPE=\"submit\" NAME=\"sc\" VALUE=\"OK\">");
1486         wprintf("<INPUT TYPE=\"submit\" NAME=\"sc\" VALUE=\"Cancel\">");
1487         wprintf("</CENTER>\n");
1488         wprintf("</FORM>\n<HR>");
1489         serv_printf("MESG roomaccess");
1490         serv_gets(buf);
1491         if (buf[0] == '1') {
1492                 fmout(NULL, "CENTER");
1493         }
1494         do_template("endbox");
1495         wDumpContent(1);
1496 }
1497
1498
1499
1500
1501 /*
1502  * support function for entroom() -- sets the default view 
1503  */
1504 void er_set_default_view(int newview) {
1505
1506         char buf[SIZ];
1507
1508         char rm_name[SIZ];
1509         char rm_pass[SIZ];
1510         char rm_dir[SIZ];
1511         int rm_bits1;
1512         int rm_floor;
1513         int rm_listorder;
1514         int rm_bits2;
1515
1516         serv_puts("GETR");
1517         serv_gets(buf);
1518         if (buf[0] != '2') return;
1519
1520         extract(rm_name, &buf[4], 0);
1521         extract(rm_pass, &buf[4], 1);
1522         extract(rm_dir, &buf[4], 2);
1523         rm_bits1 = extract_int(&buf[4], 3);
1524         rm_floor = extract_int(&buf[4], 4);
1525         rm_listorder = extract_int(&buf[4], 5);
1526         rm_bits2 = extract_int(&buf[4], 7);
1527
1528         serv_printf("SETR %s|%s|%s|%d|0|%d|%d|%d|%d",
1529                 rm_name, rm_pass, rm_dir, rm_bits1, rm_floor,
1530                 rm_listorder, newview, rm_bits2
1531         );
1532         serv_gets(buf);
1533 }
1534
1535
1536
1537 /*
1538  * enter a new room
1539  */
1540 void entroom(void)
1541 {
1542         char buf[SIZ];
1543         char er_name[SIZ];
1544         char er_type[SIZ];
1545         char er_password[SIZ];
1546         int er_floor;
1547         int er_num_type;
1548
1549         if (strcmp(bstr("sc"), "OK")) {
1550                 strcpy(WC->ImportantMessage,
1551                         "Cancelled.  No new room was created.");
1552                 display_main_menu();
1553                 return;
1554         }
1555         strcpy(er_name, bstr("er_name"));
1556         strcpy(er_type, bstr("type"));
1557         strcpy(er_password, bstr("er_password"));
1558         er_floor = atoi(bstr("er_floor"));
1559
1560         er_num_type = 0;
1561         if (!strcmp(er_type, "guessname"))
1562                 er_num_type = 1;
1563         if (!strcmp(er_type, "passworded"))
1564                 er_num_type = 2;
1565         if (!strcmp(er_type, "invonly"))
1566                 er_num_type = 3;
1567         if (!strcmp(er_type, "personal"))
1568                 er_num_type = 4;
1569
1570         sprintf(buf, "CRE8 1|%s|%d|%s|%d", 
1571                 er_name, er_num_type, er_password, er_floor);
1572         serv_puts(buf);
1573         serv_gets(buf);
1574         if (buf[0] != '2') {
1575                 strcpy(WC->ImportantMessage, &buf[4]);
1576                 display_main_menu();
1577                 return;
1578         }
1579         gotoroom(er_name, 0);
1580         er_set_default_view(atoi(bstr("er_view")));     /* Set default view */
1581         do_change_view(atoi(bstr("er_view")));          /* Now go there */
1582 }
1583
1584
1585 /*
1586  * display the screen to enter a private room
1587  */
1588 void display_private(char *rname, int req_pass)
1589 {
1590
1591         output_headers(3);
1592
1593         svprintf("BOXTITLE", WCS_STRING, "Go to a hidden room");
1594         do_template("beginbox");
1595
1596         wprintf("<CENTER>\n");
1597         wprintf("<BR>If you know the name of a hidden (guess-name) or\n");
1598         wprintf("passworded room, you can enter that room by typing\n");
1599         wprintf("its name below.  Once you gain access to a private\n");
1600         wprintf("room, it will appear in your regular room listings\n");
1601         wprintf("so you don't have to keep returning here.\n");
1602         wprintf("<BR><BR>");
1603
1604         wprintf("<FORM METHOD=\"GET\" ACTION=\"/goto_private\">\n");
1605
1606         wprintf("<table border=\"0\" cellspacing=\"5\" "
1607                 "cellpadding=\"5\" BGCOLOR=\"#EEEEEE\">\n"
1608                 "<TR><TD>"
1609                 "Enter room name:</TD><TD>"
1610                 "<INPUT TYPE=\"text\" NAME=\"gr_name\" "
1611                 "VALUE=\"%s\" MAXLENGTH=\"19\">\n", rname);
1612
1613         if (req_pass) {
1614                 wprintf("</TD></TR><TR><TD>");
1615                 wprintf("Enter room password:</TD><TD>");
1616                 wprintf("<INPUT TYPE=\"password\" NAME=\"gr_pass\" MAXLENGTH=\"9\">\n");
1617         }
1618         wprintf("</TD></TR></TABLE><BR>\n");
1619
1620         wprintf("<INPUT TYPE=\"submit\" NAME=\"sc\" VALUE=\"OK\">"
1621                 "&nbsp;"
1622                 "<INPUT TYPE=\"submit\" NAME=\"sc\" VALUE=\"Cancel\">");
1623         wprintf("</FORM>\n");
1624         do_template("endbox");
1625         wDumpContent(1);
1626 }
1627
1628 /* 
1629  * goto a private room
1630  */
1631 void goto_private(void)
1632 {
1633         char hold_rm[SIZ];
1634         char buf[SIZ];
1635
1636         if (strcasecmp(bstr("sc"), "OK")) {
1637                 display_main_menu();
1638                 return;
1639         }
1640         strcpy(hold_rm, WC->wc_roomname);
1641         strcpy(buf, "GOTO ");
1642         strcat(buf, bstr("gr_name"));
1643         strcat(buf, "|");
1644         strcat(buf, bstr("gr_pass"));
1645         serv_puts(buf);
1646         serv_gets(buf);
1647
1648         if (buf[0] == '2') {
1649                 smart_goto(bstr("gr_name"));
1650                 return;
1651         }
1652         if (!strncmp(buf, "540", 3)) {
1653                 display_private(bstr("gr_name"), 1);
1654                 return;
1655         }
1656         output_headers(1);
1657         wprintf("%s\n", &buf[4]);
1658         wDumpContent(1);
1659         return;
1660 }
1661
1662
1663 /*
1664  * display the screen to zap a room
1665  */
1666 void display_zap(void)
1667 {
1668         output_headers(1);
1669
1670         wprintf("<TABLE WIDTH=100%% BORDER=0 BGCOLOR=\"#770000\"><TR><TD>");
1671         wprintf("<SPAN CLASS=\"titlebar\">Zap (forget) the current room</SPAN>\n");
1672         wprintf("</TD></TR></TABLE>\n");
1673
1674         wprintf("If you select this option, <em>%s</em> will ", WC->wc_roomname);
1675         wprintf("disappear from your room list.  Is this what you wish ");
1676         wprintf("to do?<BR>\n");
1677
1678         wprintf("<FORM METHOD=\"GET\" ACTION=\"/zap\">\n");
1679         wprintf("<INPUT TYPE=\"submit\" NAME=\"sc\" VALUE=\"OK\">");
1680         wprintf("<INPUT TYPE=\"submit\" NAME=\"sc\" VALUE=\"Cancel\">");
1681         wprintf("</FORM>\n");
1682         wDumpContent(1);
1683 }
1684
1685
1686 /* 
1687  * zap a room
1688  */
1689 void zap(void)
1690 {
1691         char buf[SIZ];
1692         char final_destination[SIZ];
1693
1694         /* If the forget-room routine fails for any reason, we fall back
1695          * to the current room; otherwise, we go to the Lobby
1696          */
1697         strcpy(final_destination, WC->wc_roomname);
1698
1699         if (!strcasecmp(bstr("sc"), "OK")) {
1700                 serv_printf("GOTO %s", WC->wc_roomname);
1701                 serv_gets(buf);
1702                 if (buf[0] != '2') {
1703                         /* ExpressMessageCat(&buf[4]); */
1704                 } else {
1705                         serv_puts("FORG");
1706                         serv_gets(buf);
1707                         if (buf[0] != '2') {
1708                                 /* ExpressMessageCat(&buf[4]); */
1709                         } else {
1710                                 strcpy(final_destination, "_BASEROOM_");
1711                         }
1712                 }
1713         }
1714         smart_goto(final_destination);
1715 }
1716
1717
1718
1719
1720 /*
1721  * Confirm deletion of the current room
1722  */
1723 void confirm_delete_room(void)
1724 {
1725         char buf[SIZ];
1726
1727         serv_puts("KILL 0");
1728         serv_gets(buf);
1729         if (buf[0] != '2') {
1730                 strcpy(WC->ImportantMessage, &buf[4]);
1731                 display_main_menu();
1732                 return;
1733         }
1734         output_headers(1);
1735         wprintf("<TABLE WIDTH=100%% BORDER=0 BGCOLOR=\"#770000\"><TR><TD>");
1736         wprintf("<SPAN CLASS=\"titlebar\">Confirm deletion of room</SPAN>\n");
1737         wprintf("</TD></TR></TABLE>\n");
1738
1739         wprintf("<CENTER>");
1740         wprintf("<FORM METHOD=\"GET\" ACTION=\"/delete_room\">\n");
1741
1742         wprintf("Are you sure you want to delete <FONT SIZE=+1>");
1743         escputs(WC->wc_roomname);
1744         wprintf("</FONT>?<BR>\n");
1745
1746         wprintf("<INPUT TYPE=\"submit\" NAME=\"sc\" VALUE=\"Delete\">");
1747         wprintf("<INPUT TYPE=\"submit\" NAME=\"sc\" VALUE=\"Cancel\">");
1748
1749         wprintf("</FORM></CENTER>\n");
1750         wDumpContent(1);
1751 }
1752
1753
1754 /*
1755  * Delete the current room
1756  */
1757 void delete_room(void)
1758 {
1759         char buf[SIZ];
1760         char sc[SIZ];
1761
1762         strcpy(sc, bstr("sc"));
1763
1764         if (strcasecmp(sc, "Delete")) {
1765                 strcpy(WC->ImportantMessage,
1766                         "Cancelled.  This room was not deleted.");
1767                 display_main_menu();
1768                 return;
1769         }
1770         serv_puts("KILL 1");
1771         serv_gets(buf);
1772         if (buf[0] != '2') {
1773                 strcpy(WC->ImportantMessage, &buf[4]);
1774                 display_main_menu();
1775                 return;
1776         } else {
1777                 smart_goto("_BASEROOM_");
1778         }
1779 }
1780
1781
1782
1783 /*
1784  * Perform changes to a room's network configuration
1785  */
1786 void netedit(void) {
1787         FILE *fp;
1788         char buf[SIZ];
1789         char line[SIZ];
1790
1791         if (strlen(bstr("line"))==0) {
1792                 display_editroom();
1793                 return;
1794         }
1795
1796         strcpy(line, bstr("prefix"));
1797         strcat(line, bstr("line"));
1798         strcat(line, bstr("suffix"));
1799
1800         fp = tmpfile();
1801         if (fp == NULL) {
1802                 display_editroom();
1803                 return;
1804         }
1805
1806         serv_puts("GNET");
1807         serv_gets(buf);
1808         if (buf[0] != '1') {
1809                 fclose(fp);
1810                 display_editroom();
1811                 return;
1812         }
1813
1814         /* This loop works for add *or* remove.  Spiffy, eh? */
1815         while (serv_gets(buf), strcmp(buf, "000")) {
1816                 if (strcasecmp(buf, line)) {
1817                         fprintf(fp, "%s\n", buf);
1818                 }
1819         }
1820
1821         rewind(fp);
1822         serv_puts("SNET");
1823         serv_gets(buf);
1824         if (buf[0] != '4') {
1825                 fclose(fp);
1826                 display_editroom();
1827                 return;
1828         }
1829
1830         while (fgets(buf, sizeof buf, fp) != NULL) {
1831                 buf[strlen(buf)-1] = 0;
1832                 serv_puts(buf);
1833         }
1834
1835         if (!strcasecmp(bstr("cmd"), "add")) {
1836                 serv_puts(line);
1837         }
1838
1839         serv_puts("000");
1840         fclose(fp);
1841         display_editroom();
1842 }
1843
1844
1845
1846 /*
1847  * Convert a room name to a folder-ish-looking name.
1848  */
1849 void room_to_folder(char *folder, char *room, int floor, int is_mailbox)
1850 {
1851         int i;
1852
1853         /*
1854          * For mailboxes, just do it straight...
1855          */
1856         if (is_mailbox) {
1857                 sprintf(folder, "My folders|%s", room);
1858         }
1859
1860         /*
1861          * Otherwise, prefix the floor name as a "public folders" moniker
1862          */
1863         else {
1864                 sprintf(folder, "%s|%s", floorlist[floor], room);
1865         }
1866
1867         /*
1868          * Replace "\" characters with "|" for pseudo-folder-delimiting
1869          */
1870         for (i=0; i<strlen(folder); ++i) {
1871                 if (folder[i] == '\\') folder[i] = '|';
1872         }
1873 }
1874
1875
1876
1877
1878 /*
1879  * Back end for change_view()
1880  */
1881 void do_change_view(int newview) {
1882         char buf[SIZ];
1883
1884         serv_printf("VIEW %d", newview);
1885         serv_gets(buf);
1886         smart_goto(WC->wc_roomname);
1887 }
1888
1889
1890
1891 /*
1892  * Change the view for this room
1893  */
1894 void change_view(void) {
1895         int view;
1896
1897         view = atol(bstr("view"));
1898         do_change_view(view);
1899 }
1900
1901
1902 /*
1903  * One big expanded tree list view --- like a folder list
1904  */
1905 void do_folder_view(struct folder *fold, int max_folders, int num_floors) {
1906         char buf[SIZ];
1907         int levels, oldlevels;
1908         int i, t;
1909
1910         do_template("beginbox_nt");
1911         levels = 0;
1912         oldlevels = 0;
1913         for (i=0; i<max_folders; ++i) {
1914
1915                 levels = num_tokens(fold[i].name, '|');
1916                 oldlevels = levels;
1917
1918                 for (t=0; t<levels; ++t) wprintf("&nbsp;&nbsp;&nbsp;");
1919                 if (fold[i].selectable) {
1920                         wprintf("<A HREF=\"/dotgoto?room=");
1921                         urlescputs(fold[i].room);
1922                         wprintf("\">");
1923                 }
1924                 else {
1925                         wprintf("<i>");
1926                 }
1927                 if (levels == 1) {
1928                         wprintf("<SPAN CLASS=\"roomlist_floor\">");
1929                 }
1930                 else if (fold[i].hasnewmsgs) {
1931                         wprintf("<SPAN CLASS=\"roomlist_new\">");
1932                 }
1933                 else {
1934                         wprintf("<SPAN CLASS=\"roomlist_old\">");
1935                 }
1936                 extract(buf, fold[i].name, levels-1);
1937                 escputs(buf);
1938                 wprintf("</SPAN>");
1939                 if (fold[i].selectable) {
1940                         wprintf("</A>");
1941                 }
1942                 else {
1943                         wprintf("</i>");
1944                 }
1945                 if (!strcasecmp(fold[i].name, "My Folders|Mail")) {
1946                         wprintf(" (INBOX)");
1947                 }
1948                 wprintf("<BR>\n");
1949         }
1950         do_template("endbox");
1951 }
1952
1953 /*
1954  * Boxes and rooms and lists ... oh my!
1955  */
1956 void do_rooms_view(struct folder *fold, int max_folders, int num_floors) {
1957         char buf[SIZ];
1958         char boxtitle[SIZ];
1959         int levels, oldlevels;
1960         int i, t;
1961         int num_boxes = 0;
1962         static int columns = 3;
1963         int boxes_per_column = 0;
1964         int current_column = 0;
1965         int nf;
1966
1967         nf = num_floors;
1968         while (nf % columns != 0) ++nf;
1969         boxes_per_column = (nf / columns);
1970         if (boxes_per_column < 1) boxes_per_column = 1;
1971
1972         /* Outer table (for columnization) */
1973         wprintf("<TABLE BORDER=0 WIDTH=100%% CELLPADDING=5>"
1974                 "<TR><TD VALIGN=TOP>");
1975
1976         levels = 0;
1977         oldlevels = 0;
1978         for (i=0; i<max_folders; ++i) {
1979
1980                 levels = num_tokens(fold[i].name, '|');
1981
1982                 if ((levels == 1) && (oldlevels == 2)) {
1983
1984                         /* End inner box */
1985                         do_template("endbox");
1986
1987                         ++num_boxes;
1988                         if ((num_boxes % boxes_per_column) == 0) {
1989                                 ++current_column;
1990                                 if (current_column < columns) {
1991                                         wprintf("</TD><TD VALIGN=TOP>\n");
1992                                 }
1993                         }
1994                 }
1995
1996                 if (levels == 1) {
1997
1998                         /* Begin inner box */
1999                         extract(buf, fold[i].name, levels-1);
2000                         stresc(boxtitle, buf, 1, 0);
2001                         svprintf("BOXTITLE", WCS_STRING, boxtitle);
2002                         do_template("beginbox");
2003
2004                 }
2005
2006                 oldlevels = levels;
2007
2008                 if (levels > 1) {
2009                         wprintf("&nbsp;");
2010                         if (levels>2) for (t=0; t<(levels-2); ++t) wprintf("&nbsp;&nbsp;&nbsp;");
2011                         if (fold[i].selectable) {
2012                                 wprintf("<A HREF=\"/dotgoto?room=");
2013                                 urlescputs(fold[i].room);
2014                                 wprintf("\">");
2015                         }
2016                         else {
2017                                 wprintf("<i>");
2018                         }
2019                         if (fold[i].hasnewmsgs) {
2020                                 wprintf("<SPAN CLASS=\"roomlist_new\">");
2021                         }
2022                         else {
2023                                 wprintf("<SPAN CLASS=\"roomlist_old\">");
2024                         }
2025                         extract(buf, fold[i].name, levels-1);
2026                         escputs(buf);
2027                         wprintf("</SPAN>");
2028                         if (fold[i].selectable) {
2029                                 wprintf("</A>");
2030                         }
2031                         else {
2032                                 wprintf("</i>");
2033                         }
2034                         if (!strcasecmp(fold[i].name, "My Folders|Mail")) {
2035                                 wprintf(" (INBOX)");
2036                         }
2037                         wprintf("<BR>\n");
2038                 }
2039         }
2040         /* End the final inner box */
2041         do_template("endbox");
2042
2043         wprintf("</TD></TR></TABLE>\n");
2044 }
2045
2046
2047 /*
2048  * Show the room list.  (only should get called by
2049  * knrooms() because that's where output_headers() is called from)
2050  */
2051
2052 void list_all_rooms_by_floor(char *viewpref) {
2053         char buf[SIZ];
2054         int swap = 0;
2055         struct folder *fold = NULL;
2056         struct folder ftmp;
2057         int max_folders = 0;
2058         int alloc_folders = 0;
2059         int i, j;
2060         int ra_flags = 0;
2061         int flags = 0;
2062         int num_floors = 1;     /* add an extra one for private folders */
2063
2064         /* Start with the mailboxes */
2065         max_folders = 1;
2066         alloc_folders = 1;
2067         fold = malloc(sizeof(struct folder));
2068         memset(fold, 0, sizeof(struct folder));
2069         strcpy(fold[0].name, "My folders");
2070         fold[0].is_mailbox = 1;
2071
2072         /* Then add floors */
2073         serv_puts("LFLR");
2074         serv_gets(buf);
2075         if (buf[0]=='1') while(serv_gets(buf), strcmp(buf, "000")) {
2076                 if (max_folders >= alloc_folders) {
2077                         alloc_folders = max_folders + 100;
2078                         fold = realloc(fold,
2079                                 alloc_folders * sizeof(struct folder));
2080                 }
2081                 memset(&fold[max_folders], 0, sizeof(struct folder));
2082                 extract(fold[max_folders].name, buf, 1);
2083                 ++max_folders;
2084                 ++num_floors;
2085         }
2086
2087         /* Now add rooms */
2088         serv_puts("LKRA");
2089         serv_gets(buf);
2090         if (buf[0]=='1') while(serv_gets(buf), strcmp(buf, "000")) {
2091                 if (max_folders >= alloc_folders) {
2092                         alloc_folders = max_folders + 100;
2093                         fold = realloc(fold,
2094                                 alloc_folders * sizeof(struct folder));
2095                 }
2096                 memset(&fold[max_folders], 0, sizeof(struct folder));
2097                 extract(fold[max_folders].room, buf, 0);
2098                 ra_flags = extract_int(buf, 5);
2099                 flags = extract_int(buf, 1);
2100                 fold[max_folders].floor = extract_int(buf, 2);
2101                 fold[max_folders].hasnewmsgs =
2102                         ((ra_flags & UA_HASNEWMSGS) ? 1 : 0 );
2103                 if (flags & QR_MAILBOX) {
2104                         fold[max_folders].is_mailbox = 1;
2105                 }
2106                 room_to_folder(fold[max_folders].name,
2107                                 fold[max_folders].room,
2108                                 fold[max_folders].floor,
2109                                 fold[max_folders].is_mailbox);
2110                 fold[max_folders].selectable = 1;
2111                 ++max_folders;
2112         }
2113
2114         /* Bubble-sort the folder list */
2115         for (i=0; i<max_folders; ++i) {
2116                 for (j=0; j<(max_folders-1)-i; ++j) {
2117                         if (fold[j].is_mailbox == fold[j+1].is_mailbox) {
2118                                 swap = strcasecmp(fold[j].name, fold[j+1].name);
2119                         }
2120                         else {
2121                                 if ( (fold[j+1].is_mailbox)
2122                                    && (!fold[j].is_mailbox)) {
2123                                         swap = 1;
2124                                 }
2125                                 else {
2126                                         swap = 0;
2127                                 }
2128                         }
2129                         if (swap > 0) {
2130                                 memcpy(&ftmp, &fold[j], sizeof(struct folder));
2131                                 memcpy(&fold[j], &fold[j+1],
2132                                                         sizeof(struct folder));
2133                                 memcpy(&fold[j+1], &ftmp,
2134                                                         sizeof(struct folder));
2135                         }
2136                 }
2137         }
2138
2139         if (!strcasecmp(viewpref, "folders")) {
2140                 do_folder_view(fold, max_folders, num_floors);
2141         }
2142         else {
2143                 do_rooms_view(fold, max_folders, num_floors);
2144         }
2145
2146         free(fold);
2147         wDumpContent(1);
2148 }
2149
2150
2151 /* Do either a known rooms list or a folders list, depending on the
2152  * user's preference
2153  */
2154 void knrooms() {
2155         char listviewpref[SIZ];
2156
2157         output_headers(3);
2158         load_floorlist();
2159
2160         /* Determine whether the user is trying to change views */
2161         if (bstr("view") != NULL) {
2162                 if (strlen(bstr("view")) > 0) {
2163                         set_preference("roomlistview", bstr("view"));
2164                 }
2165         }
2166
2167         get_preference("roomlistview", listviewpref);
2168
2169         if ( (strcasecmp(listviewpref, "folders"))
2170            && (strcasecmp(listviewpref, "table")) ) {
2171                 strcpy(listviewpref, "rooms");
2172         }
2173
2174         /* title bar */
2175         wprintf("<TABLE WIDTH=100%% BORDER=0 BGCOLOR=\"#000077\"><TR><TD>"
2176                 "<SPAN CLASS=\"titlebar\">"
2177         );
2178         if (!strcasecmp(listviewpref, "rooms")) {
2179                 wprintf("Room list");
2180         }
2181         if (!strcasecmp(listviewpref, "folders")) {
2182                 wprintf("Folder list");
2183         }
2184         if (!strcasecmp(listviewpref, "table")) {
2185                 wprintf("Room list");
2186         }
2187         wprintf("</SPAN></TD>\n");
2188
2189
2190         /* offer the ability to switch views */
2191         wprintf("<TD><FORM NAME=\"roomlistomatic\">\n"
2192                 "<SELECT NAME=\"newview\" SIZE=\"1\" "
2193                 "OnChange=\"location.href=roomlistomatic.newview.options"
2194                 "[selectedIndex].value\">\n");
2195
2196         wprintf("<OPTION %s VALUE=\"/knrooms&view=rooms\">"
2197                 "View as room list"
2198                 "</OPTION>\n",
2199                 ( !strcasecmp(listviewpref, "rooms") ? "SELECTED" : "" )
2200         );
2201
2202         wprintf("<OPTION %s VALUE=\"/knrooms&view=folders\">"
2203                 "View as folder list"
2204                 "</OPTION>\n",
2205                 ( !strcasecmp(listviewpref, "folders") ? "SELECTED" : "" )
2206         );
2207
2208         wprintf("<OPTION %s VALUE=\"/knrooms&view=table\">"
2209                 "Classic table view"
2210                 "</OPTION>\n",
2211                 ( !strcasecmp(listviewpref, "table") ? "SELECTED" : "" )
2212         );
2213
2214         wprintf("</SELECT></FORM></TD><TD>\n");
2215         offer_start_page();
2216         wprintf("</TD></TR></TABLE>\n");
2217
2218         /* Display the room list in the user's preferred format */
2219         if (!strcasecmp(listviewpref, "table")) {
2220                 tabular_room_list();
2221         }
2222         else {
2223                 list_all_rooms_by_floor(listviewpref);
2224         }
2225 }