]> code.citadel.org Git - citadel.git/blob - citadel/routines.c
* Removed references to strucmp() and struncmp(), replaced them with
[citadel.git] / citadel / routines.c
1 /* Citadel/UX support routines */
2 /* $Id$ */
3
4 #include "sysdep.h"
5 #include <stdlib.h>
6 #include <unistd.h>
7 #include <fcntl.h>
8 #include <stdio.h>
9 #include <ctype.h>
10 #include <string.h>
11 #include <sys/types.h>
12 #include <sys/ioctl.h>
13 #include <pwd.h>
14 #include <signal.h>
15 #include <dirent.h>
16 #include <errno.h>
17 #include <time.h>
18 #include <limits.h>
19 #ifdef HAVE_UTMP_H
20 #include <utmp.h>
21 #endif
22 #ifdef HAVE_UTMPX_H
23 #include <utmpx.h>
24 #endif
25
26 #ifndef HAVE_GETUTLINE
27 struct utmp *getutline(struct utmp *ut);
28 #endif
29
30 #define ROUTINES_C
31
32 #include "citadel.h"
33 #include "routines.h"
34 #include "commands.h"
35 #include "tools.h"
36
37 void sttybbs(int cmd);
38 void newprompt(char *prompt, char *str, int len);
39 void val_user(char *, int);
40 void formout(char *name);
41 void logoff(int code);
42 void set_keepalives(int s);
43 void strprompt(char *prompt, char *str, int len);
44 void newprompt(char *prompt, char *str, int len);
45 void color(int colornum);
46
47 #define IFAIDE if(axlevel>=6)
48 #define IFNAIDE if (axlevel<6)
49
50 extern unsigned userflags;
51 extern char *axdefs[7];
52 extern char sigcaught;
53 extern struct CtdlServInfo serv_info;
54 extern char rc_floor_mode;
55 extern int rc_ansi_color;
56
57 void back(int spaces) /* Destructive backspace */
58             {
59         int a;
60         for (a=1; a<=spaces; ++a) {
61                 putc(8,stdout); putc(32,stdout); putc(8,stdout);
62                 }
63         }
64
65 void hit_any_key(void) {                /* hit any key to continue */
66         int a,b;
67
68         color(COLOR_PUSH);
69         color(DIM_RED);
70         printf("%s\r",serv_info.serv_moreprompt);
71         color(COLOR_POP);
72         sttybbs(0);
73         b=inkey();
74         for (a=0; a<strlen(serv_info.serv_moreprompt); ++a)
75                 putc(' ',stdout);
76         putc(13,stdout);
77         sttybbs(1);
78         if (b==NEXT_KEY) sigcaught = SIGINT;
79         if (b==STOP_KEY) sigcaught = SIGQUIT;
80         }
81
82 /*
83  * change a user's access level
84  */
85 void edituser(void)
86 {
87         char buf[256];
88         char who[256];
89         char pass[256];
90         int flags;
91         int timescalled;
92         int posted;
93         int axlevel;
94         long usernum;
95         time_t lastcall;
96         int userpurge;
97
98         newprompt("User name: ",who,25);
99         sprintf(buf,"AGUP %s",who);
100         serv_puts(buf);
101         serv_gets(buf);
102         if (buf[0]!='2') {
103                 printf("%s\n",&buf[4]);
104                 return;
105                 }
106         extract(who, &buf[4], 0);
107         extract(pass, &buf[4], 1);
108         flags = extract_int(&buf[4], 2);
109         timescalled = extract_int(&buf[4], 3);
110         posted = extract_int(&buf[4], 4);
111         axlevel = extract_int(&buf[4], 5);
112         usernum = extract_long(&buf[4], 6);
113         lastcall = extract_long(&buf[4], 7);
114         userpurge = extract_int(&buf[4], 8);
115
116         val_user(who, 0); /* Display registration */
117         strprompt("Password", pass, 19);
118         axlevel = intprompt("Access level", axlevel, 0, 6);
119         timescalled = intprompt("Times called", timescalled, 0, INT_MAX);
120         posted = intprompt("Messages posted", posted, 0, INT_MAX);
121         lastcall = (boolprompt("Set last call to now", 0)?time(NULL):lastcall);
122         userpurge = intprompt("Purge time (in days, 0 for system default",
123                                 userpurge, 0, INT_MAX);
124
125         sprintf(buf, "ASUP %s|%s|%d|%d|%d|%d|%ld|%ld|%d",
126                 who, pass, flags, timescalled, posted, axlevel, usernum,
127                 (long)lastcall, userpurge);
128         serv_puts(buf);
129         serv_gets(buf);
130         if (buf[0]!='2') {
131                 printf("%s\n",&buf[4]);
132                 }
133         }
134
135
136 int set_attr(int sval, char *prompt, unsigned int sbit)
137 {
138         int a;
139         int temp;
140
141         temp = sval;
142         color(DIM_WHITE);
143         printf("%45s ", prompt);
144         color(DIM_MAGENTA);
145         printf("[");
146         color(BRIGHT_MAGENTA);
147         printf("%3s", ((temp&sbit) ? "Yes":"No"));
148         color(DIM_MAGENTA);
149         printf("]? ");
150         color(BRIGHT_CYAN);
151         a=yesno_d(temp&sbit);
152         color(DIM_WHITE);
153         temp=(temp|sbit);
154         if (!a) temp=(temp^sbit);
155         return(temp);
156         }
157
158 /*
159  * modes are:  0 - .EC command, 1 - .EC for new user,
160  *             2 - toggle Xpert mode  3 - toggle floor mode
161  */
162 void enter_config(int mode)
163 {
164         int width,height,flags;
165         char buf[128];
166
167         sprintf(buf,"GETU");
168         serv_puts(buf);
169         serv_gets(buf);
170         if (buf[0]!='2') {
171                 printf("%s\n",&buf[4]);
172                 return;
173                 }
174
175         width = extract_int(&buf[4],0);
176         height = extract_int(&buf[4],1);
177         flags = extract_int(&buf[4],2);
178
179         if ((mode==0)||(mode==1)) {
180
181          width = intprompt("Enter your screen width",width,20,255);
182          height = intprompt("Enter your screen height",height,3,255);
183  
184          flags = set_attr(flags,
185                 "Are you an experienced Citadel user",US_EXPERT);
186          if ( ((flags&US_EXPERT)==0) && (mode==1))
187                 return;
188          flags = set_attr(flags,
189                 "Print last old message on New message request",US_LASTOLD);
190          if ((flags&US_EXPERT)==0) formout("unlisted");
191          flags = set_attr(flags,"Be unlisted in userlog",US_UNLISTED);
192          flags = set_attr(flags,"Suppress message prompts",US_NOPROMPT);
193          if ((flags & US_NOPROMPT)==0)
194             flags = set_attr(flags,"Use 'disappearing' prompts",US_DISAPPEAR);
195          flags = set_attr(flags,
196                 "Pause after each screenful of text",US_PAGINATOR);
197          if (rc_floor_mode == RC_DEFAULT) {
198           flags = set_attr(flags,
199                 "View rooms by floor",US_FLOORS);
200           }
201          if (rc_ansi_color == 3) {
202           flags = set_attr(flags,
203                 "Enable color support",US_COLOR);
204           }
205          }
206
207         if (mode==2) {
208          if (flags & US_EXPERT) {
209                 flags = (flags ^ US_EXPERT);
210                 printf("Expert mode now OFF\n");
211                 }
212          else {
213                 flags = (flags | US_EXPERT);
214                 printf("Expert mode now ON\n");
215                 }
216          }
217
218         if (mode==3) {
219          if (flags & US_FLOORS) {
220                 flags = (flags ^ US_FLOORS);
221                 printf("Floor mode now OFF\n");
222                 }
223          else {
224                 flags = (flags | US_FLOORS);
225                 printf("Floor mode now ON\n");
226                 }
227          }
228
229         sprintf(buf,"SETU %d|%d|%d",width,height,flags);
230         serv_puts(buf);
231         serv_gets(buf);
232         if (buf[0]!='2') printf("%s\n",&buf[4]);
233         userflags = flags;
234 }
235
236 /*
237  * getstring()  -  get a line of text from a file
238  *                 ignores lines beginning with "#"
239  */
240 int getstring(FILE *fp, char *string)
241 {
242         int a,c;
243         do {
244                 strcpy(string,"");
245                 a=0;
246                 do {
247                         c=getc(fp);
248                         if (c<0) {
249                                 string[a]=0;
250                                 return(-1);
251                                 }
252                         string[a++]=c;
253                         } while(c!=10);
254                         string[a-1]=0;
255                 } while(string[0]=='#');
256         return(strlen(string));
257         }
258
259 int pattern(char *search, char *patn)   /* Searches for patn in search string */
260               
261             
262 {
263         int a,b;
264         for (a=0; a<strlen(search); ++a)
265         {       b=strncasecmp(&search[a],patn,strlen(patn));
266                 if (b==0) return(b);
267                 }
268         return(-1);
269 }
270
271 void interr(int errnum) /* display internal error as defined in errmsgs */
272             {
273         printf("*** INTERNAL ERROR %d\n",errnum);
274         printf("(Press any key to continue)\n");
275         inkey();
276         logoff(errnum);
277 }
278
279
280
281 void strproc(char *string)
282 {
283         int a;
284
285         if (strlen(string)==0) return;
286
287         /* Convert non-printable characters to blanks */
288         for (a=0; a<strlen(string); ++a) {
289                 if (string[a]<32) string[a]=32;
290                 if (string[a]>126) string[a]=32;
291                 }
292
293         /* Remove leading and trailing blanks */
294         while(string[0]<33) strcpy(string,&string[1]);
295         while(string[strlen(string)-1]<33) string[strlen(string)-1]=0;
296
297         /* Remove double blanks */
298         for (a=0; a<strlen(string); ++a) {
299                 if ((string[a]==32)&&(string[a+1]==32)) {
300                         strcpy(&string[a],&string[a+1]);
301                         a=0;
302                         }
303                 }
304
305         /* remove characters which would interfere with the network */
306         for (a=0; a<strlen(string); ++a) {
307                 if (string[a]=='!') strcpy(&string[a],&string[a+1]);
308                 if (string[a]=='@') strcpy(&string[a],&string[a+1]);
309                 if (string[a]=='_') strcpy(&string[a],&string[a+1]);
310                 if (string[a]==',') strcpy(&string[a],&string[a+1]);
311                 if (string[a]=='%') strcpy(&string[a],&string[a+1]);
312                 if (string[a]=='|') strcpy(&string[a],&string[a+1]);
313                 }
314
315         }
316
317
318 #ifndef HAVE_STRERROR
319 /*
320  * replacement strerror() for systems that don't have it
321  */
322 char *strerror(int e)
323 {
324         static char buf[32];
325
326         sprintf(buf,"errno = %d",e);
327         return(buf);
328         }
329 #endif
330
331
332 void progress(long int curr, long int cmax)
333 {
334         static long dots_printed;
335         long a;
336
337         if (curr==0) {
338                 printf(".......................................");
339                 printf(".......................................\r");
340                 fflush(stdout);
341                 dots_printed = 0;
342                 }
343         else if (curr==cmax) {
344                 printf("\r%79s\n","");
345                 }
346         else {
347                 a=(curr * 100) / cmax;
348                 a=a*78; a=a/100;
349                 while (dots_printed < a) {
350                         printf("*");
351                         ++dots_printed;
352                         fflush(stdout);
353                         }
354                 }
355         }
356
357
358 /*
359  * NOT the same locate_host() in locate_host.c.  This one just does a
360  * 'who am i' to try to discover where the user is...
361  */
362 void locate_host(char *hbuf)
363 {
364 #ifndef HAVE_UTMP_H
365         char buf[256];
366         FILE *who;
367         int a,b;
368
369         who = (FILE *)popen("who am i","r");
370         if (who==NULL) {
371                 strcpy(hbuf,serv_info.serv_fqdn);
372                 return; 
373                 }
374         fgets(buf,256,who);
375         pclose(who);
376
377         b = 0;
378         for (a=0; a<strlen(buf); ++a) {
379                 if ((buf[a]=='(')||(buf[a]==')')) ++b;
380                 }
381         if (b<2) {
382                 strcpy(hbuf,serv_info.serv_fqdn);
383                 return;
384                 }
385
386         for (a=0; a<strlen(buf); ++a) {
387                 if (buf[a]=='(') {
388                         strcpy(buf,&buf[a+1]);
389                         }
390                 }
391         for (a=0; a<strlen(buf); ++a) {
392                 if (buf[a]==')') buf[a] = 0;
393                 }
394
395         if (strlen(buf)==0) strcpy(hbuf,serv_info.serv_fqdn);
396         else strncpy(hbuf,buf,24);
397 #else
398         char *tty = ttyname(0);
399 #ifdef HAVE_GETUTXLINE
400         struct utmpx ut, *put;
401 #else
402         struct utmp ut, *put;
403 #endif
404
405         if (tty == NULL) {
406             fail:
407                 safestrncpy(hbuf, serv_info.serv_fqdn, 24);
408                 return;
409                 }
410
411         if (strncmp(tty, "/dev/", 5))
412                 goto fail;
413
414         safestrncpy(ut.ut_line, &tty[5], sizeof ut.ut_line);
415
416 #ifdef HAVE_GETUTXLINE /* Solaris uses this */
417         if ((put = getutxline(&ut)) == NULL)
418 #else
419         if ((put = getutline(&ut)) == NULL)
420 #endif
421                 goto fail;
422
423 #if defined(HAVE_UT_TYPE) || defined(HAVE_GETUTXLINE)
424         if (put->ut_type == USER_PROCESS) {
425 #endif
426 #if defined(HAVE_UT_HOST) || defined(HAVE_GETUTXLINE)
427                 if (*put->ut_host)
428                         safestrncpy(hbuf, put->ut_host, 24);
429                 else
430 #endif
431                         safestrncpy(hbuf, put->ut_line, 24);
432 #if defined(HAVE_UT_TYPE) || defined(HAVE_GETUTXLINE)
433                 }
434         else goto fail;
435 #endif
436 #endif /* HAVE_UTMP_H */
437         }
438
439 /*
440  * miscellaneous server commands (testing, etc.)
441  */
442 void misc_server_cmd(char *cmd) {
443         char buf[256];
444
445         serv_puts(cmd);
446         serv_gets(buf);
447         printf("%s\n",buf);
448         if (buf[0]=='1') {
449                 set_keepalives(KA_NO);
450                 while (serv_gets(buf), strcmp(buf,"000")) {
451                         printf("%s\n",buf);
452                         }
453                 set_keepalives(KA_YES);
454                 return;
455                 }
456         if (buf[0]=='4') {
457                 do {
458                         newprompt("> ",buf,255);
459                         serv_puts(buf);
460                         } while(strcmp(buf,"000"));
461                 return;
462                 }
463         }
464
465
466 /*
467  * compute the checksum of a file
468  */
469 int file_checksum(char *filename)
470 {
471         int cksum = 0;
472         int ch;
473         FILE *fp;
474
475         fp = fopen(filename,"r");
476         if (fp == NULL) return(0);
477
478         /* yes, this algorithm may allow cksum to overflow, but that's ok
479          * as long as it overflows consistently, which it will.
480          */
481         while (ch=getc(fp), ch>=0) {
482                 cksum = (cksum + ch);
483                 }
484
485         fclose(fp);
486         return(cksum);
487         }
488
489 /*
490  * nuke a directory and its contents
491  */
492 int nukedir(char *dirname)
493 {
494         DIR *dp;
495         struct dirent *d;
496         char filename[256];
497
498         dp = opendir(dirname);
499         if (dp == NULL) {
500                 return(errno);
501                 }
502
503         while (d = readdir(dp), d != NULL) {
504                 sprintf(filename, "%s/%s", dirname, d->d_name);
505                 unlink(filename);
506                 }
507
508         closedir(dp);
509         return(rmdir(dirname));
510         }