* self-service
[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
629
630
631 /*
632  * Set/clear/read the "self-service list subscribe" flag for a room
633  * 
634  * Set 'newval' to 0 to clear, 1 to set, any other value to leave unchanged.
635  * Always returns the new value.
636  */
637
638 int self_service(int newval) {
639         int current_value = 0;
640         char buf[SIZ];
641         
642         char name[SIZ];
643         char password[SIZ];
644         char dirname[SIZ];
645         int flags, floor, order, view, flags2;
646
647         serv_puts("GETR");
648         serv_gets(buf);
649         if (buf[0] != '2') return(0);
650
651         extract(name, &buf[4], 0);
652         extract(password, &buf[4], 1);
653         extract(dirname, &buf[4], 2);
654         flags = extract_int(&buf[4], 3);
655         floor = extract_int(&buf[4], 4);
656         order = extract_int(&buf[4], 5);
657         view = extract_int(&buf[4], 6);
658         flags2 = extract_int(&buf[4], 7);
659
660         if (flags2 & QR2_SELFLIST) {
661                 current_value = 1;
662         }
663         else {
664                 current_value = 0;
665         }
666
667         if (newval == 1) {
668                 flags2 = flags2 | QR2_SELFLIST;
669         }
670         else if (newval == 0) {
671                 flags2 = flags2 & ~QR2_SELFLIST;
672         }
673         else {
674                 return(current_value);
675         }
676
677         if (newval != current_value) {
678                 serv_printf("SETR %s|%s|%s|%d|0|%d|%d|%d|%d",
679                         name, password, dirname, flags,
680                         floor, order, view, flags2);
681                 serv_gets(buf);
682         }
683
684         return(newval);
685
686 }
687
688
689
690
691
692
693 /*
694  * display the form for editing a room
695  */
696 void display_editroom(void)
697 {
698         char buf[SIZ];
699         char cmd[SIZ];
700         char node[SIZ];
701         char recp[SIZ];
702         char er_name[20];
703         char er_password[10];
704         char er_dirname[15];
705         char er_roomaide[26];
706         unsigned er_flags;
707         int er_floor;
708         int i, j;
709         char *tab;
710         char *shared_with;
711         char *not_shared_with;
712
713         tab = bstr("tab");
714         if (strlen(tab) == 0) tab = "admin";
715
716         serv_puts("GETR");
717         serv_gets(buf);
718
719         if (buf[0] != '2') {
720                 display_error(&buf[4]);
721                 return;
722         }
723         extract(er_name, &buf[4], 0);
724         extract(er_password, &buf[4], 1);
725         extract(er_dirname, &buf[4], 2);
726         er_flags = extract_int(&buf[4], 3);
727         er_floor = extract_int(&buf[4], 4);
728
729         output_headers(1);
730
731         /* print the tabbed dialog */
732         wprintf("<TABLE border=0 cellspacing=0 cellpadding=0 width=100%%>"
733                 "<TR ALIGN=CENTER BGCOLOR=FFFFFF>"
734                 "<TD>&nbsp;</TD>\n");
735
736         if (!strcmp(tab, "admin")) {
737                 wprintf("<TD BGCOLOR=000077><FONT SIZE=+1 COLOR=\"FFFFFF\"><B>");
738         }
739         else {
740                 wprintf("<TD BGCOLOR=AAAAAA><A HREF=\"/display_editroom&tab=admin\">");
741         }
742         wprintf("Room administration");
743         if (!strcmp(tab, "admin")) {
744                 wprintf("</B></FONT></TD>\n");
745         }
746         else {
747                 wprintf("</A></TD>\n");
748         }
749
750         wprintf("<TD>&nbsp;</TD>\n");
751
752         if (!strcmp(tab, "config")) {
753                 wprintf("<TD BGCOLOR=000077><FONT SIZE=+1 COLOR=\"FFFFFF\"><B>");
754         }
755         else {
756                 wprintf("<TD BGCOLOR=AAAAAA><A HREF=\"/display_editroom&tab=config\">");
757         }
758         wprintf("Room configuration");
759         if (!strcmp(tab, "config")) {
760                 wprintf("</B></FONT></TD>\n");
761         }
762         else {
763                 wprintf("</A></TD>\n");
764         }
765
766         wprintf("<TD>&nbsp;</TD>\n");
767
768         if (!strcmp(tab, "sharing")) {
769                 wprintf("<TD BGCOLOR=000077><FONT SIZE=+1 COLOR=\"FFFFFF\"><B>");
770         }
771         else {
772                 wprintf("<TD BGCOLOR=AAAAAA><A HREF=\"/display_editroom&tab=sharing\">");
773         }
774         wprintf("Sharing");
775         if (!strcmp(tab, "sharing")) {
776                 wprintf("</B></FONT></TD>\n");
777         }
778         else {
779                 wprintf("</A></TD>\n");
780         }
781
782         wprintf("<TD>&nbsp;</TD>\n");
783
784         if (!strcmp(tab, "listserv")) {
785                 wprintf("<TD BGCOLOR=000077><FONT SIZE=+1 COLOR=\"FFFFFF\"><B>");
786         }
787         else {
788                 wprintf("<TD BGCOLOR=AAAAAA><A HREF=\"/display_editroom&tab=listserv\">");
789         }
790         wprintf("Mailing list service");
791         if (!strcmp(tab, "listserv")) {
792                 wprintf("</B></FONT></TD>\n");
793         }
794         else {
795                 wprintf("</A></TD>\n");
796         }
797
798         wprintf("<TD>&nbsp;</TD></TR>"
799                 "<TR><TD BGCOLOR=000077 COLSPAN=9 HEIGHT=5> </TD></TR>"
800                 "</TABLE>\n");
801
802         /* end tabbed dialog */ 
803
804
805         if (!strcmp(tab, "admin")) {
806                 wprintf("<UL>"
807                         "<LI><A HREF=\"/confirm_delete_room\">\n"
808                         "Delete this room</A>\n"
809                         "<LI><A HREF=\"/display_editroompic\">\n"
810                         "Set or change the graphic for this room's banner</A>\n"
811                         "<LI><A HREF=\"/display_editinfo\">\n"
812                         "Edit this room's Info file</A>\n"
813                         "</UL>");
814         }
815
816         if (!strcmp(tab, "config")) {
817                 wprintf("<FORM METHOD=\"POST\" ACTION=\"/editroom\">\n");
818         
819                 wprintf("<UL><LI>Name of room: ");
820                 wprintf("<INPUT TYPE=\"text\" NAME=\"er_name\" VALUE=\"%s\" MAXLENGTH=\"19\">\n", er_name);
821         
822                 wprintf("<LI>Resides on floor: ");
823                 load_floorlist();
824                 wprintf("<SELECT NAME=\"er_floor\" SIZE=\"1\">\n");
825                 for (i = 0; i < 128; ++i)
826                         if (strlen(floorlist[i]) > 0) {
827                                 wprintf("<OPTION ");
828                                 if (i == er_floor)
829                                         wprintf("SELECTED ");
830                                 wprintf("VALUE=\"%d\">", i);
831                                 escputs(floorlist[i]);
832                                 wprintf("</OPTION>\n");
833                         }
834                 wprintf("</SELECT>\n");
835         
836                 wprintf("<LI>Type of room:<UL>\n");
837
838                 wprintf("<LI><INPUT TYPE=\"radio\" NAME=\"type\" VALUE=\"public\" ");
839                 if ((er_flags & QR_PRIVATE) == 0)
840                 wprintf("CHECKED ");
841                 wprintf("> Public room\n");
842
843                 wprintf("<LI><INPUT TYPE=\"radio\" NAME=\"type\" VALUE=\"guessname\" ");
844                 if ((er_flags & QR_PRIVATE) &&
845                     (er_flags & QR_GUESSNAME))
846                         wprintf("CHECKED ");
847                 wprintf("> Private - guess name\n");
848         
849                 wprintf("<LI><INPUT TYPE=\"radio\" NAME=\"type\" VALUE=\"passworded\" ");
850                 if ((er_flags & QR_PRIVATE) &&
851                     (er_flags & QR_PASSWORDED))
852                         wprintf("CHECKED ");
853                 wprintf("> Private - require password:\n");
854                 wprintf("<INPUT TYPE=\"text\" NAME=\"er_password\" VALUE=\"%s\" MAXLENGTH=\"9\">\n", er_password);
855         
856                 wprintf("<LI><INPUT TYPE=\"radio\" NAME=\"type\" VALUE=\"invonly\" ");
857                 if ((er_flags & QR_PRIVATE)
858                     && ((er_flags & QR_GUESSNAME) == 0)
859                     && ((er_flags & QR_PASSWORDED) == 0))
860                         wprintf("CHECKED ");
861                 wprintf("> Private - invitation only\n");
862         
863                 wprintf("<LI><INPUT TYPE=\"checkbox\" NAME=\"bump\" VALUE=\"yes\" ");
864                 wprintf("> If private, cause current users to forget room\n");
865         
866                 wprintf("</UL>\n");
867         
868                 wprintf("<LI><INPUT TYPE=\"checkbox\" NAME=\"prefonly\" VALUE=\"yes\" ");
869                 if (er_flags & QR_PREFONLY)
870                         wprintf("CHECKED ");
871                 wprintf("> Preferred users only\n");
872         
873                 wprintf("<LI><INPUT TYPE=\"checkbox\" NAME=\"readonly\" VALUE=\"yes\" ");
874                 if (er_flags & QR_READONLY)
875                         wprintf("CHECKED ");
876                 wprintf("> Read-only room\n");
877         
878         /* directory stuff */
879                 wprintf("<LI><INPUT TYPE=\"checkbox\" NAME=\"directory\" VALUE=\"yes\" ");
880                 if (er_flags & QR_DIRECTORY)
881                         wprintf("CHECKED ");
882                 wprintf("> File directory room\n");
883
884                 wprintf("<UL><LI>Directory name: ");
885                 wprintf("<INPUT TYPE=\"text\" NAME=\"er_dirname\" VALUE=\"%s\" MAXLENGTH=\"14\">\n", er_dirname);
886
887                 wprintf("<LI><INPUT TYPE=\"checkbox\" NAME=\"ulallowed\" VALUE=\"yes\" ");
888                 if (er_flags & QR_UPLOAD)
889                         wprintf("CHECKED ");
890                 wprintf("> Uploading allowed\n");
891         
892                 wprintf("<LI><INPUT TYPE=\"checkbox\" NAME=\"dlallowed\" VALUE=\"yes\" ");
893                 if (er_flags & QR_DOWNLOAD)
894                         wprintf("CHECKED ");
895                 wprintf("> Downloading allowed\n");
896         
897                 wprintf("<LI><INPUT TYPE=\"checkbox\" NAME=\"visdir\" VALUE=\"yes\" ");
898                 if (er_flags & QR_VISDIR)
899                         wprintf("CHECKED ");
900                 wprintf("> Visible directory</UL>\n");
901         
902         /* end of directory stuff */
903         
904                 wprintf("<LI><INPUT TYPE=\"checkbox\" NAME=\"network\" VALUE=\"yes\" ");
905                 if (er_flags & QR_NETWORK)
906                         wprintf("CHECKED ");
907                 wprintf("> Network shared room\n");
908
909         /* start of anon options */
910         
911                 wprintf("<LI>Anonymous messages<UL>\n");
912         
913                 wprintf("<LI><INPUT TYPE=\"radio\" NAME=\"anon\" VALUE=\"no\" ");
914                 if (((er_flags & QR_ANONONLY) == 0)
915                     && ((er_flags & QR_ANONOPT) == 0))
916                         wprintf("CHECKED ");
917                 wprintf("> No anonymous messages\n");
918         
919                 wprintf("<LI><INPUT TYPE=\"radio\" NAME=\"anon\" VALUE=\"anononly\" ");
920                 if (er_flags & QR_ANONONLY)
921                         wprintf("CHECKED ");
922                 wprintf("> All messages are anonymous\n");
923         
924                 wprintf("<LI><INPUT TYPE=\"radio\" NAME=\"anon\" VALUE=\"anon2\" ");
925                 if (er_flags & QR_ANONOPT)
926                         wprintf("CHECKED ");
927                 wprintf("> Prompt user when entering messages</UL>\n");
928         
929         /* end of anon options */
930         
931                 wprintf("<LI>Room aide: \n");
932                 serv_puts("GETA");
933                 serv_gets(buf);
934                 if (buf[0] != '2') {
935                         wprintf("<EM>%s</EM>\n", &buf[4]);
936                 } else {
937                         extract(er_roomaide, &buf[4], 0);
938                         wprintf("<INPUT TYPE=\"text\" NAME=\"er_roomaide\" VALUE=\"%s\" MAXLENGTH=\"25\">\n", er_roomaide);
939                 }
940         
941                 wprintf("</UL><CENTER>\n");
942                 wprintf("<INPUT TYPE=\"submit\" NAME=\"sc\" VALUE=\"OK\">");
943                 wprintf("<INPUT TYPE=\"submit\" NAME=\"sc\" VALUE=\"Cancel\">");
944                 wprintf("</CENTER>\n");
945         }
946
947
948         /* Sharing the room with other Citadel nodes... */
949         if (!strcmp(tab, "sharing")) {
950
951                 shared_with = strdup("");
952                 not_shared_with = strdup("");
953
954                 /* Learn the current configuration */
955                 serv_puts("CONF getsys|application/x-citadel-ignet-config");
956                 serv_gets(buf);
957                 if (buf[0]=='1') while (serv_gets(buf), strcmp(buf, "000")) {
958                         extract(node, buf, 0);
959                         not_shared_with = realloc(not_shared_with,
960                                         strlen(not_shared_with) + 32);
961                         strcat(not_shared_with, node);
962                         strcat(not_shared_with, "|");
963                 }
964
965                 serv_puts("GNET");
966                 serv_gets(buf);
967                 if (buf[0]=='1') while (serv_gets(buf), strcmp(buf, "000")) {
968                         extract(cmd, buf, 0);
969                         extract(node, buf, 1);
970                         if (!strcasecmp(cmd, "ignet_push_share")) {
971                                 shared_with = realloc(shared_with,
972                                                 strlen(shared_with) + 32);
973                                 strcat(shared_with, node);
974                                 strcat(shared_with, "|");
975                         }
976                 }
977
978                 for (i=0; i<num_tokens(shared_with, '|'); ++i) {
979                         extract(node, shared_with, i);
980                         for (j=0; j<num_tokens(not_shared_with, '|'); ++j) {
981                                 extract(cmd, not_shared_with, j);
982                                 if (!strcasecmp(node, cmd)) {
983                                         remove_token(not_shared_with, j, '|');
984                                 }
985                         }
986                 }
987
988                 /* Display the stuff */
989                 wprintf("<CENTER><BR>"
990                         "<TABLE border=1 cellpadding=5><TR>"
991                         "<TD><B><I>Shared with</I></B></TD>"
992                         "<TD><B><I>Not shared with</I></B></TD></TR>\n"
993                         "<TR><TD>\n");
994
995                 for (i=0; i<num_tokens(shared_with, '|'); ++i) {
996                         extract(node, shared_with, i);
997                         if (strlen(node) > 0) {
998                                 wprintf("%s ", node);
999                                 wprintf("<A HREF=\"/netedit&cmd=remove&line="
1000                                         "ignet_push_share|");
1001                                 urlescputs(node);
1002                                 wprintf("&tab=sharing\">(unshare)</A><BR>");
1003                         }
1004                 }
1005
1006                 wprintf("</TD><TD>\n");
1007
1008                 for (i=0; i<num_tokens(not_shared_with, '|'); ++i) {
1009                         extract(node, not_shared_with, i);
1010                         if (strlen(node) > 0) {
1011                                 wprintf("%s ", node);
1012                                 wprintf("<A HREF=\"/netedit&cmd=add&line="
1013                                         "ignet_push_share|");
1014                                 urlescputs(node);
1015                                 wprintf("&tab=sharing\">(share)</A><BR>");
1016                         }
1017                 }
1018
1019                 wprintf("</TD></TR>"
1020                         "</TABLE><BR>\n"
1021                         "<I><B>Reminder:</B> When sharing a room, "
1022                         "it must be shared from both ends.  Adding a node to "
1023                         "the 'shared' list sends messages out, but in order to"
1024                         " receive messages, the other nodes must be configured"
1025                         " to send messages out to your system as well.</I><BR>"
1026                         "</CENTER>\n");
1027
1028         }
1029
1030         /* Mailing list management */
1031         if (!strcmp(tab, "listserv")) {
1032
1033                 wprintf("<BR><center>"
1034                         "<TABLE BORDER=0 WIDTH=100%% CELLPADDING=5>"
1035                         "<TR><TD VALIGN=TOP>");
1036
1037                 wprintf("<i>The contents of this room are being "
1038                         "mailed <b>as individual messages</b> "
1039                         "to the following list recipients:"
1040                         "</i><br><br>\n");
1041
1042                 serv_puts("GNET");
1043                 serv_gets(buf);
1044                 if (buf[0]=='1') while (serv_gets(buf), strcmp(buf, "000")) {
1045                         extract(cmd, buf, 0);
1046                         if (!strcasecmp(cmd, "listrecp")) {
1047                                 extract(recp, buf, 1);
1048                         
1049                                 escputs(recp);
1050                                 wprintf(" <A HREF=\"/netedit&cmd=remove&line="
1051                                         "listrecp|");
1052                                 urlescputs(recp);
1053                                 wprintf("&tab=listserv\">(remove)</A><BR>");
1054
1055                         }
1056                 }
1057                 wprintf("<BR><FORM METHOD=\"POST\" ACTION=\"/netedit\">\n"
1058                         "<INPUT TYPE=\"hidden\" NAME=\"tab\" VALUE=\"listserv\">\n"
1059                         "<INPUT TYPE=\"hidden\" NAME=\"prefix\" VALUE=\"listrecp|\">\n");
1060                 wprintf("<INPUT TYPE=\"text\" NAME=\"line\">\n");
1061                 wprintf("<INPUT TYPE=\"submit\" NAME=\"cmd\" VALUE=\"Add\">");
1062                 wprintf("</FORM>\n");
1063
1064                 wprintf("</TD><TD VALIGN=TOP>\n");
1065                 
1066                 wprintf("<i>The contents of this room are being "
1067                         "mailed <b>in digest form</b> "
1068                         "to the following list recipients:"
1069                         "</i><br><br>\n");
1070
1071                 serv_puts("GNET");
1072                 serv_gets(buf);
1073                 if (buf[0]=='1') while (serv_gets(buf), strcmp(buf, "000")) {
1074                         extract(cmd, buf, 0);
1075                         if (!strcasecmp(cmd, "digestrecp")) {
1076                                 extract(recp, buf, 1);
1077                         
1078                                 escputs(recp);
1079                                 wprintf(" <A HREF=\"/netedit&cmd=remove&line="
1080                                         "digestrecp|");
1081                                 urlescputs(recp);
1082                                 wprintf("&tab=listserv\">(remove)</A><BR>");
1083
1084                         }
1085                 }
1086                 wprintf("<BR><FORM METHOD=\"POST\" ACTION=\"/netedit\">\n"
1087                         "<INPUT TYPE=\"hidden\" NAME=\"tab\" VALUE=\"listserv\">\n"
1088                         "<INPUT TYPE=\"hidden\" NAME=\"prefix\" VALUE=\"digestrecp|\">\n");
1089                 wprintf("<INPUT TYPE=\"text\" NAME=\"line\">\n");
1090                 wprintf("<INPUT TYPE=\"submit\" NAME=\"cmd\" VALUE=\"Add\">");
1091                 wprintf("</FORM>\n");
1092                 
1093                 wprintf("</TD></TR></TABLE><HR>\n");
1094
1095                 if (self_service(999) == 1) {
1096                         wprintf("This room is configured to allow "
1097                                 "self-service subscribe/unsubscribe requests."
1098                                 " <A HREF=\"/toggle_self_service?newval=0&"
1099                                 "tab=listserv\">"
1100                                 "Click to disable.</A><BR>\n"
1101                         );
1102                 }
1103                 else {
1104                         wprintf("This room is <i>not</i> configured to allow "
1105                                 "self-service subscribe/unsubscribe requests."
1106                                 " <A HREF=\"/toggle_self_service?newval=1&"
1107                                 "tab=listserv\">"
1108                                 "Click to enable.</A><BR>\n"
1109                         );
1110                 }
1111
1112
1113                 wprintf("</CENTER>\n");
1114         }
1115
1116         wDumpContent(1);
1117 }
1118
1119
1120 /* 
1121  * Toggle self-service list subscription
1122  */
1123 void toggle_self_service(void) {
1124         int newval = 0;
1125
1126         newval = atoi(bstr("newval"));
1127         self_service(newval);
1128         display_editroom();
1129 }
1130
1131
1132
1133 /*
1134  * save new parameters for a room
1135  */
1136 void editroom(void)
1137 {
1138         char buf[SIZ];
1139         char er_name[20];
1140         char er_password[10];
1141         char er_dirname[15];
1142         char er_roomaide[26];
1143         int er_floor;
1144         unsigned er_flags;
1145         int bump;
1146
1147
1148         if (strcmp(bstr("sc"), "OK")) {
1149                 display_error("Cancelled.  Changes were not saved.");
1150                 return;
1151         }
1152         serv_puts("GETR");
1153         serv_gets(buf);
1154
1155         if (buf[0] != '2') {
1156                 display_error(&buf[4]);
1157                 return;
1158         }
1159         extract(er_name, &buf[4], 0);
1160         extract(er_password, &buf[4], 1);
1161         extract(er_dirname, &buf[4], 2);
1162         er_flags = extract_int(&buf[4], 3);
1163
1164         strcpy(er_roomaide, bstr("er_roomaide"));
1165         if (strlen(er_roomaide) == 0) {
1166                 serv_puts("GETA");
1167                 serv_gets(buf);
1168                 if (buf[0] != '2') {
1169                         strcpy(er_roomaide, "");
1170                 } else {
1171                         extract(er_roomaide, &buf[4], 0);
1172                 }
1173         }
1174         strcpy(buf, bstr("er_name"));
1175         buf[20] = 0;
1176         if (strlen(buf) > 0)
1177                 strcpy(er_name, buf);
1178
1179         strcpy(buf, bstr("er_password"));
1180         buf[10] = 0;
1181         if (strlen(buf) > 0)
1182                 strcpy(er_password, buf);
1183
1184         strcpy(buf, bstr("er_dirname"));
1185         buf[15] = 0;
1186         if (strlen(buf) > 0)
1187                 strcpy(er_dirname, buf);
1188
1189         strcpy(buf, bstr("type"));
1190         er_flags &= !(QR_PRIVATE | QR_PASSWORDED | QR_GUESSNAME);
1191
1192         if (!strcmp(buf, "invonly")) {
1193                 er_flags |= (QR_PRIVATE);
1194         }
1195         if (!strcmp(buf, "guessname")) {
1196                 er_flags |= (QR_PRIVATE | QR_GUESSNAME);
1197         }
1198         if (!strcmp(buf, "passworded")) {
1199                 er_flags |= (QR_PRIVATE | QR_PASSWORDED);
1200         }
1201         if (!strcmp(bstr("prefonly"), "yes")) {
1202                 er_flags |= QR_PREFONLY;
1203         } else {
1204                 er_flags &= ~QR_PREFONLY;
1205         }
1206
1207         if (!strcmp(bstr("readonly"), "yes")) {
1208                 er_flags |= QR_READONLY;
1209         } else {
1210                 er_flags &= ~QR_READONLY;
1211         }
1212
1213         if (!strcmp(bstr("network"), "yes")) {
1214                 er_flags |= QR_NETWORK;
1215         } else {
1216                 er_flags &= ~QR_NETWORK;
1217         }
1218
1219         if (!strcmp(bstr("directory"), "yes")) {
1220                 er_flags |= QR_DIRECTORY;
1221         } else {
1222                 er_flags &= ~QR_DIRECTORY;
1223         }
1224
1225         if (!strcmp(bstr("ulallowed"), "yes")) {
1226                 er_flags |= QR_UPLOAD;
1227         } else {
1228                 er_flags &= ~QR_UPLOAD;
1229         }
1230
1231         if (!strcmp(bstr("dlallowed"), "yes")) {
1232                 er_flags |= QR_DOWNLOAD;
1233         } else {
1234                 er_flags &= ~QR_DOWNLOAD;
1235         }
1236
1237         if (!strcmp(bstr("visdir"), "yes")) {
1238                 er_flags |= QR_VISDIR;
1239         } else {
1240                 er_flags &= ~QR_VISDIR;
1241         }
1242
1243         strcpy(buf, bstr("anon"));
1244
1245         er_flags &= ~(QR_ANONONLY | QR_ANONOPT);
1246         if (!strcmp(buf, "anononly"))
1247                 er_flags |= QR_ANONONLY;
1248         if (!strcmp(buf, "anon2"))
1249                 er_flags |= QR_ANONOPT;
1250
1251         bump = 0;
1252         if (!strcmp(bstr("bump"), "yes"))
1253                 bump = 1;
1254
1255         er_floor = atoi(bstr("er_floor"));
1256
1257         sprintf(buf, "SETR %s|%s|%s|%u|%d|%d",
1258              er_name, er_password, er_dirname, er_flags, bump, er_floor);
1259         serv_puts(buf);
1260         serv_gets(buf);
1261         if (buf[0] != '2') {
1262                 display_error(&buf[4]);
1263                 return;
1264         }
1265         gotoroom(er_name, 0);
1266
1267         if (strlen(er_roomaide) > 0) {
1268                 sprintf(buf, "SETA %s", er_roomaide);
1269                 serv_puts(buf);
1270                 serv_gets(buf);
1271                 if (buf[0] != '2') {
1272                         display_error(&buf[4]);
1273                         return;
1274                 }
1275         }
1276         smart_goto(er_name);
1277 }
1278
1279 /*
1280  * Invite, Kick, and show Who Knows a room
1281  */
1282 void display_whok(void)
1283 {
1284         char buf[SIZ], room[SIZ], username[SIZ];
1285
1286         serv_puts("GETR");
1287         serv_gets(buf);
1288
1289         if (buf[0] != '2') {
1290                 display_error(&buf[4]);
1291                 return;
1292         }
1293         extract(room, &buf[4], 0);
1294
1295         strcpy(username, bstr("username"));
1296
1297         output_headers(1);
1298
1299         wprintf("<TABLE WIDTH=100%% BORDER=0 BGCOLOR=007700><TR><TD>");
1300         wprintf("<FONT SIZE=+1 COLOR=\"FFFFFF\"<B>Access control list for ");
1301         escputs(WC->wc_roomname);
1302         wprintf("</B></FONT></TD></TR></TABLE>\n");
1303
1304         if(!strcmp(bstr("sc"), "Kick")) {
1305                 sprintf(buf, "KICK %s", username);
1306                 serv_puts(buf);
1307                 serv_gets(buf);
1308
1309                 if (buf[0] != '2') {
1310                         display_error(&buf[4]);
1311                         return;
1312                 } else {
1313                         wprintf("<B><I>User %s kicked out of room %s.</I></B>\n", 
1314                                 username, room);
1315                 }
1316         } else if(!strcmp(bstr("sc"), "Invite")) {
1317                 sprintf(buf, "INVT %s", username);
1318                 serv_puts(buf);
1319                 serv_gets(buf);
1320
1321                 if (buf[0] != '2') {
1322                         display_error(&buf[4]);
1323                         return;
1324                 } else {
1325                         wprintf("<B><I>User %s invited to room %s.</I></B>\n", 
1326                                 username, room);
1327                 }
1328         }
1329         
1330
1331
1332         wprintf("<TABLE border=0 CELLSPACING=10><TR VALIGN=TOP>"
1333                 "<TD>The users listed below have access to this room.  "
1334                 "To remove a user from the access list, select the user "
1335                 "name from the list and click 'Kick'.<BR><BR>");
1336         
1337         wprintf("<CENTER><FORM METHOD=\"POST\" ACTION=\"/display_whok\">\n");
1338         wprintf("<SELECT NAME=\"username\" SIZE=10>\n");
1339         serv_puts("WHOK");
1340         serv_gets(buf);
1341         if (buf[0] == '1') {
1342                 while (serv_gets(buf), strcmp(buf, "000")) {
1343                         extract(username, buf, 0);
1344                         wprintf("<OPTION>");
1345                         escputs(username);
1346                         wprintf("\n");
1347                 }
1348         }
1349         wprintf("</SELECT><BR>\n");
1350
1351         wprintf("<input type=submit name=sc value=\"Kick\">");
1352         wprintf("</FORM></CENTER>\n");
1353
1354         wprintf("</TD><TD>"
1355                 "To grant another user access to this room, enter the "
1356                 "user name in the box below and click 'Invite'.<BR><BR>");
1357
1358         wprintf("<CENTER><FORM METHOD=\"POST\" ACTION=\"/display_whok\">\n");
1359         wprintf("Invite: ");
1360         wprintf("<input type=text name=username><BR>\n"
1361                 "<input type=hidden name=sc value=\"Invite\">"
1362                 "<input type=submit value=\"Invite\">"
1363                 "</FORM></CENTER>\n");
1364
1365         wprintf("</TD></TR></TABLE>\n");
1366         wDumpContent(1);
1367 }
1368
1369
1370
1371 /*
1372  * display the form for entering a new room
1373  */
1374 void display_entroom(void)
1375 {
1376         int i;
1377         char buf[SIZ];
1378
1379         serv_puts("CRE8 0");
1380         serv_gets(buf);
1381
1382         if (buf[0] != '2') {
1383                 display_error(&buf[4]);
1384                 return;
1385         }
1386         output_headers(1);
1387
1388         wprintf("<TABLE WIDTH=100%% BORDER=0 BGCOLOR=000077><TR><TD>");
1389         wprintf("<FONT SIZE=+1 COLOR=\"FFFFFF\"");
1390         wprintf("<B>Enter (create) a new room</B>\n");
1391         wprintf("</FONT></TD></TR></TABLE>\n");
1392
1393         wprintf("<FORM METHOD=\"POST\" ACTION=\"/entroom\">\n");
1394
1395         wprintf("<UL><LI>Name of room: ");
1396         wprintf("<INPUT TYPE=\"text\" NAME=\"er_name\" MAXLENGTH=\"19\">\n");
1397
1398         wprintf("<LI>Type of room:<UL>\n");
1399
1400         wprintf("<LI><INPUT TYPE=\"radio\" NAME=\"type\" VALUE=\"public\" ");
1401         wprintf("CHECKED > Public room\n");
1402
1403         wprintf("<LI><INPUT TYPE=\"radio\" NAME=\"type\" VALUE=\"guessname\" ");
1404         wprintf("> Private - guess name\n");
1405
1406         wprintf("<LI><INPUT TYPE=\"radio\" NAME=\"type\" VALUE=\"passworded\" ");
1407         wprintf("> Private - require password:\n");
1408         wprintf("<INPUT TYPE=\"text\" NAME=\"er_password\" MAXLENGTH=\"9\">\n");
1409
1410         wprintf("<LI><INPUT TYPE=\"radio\" NAME=\"type\" VALUE=\"invonly\" ");
1411         wprintf("> Private - invitation only\n");
1412         wprintf("</UL>\n");
1413
1414         wprintf("<LI>Resides on floor: ");
1415         load_floorlist(); 
1416         wprintf("<SELECT NAME=\"er_floor\" SIZE=\"1\">\n");
1417         for (i = 0; i < 128; ++i)
1418                 if (strlen(floorlist[i]) > 0) {
1419                         wprintf("<OPTION ");
1420                         wprintf("VALUE=\"%d\">", i);
1421                         escputs(floorlist[i]);
1422                         wprintf("</OPTION>\n");
1423                 }
1424         wprintf("</SELECT>\n");                
1425         wprintf("</UL>\n");
1426
1427
1428         wprintf("<CENTER>\n");
1429         wprintf("<INPUT TYPE=\"submit\" NAME=\"sc\" VALUE=\"OK\">");
1430         wprintf("<INPUT TYPE=\"submit\" NAME=\"sc\" VALUE=\"Cancel\">");
1431         wprintf("</CENTER>\n");
1432         wprintf("</FORM>\n<HR>");
1433         serv_printf("MESG roomaccess");
1434         serv_gets(buf);
1435         if (buf[0] == '1') {
1436                 fmout(NULL);
1437         }
1438         wDumpContent(1);
1439 }
1440
1441
1442
1443 /*
1444  * enter a new room
1445  */
1446 void entroom(void)
1447 {
1448         char buf[SIZ];
1449         char er_name[20];
1450         char er_type[20];
1451         char er_password[10];
1452         int er_floor;
1453         int er_num_type;
1454
1455         if (strcmp(bstr("sc"), "OK")) {
1456                 display_error("Cancelled.  No new room was created.");
1457                 return;
1458         }
1459         strcpy(er_name, bstr("er_name"));
1460         strcpy(er_type, bstr("type"));
1461         strcpy(er_password, bstr("er_password"));
1462         er_floor = atoi(bstr("er_floor"));
1463
1464         er_num_type = 0;
1465         if (!strcmp(er_type, "guessname"))
1466                 er_num_type = 1;
1467         if (!strcmp(er_type, "passworded"))
1468                 er_num_type = 2;
1469         if (!strcmp(er_type, "invonly"))
1470                 er_num_type = 3;
1471
1472         sprintf(buf, "CRE8 1|%s|%d|%s|%d", 
1473                 er_name, er_num_type, er_password, er_floor);
1474         serv_puts(buf);
1475         serv_gets(buf);
1476         if (buf[0] != '2') {
1477                 display_error(&buf[4]);
1478                 return;
1479         }
1480         smart_goto(er_name);
1481 }
1482
1483
1484 /*
1485  * display the screen to enter a private room
1486  */
1487 void display_private(char *rname, int req_pass)
1488 {
1489
1490         output_headers(1);
1491
1492         wprintf("<TABLE WIDTH=100%% BORDER=0 BGCOLOR=770000><TR><TD>");
1493         wprintf("<FONT SIZE=+1 COLOR=\"FFFFFF\"");
1494         wprintf("<B>Goto a private room</B>\n");
1495         wprintf("</FONT></TD></TR></TABLE>\n");
1496
1497         wprintf("<CENTER>\n");
1498         wprintf("If you know the name of a hidden (guess-name) or\n");
1499         wprintf("passworded room, you can enter that room by typing\n");
1500         wprintf("its name below.  Once you gain access to a private\n");
1501         wprintf("room, it will appear in your regular room listings\n");
1502         wprintf("so you don't have to keep returning here.\n");
1503         wprintf("<BR><BR>");
1504
1505         wprintf("<FORM METHOD=\"POST\" ACTION=\"/goto_private\">\n");
1506
1507         wprintf("<TABLE border><TR><TD>");
1508         wprintf("Enter room name:</TD><TD>");
1509         wprintf("<INPUT TYPE=\"text\" NAME=\"gr_name\" VALUE=\"%s\" MAXLENGTH=\"19\">\n", rname);
1510
1511         if (req_pass) {
1512                 wprintf("</TD></TR><TR><TD>");
1513                 wprintf("Enter room password:</TD><TD>");
1514                 wprintf("<INPUT TYPE=\"password\" NAME=\"gr_pass\" MAXLENGTH=\"9\">\n");
1515         }
1516         wprintf("</TD></TR></TABLE>\n");
1517
1518         wprintf("<INPUT TYPE=\"submit\" NAME=\"sc\" VALUE=\"OK\">");
1519         wprintf("<INPUT TYPE=\"submit\" NAME=\"sc\" VALUE=\"Cancel\">");
1520         wprintf("</FORM>\n");
1521         wDumpContent(1);
1522 }
1523
1524 /* 
1525  * goto a private room
1526  */
1527 void goto_private(void)
1528 {
1529         char hold_rm[32];
1530         char buf[SIZ];
1531
1532         if (strcasecmp(bstr("sc"), "OK")) {
1533                 display_main_menu();
1534                 return;
1535         }
1536         strcpy(hold_rm, WC->wc_roomname);
1537         strcpy(buf, "GOTO ");
1538         strcat(buf, bstr("gr_name"));
1539         strcat(buf, "|");
1540         strcat(buf, bstr("gr_pass"));
1541         serv_puts(buf);
1542         serv_gets(buf);
1543
1544         if (buf[0] == '2') {
1545                 smart_goto(bstr("gr_name"));
1546                 return;
1547         }
1548         if (!strncmp(buf, "540", 3)) {
1549                 display_private(bstr("gr_name"), 1);
1550                 return;
1551         }
1552         output_headers(1);
1553         wprintf("%s\n", &buf[4]);
1554         wDumpContent(1);
1555         return;
1556 }
1557
1558
1559 /*
1560  * display the screen to zap a room
1561  */
1562 void display_zap(void)
1563 {
1564         output_headers(1);
1565
1566         wprintf("<TABLE WIDTH=100%% BORDER=0 BGCOLOR=770000><TR><TD>");
1567         wprintf("<FONT SIZE=+1 COLOR=\"FFFFFF\"");
1568         wprintf("<B>Zap (forget) the current room</B>\n");
1569         wprintf("</FONT></TD></TR></TABLE>\n");
1570
1571         wprintf("If you select this option, <em>%s</em> will ", WC->wc_roomname);
1572         wprintf("disappear from your room list.  Is this what you wish ");
1573         wprintf("to do?<BR>\n");
1574
1575         wprintf("<FORM METHOD=\"POST\" ACTION=\"/zap\">\n");
1576         wprintf("<INPUT TYPE=\"submit\" NAME=\"sc\" VALUE=\"OK\">");
1577         wprintf("<INPUT TYPE=\"submit\" NAME=\"sc\" VALUE=\"Cancel\">");
1578         wprintf("</FORM>\n");
1579         wDumpContent(1);
1580 }
1581
1582
1583 /* 
1584  * zap a room
1585  */
1586 void zap(void)
1587 {
1588         char buf[SIZ];
1589         char final_destination[SIZ];
1590
1591         /* If the forget-room routine fails for any reason, we fall back
1592          * to the current room; otherwise, we go to the Lobby
1593          */
1594         strcpy(final_destination, WC->wc_roomname);
1595
1596         if (!strcasecmp(bstr("sc"), "OK")) {
1597                 serv_printf("GOTO %s", WC->wc_roomname);
1598                 serv_gets(buf);
1599                 if (buf[0] != '2') {
1600                         /* ExpressMessageCat(&buf[4]); */
1601                 } else {
1602                         serv_puts("FORG");
1603                         serv_gets(buf);
1604                         if (buf[0] != '2') {
1605                                 /* ExpressMessageCat(&buf[4]); */
1606                         } else {
1607                                 strcpy(final_destination, "_BASEROOM_");
1608                         }
1609                 }
1610         }
1611         smart_goto(final_destination);
1612 }
1613
1614
1615
1616
1617 /*
1618  * Confirm deletion of the current room
1619  */
1620 void confirm_delete_room(void)
1621 {
1622         char buf[SIZ];
1623
1624         serv_puts("KILL 0");
1625         serv_gets(buf);
1626         if (buf[0] != '2') {
1627                 display_error(&buf[4]);
1628                 return;
1629         }
1630         output_headers(1);
1631         wprintf("<TABLE WIDTH=100%% BORDER=0 BGCOLOR=770000><TR><TD>");
1632         wprintf("<FONT SIZE=+1 COLOR=\"FFFFFF\"");
1633         wprintf("<B>Confirm deletion of room</B>\n");
1634         wprintf("</FONT></TD></TR></TABLE>\n");
1635
1636         wprintf("<CENTER>");
1637         wprintf("<FORM METHOD=\"POST\" ACTION=\"/delete_room\">\n");
1638
1639         wprintf("Are you sure you want to delete <FONT SIZE=+1>");
1640         escputs(WC->wc_roomname);
1641         wprintf("</FONT>?<BR>\n");
1642
1643         wprintf("<INPUT TYPE=\"submit\" NAME=\"sc\" VALUE=\"Delete\">");
1644         wprintf("<INPUT TYPE=\"submit\" NAME=\"sc\" VALUE=\"Cancel\">");
1645
1646         wprintf("</FORM></CENTER>\n");
1647         wDumpContent(1);
1648 }
1649
1650
1651 /*
1652  * Delete the current room
1653  */
1654 void delete_room(void)
1655 {
1656         char buf[SIZ];
1657         char sc[SIZ];
1658
1659         strcpy(sc, bstr("sc"));
1660
1661         if (strcasecmp(sc, "Delete")) {
1662                 display_error("Cancelled.  This room was not deleted.");
1663                 return;
1664         }
1665         serv_puts("KILL 1");
1666         serv_gets(buf);
1667         if (buf[0] != '2') {
1668                 display_error(&buf[4]);
1669         } else {
1670                 smart_goto("_BASEROOM_");
1671         }
1672 }
1673
1674
1675
1676 /*
1677  * Perform changes to a room's network configuration
1678  */
1679 void netedit(void) {
1680         FILE *fp;
1681         char buf[SIZ];
1682         char line[SIZ];
1683
1684         if (strlen(bstr("line"))==0) {
1685                 display_editroom();
1686                 return;
1687         }
1688
1689         strcpy(line, bstr("prefix"));
1690         strcat(line, bstr("line"));
1691         strcat(line, bstr("suffix"));
1692
1693         fp = tmpfile();
1694         if (fp == NULL) {
1695                 display_editroom();
1696                 return;
1697         }
1698
1699         serv_puts("GNET");
1700         serv_gets(buf);
1701         if (buf[0] != '1') {
1702                 fclose(fp);
1703                 display_editroom();
1704                 return;
1705         }
1706
1707         /* This loop works for add *or* remove.  Spiffy, eh? */
1708         while (serv_gets(buf), strcmp(buf, "000")) {
1709                 if (strcasecmp(buf, line)) {
1710                         fprintf(fp, "%s\n", buf);
1711                 }
1712         }
1713
1714         rewind(fp);
1715         serv_puts("SNET");
1716         serv_gets(buf);
1717         if (buf[0] != '4') {
1718                 fclose(fp);
1719                 display_editroom();
1720                 return;
1721         }
1722
1723         while (fgets(buf, sizeof buf, fp) != NULL) {
1724                 buf[strlen(buf)-1] = 0;
1725                 serv_puts(buf);
1726         }
1727
1728         if (!strcasecmp(bstr("cmd"), "add")) {
1729                 serv_puts(line);
1730         }
1731
1732         serv_puts("000");
1733         fclose(fp);
1734         display_editroom();
1735 }
1736
1737
1738
1739 /*
1740  * Convert a room name to a folder-ish-looking name.
1741  */
1742 void room_to_folder(char *folder, char *room, int floor, int is_mailbox)
1743 {
1744         int i;
1745
1746         /*
1747          * For mailboxes, just do it straight...
1748          */
1749         if (is_mailbox) {
1750                 sprintf(folder, "My folders|%s", room);
1751         }
1752
1753         /*
1754          * Otherwise, prefix the floor name as a "public folders" moniker
1755          */
1756         else {
1757                 sprintf(folder, "%s|%s", floorlist[floor], room);
1758         }
1759
1760         /*
1761          * Replace "/" characters with "|" for pseudo-folder-delimiting
1762          */
1763         for (i=0; i<strlen(folder); ++i) {
1764                 if (folder[i] == '/') folder[i] = '|';
1765         }
1766 }
1767
1768
1769
1770
1771
1772
1773
1774 /*
1775  * Change the view for this room
1776  */
1777 void change_view(void) {
1778         int view;
1779         char buf[SIZ];
1780
1781         view = atol(bstr("view"));
1782
1783         serv_printf("VIEW %d", view);
1784         serv_gets(buf);
1785         smart_goto(WC->wc_roomname);
1786 }
1787
1788
1789 /*
1790  * Show the room list in "folders" format.  (only should get called by
1791  * knrooms() because that's where output_headers() is called from)
1792  */
1793 void folders(void) {
1794         char buf[SIZ];
1795
1796         int levels, oldlevels;
1797
1798         struct folder {
1799                 char room[SIZ];
1800                 char name[SIZ];
1801                 int hasnewmsgs;
1802                 int is_mailbox;
1803                 int selectable;
1804         };
1805
1806         struct folder *fold = NULL;
1807         struct folder ftmp;
1808         int max_folders = 0;
1809         int alloc_folders = 0;
1810         int i, j, k;
1811         int p;
1812         int flags;
1813         int floor;
1814         int nests = 0;
1815
1816         /* Start with the mailboxes */
1817         max_folders = 1;
1818         alloc_folders = 1;
1819         fold = malloc(sizeof(struct folder));
1820         memset(fold, 0, sizeof(struct folder));
1821         strcpy(fold[0].name, "My folders");
1822
1823         /* Then add floors */
1824         serv_puts("LFLR");
1825         serv_gets(buf);
1826         if (buf[0]=='1') while(serv_gets(buf), strcmp(buf, "000")) {
1827                 if (max_folders >= alloc_folders) {
1828                         alloc_folders = max_folders + 100;
1829                         fold = realloc(fold,
1830                                 alloc_folders * sizeof(struct folder));
1831                 }
1832                 memset(&fold[max_folders], 0, sizeof(struct folder));
1833                 extract(fold[max_folders].name, buf, 1);
1834                 ++max_folders;
1835         }
1836
1837         /* Now add rooms */
1838         for (p = 0; p < 2; ++p) {
1839                 if (p == 0) serv_puts("LKRN");
1840                 else if (p == 1) serv_puts("LKRO");
1841                 serv_gets(buf);
1842                 if (buf[0]=='1') while(serv_gets(buf), strcmp(buf, "000")) {
1843                         if (max_folders >= alloc_folders) {
1844                                 alloc_folders = max_folders + 100;
1845                                 fold = realloc(fold,
1846                                         alloc_folders * sizeof(struct folder));
1847                         }
1848                         memset(&fold[max_folders], 0, sizeof(struct folder));
1849                         extract(fold[max_folders].room, buf, 0);
1850                         if (p == 0) fold[max_folders].hasnewmsgs = 1;
1851                         flags = extract_int(buf, 1);
1852                         floor = extract_int(buf, 2);
1853                         if (flags & QR_MAILBOX) {
1854                                 fold[max_folders].is_mailbox = 1;
1855                         }
1856                         room_to_folder(fold[max_folders].name,
1857                                         fold[max_folders].room,
1858                                         floor,
1859                                         fold[max_folders].is_mailbox);
1860                         fold[max_folders].selectable = 1;
1861                         ++max_folders;
1862                 }
1863         }
1864
1865         /* Bubble-sort the folder list */
1866         for (i=0; i<max_folders; ++i) {
1867                 for (j=0; j<(max_folders-1)-i; ++j) {
1868                         if (strcasecmp(fold[j].name, fold[j+1].name) > 0) {
1869                                 memcpy(&ftmp, &fold[j], sizeof(struct folder));
1870                                 memcpy(&fold[j], &fold[j+1],
1871                                                         sizeof(struct folder));
1872                                 memcpy(&fold[j+1], &ftmp,
1873                                                         sizeof(struct folder));
1874                         }
1875                 }
1876         }
1877
1878         /* Output */
1879         nests = 0;
1880         levels = 0;
1881         oldlevels = 0;
1882         for (i=0; i<max_folders; ++i) {
1883
1884                 levels = num_tokens(fold[i].name, '|');
1885                 if (levels > oldlevels) {
1886                         for (k=0; k<(levels-oldlevels); ++k) {
1887                                 wprintf("<UL>");
1888                                 ++nests;
1889                         }
1890                 }
1891                 if (levels < oldlevels) {
1892                         for (k=0; k<(oldlevels-levels); ++k) {
1893                                 wprintf("</UL>");
1894                                 --nests;
1895                         }
1896                 }
1897                 oldlevels = levels;
1898
1899                 wprintf("<LI>");
1900                 if (fold[i].selectable) {
1901                         wprintf("<A HREF=\"/dotgoto?room=");
1902                         urlescputs(fold[i].room);
1903                         wprintf("\">");
1904                 }
1905                 if (fold[i].hasnewmsgs) wprintf("<B>");
1906                 extract(buf, fold[i].name, levels-1);
1907                 escputs(buf);
1908                 if (fold[i].hasnewmsgs) wprintf("</B>");
1909                 if (fold[i].selectable) wprintf("</A>\n");
1910         }
1911         while (nests-- > 0) wprintf("</UL>\n");
1912
1913         free(fold);
1914         wDumpContent(1);
1915 }
1916
1917
1918 /* Do either a known rooms list or a folders list, depending on the
1919  * user's preference
1920  */
1921 void knrooms() {
1922         char listviewpref[SIZ];
1923
1924         output_headers(3);
1925         load_floorlist();
1926
1927         /* Determine whether the user is trying to change views */
1928         if (bstr("view") != NULL) {
1929                 if (strlen(bstr("view")) > 0) {
1930                         set_preference("roomlistview", bstr("view"));
1931                 }
1932         }
1933
1934         get_preference("roomlistview", listviewpref);
1935
1936         if (strcasecmp(listviewpref, "folders")) {
1937                 strcpy(listviewpref, "rooms");
1938         }
1939
1940         /* title bar */
1941         wprintf("<TABLE WIDTH=100%% BORDER=0 BGCOLOR=000077><TR><TD>"
1942                 "<FONT SIZE=+1 COLOR=\"FFFFFF\"<B>"
1943         );
1944         if (!strcasecmp(listviewpref, "rooms")) {
1945                 wprintf("Room list");
1946         }
1947         if (!strcasecmp(listviewpref, "folders")) {
1948                 wprintf("Folder list");
1949         }
1950         wprintf("</B></TD>\n");
1951
1952
1953         /* offer the ability to switch views */
1954         wprintf("<TD><FORM NAME=\"roomlistomatic\">\n"
1955                 "<SELECT NAME=\"newview\" SIZE=\"1\" "
1956                 "OnChange=\"location.href=roomlistomatic.newview.options"
1957                 "[selectedIndex].value\">\n");
1958
1959         wprintf("<OPTION %s VALUE=\"/knrooms&view=rooms\">"
1960                 "View as room list"
1961                 "</OPTION>\n",
1962                 ( !strcasecmp(listviewpref, "rooms") ? "SELECTED" : "" )
1963         );
1964
1965         wprintf("<OPTION %s VALUE=\"/knrooms&view=folders\">"
1966                 "View as folder list"
1967                 "</OPTION>\n",
1968                 ( !strcasecmp(listviewpref, "folders") ? "SELECTED" : "" )
1969         );
1970
1971         wprintf("</SELECT></FORM></TD><TD>\n");
1972         offer_start_page();
1973         wprintf("</TD></TR></TABLE><BR>\n");
1974
1975         /* Display the room list in the user's preferred format */
1976         if (!strcasecmp(listviewpref, "folders")) {
1977                 folders();
1978         }
1979         else {
1980                 list_all_rooms_by_floor();
1981         }
1982 }