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