* First cut at Solaris fixes. There may still be some *printf("%s", NULL)
[citadel.git] / citadel / routines.c
index 2d47c6b6d30299de52fdfcd0bd255f4a4ff7aea3..6757a59f0d70e47e861b35e1d0cfadc22c14e752 100644 (file)
@@ -1,4 +1,5 @@
 /* Citadel/UX support routines */
+/* $Id$ */
 
 #include "sysdep.h"
 #include <stdlib.h>
 #include <signal.h>
 #include <dirent.h>
 #include <errno.h>
+#include <time.h>
+#include <limits.h>
+#ifdef HAVE_UTMP_H
+#include <utmp.h>
+#endif
+#ifdef HAVE_UTMPX_H
+#include <utmpx.h>
+#endif
 
+#ifndef HAVE_GETUTLINE
+struct utmp *getutline(struct utmp *ut);
+#endif
 
 #define ROUTINES_C
 
 #include "citadel.h"
-
-char inkey();
-void sttybbs();
-void newprompt();
-void val_user();
-int intprompt();
-void formout();
-void logoff();
-void set_keepalives();
-void strprompt();
-void newprompt();
-void color();
+#include "routines.h"
+#include "commands.h"
+#include "tools.h"
+
+void sttybbs(int cmd);
+void newprompt(char *prompt, char *str, int len);
+void val_user(char *, int);
+void formout(char *name);
+void logoff(int code);
+void set_keepalives(int s);
+void strprompt(char *prompt, char *str, int len);
+void newprompt(char *prompt, char *str, int len);
+void color(int colornum);
 
 #define IFAIDE if(axlevel>=6)
 #define IFNAIDE if (axlevel<6)
@@ -37,12 +50,12 @@ void color();
 extern unsigned userflags;
 extern char *axdefs[7];
 extern char sigcaught;
-extern struct serv_info serv_info;
+extern struct CtdlServInfo serv_info;
 extern char rc_floor_mode;
+extern int rc_ansi_color;
 
