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