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