1 /* Citadel/UX support routines */
11 #include <sys/types.h>
12 #include <sys/ioctl.h>
23 #ifndef HAVE_GETUTLINE
24 struct utmp *getutline(struct utmp *ut);
34 void sttybbs(int cmd);
35 void newprompt(char *prompt, char *str, int len);
36 void val_user(char *, int);
37 void formout(char *name);
38 void logoff(int code);
39 void set_keepalives(int s);
40 void strprompt(char *prompt, char *str, int len);
41 void newprompt(char *prompt, char *str, int len);
42 void color(int colornum);
44 #define IFAIDE if(axlevel>=6)
45 #define IFNAIDE if (axlevel<6)
47 extern unsigned userflags;
48 extern char *axdefs[7];
49 extern char sigcaught;
50 extern struct CtdlServInfo serv_info;
51 extern char rc_floor_mode;
52 extern int rc_ansi_color;
54 int struncmp(char *lstr, char *rstr, int len)
59 lc=tolower(lstr[pos]);
60 rc=tolower(rstr[pos]);
61 if ((lc==0)&&(rc==0)) return(0);
62 if (lc<rc) return(-1);
71 * check for the presence of a character within a string (returns count)
73 int haschar(char *st, int ch)
77 for (a=0; a<strlen(st); ++a) if (st[a]==ch) ++b;
82 void back(int spaces) /* Destructive backspace */
85 for (a=1; a<=spaces; ++a) {
86 putc(8,stdout); putc(32,stdout); putc(8,stdout);
90 void hit_any_key(void) { /* hit any key to continue */
93 printf("%s\r",serv_info.serv_moreprompt);
96 for (a=0; a<strlen(serv_info.serv_moreprompt); ++a)
100 if (b==NEXT_KEY) sigcaught = SIGINT;
101 if (b==STOP_KEY) sigcaught = SIGQUIT;
105 * change a user's access level
120 newprompt("User name: ",who,25);
121 sprintf(buf,"AGUP %s",who);
125 printf("%s\n",&buf[4]);
128 extract(who, &buf[4], 0);
129 extract(pass, &buf[4], 1);
130 flags = extract_int(&buf[4], 2);
131 timescalled = extract_int(&buf[4], 3);
132 posted = extract_int(&buf[4], 4);
133 axlevel = extract_int(&buf[4], 5);
134 usernum = extract_long(&buf[4], 6);
135 lastcall = extract_long(&buf[4], 7);
136 userpurge = extract_int(&buf[4], 8);
138 val_user(who, 0); /* Display registration */
139 strprompt("Password", pass, 19);
140 axlevel = intprompt("Access level", axlevel, 0, 6);
141 timescalled = intprompt("Times called", timescalled, 0, INT_MAX);
142 posted = intprompt("Messages posted", posted, 0, INT_MAX);
143 lastcall = (boolprompt("Set last call to now", 0)?time(NULL):lastcall);
144 userpurge = intprompt("Purge time (in days, 0 for system default",
145 userpurge, 0, INT_MAX);
147 sprintf(buf, "ASUP %s|%s|%d|%d|%d|%d|%ld|%ld|%d",
148 who, pass, flags, timescalled, posted, axlevel, usernum,
149 lastcall, userpurge);
153 printf("%s\n",&buf[4]);
158 int set_attr(int sval, char *prompt, unsigned int sbit)
165 printf("%45s [", prompt);
167 printf("%3s", ((temp&sbit) ? "Yes":"No"));
171 a=yesno_d(temp&sbit);
174 if (!a) temp=(temp^sbit);
179 * modes are: 0 - .EC command, 1 - .EC for new user,
180 * 2 - toggle Xpert mode 3 - toggle floor mode
182 void enter_config(int mode)
184 int width,height,flags;
191 printf("%s\n",&buf[4]);
195 width = extract_int(&buf[4],0);
196 height = extract_int(&buf[4],1);
197 flags = extract_int(&buf[4],2);
199 if ((mode==0)||(mode==1)) {
201 width = intprompt("Enter your screen width",width,20,255);
202 height = intprompt("Enter your screen height",height,3,255);
204 flags = set_attr(flags,
205 "Are you an experienced Citadel user",US_EXPERT);
206 if ( ((flags&US_EXPERT)==0) && (mode==1))
208 flags = set_attr(flags,
209 "Print last old message on New message request",US_LASTOLD);
210 if ((flags&US_EXPERT)==0) formout("unlisted");
211 flags = set_attr(flags,"Be unlisted in userlog",US_UNLISTED);
212 flags = set_attr(flags,"Suppress message prompts",US_NOPROMPT);
213 if ((flags & US_NOPROMPT)==0)
214 flags = set_attr(flags,"Use 'disappearing' prompts",US_DISAPPEAR);
215 flags = set_attr(flags,
216 "Pause after each screenful of text",US_PAGINATOR);
217 if (rc_floor_mode == RC_DEFAULT) {
218 flags = set_attr(flags,
219 "View rooms by floor",US_FLOORS);
221 if (rc_ansi_color == 3) {
222 flags = set_attr(flags,
223 "Enable color support",US_COLOR);
228 if (flags & US_EXPERT) {
229 flags = (flags ^ US_EXPERT);
230 printf("Expert mode now OFF\n");
233 flags = (flags | US_EXPERT);
234 printf("Expert mode now ON\n");
239 if (flags & US_FLOORS) {
240 flags = (flags ^ US_FLOORS);
241 printf("Floor mode now OFF\n");
244 flags = (flags | US_FLOORS);
245 printf("Floor mode now ON\n");
249 sprintf(buf,"SETU %d|%d|%d",width,height,flags);
252 if (buf[0]!='2') printf("%s\n",&buf[4]);
257 * getstring() - get a line of text from a file
258 * ignores lines beginning with "#"
260 int getstring(FILE *fp, char *string)
275 } while(string[0]=='#');
276 return(strlen(string));
279 int pattern(char *search, char *patn) /* Searches for patn in search string */
284 for (a=0; a<strlen(search); ++a)
285 { b=struncmp(&search[a],patn,strlen(patn));
291 void interr(int errnum) /* display internal error as defined in errmsgs */
293 printf("*** INTERNAL ERROR %d\n",errnum);
294 printf("(Press any key to continue)\n");
302 * Check to see if we need to pause at the end of a screen.
303 * If we do, we have to disable server keepalives during the pause because
304 * we are probably in the middle of a server operation and the NOOP command
305 * would confuse everything.
307 int checkpagin(int lp, int pagin, int height)
309 if (pagin!=1) return(0);
310 if (lp>=(height-1)) {
311 set_keepalives(KA_NO);
313 set_keepalives(KA_YES);
320 void strproc(char *string)
324 if (strlen(string)==0) return;
326 /* Convert non-printable characters to blanks */
327 for (a=0; a<strlen(string); ++a) {
328 if (string[a]<32) string[a]=32;
329 if (string[a]>126) string[a]=32;
332 /* Remove leading and trailing blanks */
333 while(string[0]<33) strcpy(string,&string[1]);
334 while(string[strlen(string)-1]<33) string[strlen(string)-1]=0;
336 /* Remove double blanks */
337 for (a=0; a<strlen(string); ++a) {
338 if ((string[a]==32)&&(string[a+1]==32)) {
339 strcpy(&string[a],&string[a+1]);
344 /* remove characters which would interfere with the network */
345 for (a=0; a<strlen(string); ++a) {
346 if (string[a]=='!') strcpy(&string[a],&string[a+1]);
347 if (string[a]=='@') strcpy(&string[a],&string[a+1]);
348 if (string[a]=='_') strcpy(&string[a],&string[a+1]);
349 if (string[a]==',') strcpy(&string[a],&string[a+1]);
350 if (string[a]=='%') strcpy(&string[a],&string[a+1]);
351 if (string[a]=='|') strcpy(&string[a],&string[a+1]);
357 #ifndef HAVE_STRERROR
359 * replacement strerror() for systems that don't have it
361 char *strerror(int e)
365 sprintf(buf,"errno = %d",e);
371 void progress(long int curr, long int cmax)
373 static long dots_printed;
377 printf(".......................................");
378 printf(".......................................\r");
382 else if (curr==cmax) {
383 printf("\r%79s\n","");
386 a=(curr * 100) / cmax;
388 while (dots_printed < a) {
398 * NOT the same locate_host() in locate_host.c. This one just does a
399 * 'who am i' to try to discover where the user is...
401 void locate_host(char *hbuf)
408 who = (FILE *)popen("who am i","r");
410 strcpy(hbuf,serv_info.serv_fqdn);
417 for (a=0; a<strlen(buf); ++a) {
418 if ((buf[a]=='(')||(buf[a]==')')) ++b;
421 strcpy(hbuf,serv_info.serv_fqdn);
425 for (a=0; a<strlen(buf); ++a) {
427 strcpy(buf,&buf[a+1]);
430 for (a=0; a<strlen(buf); ++a) {
431 if (buf[a]==')') buf[a] = 0;
434 if (strlen(buf)==0) strcpy(hbuf,serv_info.serv_fqdn);
435 else strncpy(hbuf,buf,24);
437 char *tty = ttyname(0);
438 struct utmp ut, *put;
442 safestrncpy(hbuf, serv_info.serv_fqdn, 24);
446 if (strncmp(tty, "/dev/", 5))
449 safestrncpy(ut.ut_line, &tty[5], sizeof ut.ut_line);
451 if ((put = getutline(&ut)) == NULL)
455 if (put->ut_type == USER_PROCESS) {
458 safestrncpy(hbuf, put->ut_host, 24);
460 safestrncpy(hbuf, put->ut_line, 24);
465 #endif /* HAVE_UTMP_H */
469 * miscellaneous server commands (testing, etc.)
471 void misc_server_cmd(char *cmd) {
478 set_keepalives(KA_NO);
479 while (serv_gets(buf), strcmp(buf,"000")) {
482 set_keepalives(KA_YES);
487 newprompt("> ",buf,255);
489 } while(strcmp(buf,"000"));
496 * compute the checksum of a file
498 int file_checksum(char *filename)
504 fp = fopen(filename,"r");
505 if (fp == NULL) return(0);
507 /* yes, this algorithm may allow cksum to overflow, but that's ok
508 * as long as it overflows consistently, which it will.
510 while (ch=getc(fp), ch>=0) {
511 cksum = (cksum + ch);
519 * nuke a directory and its contents
521 int nukedir(char *dirname)
527 dp = opendir(dirname);
532 while (d = readdir(dp), d != NULL) {
533 sprintf(filename, "%s/%s", dirname, d->d_name);
538 return(rmdir(dirname));