4 * Client-side support functions.
15 #include <sys/types.h>
16 #include <sys/ioctl.h>
22 #if TIME_WITH_SYS_TIME
23 # include <sys/time.h>
27 # include <sys/time.h>
42 #include "citadel_ipc.h"
45 #ifndef HAVE_GETUTLINE
46 struct utmp *getutline(struct utmp *ut);
55 #include "citadel_decls.h"
56 #include "routines2.h"
58 #define IFAIDE if(axlevel>=6)
59 #define IFNAIDE if (axlevel<6)
61 extern unsigned userflags;
62 extern char *axdefs[7];
63 extern char sigcaught;
64 extern char rc_floor_mode;
65 extern int rc_ansi_color;
66 extern int rc_prompt_control;
68 /* Destructive backspace */
69 void back(int spaces) {
71 for (a=0; a<spaces; ++a) {
78 void hit_any_key(CtdlIPC *ipc) { /* hit any key to continue */
83 scr_printf("%s\r", ipc->ServInfo.moreprompt);
87 for (a=0; a<strlen(ipc->ServInfo.moreprompt); ++a)
91 if ( (rc_prompt_control == 1)
92 || ((rc_prompt_control == 3) && (userflags & US_PROMPTCTL)) ) {
93 if (b == 'q' || b == 'Q' || b == 's' || b == 'S')
95 if (b == 'n' || b == 'N')
98 if (b==NEXT_KEY) sigcaught = SIGINT;
99 if (b==STOP_KEY) sigcaught = SIGQUIT;
103 * change a user's access level
105 void edituser(CtdlIPC *ipc)
108 char who[USERNAME_SIZE];
109 struct ctdluser *user = NULL;
111 int r; /* IPC response code */
113 newprompt("User name: ", who, 25);
114 while ((r = CtdlIPCAideGetUserParameters(ipc, who, &user, buf)) / 100 != 2) {
115 scr_printf("%s\n", buf);
116 scr_printf("Do you want to create this user? ");
118 r = CtdlIPCCreateUser(ipc, who, 0, buf);
123 scr_printf("%s\n",&buf[4]);
129 val_user(ipc, user->fullname, 0); /* Display registration */
131 if (newnow || boolprompt("Change password", 0)) { /* I'm lazy */
132 strprompt("Password", user->password, -19);
135 user->axlevel = intprompt("Access level", user->axlevel, 0, 6);
137 user->flags = set_attr(ipc, user->flags,
138 "Permission to send Internet mail",
141 if (boolprompt("Ask user to register again", !(user->flags & US_REGIS)))
142 user->flags &= ~US_REGIS;
144 user->flags |= US_REGIS;
145 user->timescalled = intprompt("Times called",
146 user->timescalled, 0, INT_MAX);
147 user->posted = intprompt("Messages posted",
148 user->posted, 0, INT_MAX);
149 user->lastcall = boolprompt("Set last call to now", 0) ?
150 time(NULL) : user->lastcall;
151 user->USuserpurge = intprompt("Purge time (in days, 0 for system default",
152 user->USuserpurge, 0, INT_MAX);
154 r = CtdlIPCAideSetUserParameters(ipc, user, buf);
156 scr_printf("%s\n", buf);
162 /* Display a prompt and flip a bit based on whether the user answers
163 * yes or no. Yes=1 and No=0, unless 'backwards' is set to a nonzero value
164 * in which case No=1 and Yes=0.
166 int set_attr(CtdlIPC *ipc, unsigned int sval, char *prompt, unsigned int sbit, int backwards)
173 scr_printf("%45s ", prompt);
176 color(BRIGHT_MAGENTA);
179 scr_printf("%3s", ((temp&sbit) ? "No":"Yes"));
182 scr_printf("%3s", ((temp&sbit) ? "Yes":"No"));
190 if (backwards) a = 1 - a;
192 if (backwards) a = 1 - a;
195 if (!a) temp = (temp^sbit);
200 * modes are: 0 - .EC command, 1 - .EC for new user,
201 * 2 - toggle Xpert mode 3 - toggle floor mode
203 void enter_config(CtdlIPC *ipc, int mode)
206 struct ctdluser *user = NULL;
207 int r; /* IPC response code */
209 r = CtdlIPCGetConfig(ipc, &user, buf);
211 scr_printf("%s\n", buf);
216 if (mode == 0 || mode == 1) {
218 user->USscreenwidth = intprompt("Enter your screen width",
219 user->USscreenwidth, 20, 255);
220 user->USscreenheight = intprompt("Enter your screen height",
221 user->USscreenheight, 3, 255);
223 user->flags = set_attr(ipc, user->flags,
224 "Are you an experienced Citadel user",
226 if ((user->flags & US_EXPERT) == 0 && mode == 1) {
231 user->flags = set_attr(ipc, user->flags,
232 "Print last old message on New message request",
235 user->flags = set_attr(ipc, user->flags,
236 "Prompt after each message",
239 if ((user->flags & US_NOPROMPT) == 0)
240 user->flags = set_attr(ipc, user->flags,
241 "Use 'disappearing' prompts",
244 user->flags = set_attr(ipc, user->flags,
245 "Pause after each screenful of text",
248 if (rc_prompt_control == 3 && (user->flags & US_PAGINATOR))
249 user->flags = set_attr(ipc, user->flags,
250 "<N>ext and <S>top work at paginator prompt",
253 if (rc_floor_mode == RC_DEFAULT)
254 user->flags = set_attr(ipc, user->flags,
255 "View rooms by floor",
258 if (rc_ansi_color == 3)
259 user->flags = set_attr(ipc, user->flags,
260 "Enable color support",
263 if ((user->flags & US_EXPERT) == 0)
264 formout(ipc, "unlisted");
266 user->flags = set_attr(ipc, user->flags,
267 "Be unlisted in userlog",
272 if (user->flags & US_EXPERT) {
273 user->flags ^= US_EXPERT;
274 scr_printf("Expert mode now OFF\n");
276 user->flags |= US_EXPERT;
277 scr_printf("Expert mode now ON\n");
282 if (user->flags & US_FLOORS) {
283 user->flags ^= US_FLOORS;
284 scr_printf("Floor mode now OFF\n");
286 user->flags |= US_FLOORS;
287 scr_printf("Floor mode now ON\n");
291 r = CtdlIPCSetConfig(ipc, user, buf);
292 if (r / 100 != 2) scr_printf("%s\n", buf);
293 userflags = user->flags;
298 * getstring() - get a line of text from a file
299 * ignores lines beginning with "#"
301 int getstring(FILE *fp, char *string)
316 } while(string[0]=='#');
317 return(strlen(string));
321 /* Searches for patn in search string */
322 int pattern(char *search, char *patn) {
325 for (a=0; a<strlen(search); ++a) {
326 b=strncasecmp(&search[a],patn,strlen(patn));
333 void strproc(char *string)
337 if (strlen(string)==0) return;
339 /* Convert non-printable characters to blanks */
340 for (a=0; a<strlen(string); ++a) {
341 if (string[a]<32) string[a]=32;
342 if (string[a]>126) string[a]=32;
345 /* Remove leading and trailing blanks */
346 while(string[0]<33) strcpy(string,&string[1]);
347 while(string[strlen(string)-1]<33) string[strlen(string)-1]=0;
349 /* Remove double blanks */
350 for (a=0; a<strlen(string); ++a) {
351 if ((string[a]==32)&&(string[a+1]==32)) {
352 strcpy(&string[a],&string[a+1]);
357 /* remove characters which would interfere with the network */
358 for (a=0; a<strlen(string); ++a) {
359 if (string[a]=='!') strcpy(&string[a],&string[a+1]);
360 if (string[a]=='@') strcpy(&string[a],&string[a+1]);
361 if (string[a]=='_') strcpy(&string[a],&string[a+1]);
362 if (string[a]==',') strcpy(&string[a],&string[a+1]);
363 if (string[a]=='%') strcpy(&string[a],&string[a+1]);
364 if (string[a]=='|') strcpy(&string[a],&string[a+1]);
370 #ifndef HAVE_STRERROR
372 * replacement strerror() for systems that don't have it
374 char *strerror(int e)
376 static char buf[128];
378 snprintf(buf, sizeof buf, "errno = %d",e);
384 void progress(CtdlIPC* ipc, unsigned long curr, unsigned long cmax)
387 "**************************************************";
388 char dots_printed[51];
393 sln_printf("\r%79s\r","");
394 status_line(ipc->ServInfo.humannode, ipc->ServInfo.bbs_city,
395 room_name, secure, 0);
397 /* a will be range 0-50 rather than 0-100 */
398 a=(curr * 50) / cmax;
399 sprintf(fmt, "[%%s%%%lds] %%3ld%%%% %%10ld/%%10ld\r", 50 - a);
400 strncpy(dots_printed, dots, a);
402 sln_printf(fmt, dots_printed, "",
403 curr * 100 / cmax, curr, cmax);
410 * NOT the same locate_host() in locate_host.c. This one just does a
411 * 'who am i' to try to discover where the user is...
413 void locate_host(CtdlIPC* ipc, char *hbuf)
420 who = (FILE *)popen("who am i","r");
422 strcpy(hbuf, ipc->ServInfo.fqdn);
425 fgets(buf,sizeof buf,who);
429 for (a=0; a<strlen(buf); ++a) {
430 if ((buf[a]=='(')||(buf[a]==')')) ++b;
433 strcpy(hbuf, ipc->ServInfo.fqdn);
437 for (a=0; a<strlen(buf); ++a) {
439 strcpy(buf,&buf[a+1]);
442 for (a=0; a<strlen(buf); ++a) {
443 if (buf[a]==')') buf[a] = 0;
446 if (strlen(buf)==0) strcpy(hbuf, ipc->ServInfo.fqdn);
447 else strncpy(hbuf,buf,24);
449 char *tty = ttyname(0);
450 #ifdef HAVE_GETUTXLINE
451 struct utmpx ut, *put;
453 struct utmp ut, *put;
458 safestrncpy(hbuf, ipc->ServInfo.fqdn, 24);
462 if (strncmp(tty, "/dev/", 5))
465 safestrncpy(ut.ut_line, &tty[5], sizeof ut.ut_line);
467 #ifdef HAVE_GETUTXLINE /* Solaris uses this */
468 if ((put = getutxline(&ut)) == NULL)
470 if ((put = getutline(&ut)) == NULL)
474 #if defined(HAVE_UT_TYPE) || defined(HAVE_GETUTXLINE)
475 if (put->ut_type == USER_PROCESS) {
477 #if defined(HAVE_UT_HOST) || defined(HAVE_GETUTXLINE)
479 safestrncpy(hbuf, put->ut_host, 24);
482 safestrncpy(hbuf, put->ut_line, 24);
483 #if defined(HAVE_UT_TYPE) || defined(HAVE_GETUTXLINE)
487 #endif /* HAVE_UTMP_H */
491 * miscellaneous server commands (testing, etc.)
493 void misc_server_cmd(CtdlIPC *ipc, char *cmd) {
496 CtdlIPC_chat_send(ipc, cmd);
497 CtdlIPC_chat_recv(ipc, buf);
498 scr_printf("%s\n",buf);
500 set_keepalives(KA_HALF);
501 while (CtdlIPC_chat_recv(ipc, buf), strcmp(buf,"000")) {
502 scr_printf("%s\n",buf);
504 set_keepalives(KA_YES);
509 newprompt("> ",buf,255);
510 CtdlIPC_chat_send(ipc, buf);
511 } while(strcmp(buf,"000"));
518 * compute the checksum of a file
520 int file_checksum(char *filename)
526 fp = fopen(filename,"r");
527 if (fp == NULL) return(0);
529 /* yes, this algorithm may allow cksum to overflow, but that's ok
530 * as long as it overflows consistently, which it will.
532 while (ch=getc(fp), ch>=0) {
533 cksum = (cksum + ch);
541 * nuke a directory and its contents
543 int nukedir(char *dirname)
549 dp = opendir(dirname);
554 while (d = readdir(dp), d != NULL) {
555 snprintf(filename, sizeof filename, "%s/%s",
561 return(rmdir(dirname));