-int struncmp(lstr,rstr,len)
-char lstr[],rstr[];
-int len; {
+int struncmp(char *lstr, char *rstr, int len)
+{
        int pos = 0;
        char lc,rc;
        while (pos<len) {
@@ -57,121 +70,22 @@ int len; {
        }
 
 
-/* 
- * check for the presence of a character within a string (returns count)
- */
-int haschar(st,ch)
-char st[];
-int ch; {
-       int a,b;
-       b=0;
-       for (a=0; a<strlen(st); ++a) if (st[a]==ch) ++b;
-       return(b);
-       }
-
-
-/*
- * num_parms()  -  discover number of parameters...
- */
-int num_parms(source)
-char source[]; {
-       int a;
-       int count = 1;
-
-       for (a=0; a<strlen(source); ++a) 
-               if (source[a]=='|') ++count;
-       return(count);
-       }
-
-/*
- * extract()  -  extract a parameter from a series of "|" separated...
- */
-void extract(dest,source,parmnum)
-char dest[];
-char source[];
-int parmnum; {
-       char buf[256];
-       int count = 0;
-       int n;
-
-       n = num_parms(source);
-
-       if (parmnum >= n) {
-               strcpy(dest,"");
-               return;
-               }
-       strcpy(buf,source);
-       if ( (parmnum == 0) && (n == 1) ) {
-               strcpy(dest,buf);
-               return;
-               }
-
-       while (count++ < parmnum) do {
-               strcpy(buf,&buf[1]);
-               } while( (strlen(buf)>0) && (buf[0]!='|') );
-       if (buf[0]=='|') strcpy(buf,&buf[1]);
-       for (count = 0; count<strlen(buf); ++count)
-               if (buf[count] == '|') buf[count] = 0;
-       strcpy(dest,buf);
-       }
 
-/*
- * extract_int()  -  extract an int parm w/o supplying a buffer
- */
-int extract_int(source,parmnum)
-char *source;
-int parmnum; {
-       char buf[256];
-       
-       extract(buf,source,parmnum);
-       return(atoi(buf));
-       }
-
-/*
- * extract_long()  -  extract a long parm w/o supplying a buffer
- */
-long extract_long(source,parmnum)
-char *source;
-int parmnum; {
-       char buf[256];
-       
-       extract(buf,source,parmnum);
-       return(atol(buf));
-       }
-
-void back(spaces) /* Destructive backspace */
-int spaces; {
+void back(int spaces) /* Destructive backspace */
+            {
 int a;
        for (a=1; a<=spaces; ++a) {
                putc(8,stdout); putc(32,stdout); putc(8,stdout);
                }
        }
 
-int yesno() { /* Returns 1 for yes, 0 for no */
-int a;
-       while (1) {
-               a=inkey(); a=tolower(a);
-               if (a=='y') { printf("Yes\n"); return(1); }
-               if (a=='n') { printf("No\n");  return(0); }
-               }
-       }
-
-int yesno_d(d) /* Returns 1 for yes, 0 for no, arg is default value */
-int d; {
-int a;
-       while (1) {
-               a=inkey(); a=tolower(a);
-               if (a==13) a=(d ? 'y' : 'n');
-               if (a=='y') { printf("Yes\n"); return(1); }
-               if (a=='n') { printf("No\n");  return(0); }
-               }
-       }
-
-
-void hit_any_key() {           /* hit any key to continue */
+void hit_any_key(void) {               /* hit any key to continue */
        int a,b;
 
+       color(COLOR_PUSH);
+       color(DIM_RED);
        printf("%s\r",serv_info.serv_moreprompt);
+       color(COLOR_POP);
        sttybbs(0);
        b=inkey();
        for (a=0; a<strlen(serv_info.serv_moreprompt); ++a)
@@ -185,41 +99,74 @@ void hit_any_key() {               /* hit any key to continue */
 /*
  * change a user's access level
  */
-void edituser(userbuf)
-struct usersupp *userbuf; {
-       char who[256];
+void edituser(void)
+{
        char buf[256];
+       char who[256];
+       char pass[256];
+       int flags;
+       int timescalled;
+       int posted;
+       int axlevel;
+       long usernum;
+       time_t lastcall;
+       int userpurge;
 
        newprompt("User name: ",who,25);
-       sprintf(buf,"QUSR %s",who);
+       sprintf(buf,"AGUP %s",who);
        serv_puts(buf);
        serv_gets(buf);
        if (buf[0]!='2') {
                printf("%s\n",&buf[4]);
                return;
                }
-       
-       val_user(who);
+       extract(who, &buf[4], 0);
+       extract(pass, &buf[4], 1);
+       flags = extract_int(&buf[4], 2);
+       timescalled = extract_int(&buf[4], 3);
+       posted = extract_int(&buf[4], 4);
+       axlevel = extract_int(&buf[4], 5);
+       usernum = extract_long(&buf[4], 6);
+       lastcall = extract_long(&buf[4], 7);
+       userpurge = extract_int(&buf[4], 8);
+
+       val_user(who, 0); /* Display registration */
+       strprompt("Password", pass, 19);
+       axlevel = intprompt("Access level", axlevel, 0, 6);
+       timescalled = intprompt("Times called", timescalled, 0, INT_MAX);
+       posted = intprompt("Messages posted", posted, 0, INT_MAX);
+       lastcall = (boolprompt("Set last call to now", 0)?time(NULL):lastcall);
+       userpurge = intprompt("Purge time (in days, 0 for system default",
+                               userpurge, 0, INT_MAX);
+
+       sprintf(buf, "ASUP %s|%s|%d|%d|%d|%d|%ld|%ld|%d",
+               who, pass, flags, timescalled, posted, axlevel, usernum,
+               (long)lastcall, userpurge);
+       serv_puts(buf);
+       serv_gets(buf);
+       if (buf[0]!='2') {
+               printf("%s\n",&buf[4]);
+               }
        }
 
 
-int set_attr(sval,prompt,sbit)
-int sval;
-char *prompt;
-unsigned sbit; {
+int set_attr(int sval, char *prompt, unsigned int sbit)
+{
        int a;
        int temp;
 
        temp = sval;
-       color(3);
-       printf("%45s [", prompt);
-       color(1);
+       color(DIM_WHITE);
+       printf("%45s ", prompt);
+       color(DIM_MAGENTA);
+       printf("[");
+       color(BRIGHT_MAGENTA);
        printf("%3s", ((temp&sbit) ? "Yes":"No"));
-       color(3);
+       color(DIM_MAGENTA);
        printf("]? ");
-       color(2);
+       color(BRIGHT_CYAN);
        a=yesno_d(temp&sbit);
-       color(7);
+       color(DIM_WHITE);
        temp=(temp|sbit);
        if (!a) temp=(temp^sbit);
        return(temp);
@@ -229,8 +176,8 @@ unsigned sbit; {
  * modes are:  0 - .EC command, 1 - .EC for new user,
  *             2 - toggle Xpert mode  3 - toggle floor mode
  */
-void enter_config(mode)
-int mode; {
+void enter_config(int mode)
+{
        int width,height,flags;
        char buf[128];
 
@@ -268,6 +215,10 @@ int mode; {
          flags = set_attr(flags,
                "View rooms by floor",US_FLOORS);
          }
+        if (rc_ansi_color == 3) {
+         flags = set_attr(flags,
+               "Enable color support",US_COLOR);
+         }
         }
 
        if (mode==2) {
@@ -303,9 +254,8 @@ int mode; {
  * getstring()  -  get a line of text from a file
  *                ignores lines beginning with "#"
  */
-int getstring(fp,string)
-FILE *fp;              
-char string[]; {
+int getstring(FILE *fp, char *string)
+{
        int a,c;
        do {
                strcpy(string,"");
@@ -323,9 +273,9 @@ char string[]; {
        return(strlen(string));
        }
 
-int pattern(search,patn)       /* Searches for patn in search string */
-char search[];
-char patn[];
+int pattern(char *search, char *patn)  /* Searches for patn in search string */
+              
+            
 {
        int a,b;
        for (a=0; a<strlen(search); ++a)
@@ -335,8 +285,8 @@ char patn[];
        return(-1);
 }
 
-void interr(errnum)    /* display internal error as defined in errmsgs */
-int errnum; {
+void interr(int errnum)        /* display internal error as defined in errmsgs */
+            {
        printf("*** INTERNAL ERROR %d\n",errnum);
        printf("(Press any key to continue)\n");
        inkey();
@@ -351,9 +301,8 @@ int errnum; {
  * we are probably in the middle of a server operation and the NOOP command
  * would confuse everything.
  */
-int checkpagin(lp,pagin,height)
-int lp;
-int pagin; {
+int checkpagin(int lp, int pagin, int height)
+{
        if (pagin!=1) return(0);
        if (lp>=(height-1)) {
                set_keepalives(KA_NO);
@@ -365,8 +314,7 @@ int pagin; {
        }
 
 
-void strproc(string)
-char string[];
+void strproc(char *string)
 {
        int a;
 
@@ -403,124 +351,12 @@ char string[];
        }
 
 
-int hash(str)
-char str[]; {
-       int h = 0;
-       int i;
-
-       for (i=0; i<strlen(str); ++i) h=h+((i+1)*tolower(str[i]));
-       return(h);
-       }
-
-long finduser(file,name)
-int file;
-char *name; {
-       FILE *fp;
-       int uh,fh;
-       long pp=0L;
-       
-       uh=hash(name);
-       fp=fopen("hashtab","r");
-       while(fread((char *)&fh,sizeof(int),1,fp)>0) {
-               if (uh==fh) {
-                       lseek(file,pp,0);
-                       return(pp);
-                       }
-               pp = pp + (long)sizeof(struct usersupp);
-               }
-       fclose(fp);
-       return(-1L);
-       }
-
-
-int alias(name)                /* process alias and routing info for mail */
-char name[]; {
-       FILE *fp;
-       int a,b;
-       char aaa[300],bbb[300];
-       
-       fp=fopen("network/mail.aliases","r");
-       if (fp==NULL) return(2);
-GNA:   strcpy(aaa,""); strcpy(bbb,"");
-       do {
-               a=getc(fp);
-               if (a==',') a=0;
-               if (a>0) { b=strlen(aaa); aaa[b]=a; aaa[b+1]=0; }
-               } while(a>0);
-       do {
-               a=getc(fp);
-               if (a==10) a=0;
-               if (a>0) { b=strlen(bbb); bbb[b]=a; bbb[b+1]=0; }
-               } while(a>0);
-       if (a<0) {
-               fclose(fp);
-               goto DETYPE;
-               }
-       if (strucmp(name,aaa)) goto GNA;
-       fclose(fp);
-       strcpy(name,bbb);
-       printf("*** Mail is being forwarded to %s\n",name);
-
-DETYPE:        /* determine local or remote type, see citadel.h */
-       for (a=0; a<strlen(name); ++a) if (name[a]=='!') return(M_INTERNET);
-       for (a=0; a<strlen(name); ++a)
-               if (name[a]=='@')
-                       for (b=a; b<strlen(name); ++b)
-                               if (name[b]=='.') return(M_INTERNET);
-       b=0; for (a=0; a<strlen(name); ++a) if (name[a]=='@') ++b;
-       if (b>1) {
-               printf("Too many @'s in address\n");
-               return(M_ERROR);
-               }
-       if (b==1) {
-               for (a=0; a<strlen(name); ++a)
-                       if (name[a]=='@') strcpy(bbb,&name[a+1]);
-               while (bbb[0]==32) strcpy(bbb,&bbb[1]);
-               fp = fopen("network/mail.sysinfo","r");
-               if (fp==NULL) return(M_ERROR);
-GETSN:         do {
-                       a=getstring(fp,aaa);
-                       } while ((a>=0)&&(strucmp(aaa,bbb)));
-               a=getstring(fp,aaa);
-               if (!strncmp(aaa,"use ",4)) {
-                       strcpy(bbb,&aaa[4]);
-                       fseek(fp,0L,0);
-                       goto GETSN;
-                       }
-               fclose(fp);
-               if (!strncmp(aaa,"uum",3)) {
-                       strcpy(bbb,name);
-                       for (a=0; a<strlen(bbb); ++a) {
-                               if (bbb[a]=='@') bbb[a]=0;
-                               if (bbb[a]==' ') bbb[a]='_';
-                               }
-                       while(bbb[strlen(bbb)-1]=='_') bbb[strlen(bbb)-1]=0;
-                       sprintf(name,&aaa[4],bbb);
-                       return(M_INTERNET);
-                       }
-               if (!strncmp(aaa,"bin",3)) {
-                       strcpy(aaa,name); strcpy(bbb,name);
-                       while (aaa[strlen(aaa)-1]!='@') aaa[strlen(aaa)-1]=0;
-                       aaa[strlen(aaa)-1]=0;
-                       while (aaa[strlen(aaa)-1]==' ') aaa[strlen(aaa)-1]=0;
-                       while (bbb[0]!='@') strcpy(bbb,&bbb[1]);
-                       strcpy(bbb,&bbb[1]);
-                       while (bbb[0]==' ') strcpy(bbb,&bbb[1]);
-                       sprintf(name,"%s @%s",aaa,bbb);
-                       return(M_BINARY);
-                       }
-               return(M_ERROR);
-               }
-       return(M_LOCAL);
-       }
-
-
-#ifdef NO_STRERROR
+#ifndef HAVE_STRERROR
 /*
  * replacement strerror() for systems that don't have it
  */
-char *strerror(e)
-int e; {
+char *strerror(int e)
+{
        static char buf[32];
 
        sprintf(buf,"errno = %d",e);
@@ -529,9 +365,8 @@ int e; {
 #endif
 
 
-void progress(curr,cmax)
-long curr;
-long cmax; {
+void progress(long int curr, long int cmax)
+{
        static long dots_printed;
        long a;
 
@@ -560,8 +395,9 @@ long cmax; {
  * NOT the same locate_host() in locate_host.c.  This one just does a
  * 'who am i' to try to discover where the user is...
  */
-void locate_host(hbuf)
-char hbuf[]; {
+void locate_host(char *hbuf)
+{
+#ifndef HAVE_UTMP_H
        char buf[256];
        FILE *who;
        int a,b;
@@ -594,6 +430,46 @@ char hbuf[]; {
 
        if (strlen(buf)==0) strcpy(hbuf,serv_info.serv_fqdn);
        else strncpy(hbuf,buf,24);
+#else
+       char *tty = ttyname(0);
+#ifdef HAVE_GETUTXLINE
+       struct utmpx ut, *put;
+#else
+       struct utmp ut, *put;
+#endif
+
+       if (tty == NULL) {
+           fail:
+               safestrncpy(hbuf, serv_info.serv_fqdn, 24);
+               return;
+               }
+
+       if (strncmp(tty, "/dev/", 5))
+               goto fail;
+
+       safestrncpy(ut.ut_line, &tty[5], sizeof ut.ut_line);
+
+#ifdef HAVE_GETUTXLINE /* Solaris uses this */
+       if ((put = getutxline(&ut)) == NULL)
+#else
+       if ((put = getutline(&ut)) == NULL)
+#endif
+               goto fail;
+
+#if defined(HAVE_UT_TYPE) || defined(HAVE_GETUTXLINE)
+       if (put->ut_type == USER_PROCESS) {
+#endif
+#if defined(HAVE_UT_HOST) || defined(HAVE_GETUTXLINE)
+               if (*put->ut_host)
+                       safestrncpy(hbuf, put->ut_host, 24);
+               else
+#endif
+                       safestrncpy(hbuf, put->ut_line, 24);
+#if defined(HAVE_UT_TYPE) || defined(HAVE_GETUTXLINE)
+               }
+       else goto fail;
+#endif
+#endif /* HAVE_UTMP_H */
        }
 
 /*
@@ -606,9 +482,11 @@ void misc_server_cmd(char *cmd) {
        serv_gets(buf);
        printf("%s\n",buf);
        if (buf[0]=='1') {
+               set_keepalives(KA_NO);
                while (serv_gets(buf), strcmp(buf,"000")) {
                        printf("%s\n",buf);
                        }
+               set_keepalives(KA_YES);
                return;
                }
        if (buf[0]=='4') {
@@ -624,8 +502,8 @@ void misc_server_cmd(char *cmd) {
 /*
  * compute the checksum of a file
  */
-int file_checksum(filename)
-char *filename; {
+int file_checksum(char *filename)
+{
        int cksum = 0;
        int ch;
        FILE *fp;
@@ -647,8 +525,8 @@ char *filename; {
 /*
  * nuke a directory and its contents
  */
-int nukedir(dirname)
-char *dirname; {
+int nukedir(char *dirname)
+{
        DIR *dp;
        struct dirent *d;
        char filename[256];