874d2147af73472dabcca1e5cc86ad09c5be2550
[citadel.git] / webcit / roomops.c
1 /* $Id$ */
2
3
4 #include <ctype.h>
5 #include <stdlib.h>
6 #include <unistd.h>
7 #include <stdio.h>
8 #include <fcntl.h>
9 #include <signal.h>
10 #include <sys/types.h>
11 #include <sys/wait.h>
12 #include <sys/socket.h>
13 #include <sys/time.h>
14 #include <limits.h>
15 #include <netinet/in.h>
16 #include <netdb.h>
17 #include <string.h>
18 #include <pwd.h>
19 #include <errno.h>
20 #include <stdarg.h>
21 #include <pthread.h>
22 #include <signal.h>
23 #include "webcit.h"
24
25
26 char *viewdefs[] = {
27         "Messages",
28         "Summary",
29         "Address Book"
30 };
31
32 char floorlist[128][SIZ];
33
34 /*
35  * load the list of floors
36  */
37 void load_floorlist(void)
38 {
39         int a;
40         char buf[SIZ];
41
42         for (a = 0; a < 128; ++a)
43                 floorlist[a][0] = 0;
44
45         serv_puts("LFLR");
46         serv_gets(buf);
47         if (buf[0] != '1') {
48                 strcpy(floorlist[0], "Main Floor");
49                 return;
50         }
51         while (serv_gets(buf), strcmp(buf, "000")) {
52                 extract(floorlist[extract_int(buf, 0)], buf, 1);
53         }
54 }
55
56
57 /*
58  * remove a room from the march list
59  */
60 void remove_march(char *aaa)
61 {
62         struct march *mptr, *mptr2;
63
64         if (WC->march == NULL)
65                 return;
66
67         if (!strcasecmp(WC->march->march_name, aaa)) {
68                 mptr = WC->march->next;
69                 free(WC->march);
70                 WC->march = mptr;
71                 return;
72         }
73         mptr2 = WC->march;
74         for (mptr = WC->march; mptr != NULL; mptr = mptr->next) {
75                 if (!strcasecmp(mptr->march_name, aaa)) {
76                         mptr2->next = mptr->next;
77                         free(mptr);
78                         mptr = mptr2;
79                 } else {
80                         mptr2 = mptr;
81                 }
82         }
83 }
84
85
86
87
88
89 void room_tree_list(struct roomlisting *rp)
90 {
91         char rmname[64];
92         int f;
93
94         if (rp == NULL)
95                 return;
96
97         if (rp->lnext != NULL) {
98                 room_tree_list(rp->lnext);
99         }
100         strcpy(rmname, rp->rlname);
101         f = rp->rlflags;
102
103         wprintf("<A HREF=\"/dotgoto&room=");
104         urlescputs(rmname);
105         wprintf("\"");
106         wprintf(">");
107         escputs1(rmname, 1);
108         if ((f & QR_DIRECTORY) && (f & QR_NETWORK))
109                 wprintf("}");
110         else if (f & QR_DIRECTORY)
111                 wprintf("]");
112         else if (f & QR_NETWORK)
113                 wprintf(")");
114         else
115                 wprintf("&gt;");
116         wprintf("</A><TT> </TT>\n");
117
118         if (rp->rnext != NULL) {
119                 room_tree_list(rp->rnext);
120         }
121         free(rp);
122 }
123
124
125 /* 
126  * Room ordering stuff (compare first by floor, then by order)
127  */
128 int rordercmp(struct roomlisting *r1, struct roomlisting *r2)
129 {
130         if ((r1 == NULL) && (r2 == NULL))
131                 return (0);
132         if (r1 == NULL)
133                 return (-1);
134         if (r2 == NULL)
135                 return (1);
136         if (r1->rlfloor < r2->rlfloor)
137                 return (-1);
138         if (r1->rlfloor > r2->rlfloor)
139                 return (1);
140         if (r1->rlorder < r2->rlorder)
141                 return (-1);
142         if (r1->rlorder > r2->rlorder)
143                 return (1);
144         return (0);
145 }
146
147
148 /*
149  * Common code for all room listings
150  */
151 void listrms(char *variety)
152 {
153         char buf[SIZ];
154         int num_rooms = 0;
155
156         struct roomlisting *rl = NULL;
157         struct roomlisting *rp;
158         struct roomlisting *rs;
159
160
161         /* Ask the server for a room list */
162         serv_puts(variety);
163         serv_gets(buf);
164         if (buf[0] != '1') {
165                 wprintf("&nbsp;");
166                 return;
167         }
168         while (serv_gets(buf), strcmp(buf, "000")) {
169                 ++num_rooms;
170                 rp = malloc(sizeof(struct roomlisting));
171                 extract(rp->rlname, buf, 0);
172                 rp->rlflags = extract_int(buf, 1);
173                 rp->rlfloor = extract_int(buf, 2);
174                 rp->rlorder = extract_int(buf, 3);
175                 rp->lnext = NULL;
176                 rp->rnext = NULL;
177
178                 rs = rl;
179                 if (rl == NULL) {
180                         rl = rp;
181                 } else
182                         while (rp != NULL) {
183                                 if (rordercmp(rp, rs) < 0) {
184                                         if (rs->lnext == NULL) {
185                                                 rs->lnext = rp;
186                                                 rp = NULL;
187                                         } else {
188                                                 rs = rs->lnext;
189                                         }
190                                 } else {
191                                         if (rs->rnext == NULL) {
192                                                 rs->rnext = rp;
193                                                 rp = NULL;
194                                         } else {
195                                                 rs = rs->rnext;
196                                         }
197                                 }
198                         }
199         }
200
201         room_tree_list(rl);
202
203         /* If no rooms were listed, print an nbsp to make the cell
204          * borders show up anyway.
205          */
206         if (num_rooms == 0) wprintf("&nbsp;");
207 }
208
209
210
211
212
213
214
215
216
217 /*
218  * list all rooms by floor (only should get called from knrooms() because
219  * that's where output_headers() is called from)
220  */
221 void list_all_rooms_by_floor(void)
222 {
223         int a;
224         char buf[SIZ];
225
226         wprintf("<TABLE width=100%% border><TR><TH>Floor</TH>");
227         wprintf("<TH>Rooms with new messages</TH>");
228         wprintf("<TH>Rooms with no new messages</TH></TR>\n");
229
230         for (a = 0; a < 128; ++a)
231                 if (floorlist[a][0] != 0) {
232
233                         /* Floor name column */
234                         wprintf("<TR><TD>");
235
236                         serv_printf("OIMG _floorpic_|%d", a);
237                         serv_gets(buf);
238                         if (buf[0] == '2') {
239                                 serv_puts("CLOS");
240                                 serv_gets(buf);
241                                 wprintf("<IMG SRC=\"/image&name=_floorpic_&parm=%d\" ALT=\"%s\">",
242                                         a, &floorlist[a][0]);
243                         } else {
244                                 escputs(&floorlist[a][0]);
245                         }
246
247                         wprintf("</TD>");
248
249                         /* Rooms with new messages column */
250                         wprintf("<TD>");
251                         sprintf(buf, "LKRN %d", a);
252                         listrms(buf);
253                         wprintf("</TD>\n<TD>");
254
255                         /* Rooms with old messages column */
256                         sprintf(buf, "LKRO %d", a);
257                         listrms(buf);
258                         wprintf("</TD></TR>\n");
259                 }
260         wprintf("</TABLE>\n");
261         wDumpContent(1);
262 }
263
264
265 /*
266  * list all forgotten rooms
267  */
268 void zapped_list(void)
269 {
270         output_headers(1);
271         wprintf("<TABLE WIDTH=100%% BORDER=0 BGCOLOR=770000><TR><TD>");
272         wprintf("<FONT SIZE=+1 COLOR=\"FFFFFF\"");
273         wprintf("<B>Zapped (forgotten) rooms</B>\n");
274         wprintf("</TD></TR></TABLE><BR>\n");
275         listrms("LZRM -1");
276         wprintf("<BR><BR>\n");
277         wprintf("Click on any room to un-zap it and goto that room.\n");
278         wDumpContent(1);
279 }
280
281
282 /*
283  * read this room's info file (set v to 1 for verbose mode)
284  */
285 void readinfo(void)
286 {
287         char buf[SIZ];
288
289         serv_puts("RINF");
290         serv_gets(buf);
291         if (buf[0] == '1') {
292                 fmout(NULL);
293         }
294 }
295
296
297
298
299 /* Display room graphic.  The server doesn't actually
300  * need the room name, but we supply it in order to
301  * keep the browser from using a cached graphic from 
302  * another room.
303  */
304 void embed_room_graphic(void) {
305         char buf[SIZ];
306
307         serv_puts("OIMG _roompic_");
308         serv_gets(buf);
309         if (buf[0] == '2') {
310                 wprintf("<TD BGCOLOR=444455>");
311                 wprintf("<IMG SRC=\"/image&name=_roompic_&room=");
312                 urlescputs(WC->wc_roomname);
313                 wprintf("\"></TD>");
314                 serv_puts("CLOS");
315                 serv_gets(buf);
316         }
317
318 }
319
320
321 /* Let the user know if new mail has arrived 
322  */
323 void embed_newmail_button(void) {
324         if ( (WC->new_mail > WC->remember_new_mail) && (WC->new_mail>0) ) {
325                 wprintf(
326                         "<A HREF=\"/dotgoto?room=_MAIL_\">"
327                         "<IMG SRC=\"/static/mail.gif\" border=0 "
328                         "ALT=\"You have new mail\">"
329                         "<BR><FONT SIZE=2 COLOR=FFFFFF>"
330                         "%d new mail</FONT></A>", WC->new_mail);
331                 WC->remember_new_mail = WC->new_mail;
332         }
333 }
334
335
336
337 /*
338  * Display the current view and offer an option to change it
339  */
340 void embed_view_o_matic(void) {
341         int i;
342
343         wprintf("<FORM NAME=\"viewomatic\">\n"
344                 "<SELECT NAME=\"newview\" SIZE=\"1\" "
345                 "OnChange=\"location.href=viewomatic.newview.options"
346                 "[selectedIndex].value\">\n");
347
348         for (i=0; i<(sizeof viewdefs / sizeof (char *)); ++i) {
349                 wprintf("<OPTION %s VALUE=\"/changeview?view=%d\">",
350                         ((i == WC->wc_view) ? "SELECTED" : ""),
351                         i );
352                 escputs(viewdefs[i]);
353                 wprintf("</OPTION>\n");
354         }
355         wprintf("</SELECT></FORM>\n");
356 }
357
358
359
360 void embed_room_banner(char *got) {
361         char fakegot[SIZ];
362
363         /* We need to have the information returned by a GOTO server command.
364          * If it isn't supplied, we fake it by issuing our own GOTO.
365          */
366         if (got == NULL) {
367                 serv_printf("GOTO %s", WC->wc_roomname);
368                 serv_gets(fakegot);
369                 got = fakegot;
370         }
371
372         /* If the user happens to select the "make this my start page" link,
373          * we want it to remember the URL as a "/dotskip" one instead of
374          * a "skip" or "gotonext" or something like that.
375          */
376         snprintf(WC->this_page, sizeof(WC->this_page), "/dotskip&room=%s",
377                 WC->wc_roomname);
378
379         /* Check for new mail. */
380         WC->new_mail = extract_int(&got[4], 9);
381         WC->wc_view = extract_int(&got[4], 11);
382
383         svprintf("ROOMNAME", WCS_STRING, "%s", WC->wc_roomname);
384         svprintf("NEWMSGS", WCS_STRING, "%d", extract_int(&got[4], 1));
385         svprintf("TOTALMSGS", WCS_STRING, "%d", extract_int(&got[4], 2));
386         svcallback("ROOMPIC", embed_room_graphic);
387         svcallback("ROOMINFO", readinfo);
388         svcallback("YOUHAVEMAIL", embed_newmail_button);
389         svcallback("VIEWOMATIC", embed_view_o_matic);
390         svcallback("START", offer_start_page);
391
392         do_template("roombanner");
393         clear_local_substs();
394 }
395
396
397
398
399
400 /*
401  * generic routine to take the session to a new room
402  *
403  * display_name values:  0 = goto only
404  *                       1 = goto and display
405  *                       2 = display only
406  */
407 void gotoroom(char *gname, int display_name)
408 {
409         char buf[SIZ];
410         static long ls = (-1L);
411
412
413         if (display_name) {
414                 output_headers(0);
415                 wprintf("Pragma: no-cache\n");
416                 wprintf("Cache-Control: no-store\n");
417
418                 wprintf("<HTML><HEAD>\n"
419                         "<META HTTP-EQUIV=\"refresh\" CONTENT=\"500363689;\">\n"
420                         "<META HTTP-EQUIV=\"Pragma\" CONTENT=\"no-cache\">\n"
421                         "<META HTTP-EQUIV=\"expired\" CONTENT=\"28-May-1971 18:10:00 GMT\">\n"
422                         "<meta name=\"MSSmartTagsPreventParsing\" content=\"TRUE\">\n"
423                         "</HEAD>\n");
424                 do_template("background");
425         }
426         if (display_name != 2) {
427                 /* store ungoto information */
428                 strcpy(WC->ugname, WC->wc_roomname);
429                 WC->uglsn = ls;
430         }
431         /* move to the new room */
432         serv_printf("GOTO %s", gname);
433         serv_gets(buf);
434         if (buf[0] != '2') {
435                 serv_puts("GOTO _BASEROOM_");
436                 serv_gets(buf);
437         }
438         if (buf[0] != '2') {
439                 if (display_name) {
440                         wprintf("<EM>%s</EM><BR>\n", &buf[4]);
441                         wDumpContent(1);
442                 }
443                 return;
444         }
445         extract(WC->wc_roomname, &buf[4], 0);
446         WC->room_flags = extract_int(&buf[4], 4);
447         /* highest_msg_read = extract_int(&buf[4],6);
448            maxmsgnum = extract_int(&buf[4],5);
449            is_mail = (char) extract_int(&buf[4],7); */
450         ls = extract_long(&buf[4], 6);
451
452         if (WC->is_aide)
453                 WC->is_room_aide = WC->is_aide;
454         else
455                 WC->is_room_aide = (char) extract_int(&buf[4], 8);
456
457         remove_march(WC->wc_roomname);
458         if (!strcasecmp(gname, "_BASEROOM_"))
459                 remove_march(gname);
460
461         /* Display the room banner */
462         if (display_name) {
463                 embed_room_banner(buf);
464                 wDumpContent(1);
465         }
466         strcpy(WC->wc_roomname, WC->wc_roomname);
467         WC->wc_view = extract_int(&buf[4], 11);
468 }
469
470
471 /*
472  * Locate the room on the march list which we most want to go to.  Each room
473  * is measured given a "weight" of preference based on various factors.
474  */
475 char *pop_march(int desired_floor)
476 {
477         static char TheRoom[64];
478         int TheFloor = 0;
479         int TheOrder = 32767;
480         int TheWeight = 0;
481         int weight;
482         struct march *mptr = NULL;
483
484         strcpy(TheRoom, "_BASEROOM_");
485         if (WC->march == NULL)
486                 return (TheRoom);
487
488         for (mptr = WC->march; mptr != NULL; mptr = mptr->next) {
489                 weight = 0;
490                 if ((strcasecmp(mptr->march_name, "_BASEROOM_")))
491                         weight = weight + 10000;
492                 if (mptr->march_floor == desired_floor)
493                         weight = weight + 5000;
494
495                 weight = weight + ((128 - (mptr->march_floor)) * 128);
496                 weight = weight + (128 - (mptr->march_order));
497
498                 if (weight > TheWeight) {
499                         TheWeight = weight;
500                         strcpy(TheRoom, mptr->march_name);
501                         TheFloor = mptr->march_floor;
502                         TheOrder = mptr->march_order;
503                 }
504         }
505         return (TheRoom);
506 }
507
508
509
510 /* Goto next room having unread messages.
511  * We want to skip over rooms that the user has already been to, and take the
512  * user back to the lobby when done.  The room we end up in is placed in
513  * newroom - which is set to 0 (the lobby) initially.
514  * We start the search in the current room rather than the beginning to prevent
515  * two or more concurrent users from dragging each other back to the same room.
516  */
517 void gotonext(void)
518 {
519         char buf[SIZ];
520         struct march *mptr, *mptr2;
521         char next_room[32];
522
523         /* First check to see if the march-mode list is already allocated.
524          * If it is, pop the first room off the list and go there.
525          */
526
527         if (WC->march == NULL) {
528                 serv_puts("LKRN");
529                 serv_gets(buf);
530                 if (buf[0] == '1')
531                         while (serv_gets(buf), strcmp(buf, "000")) {
532                                 mptr = (struct march *) malloc(sizeof(struct march));
533                                 mptr->next = NULL;
534                                 extract(mptr->march_name, buf, 0);
535                                 mptr->march_floor = extract_int(buf, 2);
536                                 mptr->march_order = extract_int(buf, 3);
537                                 if (WC->march == NULL) {
538                                         WC->march = mptr;
539                                 } else {
540                                         mptr2 = WC->march;
541                                         while (mptr2->next != NULL)
542                                                 mptr2 = mptr2->next;
543                                         mptr2->next = mptr;
544                                 }
545                         }
546 /* add _BASEROOM_ to the end of the march list, so the user will end up
547  * in the system base room (usually the Lobby>) at the end of the loop
548  */
549                 mptr = (struct march *) malloc(sizeof(struct march));
550                 mptr->next = NULL;
551                 strcpy(mptr->march_name, "_BASEROOM_");
552                 if (WC->march == NULL) {
553                         WC->march = mptr;
554                 } else {
555                         mptr2 = WC->march;
556                         while (mptr2->next != NULL)
557                                 mptr2 = mptr2->next;
558                         mptr2->next = mptr;
559                 }
560 /*
561  * ...and remove the room we're currently in, so a <G>oto doesn't make us
562  * walk around in circles
563  */
564                 remove_march(WC->wc_roomname);
565         }
566         if (WC->march != NULL) {
567                 strcpy(next_room, pop_march(-1));
568         } else {
569                 strcpy(next_room, "_BASEROOM_");
570         }
571
572
573         smart_goto(next_room);
574 }
575
576
577 void smart_goto(char *next_room) {
578         gotoroom(next_room, 0);
579         readloop("readnew");
580 }
581
582
583
584 /*
585  * mark all messages in current room as having been read
586  */
587 void slrp_highest(void)
588 {
589         char buf[SIZ];
590
591         /* set pointer */
592         serv_puts("SLRP HIGHEST");
593         serv_gets(buf);
594         if (buf[0] != '2') {
595                 wprintf("<EM>%s</EM><BR>\n", &buf[4]);
596                 return;
597         }
598 }
599
600
601 /*
602  * un-goto the previous room
603  */
604 void ungoto(void)
605 {
606         char buf[SIZ];
607
608         if (!strcmp(WC->ugname, "")) {
609                 smart_goto(WC->wc_roomname);
610                 return;
611         }
612         serv_printf("GOTO %s", WC->ugname);
613         serv_gets(buf);
614         if (buf[0] != '2') {
615                 smart_goto(WC->wc_roomname);
616                 return;
617         }
618         if (WC->uglsn >= 0L) {
619                 serv_printf("SLRP %ld", WC->uglsn);
620                 serv_gets(buf);
621         }
622         strcpy(buf, WC->ugname);
623         strcpy(WC->ugname, "");
624         smart_goto(buf);
625 }
626
627 /*
628  * display the form for editing a room
629  */
630 void display_editroom(void)
631 {
632         char buf[SIZ];
633         char cmd[SIZ];
634         char node[SIZ];
635         char recp[SIZ];
636         char er_name[20];
637         char er_password[10];
638         char er_dirname[15];
639         char er_roomaide[26];
640         unsigned er_flags;
641         int er_floor;
642         int i, j;
643         char *tab;
644         char *shared_with;
645         char *not_shared_with;
646
647         tab = bstr("tab");
648         if (strlen(tab) == 0) tab = "admin";
649
650         serv_puts("GETR");
651         serv_gets(buf);
652
653         if (buf[0] != '2') {
654                 display_error(&buf[4]);
655                 return;
656         }
657         extract(er_name, &buf[4], 0);
658         extract(er_password, &buf[4], 1);
659         extract(er_dirname, &buf[4], 2);
660         er_flags = extract_int(&buf[4], 3);
661         er_floor = extract_int(&buf[4], 4);
662
663         output_headers(1);
664
665         /* print the tabbed dialog */
666         wprintf("<TABLE border=0 cellspacing=0 cellpadding=0 width=100%%>"
667                 "<TR ALIGN=CENTER BGCOLOR=FFFFFF>"
668                 "<TD>&nbsp;</TD>\n");
669
670         if (!strcmp(tab, "admin")) {
671                 wprintf("<TD BGCOLOR=000077><FONT SIZE=+1 COLOR=\"FFFFFF\"><B>");
672         }
673         else {
674                 wprintf("<TD BGCOLOR=AAAAAA><A HREF=\"/display_editroom&tab=admin\">");
675         }
676         wprintf("Room administration");
677         if (!strcmp(tab, "admin")) {
678                 wprintf("</B></FONT></TD>\n");
679         }
680         else {
681                 wprintf("</A></TD>\n");
682         }
683
684         wprintf("<TD>&nbsp;</TD>\n");
685
686         if (!strcmp(tab, "config")) {
687                 wprintf("<TD BGCOLOR=000077><FONT SIZE=+1 COLOR=\"FFFFFF\"><B>");
688         }
689         else {
690                 wprintf("<TD BGCOLOR=AAAAAA><A HREF=\"/display_editroom&tab=config\">");
691         }
692         wprintf("Room configuration");
693         if (!strcmp(tab, "config")) {
694                 wprintf("</B></FONT></TD>\n");
695         }
696         else {
697                 wprintf("</A></TD>\n");
698         }
699
700         wprintf("<TD>&nbsp;</TD>\n");
701
702         if (!strcmp(tab, "sharing")) {
703                 wprintf("<TD BGCOLOR=000077><FONT SIZE=+1 COLOR=\"FFFFFF\"><B>");
704         }
705         else {
706                 wprintf("<TD BGCOLOR=AAAAAA><A HREF=\"/display_editroom&tab=sharing\">");
707         }
708         wprintf("Sharing");
709         if (!strcmp(tab, "sharing")) {
710                 wprintf("</B></FONT></TD>\n");
711         }
712         else {
713                 wprintf("</A></TD>\n");
714         }
715
716         wprintf("<TD>&nbsp;</TD>\n");
717
718         if (!strcmp(tab, "listserv")) {
719                 wprintf("<TD BGCOLOR=000077><FONT SIZE=+1 COLOR=\"FFFFFF\"><B>");
720         }
721         else {
722                 wprintf("<TD BGCOLOR=AAAAAA><A HREF=\"/display_editroom&tab=listserv\">");
723         }
724         wprintf("Mailing list service");
725         if (!strcmp(tab, "listserv")) {
726                 wprintf("</B></FONT></TD>\n");
727         }
728         else {
729                 wprintf("</A></TD>\n");
730         }
731
732         wprintf("<TD>&nbsp;</TD></TR>"
733                 "<TR><TD BGCOLOR=000077 COLSPAN=9 HEIGHT=5> </TD></TR>"
734                 "</TABLE>\n");
735
736         /* end tabbed dialog */ 
737
738
739         if (!strcmp(tab, "admin")) {
740                 wprintf("<UL>"
741                         "<LI><A HREF=\"/confirm_delete_room\">\n"
742                         "Delete this room</A>\n"
743                         "<LI><A HREF=\"/display_editroompic\">\n"
744                         "Set or change the graphic for this room's banner</A>\n"
745                         "<LI><A HREF=\"/display_editinfo\">\n"
746                         "Edit this room's Info file</A>\n"
747                         "</UL>");
748         }
749
750         if (!strcmp(tab, "config")) {
751                 wprintf("<FORM METHOD=\"POST\" ACTION=\"/editroom\">\n");
752         
753                 wprintf("<UL><LI>Name of room: ");
754                 wprintf("<INPUT TYPE=\"text\" NAME=\"er_name\" VALUE=\"%s\" MAXLENGTH=\"19\">\n", er_name);
755         
756                 wprintf("<LI>Resides on floor: ");
757                 load_floorlist();
758                 wprintf("<SELECT NAME=\"er_floor\" SIZE=\"1\">\n");
759                 for (i = 0; i < 128; ++i)
760                         if (strlen(floorlist[i]) > 0) {
761                                 wprintf("<OPTION ");
762                                 if (i == er_floor)
763                                         wprintf("SELECTED ");
764                                 wprintf("VALUE=\"%d\">", i);
765                                 escputs(floorlist[i]);
766                                 wprintf("</OPTION>\n");
767                         }
768                 wprintf("</SELECT>\n");
769         
770                 wprintf("<LI>Type of room:<UL>\n");
771
772                 wprintf("<LI><INPUT TYPE=\"radio\" NAME=\"type\" VALUE=\"public\" ");
773                 if ((er_flags & QR_PRIVATE) == 0)
774                 wprintf("CHECKED ");
775                 wprintf("> Public room\n");
776
777                 wprintf("<LI><INPUT TYPE=\"radio\" NAME=\"type\" VALUE=\"guessname\" ");
778                 if ((er_flags & QR_PRIVATE) &&
779                     (er_flags & QR_GUESSNAME))
780                         wprintf("CHECKED ");
781                 wprintf("> Private - guess name\n");
782         
783                 wprintf("<LI><INPUT TYPE=\"radio\" NAME=\"type\" VALUE=\"passworded\" ");
784                 if ((er_flags & QR_PRIVATE) &&
785                     (er_flags & QR_PASSWORDED))
786                         wprintf("CHECKED ");
787                 wprintf("> Private - require password:\n");
788                 wprintf("<INPUT TYPE=\"text\" NAME=\"er_password\" VALUE=\"%s\" MAXLENGTH=\"9\">\n", er_password);
789         
790                 wprintf("<LI><INPUT TYPE=\"radio\" NAME=\"type\" VALUE=\"invonly\" ");
791                 if ((er_flags & QR_PRIVATE)
792                     && ((er_flags & QR_GUESSNAME) == 0)
793                     && ((er_flags & QR_PASSWORDED) == 0))
794                         wprintf("CHECKED ");
795                 wprintf("> Private - invitation only\n");
796         
797                 wprintf("<LI><INPUT TYPE=\"checkbox\" NAME=\"bump\" VALUE=\"yes\" ");
798                 wprintf("> If private, cause current users to forget room\n");
799         
800                 wprintf("</UL>\n");
801         
802                 wprintf("<LI><INPUT TYPE=\"checkbox\" NAME=\"prefonly\" VALUE=\"yes\" ");
803                 if (er_flags & QR_PREFONLY)
804                         wprintf("CHECKED ");
805                 wprintf("> Preferred users only\n");
806         
807                 wprintf("<LI><INPUT TYPE=\"checkbox\" NAME=\"readonly\" VALUE=\"yes\" ");
808                 if (er_flags & QR_READONLY)
809                         wprintf("CHECKED ");
810                 wprintf("> Read-only room\n");
811         
812         /* directory stuff */
813                 wprintf("<LI><INPUT TYPE=\"checkbox\" NAME=\"directory\" VALUE=\"yes\" ");
814                 if (er_flags & QR_DIRECTORY)
815                         wprintf("CHECKED ");
816                 wprintf("> File directory room\n");
817
818                 wprintf("<UL><LI>Directory name: ");
819                 wprintf("<INPUT TYPE=\"text\" NAME=\"er_dirname\" VALUE=\"%s\" MAXLENGTH=\"14\">\n", er_dirname);
820
821                 wprintf("<LI><INPUT TYPE=\"checkbox\" NAME=\"ulallowed\" VALUE=\"yes\" ");
822                 if (er_flags & QR_UPLOAD)
823                         wprintf("CHECKED ");
824                 wprintf("> Uploading allowed\n");
825         
826                 wprintf("<LI><INPUT TYPE=\"checkbox\" NAME=\"dlallowed\" VALUE=\"yes\" ");
827                 if (er_flags & QR_DOWNLOAD)
828                         wprintf("CHECKED ");
829                 wprintf("> Downloading allowed\n");
830         
831                 wprintf("<LI><INPUT TYPE=\"checkbox\" NAME=\"visdir\" VALUE=\"yes\" ");
832                 if (er_flags & QR_VISDIR)
833                         wprintf("CHECKED ");
834                 wprintf("> Visible directory</UL>\n");
835         
836         /* end of directory stuff */
837         
838                 wprintf("<LI><INPUT TYPE=\"checkbox\" NAME=\"network\" VALUE=\"yes\" ");
839                 if (er_flags & QR_NETWORK)
840                         wprintf("CHECKED ");
841                 wprintf("> Network shared room\n");
842
843         /* start of anon options */
844         
845                 wprintf("<LI>Anonymous messages<UL>\n");
846         
847                 wprintf("<LI><INPUT TYPE=\"radio\" NAME=\"anon\" VALUE=\"no\" ");
848                 if (((er_flags & QR_ANONONLY) == 0)
849                     && ((er_flags & QR_ANONOPT) == 0))
850                         wprintf("CHECKED ");
851                 wprintf("> No anonymous messages\n");
852         
853                 wprintf("<LI><INPUT TYPE=\"radio\" NAME=\"anon\" VALUE=\"anononly\" ");
854                 if (er_flags & QR_ANONONLY)
855                         wprintf("CHECKED ");
856                 wprintf("> All messages are anonymous\n");
857         
858                 wprintf("<LI><INPUT TYPE=\"radio\" NAME=\"anon\" VALUE=\"anon2\" ");
859                 if (er_flags & QR_ANONOPT)
860                         wprintf("CHECKED ");
861                 wprintf("> Prompt user when entering messages</UL>\n");
862         
863         /* end of anon options */
864         
865                 wprintf("<LI>Room aide: \n");
866                 serv_puts("GETA");
867                 serv_gets(buf);
868                 if (buf[0] != '2') {
869                         wprintf("<EM>%s</EM>\n", &buf[4]);
870                 } else {
871                         extract(er_roomaide, &buf[4], 0);
872                         wprintf("<INPUT TYPE=\"text\" NAME=\"er_roomaide\" VALUE=\"%s\" MAXLENGTH=\"25\">\n", er_roomaide);
873                 }
874         
875                 wprintf("</UL><CENTER>\n");
876                 wprintf("<INPUT TYPE=\"submit\" NAME=\"sc\" VALUE=\"OK\">");
877                 wprintf("<INPUT TYPE=\"submit\" NAME=\"sc\" VALUE=\"Cancel\">");
878                 wprintf("</CENTER>\n");
879         }
880
881
882         /* Sharing the room with other Citadel nodes... */
883         if (!strcmp(tab, "sharing")) {
884
885                 shared_with = strdup("");
886                 not_shared_with = strdup("");
887
888                 /* Learn the current configuration */
889                 serv_puts("CONF getsys|application/x-citadel-ignet-config");
890                 serv_gets(buf);
891                 if (buf[0]=='1') while (serv_gets(buf), strcmp(buf, "000")) {
892                         extract(node, buf, 0);
893                         not_shared_with = realloc(not_shared_with,
894                                         strlen(not_shared_with) + 32);
895                         strcat(not_shared_with, node);
896                         strcat(not_shared_with, "|");
897                 }
898
899                 serv_puts("GNET");
900                 serv_gets(buf);
901                 if (buf[0]=='1') while (serv_gets(buf), strcmp(buf, "000")) {
902                         extract(cmd, buf, 0);
903                         extract(node, buf, 1);
904                         if (!strcasecmp(cmd, "ignet_push_share")) {
905                                 shared_with = realloc(shared_with,
906                                                 strlen(shared_with) + 32);
907                                 strcat(shared_with, node);
908                                 strcat(shared_with, "|");
909                         }
910                 }
911
912                 for (i=0; i<num_tokens(shared_with, '|'); ++i) {
913                         extract(node, shared_with, i);
914                         for (j=0; j<num_tokens(not_shared_with, '|'); ++j) {
915                                 extract(cmd, not_shared_with, j);
916                                 if (!strcasecmp(node, cmd)) {
917                                         remove_token(not_shared_with, j, '|');
918                                 }
919                         }
920                 }
921
922                 /* Display the stuff */
923                 wprintf("<CENTER><BR>"
924                         "<TABLE border=1 cellpadding=5><TR>"
925                         "<TD><B><I>Shared with</I></B></TD>"
926                         "<TD><B><I>Not shared with</I></B></TD></TR>\n"
927                         "<TR><TD>\n");
928
929                 for (i=0; i<num_tokens(shared_with, '|'); ++i) {
930                         extract(node, shared_with, i);
931                         if (strlen(node) > 0) {
932                                 wprintf("%s ", node);
933                                 wprintf("<A HREF=\"/netedit&cmd=remove&line="
934                                         "ignet_push_share|");
935                                 urlescputs(node);
936                                 wprintf("&tab=sharing\">(unshare)</A><BR>");
937                         }
938                 }
939
940                 wprintf("</TD><TD>\n");
941
942                 for (i=0; i<num_tokens(not_shared_with, '|'); ++i) {
943                         extract(node, not_shared_with, i);
944                         if (strlen(node) > 0) {
945                                 wprintf("%s ", node);
946                                 wprintf("<A HREF=\"/netedit&cmd=add&line="
947                                         "ignet_push_share|");
948                                 urlescputs(node);
949                                 wprintf("&tab=sharing\">(share)</A><BR>");
950                         }
951                 }
952
953                 wprintf("</TD></TR>"
954                         "</TABLE><BR>\n"
955                         "<I><B>Reminder:</B> When sharing a room, "
956                         "it must be shared from both ends.  Adding a node to "
957                         "the 'shared' list sends messages out, but in order to"
958                         " receive messages, the other nodes must be configured"
959                         " to send messages out to your system as well.</I><BR>"
960                         "</CENTER>\n");
961
962         }
963
964         /* Mailing list management */
965         if (!strcmp(tab, "listserv")) {
966
967                 wprintf("<BR><center><i>The contents of this room are being "
968                         "mailed to the following list recipients:"
969                         "</i><br><br>\n");
970
971                 serv_puts("GNET");
972                 serv_gets(buf);
973                 if (buf[0]=='1') while (serv_gets(buf), strcmp(buf, "000")) {
974                         extract(cmd, buf, 0);
975                         if (!strcasecmp(cmd, "listrecp")) {
976                                 extract(recp, buf, 1);
977                         
978                                 escputs(recp);
979                                 wprintf(" <A HREF=\"/netedit&cmd=remove&line="
980                                         "listrecp|");
981                                 urlescputs(recp);
982                                 wprintf("&tab=listserv\">(remove)</A><BR>");
983
984                         }
985                 }
986                 wprintf("<BR><FORM METHOD=\"POST\" ACTION=\"/netedit\">\n"
987                         "<INPUT TYPE=\"hidden\" NAME=\"tab\" VALUE=\"listserv\">\n"
988                         "<INPUT TYPE=\"hidden\" NAME=\"prefix\" VALUE=\"listrecp|\">\n");
989                 wprintf("<INPUT TYPE=\"text\" NAME=\"line\">\n");
990                 wprintf("<INPUT TYPE=\"submit\" NAME=\"cmd\" VALUE=\"Add\">");
991                 wprintf("</FORM><BR></CENTER>\n");
992         }
993
994         wDumpContent(1);
995 }
996
997
998 /*
999  * save new parameters for a room
1000  */
1001 void editroom(void)
1002 {
1003         char buf[SIZ];
1004         char er_name[20];
1005         char er_password[10];
1006         char er_dirname[15];
1007         char er_roomaide[26];
1008         int er_floor;
1009         unsigned er_flags;
1010         int bump;
1011
1012
1013         if (strcmp(bstr("sc"), "OK")) {
1014                 display_error("Cancelled.  Changes were not saved.");
1015                 return;
1016         }
1017         serv_puts("GETR");
1018         serv_gets(buf);
1019
1020         if (buf[0] != '2') {
1021                 display_error(&buf[4]);
1022                 return;
1023         }
1024         extract(er_name, &buf[4], 0);
1025         extract(er_password, &buf[4], 1);
1026         extract(er_dirname, &buf[4], 2);
1027         er_flags = extract_int(&buf[4], 3);
1028
1029         strcpy(er_roomaide, bstr("er_roomaide"));
1030         if (strlen(er_roomaide) == 0) {
1031                 serv_puts("GETA");
1032                 serv_gets(buf);
1033                 if (buf[0] != '2') {
1034                         strcpy(er_roomaide, "");
1035                 } else {
1036                         extract(er_roomaide, &buf[4], 0);
1037                 }
1038         }
1039         strcpy(buf, bstr("er_name"));
1040         buf[20] = 0;
1041         if (strlen(buf) > 0)
1042                 strcpy(er_name, buf);
1043
1044         strcpy(buf, bstr("er_password"));
1045         buf[10] = 0;
1046         if (strlen(buf) > 0)
1047                 strcpy(er_password, buf);
1048
1049         strcpy(buf, bstr("er_dirname"));
1050         buf[15] = 0;
1051         if (strlen(buf) > 0)
1052                 strcpy(er_dirname, buf);
1053
1054         strcpy(buf, bstr("type"));
1055         er_flags &= !(QR_PRIVATE | QR_PASSWORDED | QR_GUESSNAME);
1056
1057         if (!strcmp(buf, "invonly")) {
1058                 er_flags |= (QR_PRIVATE);
1059         }
1060         if (!strcmp(buf, "guessname")) {
1061                 er_flags |= (QR_PRIVATE | QR_GUESSNAME);
1062         }
1063         if (!strcmp(buf, "passworded")) {
1064                 er_flags |= (QR_PRIVATE | QR_PASSWORDED);
1065         }
1066         if (!strcmp(bstr("prefonly"), "yes")) {
1067                 er_flags |= QR_PREFONLY;
1068         } else {
1069                 er_flags &= ~QR_PREFONLY;
1070         }
1071
1072         if (!strcmp(bstr("readonly"), "yes")) {
1073                 er_flags |= QR_READONLY;
1074         } else {
1075                 er_flags &= ~QR_READONLY;
1076         }
1077
1078         if (!strcmp(bstr("network"), "yes")) {
1079                 er_flags |= QR_NETWORK;
1080         } else {
1081                 er_flags &= ~QR_NETWORK;
1082         }
1083
1084         if (!strcmp(bstr("directory"), "yes")) {
1085                 er_flags |= QR_DIRECTORY;
1086         } else {
1087                 er_flags &= ~QR_DIRECTORY;
1088         }
1089
1090         if (!strcmp(bstr("ulallowed"), "yes")) {
1091                 er_flags |= QR_UPLOAD;
1092         } else {
1093                 er_flags &= ~QR_UPLOAD;
1094         }
1095
1096         if (!strcmp(bstr("dlallowed"), "yes")) {
1097                 er_flags |= QR_DOWNLOAD;
1098         } else {
1099                 er_flags &= ~QR_DOWNLOAD;
1100         }
1101
1102         if (!strcmp(bstr("visdir"), "yes")) {
1103                 er_flags |= QR_VISDIR;
1104         } else {
1105                 er_flags &= ~QR_VISDIR;
1106         }
1107
1108         strcpy(buf, bstr("anon"));
1109
1110         er_flags &= ~(QR_ANONONLY | QR_ANONOPT);
1111         if (!strcmp(buf, "anononly"))
1112                 er_flags |= QR_ANONONLY;
1113         if (!strcmp(buf, "anon2"))
1114                 er_flags |= QR_ANONOPT;
1115
1116         bump = 0;
1117         if (!strcmp(bstr("bump"), "yes"))
1118                 bump = 1;
1119
1120         er_floor = atoi(bstr("er_floor"));
1121
1122         sprintf(buf, "SETR %s|%s|%s|%u|%d|%d",
1123              er_name, er_password, er_dirname, er_flags, bump, er_floor);
1124         serv_puts(buf);
1125         serv_gets(buf);
1126         if (buf[0] != '2') {
1127                 display_error(&buf[4]);
1128                 return;
1129         }
1130         gotoroom(er_name, 0);
1131
1132         if (strlen(er_roomaide) > 0) {
1133                 sprintf(buf, "SETA %s", er_roomaide);
1134                 serv_puts(buf);
1135                 serv_gets(buf);
1136                 if (buf[0] != '2') {
1137                         display_error(&buf[4]);
1138                         return;
1139                 }
1140         }
1141         smart_goto(er_name);
1142 }
1143
1144 /*
1145  * Invite, Kick, and show Who Knows a room
1146  */
1147 void display_whok(void)
1148 {
1149         char buf[SIZ], room[SIZ], username[SIZ];
1150
1151         serv_puts("GETR");
1152         serv_gets(buf);
1153
1154         if (buf[0] != '2') {
1155                 display_error(&buf[4]);
1156                 return;
1157         }
1158         extract(room, &buf[4], 0);
1159
1160         strcpy(username, bstr("username"));
1161
1162         output_headers(1);
1163
1164         wprintf("<TABLE WIDTH=100%% BORDER=0 BGCOLOR=007700><TR><TD>");
1165         wprintf("<FONT SIZE=+1 COLOR=\"FFFFFF\"<B>Access control list for ");
1166         escputs(WC->wc_roomname);
1167         wprintf("</B></FONT></TD></TR></TABLE>\n");
1168
1169         if(!strcmp(bstr("sc"), "Kick")) {
1170                 sprintf(buf, "KICK %s", username);
1171                 serv_puts(buf);
1172                 serv_gets(buf);
1173
1174                 if (buf[0] != '2') {
1175                         display_error(&buf[4]);
1176                         return;
1177                 } else {
1178                         wprintf("<B><I>User %s kicked out of room %s.</I></B>\n", 
1179                                 username, room);
1180                 }
1181         } else if(!strcmp(bstr("sc"), "Invite")) {
1182                 sprintf(buf, "INVT %s", username);
1183                 serv_puts(buf);
1184                 serv_gets(buf);
1185
1186                 if (buf[0] != '2') {
1187                         display_error(&buf[4]);
1188                         return;
1189                 } else {
1190                         wprintf("<B><I>User %s invited to room %s.</I></B>\n", 
1191                                 username, room);
1192                 }
1193         }
1194         
1195
1196
1197         wprintf("<TABLE border=0 CELLSPACING=10><TR VALIGN=TOP>"
1198                 "<TD>The users listed below have access to this room.  "
1199                 "To remove a user from the access list, select the user "
1200                 "name from the list and click 'Kick'.<BR><BR>");
1201         
1202         wprintf("<CENTER><FORM METHOD=\"POST\" ACTION=\"/display_whok\">\n");
1203         wprintf("<SELECT NAME=\"username\" SIZE=10>\n");
1204         serv_puts("WHOK");
1205         serv_gets(buf);
1206         if (buf[0] == '1') {
1207                 while (serv_gets(buf), strcmp(buf, "000")) {
1208                         extract(username, buf, 0);
1209                         wprintf("<OPTION>");
1210                         escputs(username);
1211                         wprintf("\n");
1212                 }
1213         }
1214         wprintf("</SELECT><BR>\n");
1215
1216         wprintf("<input type=submit name=sc value=\"Kick\">");
1217         wprintf("</FORM></CENTER>\n");
1218
1219         wprintf("</TD><TD>"
1220                 "To grant another user access to this room, enter the "
1221                 "user name in the box below and click 'Invite'.<BR><BR>");
1222
1223         wprintf("<CENTER><FORM METHOD=\"POST\" ACTION=\"/display_whok\">\n");
1224         wprintf("Invite: ");
1225         wprintf("<input type=text name=username><BR>\n"
1226                 "<input type=hidden name=sc value=\"Invite\">"
1227                 "<input type=submit value=\"Invite\">"
1228                 "</FORM></CENTER>\n");
1229
1230         wprintf("</TD></TR></TABLE>\n");
1231         wDumpContent(1);
1232 }
1233
1234
1235
1236 /*
1237  * display the form for entering a new room
1238  */
1239 void display_entroom(void)
1240 {
1241         int i;
1242         char buf[SIZ];
1243
1244         serv_puts("CRE8 0");
1245         serv_gets(buf);
1246
1247         if (buf[0] != '2') {
1248                 display_error(&buf[4]);
1249                 return;
1250         }
1251         output_headers(1);
1252
1253         wprintf("<TABLE WIDTH=100%% BORDER=0 BGCOLOR=000077><TR><TD>");
1254         wprintf("<FONT SIZE=+1 COLOR=\"FFFFFF\"");
1255         wprintf("<B>Enter (create) a new room</B>\n");
1256         wprintf("</FONT></TD></TR></TABLE>\n");
1257
1258         wprintf("<FORM METHOD=\"POST\" ACTION=\"/entroom\">\n");
1259
1260         wprintf("<UL><LI>Name of room: ");
1261         wprintf("<INPUT TYPE=\"text\" NAME=\"er_name\" MAXLENGTH=\"19\">\n");
1262
1263         wprintf("<LI>Type of room:<UL>\n");
1264
1265         wprintf("<LI><INPUT TYPE=\"radio\" NAME=\"type\" VALUE=\"public\" ");
1266         wprintf("CHECKED > Public room\n");
1267
1268         wprintf("<LI><INPUT TYPE=\"radio\" NAME=\"type\" VALUE=\"guessname\" ");
1269         wprintf("> Private - guess name\n");
1270
1271         wprintf("<LI><INPUT TYPE=\"radio\" NAME=\"type\" VALUE=\"passworded\" ");
1272         wprintf("> Private - require password:\n");
1273         wprintf("<INPUT TYPE=\"text\" NAME=\"er_password\" MAXLENGTH=\"9\">\n");
1274
1275         wprintf("<LI><INPUT TYPE=\"radio\" NAME=\"type\" VALUE=\"invonly\" ");
1276         wprintf("> Private - invitation only\n");
1277         wprintf("</UL>\n");
1278
1279         wprintf("<LI>Resides on floor: ");
1280         load_floorlist(); 
1281         wprintf("<SELECT NAME=\"er_floor\" SIZE=\"1\">\n");
1282         for (i = 0; i < 128; ++i)
1283                 if (strlen(floorlist[i]) > 0) {
1284                         wprintf("<OPTION ");
1285                         wprintf("VALUE=\"%d\">", i);
1286                         escputs(floorlist[i]);
1287                         wprintf("</OPTION>\n");
1288                 }
1289         wprintf("</SELECT>\n");                
1290         wprintf("</UL>\n");
1291
1292
1293         wprintf("<CENTER>\n");
1294         wprintf("<INPUT TYPE=\"submit\" NAME=\"sc\" VALUE=\"OK\">");
1295         wprintf("<INPUT TYPE=\"submit\" NAME=\"sc\" VALUE=\"Cancel\">");
1296         wprintf("</CENTER>\n");
1297         wprintf("</FORM>\n<HR>");
1298         serv_printf("MESG roomaccess");
1299         serv_gets(buf);
1300         if (buf[0] == '1') {
1301                 fmout(NULL);
1302         }
1303         wDumpContent(1);
1304 }
1305
1306
1307
1308 /*
1309  * enter a new room
1310  */
1311 void entroom(void)
1312 {
1313         char buf[SIZ];
1314         char er_name[20];
1315         char er_type[20];
1316         char er_password[10];
1317         int er_floor;
1318         int er_num_type;
1319
1320         if (strcmp(bstr("sc"), "OK")) {
1321                 display_error("Cancelled.  No new room was created.");
1322                 return;
1323         }
1324         strcpy(er_name, bstr("er_name"));
1325         strcpy(er_type, bstr("type"));
1326         strcpy(er_password, bstr("er_password"));
1327         er_floor = atoi(bstr("er_floor"));
1328
1329         er_num_type = 0;
1330         if (!strcmp(er_type, "guessname"))
1331                 er_num_type = 1;
1332         if (!strcmp(er_type, "passworded"))
1333                 er_num_type = 2;
1334         if (!strcmp(er_type, "invonly"))
1335                 er_num_type = 3;
1336
1337         sprintf(buf, "CRE8 1|%s|%d|%s|%d", 
1338                 er_name, er_num_type, er_password, er_floor);
1339         serv_puts(buf);
1340         serv_gets(buf);
1341         if (buf[0] != '2') {
1342                 display_error(&buf[4]);
1343                 return;
1344         }
1345         smart_goto(er_name);
1346 }
1347
1348
1349 /*
1350  * display the screen to enter a private room
1351  */
1352 void display_private(char *rname, int req_pass)
1353 {
1354
1355         output_headers(1);
1356
1357         wprintf("<TABLE WIDTH=100%% BORDER=0 BGCOLOR=770000><TR><TD>");
1358         wprintf("<FONT SIZE=+1 COLOR=\"FFFFFF\"");
1359         wprintf("<B>Goto a private room</B>\n");
1360         wprintf("</FONT></TD></TR></TABLE>\n");
1361
1362         wprintf("<CENTER>\n");
1363         wprintf("If you know the name of a hidden (guess-name) or\n");
1364         wprintf("passworded room, you can enter that room by typing\n");
1365         wprintf("its name below.  Once you gain access to a private\n");
1366         wprintf("room, it will appear in your regular room listings\n");
1367         wprintf("so you don't have to keep returning here.\n");
1368         wprintf("<BR><BR>");
1369
1370         wprintf("<FORM METHOD=\"POST\" ACTION=\"/goto_private\">\n");
1371
1372         wprintf("<TABLE border><TR><TD>");
1373         wprintf("Enter room name:</TD><TD>");
1374         wprintf("<INPUT TYPE=\"text\" NAME=\"gr_name\" VALUE=\"%s\" MAXLENGTH=\"19\">\n", rname);
1375
1376         if (req_pass) {
1377                 wprintf("</TD></TR><TR><TD>");
1378                 wprintf("Enter room password:</TD><TD>");
1379                 wprintf("<INPUT TYPE=\"password\" NAME=\"gr_pass\" MAXLENGTH=\"9\">\n");
1380         }
1381         wprintf("</TD></TR></TABLE>\n");
1382
1383         wprintf("<INPUT TYPE=\"submit\" NAME=\"sc\" VALUE=\"OK\">");
1384         wprintf("<INPUT TYPE=\"submit\" NAME=\"sc\" VALUE=\"Cancel\">");
1385         wprintf("</FORM>\n");
1386         wDumpContent(1);
1387 }
1388
1389 /* 
1390  * goto a private room
1391  */
1392 void goto_private(void)
1393 {
1394         char hold_rm[32];
1395         char buf[SIZ];
1396
1397         if (strcasecmp(bstr("sc"), "OK")) {
1398                 display_main_menu();
1399                 return;
1400         }
1401         strcpy(hold_rm, WC->wc_roomname);
1402         strcpy(buf, "GOTO ");
1403         strcat(buf, bstr("gr_name"));
1404         strcat(buf, "|");
1405         strcat(buf, bstr("gr_pass"));
1406         serv_puts(buf);
1407         serv_gets(buf);
1408
1409         if (buf[0] == '2') {
1410                 smart_goto(bstr("gr_name"));
1411                 return;
1412         }
1413         if (!strncmp(buf, "540", 3)) {
1414                 display_private(bstr("gr_name"), 1);
1415                 return;
1416         }
1417         output_headers(1);
1418         wprintf("%s\n", &buf[4]);
1419         wDumpContent(1);
1420         return;
1421 }
1422
1423
1424 /*
1425  * display the screen to zap a room
1426  */
1427 void display_zap(void)
1428 {
1429         output_headers(1);
1430
1431         wprintf("<TABLE WIDTH=100%% BORDER=0 BGCOLOR=770000><TR><TD>");
1432         wprintf("<FONT SIZE=+1 COLOR=\"FFFFFF\"");
1433         wprintf("<B>Zap (forget) the current room</B>\n");
1434         wprintf("</FONT></TD></TR></TABLE>\n");
1435
1436         wprintf("If you select this option, <em>%s</em> will ", WC->wc_roomname);
1437         wprintf("disappear from your room list.  Is this what you wish ");
1438         wprintf("to do?<BR>\n");
1439
1440         wprintf("<FORM METHOD=\"POST\" ACTION=\"/zap\">\n");
1441         wprintf("<INPUT TYPE=\"submit\" NAME=\"sc\" VALUE=\"OK\">");
1442         wprintf("<INPUT TYPE=\"submit\" NAME=\"sc\" VALUE=\"Cancel\">");
1443         wprintf("</FORM>\n");
1444         wDumpContent(1);
1445 }
1446
1447
1448 /* 
1449  * zap a room
1450  */
1451 void zap(void)
1452 {
1453         char buf[SIZ];
1454         char final_destination[SIZ];
1455
1456         /* If the forget-room routine fails for any reason, we fall back
1457          * to the current room; otherwise, we go to the Lobby
1458          */
1459         strcpy(final_destination, WC->wc_roomname);
1460
1461         if (!strcasecmp(bstr("sc"), "OK")) {
1462                 serv_printf("GOTO %s", WC->wc_roomname);
1463                 serv_gets(buf);
1464                 if (buf[0] != '2') {
1465                         /* ExpressMessageCat(&buf[4]); */
1466                 } else {
1467                         serv_puts("FORG");
1468                         serv_gets(buf);
1469                         if (buf[0] != '2') {
1470                                 /* ExpressMessageCat(&buf[4]); */
1471                         } else {
1472                                 strcpy(final_destination, "_BASEROOM_");
1473                         }
1474                 }
1475         }
1476         smart_goto(final_destination);
1477 }
1478
1479
1480
1481
1482 /*
1483  * Confirm deletion of the current room
1484  */
1485 void confirm_delete_room(void)
1486 {
1487         char buf[SIZ];
1488
1489         serv_puts("KILL 0");
1490         serv_gets(buf);
1491         if (buf[0] != '2') {
1492                 display_error(&buf[4]);
1493                 return;
1494         }
1495         output_headers(1);
1496         wprintf("<TABLE WIDTH=100%% BORDER=0 BGCOLOR=770000><TR><TD>");
1497         wprintf("<FONT SIZE=+1 COLOR=\"FFFFFF\"");
1498         wprintf("<B>Confirm deletion of room</B>\n");
1499         wprintf("</FONT></TD></TR></TABLE>\n");
1500
1501         wprintf("<CENTER>");
1502         wprintf("<FORM METHOD=\"POST\" ACTION=\"/delete_room\">\n");
1503
1504         wprintf("Are you sure you want to delete <FONT SIZE=+1>");
1505         escputs(WC->wc_roomname);
1506         wprintf("</FONT>?<BR>\n");
1507
1508         wprintf("<INPUT TYPE=\"submit\" NAME=\"sc\" VALUE=\"Delete\">");
1509         wprintf("<INPUT TYPE=\"submit\" NAME=\"sc\" VALUE=\"Cancel\">");
1510
1511         wprintf("</FORM></CENTER>\n");
1512         wDumpContent(1);
1513 }
1514
1515
1516 /*
1517  * Delete the current room
1518  */
1519 void delete_room(void)
1520 {
1521         char buf[SIZ];
1522         char sc[SIZ];
1523
1524         strcpy(sc, bstr("sc"));
1525
1526         if (strcasecmp(sc, "Delete")) {
1527                 display_error("Cancelled.  This room was not deleted.");
1528                 return;
1529         }
1530         serv_puts("KILL 1");
1531         serv_gets(buf);
1532         if (buf[0] != '2') {
1533                 display_error(&buf[4]);
1534         } else {
1535                 smart_goto("_BASEROOM_");
1536         }
1537 }
1538
1539
1540
1541 /*
1542  * Perform changes to a room's network configuration
1543  */
1544 void netedit(void) {
1545         FILE *fp;
1546         char buf[SIZ];
1547         char line[SIZ];
1548
1549         if (strlen(bstr("line"))==0) {
1550                 display_editroom();
1551                 return;
1552         }
1553
1554         strcpy(line, bstr("prefix"));
1555         strcat(line, bstr("line"));
1556         strcat(line, bstr("suffix"));
1557
1558         fp = tmpfile();
1559         if (fp == NULL) {
1560                 display_editroom();
1561                 return;
1562         }
1563
1564         serv_puts("GNET");
1565         serv_gets(buf);
1566         if (buf[0] != '1') {
1567                 fclose(fp);
1568                 display_editroom();
1569                 return;
1570         }
1571
1572         /* This loop works for add *or* remove.  Spiffy, eh? */
1573         while (serv_gets(buf), strcmp(buf, "000")) {
1574                 if (strcasecmp(buf, line)) {
1575                         fprintf(fp, "%s\n", buf);
1576                 }
1577         }
1578
1579         rewind(fp);
1580         serv_puts("SNET");
1581         serv_gets(buf);
1582         if (buf[0] != '4') {
1583                 fclose(fp);
1584                 display_editroom();
1585                 return;
1586         }
1587
1588         while (fgets(buf, sizeof buf, fp) != NULL) {
1589                 buf[strlen(buf)-1] = 0;
1590                 serv_puts(buf);
1591         }
1592
1593         if (!strcasecmp(bstr("cmd"), "add")) {
1594                 serv_puts(line);
1595         }
1596
1597         serv_puts("000");
1598         fclose(fp);
1599         display_editroom();
1600 }
1601
1602
1603
1604 /*
1605  * Convert a room name to a folder-ish-looking name.
1606  */
1607 void room_to_folder(char *folder, char *room, int floor, int is_mailbox)
1608 {
1609         int i;
1610
1611         /*
1612          * For mailboxes, just do it straight...
1613          */
1614         if (is_mailbox) {
1615                 sprintf(folder, "My folders|%s", room);
1616         }
1617
1618         /*
1619          * Otherwise, prefix the floor name as a "public folders" moniker
1620          */
1621         else {
1622                 sprintf(folder, "%s|%s", floorlist[floor], room);
1623         }
1624
1625         /*
1626          * Replace "/" characters with "|" for pseudo-folder-delimiting
1627          */
1628         for (i=0; i<strlen(folder); ++i) {
1629                 if (folder[i] == '/') folder[i] = '|';
1630         }
1631 }
1632
1633
1634
1635
1636
1637
1638
1639 /*
1640  * Change the view for this room
1641  */
1642 void change_view(void) {
1643         int view;
1644         char buf[SIZ];
1645
1646         view = atol(bstr("view"));
1647
1648         serv_printf("VIEW %d", view);
1649         serv_gets(buf);
1650         smart_goto(WC->wc_roomname);
1651 }
1652
1653
1654 /*
1655  * Show the room list in "folders" format.  (only should get called by
1656  * knrooms() because that's where output_headers() is called from)
1657  */
1658 void folders(void) {
1659         char buf[SIZ];
1660
1661         int levels, oldlevels;
1662
1663         struct folder {
1664                 char room[SIZ];
1665                 char name[SIZ];
1666                 int hasnewmsgs;
1667                 int is_mailbox;
1668                 int selectable;
1669         };
1670
1671         struct folder *fold = NULL;
1672         struct folder ftmp;
1673         int max_folders = 0;
1674         int alloc_folders = 0;
1675         int i, j, k;
1676         int p;
1677         int flags;
1678         int floor;
1679         int nests = 0;
1680
1681         /* Start with the mailboxes */
1682         max_folders = 1;
1683         alloc_folders = 1;
1684         fold = malloc(sizeof(struct folder));
1685         memset(fold, 0, sizeof(struct folder));
1686         strcpy(fold[0].name, "My folders");
1687
1688         /* Then add floors */
1689         serv_puts("LFLR");
1690         serv_gets(buf);
1691         if (buf[0]=='1') while(serv_gets(buf), strcmp(buf, "000")) {
1692                 if (max_folders >= alloc_folders) {
1693                         alloc_folders = max_folders + 100;
1694                         fold = realloc(fold,
1695                                 alloc_folders * sizeof(struct folder));
1696                 }
1697                 memset(&fold[max_folders], 0, sizeof(struct folder));
1698                 extract(fold[max_folders].name, buf, 1);
1699                 ++max_folders;
1700         }
1701
1702         /* Now add rooms */
1703         for (p = 0; p < 2; ++p) {
1704                 if (p == 0) serv_puts("LKRN");
1705                 else if (p == 1) serv_puts("LKRO");
1706                 serv_gets(buf);
1707                 if (buf[0]=='1') while(serv_gets(buf), strcmp(buf, "000")) {
1708                         if (max_folders >= alloc_folders) {
1709                                 alloc_folders = max_folders + 100;
1710                                 fold = realloc(fold,
1711                                         alloc_folders * sizeof(struct folder));
1712                         }
1713                         memset(&fold[max_folders], 0, sizeof(struct folder));
1714                         extract(fold[max_folders].room, buf, 0);
1715                         if (p == 0) fold[max_folders].hasnewmsgs = 1;
1716                         flags = extract_int(buf, 1);
1717                         floor = extract_int(buf, 2);
1718                         if (flags & QR_MAILBOX) {
1719                                 fold[max_folders].is_mailbox = 1;
1720                         }
1721                         room_to_folder(fold[max_folders].name,
1722                                         fold[max_folders].room,
1723                                         floor,
1724                                         fold[max_folders].is_mailbox);
1725                         fold[max_folders].selectable = 1;
1726                         ++max_folders;
1727                 }
1728         }
1729
1730         /* Bubble-sort the folder list */
1731         for (i=0; i<max_folders; ++i) {
1732                 for (j=0; j<(max_folders-1)-i; ++j) {
1733                         if (strcasecmp(fold[j].name, fold[j+1].name) > 0) {
1734                                 memcpy(&ftmp, &fold[j], sizeof(struct folder));
1735                                 memcpy(&fold[j], &fold[j+1],
1736                                                         sizeof(struct folder));
1737                                 memcpy(&fold[j+1], &ftmp,
1738                                                         sizeof(struct folder));
1739                         }
1740                 }
1741         }
1742
1743         /* Output */
1744         nests = 0;
1745         levels = 0;
1746         oldlevels = 0;
1747         for (i=0; i<max_folders; ++i) {
1748
1749                 levels = num_tokens(fold[i].name, '|');
1750                 if (levels > oldlevels) {
1751                         for (k=0; k<(levels-oldlevels); ++k) {
1752                                 wprintf("<UL>");
1753                                 ++nests;
1754                         }
1755                 }
1756                 if (levels < oldlevels) {
1757                         for (k=0; k<(oldlevels-levels); ++k) {
1758                                 wprintf("</UL>");
1759                                 --nests;
1760                         }
1761                 }
1762                 oldlevels = levels;
1763
1764                 wprintf("<LI>");
1765                 if (fold[i].selectable) {
1766                         wprintf("<A HREF=\"/dotgoto?room=");
1767                         urlescputs(fold[i].room);
1768                         wprintf("\">");
1769                 }
1770                 if (fold[i].hasnewmsgs) wprintf("<B>");
1771                 extract(buf, fold[i].name, levels-1);
1772                 escputs(buf);
1773                 if (fold[i].hasnewmsgs) wprintf("</B>");
1774                 if (fold[i].selectable) wprintf("</A>\n");
1775         }
1776         while (nests-- > 0) wprintf("</UL>\n");
1777
1778         free(fold);
1779         wDumpContent(1);
1780 }
1781
1782
1783 /* Do either a known rooms list or a folders list, depending on the
1784  * user's preference
1785  */
1786 void knrooms() {
1787         char listviewpref[SIZ];
1788
1789         output_headers(3);
1790         load_floorlist();
1791
1792         /* Determine whether the user is trying to change views */
1793         if (bstr("view") != NULL) {
1794                 if (strlen(bstr("view")) > 0) {
1795                         set_preference("roomlistview", bstr("view"));
1796                 }
1797         }
1798
1799         get_preference("roomlistview", listviewpref);
1800
1801         if (strcasecmp(listviewpref, "folders")) {
1802                 strcpy(listviewpref, "rooms");
1803         }
1804
1805         /* title bar */
1806         wprintf("<TABLE WIDTH=100%% BORDER=0 BGCOLOR=000077><TR><TD>"
1807                 "<FONT SIZE=+1 COLOR=\"FFFFFF\"<B>"
1808         );
1809         if (!strcasecmp(listviewpref, "rooms")) {
1810                 wprintf("Room list");
1811         }
1812         if (!strcasecmp(listviewpref, "folders")) {
1813                 wprintf("Folder list");
1814         }
1815         wprintf("</B></TD>\n");
1816
1817
1818         /* offer the ability to switch views */
1819         wprintf("<TD><FORM NAME=\"roomlistomatic\">\n"
1820                 "<SELECT NAME=\"newview\" SIZE=\"1\" "
1821                 "OnChange=\"location.href=roomlistomatic.newview.options"
1822                 "[selectedIndex].value\">\n");
1823
1824         wprintf("<OPTION %s VALUE=\"/knrooms&view=rooms\">"
1825                 "View as room list"
1826                 "</OPTION>\n",
1827                 ( !strcasecmp(listviewpref, "rooms") ? "SELECTED" : "" )
1828         );
1829
1830         wprintf("<OPTION %s VALUE=\"/knrooms&view=folders\">"
1831                 "View as folder list"
1832                 "</OPTION>\n",
1833                 ( !strcasecmp(listviewpref, "folders") ? "SELECTED" : "" )
1834         );
1835
1836         wprintf("</SELECT></FORM></TD><TD>\n");
1837         offer_start_page();
1838         wprintf("</TD></TR></TABLE><BR>\n");
1839
1840         /* Display the room list in the user's preferred format */
1841         if (!strcasecmp(listviewpref, "folders")) {
1842                 folders();
1843         }
1844         else {
1845                 list_all_rooms_by_floor();
1846         }
1847 }