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