#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include "ctype.h"

#if !defined(_lint)
static char rcsid[] OPTIONAL = "$Id: cleanup.c,v 1.6 1996/12/23 20:37:36 root Exp $";
#endif

int main (int argc, char *argv[]);

static char UDefbak[] =  NOSDIR"/spool/users.bak";
static char UDefaults[] =  NOSDIR"/spool/users.dat";

#define NULLFILE ((FILE *) 0)
#define NULLCHAR ((char *) 0)

int
main(argc,argv)
int argc;
char *argv[];
{
FILE *Ufile, *tfile;
char buf[256], *cp, *cp2, *cp3;
int nope;
int didone;
char c;
char *name, *bbs, *realname, *date, *more, *options;
char outgoing[256], tempstr[2];

	/* Save old defaults file to backup */
	unlink(UDefbak);
	if(rename(UDefaults,UDefbak))
		return 0;
        if ((tfile = fopen(UDefbak,"r")) != NULLFILE) {
        	if ((Ufile = fopen(UDefaults,"w")) == NULLFILE)	  {
        		fclose (tfile);
        		return 0;
        	}
       	        while(fgets(buf,sizeof(buf),tfile) != NULLCHAR)	{
			printf ("< %s", buf);
			cp = strchr (buf, ' ');
			if (!cp)
				continue;
			*cp++ = 0;
			bbs = cp;
			cp2 = name = buf;
			nope = 0;
			while (*cp2)	{
				if (!isalnum (*cp2) || !isascii(*cp2))	{
					nope = 1;
					break;
				}
				cp2++;
			}
			if (nope)
				continue;

			if (strlen (name) == 1)
				continue;

			/* write user name */
			strcpy (outgoing, name);
			strcat (outgoing, " ");

			if (*bbs != '@')
				continue;

			cp2 = strchr (bbs, ' ');
			if (!cp2)
				continue;
			*cp2++ = 0;
			realname = cp2;

			/* look for a second '@' */
			cp3 = strchr (&bbs[1], '@');
			if (cp3)
				*cp3 = 0;

			/* look for a '.' (haddress) */
			cp3 = strchr (bbs, '.');
			if (cp3)
				*cp3 = 0;

			nope = 0;
			cp2 = &bbs[1];
			if (*cp2 == '-')
				cp2[1] = 0;
			else	{
				while (*cp2)	{
					if (!isalnum (*cp2))	{
						nope = 1;
						break;
					}
					cp2++;
				}
				if (nope)
					continue;
			}

			/* write home bbs */
			strcat (outgoing, bbs);
			strcat (outgoing, " ");

			if (*realname != '(')
				continue;

			cp = strchr (realname, ')');
			if (!cp)
				continue;

			cp++;
			c = *cp;
			if (c != ' ')
				continue;
			*cp++ = 0;
			date = cp;

			cp2 = realname;
			nope = 0;
			while (*cp2)	{
				if (!isascii(*cp2))	{
					nope = 1;
					break;
				}
				cp2++;
			}
			if (nope)
				continue;

			/* write realname */
			strcat (outgoing, realname);
			strcat (outgoing, " ");

			cp2 = strchr (date, ' ');
			if (!cp2)
				continue;
			*cp2++ = 0;
			more = cp2;

			nope = 0;
			cp2 = date;
			while (*cp2)	{
				if (!isdigit (*cp2))	{
					nope = 1;
					break;
				}
				cp2++;
			}
			if (nope)
				continue;

			/* write timestamp */
			strcat (outgoing, date);
			strcat (outgoing, " ");

			cp = strchr (more, ' ');
			if (!cp)
				continue;
			*cp++ = 0;
			options = cp;

			nope = 0;
			cp2 = &more[1];
			while (*cp2)	{
				if (!isdigit (*cp2))	{
					nope = 1;
					break;
				}
				cp2++;
			}
			if (nope)
				continue;

			/* write more lines */
			strcat (outgoing, more);
			strcat (outgoing, " ");

			didone = 0;
			cp = options;
			tempstr[1] = 0;
			nope = 0;
			while (*cp)	{
				if (*cp != ' ')	{
					if (*cp == 'C' && didone)
						strcat (outgoing, " ");
					*tempstr = *cp;
					if (*cp != '\n' && !isupper (*cp))	{
						nope = 1;
						break;
					}
					strcat (outgoing, tempstr);
					didone = 1;
				}
				cp++;
			}
			if (nope)
				continue;
			fputs (outgoing, Ufile);
			printf ("> %s", outgoing);
               	}
               	fclose(tfile);
               	fclose(Ufile);
        } else
	        rename(UDefbak,UDefaults);
	return 0;
}


