af4b85c80e2fc3842f1df38a3c805486fd77cfc7
[citadel.git] / webcit / roomops.c
1 /* $Id$ */
2
3
4 #include <ctype.h>
5 #include <stdlib.h>
6 #include <unistd.h>
7 #include <stdio.h>
8 #include <fcntl.h>
9 #include <signal.h>
10 #include <sys/types.h>
11 #include <sys/wait.h>
12 #include <sys/socket.h>
13 #include <sys/time.h>
14 #include <limits.h>
15 #include <netinet/in.h>
16 #include <netdb.h>
17 #include <string.h>
18 #include <pwd.h>
19 #include <errno.h>
20 #include <stdarg.h>
21 #include <pthread.h>
22 #include <signal.h>
23 #include "webcit.h"
24
25 struct folder {
26         int floor;
27         char room[SIZ];
28         char name[SIZ];
29         int hasnewmsgs;
30         int is_mailbox;
31         int selectable;
32 };
33
34 char *viewdefs[] = {
35         "Messages",
36         "Summary",
37         "Address Book",
38         "Calendar",
39         "Tasks"
40 };
41
42 char floorlist[128][SIZ];
43
44 /*
45  * load the list of floors
46  */
47 void load_floorlist(void)
48 {
49         int a;
50         char buf[SIZ];
51
52         for (a = 0; a < 128; ++a)
53                 floorlist[a][0] = 0;
54
55         serv_puts("LFLR");
56         serv_gets(buf);
57         if (buf[0] != '1') {
58                 strcpy(floorlist[0], "Main Floor");
59                 return;
60         }
61         while (serv_gets(buf), strcmp(buf, "000")) {
62                 extract(floorlist[extract_int(buf, 0)], buf, 1);
63         }
64 }
65
66
67 /*
68  * remove a room from the march list
69  */
70 void remove_march(char *aaa)
71 {
72         struct march *mptr, *mptr2;
73
74         if (WC->march == NULL)
75                 return;
76
77         if (!strcasecmp(WC->march->march_name, aaa)) {
78                 mptr = WC->march->next;
79                 free(WC->march);
80                 WC->march = mptr;
81                 return;
82         }
83         mptr2 = WC->march;
84         for (mptr = WC->march; mptr != NULL; mptr = mptr->next) {
85                 if (!strcasecmp(mptr->march_name, aaa)) {
86                         mptr2->next = mptr->next;
87                         free(mptr);
88                         mptr = mptr2;
89                 } else {
90                         mptr2 = mptr;
91                 }
92         }
93 }
94
95
96
97
98
99 void room_tree_list(struct roomlisting *rp)
100 {
101         char rmname[64];
102         int f;
103
104         if (rp == NULL) {
105                 return;
106         }
107
108         room_tree_list(rp->lnext);
109
110         strcpy(rmname, rp->rlname);
111         f = rp->rlflags;
112
113         wprintf("<A HREF=\"/dotgoto&room=");
114         urlescputs(rmname);
115         wprintf("\"");
116         wprintf(">");
117         escputs1(rmname, 1, 1);
118         if ((f & QR_DIRECTORY) && (f & QR_NETWORK))
119                 wprintf("}");
120         else if (f & QR_DIRECTORY)
121                 wprintf("]");
122         else if (f & QR_NETWORK)
123                 wprintf(")");
124         else
125                 wprintf("&gt;");
126         wprintf("</A><TT> </TT>\n");
127
128         room_tree_list(rp->rnext);
129         free(rp);
130 }
131
132
133 /* 
134  * Room ordering stuff (compare first by floor, then by order)
135  */
136 int rordercmp(struct roomlisting *r1, struct roomlisting *r2)
137 {
138         if ((r1 == NULL) && (r2 == NULL))
139                 return (0);
140         if (r1 == NULL)
141                 return (-1);
142         if (r2 == NULL)
143                 return (1);
144         if (r1->rlfloor < r2->rlfloor)
145                 return (-1);
146         if (r1->rlfloor > r2->rlfloor)
147                 return (1);
148         if (r1->rlorder < r2->rlorder)
149                 return (-1);
150         if (r1->rlorder > r2->rlorder)
151                 return (1);
152         return (0);
153 }
154
155
156 /*
157  * Common code for all room listings
158  */
159 void listrms(char *variety)
160 {
161         char buf[SIZ];
162         int num_rooms = 0;
163
164         struct roomlisting *rl = NULL;
165         struct roomlisting *rp;
166         struct roomlisting *rs;
167
168
169         /* Ask the server for a room list */
170         serv_puts(variety);
171         serv_gets(buf);
172         if (buf[0] != '1') {
173                 wprintf("&nbsp;");
174                 return;
175         }
176         while (serv_gets(buf), strcmp(buf, "000")) {
177                 ++num_rooms;
178                 rp = malloc(sizeof(struct roomlisting));
179                 extract(rp->rlname, buf, 0);
180                 rp->rlflags = extract_int(buf, 1);
181                 rp->rlfloor = extract_int(buf, 2);
182                 rp->rlorder = extract_int(buf, 3);
183                 rp->lnext = NULL;
184                 rp->rnext = NULL;
185
186                 rs = rl;
187                 if (rl == NULL) {
188                         rl = rp;
189                 } else
190                         while (rp != NULL) {
191                                 if (rordercmp(rp, rs) < 0) {
192                                         if (rs->lnext == NULL) {
193                                                 rs->lnext = rp;
194                                                 rp = NULL;
195                                         } else {
196                                                 rs = rs->lnext;
197                                         }
198                                 } else {
199                                         if (rs->rnext == NULL) {
200                                                 rs->rnext = rp;
201                                                 rp = NULL;
202                                         } else {
203                                                 rs = rs->rnext;
204                                         }
205                                 }
206                         }
207         }
208
209         room_tree_list(rl);
210
211         /* If no rooms were listed, print an nbsp to make the cell
212          * borders show up anyway.
213          */
214         if (num_rooms == 0) wprintf("&nbsp;");
215 }
216
217
218 /*
219  * list all forgotten rooms
220  */
221 void zapped_list(void)
222 {
223         output_headers(3);
224
225         svprintf("BOXTITLE", WCS_STRING, "Zapped (forgotten) rooms");
226         do_template("beginbox");
227
228         listrms("LZRM -1");
229
230         wprintf("<BR><BR>\n");
231         wprintf("Click on any room to un-zap it and goto that room.\n");
232         do_template("endbox");
233         wDumpContent(1);
234 }
235
236
237 /*
238  * read this room's info file (set v to 1 for verbose mode)
239  */
240 void readinfo(void)
241 {
242         char buf[SIZ];
243
244         serv_puts("RINF");
245         serv_gets(buf);
246         if (buf[0] == '1') {
247                 fmout(NULL, "CENTER");
248         }
249 }
250
251
252
253
254 /* Display room graphic.  The server doesn't actually
255  * need the room name, but we supply it in order to
256  * keep the browser from using a cached graphic from 
257  * another room.
258  */
259 void embed_room_graphic(void) {
260         char buf[SIZ];
261
262         serv_puts("OIMG _roompic_");
263         serv_gets(buf);
264         if (buf[0] == '2') {
265                 wprintf("<TD BGCOLOR=\"#444455\">");
266                 wprintf("<IMG HEIGHT=64 SRC=\"/image&name=_roompic_&room=");
267                 urlescputs(WC->wc_roomname);
268                 wprintf("\"></TD>");
269                 serv_puts("CLOS");
270                 serv_gets(buf);
271         }
272
273 }
274
275
276 /* Let the user know if new mail has arrived 
277  */
278 void embed_newmail_button(void) {
279         if ( (WC->new_mail > WC->remember_new_mail) && (WC->new_mail>0) ) {
280                 wprintf(
281                         "<A HREF=\"/dotgoto?room=_MAIL_\">"
282                         "<IMG SRC=\"/static/mail.gif\" border=0 "
283                         "ALT=\"You have new mail\">"
284                         "<BR><SPAN CLASS=\"youhavemail\">"
285                         "%d new mail</SPAN></A>", WC->new_mail);
286                 WC->remember_new_mail = WC->new_mail;
287         }
288 }
289
290
291
292 /*
293  * Display the current view and offer an option to change it
294  */
295 void embed_view_o_matic(void) {
296         int i;
297
298         wprintf("<FORM NAME=\"viewomatic\">\n"
299                 "<SELECT NAME=\"newview\" SIZE=\"1\" "
300                 "OnChange=\"location.href=viewomatic.newview.options"
301                 "[selectedIndex].value\">\n");
302
303         for (i=0; i<(sizeof viewdefs / sizeof (char *)); ++i) {
304                 /*
305                  * Only offer the views that make sense, given the default
306                  * view for the room.  For example, don't offer a Calendar
307                  * view in a non-Calendar room.
308                  */
309                 if (
310                         (i == WC->wc_view)
311                    ||   (i == WC->wc_default_view)
312                    ||   ( (i == 0) && (WC->wc_default_view == 1) )
313                    ||   ( (i == 1) && (WC->wc_default_view == 0) )
314                 ) {
315
316                         wprintf("<OPTION %s VALUE=\"/changeview?view=%d\">",
317                                 ((i == WC->wc_view) ? "SELECTED" : ""),
318                                 i );
319                         escputs(viewdefs[i]);
320                         wprintf("</OPTION>\n");
321                 }
322         }
323         wprintf("</SELECT></FORM>\n");
324 }
325
326
327
328 void embed_room_banner(char *got) {
329         char fakegot[SIZ];
330
331         /* We need to have the information returned by a GOTO server command.
332          * If it isn't supplied, we fake it by issuing our own GOTO.
333          */
334         if (got == NULL) {
335                 serv_printf("GOTO %s", WC->wc_roomname);
336                 serv_gets(fakegot);
337                 got = fakegot;
338         }
339
340         /* If the user happens to select the "make this my start page" link,
341          * we want it to remember the URL as a "/dotskip" one instead of
342          * a "skip" or "gotonext" or something like that.
343          */
344         snprintf(WC->this_page, sizeof(WC->this_page), "/dotskip&room=%s",
345                 WC->wc_roomname);
346
347         /* Check for new mail. */
348         WC->new_mail = extract_int(&got[4], 9);
349         WC->wc_view = extract_int(&got[4], 11);
350
351         svprintf("ROOMNAME", WCS_STRING, "%s", WC->wc_roomname);
352         svprintf("NEWMSGS", WCS_STRING, "%d", extract_int(&got[4], 1));
353         svprintf("TOTALMSGS", WCS_STRING, "%d", extract_int(&got[4], 2));
354         svcallback("ROOMPIC", embed_room_graphic);
355         svcallback("ROOMINFO", readinfo);
356         svcallback("YOUHAVEMAIL", embed_newmail_button);
357         svcallback("VIEWOMATIC", embed_view_o_matic);
358         svcallback("START", offer_start_page);
359
360         do_template("roombanner");
361         clear_local_substs();
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 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);
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, "|");
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                         if (!strcasecmp(cmd, "ignet_push_share")) {
943                                 shared_with = realloc(shared_with,
944                                                 strlen(shared_with) + 32);
945                                 strcat(shared_with, node);
946                                 strcat(shared_with, "|");
947                         }
948                 }
949
950                 for (i=0; i<num_tokens(shared_with, '|'); ++i) {
951                         extract(node, shared_with, i);
952                         for (j=0; j<num_tokens(not_shared_with, '|'); ++j) {
953                                 extract(cmd, not_shared_with, j);
954                                 if (!strcasecmp(node, cmd)) {
955                                         remove_token(not_shared_with, j, '|');
956                                 }
957                         }
958                 }
959
960                 /* Display the stuff */
961                 wprintf("<CENTER><BR>"
962                         "<TABLE border=1 cellpadding=5><TR>"
963                         "<TD><B><I>Shared with</I></B></TD>"
964                         "<TD><B><I>Not shared with</I></B></TD></TR>\n"
965                         "<TR><TD>\n");
966
967                 for (i=0; i<num_tokens(shared_with, '|'); ++i) {
968                         extract(node, shared_with, i);
969                         if (strlen(node) > 0) {
970                                 wprintf("%s ", node);
971                                 wprintf("<A HREF=\"/netedit&cmd=remove&line="
972                                         "ignet_push_share|");
973                                 urlescputs(node);
974                                 wprintf("&tab=sharing\">(unshare)</A><BR>");
975                         }
976                 }
977
978                 wprintf("</TD><TD>\n");
979
980                 for (i=0; i<num_tokens(not_shared_with, '|'); ++i) {
981                         extract(node, not_shared_with, i);
982                         if (strlen(node) > 0) {
983                                 wprintf("%s ", node);
984                                 wprintf("<A HREF=\"/netedit&cmd=add&line="
985                                         "ignet_push_share|");
986                                 urlescputs(node);
987                                 wprintf("&tab=sharing\">(share)</A><BR>");
988                         }
989                 }
990
991                 wprintf("</TD></TR>"
992                         "</TABLE><BR>\n"
993                         "<I><B>Reminder:</B> When sharing a room, "
994                         "it must be shared from both ends.  Adding a node to "
995                         "the 'shared' list sends messages out, but in order to"
996                         " receive messages, the other nodes must be configured"
997                         " to send messages out to your system as well.</I><BR>"
998                         "</CENTER>\n");
999
1000         }
1001
1002         /* Mailing list management */
1003         if (!strcmp(tab, "listserv")) {
1004
1005                 wprintf("<BR><center>"
1006                         "<TABLE BORDER=0 WIDTH=100%% CELLPADDING=5>"
1007                         "<TR><TD VALIGN=TOP>");
1008
1009                 wprintf("<i>The contents of this room are being "
1010                         "mailed <b>as individual messages</b> "
1011                         "to the following list recipients:"
1012                         "</i><br><br>\n");
1013
1014                 serv_puts("GNET");
1015                 serv_gets(buf);
1016                 if (buf[0]=='1') while (serv_gets(buf), strcmp(buf, "000")) {
1017                         extract(cmd, buf, 0);
1018                         if (!strcasecmp(cmd, "listrecp")) {
1019                                 extract(recp, buf, 1);
1020                         
1021                                 escputs(recp);
1022                                 wprintf(" <A HREF=\"/netedit&cmd=remove&line="
1023                                         "listrecp|");
1024                                 urlescputs(recp);
1025                                 wprintf("&tab=listserv\">(remove)</A><BR>");
1026
1027                         }
1028                 }
1029                 wprintf("<BR><FORM METHOD=\"POST\" ACTION=\"/netedit\">\n"
1030                         "<INPUT TYPE=\"hidden\" NAME=\"tab\" VALUE=\"listserv\">\n"
1031                         "<INPUT TYPE=\"hidden\" NAME=\"prefix\" VALUE=\"listrecp|\">\n");
1032                 wprintf("<INPUT TYPE=\"text\" NAME=\"line\">\n");
1033                 wprintf("<INPUT TYPE=\"submit\" NAME=\"cmd\" VALUE=\"Add\">");
1034                 wprintf("</FORM>\n");
1035
1036                 wprintf("</TD><TD VALIGN=TOP>\n");
1037                 
1038                 wprintf("<i>The contents of this room are being "
1039                         "mailed <b>in digest form</b> "
1040                         "to the following list recipients:"
1041                         "</i><br><br>\n");
1042
1043                 serv_puts("GNET");
1044                 serv_gets(buf);
1045                 if (buf[0]=='1') while (serv_gets(buf), strcmp(buf, "000")) {
1046                         extract(cmd, buf, 0);
1047                         if (!strcasecmp(cmd, "digestrecp")) {
1048                                 extract(recp, buf, 1);
1049                         
1050                                 escputs(recp);
1051                                 wprintf(" <A HREF=\"/netedit&cmd=remove&line="
1052                                         "digestrecp|");
1053                                 urlescputs(recp);
1054                                 wprintf("&tab=listserv\">(remove)</A><BR>");
1055
1056                         }
1057                 }
1058                 wprintf("<BR><FORM METHOD=\"POST\" ACTION=\"/netedit\">\n"
1059                         "<INPUT TYPE=\"hidden\" NAME=\"tab\" VALUE=\"listserv\">\n"
1060                         "<INPUT TYPE=\"hidden\" NAME=\"prefix\" VALUE=\"digestrecp|\">\n");
1061                 wprintf("<INPUT TYPE=\"text\" NAME=\"line\">\n");
1062                 wprintf("<INPUT TYPE=\"submit\" NAME=\"cmd\" VALUE=\"Add\">");
1063                 wprintf("</FORM>\n");
1064                 
1065                 wprintf("</TD></TR></TABLE><HR>\n");
1066
1067                 if (self_service(999) == 1) {
1068                         wprintf("This room is configured to allow "
1069                                 "self-service subscribe/unsubscribe requests."
1070                                 " <A HREF=\"/toggle_self_service?newval=0&"
1071                                 "tab=listserv\">"
1072                                 "Click to disable.</A><BR>\n"
1073                                 "The URL for subscribe/unsubscribe is: "
1074                                 "<TT>http://%s/listsub</TT><BR>\n",
1075                                 WC->http_host
1076                         );
1077                 }
1078                 else {
1079                         wprintf("This room is <i>not</i> configured to allow "
1080                                 "self-service subscribe/unsubscribe requests."
1081                                 " <A HREF=\"/toggle_self_service?newval=1&"
1082                                 "tab=listserv\">"
1083                                 "Click to enable.</A><BR>\n"
1084                         );
1085                 }
1086
1087
1088                 wprintf("</CENTER>\n");
1089         }
1090
1091
1092         /* Mailing list management */
1093         if (!strcmp(tab, "expire")) {
1094
1095                 serv_puts("GPEX room");
1096                 serv_gets(buf);
1097                 if (buf[0] == '2') {
1098                         roompolicy = extract_int(&buf[4], 0);
1099                         roomvalue = extract_int(&buf[4], 1);
1100                 }
1101                 
1102                 serv_puts("GPEX floor");
1103                 serv_gets(buf);
1104                 if (buf[0] == '2') {
1105                         floorpolicy = extract_int(&buf[4], 0);
1106                         floorvalue = extract_int(&buf[4], 1);
1107                 }
1108                 
1109                 wprintf("<BR><FORM METHOD=\"POST\" ACTION=\"/set_room_policy\">\n");
1110                 wprintf("<TABLE border=0 cellspacing=5>\n");
1111                 wprintf("<TR><TD>Message expire policy for this room<BR>(");
1112                 escputs(WC->wc_roomname);
1113                 wprintf(")</TD><TD>");
1114                 wprintf("<INPUT TYPE=\"radio\" NAME=\"roompolicy\" VALUE=\"0\" %s>",
1115                         ((roompolicy == 0) ? "CHECKED" : "") );
1116                 wprintf("Use the default policy for this floor<BR>\n");
1117                 wprintf("<INPUT TYPE=\"radio\" NAME=\"roompolicy\" VALUE=\"1\" %s>",
1118                         ((roompolicy == 1) ? "CHECKED" : "") );
1119                 wprintf("Never automatically expire messages<BR>\n");
1120                 wprintf("<INPUT TYPE=\"radio\" NAME=\"roompolicy\" VALUE=\"2\" %s>",
1121                         ((roompolicy == 2) ? "CHECKED" : "") );
1122                 wprintf("Expire by message count<BR>\n");
1123                 wprintf("<INPUT TYPE=\"radio\" NAME=\"roompolicy\" VALUE=\"3\" %s>",
1124                         ((roompolicy == 3) ? "CHECKED" : "") );
1125                 wprintf("Expire by message age<BR>");
1126                 wprintf("Number of messages or days: ");
1127                 wprintf("<INPUT TYPE=\"text\" NAME=\"roomvalue\" MAXLENGTH=\"5\" VALUE=\"%d\">", roomvalue);
1128                 wprintf("</TD></TR>\n");
1129
1130                 if (WC->axlevel >= 6) {
1131                         wprintf("<TR><TD COLSPAN=2><HR></TD></TR>\n");
1132                         wprintf("<TR><TD>Message expire policy for this floor<BR>(");
1133                         escputs(floorlist[WC->wc_floor]);
1134                         wprintf(")</TD><TD>");
1135                         wprintf("<INPUT TYPE=\"radio\" NAME=\"floorpolicy\" VALUE=\"0\" %s>",
1136                                 ((floorpolicy == 0) ? "CHECKED" : "") );
1137                         wprintf("Use the system default<BR>\n");
1138                         wprintf("<INPUT TYPE=\"radio\" NAME=\"floorpolicy\" VALUE=\"1\" %s>",
1139                                 ((floorpolicy == 1) ? "CHECKED" : "") );
1140                         wprintf("Never automatically expire messages<BR>\n");
1141                         wprintf("<INPUT TYPE=\"radio\" NAME=\"floorpolicy\" VALUE=\"2\" %s>",
1142                                 ((floorpolicy == 2) ? "CHECKED" : "") );
1143                         wprintf("Expire by message count<BR>\n");
1144                         wprintf("<INPUT TYPE=\"radio\" NAME=\"floorpolicy\" VALUE=\"3\" %s>",
1145                                 ((floorpolicy == 3) ? "CHECKED" : "") );
1146                         wprintf("Expire by message age<BR>");
1147                         wprintf("Number of messages or days: ");
1148                         wprintf("<INPUT TYPE=\"text\" NAME=\"floorvalue\" MAXLENGTH=\"5\" VALUE=\"%d\">",
1149                                 floorvalue);
1150                 }
1151
1152                 wprintf("<CENTER>\n");
1153                 wprintf("<TR><TD COLSPAN=2><HR><CENTER>\n");
1154                 wprintf("<INPUT TYPE=\"submit\" NAME=\"sc\" VALUE=\"OK\">");
1155                 wprintf("&nbsp;");
1156                 wprintf("<INPUT TYPE=\"submit\" NAME=\"sc\" VALUE=\"Cancel\">");
1157                 wprintf("</CENTER></TD><TR>\n");
1158
1159                 wprintf("</TABLE>\n"
1160                         "<INPUT TYPE=\"hidden\" NAME=\"tab\" VALUE=\"expire\">\n"
1161                         "</FORM>\n"
1162                 );
1163
1164         }
1165
1166
1167         /* end content of whatever tab is open now */
1168         wprintf("</TD></TR></TABLE>\n");
1169
1170         wDumpContent(1);
1171 }
1172
1173
1174 /* 
1175  * Toggle self-service list subscription
1176  */
1177 void toggle_self_service(void) {
1178         int newval = 0;
1179
1180         newval = atoi(bstr("newval"));
1181         self_service(newval);
1182         display_editroom();
1183 }
1184
1185
1186
1187 /*
1188  * save new parameters for a room
1189  */
1190 void editroom(void)
1191 {
1192         char buf[SIZ];
1193         char er_name[20];
1194         char er_password[10];
1195         char er_dirname[15];
1196         char er_roomaide[26];
1197         int er_floor;
1198         unsigned er_flags;
1199         int bump;
1200
1201
1202         if (strcmp(bstr("sc"), "OK")) {
1203                 strcpy(WC->ImportantMessage,
1204                         "Cancelled.  Changes were not saved.");
1205                 display_editroom();
1206                 return;
1207         }
1208         serv_puts("GETR");
1209         serv_gets(buf);
1210
1211         if (buf[0] != '2') {
1212                 strcpy(WC->ImportantMessage, &buf[4]);
1213                 display_editroom();
1214                 return;
1215         }
1216         extract(er_name, &buf[4], 0);
1217         extract(er_password, &buf[4], 1);
1218         extract(er_dirname, &buf[4], 2);
1219         er_flags = extract_int(&buf[4], 3);
1220
1221         strcpy(er_roomaide, bstr("er_roomaide"));
1222         if (strlen(er_roomaide) == 0) {
1223                 serv_puts("GETA");
1224                 serv_gets(buf);
1225                 if (buf[0] != '2') {
1226                         strcpy(er_roomaide, "");
1227                 } else {
1228                         extract(er_roomaide, &buf[4], 0);
1229                 }
1230         }
1231         strcpy(buf, bstr("er_name"));
1232         buf[20] = 0;
1233         if (strlen(buf) > 0)
1234                 strcpy(er_name, buf);
1235
1236         strcpy(buf, bstr("er_password"));
1237         buf[10] = 0;
1238         if (strlen(buf) > 0)
1239                 strcpy(er_password, buf);
1240
1241         strcpy(buf, bstr("er_dirname"));
1242         buf[15] = 0;
1243         if (strlen(buf) > 0)
1244                 strcpy(er_dirname, buf);
1245
1246         strcpy(buf, bstr("type"));
1247         er_flags &= !(QR_PRIVATE | QR_PASSWORDED | QR_GUESSNAME);
1248
1249         if (!strcmp(buf, "invonly")) {
1250                 er_flags |= (QR_PRIVATE);
1251         }
1252         if (!strcmp(buf, "guessname")) {
1253                 er_flags |= (QR_PRIVATE | QR_GUESSNAME);
1254         }
1255         if (!strcmp(buf, "passworded")) {
1256                 er_flags |= (QR_PRIVATE | QR_PASSWORDED);
1257         }
1258         if (!strcmp(bstr("prefonly"), "yes")) {
1259                 er_flags |= QR_PREFONLY;
1260         } else {
1261                 er_flags &= ~QR_PREFONLY;
1262         }
1263
1264         if (!strcmp(bstr("readonly"), "yes")) {
1265                 er_flags |= QR_READONLY;
1266         } else {
1267                 er_flags &= ~QR_READONLY;
1268         }
1269
1270         if (!strcmp(bstr("permanent"), "yes")) {
1271                 er_flags |= QR_PERMANENT;
1272         } else {
1273                 er_flags &= ~QR_PERMANENT;
1274         }
1275
1276         if (!strcmp(bstr("network"), "yes")) {
1277                 er_flags |= QR_NETWORK;
1278         } else {
1279                 er_flags &= ~QR_NETWORK;
1280         }
1281
1282         if (!strcmp(bstr("directory"), "yes")) {
1283                 er_flags |= QR_DIRECTORY;
1284         } else {
1285                 er_flags &= ~QR_DIRECTORY;
1286         }
1287
1288         if (!strcmp(bstr("ulallowed"), "yes")) {
1289                 er_flags |= QR_UPLOAD;
1290         } else {
1291                 er_flags &= ~QR_UPLOAD;
1292         }
1293
1294         if (!strcmp(bstr("dlallowed"), "yes")) {
1295                 er_flags |= QR_DOWNLOAD;
1296         } else {
1297                 er_flags &= ~QR_DOWNLOAD;
1298         }
1299
1300         if (!strcmp(bstr("visdir"), "yes")) {
1301                 er_flags |= QR_VISDIR;
1302         } else {
1303                 er_flags &= ~QR_VISDIR;
1304         }
1305
1306         strcpy(buf, bstr("anon"));
1307
1308         er_flags &= ~(QR_ANONONLY | QR_ANONOPT);
1309         if (!strcmp(buf, "anononly"))
1310                 er_flags |= QR_ANONONLY;
1311         if (!strcmp(buf, "anon2"))
1312                 er_flags |= QR_ANONOPT;
1313
1314         bump = 0;
1315         if (!strcmp(bstr("bump"), "yes"))
1316                 bump = 1;
1317
1318         er_floor = atoi(bstr("er_floor"));
1319
1320         sprintf(buf, "SETR %s|%s|%s|%u|%d|%d",
1321              er_name, er_password, er_dirname, er_flags, bump, er_floor);
1322         serv_puts(buf);
1323         serv_gets(buf);
1324         if (buf[0] != '2') {
1325                 strcpy(WC->ImportantMessage, &buf[4]);
1326                 display_editroom();
1327                 return;
1328         }
1329         gotoroom(er_name);
1330
1331         if (strlen(er_roomaide) > 0) {
1332                 sprintf(buf, "SETA %s", er_roomaide);
1333                 serv_puts(buf);
1334                 serv_gets(buf);
1335                 if (buf[0] != '2') {
1336                         strcpy(WC->ImportantMessage, &buf[4]);
1337                         display_main_menu();
1338                         return;
1339                 }
1340         }
1341         gotoroom(er_name);
1342         strcpy(WC->ImportantMessage, "Your changes have been saved.");
1343         display_editroom();
1344         return;
1345 }
1346
1347 /*
1348  * Invite, Kick, and show Who Knows a room
1349  */
1350 void display_whok(void)
1351 {
1352         char buf[SIZ], room[SIZ], username[SIZ];
1353
1354         serv_puts("GETR");
1355         serv_gets(buf);
1356
1357         if (buf[0] != '2') {
1358                 strcpy(WC->ImportantMessage, &buf[4]);
1359                 display_main_menu();
1360                 return;
1361         }
1362         extract(room, &buf[4], 0);
1363
1364         strcpy(username, bstr("username"));
1365
1366         if(!strcmp(bstr("sc"), "Kick")) {
1367                 sprintf(buf, "KICK %s", username);
1368                 serv_puts(buf);
1369                 serv_gets(buf);
1370
1371                 if (buf[0] != '2') {
1372                         strcpy(WC->ImportantMessage, &buf[4]);
1373                 } else {
1374                         sprintf(WC->ImportantMessage,
1375                                 "<B><I>User %s kicked out of room %s.</I></B>\n", 
1376                                 username, room);
1377                 }
1378         } else if(!strcmp(bstr("sc"), "Invite")) {
1379                 sprintf(buf, "INVT %s", username);
1380                 serv_puts(buf);
1381                 serv_gets(buf);
1382
1383                 if (buf[0] != '2') {
1384                         strcpy(WC->ImportantMessage, &buf[4]);
1385                 } else {
1386                         sprintf(WC->ImportantMessage,
1387                                 "<B><I>User %s invited to room %s.</I></B>\n", 
1388                                 username, room);
1389                 }
1390         }
1391         
1392         output_headers(1);
1393         stresc(buf, WC->wc_roomname, 1, 1);
1394         svprintf("BOXTITLE", WCS_STRING, "Access control list for %s", buf);
1395         do_template("beginbox");
1396
1397         wprintf("<TABLE border=0 CELLSPACING=10><TR VALIGN=TOP>"
1398                 "<TD>The users listed below have access to this room.  "
1399                 "To remove a user from the access list, select the user "
1400                 "name from the list and click 'Kick'.<BR><BR>");
1401         
1402         wprintf("<CENTER><FORM METHOD=\"POST\" ACTION=\"/display_whok\">\n");
1403         wprintf("<SELECT NAME=\"username\" SIZE=10>\n");
1404         serv_puts("WHOK");
1405         serv_gets(buf);
1406         if (buf[0] == '1') {
1407                 while (serv_gets(buf), strcmp(buf, "000")) {
1408                         extract(username, buf, 0);
1409                         wprintf("<OPTION>");
1410                         escputs(username);
1411                         wprintf("\n");
1412                 }
1413         }
1414         wprintf("</SELECT><BR>\n");
1415
1416         wprintf("<input type=submit name=sc value=\"Kick\">");
1417         wprintf("</FORM></CENTER>\n");
1418
1419         wprintf("</TD><TD>"
1420                 "To grant another user access to this room, enter the "
1421                 "user name in the box below and click 'Invite'.<BR><BR>");
1422
1423         wprintf("<CENTER><FORM METHOD=\"POST\" ACTION=\"/display_whok\">\n");
1424         wprintf("Invite: ");
1425         wprintf("<input type=text name=username><BR>\n"
1426                 "<input type=hidden name=sc value=\"Invite\">"
1427                 "<input type=submit value=\"Invite\">"
1428                 "</FORM></CENTER>\n");
1429
1430         wprintf("</TD></TR></TABLE>\n");
1431         do_template("endbox");
1432         wDumpContent(1);
1433 }
1434
1435
1436
1437 /*
1438  * display the form for entering a new room
1439  */
1440 void display_entroom(void)
1441 {
1442         int i;
1443         char buf[SIZ];
1444
1445         serv_puts("CRE8 0");
1446         serv_gets(buf);
1447
1448         if (buf[0] != '2') {
1449                 strcpy(WC->ImportantMessage, &buf[4]);
1450                 display_main_menu();
1451                 return;
1452         }
1453         output_headers(3);
1454         svprintf("BOXTITLE", WCS_STRING, "Create a new room");
1455         do_template("beginbox");
1456
1457         wprintf("<FORM METHOD=\"POST\" ACTION=\"/entroom\">\n");
1458
1459         wprintf("<UL><LI>Name of room: ");
1460         wprintf("<INPUT TYPE=\"text\" NAME=\"er_name\" MAXLENGTH=\"127\">\n");
1461
1462         wprintf("<LI>Resides on floor: ");
1463         load_floorlist(); 
1464         wprintf("<SELECT NAME=\"er_floor\" SIZE=\"1\">\n");
1465         for (i = 0; i < 128; ++i)
1466                 if (strlen(floorlist[i]) > 0) {
1467                         wprintf("<OPTION ");
1468                         wprintf("VALUE=\"%d\">", i);
1469                         escputs(floorlist[i]);
1470                         wprintf("</OPTION>\n");
1471                 }
1472         wprintf("</SELECT>\n");
1473
1474         wprintf("<LI>Type of room:<UL>\n");
1475
1476         wprintf("<LI><INPUT TYPE=\"radio\" NAME=\"type\" VALUE=\"public\" ");
1477         wprintf("CHECKED > Public room\n");
1478
1479         wprintf("<LI><INPUT TYPE=\"radio\" NAME=\"type\" VALUE=\"guessname\" ");
1480         wprintf("> Private - guess name\n");
1481
1482         wprintf("<LI><INPUT TYPE=\"radio\" NAME=\"type\" VALUE=\"passworded\" ");
1483         wprintf("> Private - require password:\n");
1484         wprintf("<INPUT TYPE=\"text\" NAME=\"er_password\" MAXLENGTH=\"9\">\n");
1485
1486         wprintf("<LI><INPUT TYPE=\"radio\" NAME=\"type\" VALUE=\"invonly\" ");
1487         wprintf("> Private - invitation only\n");
1488
1489         wprintf("<LI><INPUT TYPE=\"radio\" NAME=\"type\" VALUE=\"personal\" ");
1490         wprintf("> Personal (mailbox for you only)\n");
1491         wprintf("</UL>\n");
1492
1493         wprintf("<LI>Default view for room: "); /* FOO */
1494         wprintf("<SELECT NAME=\"er_view\" SIZE=\"1\">\n");
1495         for (i=0; i<(sizeof viewdefs / sizeof (char *)); ++i) {
1496                 wprintf("<OPTION %s VALUE=\"%d\">",
1497                         ((i == 0) ? "SELECTED" : ""), i );
1498                 escputs(viewdefs[i]);
1499                 wprintf("</OPTION>\n");
1500         }
1501         wprintf("</SELECT>\n");
1502
1503         wprintf("<CENTER>\n");
1504         wprintf("<INPUT TYPE=\"submit\" NAME=\"sc\" VALUE=\"OK\">");
1505         wprintf("&nbsp;");
1506         wprintf("<INPUT TYPE=\"submit\" NAME=\"sc\" VALUE=\"Cancel\">");
1507         wprintf("</CENTER>\n");
1508         wprintf("</FORM>\n<HR>");
1509         serv_printf("MESG roomaccess");
1510         serv_gets(buf);
1511         if (buf[0] == '1') {
1512                 fmout(NULL, "CENTER");
1513         }
1514         do_template("endbox");
1515         wDumpContent(1);
1516 }
1517
1518
1519
1520
1521 /*
1522  * support function for entroom() -- sets the default view 
1523  */
1524 void er_set_default_view(int newview) {
1525
1526         char buf[SIZ];
1527
1528         char rm_name[SIZ];
1529         char rm_pass[SIZ];
1530         char rm_dir[SIZ];
1531         int rm_bits1;
1532         int rm_floor;
1533         int rm_listorder;
1534         int rm_bits2;
1535
1536         serv_puts("GETR");
1537         serv_gets(buf);
1538         if (buf[0] != '2') return;
1539
1540         extract(rm_name, &buf[4], 0);
1541         extract(rm_pass, &buf[4], 1);
1542         extract(rm_dir, &buf[4], 2);
1543         rm_bits1 = extract_int(&buf[4], 3);
1544         rm_floor = extract_int(&buf[4], 4);
1545         rm_listorder = extract_int(&buf[4], 5);
1546         rm_bits2 = extract_int(&buf[4], 7);
1547
1548         serv_printf("SETR %s|%s|%s|%d|0|%d|%d|%d|%d",
1549                 rm_name, rm_pass, rm_dir, rm_bits1, rm_floor,
1550                 rm_listorder, newview, rm_bits2
1551         );
1552         serv_gets(buf);
1553 }
1554
1555
1556
1557 /*
1558  * enter a new room
1559  */
1560 void entroom(void)
1561 {
1562         char buf[SIZ];
1563         char er_name[SIZ];
1564         char er_type[SIZ];
1565         char er_password[SIZ];
1566         int er_floor;
1567         int er_num_type;
1568         int er_view;
1569
1570         if (strcmp(bstr("sc"), "OK")) {
1571                 strcpy(WC->ImportantMessage,
1572                         "Cancelled.  No new room was created.");
1573                 display_main_menu();
1574                 return;
1575         }
1576         strcpy(er_name, bstr("er_name"));
1577         strcpy(er_type, bstr("type"));
1578         strcpy(er_password, bstr("er_password"));
1579         er_floor = atoi(bstr("er_floor"));
1580         er_view = atoi(bstr("er_view"));
1581
1582         er_num_type = 0;
1583         if (!strcmp(er_type, "guessname"))
1584                 er_num_type = 1;
1585         if (!strcmp(er_type, "passworded"))
1586                 er_num_type = 2;
1587         if (!strcmp(er_type, "invonly"))
1588                 er_num_type = 3;
1589         if (!strcmp(er_type, "personal"))
1590                 er_num_type = 4;
1591
1592         sprintf(buf, "CRE8 1|%s|%d|%s|%d|%d|%d", 
1593                 er_name, er_num_type, er_password, er_floor, 0, er_view);
1594         serv_puts(buf);
1595         serv_gets(buf);
1596         if (buf[0] != '2') {
1597                 strcpy(WC->ImportantMessage, &buf[4]);
1598                 display_main_menu();
1599                 return;
1600         }
1601         gotoroom(er_name);
1602         do_change_view(er_view);                /* Now go there */
1603 }
1604
1605
1606 /*
1607  * display the screen to enter a private room
1608  */
1609 void display_private(char *rname, int req_pass)
1610 {
1611
1612         output_headers(3);
1613
1614         svprintf("BOXTITLE", WCS_STRING, "Go to a hidden room");
1615         do_template("beginbox");
1616
1617         wprintf("<CENTER>\n");
1618         wprintf("<BR>If you know the name of a hidden (guess-name) or\n");
1619         wprintf("passworded room, you can enter that room by typing\n");
1620         wprintf("its name below.  Once you gain access to a private\n");
1621         wprintf("room, it will appear in your regular room listings\n");
1622         wprintf("so you don't have to keep returning here.\n");
1623         wprintf("<BR><BR>");
1624
1625         wprintf("<FORM METHOD=\"GET\" ACTION=\"/goto_private\">\n");
1626
1627         wprintf("<table border=\"0\" cellspacing=\"5\" "
1628                 "cellpadding=\"5\" BGCOLOR=\"#EEEEEE\">\n"
1629                 "<TR><TD>"
1630                 "Enter room name:</TD><TD>"
1631                 "<INPUT TYPE=\"text\" NAME=\"gr_name\" "
1632                 "VALUE=\"%s\" MAXLENGTH=\"19\">\n", rname);
1633
1634         if (req_pass) {
1635                 wprintf("</TD></TR><TR><TD>");
1636                 wprintf("Enter room password:</TD><TD>");
1637                 wprintf("<INPUT TYPE=\"password\" NAME=\"gr_pass\" MAXLENGTH=\"9\">\n");
1638         }
1639         wprintf("</TD></TR></TABLE><BR>\n");
1640
1641         wprintf("<INPUT TYPE=\"submit\" NAME=\"sc\" VALUE=\"OK\">"
1642                 "&nbsp;"
1643                 "<INPUT TYPE=\"submit\" NAME=\"sc\" VALUE=\"Cancel\">");
1644         wprintf("</FORM>\n");
1645         do_template("endbox");
1646         wDumpContent(1);
1647 }
1648
1649 /* 
1650  * goto a private room
1651  */
1652 void goto_private(void)
1653 {
1654         char hold_rm[SIZ];
1655         char buf[SIZ];
1656
1657         if (strcasecmp(bstr("sc"), "OK")) {
1658                 display_main_menu();
1659                 return;
1660         }
1661         strcpy(hold_rm, WC->wc_roomname);
1662         strcpy(buf, "GOTO ");
1663         strcat(buf, bstr("gr_name"));
1664         strcat(buf, "|");
1665         strcat(buf, bstr("gr_pass"));
1666         serv_puts(buf);
1667         serv_gets(buf);
1668
1669         if (buf[0] == '2') {
1670                 smart_goto(bstr("gr_name"));
1671                 return;
1672         }
1673         if (!strncmp(buf, "540", 3)) {
1674                 display_private(bstr("gr_name"), 1);
1675                 return;
1676         }
1677         output_headers(1);
1678         wprintf("%s\n", &buf[4]);
1679         wDumpContent(1);
1680         return;
1681 }
1682
1683
1684 /*
1685  * display the screen to zap a room
1686  */
1687 void display_zap(void)
1688 {
1689         output_headers(1);
1690
1691         wprintf("<TABLE WIDTH=100%% BORDER=0 BGCOLOR=\"#770000\"><TR><TD>");
1692         wprintf("<SPAN CLASS=\"titlebar\">Zap (forget) the current room</SPAN>\n");
1693         wprintf("</TD></TR></TABLE>\n");
1694
1695         wprintf("If you select this option, <em>%s</em> will ", WC->wc_roomname);
1696         wprintf("disappear from your room list.  Is this what you wish ");
1697         wprintf("to do?<BR>\n");
1698
1699         wprintf("<FORM METHOD=\"GET\" ACTION=\"/zap\">\n");
1700         wprintf("<INPUT TYPE=\"submit\" NAME=\"sc\" VALUE=\"OK\">");
1701         wprintf("&nbsp;");
1702         wprintf("<INPUT TYPE=\"submit\" NAME=\"sc\" VALUE=\"Cancel\">");
1703         wprintf("</FORM>\n");
1704         wDumpContent(1);
1705 }
1706
1707
1708 /* 
1709  * zap a room
1710  */
1711 void zap(void)
1712 {
1713         char buf[SIZ];
1714         char final_destination[SIZ];
1715
1716         /* If the forget-room routine fails for any reason, we fall back
1717          * to the current room; otherwise, we go to the Lobby
1718          */
1719         strcpy(final_destination, WC->wc_roomname);
1720
1721         if (!strcasecmp(bstr("sc"), "OK")) {
1722                 serv_printf("GOTO %s", WC->wc_roomname);
1723                 serv_gets(buf);
1724                 if (buf[0] != '2') {
1725                         /* ExpressMessageCat(&buf[4]); */
1726                 } else {
1727                         serv_puts("FORG");
1728                         serv_gets(buf);
1729                         if (buf[0] != '2') {
1730                                 /* ExpressMessageCat(&buf[4]); */
1731                         } else {
1732                                 strcpy(final_destination, "_BASEROOM_");
1733                         }
1734                 }
1735         }
1736         smart_goto(final_destination);
1737 }
1738
1739
1740
1741
1742 /*
1743  * Confirm deletion of the current room
1744  */
1745 void confirm_delete_room(void)
1746 {
1747         char buf[SIZ];
1748
1749         serv_puts("KILL 0");
1750         serv_gets(buf);
1751         if (buf[0] != '2') {
1752                 strcpy(WC->ImportantMessage, &buf[4]);
1753                 display_main_menu();
1754                 return;
1755         }
1756         output_headers(1);
1757         wprintf("<TABLE WIDTH=100%% BORDER=0 BGCOLOR=\"#770000\"><TR><TD>");
1758         wprintf("<SPAN CLASS=\"titlebar\">Confirm deletion of room</SPAN>\n");
1759         wprintf("</TD></TR></TABLE>\n");
1760
1761         wprintf("<CENTER>");
1762         wprintf("<FORM METHOD=\"GET\" ACTION=\"/delete_room\">\n");
1763
1764         wprintf("Are you sure you want to delete <FONT SIZE=+1>");
1765         escputs(WC->wc_roomname);
1766         wprintf("</FONT>?<BR>\n");
1767
1768         wprintf("<INPUT TYPE=\"submit\" NAME=\"sc\" VALUE=\"Delete\">");
1769         wprintf("<INPUT TYPE=\"submit\" NAME=\"sc\" VALUE=\"Cancel\">");
1770
1771         wprintf("</FORM></CENTER>\n");
1772         wDumpContent(1);
1773 }
1774
1775
1776 /*
1777  * Delete the current room
1778  */
1779 void delete_room(void)
1780 {
1781         char buf[SIZ];
1782         char sc[SIZ];
1783
1784         strcpy(sc, bstr("sc"));
1785
1786         if (strcasecmp(sc, "Delete")) {
1787                 strcpy(WC->ImportantMessage,
1788                         "Cancelled.  This room was not deleted.");
1789                 display_main_menu();
1790                 return;
1791         }
1792         serv_puts("KILL 1");
1793         serv_gets(buf);
1794         if (buf[0] != '2') {
1795                 strcpy(WC->ImportantMessage, &buf[4]);
1796                 display_main_menu();
1797                 return;
1798         } else {
1799                 smart_goto("_BASEROOM_");
1800         }
1801 }
1802
1803
1804
1805 /*
1806  * Perform changes to a room's network configuration
1807  */
1808 void netedit(void) {
1809         FILE *fp;
1810         char buf[SIZ];
1811         char line[SIZ];
1812
1813         if (strlen(bstr("line"))==0) {
1814                 display_editroom();
1815                 return;
1816         }
1817
1818         strcpy(line, bstr("prefix"));
1819         strcat(line, bstr("line"));
1820         strcat(line, bstr("suffix"));
1821
1822         fp = tmpfile();
1823         if (fp == NULL) {
1824                 display_editroom();
1825                 return;
1826         }
1827
1828         serv_puts("GNET");
1829         serv_gets(buf);
1830         if (buf[0] != '1') {
1831                 fclose(fp);
1832                 display_editroom();
1833                 return;
1834         }
1835
1836         /* This loop works for add *or* remove.  Spiffy, eh? */
1837         while (serv_gets(buf), strcmp(buf, "000")) {
1838                 if (strcasecmp(buf, line)) {
1839                         fprintf(fp, "%s\n", buf);
1840                 }
1841         }
1842
1843         rewind(fp);
1844         serv_puts("SNET");
1845         serv_gets(buf);
1846         if (buf[0] != '4') {
1847                 fclose(fp);
1848                 display_editroom();
1849                 return;
1850         }
1851
1852         while (fgets(buf, sizeof buf, fp) != NULL) {
1853                 buf[strlen(buf)-1] = 0;
1854                 serv_puts(buf);
1855         }
1856
1857         if (!strcasecmp(bstr("cmd"), "add")) {
1858                 serv_puts(line);
1859         }
1860
1861         serv_puts("000");
1862         fclose(fp);
1863         display_editroom();
1864 }
1865
1866
1867
1868 /*
1869  * Convert a room name to a folder-ish-looking name.
1870  */
1871 void room_to_folder(char *folder, char *room, int floor, int is_mailbox)
1872 {
1873         int i;
1874
1875         /*
1876          * For mailboxes, just do it straight...
1877          */
1878         if (is_mailbox) {
1879                 sprintf(folder, "My folders|%s", room);
1880         }
1881
1882         /*
1883          * Otherwise, prefix the floor name as a "public folders" moniker
1884          */
1885         else {
1886                 sprintf(folder, "%s|%s", floorlist[floor], room);
1887         }
1888
1889         /*
1890          * Replace "\" characters with "|" for pseudo-folder-delimiting
1891          */
1892         for (i=0; i<strlen(folder); ++i) {
1893                 if (folder[i] == '\\') folder[i] = '|';
1894         }
1895 }
1896
1897
1898
1899
1900 /*
1901  * Back end for change_view()
1902  */
1903 void do_change_view(int newview) {
1904         char buf[SIZ];
1905
1906         serv_printf("VIEW %d", newview);
1907         serv_gets(buf);
1908         WC->wc_view = newview;
1909         smart_goto(WC->wc_roomname);
1910 }
1911
1912
1913
1914 /*
1915  * Change the view for this room
1916  */
1917 void change_view(void) {
1918         int view;
1919
1920         view = atol(bstr("view"));
1921         do_change_view(view);
1922 }
1923
1924
1925 /*
1926  * One big expanded tree list view --- like a folder list
1927  */
1928 void do_folder_view(struct folder *fold, int max_folders, int num_floors) {
1929         char buf[SIZ];
1930         int levels, oldlevels;
1931         int i, t;
1932         int actnum = 0;
1933         int has_subfolders = 0;
1934
1935         /* Include the menu expanding/collapsing code */
1936         wprintf("<script type=\"text/javascript\" src=\"/static/menuExpandable3.js\"></script>\n");
1937
1938         do_template("beginbox_nt");
1939         wprintf("<div id=\"mainMenu\">\n");
1940         wprintf("<UL id=\"menuList\">\n");
1941         levels = 0;
1942         oldlevels = 0;
1943
1944         for (i=0; i<max_folders; ++i) {
1945
1946                 has_subfolders = 0;
1947                 if ((i+1) < max_folders) {
1948                         if ( (!strncasecmp(fold[i].name, fold[i+1].name, strlen(fold[i].name)))
1949                            && (fold[i+1].name[strlen(fold[i].name)] == '|') ) {
1950                                 has_subfolders = 1;
1951                         }
1952                 }
1953
1954                 levels = num_tokens(fold[i].name, '|');
1955
1956                 if ( (levels < oldlevels) || ((levels==1)&&(i!=0)) ) {
1957                         for (t=0; t<(oldlevels-levels); ++t) {
1958                                 wprintf("</UL>\n");
1959                         }
1960                 }
1961
1962                 if (has_subfolders) {
1963                         wprintf("<LI");
1964                         if (levels == 1) wprintf(" class=\"menubar\"");
1965                         wprintf(">");
1966                         wprintf("<A href=\"#\" id=\"actuator%d\" class=\"actuator\"></a>\n", actnum);
1967                 }
1968                 else {
1969                         wprintf("<LI>");
1970                 }
1971
1972                 if (fold[i].selectable) {
1973                         wprintf("<A HREF=\"/dotgoto?room=");
1974                         urlescputs(fold[i].room);
1975                         wprintf("\">");
1976                 }
1977
1978                 if (levels == 1) {
1979                         wprintf("<SPAN CLASS=\"roomlist_floor\">");
1980                 }
1981                 else if (fold[i].hasnewmsgs) {
1982                         wprintf("<SPAN CLASS=\"roomlist_new\">");
1983                 }
1984                 else {
1985                         wprintf("<SPAN CLASS=\"roomlist_old\">");
1986                 }
1987                 extract(buf, fold[i].name, levels-1);
1988                 escputs(buf);
1989                 wprintf("</SPAN>");
1990
1991                 if (!strcasecmp(fold[i].name, "My Folders|Mail")) {
1992                         wprintf(" (INBOX)");
1993                 }
1994
1995                 if (fold[i].selectable) {
1996                         wprintf("</A>");
1997                 }
1998                 wprintf("\n");
1999
2000                 if (has_subfolders) {
2001                         wprintf("<UL id=\"menu%d\" class=\"%s\">\n",
2002                                 actnum++,
2003                                 ( (levels == 1) ? "menu" : "submenu")
2004                         );
2005                 }
2006
2007                 oldlevels = levels;
2008         }
2009         wprintf("</UL></UL>\n");
2010         wprintf("<img src=\"/static/blank.gif\" onLoad = ' \n");
2011         for (i=0; i<actnum; ++i) {
2012                 wprintf(" initializeMenu(\"menu%d\", \"actuator%d\");\n", i, i);
2013         }
2014         wprintf(" ' > \n");
2015         wprintf("</DIV>\n");
2016         do_template("endbox");
2017 }
2018
2019 /*
2020  * Boxes and rooms and lists ... oh my!
2021  */
2022 void do_rooms_view(struct folder *fold, int max_folders, int num_floors) {
2023         char buf[SIZ];
2024         char boxtitle[SIZ];
2025         int levels, oldlevels;
2026         int i, t;
2027         int num_boxes = 0;
2028         static int columns = 3;
2029         int boxes_per_column = 0;
2030         int current_column = 0;
2031         int nf;
2032
2033         nf = num_floors;
2034         while (nf % columns != 0) ++nf;
2035         boxes_per_column = (nf / columns);
2036         if (boxes_per_column < 1) boxes_per_column = 1;
2037
2038         /* Outer table (for columnization) */
2039         wprintf("<TABLE BORDER=0 WIDTH=100%% CELLPADDING=5>"
2040                 "<TR><TD VALIGN=TOP>");
2041
2042         levels = 0;
2043         oldlevels = 0;
2044         for (i=0; i<max_folders; ++i) {
2045
2046                 levels = num_tokens(fold[i].name, '|');
2047
2048                 if ((levels == 1) && (oldlevels > 1)) {
2049
2050                         /* End inner box */
2051                         do_template("endbox");
2052
2053                         ++num_boxes;
2054                         if ((num_boxes % boxes_per_column) == 0) {
2055                                 ++current_column;
2056                                 if (current_column < columns) {
2057                                         wprintf("</TD><TD VALIGN=TOP>\n");
2058                                 }
2059                         }
2060                 }
2061
2062                 if (levels == 1) {
2063
2064                         /* Begin inner box */
2065                         extract(buf, fold[i].name, levels-1);
2066                         stresc(boxtitle, buf, 1, 0);
2067                         svprintf("BOXTITLE", WCS_STRING, boxtitle);
2068                         do_template("beginbox");
2069
2070                 }
2071
2072                 oldlevels = levels;
2073
2074                 if (levels > 1) {
2075                         wprintf("&nbsp;");
2076                         if (levels>2) for (t=0; t<(levels-2); ++t) wprintf("&nbsp;&nbsp;&nbsp;");
2077                         if (fold[i].selectable) {
2078                                 wprintf("<A HREF=\"/dotgoto?room=");
2079                                 urlescputs(fold[i].room);
2080                                 wprintf("\">");
2081                         }
2082                         else {
2083                                 wprintf("<i>");
2084                         }
2085                         if (fold[i].hasnewmsgs) {
2086                                 wprintf("<SPAN CLASS=\"roomlist_new\">");
2087                         }
2088                         else {
2089                                 wprintf("<SPAN CLASS=\"roomlist_old\">");
2090                         }
2091                         extract(buf, fold[i].name, levels-1);
2092                         escputs(buf);
2093                         wprintf("</SPAN>");
2094                         if (fold[i].selectable) {
2095                                 wprintf("</A>");
2096                         }
2097                         else {
2098                                 wprintf("</i>");
2099                         }
2100                         if (!strcasecmp(fold[i].name, "My Folders|Mail")) {
2101                                 wprintf(" (INBOX)");
2102                         }
2103                         wprintf("<BR>\n");
2104                 }
2105         }
2106         /* End the final inner box */
2107         do_template("endbox");
2108
2109         wprintf("</TD></TR></TABLE>\n");
2110 }
2111
2112
2113 /*
2114  * Show the room list.  (only should get called by
2115  * knrooms() because that's where output_headers() is called from)
2116  */
2117
2118 void list_all_rooms_by_floor(char *viewpref) {
2119         char buf[SIZ];
2120         int swap = 0;
2121         struct folder *fold = NULL;
2122         struct folder ftmp;
2123         int max_folders = 0;
2124         int alloc_folders = 0;
2125         int i, j;
2126         int ra_flags = 0;
2127         int flags = 0;
2128         int num_floors = 1;     /* add an extra one for private folders */
2129
2130         /* Start with the mailboxes */
2131         max_folders = 1;
2132         alloc_folders = 1;
2133         fold = malloc(sizeof(struct folder));
2134         memset(fold, 0, sizeof(struct folder));
2135         strcpy(fold[0].name, "My folders");
2136         fold[0].is_mailbox = 1;
2137
2138         /* Then add floors */
2139         serv_puts("LFLR");
2140         serv_gets(buf);
2141         if (buf[0]=='1') while(serv_gets(buf), strcmp(buf, "000")) {
2142                 if (max_folders >= alloc_folders) {
2143                         alloc_folders = max_folders + 100;
2144                         fold = realloc(fold,
2145                                 alloc_folders * sizeof(struct folder));
2146                 }
2147                 memset(&fold[max_folders], 0, sizeof(struct folder));
2148                 extract(fold[max_folders].name, buf, 1);
2149                 ++max_folders;
2150                 ++num_floors;
2151         }
2152
2153         /* Now add rooms */
2154         serv_puts("LKRA");
2155         serv_gets(buf);
2156         if (buf[0]=='1') while(serv_gets(buf), strcmp(buf, "000")) {
2157                 if (max_folders >= alloc_folders) {
2158                         alloc_folders = max_folders + 100;
2159                         fold = realloc(fold,
2160                                 alloc_folders * sizeof(struct folder));
2161                 }
2162                 memset(&fold[max_folders], 0, sizeof(struct folder));
2163                 extract(fold[max_folders].room, buf, 0);
2164                 ra_flags = extract_int(buf, 5);
2165                 flags = extract_int(buf, 1);
2166                 fold[max_folders].floor = extract_int(buf, 2);
2167                 fold[max_folders].hasnewmsgs =
2168                         ((ra_flags & UA_HASNEWMSGS) ? 1 : 0 );
2169                 if (flags & QR_MAILBOX) {
2170                         fold[max_folders].is_mailbox = 1;
2171                 }
2172                 room_to_folder(fold[max_folders].name,
2173                                 fold[max_folders].room,
2174                                 fold[max_folders].floor,
2175                                 fold[max_folders].is_mailbox);
2176                 fold[max_folders].selectable = 1;
2177                 ++max_folders;
2178         }
2179
2180         /* Bubble-sort the folder list */
2181         for (i=0; i<max_folders; ++i) {
2182                 for (j=0; j<(max_folders-1)-i; ++j) {
2183                         if (fold[j].is_mailbox == fold[j+1].is_mailbox) {
2184                                 swap = strcasecmp(fold[j].name, fold[j+1].name);
2185                         }
2186                         else {
2187                                 if ( (fold[j+1].is_mailbox)
2188                                    && (!fold[j].is_mailbox)) {
2189                                         swap = 1;
2190                                 }
2191                                 else {
2192                                         swap = 0;
2193                                 }
2194                         }
2195                         if (swap > 0) {
2196                                 memcpy(&ftmp, &fold[j], sizeof(struct folder));
2197                                 memcpy(&fold[j], &fold[j+1],
2198                                                         sizeof(struct folder));
2199                                 memcpy(&fold[j+1], &ftmp,
2200                                                         sizeof(struct folder));
2201                         }
2202                 }
2203         }
2204
2205         if (!strcasecmp(viewpref, "folders")) {
2206                 do_folder_view(fold, max_folders, num_floors);
2207         }
2208         else {
2209                 do_rooms_view(fold, max_folders, num_floors);
2210         }
2211
2212         free(fold);
2213         wDumpContent(1);
2214 }
2215
2216
2217 /* Do either a known rooms list or a folders list, depending on the
2218  * user's preference
2219  */
2220 void knrooms() {
2221         char listviewpref[SIZ];
2222
2223         output_headers(3);
2224         load_floorlist();
2225
2226         /* Determine whether the user is trying to change views */
2227         if (bstr("view") != NULL) {
2228                 if (strlen(bstr("view")) > 0) {
2229                         set_preference("roomlistview", bstr("view"));
2230                 }
2231         }
2232
2233         get_preference("roomlistview", listviewpref);
2234
2235         if ( (strcasecmp(listviewpref, "folders"))
2236            && (strcasecmp(listviewpref, "table")) ) {
2237                 strcpy(listviewpref, "rooms");
2238         }
2239
2240         /* title bar */
2241         wprintf("<TABLE WIDTH=100%% BORDER=0 BGCOLOR=\"#444455\"><TR><TD>"
2242                 "<SPAN CLASS=\"titlebar\">"
2243         );
2244         if (!strcasecmp(listviewpref, "rooms")) {
2245                 wprintf("Room list");
2246         }
2247         if (!strcasecmp(listviewpref, "folders")) {
2248                 wprintf("Folder list");
2249         }
2250         if (!strcasecmp(listviewpref, "table")) {
2251                 wprintf("Room list");
2252         }
2253         wprintf("</SPAN></TD>\n");
2254
2255
2256         /* offer the ability to switch views */
2257         wprintf("<TD ALIGN=RIGHT><FORM NAME=\"roomlistomatic\">\n"
2258                 "<SELECT NAME=\"newview\" SIZE=\"1\" "
2259                 "OnChange=\"location.href=roomlistomatic.newview.options"
2260                 "[selectedIndex].value\">\n");
2261
2262         wprintf("<OPTION %s VALUE=\"/knrooms&view=rooms\">"
2263                 "View as room list"
2264                 "</OPTION>\n",
2265                 ( !strcasecmp(listviewpref, "rooms") ? "SELECTED" : "" )
2266         );
2267
2268         wprintf("<OPTION %s VALUE=\"/knrooms&view=folders\">"
2269                 "View as folder list"
2270                 "</OPTION>\n",
2271                 ( !strcasecmp(listviewpref, "folders") ? "SELECTED" : "" )
2272         );
2273
2274         wprintf("</SELECT><BR>");
2275         offer_start_page();
2276         wprintf("</FORM></TD></TR></TABLE>\n");
2277
2278         /* Display the room list in the user's preferred format */
2279         list_all_rooms_by_floor(listviewpref);
2280 }
2281
2282
2283
2284 /* 
2285  * Set the message expire policy for this room and/or floor
2286  */
2287 void set_room_policy(void) {
2288         char buf[SIZ];
2289
2290         if (strcmp(bstr("sc"), "OK")) {
2291                 strcpy(WC->ImportantMessage,
2292                         "Cancelled.  Changes were not saved.");
2293                 display_editroom();
2294                 return;
2295         }
2296
2297         serv_printf("SPEX room|%d|%d", atoi(bstr("roompolicy")), atoi(bstr("roomvalue")));
2298         serv_gets(buf);
2299         strcpy(WC->ImportantMessage, &buf[4]);
2300
2301         if (WC->axlevel >= 6) {
2302                 strcat(WC->ImportantMessage, "<BR>\n");
2303                 serv_printf("SPEX floor|%d|%d", atoi(bstr("floorpolicy")), atoi(bstr("floorvalue")));
2304                 serv_gets(buf);
2305                 strcat(WC->ImportantMessage, &buf[4]);
2306         }
2307
2308         display_editroom();
2309 }