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