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