]> code.citadel.org Git - citadel.git/blob - textclient/citadel.c
Temp nav now includes view select.
[citadel.git] / textclient / citadel.c
1 // Main source module for the client program.
2 //
3 // Copyright (c) 1987-2022 by the citadel.org team
4 //
5 // This program is open source software.  Use, duplication, or disclosure is subject to the GNU General Public License version 3.
6
7 #include "textclient.h"
8
9 #define IFEXPERT if (userflags&US_EXPERT)
10 #define IFNEXPERT if ((userflags&US_EXPERT)==0)
11 #define IFAIDE if (axlevel>=AxAideU)
12 #define IFNAIDE if (axlevel<AxAideU)
13
14 int rordercmp(struct ctdlroomlisting *r1, struct ctdlroomlisting *r2);
15 march *marchptr = NULL;
16 extern char *moreprompt;
17
18 /* globals associated with the client program */
19 char temp[PATH_MAX];            /* Name of general-purpose temp file */
20 char temp2[PATH_MAX];           /* Name of general-purpose temp file */
21 char tempdir[PATH_MAX];         /* Name of general-purpose temp directory */
22 char printcmd[SIZ];             /* print command */
23 int editor_pid = (-1);
24 char fullname[USERNAME_SIZE];
25 unsigned room_flags;
26 unsigned room_flags2;
27 int entmsg_ok = 0;
28 char room_name[ROOMNAMELEN];
29 char *uglist[UGLISTLEN];        /* size of the ungoto list */
30 long uglistlsn[UGLISTLEN];      /* current read position for all the ungoto's. Not going to make any friends with this one. */
31 int uglistsize = 0;
32 char is_mail = 0;               /* nonzero when we're in a mail room */
33 char axlevel = AxDeleted;       /* access level */
34 char is_room_aide = 0;          /* boolean flag, 1 if room admin */
35 unsigned userflags;
36 long usernum = 0L;              /* user number */
37 time_t lastcall = 0L;           /* Date/time of previous login */
38 char newnow;
39 long highest_msg_read;          /* used for <A>bandon room cmd */
40 long maxmsgnum;                 /* used for <G>oto */
41 char sigcaught = 0;
42 char rc_username[USERNAME_SIZE];
43 char rc_password[32];
44 char hostbuf[SIZ];
45 char portbuf[SIZ];
46 char rc_floor_mode;
47 char floor_mode;
48 char curr_floor = 0;            /* number of current floor */
49 char floorlist[128][SIZ];       /* names of floors */
50 int termn8 = 0;                 /* Set to nonzero to cause a logoff */
51 int secure;                     /* Set to nonzero when wire is encrypted */
52
53 extern char instant_msgs;       /* instant messages waiting! */
54 extern int rc_ansi_color;       /* ansi color value from citadel.rc */
55 extern int rc_sixel;            /* sixel graphics value from citadel.rc */
56 extern int next_lazy_cmd;
57
58 CtdlIPC *ipc_for_signal_handlers;       /* KLUDGE cover your eyes */
59 int enable_syslog = 0;
60
61
62 // Here is our 'clean up gracefully and exit' routine
63 void ctdl_logoff(char *file, int line, CtdlIPC * ipc, int code) {
64         int lp;
65
66         if (editor_pid > 0) {   /* kill the editor if it's running */
67                 kill(editor_pid, SIGHUP);
68         }
69
70         // Free the ungoto list
71         for (lp = 0; lp < uglistsize; lp++) {
72                 free(uglist[lp]);
73         }
74
75         // Shut down the server connection ... but not if the logoff code is 3,
76         // because that means we're exiting because we already lost the server.
77         if (code != 3) {
78                 CtdlIPCQuit(ipc);
79         }
80
81         // now clean up various things
82         unlink(temp);
83         unlink(temp2);
84         nukedir(tempdir);
85
86         // Violently kill off any child processes if Citadel is the login shell. 
87         if (getppid() == 1) {
88                 kill(0 - getpgrp(), SIGTERM);
89                 sleep(1);
90                 kill(0 - getpgrp(), SIGKILL);
91         }
92         color(ORIGINAL_PAIR);   // Restore the old color settings
93         stty_ctdl(SB_RESTORE);  // return the old terminal settings
94         exit(code);             // exit with the proper exit code
95 }
96
97
98
99 /*
100  * signal catching function for hangups...
101  */
102 void dropcarr(int signum) {
103         logoff(NULL, 3);        /* No IPC when server's already gone! */
104 }
105
106
107
108 /*
109  * catch SIGCONT to reset terminal modes when were are put back into the
110  * foreground.
111  */
112 void catch_sigcont(int signum) {
113         stty_ctdl(SB_LAST);
114         signal(SIGCONT, catch_sigcont);
115 }
116
117
118 /* general purpose routines */
119
120 /* display a file */
121 void formout(CtdlIPC * ipc, char *name) {
122         int r;                  /* IPC return code */
123         char buf[SIZ];
124         char *text = NULL;
125
126         r = CtdlIPCSystemMessage(ipc, name, &text, buf);
127         if (r / 100 != 1) {
128                 scr_printf("%s\n", buf);
129                 return;
130         }
131         if (text) {
132                 fmout(screenwidth, NULL, text, NULL, 1);
133                 free(text);
134         }
135 }
136
137
138 void userlist(CtdlIPC * ipc, char *patn) {
139         char buf[SIZ];
140         char fl[64];            // a buffer this small will prevent it overrunning the column
141         struct tm tmbuf;
142         time_t lc;
143         int r;                  // IPC response code
144         char *listing = NULL;
145
146         r = CtdlIPCUserListing(ipc, patn, &listing, buf);
147         if (r / 100 != 1) {
148                 scr_printf("%s\n", buf);
149                 return;
150         }
151
152         scr_printf("User Name                                                        Last Login\n");
153         scr_printf("---------------------------------------------------------------- ----------\n");
154         if (listing != NULL)
155                 while (!IsEmptyStr(listing)) {
156                         extract_token(buf, listing, 0, '\n', sizeof buf);
157                         remove_token(listing, 0, '\n');
158
159                         if (sigcaught == 0) {
160                                 extract_token(fl, buf, 0, '|', sizeof fl);
161                                 if (pattern(fl, patn) >= 0) {
162                                         scr_printf("%-64s ", fl);
163                                         lc = extract_long(buf, 3);
164                                         localtime_r(&lc, &tmbuf);
165                                         scr_printf("%02d/%02d/%04d\n", (tmbuf.tm_mon + 1), tmbuf.tm_mday, (tmbuf.tm_year + 1900));
166                                 }
167
168                         }
169                 }
170         free(listing);
171         scr_printf("\n");
172 }
173
174
175 // grab assorted info about the user...
176 void load_user_info(char *params) {
177         extract_token(fullname, params, 0, '|', sizeof fullname);
178         axlevel = extract_int(params, 1);
179         userflags = extract_int(params, 4);
180         usernum = extract_long(params, 5);
181         lastcall = extract_long(params, 6);
182 }
183
184
185 // Remove a room from the march list.  'floornum' is ignored unless
186 // 'roomname' is set to _FLOOR_, in which case all rooms on the requested
187 // floor will be removed from the march list.
188 void remove_march(char *roomname, int floornum) {
189         struct march *mptr, *mptr2;
190
191         if (marchptr == NULL)
192                 return;
193
194         if ((!strcasecmp(marchptr->march_name, roomname))
195             || ((!strcasecmp(roomname, "_FLOOR_")) && (marchptr->march_floor == floornum))) {
196                 mptr = marchptr->next;
197                 free(marchptr);
198                 marchptr = mptr;
199                 return;
200         }
201         mptr2 = marchptr;
202         for (mptr = marchptr; mptr != NULL; mptr = mptr->next) {
203
204                 if ((!strcasecmp(mptr->march_name, roomname))
205                     || ((!strcasecmp(roomname, "_FLOOR_"))
206                         && (mptr->march_floor == floornum))) {
207
208                         mptr2->next = mptr->next;
209                         free(mptr);
210                         mptr = mptr2;
211                 }
212                 else {
213                         mptr2 = mptr;
214                 }
215         }
216 }
217
218
219 // Locate the room on the march list which we most want to go to.  Each room
220 // is measured given a "weight" of preference based on various factors.
221 char *pop_march(int desired_floor, struct march *_march) {
222         static char TheRoom[ROOMNAMELEN];
223         int TheWeight = 0;
224         int weight;
225         struct march *mptr = NULL;
226
227         strcpy(TheRoom, "_BASEROOM_");
228         if (_march == NULL)
229                 return (TheRoom);
230
231         for (mptr = _march; mptr != NULL; mptr = mptr->next) {
232                 weight = 0;
233                 if ((strcasecmp(mptr->march_name, "_BASEROOM_")))
234                         weight = weight + 10000;
235                 if (mptr->march_floor == desired_floor)
236                         weight = weight + 5000;
237
238                 weight = weight + ((128 - (mptr->march_floor)) * 128);
239                 weight = weight + (128 - (mptr->march_order));
240
241                 if (weight > TheWeight) {
242                         TheWeight = weight;
243                         strcpy(TheRoom, mptr->march_name);
244                 }
245         }
246         return (TheRoom);
247 }
248
249
250 // jump directly to a room
251 void dotgoto(CtdlIPC * ipc, char *towhere, int display_name, int fromungoto) {
252         char aaa[SIZ], bbb[SIZ];
253         static long ls = 0L;
254         int partial_match, best_match;
255         char from_floor;
256         int ugpos = uglistsize;
257         int r;                  /* IPC result code */
258         struct ctdlipcroom *room = NULL;
259         int rv = 0;
260
261         /* store ungoto information */
262         if (fromungoto == 0) {
263                 /* sloppy slide them all down, hey it's the client, who cares. :-) */
264                 if (uglistsize >= (UGLISTLEN - 1)) {
265                         int lp;
266                         free(uglist[0]);
267                         for (lp = 0; lp < (UGLISTLEN - 1); lp++) {
268                                 uglist[lp] = uglist[lp + 1];
269                                 uglistlsn[lp] = uglistlsn[lp + 1];
270                         }
271                         ugpos--;
272                 }
273                 else {
274                         uglistsize++;
275                 }
276
277                 uglist[ugpos] = malloc(strlen(room_name) + 1);
278                 strcpy(uglist[ugpos], room_name);
279                 uglistlsn[ugpos] = ls;
280         }
281
282         /* first try an exact match */
283         r = CtdlIPCGotoRoom(ipc, towhere, "", &room, aaa);
284         if (r / 10 == 54) {
285                 newprompt("Enter room password: ", bbb, 9);
286                 r = CtdlIPCGotoRoom(ipc, towhere, bbb, &room, aaa);
287                 if (r / 10 == 54) {
288                         scr_printf("Wrong password.\n");
289                         return;
290                 }
291         }
292
293         /*
294          * If a match is not found, try a partial match.
295          * Partial matches anywhere in the string carry a weight of 1,
296          * left-aligned matches carry a weight of 2.  Pick the room that
297          * has the highest-weighted match.  Do not match on forgotten
298          * rooms.
299          */
300         if (r / 100 != 2) {
301                 struct march *march = NULL;
302
303                 best_match = 0;
304                 strcpy(bbb, "");
305
306                 r = CtdlIPCKnownRooms(ipc, SubscribedRooms, AllFloors, &march, aaa);
307                 if (r / 100 == 1) {
308                         /* Run the roomlist; free the data as we go */
309                         struct march *mp = march;       /* Current */
310
311                         while (mp) {
312                                 partial_match = 0;
313                                 if (pattern(mp->march_name, towhere) >= 0) {
314                                         partial_match = 1;
315                                 }
316                                 if (!strncasecmp(mp->march_name, towhere, strlen(towhere))) {
317                                         partial_match = 2;
318                                 }
319                                 if (partial_match > best_match) {
320                                         strcpy(bbb, mp->march_name);
321                                         best_match = partial_match;
322                                 }
323                                 /* Both pointers are NULL at end of list */
324                                 march = mp->next;
325                                 free(mp);
326                                 mp = march;
327                         }
328                 }
329
330                 if (IsEmptyStr(bbb)) {
331                         scr_printf("No room '%s'.\n", towhere);
332                         return;
333                 }
334                 r = CtdlIPCGotoRoom(ipc, bbb, "", &room, aaa);
335         }
336         if (r / 100 != 1 && r / 100 != 2) {
337                 scr_printf("%s\n", aaa);
338                 return;
339         }
340         strncpy(room_name, room->RRname, ROOMNAMELEN);
341         room_flags = room->RRflags;
342         room_flags2 = room->RRflags2;
343         from_floor = curr_floor;
344         curr_floor = room->RRfloor;
345
346         // Determine, based on the room's default view, whether an <E>nter message command will be valid here.
347         switch (room->RRdefaultview) {
348         case VIEW_BBS:
349         case VIEW_MAILBOX:
350                 entmsg_ok = ENTMSG_OK_YES;
351                 break;
352         case VIEW_BLOG:
353                 entmsg_ok = ENTMSG_OK_BLOG;
354                 break;
355         default:
356                 entmsg_ok = ENTMSG_OK_NO;
357                 break;
358         }
359
360         remove_march(room_name, 0);
361         if (!strcasecmp(towhere, "_BASEROOM_"))
362                 remove_march(towhere, 0);
363         if (!room->RRunread)
364                 next_lazy_cmd = 5;      /* Don't read new if no new msgs */
365         if ((from_floor != curr_floor) && (display_name > 0) && (floor_mode == 1)) {
366                 if (floorlist[(int) curr_floor][0] == 0)
367                         load_floorlist(ipc);
368                 scr_printf("(Entering floor: %s)\n", &floorlist[(int) curr_floor][0]);
369         }
370         if (display_name == 1) {
371                 color(BRIGHT_WHITE);
372                 scr_printf("%s ", room_name);
373                 color(DIM_WHITE);
374                 scr_printf("- ");
375         }
376         if (display_name != 2) {
377                 color(BRIGHT_YELLOW);
378                 scr_printf("%d ", room->RRunread);
379                 color(DIM_WHITE);
380                 scr_printf("new of ");
381                 color(BRIGHT_YELLOW);
382                 scr_printf("%d ", room->RRtotal);
383                 color(DIM_WHITE);
384                 scr_printf("messages.\n");
385         }
386         highest_msg_read = room->RRlastread;
387         maxmsgnum = room->RRhighest;
388         is_mail = room->RRismailbox;
389         is_room_aide = room->RRaide;
390         ls = room->RRlastread;
391
392         /* read info file if necessary */
393         if (room->RRinfoupdated > 0)
394                 readinfo(ipc);
395
396         /* check for newly arrived mail if we can   FIXME use BIFF command for this
397            if (newmailcount > 0) {
398            color(BRIGHT_RED);
399            if (newmailcount == 1) {
400            scr_printf("*** A new mail message has arrived.\n");
401            }
402            else {
403            scr_printf("*** %d new mail messages have arrived.\n", newmailcount);
404            }
405            color(DIM_WHITE);
406            if (!IsEmptyStr(rc_gotmail_cmd)) {
407            rv = system(rc_gotmail_cmd);
408            if (rv)
409            scr_printf("*** failed to check for mail calling %s Reason %d.\n", rc_gotmail_cmd, rv);
410            }
411            }
412          */
413
414         free(room);
415
416         if (screenwidth > 5)
417                 snprintf(&status_line[1], screenwidth - 1, "%s  |  %s  |  %s  |  %s  |  %d new mail  |", (secure ? "Encrypted" : "Unencrypted"), ipc->ServInfo.humannode, ipc->ServInfo.site_location, room_name, 0);   // FIXME use BIFF
418 }
419
420
421 /* Goto next room having unread messages.
422  * We want to skip over rooms that the user has already been to, and take the
423  * user back to the lobby when done.  The room we end up in is placed in
424  * newroom - which is set to 0 (the lobby) initially.
425  */
426 void gotonext(CtdlIPC * ipc) {
427         char buf[SIZ];
428         struct march *mptr, *mptr2;
429         char next_room[ROOMNAMELEN];
430
431         // Check to see if the march-mode list is already allocated.
432         // If it is, pop the first room off the list and go there.
433         if (marchptr == NULL) {
434                 CtdlIPCKnownRooms(ipc, SubscribedRoomsWithNewMessages, AllFloors, &marchptr, buf);
435
436                 // Add _BASEROOM_ to the end of the march list, so the user will end up
437                 // in the system base room (usually the Lobby>) at the end of the loop.
438                 mptr = (struct march *) malloc(sizeof(struct march));
439                 mptr->next = NULL;
440                 mptr->march_order = 0;
441                 mptr->march_floor = 0;
442                 strcpy(mptr->march_name, "_BASEROOM_");
443                 if (marchptr == NULL) {
444                         marchptr = mptr;
445                 }
446                 else {
447                         mptr2 = marchptr;
448                         while (mptr2->next != NULL)
449                                 mptr2 = mptr2->next;
450                         mptr2->next = mptr;
451                 }
452
453                 // ...and remove the room we're currently in, so a <G>oto doesn't make us walk around in circles
454                 remove_march(room_name, 0);
455         }
456         if (marchptr != NULL) {
457                 strcpy(next_room, pop_march(curr_floor, marchptr));
458         }
459         else {
460                 strcpy(next_room, "_BASEROOM_");
461         }
462         remove_march(next_room, 0);
463         dotgoto(ipc, next_room, 1, 0);
464 }
465
466
467 // forget all rooms on a given floor
468 void forget_all_rooms_on(CtdlIPC * ipc, int ffloor) {
469         char buf[SIZ];
470         struct march *flist = NULL;
471         struct march *fptr = NULL;
472         struct ctdlipcroom *room = NULL;
473         int r;                  // IPC response code
474
475         scr_printf("Forgetting all rooms on %s...\n", &floorlist[ffloor][0]);
476         remove_march("_FLOOR_", ffloor);
477         r = CtdlIPCKnownRooms(ipc, AllAccessibleRooms, ffloor, &flist, buf);
478         if (r / 100 != 1) {
479                 scr_printf("Error %d: %s\n", r, buf);
480                 return;
481         }
482         while (flist) {
483                 r = CtdlIPCGotoRoom(ipc, flist->march_name, "", &room, buf);
484                 if (r / 100 == 2) {
485                         r = CtdlIPCForgetRoom(ipc, buf);
486                         if (r / 100 != 2) {
487                                 scr_printf("Error %d: %s\n", r, buf);
488                         }
489
490                 }
491                 fptr = flist;
492                 flist = flist->next;
493                 free(fptr);
494         }
495         if (room)
496                 free(room);
497 }
498
499
500 // routine called by gotofloor() to move to a new room on a new floor
501 void gf_toroom(CtdlIPC * ipc, char *towhere, int mode) {
502         int floor_being_left;
503
504         floor_being_left = curr_floor;
505
506         if (mode == GF_GOTO) {  /* <;G>oto mode */
507                 updatels(ipc);
508                 dotgoto(ipc, towhere, 1, 0);
509         }
510         else if (mode == GF_SKIP) {     /* <;S>kip mode */
511                 dotgoto(ipc, towhere, 1, 0);
512                 remove_march("_FLOOR_", floor_being_left);
513         }
514         else if (mode == GF_ZAP) {      /* <;Z>ap mode */
515                 dotgoto(ipc, towhere, 1, 0);
516                 remove_march("_FLOOR_", floor_being_left);
517                 forget_all_rooms_on(ipc, floor_being_left);
518         }
519 }
520
521
522 // go to a new floor
523 void gotofloor(CtdlIPC * ipc, char *towhere, int mode) {
524         int a, tofloor;
525         int r;                  /* IPC response code */
526         struct march *mptr;
527         char buf[SIZ], targ[SIZ];
528
529         if (floorlist[0][0] == 0)
530                 load_floorlist(ipc);
531         tofloor = (-1);
532         for (a = 0; a < 128; ++a)
533                 if (!strcasecmp(&floorlist[a][0], towhere))
534                         tofloor = a;
535
536         if (tofloor < 0) {
537                 for (a = 0; a < 128; ++a) {
538                         if (!strncasecmp(&floorlist[a][0], towhere, strlen(towhere))) {
539                                 tofloor = a;
540                         }
541                 }
542         }
543         if (tofloor < 0) {
544                 for (a = 0; a < 128; ++a)
545                         if (pattern(towhere, &floorlist[a][0]) > 0)
546                                 tofloor = a;
547         }
548         if (tofloor < 0) {
549                 scr_printf("No floor '%s'.\n", towhere);
550                 return;
551         }
552         for (mptr = marchptr; mptr != NULL; mptr = mptr->next) {
553                 if ((mptr->march_floor) == tofloor) {
554                         gf_toroom(ipc, mptr->march_name, mode);
555                         return;
556                 }
557         }
558
559         /* Find first known room on the floor */
560
561         strcpy(targ, "");
562         mptr = NULL;
563         r = CtdlIPCKnownRooms(ipc, SubscribedRooms, tofloor, &mptr, buf);
564         if (r / 100 == 1) {
565                 struct march *tmp = mptr;
566
567                 /*. . . according to room order */
568                 if (mptr)
569                         strcpy(targ, pop_march(tofloor, mptr));
570                 while (mptr) {
571                         tmp = mptr->next;
572                         free(mptr);
573                         mptr = tmp;
574                 }
575         }
576         if (!IsEmptyStr(targ)) {
577                 gf_toroom(ipc, targ, mode);
578                 return;
579         }
580
581         /* No known rooms on the floor; unzap the first one then */
582
583         strcpy(targ, "");
584         mptr = NULL;
585         r = CtdlIPCKnownRooms(ipc, AllAccessibleRooms, tofloor, &mptr, buf);
586         if (r / 100 == 1) {
587                 struct march *tmp = mptr;
588
589                 /*. . . according to room order */
590                 if (mptr)
591                         strcpy(targ, pop_march(tofloor, mptr));
592                 while (mptr) {
593                         tmp = mptr->next;
594                         free(mptr);
595                         mptr = tmp;
596                 }
597         }
598         if (!IsEmptyStr(targ)) {
599                 gf_toroom(ipc, targ, mode);
600         }
601         else {
602                 scr_printf("There are no rooms on '%s'.\n", &floorlist[tofloor][0]);
603         }
604 }
605
606
607 /*
608  * Indexing mechanism for a room list, called by gotoroomstep()
609  */
610 void room_tree_list_query(struct ctdlroomlisting *rp, char *findrmname, int findrmslot, char *rmname, int *rmslot, int *rmtotal) {
611         char roomname[ROOMNAMELEN];
612         static int cur_rmslot = 0;
613
614         if (rp == NULL) {
615                 cur_rmslot = 0;
616                 return;
617         }
618
619         if (rp->lnext != NULL) {
620                 room_tree_list_query(rp->lnext, findrmname, findrmslot, rmname, rmslot, rmtotal);
621         }
622
623         if (sigcaught == 0) {
624                 strcpy(roomname, rp->rlname);
625
626                 if (rmname != NULL) {
627                         if (cur_rmslot == findrmslot) {
628                                 strcpy(rmname, roomname);
629                         }
630                 }
631                 if (rmslot != NULL) {
632                         if (!strcmp(roomname, findrmname)) {
633                                 *rmslot = cur_rmslot;
634                         }
635                 }
636                 cur_rmslot++;
637         }
638
639         if (rp->rnext != NULL) {
640                 room_tree_list_query(rp->rnext, findrmname, findrmslot, rmname, rmslot, rmtotal);
641         }
642
643         if ((rmname == NULL) && (rmslot == NULL))
644                 free(rp);
645
646         if (rmtotal != NULL) {
647                 *rmtotal = cur_rmslot;
648         }
649 }
650
651 /*
652  * step through rooms on current floor
653  */
654 void gotoroomstep(CtdlIPC * ipc, int direction, int mode) {
655         struct march *listing = NULL;
656         struct march *mptr;
657         int r;                  /* IPC response code */
658         char buf[SIZ];
659         struct ctdlroomlisting *rl = NULL;
660         struct ctdlroomlisting *rp;
661         struct ctdlroomlisting *rs;
662         int list_it;
663         char rmname[ROOMNAMELEN];
664         int rmslot = 0;
665         int rmtotal;
666
667         /* Ask the server for a room list */
668         r = CtdlIPCKnownRooms(ipc, SubscribedRooms, (-1), &listing, buf);
669         if (r / 100 != 1) {
670                 listing = NULL;
671         }
672
673         load_floorlist(ipc);
674
675         for (mptr = listing; mptr != NULL; mptr = mptr->next) {
676                 list_it = 1;
677
678                 if (floor_mode && (mptr->march_floor != curr_floor))
679                         list_it = 0;
680
681                 if (list_it) {
682                         rp = malloc(sizeof(struct ctdlroomlisting));
683                         strncpy(rp->rlname, mptr->march_name, ROOMNAMELEN);
684                         rp->rlflags = mptr->march_flags;
685                         rp->rlfloor = mptr->march_floor;
686                         rp->rlorder = mptr->march_order;
687                         rp->lnext = NULL;
688                         rp->rnext = NULL;
689
690                         rs = rl;
691                         if (rl == NULL) {
692                                 rl = rp;
693                         }
694                         else {
695                                 while (rp != NULL) {
696                                         if (rordercmp(rp, rs) < 0) {
697                                                 if (rs->lnext == NULL) {
698                                                         rs->lnext = rp;
699                                                         rp = NULL;
700                                                 }
701                                                 else {
702                                                         rs = rs->lnext;
703                                                 }
704                                         }
705                                         else {
706                                                 if (rs->rnext == NULL) {
707                                                         rs->rnext = rp;
708                                                         rp = NULL;
709                                                 }
710                                                 else {
711                                                         rs = rs->rnext;
712                                                 }
713                                         }
714                                 }
715                         }
716                 }
717         }
718
719         /* Find position of current room */
720         room_tree_list_query(NULL, NULL, 0, NULL, NULL, NULL);
721         room_tree_list_query(rl, room_name, 0, NULL, &rmslot, &rmtotal);
722
723         if (direction == 0) {   /* Previous room */
724                 /* If we're at the first room, wrap to the last room */
725                 if (rmslot == 0) {
726                         rmslot = rmtotal - 1;
727                 }
728                 else {
729                         rmslot--;
730                 }
731         }
732         else {                  /* Next room */
733                 /* If we're at the last room, wrap to the first room */
734                 if (rmslot == rmtotal - 1) {
735                         rmslot = 0;
736                 }
737                 else {
738                         rmslot++;
739                 }
740         }
741
742         /* Get name of next/previous room */
743         room_tree_list_query(NULL, NULL, 0, NULL, NULL, NULL);
744         room_tree_list_query(rl, NULL, rmslot, rmname, NULL, NULL);
745
746         /* Free the tree */
747         room_tree_list_query(rl, NULL, 0, NULL, NULL, NULL);
748
749         if (mode == 0) {        /* not skipping */
750                 updatels(ipc);
751         }
752
753         /* Free the room list */
754         while (listing) {
755                 mptr = listing->next;
756                 free(listing);
757                 listing = mptr;
758         };
759
760         dotgoto(ipc, rmname, 1, 0);
761 }
762
763
764 /*
765  * step through floors on system
766  */
767 void gotofloorstep(CtdlIPC * ipc, int direction, int mode) {
768         int tofloor;
769
770         if (floorlist[0][0] == 0)
771                 load_floorlist(ipc);
772
773       empty_keep_going:
774
775         if (direction == 0) {   /* Previous floor */
776                 if (curr_floor)
777                         tofloor = curr_floor - 1;
778                 else
779                         tofloor = 127;
780
781                 while (!floorlist[tofloor][0])
782                         tofloor--;
783         }
784         else {                  /* Next floor */
785                 if (curr_floor < 127)
786                         tofloor = curr_floor + 1;
787                 else
788                         tofloor = 0;
789
790                 while (!floorlist[tofloor][0] && tofloor < 127)
791                         tofloor++;
792                 if (!floorlist[tofloor][0])
793                         tofloor = 0;
794         }
795         /* ;g works when not in floor mode so . . . */
796         if (!floor_mode) {
797                 scr_printf("(%s)\n", floorlist[tofloor]);
798         }
799
800         gotofloor(ipc, floorlist[tofloor], mode);
801         if (curr_floor != tofloor) {    /* gotofloor failed */
802                 curr_floor = tofloor;
803                 goto empty_keep_going;
804         }
805 }
806
807
808 /* 
809  * Display user 'preferences'.
810  */
811 extern int rc_prompt_control;
812 void read_config(CtdlIPC * ipc) {
813         char buf[SIZ];
814         char *resp = NULL;
815         int r;                  /* IPC response code */
816         char _fullname[USERNAME_SIZE];
817         long _usernum;
818         int _axlevel;
819         time_t _lastcall;
820         struct ctdluser *user = NULL;
821
822         /* get misc user info */
823         r = CtdlIPCGetBio(ipc, fullname, &resp, buf);
824         if (r / 100 != 1) {
825                 scr_printf("%s\n", buf);
826                 return;
827         }
828         extract_token(_fullname, buf, 1, '|', sizeof fullname);
829         _usernum = extract_long(buf, 2);
830         _axlevel = extract_int(buf, 3);
831         _lastcall = extract_long(buf, 4);
832         free(resp);
833         resp = NULL;
834
835         /* get preferences */
836         r = CtdlIPCGetConfig(ipc, &user, buf);
837         if (r / 100 != 2) {
838                 scr_printf("%s\n", buf);
839                 free(user);
840                 return;
841         }
842
843         /* show misc user info */
844         if (_lastcall > 0L) {
845                 scr_printf("Last login: %s", asctime(localtime(&_lastcall)));
846         }
847         scr_printf("\n");
848
849         /* show preferences */
850         scr_printf("Are you an experienced Citadel user: ");
851         color(BRIGHT_CYAN);
852         scr_printf("%s\n", (user->flags & US_EXPERT) ? "Yes" : "No");
853         color(DIM_WHITE);
854         scr_printf("Print last old message on New message request: ");
855         color(BRIGHT_CYAN);
856         scr_printf("%s\n", (user->flags & US_LASTOLD) ? "Yes" : "No");
857         color(DIM_WHITE);
858         scr_printf("Prompt after each message: ");
859         color(BRIGHT_CYAN);
860         scr_printf("%s\n", (!(user->flags & US_NOPROMPT)) ? "Yes" : "No");
861         color(DIM_WHITE);
862         if ((user->flags & US_NOPROMPT) == 0) {
863                 scr_printf("Use 'disappearing' prompts: ");
864                 color(BRIGHT_CYAN);
865                 scr_printf("%s\n", (user->flags & US_DISAPPEAR) ? "Yes" : "No");
866                 color(DIM_WHITE);
867         }
868         scr_printf("Pause after each screenful of text: ");
869         color(BRIGHT_CYAN);
870         scr_printf("%s\n", (user->flags & US_PAGINATOR) ? "Yes" : "No");
871         color(DIM_WHITE);
872         if (rc_prompt_control == 3 && (user->flags & US_PAGINATOR)) {
873                 scr_printf("<N>ext and <S>top work at paginator prompt: ");
874                 color(BRIGHT_CYAN);
875                 scr_printf("%s\n", (user->flags & US_PROMPTCTL) ? "Yes" : "No");
876                 color(DIM_WHITE);
877         }
878         if (rc_floor_mode == RC_DEFAULT) {
879                 scr_printf("View rooms by floor: ");
880                 color(BRIGHT_CYAN);
881                 scr_printf("%s\n", (user->flags & US_FLOORS) ? "Yes" : "No");
882                 color(DIM_WHITE);
883         }
884         if (rc_ansi_color == 3) {
885                 scr_printf("Enable color support: ");
886                 color(BRIGHT_CYAN);
887                 scr_printf("%s\n", (user->flags & US_COLOR) ? "Yes" : "No");
888                 color(DIM_WHITE);
889         }
890         scr_printf("Be unlisted in userlog: ");
891         color(BRIGHT_CYAN);
892         scr_printf("%s\n", (user->flags & US_UNLISTED) ? "Yes" : "No");
893         color(DIM_WHITE);
894         if (!IsEmptyStr(editor_path)) {
895                 scr_printf("Always enter messages with the full-screen editor: ");
896                 color(BRIGHT_CYAN);
897                 scr_printf("%s\n", (user->flags & US_EXTEDIT) ? "Yes" : "No");
898                 color(DIM_WHITE);
899         }
900         free(user);
901 }
902
903
904 /*
905  * Display system statistics.
906  */
907 void system_info(CtdlIPC * ipc) {
908         char buf[SIZ];
909         char *resp = NULL;
910         size_t bytes;
911         int mrtg_users, mrtg_active_users;
912         char mrtg_server_uptime[40];
913         long mrtg_himessage;
914
915         /* get #users, #active & server uptime */
916         CtdlIPCGenericCommand(ipc, "MRTG|users", NULL, 0, &resp, &bytes, buf);
917         mrtg_users = extract_int(resp, 0);
918         remove_token(resp, 0, '\n');
919         mrtg_active_users = extract_int(resp, 0);
920         remove_token(resp, 0, '\n');
921         extract_token(mrtg_server_uptime, resp, 0, '\n', sizeof mrtg_server_uptime);
922         free(resp);
923         resp = NULL;
924
925         /* get high message# */
926         CtdlIPCGenericCommand(ipc, "MRTG|messages", NULL, 0, &resp, &bytes, buf);
927         mrtg_himessage = extract_long(resp, 0);
928         free(resp);
929         resp = NULL;
930
931         /* refresh server info just in case */
932         CtdlIPCServerInfo(ipc, buf);
933
934         scr_printf("You are connected to %s (%s) @%s\n", ipc->ServInfo.nodename, ipc->ServInfo.humannode, ipc->ServInfo.fqdn);
935         scr_printf("running %s with text client v%.2f,\n", ipc->ServInfo.software, (float) CLIENT_VERSION / 100);
936         scr_printf("server build %s,\n", ipc->ServInfo.svn_revision, (float) CLIENT_VERSION / 100);
937         scr_printf("and located in %s.\n", ipc->ServInfo.site_location);
938         scr_printf("Connected users %d / Active users %d / Highest message #%ld\n", mrtg_users, mrtg_active_users, mrtg_himessage);
939         scr_printf("Server uptime: %s\n", mrtg_server_uptime);
940         scr_printf("Your system administrator is %s.\n", ipc->ServInfo.sysadm);
941 }
942
943
944 /*
945  * forget all rooms on current floor
946  */
947 void forget_this_floor(CtdlIPC * ipc) {
948         if (curr_floor == 0) {
949                 scr_printf("Can't forget this floor.\n");
950                 return;
951         }
952         if (floorlist[0][0] == 0) {
953                 load_floorlist(ipc);
954         }
955         scr_printf("Are you sure you want to forget all rooms on %s? ", &floorlist[(int) curr_floor][0]);
956         if (yesno() == 0) {
957                 return;
958         }
959
960         gf_toroom(ipc, "_BASEROOM_", GF_ZAP);
961 }
962
963
964 /*
965  * set floor mode depending on client, server, and user settings
966  */
967 void set_floor_mode(CtdlIPC * ipc) {
968         if (ipc->ServInfo.ok_floors == 0) {
969                 floor_mode = 0; /* Don't use floors if the server */
970         }
971         /* doesn't support them!          */
972         else {
973                 if (rc_floor_mode == RC_NO) {   /* never use floors */
974                         floor_mode = 0;
975                 }
976                 if (rc_floor_mode == RC_YES) {  /* always use floors */
977                         floor_mode = 1;
978                 }
979                 if (rc_floor_mode == RC_DEFAULT) {      /* user choice */
980                         floor_mode = ((userflags & US_FLOORS) ? 1 : 0);
981                 }
982         }
983 }
984
985 /*
986  * Set or change the user's password
987  */
988 int set_password(CtdlIPC * ipc) {
989         char pass1[20];
990         char pass2[20];
991         char buf[SIZ];
992
993         if (!IsEmptyStr(rc_password)) {
994                 strcpy(pass1, rc_password);
995                 strcpy(pass2, rc_password);
996         }
997         else {
998                 IFNEXPERT formout(ipc, "changepw");
999                 newprompt("Enter a new password: ", pass1, -19);
1000                 newprompt("Enter it again to confirm: ", pass2, -19);
1001         }
1002         strproc(pass1);
1003         strproc(pass2);
1004         if (!strcasecmp(pass1, pass2)) {
1005                 CtdlIPCChangePassword(ipc, pass1, buf);
1006                 scr_printf("%s\n", buf);
1007                 offer_to_remember_password(ipc, hostbuf, portbuf, fullname, pass1);
1008                 return (0);
1009         }
1010         else {
1011                 scr_printf("*** They don't match... try again.\n");
1012                 return (1);
1013         }
1014 }
1015
1016
1017 /*
1018  * get info about the server we've connected to
1019  */
1020 void get_serv_info(CtdlIPC * ipc, char *supplied_hostname) {
1021         char buf[SIZ];
1022
1023         CtdlIPCServerInfo(ipc, buf);
1024         moreprompt = ipc->ServInfo.moreprompt;
1025
1026         /* be nice and identify ourself to the server */
1027         CtdlIPCIdentifySoftware(ipc, CLIENT_TYPE, 0, CLIENT_VERSION,
1028                                 (ipc->isLocal ? "local" : "Citadel text mode client"), (supplied_hostname) ? supplied_hostname :
1029                                 /* Look up the , in the bible if you're confused */
1030                                 (locate_host(ipc, buf), buf), buf);
1031
1032         // Indicate to the server that we prefer to decode Base64 and
1033         // quoted-printable on the client side.
1034         if ((CtdlIPCSpecifyPreferredFormats(ipc, buf, "dont_decode") / 100) != 2) {
1035                 scr_printf("Error %s:%d", __FILE__, __LINE__);
1036                 logoff(ipc, 0);
1037         }
1038
1039         /*
1040          * Tell the server what our preferred content formats are.
1041          *
1042          * Originally we preferred HTML over plain text because we can format
1043          * it to the reader's screen width, but since our HTML-to-text parser
1044          * isn't really all that great, it's probably better to just go with
1045          * the plain text when we have it available.
1046          */
1047         if ((CtdlIPCSpecifyPreferredFormats(ipc, buf, "text/plain|text/html|text/x-markdown") / 100) != 2) {
1048                 scr_printf("Error %s:%d", __FILE__, __LINE__);
1049                 logoff(ipc, 0);
1050         }
1051 }
1052
1053
1054 /*
1055  * Session username compare function for SortOnlineUsers()
1056  */
1057 int rwho_username_cmp(const void *rec1, const void *rec2) {
1058         char *u1, *u2;
1059
1060         u1 = strchr(rec1, '|');
1061         u2 = strchr(rec2, '|');
1062
1063         return strcasecmp((u1 ? ++u1 : ""), (u2 ? ++u2 : ""));
1064 }
1065
1066
1067 /*
1068  * Idle time compare function for SortOnlineUsers()
1069  */
1070 int idlecmp(const void *rec1, const void *rec2) {
1071         time_t i1, i2;
1072
1073         i1 = extract_long(rec1, 5);
1074         i2 = extract_long(rec2, 5);
1075
1076         if (i1 < i2)
1077                 return (1);
1078         if (i1 > i2)
1079                 return (-1);
1080         return (0);
1081 }
1082
1083
1084 /*
1085  * Sort the list of online users by idle time.
1086  * This function frees the supplied buffer, and returns a new one
1087  * to the caller.  The caller is responsible for freeing the returned buffer.
1088  *
1089  * If 'condense' is nonzero, multiple sessions for the same user will be
1090  * combined into one for brevity.
1091  */
1092 char *SortOnlineUsers(char *listing, int condense) {
1093         int rows;
1094         char *sortbuf;
1095         char *retbuf;
1096         char buf[SIZ];
1097         int i;
1098
1099         rows = num_tokens(listing, '\n');
1100         sortbuf = malloc(rows * SIZ);
1101         if (sortbuf == NULL)
1102                 return (listing);
1103         retbuf = malloc(rows * SIZ);
1104         if (retbuf == NULL) {
1105                 free(sortbuf);
1106                 return (listing);
1107         }
1108
1109         /* Copy the list into a fixed-record-size array for sorting */
1110         for (i = 0; i < rows; ++i) {
1111                 memset(buf, 0, SIZ);
1112                 extract_token(buf, listing, i, '\n', sizeof buf);
1113                 memcpy(&sortbuf[i * SIZ], buf, (size_t) SIZ);
1114         }
1115
1116         /* Sort by idle time */
1117         qsort(sortbuf, rows, SIZ, idlecmp);
1118
1119         /* Combine multiple sessions for the same user */
1120         if (condense) {
1121                 qsort(sortbuf, rows, SIZ, rwho_username_cmp);
1122                 if (rows > 1)
1123                         for (i = 1; i < rows; ++i)
1124                                 if (i > 0) {
1125                                         char u1[USERNAME_SIZE];
1126                                         char u2[USERNAME_SIZE];
1127                                         extract_token(u1, &sortbuf[(i - 1) * SIZ], 1, '|', sizeof u1);
1128                                         extract_token(u2, &sortbuf[i * SIZ], 1, '|', sizeof u2);
1129                                         if (!strcasecmp(u1, u2)) {
1130                                                 memcpy(&sortbuf[i * SIZ], &sortbuf[(i + 1) * SIZ], (rows - i - 1) * SIZ);
1131                                                 --rows;
1132                                                 --i;
1133                                         }
1134                                 }
1135
1136                 qsort(sortbuf, rows, SIZ, idlecmp);     /* idle sort again */
1137         }
1138
1139         /* Copy back to a \n delimited list */
1140         strcpy(retbuf, "");
1141         for (i = 0; i < rows; ++i) {
1142                 if (!IsEmptyStr(&sortbuf[i * SIZ])) {
1143                         strcat(retbuf, &sortbuf[i * SIZ]);
1144                         if (i < (rows - 1))
1145                                 strcat(retbuf, "\n");
1146                 }
1147         }
1148         free(listing);
1149         free(sortbuf);
1150         return (retbuf);
1151 }
1152
1153
1154 /*
1155  * Display list of users currently logged on to the server
1156  */
1157 void who_is_online(CtdlIPC * ipc, int longlist) {
1158         char buf[SIZ], username[SIZ], roomname[SIZ], fromhost[SIZ];
1159         char flags[SIZ];
1160         char actual_user[SIZ], actual_room[SIZ], actual_host[SIZ];
1161         char clientsoft[SIZ];
1162         time_t timenow = 0;
1163         time_t idletime, idlehours, idlemins, idlesecs;
1164         int last_session = (-1);
1165         int skipidle = 0;
1166         char *listing = NULL;
1167         int r;                  /* IPC response code */
1168
1169         if (longlist == 2) {
1170                 longlist = 0;
1171                 skipidle = 1;
1172         }
1173
1174         if (!longlist) {
1175                 color(BRIGHT_WHITE);
1176                 scr_printf("           User Name               Room          ");
1177                 if (screenwidth >= 80)
1178                         scr_printf(" Idle        From host");
1179                 scr_printf("\n");
1180                 color(DIM_WHITE);
1181                 scr_printf("   ------------------------- --------------------");
1182                 if (screenwidth >= 80)
1183                         scr_printf(" ---- ------------------------");
1184                 scr_printf("\n");
1185         }
1186         r = CtdlIPCOnlineUsers(ipc, &listing, &timenow, buf);
1187         listing = SortOnlineUsers(listing, (!longlist));
1188         if (r / 100 == 1) {
1189                 while (!IsEmptyStr(listing)) {
1190                         int isidle = 0;
1191
1192                         /* Get another line */
1193                         extract_token(buf, listing, 0, '\n', sizeof buf);
1194                         remove_token(listing, 0, '\n');
1195
1196                         extract_token(username, buf, 1, '|', sizeof username);
1197                         extract_token(roomname, buf, 2, '|', sizeof roomname);
1198                         extract_token(fromhost, buf, 3, '|', sizeof fromhost);
1199                         extract_token(clientsoft, buf, 4, '|', sizeof clientsoft);
1200                         extract_token(flags, buf, 7, '|', sizeof flags);
1201
1202                         idletime = timenow - extract_long(buf, 5);
1203                         idlehours = idletime / 3600;
1204                         idlemins = (idletime - (idlehours * 3600)) / 60;
1205                         idlesecs = (idletime - (idlehours * 3600) - (idlemins * 60));
1206
1207                         if (idletime > rc_idle_threshold) {
1208                                 if (skipidle) {
1209                                         isidle = 1;
1210                                 }
1211                         }
1212
1213                         if (longlist) {
1214                                 extract_token(actual_user, buf, 8, '|', sizeof actual_user);
1215                                 extract_token(actual_room, buf, 9, '|', sizeof actual_room);
1216                                 extract_token(actual_host, buf, 10, '|', sizeof actual_host);
1217
1218                                 scr_printf("  Flags: %s\n", flags);
1219                                 scr_printf("Session: %d\n", extract_int(buf, 0));
1220                                 scr_printf("   Name: %s\n", username);
1221                                 scr_printf("In room: %s\n", roomname);
1222                                 scr_printf("   Host: %s\n", fromhost);
1223                                 scr_printf(" Client: %s\n", clientsoft);
1224                                 scr_printf("   Idle: %ld:%02ld:%02ld\n", (long) idlehours, (long) idlemins, (long) idlesecs);
1225
1226                                 if ((!IsEmptyStr(actual_user) && !IsEmptyStr(actual_room) && !IsEmptyStr(actual_host))) {
1227                                         scr_printf("(really ");
1228                                         if (!IsEmptyStr(actual_user))
1229                                                 scr_printf("<%s> ", actual_user);
1230                                         if (!IsEmptyStr(actual_room))
1231                                                 scr_printf("in <%s> ", actual_room);
1232                                         if (!IsEmptyStr(actual_host))
1233                                                 scr_printf("from <%s> ", actual_host);
1234                                         scr_printf(")\n");
1235                                 }
1236                                 scr_printf("\n");
1237
1238                         }
1239                         else {
1240                                 if (isidle == 0) {
1241                                         if (extract_int(buf, 0) == last_session) {
1242                                                 scr_printf("        ");
1243                                         }
1244                                         else {
1245                                                 color(BRIGHT_MAGENTA);
1246                                                 scr_printf("%-3s", flags);
1247                                         }
1248                                         last_session = extract_int(buf, 0);
1249                                         color(BRIGHT_CYAN);
1250                                         scr_printf("%-25s ", username);
1251                                         color(BRIGHT_MAGENTA);
1252                                         roomname[20] = 0;
1253                                         scr_printf("%-20s", roomname);
1254
1255                                         if (screenwidth >= 80) {
1256                                                 scr_printf(" ");
1257                                                 if (idletime > rc_idle_threshold) {
1258                                                         /* over 1000d, must be gone fishing */
1259                                                         if (idlehours > 23999) {
1260                                                                 scr_printf("fish");
1261                                                                 /* over 10 days */
1262                                                         }
1263                                                         else if (idlehours > 239) {
1264                                                                 scr_printf("%3ldd", idlehours / 24);
1265                                                                 /* over 10 hours */
1266                                                         }
1267                                                         else if (idlehours > 9) {
1268                                                                 scr_printf("%1ldd%02ld", idlehours / 24, idlehours % 24);
1269                                                                 /* less than 10 hours */
1270                                                         }
1271                                                         else {
1272                                                                 scr_printf("%1ld:%02ld", idlehours, idlemins);
1273                                                         }
1274                                                 }
1275                                                 else {
1276                                                         scr_printf("    ");
1277                                                 }
1278                                                 scr_printf(" ");
1279                                                 color(BRIGHT_CYAN);
1280                                                 fromhost[24] = '\0';
1281                                                 scr_printf("%-24s", fromhost);
1282                                         }
1283                                         scr_printf("\n");
1284                                         color(DIM_WHITE);
1285                                 }
1286                         }
1287                 }
1288         }
1289         free(listing);
1290 }
1291
1292
1293 void enternew(CtdlIPC * ipc, char *desc, char *buf, int maxlen) {
1294         char bbb[128];
1295         snprintf(bbb, sizeof bbb, "Enter in your new %s: ", desc);
1296         newprompt(bbb, buf, maxlen);
1297 }
1298
1299
1300 int shift(int argc, char **argv, int start, int count) {
1301         int i;
1302
1303         for (i = start; i < (argc - count); ++i) {
1304                 argv[i] = argv[i + count];
1305         }
1306         argc = argc - count;
1307         return argc;
1308 }
1309
1310
1311 static void statusHook(char *s) {
1312         scr_printf(s);
1313 }
1314
1315
1316 /*
1317  * main
1318  */
1319 int main(int argc, char **argv) {
1320         int a, b, mcmd;
1321         char aaa[100], bbb[100];        /* general purpose variables */
1322         char argbuf[64];        /* command line buf */
1323         char *telnet_client_host = NULL;
1324         char *sptr, *sptr2;     /* USed to extract the nonce */
1325         char password[SIZ];
1326         struct ctdlipcmisc chek;
1327         struct ctdluser *myself = NULL;
1328         CtdlIPC *ipc;           /* Our server connection */
1329         int r;                  /* IPC result code */
1330         int rv = 0;             /* fetch but ignore syscall return value to suppress warnings */
1331
1332         int relh = 0;
1333         int home = 0;
1334         char relhome[PATH_MAX] = "";
1335         char ctdldir[PATH_MAX] = CTDLDIR;
1336         int lp;
1337         calc_dirs_n_files(relh, home, relhome, ctdldir, 0);
1338
1339 #ifdef HAVE_BACKTRACE
1340         bzero(&params, sizeof(params));
1341         params.debugLevel = ECRASH_DEBUG_VERBOSE;
1342         params.dumpAllThreads = TRUE;
1343         params.useBacktraceSymbols = 1;
1344         params.signals[0] = SIGSEGV;
1345         params.signals[1] = SIGILL;
1346         params.signals[2] = SIGBUS;
1347         params.signals[3] = SIGABRT;
1348 #endif
1349         setIPCErrorPrintf(scr_printf);
1350         setCryptoStatusHook(statusHook);
1351
1352         stty_ctdl(SB_SAVE);     /* Store the old terminal parameters */
1353         load_command_set();     /* parse the citadel.rc file */
1354         stty_ctdl(SB_NO_INTR);  /* Install the new ones */
1355         signal(SIGPIPE, dropcarr);      /* Cleanup gracefully if local conn. dropped */
1356         signal(SIGTERM, dropcarr);      /* Cleanup gracefully if terminated */
1357         signal(SIGCONT, catch_sigcont); /* Catch SIGCONT so we can reset terminal */
1358 #ifdef SIGWINCH
1359         signal(SIGWINCH, scr_winch);    /* Window resize signal */
1360 #endif
1361
1362 #ifdef HAVE_OPENSSL
1363         arg_encrypt = RC_DEFAULT;
1364 #endif
1365
1366         // Handle command line options as if we were called like /bin/login (i.e. from in.telnetd)
1367         for (a = 0; a < argc; ++a) {
1368                 if ((argc > a + 1) && (!strcmp(argv[a], "-h"))) {
1369                         telnet_client_host = argv[a + 1];
1370                         argc = shift(argc, argv, a, 2);
1371                 }
1372                 if (!strcmp(argv[a], "-x")) {
1373 #ifdef HAVE_OPENSSL
1374                         arg_encrypt = RC_NO;
1375 #endif
1376                         argc = shift(argc, argv, a, 1);
1377                 }
1378                 if (!strcmp(argv[a], "-X")) {
1379 #ifdef HAVE_OPENSSL
1380                         arg_encrypt = RC_YES;
1381                         argc = shift(argc, argv, a, 1);
1382 #else
1383                         fprintf(stderr, "Not compiled with encryption support");
1384                         return 1;
1385 #endif
1386                 }
1387                 if (!strcmp(argv[a], "-p")) {
1388                         // ignore this argument when called from telnetd
1389                         argc = shift(argc, argv, a, 1);
1390                 }
1391         }
1392
1393         screen_new();
1394         /* Get screen dimensions.  First we go to a default of 80x24.
1395          * Then attempt to read the actual screen size from the terminal.
1396          */
1397         check_screen_dims();
1398
1399         scr_printf("Attaching to server...\n");
1400         ipc = CtdlIPC_new(argc, argv, hostbuf, portbuf);
1401         if (!ipc) {
1402                 error_printf("Can't connect: %s\n", strerror(errno));
1403                 logoff(NULL, 3);
1404         }
1405
1406         CtdlIPC_SetNetworkStatusCallback(ipc, scr_wait_indicator);
1407
1408         if (!(ipc->isLocal)) {
1409                 scr_printf("Connected to %s [%s].\n", ipc->ip_hostname, ipc->ip_address);
1410         }
1411
1412         ipc_for_signal_handlers = ipc;  /* KLUDGE cover your eyes */
1413
1414         CtdlIPC_chat_recv(ipc, aaa);
1415         if (aaa[0] != '2') {
1416                 scr_printf("%s\n", &aaa[4]);
1417                 logoff(ipc, atoi(aaa));
1418         }
1419
1420 #ifdef HAVE_OPENSSLLLLLL
1421         /* Evaluate encryption preferences */
1422         if (arg_encrypt != RC_NO && rc_encrypt != RC_NO) {
1423                 if (!ipc->isLocal || arg_encrypt == RC_YES || rc_encrypt == RC_YES) {
1424                         secure = (CtdlIPCStartEncryption(ipc, aaa) / 100 == 2) ? 1 : 0;
1425                         if (!secure)
1426                                 error_printf("Can't encrypt: %s\n", aaa);
1427                 }
1428         }
1429 #endif
1430
1431         get_serv_info(ipc, telnet_client_host);
1432         scr_printf("%-24s\n%s\n%s\n", ipc->ServInfo.software, ipc->ServInfo.humannode, ipc->ServInfo.site_location);
1433
1434         scr_printf(" pause    next    stop\n");
1435         scr_printf(" ctrl-s  ctrl-o  ctrl-c\n\n");
1436         formout(ipc, "hello");  /* print the opening greeting */
1437         scr_printf("\n");
1438
1439       GSTA:                     /* See if we have a username and password on disk */
1440         if (rc_remember_passwords) {
1441                 get_stored_password(hostbuf, portbuf, fullname, password);
1442                 if (!IsEmptyStr(fullname)) {
1443                         r = CtdlIPCTryLogin(ipc, fullname, aaa);
1444                         if (r / 100 == 3) {
1445                                 r = CtdlIPCTryPassword(ipc, password, aaa);
1446                         }
1447                         if (r / 100 == 2) {
1448                                 load_user_info(aaa);
1449                                 goto PWOK;
1450                         }
1451                         else {
1452                                 set_stored_password(hostbuf, portbuf, "", "");
1453                         }
1454                 }
1455         }
1456
1457         termn8 = 0;
1458         newnow = 0;
1459         do {
1460                 if (!IsEmptyStr(rc_username)) {
1461                         strcpy(fullname, rc_username);
1462                 }
1463                 else {
1464                         newprompt("Enter your name: ", fullname, 29);
1465                 }
1466                 strproc(fullname);
1467                 if (!strcasecmp(fullname, "new")) {     /* just in case */
1468                         scr_printf("Please enter the name you wish to log in with.\n");
1469                 }
1470         } while ((!strcasecmp(fullname, "bbs"))
1471                  || (!strcasecmp(fullname, "new"))
1472                  || (IsEmptyStr(fullname)));
1473
1474         if (!strcasecmp(fullname, "off")) {
1475                 mcmd = 29;
1476                 goto TERMN8;
1477         }
1478
1479         /* FIXME this is a stupid way to do guest mode but it's a reasonable test harness FIXME */
1480         if ((ipc->ServInfo.guest_logins) && (!strcasecmp(fullname, "guest"))) {
1481                 goto PWOK;
1482         }
1483
1484         /* sign on to the server */
1485         r = CtdlIPCTryLogin(ipc, fullname, aaa);
1486         if (r / 100 != 3)
1487                 goto NEWUSR;
1488
1489         /* password authentication */
1490         if (!IsEmptyStr(rc_password)) {
1491                 strcpy(password, rc_password);
1492         }
1493         else {
1494                 newprompt("\rPlease enter your password: ", password, -(SIZ - 1));
1495         }
1496
1497         r = CtdlIPCTryPassword(ipc, password, aaa);
1498         if (r / 100 != 2) {
1499                 strproc(password);
1500                 r = CtdlIPCTryPassword(ipc, password, aaa);
1501         }
1502
1503         if (r / 100 == 2) {
1504                 load_user_info(aaa);
1505                 offer_to_remember_password(ipc, hostbuf, portbuf, fullname, password);
1506                 goto PWOK;
1507         }
1508         scr_printf("<< wrong password >>\n");
1509         if (!IsEmptyStr(rc_password))
1510                 logoff(ipc, 2);
1511         goto GSTA;
1512
1513       NEWUSR:if (IsEmptyStr(rc_password)) {
1514                 scr_printf("'%s' not found.\n", fullname);
1515                 scr_printf("Type 'off' if you would like to exit.\n");
1516                 if (ipc->ServInfo.newuser_disabled == 1) {
1517                         goto GSTA;
1518                 }
1519                 scr_printf("Do you want to create a new user account called '%s'? ", fullname);
1520                 if (yesno() == 0) {
1521                         goto GSTA;
1522                 }
1523         }
1524
1525         r = CtdlIPCCreateUser(ipc, fullname, 1, aaa);
1526         if (r / 100 != 2) {
1527                 scr_printf("%s\n", aaa);
1528                 goto GSTA;
1529         }
1530         load_user_info(aaa);
1531
1532         while (set_password(ipc) != 0);
1533         newnow = 1;
1534
1535         enter_config(ipc, 1);
1536
1537       PWOK:
1538         /* Switch color support on or off if we're in user mode */
1539         if (rc_ansi_color == 3) {
1540                 if (userflags & US_COLOR)
1541                         enable_color = 1;
1542                 else
1543                         enable_color = 0;
1544         }
1545
1546         color(BRIGHT_WHITE);
1547         if (lastcall > 0L) {
1548                 scr_printf("Last login: %s", asctime(localtime(&lastcall)));
1549         }
1550         scr_printf("\n");
1551
1552         r = CtdlIPCMiscCheck(ipc, &chek, aaa);
1553         if (r / 100 == 2) {
1554                 b = chek.newmail;
1555                 if (b > 0) {
1556                         color(BRIGHT_RED);
1557                         if (b == 1)
1558                                 scr_printf("*** You have a new private message in Mail>\n");
1559                         if (b > 1)
1560                                 scr_printf("*** You have %d new private messages in Mail>\n", b);
1561                         color(DIM_WHITE);
1562                         if (!IsEmptyStr(rc_gotmail_cmd)) {
1563                                 rv = system(rc_gotmail_cmd);
1564                                 if (rv)
1565                                         scr_printf("*** failed to check for mail calling %s Reason %d.\n", rc_gotmail_cmd, rv);
1566
1567                         }
1568                 }
1569                 if ((axlevel >= AxAideU) && (chek.needvalid > 0)) {
1570                         scr_printf("*** Users need validation\n");
1571                 }
1572                 if (chek.needregis > 0) {
1573                         scr_printf("*** Please register.\n");
1574                         formout(ipc, "register");
1575                         entregis(ipc);
1576                 }
1577         }
1578         /* Make up some temporary filenames for use in various parts of the
1579          * program.  Don't mess with these once they've been set, because we
1580          * will be unlinking them later on in the program and we don't
1581          * want to delete something that we didn't create. */
1582         CtdlMakeTempFileName(temp, sizeof temp);
1583         CtdlMakeTempFileName(temp2, sizeof temp2);
1584         CtdlMakeTempFileName(tempdir, sizeof tempdir);
1585
1586         r = CtdlIPCGetConfig(ipc, &myself, aaa);
1587         set_floor_mode(ipc);
1588
1589         /* Enter the lobby */
1590         dotgoto(ipc, "_BASEROOM_", 1, 0);
1591
1592         /* Main loop for the system... user is logged in. */
1593         free(uglist[0]);
1594         uglistsize = 0;
1595
1596         if (newnow == 1)
1597                 readmsgs(ipc, LastMessages, ReadForward, 5);
1598         else
1599                 readmsgs(ipc, NewMessages, ReadForward, 0);
1600
1601         /* MAIN COMMAND LOOP */
1602         do {
1603                 mcmd = getcmd(ipc, argbuf);     /* Get keyboard command */
1604
1605 #ifdef TIOCGWINSZ
1606                 check_screen_dims();    /* get screen size */
1607 #endif
1608
1609                 if (termn8 == 0)
1610                         switch (mcmd) {
1611                         case 1:
1612                                 display_help(ipc, "help");
1613                                 break;
1614                         case 4:
1615                                 entmsg(ipc, 0, ((userflags & US_EXTEDIT) ? 2 : 0), 0);
1616                                 break;
1617                         case 36:
1618                                 entmsg(ipc, 0, 1, 0);
1619                                 break;
1620                         case 46:
1621                                 entmsg(ipc, 0, 2, 0);
1622                                 break;
1623                         case 78:
1624                                 entmsg(ipc, 0, ((userflags & US_EXTEDIT) ? 2 : 0), 1);
1625                                 break;
1626                         case 5: /* <G>oto */
1627                                 updatels(ipc);
1628                                 gotonext(ipc);
1629                                 break;
1630                         case 47:        /* <A>bandon */
1631                                 gotonext(ipc);
1632                                 break;
1633                         case 90:        /* <.A>bandon */
1634                                 dotgoto(ipc, argbuf, 0, 0);
1635                                 break;
1636                         case 58:        /* <M>ail */
1637                                 updatelsa(ipc);
1638                                 dotgoto(ipc, "_MAIL_", 1, 0);
1639                                 break;
1640                         case 20:
1641                                 if (!IsEmptyStr(argbuf)) {
1642                                         updatels(ipc);
1643                                         dotgoto(ipc, argbuf, 0, 0);
1644                                 }
1645                                 break;
1646                         case 52:
1647                                 if (!IsEmptyStr(argbuf)) {
1648                                         dotgoto(ipc, argbuf, 0, 0);
1649                                 }
1650                                 break;
1651                         case 95:        /* what exactly is the numbering scheme supposed to be anyway? --Ford, there isn't one. -IO */
1652                                 dotungoto(ipc, argbuf);
1653                                 break;
1654                         case 10:
1655                                 readmsgs(ipc, AllMessages, ReadForward, 0);
1656                                 break;
1657                         case 9:
1658                                 readmsgs(ipc, LastMessages, ReadForward, 5);
1659                                 break;
1660                         case 13:
1661                                 readmsgs(ipc, NewMessages, ReadForward, 0);
1662                                 break;
1663                         case 11:
1664                                 readmsgs(ipc, AllMessages, ReadReverse, 0);
1665                                 break;
1666                         case 12:
1667                                 readmsgs(ipc, OldMessages, ReadReverse, 0);
1668                                 break;
1669                         case 71:
1670                                 readmsgs(ipc, LastMessages, ReadForward, atoi(argbuf));
1671                                 break;
1672                         case 7:
1673                                 forget(ipc);
1674                                 break;
1675                         case 18:
1676                                 subshell();
1677                                 break;
1678                         case 38:
1679                                 updatels(ipc);
1680                                 entroom(ipc);
1681                                 break;
1682                         case 22:
1683                                 killroom(ipc);
1684                                 break;
1685                         case 32:
1686                                 userlist(ipc, argbuf);
1687                                 break;
1688                         case 27:
1689                                 invite(ipc);
1690                                 break;
1691                         case 28:
1692                                 kickout(ipc);
1693                                 break;
1694                         case 23:
1695                                 editthisroom(ipc);
1696                                 break;
1697                         case 14:
1698                                 roomdir(ipc);
1699                                 break;
1700                         case 33:
1701                                 download(ipc, 0);
1702                                 break;
1703                         case 34:
1704                                 download(ipc, 1);
1705                                 break;
1706                         case 31:
1707                                 download(ipc, 2);
1708                                 break;
1709                         case 43:
1710                                 download(ipc, 3);
1711                                 break;
1712                         case 45:
1713                                 download(ipc, 4);
1714                                 break;
1715                         case 55:
1716                                 download(ipc, 5);
1717                                 break;
1718                         case 39:
1719                                 upload(ipc, 0);
1720                                 break;
1721                         case 40:
1722                                 upload(ipc, 1);
1723                                 break;
1724                         case 42:
1725                                 upload(ipc, 2);
1726                                 break;
1727                         case 44:
1728                                 upload(ipc, 3);
1729                                 break;
1730                         case 57:
1731                                 cli_upload(ipc);
1732                                 break;
1733                         case 16:
1734                                 ungoto(ipc);
1735                                 break;
1736                         case 24:
1737                                 whoknows(ipc);
1738                                 break;
1739                         case 26:
1740                                 validate(ipc);
1741                                 break;
1742                         case 29:
1743                         case 30:
1744                                 updatels(ipc);
1745                                 termn8 = 1;
1746                                 break;
1747                         case 48:
1748                                 enterinfo(ipc);
1749                                 break;
1750                         case 49:
1751                                 readinfo(ipc);
1752                                 break;
1753                         case 72:
1754                                 cli_image_upload(ipc, "_userpic_");
1755                                 break;
1756                         case 73:
1757                                 cli_image_upload(ipc, "_roompic_");
1758                                 break;
1759                         case 35:
1760                                 set_password(ipc);
1761                                 break;
1762
1763                         case 21:
1764                                 if (argbuf[0] == 0) {
1765                                         strcpy(argbuf, "?");
1766                                 }
1767                                 display_help(ipc, argbuf);
1768                                 break;
1769
1770                         case 41:
1771                                 formout(ipc, "register");
1772                                 entregis(ipc);
1773                                 break;
1774
1775                         case 15:
1776                                 scr_printf("Are you sure (y/n)? ");
1777                                 if (yesno() == 1) {
1778                                         updatels(ipc);
1779                                         a = 0;
1780                                         termn8 = 1;
1781                                 }
1782                                 break;
1783
1784                         case 85:
1785                                 scr_printf("All users will be disconnected!  " "Really terminate the server? ");
1786                                 if (yesno() == 1) {
1787                                         updatels(ipc);
1788                                         r = CtdlIPCTerminateServerNow(ipc, aaa);
1789                                         scr_printf("%s\n", aaa);
1790                                         if (r / 100 == 2) {
1791                                                 a = 0;
1792                                                 termn8 = 1;
1793                                         }
1794                                 }
1795                                 break;
1796
1797                         case 86:
1798                                 scr_printf("Do you really want to schedule a " "server shutdown? ");
1799                                 if (yesno() == 1) {
1800                                         r = CtdlIPCTerminateServerScheduled(ipc, 1, aaa);
1801                                         if (r / 100 == 2) {
1802                                                 if (atoi(aaa)) {
1803                                                         scr_printf
1804                                                             ("The Citadel server will terminate when all users are logged off.\n");
1805                                                 }
1806                                                 else {
1807                                                         scr_printf("The Citadel server will not terminate.\n");
1808                                                 }
1809                                         }
1810                                 }
1811                                 break;
1812
1813                         case 87:
1814                                 network_config_management(ipc, "listrecp", "Message-by-message mailing list recipients");
1815                                 break;
1816
1817                         case 94:
1818                                 network_config_management(ipc, "digestrecp", "Digest mailing list recipients");
1819                                 break;
1820
1821                         case 6:
1822                                 gotonext(ipc);
1823                                 break;
1824
1825                         case 3:
1826                                 chatmode(ipc);
1827                                 break;
1828
1829                         case 17:
1830                                 who_is_online(ipc, 0);
1831                                 break;
1832
1833                         case 79:
1834                                 who_is_online(ipc, 1);
1835                                 break;
1836
1837                         case 91:
1838                                 who_is_online(ipc, 2);
1839                                 break;
1840
1841                         case 80:
1842                                 do_system_configuration(ipc);
1843                                 break;
1844
1845                         case 82:
1846                                 do_internet_configuration(ipc);
1847                                 break;
1848
1849                         case 84:
1850                                 quiet_mode(ipc);
1851                                 break;
1852
1853                         case 93:
1854                                 stealth_mode(ipc);
1855                                 break;
1856
1857                         case 50:
1858                                 enter_config(ipc, 2);
1859                                 break;
1860
1861                         case 37:
1862                                 enter_config(ipc, 0);
1863                                 set_floor_mode(ipc);
1864                                 break;
1865
1866                         case 59:
1867                                 enter_config(ipc, 3);
1868                                 set_floor_mode(ipc);
1869                                 break;
1870
1871                         case 60:
1872                                 gotofloor(ipc, argbuf, GF_GOTO);
1873                                 break;
1874
1875                         case 61:
1876                                 gotofloor(ipc, argbuf, GF_SKIP);
1877                                 break;
1878
1879                         case 62:
1880                                 forget_this_floor(ipc);
1881                                 break;
1882
1883                         case 63:
1884                                 create_floor(ipc);
1885                                 break;
1886
1887                         case 64:
1888                                 edit_floor(ipc);
1889                                 break;
1890
1891                         case 65:
1892                                 kill_floor(ipc);
1893                                 break;
1894
1895                         case 66:
1896                                 enter_bio(ipc);
1897                                 break;
1898
1899                         case 67:
1900                                 read_bio(ipc);
1901                                 break;
1902
1903                         case 25:
1904                                 edituser(ipc, 25);
1905                                 break;
1906
1907                         case 96:
1908                                 edituser(ipc, 96);
1909                                 break;
1910
1911                         case 8:
1912                                 knrooms(ipc, floor_mode);
1913                                 scr_printf("\n");
1914                                 break;
1915
1916                         case 68:
1917                                 knrooms(ipc, 2);
1918                                 scr_printf("\n");
1919                                 break;
1920
1921                         case 69:
1922                                 misc_server_cmd(ipc, argbuf);
1923                                 break;
1924
1925                         case 70:
1926                                 edit_system_message(ipc, argbuf);
1927                                 break;
1928
1929                         case 19:
1930                                 listzrooms(ipc);
1931                                 scr_printf("\n");
1932                                 break;
1933
1934                         case 51:
1935                                 deletefile(ipc);
1936                                 break;
1937
1938                         case 54:
1939                                 movefile(ipc);
1940                                 break;
1941
1942                         case 56:
1943                                 page_user(ipc);
1944                                 break;
1945
1946                         case 110:       /* <+> Next room */
1947                                 gotoroomstep(ipc, 1, 0);
1948                                 break;
1949
1950                         case 111:       /* <-> Previous room */
1951                                 gotoroomstep(ipc, 0, 0);
1952                                 break;
1953
1954                         case 112:       /* <>> Next floor */
1955                                 gotofloorstep(ipc, 1, GF_GOTO);
1956                                 break;
1957
1958                         case 113:       /* <<> Previous floor */
1959                                 gotofloorstep(ipc, 0, GF_GOTO);
1960                                 break;
1961
1962                         case 116:       /* <.> skip to <+> Next room */
1963                                 gotoroomstep(ipc, 1, 1);
1964                                 break;
1965
1966                         case 117:       /* <.> skip to <-> Previous room */
1967                                 gotoroomstep(ipc, 0, 1);
1968                                 break;
1969
1970                         case 118:       /* <.> skip to <>> Next floor */
1971                                 gotofloorstep(ipc, 1, GF_SKIP);
1972                                 break;
1973
1974                         case 119:       /* <.> skip to <<> Previous floor */
1975                                 gotofloorstep(ipc, 0, GF_SKIP);
1976                                 break;
1977
1978                         case 114:
1979                                 read_config(ipc);
1980                                 break;
1981
1982                         case 115:
1983                                 system_info(ipc);
1984                                 break;
1985
1986                         case 120:       /* .KAnonymous */
1987                                 dotknown(ipc, 0, NULL);
1988                                 break;
1989
1990                         case 121:       /* .KDirectory */
1991                                 dotknown(ipc, 1, NULL);
1992                                 break;
1993
1994                         case 122:       /* .KMatch */
1995                                 dotknown(ipc, 2, argbuf);
1996                                 break;
1997
1998                         case 123:       /* .KpreferredOnly */
1999                                 dotknown(ipc, 3, NULL);
2000                                 break;
2001
2002                         case 124:       /* .KPrivate */
2003                                 dotknown(ipc, 4, NULL);
2004                                 break;
2005
2006                         case 125:       /* .KRead only */
2007                                 dotknown(ipc, 5, NULL);
2008                                 break;
2009
2010                         case 127:       /* Configure POP3 aggregation */
2011                                 do_pop3client_configuration(ipc);
2012                                 break;
2013
2014                         case 128:       /* Configure XML/RSS feed retrieval */
2015                                 do_rssclient_configuration(ipc);
2016                                 break;
2017
2018                         default:
2019                                 break;
2020                         }       /* end switch */
2021         } while (termn8 == 0);
2022
2023       TERMN8:scr_printf("%s logged out.", fullname);
2024         termn8 = 0;
2025         color(ORIGINAL_PAIR);
2026         scr_printf("\n");
2027         while (marchptr != NULL) {
2028                 remove_march(marchptr->march_name, 0);
2029         }
2030         if (mcmd == 30) {
2031                 scr_printf("\n\nType 'off' to disconnect, or next user...\n");
2032         }
2033         CtdlIPCLogout(ipc);
2034         if ((mcmd == 29) || (mcmd == 15)) {
2035                 stty_ctdl(SB_RESTORE);
2036                 formout(ipc, "goodbye");
2037                 logoff(ipc, 0);
2038         }
2039         /* Free the ungoto list */
2040         for (lp = 0; lp < uglistsize; lp++) {
2041                 free(uglist[lp]);
2042         }
2043         uglistsize = 0;
2044         goto GSTA;
2045
2046 }                               /* end main() */