]> code.citadel.org Git - citadel.git/blob - webcit/roomops.c
* Message entry using Kevin Roth's excellent cross-platform richtext
[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         /* start of anon options */
937         
938                 wprintf("<LI>Anonymous messages<UL>\n");
939         
940                 wprintf("<LI><INPUT TYPE=\"radio\" NAME=\"anon\" VALUE=\"no\" ");
941                 if (((er_flags & QR_ANONONLY) == 0)
942                     && ((er_flags & QR_ANONOPT) == 0))
943                         wprintf("CHECKED ");
944                 wprintf("> No anonymous messages\n");
945         
946                 wprintf("<LI><INPUT TYPE=\"radio\" NAME=\"anon\" VALUE=\"anononly\" ");
947                 if (er_flags & QR_ANONONLY)
948                         wprintf("CHECKED ");
949                 wprintf("> All messages are anonymous\n");
950         
951                 wprintf("<LI><INPUT TYPE=\"radio\" NAME=\"anon\" VALUE=\"anon2\" ");
952                 if (er_flags & QR_ANONOPT)
953                         wprintf("CHECKED ");
954                 wprintf("> Prompt user when entering messages</UL>\n");
955         
956         /* end of anon options */
957         
958                 wprintf("<LI>Room aide: \n");
959                 serv_puts("GETA");
960                 serv_gets(buf);
961                 if (buf[0] != '2') {
962                         wprintf("<EM>%s</EM>\n", &buf[4]);
963                 } else {
964                         extract(er_roomaide, &buf[4], 0);
965                         wprintf("<INPUT TYPE=\"text\" NAME=\"er_roomaide\" VALUE=\"%s\" MAXLENGTH=\"25\">\n", er_roomaide);
966                 }
967         
968                 wprintf("</UL><CENTER>\n");
969                 wprintf("<INPUT TYPE=\"submit\" NAME=\"sc\" VALUE=\"OK\">");
970                 wprintf("<INPUT TYPE=\"submit\" NAME=\"sc\" VALUE=\"Cancel\">");
971                 wprintf("</CENTER>\n");
972         }
973
974
975         /* Sharing the room with other Citadel nodes... */
976         if (!strcmp(tab, "sharing")) {
977
978                 shared_with = strdup("");
979                 not_shared_with = strdup("");
980
981                 /* Learn the current configuration */
982                 serv_puts("CONF getsys|application/x-citadel-ignet-config");
983                 serv_gets(buf);
984                 if (buf[0]=='1') while (serv_gets(buf), strcmp(buf, "000")) {
985                         extract(node, buf, 0);
986                         not_shared_with = realloc(not_shared_with,
987                                         strlen(not_shared_with) + 32);
988                         strcat(not_shared_with, node);
989                         strcat(not_shared_with, "|");
990                 }
991
992                 serv_puts("GNET");
993                 serv_gets(buf);
994                 if (buf[0]=='1') while (serv_gets(buf), strcmp(buf, "000")) {
995                         extract(cmd, buf, 0);
996                         extract(node, buf, 1);
997                         if (!strcasecmp(cmd, "ignet_push_share")) {
998                                 shared_with = realloc(shared_with,
999                                                 strlen(shared_with) + 32);
1000                                 strcat(shared_with, node);
1001                                 strcat(shared_with, "|");
1002                         }
1003                 }
1004
1005                 for (i=0; i<num_tokens(shared_with, '|'); ++i) {
1006                         extract(node, shared_with, i);
1007                         for (j=0; j<num_tokens(not_shared_with, '|'); ++j) {
1008                                 extract(cmd, not_shared_with, j);
1009                                 if (!strcasecmp(node, cmd)) {
1010                                         remove_token(not_shared_with, j, '|');
1011                                 }
1012                         }
1013                 }
1014
1015                 /* Display the stuff */
1016                 wprintf("<CENTER><BR>"
1017                         "<TABLE border=1 cellpadding=5><TR>"
1018                         "<TD><B><I>Shared with</I></B></TD>"
1019                         "<TD><B><I>Not shared with</I></B></TD></TR>\n"
1020                         "<TR><TD>\n");
1021
1022                 for (i=0; i<num_tokens(shared_with, '|'); ++i) {
1023                         extract(node, shared_with, i);
1024                         if (strlen(node) > 0) {
1025                                 wprintf("%s ", node);
1026                                 wprintf("<A HREF=\"/netedit&cmd=remove&line="
1027                                         "ignet_push_share|");
1028                                 urlescputs(node);
1029                                 wprintf("&tab=sharing\">(unshare)</A><BR>");
1030                         }
1031                 }
1032
1033                 wprintf("</TD><TD>\n");
1034
1035                 for (i=0; i<num_tokens(not_shared_with, '|'); ++i) {
1036                         extract(node, not_shared_with, i);
1037                         if (strlen(node) > 0) {
1038                                 wprintf("%s ", node);
1039                                 wprintf("<A HREF=\"/netedit&cmd=add&line="
1040                                         "ignet_push_share|");
1041                                 urlescputs(node);
1042                                 wprintf("&tab=sharing\">(share)</A><BR>");
1043                         }
1044                 }
1045
1046                 wprintf("</TD></TR>"
1047                         "</TABLE><BR>\n"
1048                         "<I><B>Reminder:</B> When sharing a room, "
1049                         "it must be shared from both ends.  Adding a node to "
1050                         "the 'shared' list sends messages out, but in order to"
1051                         " receive messages, the other nodes must be configured"
1052                         " to send messages out to your system as well.</I><BR>"
1053                         "</CENTER>\n");
1054
1055         }
1056
1057         /* Mailing list management */
1058         if (!strcmp(tab, "listserv")) {
1059
1060                 wprintf("<BR><center>"
1061                         "<TABLE BORDER=0 WIDTH=100%% CELLPADDING=5>"
1062                         "<TR><TD VALIGN=TOP>");
1063
1064                 wprintf("<i>The contents of this room are being "
1065                         "mailed <b>as individual messages</b> "
1066                         "to the following list recipients:"
1067                         "</i><br><br>\n");
1068
1069                 serv_puts("GNET");
1070                 serv_gets(buf);
1071                 if (buf[0]=='1') while (serv_gets(buf), strcmp(buf, "000")) {
1072                         extract(cmd, buf, 0);
1073                         if (!strcasecmp(cmd, "listrecp")) {
1074                                 extract(recp, buf, 1);
1075                         
1076                                 escputs(recp);
1077                                 wprintf(" <A HREF=\"/netedit&cmd=remove&line="
1078                                         "listrecp|");
1079                                 urlescputs(recp);
1080                                 wprintf("&tab=listserv\">(remove)</A><BR>");
1081
1082                         }
1083                 }
1084                 wprintf("<BR><FORM METHOD=\"POST\" ACTION=\"/netedit\">\n"
1085                         "<INPUT TYPE=\"hidden\" NAME=\"tab\" VALUE=\"listserv\">\n"
1086                         "<INPUT TYPE=\"hidden\" NAME=\"prefix\" VALUE=\"listrecp|\">\n");
1087                 wprintf("<INPUT TYPE=\"text\" NAME=\"line\">\n");
1088                 wprintf("<INPUT TYPE=\"submit\" NAME=\"cmd\" VALUE=\"Add\">");
1089                 wprintf("</FORM>\n");
1090
1091                 wprintf("</TD><TD VALIGN=TOP>\n");
1092                 
1093                 wprintf("<i>The contents of this room are being "
1094                         "mailed <b>in digest form</b> "
1095                         "to the following list recipients:"
1096                         "</i><br><br>\n");
1097
1098                 serv_puts("GNET");
1099                 serv_gets(buf);
1100                 if (buf[0]=='1') while (serv_gets(buf), strcmp(buf, "000")) {
1101                         extract(cmd, buf, 0);
1102                         if (!strcasecmp(cmd, "digestrecp")) {
1103                                 extract(recp, buf, 1);
1104                         
1105                                 escputs(recp);
1106                                 wprintf(" <A HREF=\"/netedit&cmd=remove&line="
1107                                         "digestrecp|");
1108                                 urlescputs(recp);
1109                                 wprintf("&tab=listserv\">(remove)</A><BR>");
1110
1111                         }
1112                 }
1113                 wprintf("<BR><FORM METHOD=\"POST\" ACTION=\"/netedit\">\n"
1114                         "<INPUT TYPE=\"hidden\" NAME=\"tab\" VALUE=\"listserv\">\n"
1115                         "<INPUT TYPE=\"hidden\" NAME=\"prefix\" VALUE=\"digestrecp|\">\n");
1116                 wprintf("<INPUT TYPE=\"text\" NAME=\"line\">\n");
1117                 wprintf("<INPUT TYPE=\"submit\" NAME=\"cmd\" VALUE=\"Add\">");
1118                 wprintf("</FORM>\n");
1119                 
1120                 wprintf("</TD></TR></TABLE><HR>\n");
1121
1122                 if (self_service(999) == 1) {
1123                         wprintf("This room is configured to allow "
1124                                 "self-service subscribe/unsubscribe requests."
1125                                 " <A HREF=\"/toggle_self_service?newval=0&"
1126                                 "tab=listserv\">"
1127                                 "Click to disable.</A><BR>\n"
1128                                 "The URL for subscribe/unsubscribe is: "
1129                                 "<TT>http://%s/listsub</TT><BR>\n",
1130                                 WC->http_host
1131                         );
1132                 }
1133                 else {
1134                         wprintf("This room is <i>not</i> configured to allow "
1135                                 "self-service subscribe/unsubscribe requests."
1136                                 " <A HREF=\"/toggle_self_service?newval=1&"
1137                                 "tab=listserv\">"
1138                                 "Click to enable.</A><BR>\n"
1139                         );
1140                 }
1141
1142
1143                 wprintf("</CENTER>\n");
1144         }
1145
1146         /* end content of whatever tab is open now */
1147         wprintf("</TD></TR></TABLE>\n");
1148
1149         wDumpContent(1);
1150 }
1151
1152
1153 /* 
1154  * Toggle self-service list subscription
1155  */
1156 void toggle_self_service(void) {
1157         int newval = 0;
1158
1159         newval = atoi(bstr("newval"));
1160         self_service(newval);
1161         display_editroom();
1162 }
1163
1164
1165
1166 /*
1167  * save new parameters for a room
1168  */
1169 void editroom(void)
1170 {
1171         char buf[SIZ];
1172         char er_name[20];
1173         char er_password[10];
1174         char er_dirname[15];
1175         char er_roomaide[26];
1176         int er_floor;
1177         unsigned er_flags;
1178         int bump;
1179
1180
1181         if (strcmp(bstr("sc"), "OK")) {
1182                 strcpy(WC->ImportantMessage,
1183                         "Cancelled.  Changes were not saved.");
1184                 display_main_menu();
1185                 return;
1186         }
1187         serv_puts("GETR");
1188         serv_gets(buf);
1189
1190         if (buf[0] != '2') {
1191                 strcpy(WC->ImportantMessage, &buf[4]);
1192                 display_main_menu();
1193                 return;
1194         }
1195         extract(er_name, &buf[4], 0);
1196         extract(er_password, &buf[4], 1);
1197         extract(er_dirname, &buf[4], 2);
1198         er_flags = extract_int(&buf[4], 3);
1199
1200         strcpy(er_roomaide, bstr("er_roomaide"));
1201         if (strlen(er_roomaide) == 0) {
1202                 serv_puts("GETA");
1203                 serv_gets(buf);
1204                 if (buf[0] != '2') {
1205                         strcpy(er_roomaide, "");
1206                 } else {
1207                         extract(er_roomaide, &buf[4], 0);
1208                 }
1209         }
1210         strcpy(buf, bstr("er_name"));
1211         buf[20] = 0;
1212         if (strlen(buf) > 0)
1213                 strcpy(er_name, buf);
1214
1215         strcpy(buf, bstr("er_password"));
1216         buf[10] = 0;
1217         if (strlen(buf) > 0)
1218                 strcpy(er_password, buf);
1219
1220         strcpy(buf, bstr("er_dirname"));
1221         buf[15] = 0;
1222         if (strlen(buf) > 0)
1223                 strcpy(er_dirname, buf);
1224
1225         strcpy(buf, bstr("type"));
1226         er_flags &= !(QR_PRIVATE | QR_PASSWORDED | QR_GUESSNAME);
1227
1228         if (!strcmp(buf, "invonly")) {
1229                 er_flags |= (QR_PRIVATE);
1230         }
1231         if (!strcmp(buf, "guessname")) {
1232                 er_flags |= (QR_PRIVATE | QR_GUESSNAME);
1233         }
1234         if (!strcmp(buf, "passworded")) {
1235                 er_flags |= (QR_PRIVATE | QR_PASSWORDED);
1236         }
1237         if (!strcmp(bstr("prefonly"), "yes")) {
1238                 er_flags |= QR_PREFONLY;
1239         } else {
1240                 er_flags &= ~QR_PREFONLY;
1241         }
1242
1243         if (!strcmp(bstr("readonly"), "yes")) {
1244                 er_flags |= QR_READONLY;
1245         } else {
1246                 er_flags &= ~QR_READONLY;
1247         }
1248
1249         if (!strcmp(bstr("network"), "yes")) {
1250                 er_flags |= QR_NETWORK;
1251         } else {
1252                 er_flags &= ~QR_NETWORK;
1253         }
1254
1255         if (!strcmp(bstr("directory"), "yes")) {
1256                 er_flags |= QR_DIRECTORY;
1257         } else {
1258                 er_flags &= ~QR_DIRECTORY;
1259         }
1260
1261         if (!strcmp(bstr("ulallowed"), "yes")) {
1262                 er_flags |= QR_UPLOAD;
1263         } else {
1264                 er_flags &= ~QR_UPLOAD;
1265         }
1266
1267         if (!strcmp(bstr("dlallowed"), "yes")) {
1268                 er_flags |= QR_DOWNLOAD;
1269         } else {
1270                 er_flags &= ~QR_DOWNLOAD;
1271         }
1272
1273         if (!strcmp(bstr("visdir"), "yes")) {
1274                 er_flags |= QR_VISDIR;
1275         } else {
1276                 er_flags &= ~QR_VISDIR;
1277         }
1278
1279         strcpy(buf, bstr("anon"));
1280
1281         er_flags &= ~(QR_ANONONLY | QR_ANONOPT);
1282         if (!strcmp(buf, "anononly"))
1283                 er_flags |= QR_ANONONLY;
1284         if (!strcmp(buf, "anon2"))
1285                 er_flags |= QR_ANONOPT;
1286
1287         bump = 0;
1288         if (!strcmp(bstr("bump"), "yes"))
1289                 bump = 1;
1290
1291         er_floor = atoi(bstr("er_floor"));
1292
1293         sprintf(buf, "SETR %s|%s|%s|%u|%d|%d",
1294              er_name, er_password, er_dirname, er_flags, bump, er_floor);
1295         serv_puts(buf);
1296         serv_gets(buf);
1297         if (buf[0] != '2') {
1298                 strcpy(WC->ImportantMessage, &buf[4]);
1299                 display_main_menu();
1300                 return;
1301         }
1302         gotoroom(er_name, 0);
1303
1304         if (strlen(er_roomaide) > 0) {
1305                 sprintf(buf, "SETA %s", er_roomaide);
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         }
1314         smart_goto(er_name);
1315 }
1316
1317 /*
1318  * Invite, Kick, and show Who Knows a room
1319  */
1320 void display_whok(void)
1321 {
1322         char buf[SIZ], room[SIZ], username[SIZ];
1323
1324         serv_puts("GETR");
1325         serv_gets(buf);
1326
1327         if (buf[0] != '2') {
1328                 strcpy(WC->ImportantMessage, &buf[4]);
1329                 display_main_menu();
1330                 return;
1331         }
1332         extract(room, &buf[4], 0);
1333
1334         strcpy(username, bstr("username"));
1335
1336         if(!strcmp(bstr("sc"), "Kick")) {
1337                 sprintf(buf, "KICK %s", username);
1338                 serv_puts(buf);
1339                 serv_gets(buf);
1340
1341                 if (buf[0] != '2') {
1342                         strcpy(WC->ImportantMessage, &buf[4]);
1343                 } else {
1344                         sprintf(WC->ImportantMessage,
1345                                 "<B><I>User %s kicked out of room %s.</I></B>\n", 
1346                                 username, room);
1347                 }
1348         } else if(!strcmp(bstr("sc"), "Invite")) {
1349                 sprintf(buf, "INVT %s", username);
1350                 serv_puts(buf);
1351                 serv_gets(buf);
1352
1353                 if (buf[0] != '2') {
1354                         strcpy(WC->ImportantMessage, &buf[4]);
1355                 } else {
1356                         sprintf(WC->ImportantMessage,
1357                                 "<B><I>User %s invited to room %s.</I></B>\n", 
1358                                 username, room);
1359                 }
1360         }
1361         
1362         output_headers(1);
1363         stresc(buf, WC->wc_roomname, 1, 1);
1364         svprintf("BOXTITLE", WCS_STRING, "Access control list for %s", buf);
1365         do_template("beginbox");
1366
1367         wprintf("<TABLE border=0 CELLSPACING=10><TR VALIGN=TOP>"
1368                 "<TD>The users listed below have access to this room.  "
1369                 "To remove a user from the access list, select the user "
1370                 "name from the list and click 'Kick'.<BR><BR>");
1371         
1372         wprintf("<CENTER><FORM METHOD=\"POST\" ACTION=\"/display_whok\">\n");
1373         wprintf("<SELECT NAME=\"username\" SIZE=10>\n");
1374         serv_puts("WHOK");
1375         serv_gets(buf);
1376         if (buf[0] == '1') {
1377                 while (serv_gets(buf), strcmp(buf, "000")) {
1378                         extract(username, buf, 0);
1379                         wprintf("<OPTION>");
1380                         escputs(username);
1381                         wprintf("\n");
1382                 }
1383         }
1384         wprintf("</SELECT><BR>\n");
1385
1386         wprintf("<input type=submit name=sc value=\"Kick\">");
1387         wprintf("</FORM></CENTER>\n");
1388
1389         wprintf("</TD><TD>"
1390                 "To grant another user access to this room, enter the "
1391                 "user name in the box below and click 'Invite'.<BR><BR>");
1392
1393         wprintf("<CENTER><FORM METHOD=\"POST\" ACTION=\"/display_whok\">\n");
1394         wprintf("Invite: ");
1395         wprintf("<input type=text name=username><BR>\n"
1396                 "<input type=hidden name=sc value=\"Invite\">"
1397                 "<input type=submit value=\"Invite\">"
1398                 "</FORM></CENTER>\n");
1399
1400         wprintf("</TD></TR></TABLE>\n");
1401         do_template("endbox");
1402         wDumpContent(1);
1403 }
1404
1405
1406
1407 /*
1408  * display the form for entering a new room
1409  */
1410 void display_entroom(void)
1411 {
1412         int i;
1413         char buf[SIZ];
1414
1415         serv_puts("CRE8 0");
1416         serv_gets(buf);
1417
1418         if (buf[0] != '2') {
1419                 strcpy(WC->ImportantMessage, &buf[4]);
1420                 display_main_menu();
1421                 return;
1422         }
1423         output_headers(3);
1424         svprintf("BOXTITLE", WCS_STRING, "Create a new room");
1425         do_template("beginbox");
1426
1427         wprintf("<FORM METHOD=\"POST\" ACTION=\"/entroom\">\n");
1428
1429         wprintf("<UL><LI>Name of room: ");
1430         wprintf("<INPUT TYPE=\"text\" NAME=\"er_name\" MAXLENGTH=\"19\">\n");
1431
1432         wprintf("<LI>Resides on floor: ");
1433         load_floorlist(); 
1434         wprintf("<SELECT NAME=\"er_floor\" SIZE=\"1\">\n");
1435         for (i = 0; i < 128; ++i)
1436                 if (strlen(floorlist[i]) > 0) {
1437                         wprintf("<OPTION ");
1438                         wprintf("VALUE=\"%d\">", i);
1439                         escputs(floorlist[i]);
1440                         wprintf("</OPTION>\n");
1441                 }
1442         wprintf("</SELECT>\n");
1443
1444         wprintf("<LI>Type of room:<UL>\n");
1445
1446         wprintf("<LI><INPUT TYPE=\"radio\" NAME=\"type\" VALUE=\"public\" ");
1447         wprintf("CHECKED > Public room\n");
1448
1449         wprintf("<LI><INPUT TYPE=\"radio\" NAME=\"type\" VALUE=\"guessname\" ");
1450         wprintf("> Private - guess name\n");
1451
1452         wprintf("<LI><INPUT TYPE=\"radio\" NAME=\"type\" VALUE=\"passworded\" ");
1453         wprintf("> Private - require password:\n");
1454         wprintf("<INPUT TYPE=\"text\" NAME=\"er_password\" MAXLENGTH=\"9\">\n");
1455
1456         wprintf("<LI><INPUT TYPE=\"radio\" NAME=\"type\" VALUE=\"invonly\" ");
1457         wprintf("> Private - invitation only\n");
1458
1459         wprintf("<LI><INPUT TYPE=\"radio\" NAME=\"type\" VALUE=\"personal\" ");
1460         wprintf("> Personal (mailbox for you only)\n");
1461         wprintf("</UL>\n");
1462
1463         wprintf("<LI>Default view for room: "); /* FOO */
1464         wprintf("<SELECT NAME=\"er_view\" SIZE=\"1\">\n");
1465         for (i=0; i<(sizeof viewdefs / sizeof (char *)); ++i) {
1466                 wprintf("<OPTION %s VALUE=\"%d\">",
1467                         ((i == 0) ? "SELECTED" : ""), i );
1468                 escputs(viewdefs[i]);
1469                 wprintf("</OPTION>\n");
1470         }
1471         wprintf("</SELECT>\n");
1472
1473         wprintf("<CENTER>\n");
1474         wprintf("<INPUT TYPE=\"submit\" NAME=\"sc\" VALUE=\"OK\">");
1475         wprintf("<INPUT TYPE=\"submit\" NAME=\"sc\" VALUE=\"Cancel\">");
1476         wprintf("</CENTER>\n");
1477         wprintf("</FORM>\n<HR>");
1478         serv_printf("MESG roomaccess");
1479         serv_gets(buf);
1480         if (buf[0] == '1') {
1481                 fmout(NULL, "CENTER");
1482         }
1483         do_template("endbox");
1484         wDumpContent(1);
1485 }
1486
1487
1488
1489
1490 /*
1491  * support function for entroom() -- sets the default view 
1492  */
1493 void er_set_default_view(int newview) {
1494
1495         char buf[SIZ];
1496
1497         char rm_name[SIZ];
1498         char rm_pass[SIZ];
1499         char rm_dir[SIZ];
1500         int rm_bits1;
1501         int rm_floor;
1502         int rm_listorder;
1503         int rm_bits2;
1504
1505         serv_puts("GETR");
1506         serv_gets(buf);
1507         if (buf[0] != '2') return;
1508
1509         extract(rm_name, &buf[4], 0);
1510         extract(rm_pass, &buf[4], 1);
1511         extract(rm_dir, &buf[4], 2);
1512         rm_bits1 = extract_int(&buf[4], 3);
1513         rm_floor = extract_int(&buf[4], 4);
1514         rm_listorder = extract_int(&buf[4], 5);
1515         rm_bits2 = extract_int(&buf[4], 7);
1516
1517         serv_printf("SETR %s|%s|%s|%d|0|%d|%d|%d|%d",
1518                 rm_name, rm_pass, rm_dir, rm_bits1, rm_floor,
1519                 rm_listorder, newview, rm_bits2
1520         );
1521         serv_gets(buf);
1522 }
1523
1524
1525
1526 /*
1527  * enter a new room
1528  */
1529 void entroom(void)
1530 {
1531         char buf[SIZ];
1532         char er_name[20];
1533         char er_type[20];
1534         char er_password[10];
1535         int er_floor;
1536         int er_num_type;
1537
1538         if (strcmp(bstr("sc"), "OK")) {
1539                 strcpy(WC->ImportantMessage,
1540                         "Cancelled.  No new room was created.");
1541                 display_main_menu();
1542                 return;
1543         }
1544         strcpy(er_name, bstr("er_name"));
1545         strcpy(er_type, bstr("type"));
1546         strcpy(er_password, bstr("er_password"));
1547         er_floor = atoi(bstr("er_floor"));
1548
1549         er_num_type = 0;
1550         if (!strcmp(er_type, "guessname"))
1551                 er_num_type = 1;
1552         if (!strcmp(er_type, "passworded"))
1553                 er_num_type = 2;
1554         if (!strcmp(er_type, "invonly"))
1555                 er_num_type = 3;
1556         if (!strcmp(er_type, "personal"))
1557                 er_num_type = 4;
1558
1559         sprintf(buf, "CRE8 1|%s|%d|%s|%d", 
1560                 er_name, er_num_type, er_password, er_floor);
1561         serv_puts(buf);
1562         serv_gets(buf);
1563         if (buf[0] != '2') {
1564                 strcpy(WC->ImportantMessage, &buf[4]);
1565                 display_main_menu();
1566                 return;
1567         }
1568         gotoroom(er_name, 0);
1569         er_set_default_view(atoi(bstr("er_view")));     /* Set default view */
1570         do_change_view(atoi(bstr("er_view")));          /* Now go there */
1571 }
1572
1573
1574 /*
1575  * display the screen to enter a private room
1576  */
1577 void display_private(char *rname, int req_pass)
1578 {
1579
1580         output_headers(3);
1581
1582         svprintf("BOXTITLE", WCS_STRING, "Go to a hidden room");
1583         do_template("beginbox");
1584
1585         wprintf("<CENTER>\n");
1586         wprintf("<BR>If you know the name of a hidden (guess-name) or\n");
1587         wprintf("passworded room, you can enter that room by typing\n");
1588         wprintf("its name below.  Once you gain access to a private\n");
1589         wprintf("room, it will appear in your regular room listings\n");
1590         wprintf("so you don't have to keep returning here.\n");
1591         wprintf("<BR><BR>");
1592
1593         wprintf("<FORM METHOD=\"GET\" ACTION=\"/goto_private\">\n");
1594
1595         wprintf("<table border=\"0\" cellspacing=\"5\" "
1596                 "cellpadding=\"5\" BGCOLOR=\"#EEEEEE\">\n"
1597                 "<TR><TD>"
1598                 "Enter room name:</TD><TD>"
1599                 "<INPUT TYPE=\"text\" NAME=\"gr_name\" "
1600                 "VALUE=\"%s\" MAXLENGTH=\"19\">\n", rname);
1601
1602         if (req_pass) {
1603                 wprintf("</TD></TR><TR><TD>");
1604                 wprintf("Enter room password:</TD><TD>");
1605                 wprintf("<INPUT TYPE=\"password\" NAME=\"gr_pass\" MAXLENGTH=\"9\">\n");
1606         }
1607         wprintf("</TD></TR></TABLE><BR>\n");
1608
1609         wprintf("<INPUT TYPE=\"submit\" NAME=\"sc\" VALUE=\"OK\">"
1610                 "&nbsp;"
1611                 "<INPUT TYPE=\"submit\" NAME=\"sc\" VALUE=\"Cancel\">");
1612         wprintf("</FORM>\n");
1613         do_template("endbox");
1614         wDumpContent(1);
1615 }
1616
1617 /* 
1618  * goto a private room
1619  */
1620 void goto_private(void)
1621 {
1622         char hold_rm[SIZ];
1623         char buf[SIZ];
1624
1625         if (strcasecmp(bstr("sc"), "OK")) {
1626                 display_main_menu();
1627                 return;
1628         }
1629         strcpy(hold_rm, WC->wc_roomname);
1630         strcpy(buf, "GOTO ");
1631         strcat(buf, bstr("gr_name"));
1632         strcat(buf, "|");
1633         strcat(buf, bstr("gr_pass"));
1634         serv_puts(buf);
1635         serv_gets(buf);
1636
1637         if (buf[0] == '2') {
1638                 smart_goto(bstr("gr_name"));
1639                 return;
1640         }
1641         if (!strncmp(buf, "540", 3)) {
1642                 display_private(bstr("gr_name"), 1);
1643                 return;
1644         }
1645         output_headers(1);
1646         wprintf("%s\n", &buf[4]);
1647         wDumpContent(1);
1648         return;
1649 }
1650
1651
1652 /*
1653  * display the screen to zap a room
1654  */
1655 void display_zap(void)
1656 {
1657         output_headers(1);
1658
1659         wprintf("<TABLE WIDTH=100%% BORDER=0 BGCOLOR=\"#770000\"><TR><TD>");
1660         wprintf("<SPAN CLASS=\"titlebar\">Zap (forget) the current room</SPAN>\n");
1661         wprintf("</TD></TR></TABLE>\n");
1662
1663         wprintf("If you select this option, <em>%s</em> will ", WC->wc_roomname);
1664         wprintf("disappear from your room list.  Is this what you wish ");
1665         wprintf("to do?<BR>\n");
1666
1667         wprintf("<FORM METHOD=\"GET\" ACTION=\"/zap\">\n");
1668         wprintf("<INPUT TYPE=\"submit\" NAME=\"sc\" VALUE=\"OK\">");
1669         wprintf("<INPUT TYPE=\"submit\" NAME=\"sc\" VALUE=\"Cancel\">");
1670         wprintf("</FORM>\n");
1671         wDumpContent(1);
1672 }
1673
1674
1675 /* 
1676  * zap a room
1677  */
1678 void zap(void)
1679 {
1680         char buf[SIZ];
1681         char final_destination[SIZ];
1682
1683         /* If the forget-room routine fails for any reason, we fall back
1684          * to the current room; otherwise, we go to the Lobby
1685          */
1686         strcpy(final_destination, WC->wc_roomname);
1687
1688         if (!strcasecmp(bstr("sc"), "OK")) {
1689                 serv_printf("GOTO %s", WC->wc_roomname);
1690                 serv_gets(buf);
1691                 if (buf[0] != '2') {
1692                         /* ExpressMessageCat(&buf[4]); */
1693                 } else {
1694                         serv_puts("FORG");
1695                         serv_gets(buf);
1696                         if (buf[0] != '2') {
1697                                 /* ExpressMessageCat(&buf[4]); */
1698                         } else {
1699                                 strcpy(final_destination, "_BASEROOM_");
1700                         }
1701                 }
1702         }
1703         smart_goto(final_destination);
1704 }
1705
1706
1707
1708
1709 /*
1710  * Confirm deletion of the current room
1711  */
1712 void confirm_delete_room(void)
1713 {
1714         char buf[SIZ];
1715
1716         serv_puts("KILL 0");
1717         serv_gets(buf);
1718         if (buf[0] != '2') {
1719                 strcpy(WC->ImportantMessage, &buf[4]);
1720                 display_main_menu();
1721                 return;
1722         }
1723         output_headers(1);
1724         wprintf("<TABLE WIDTH=100%% BORDER=0 BGCOLOR=\"#770000\"><TR><TD>");
1725         wprintf("<SPAN CLASS=\"titlebar\">Confirm deletion of room</SPAN>\n");
1726         wprintf("</TD></TR></TABLE>\n");
1727
1728         wprintf("<CENTER>");
1729         wprintf("<FORM METHOD=\"GET\" ACTION=\"/delete_room\">\n");
1730
1731         wprintf("Are you sure you want to delete <FONT SIZE=+1>");
1732         escputs(WC->wc_roomname);
1733         wprintf("</FONT>?<BR>\n");
1734
1735         wprintf("<INPUT TYPE=\"submit\" NAME=\"sc\" VALUE=\"Delete\">");
1736         wprintf("<INPUT TYPE=\"submit\" NAME=\"sc\" VALUE=\"Cancel\">");
1737
1738         wprintf("</FORM></CENTER>\n");
1739         wDumpContent(1);
1740 }
1741
1742
1743 /*
1744  * Delete the current room
1745  */
1746 void delete_room(void)
1747 {
1748         char buf[SIZ];
1749         char sc[SIZ];
1750
1751         strcpy(sc, bstr("sc"));
1752
1753         if (strcasecmp(sc, "Delete")) {
1754                 strcpy(WC->ImportantMessage,
1755                         "Cancelled.  This room was not deleted.");
1756                 display_main_menu();
1757                 return;
1758         }
1759         serv_puts("KILL 1");
1760         serv_gets(buf);
1761         if (buf[0] != '2') {
1762                 strcpy(WC->ImportantMessage, &buf[4]);
1763                 display_main_menu();
1764                 return;
1765         } else {
1766                 smart_goto("_BASEROOM_");
1767         }
1768 }
1769
1770
1771
1772 /*
1773  * Perform changes to a room's network configuration
1774  */
1775 void netedit(void) {
1776         FILE *fp;
1777         char buf[SIZ];
1778         char line[SIZ];
1779
1780         if (strlen(bstr("line"))==0) {
1781                 display_editroom();
1782                 return;
1783         }
1784
1785         strcpy(line, bstr("prefix"));
1786         strcat(line, bstr("line"));
1787         strcat(line, bstr("suffix"));
1788
1789         fp = tmpfile();
1790         if (fp == NULL) {
1791                 display_editroom();
1792                 return;
1793         }
1794
1795         serv_puts("GNET");
1796         serv_gets(buf);
1797         if (buf[0] != '1') {
1798                 fclose(fp);
1799                 display_editroom();
1800                 return;
1801         }
1802
1803         /* This loop works for add *or* remove.  Spiffy, eh? */
1804         while (serv_gets(buf), strcmp(buf, "000")) {
1805                 if (strcasecmp(buf, line)) {
1806                         fprintf(fp, "%s\n", buf);
1807                 }
1808         }
1809
1810         rewind(fp);
1811         serv_puts("SNET");
1812         serv_gets(buf);
1813         if (buf[0] != '4') {
1814                 fclose(fp);
1815                 display_editroom();
1816                 return;
1817         }
1818
1819         while (fgets(buf, sizeof buf, fp) != NULL) {
1820                 buf[strlen(buf)-1] = 0;
1821                 serv_puts(buf);
1822         }
1823
1824         if (!strcasecmp(bstr("cmd"), "add")) {
1825                 serv_puts(line);
1826         }
1827
1828         serv_puts("000");
1829         fclose(fp);
1830         display_editroom();
1831 }
1832
1833
1834
1835 /*
1836  * Convert a room name to a folder-ish-looking name.
1837  */
1838 void room_to_folder(char *folder, char *room, int floor, int is_mailbox)
1839 {
1840         int i;
1841
1842         /*
1843          * For mailboxes, just do it straight...
1844          */
1845         if (is_mailbox) {
1846                 sprintf(folder, "My folders|%s", room);
1847         }
1848
1849         /*
1850          * Otherwise, prefix the floor name as a "public folders" moniker
1851          */
1852         else {
1853                 sprintf(folder, "%s|%s", floorlist[floor], room);
1854         }
1855
1856         /*
1857          * Replace "\" characters with "|" for pseudo-folder-delimiting
1858          */
1859         for (i=0; i<strlen(folder); ++i) {
1860                 if (folder[i] == '\\') folder[i] = '|';
1861         }
1862 }
1863
1864
1865
1866
1867 /*
1868  * Back end for change_view()
1869  */
1870 void do_change_view(int newview) {
1871         char buf[SIZ];
1872
1873         serv_printf("VIEW %d", newview);
1874         serv_gets(buf);
1875         smart_goto(WC->wc_roomname);
1876 }
1877
1878
1879
1880 /*
1881  * Change the view for this room
1882  */
1883 void change_view(void) {
1884         int view;
1885
1886         view = atol(bstr("view"));
1887         do_change_view(view);
1888 }
1889
1890
1891 /*
1892  * One big expanded tree list view --- like a folder list
1893  */
1894 void do_folder_view(struct folder *fold, int max_folders, int num_floors) {
1895         char buf[SIZ];
1896         int levels, oldlevels;
1897         int i, t;
1898
1899         do_template("beginbox_nt");
1900         levels = 0;
1901         oldlevels = 0;
1902         for (i=0; i<max_folders; ++i) {
1903
1904                 levels = num_tokens(fold[i].name, '|');
1905                 oldlevels = levels;
1906
1907                 for (t=0; t<levels; ++t) wprintf("&nbsp;&nbsp;&nbsp;");
1908                 if (fold[i].selectable) {
1909                         wprintf("<A HREF=\"/dotgoto?room=");
1910                         urlescputs(fold[i].room);
1911                         wprintf("\">");
1912                 }
1913                 else {
1914                         wprintf("<i>");
1915                 }
1916                 if (levels == 1) {
1917                         wprintf("<SPAN CLASS=\"roomlist_floor\">");
1918                 }
1919                 else if (fold[i].hasnewmsgs) {
1920                         wprintf("<SPAN CLASS=\"roomlist_new\">");
1921                 }
1922                 else {
1923                         wprintf("<SPAN CLASS=\"roomlist_old\">");
1924                 }
1925                 extract(buf, fold[i].name, levels-1);
1926                 escputs(buf);
1927                 wprintf("</SPAN>");
1928                 if (fold[i].selectable) {
1929                         wprintf("</A>");
1930                 }
1931                 else {
1932                         wprintf("</i>");
1933                 }
1934                 if (!strcasecmp(fold[i].name, "My Folders|Mail")) {
1935                         wprintf(" (INBOX)");
1936                 }
1937                 wprintf("<BR>\n");
1938         }
1939         do_template("endbox");
1940 }
1941
1942 /*
1943  * Boxes and rooms and lists ... oh my!
1944  */
1945 void do_rooms_view(struct folder *fold, int max_folders, int num_floors) {
1946         char buf[SIZ];
1947         char boxtitle[SIZ];
1948         int levels, oldlevels;
1949         int i, t;
1950         int num_boxes = 0;
1951         static int columns = 3;
1952         int boxes_per_column = 0;
1953         int current_column = 0;
1954         int nf;
1955
1956         nf = num_floors;
1957         while (nf % columns != 0) ++nf;
1958         boxes_per_column = (nf / columns);
1959         if (boxes_per_column < 1) boxes_per_column = 1;
1960
1961         /* Outer table (for columnization) */
1962         wprintf("<TABLE BORDER=0 WIDTH=100%% CELLPADDING=5>"
1963                 "<TR><TD VALIGN=TOP>");
1964
1965         levels = 0;
1966         oldlevels = 0;
1967         for (i=0; i<max_folders; ++i) {
1968
1969                 levels = num_tokens(fold[i].name, '|');
1970
1971                 if ((levels == 1) && (oldlevels == 2)) {
1972
1973                         /* End inner box */
1974                         do_template("endbox");
1975
1976                         ++num_boxes;
1977                         if ((num_boxes % boxes_per_column) == 0) {
1978                                 ++current_column;
1979                                 if (current_column < columns) {
1980                                         wprintf("</TD><TD VALIGN=TOP>\n");
1981                                 }
1982                         }
1983                 }
1984
1985                 if (levels == 1) {
1986
1987                         /* Begin inner box */
1988                         extract(buf, fold[i].name, levels-1);
1989                         stresc(boxtitle, buf, 1, 0);
1990                         svprintf("BOXTITLE", WCS_STRING, boxtitle);
1991                         do_template("beginbox");
1992
1993                 }
1994
1995                 oldlevels = levels;
1996
1997                 if (levels > 1) {
1998                         wprintf("&nbsp;");
1999                         if (levels>2) for (t=0; t<(levels-2); ++t) wprintf("&nbsp;&nbsp;&nbsp;");
2000                         if (fold[i].selectable) {
2001                                 wprintf("<A HREF=\"/dotgoto?room=");
2002                                 urlescputs(fold[i].room);
2003                                 wprintf("\">");
2004                         }
2005                         else {
2006                                 wprintf("<i>");
2007                         }
2008                         if (fold[i].hasnewmsgs) {
2009                                 wprintf("<SPAN CLASS=\"roomlist_new\">");
2010                         }
2011                         else {
2012                                 wprintf("<SPAN CLASS=\"roomlist_old\">");
2013                         }
2014                         extract(buf, fold[i].name, levels-1);
2015                         escputs(buf);
2016                         wprintf("</SPAN>");
2017                         if (fold[i].selectable) {
2018                                 wprintf("</A>");
2019                         }
2020                         else {
2021                                 wprintf("</i>");
2022                         }
2023                         if (!strcasecmp(fold[i].name, "My Folders|Mail")) {
2024                                 wprintf(" (INBOX)");
2025                         }
2026                         wprintf("<BR>\n");
2027                 }
2028         }
2029         /* End the final inner box */
2030         do_template("endbox");
2031
2032         wprintf("</TD></TR></TABLE>\n");
2033 }
2034
2035
2036 /*
2037  * Show the room list.  (only should get called by
2038  * knrooms() because that's where output_headers() is called from)
2039  */
2040
2041 void list_all_rooms_by_floor(char *viewpref) {
2042         char buf[SIZ];
2043         int swap = 0;
2044         struct folder *fold = NULL;
2045         struct folder ftmp;
2046         int max_folders = 0;
2047         int alloc_folders = 0;
2048         int i, j;
2049         int ra_flags = 0;
2050         int flags = 0;
2051         int num_floors = 1;     /* add an extra one for private folders */
2052
2053         /* Start with the mailboxes */
2054         max_folders = 1;
2055         alloc_folders = 1;
2056         fold = malloc(sizeof(struct folder));
2057         memset(fold, 0, sizeof(struct folder));
2058         strcpy(fold[0].name, "My folders");
2059         fold[0].is_mailbox = 1;
2060
2061         /* Then add floors */
2062         serv_puts("LFLR");
2063         serv_gets(buf);
2064         if (buf[0]=='1') while(serv_gets(buf), strcmp(buf, "000")) {
2065                 if (max_folders >= alloc_folders) {
2066                         alloc_folders = max_folders + 100;
2067                         fold = realloc(fold,
2068                                 alloc_folders * sizeof(struct folder));
2069                 }
2070                 memset(&fold[max_folders], 0, sizeof(struct folder));
2071                 extract(fold[max_folders].name, buf, 1);
2072                 ++max_folders;
2073                 ++num_floors;
2074         }
2075
2076         /* Now add rooms */
2077         serv_puts("LKRA");
2078         serv_gets(buf);
2079         if (buf[0]=='1') while(serv_gets(buf), strcmp(buf, "000")) {
2080                 if (max_folders >= alloc_folders) {
2081                         alloc_folders = max_folders + 100;
2082                         fold = realloc(fold,
2083                                 alloc_folders * sizeof(struct folder));
2084                 }
2085                 memset(&fold[max_folders], 0, sizeof(struct folder));
2086                 extract(fold[max_folders].room, buf, 0);
2087                 ra_flags = extract_int(buf, 5);
2088                 flags = extract_int(buf, 1);
2089                 fold[max_folders].floor = extract_int(buf, 2);
2090                 fold[max_folders].hasnewmsgs =
2091                         ((ra_flags & UA_HASNEWMSGS) ? 1 : 0 );
2092                 if (flags & QR_MAILBOX) {
2093                         fold[max_folders].is_mailbox = 1;
2094                 }
2095                 room_to_folder(fold[max_folders].name,
2096                                 fold[max_folders].room,
2097                                 fold[max_folders].floor,
2098                                 fold[max_folders].is_mailbox);
2099                 fold[max_folders].selectable = 1;
2100                 ++max_folders;
2101         }
2102
2103         /* Bubble-sort the folder list */
2104         for (i=0; i<max_folders; ++i) {
2105                 for (j=0; j<(max_folders-1)-i; ++j) {
2106                         if (fold[j].is_mailbox == fold[j+1].is_mailbox) {
2107                                 swap = strcasecmp(fold[j].name, fold[j+1].name);
2108                         }
2109                         else {
2110                                 if ( (fold[j+1].is_mailbox)
2111                                    && (!fold[j].is_mailbox)) {
2112                                         swap = 1;
2113                                 }
2114                                 else {
2115                                         swap = 0;
2116                                 }
2117                         }
2118                         if (swap > 0) {
2119                                 memcpy(&ftmp, &fold[j], sizeof(struct folder));
2120                                 memcpy(&fold[j], &fold[j+1],
2121                                                         sizeof(struct folder));
2122                                 memcpy(&fold[j+1], &ftmp,
2123                                                         sizeof(struct folder));
2124                         }
2125                 }
2126         }
2127
2128         if (!strcasecmp(viewpref, "folders")) {
2129                 do_folder_view(fold, max_folders, num_floors);
2130         }
2131         else {
2132                 do_rooms_view(fold, max_folders, num_floors);
2133         }
2134
2135         free(fold);
2136         wDumpContent(1);
2137 }
2138
2139
2140 /* Do either a known rooms list or a folders list, depending on the
2141  * user's preference
2142  */
2143 void knrooms() {
2144         char listviewpref[SIZ];
2145
2146         output_headers(3);
2147         load_floorlist();
2148
2149         /* Determine whether the user is trying to change views */
2150         if (bstr("view") != NULL) {
2151                 if (strlen(bstr("view")) > 0) {
2152                         set_preference("roomlistview", bstr("view"));
2153                 }
2154         }
2155
2156         get_preference("roomlistview", listviewpref);
2157
2158         if ( (strcasecmp(listviewpref, "folders"))
2159            && (strcasecmp(listviewpref, "table")) ) {
2160                 strcpy(listviewpref, "rooms");
2161         }
2162
2163         /* title bar */
2164         wprintf("<TABLE WIDTH=100%% BORDER=0 BGCOLOR=\"#000077\"><TR><TD>"
2165                 "<SPAN CLASS=\"titlebar\">"
2166         );
2167         if (!strcasecmp(listviewpref, "rooms")) {
2168                 wprintf("Room list");
2169         }
2170         if (!strcasecmp(listviewpref, "folders")) {
2171                 wprintf("Folder list");
2172         }
2173         if (!strcasecmp(listviewpref, "table")) {
2174                 wprintf("Room list");
2175         }
2176         wprintf("</SPAN></TD>\n");
2177
2178
2179         /* offer the ability to switch views */
2180         wprintf("<TD><FORM NAME=\"roomlistomatic\">\n"
2181                 "<SELECT NAME=\"newview\" SIZE=\"1\" "
2182                 "OnChange=\"location.href=roomlistomatic.newview.options"
2183                 "[selectedIndex].value\">\n");
2184
2185         wprintf("<OPTION %s VALUE=\"/knrooms&view=rooms\">"
2186                 "View as room list"
2187                 "</OPTION>\n",
2188                 ( !strcasecmp(listviewpref, "rooms") ? "SELECTED" : "" )
2189         );
2190
2191         wprintf("<OPTION %s VALUE=\"/knrooms&view=folders\">"
2192                 "View as folder list"
2193                 "</OPTION>\n",
2194                 ( !strcasecmp(listviewpref, "folders") ? "SELECTED" : "" )
2195         );
2196
2197         wprintf("<OPTION %s VALUE=\"/knrooms&view=table\">"
2198                 "Classic table view"
2199                 "</OPTION>\n",
2200                 ( !strcasecmp(listviewpref, "table") ? "SELECTED" : "" )
2201         );
2202
2203         wprintf("</SELECT></FORM></TD><TD>\n");
2204         offer_start_page();
2205         wprintf("</TD></TR></TABLE>\n");
2206
2207         /* Display the room list in the user's preferred format */
2208         if (!strcasecmp(listviewpref, "table")) {
2209                 tabular_room_list();
2210         }
2211         else {
2212                 list_all_rooms_by_floor(listviewpref);
2213         }
2214 }