h25617
s 00031/00014/00215
d D 1.4 95/07/06 20:13:16 tim 4 3
c user the getpass command to get the users password as it is secure. modified getstr so is the field is secure and there is an existing string a - will remove the data
e
s 00001/00000/00228
d D 1.3 95/07/06 14:49:16 tim 3 2
c forgot to include pwd.h, now fixed
e
s 00013/00000/00215
d D 1.2 95/07/06 14:12:37 tim 2 1
c getuname moved from talktopopconfig to talktopopconf so it is available to
c talktopop
e
s 00215/00000/00000
d D 1.1 95/07/06 14:00:28 tim 1 0
c 
e
u
U
f e 0
t
T
I 1
/* this is a library of routines to read / write config files
   and deal with passwords etc. */
/* the file format is
   username
   password
   pop server name
   number of output file to start with
   delete flag
   extract from start or end of mail file 
   verbose flag
   max size of email to retrieve
   max number of emails to retrieve
   pop server name (e.g. pop-3)
   pop protocol name (e.g. tcp)
   */


#include "talktopop.h"
#include <stdio.h> 
#include <string.h> 
I 4
#include <stdlib.h>
E 4
I 3
#include <pwd.h>
E 3
extern char popserver [] ;
extern char popname [] ;
extern char protname [] ;
extern char uname [] ;
extern char passwd [] ;
extern int filestartno ;
extern int dodel ;
extern int direction ;
extern int verbose ;
extern int maxsize ;
extern int maxemail ;
extern int hnset, unset, pwset ;

confinit()
{
	strcpy(popserver, "") ;
	strcpy(popname, POPNAME) ;
	strcpy(protname, PROTNAME) ;
	strcpy(uname, "") ;
	strcpy(passwd, "") ;
	filestartno = 0 ;
	dodel - FALSE ;
	direction = TRUE ;
	verbose = FALSE ;
	maxsize = MAXSIZE ;
	maxemail = MAXCOUNT ;
	hnset = unset = pwset = FALSE ;
}

readconf()
{
	FILE * fd ;
	char  tmp[1024] ;
	int ret ;
	if ((fd = fopen(POPCONF, "r")) == NULL)
		return ;
	if (getline(fd, uname) == TRUE) /* username */
		unset = TRUE ;
	if (getline(fd, passwd) == TRUE) /* password */
		pwset = TRUE ;
	if (getline(fd, popserver) == TRUE) /* server name */
		hnset = TRUE ;
	if (getline(fd, tmp) == TRUE) /* output file start number */
		filestartno = atoi(tmp) ;
	if (getline(fd, tmp) == TRUE) /* delete flag */
	{
		if (tmp[0] == 'T')
			dodel = TRUE ;
		else
			dodel = FALSE ;
	}
	if (getline(fd, tmp) == TRUE) /* start from begining or end of mail file */
	{
		if (tmp[0] == 'T')
			direction = TRUE ;
		else
			direction = FALSE ;
	}
	if (getline(fd, tmp) == TRUE) /* be verbose or not */
	{
		if (tmp[0] == 'T')
			verbose = TRUE ;
		else
			verbose = FALSE ;
	}
	if (getline(fd, tmp) == TRUE) /* max email size */
		maxsize = atoi(tmp) ;
	if (getline(fd, tmp) == TRUE) /* max emails to retrieve */
		maxemail = atoi(tmp) ;
	getline(fd, popname) ;
	getline(fd, protname) ;
	fclose(fd) ;
}

writeconf()
{
	FILE * fd ;
	if ((fd = fopen(POPCONF, "w")) == NULL)
	{
		printf("ERROR, Cant open %s to write pop output file\n") ;
		return ;
	}
	fprintf(fd, "%s\n", uname) ;
	fprintf(fd, "%s\n", passwd) ;
	fprintf(fd, "%s\n", popserver) ;
	fprintf(fd, "%d\n", filestartno) ;
	if (dodel == TRUE)
		fprintf(fd, "TRUE\n") ;
	else
		fprintf(fd, "FALSE\n") ;
	if (direction == TRUE)
		fprintf(fd, "TRUE\n");
	else
		fprintf(fd, "FALSE\n") ;
	if (verbose == TRUE)
		fprintf(fd, "TRUE\n") ;
	else
		fprintf(fd, "FALSE\n") ;
	fprintf(fd, "%d\n", maxsize) ;
	fprintf(fd, "%d\n", maxemail) ;
	fprintf(fd, "%s\n", popname) ;
	fprintf(fd, "%s\n", protname) ;
	fclose(fd) ;
}
getyn(prompt, current)
char * prompt ;
int * current ;
{
	char tmp[1024] ;
	printf("Please enter %s [%s]\n", prompt, (*current == TRUE) ? "TRUE" : "FALSE") ;
	gets(tmp) ;
	if (tmp[0] == '\0')
	{
		return ;
	}
	else
	{
		*current = ((tmp[0] == 't') || (tmp[0] == 'T')) ? TRUE : FALSE ;
		return ;
	}
}
	
getline(fd, str)
FILE * fd ;
char * str ;
{
	int len ;
	char * ret ;
	char tmp[1024] ;
	ret = fgets(tmp, 1024, fd) ;
	if (ret == NULL)
		return(FALSE) ;
	/* remove the newline */
	len = strlen(tmp) ;
	if (tmp[len-1] == '\n')
		tmp[len-1] = '\0' ;
	if (len -1 <= 0)
		return(FALSE) ;
	/* we have a string with data in it */
	strcpy(str, tmp) ;
	return(TRUE) ;
}

getno(number, prompt)
int * number ;
char * prompt;
{
	char tmp[1024] ;
	printf("Please enter %s [%d]\n", prompt, *number) ;
	gets(tmp) ;
	if (tmp[0] == '\0')
	{
		return ;
	}
	else
	{
		*number = atoi(tmp) ;
		return ;
	}
}

getstr(str, prompt, noecho)
int noecho ;
char * str, * prompt ;
{
D 4
	char tmp[1024] ;
	if (noecho == TRUE)
E 4
I 4
	char tmp[1024], tmp1[1024] ;
	char * strptr ;
	if (str[0] == '\0')
E 4
	{
D 4
		/* turn of terminal echoing */
		printf("Please enter %s\n", prompt) ;
		gets(str) ;
		/* reset the terminal state */
E 4
I 4
		sprintf(tmp1, "Please enter %s : ", prompt) ;
		if (noecho == TRUE)
		{
			strptr = getpass(tmp1) ;
			strcpy(str, strptr) ;
		}
		else
		{
			printf("%s", tmp1) ;
			gets(str) ;
		}
E 4
		return ;
	}
D 4
	if (str[0] == 0)
	{
		printf("Please enter %s\n", prompt) ;
		gets(str) ;
		return ;
	}
E 4
	else
	{
D 4
		printf("Please enter %s [%s]\n", prompt, str) ;
		gets(tmp) ;
E 4
I 4
		if (noecho == TRUE)
		{
			sprintf(tmp1, "Please enter %s [exiting value not pronted for security reasons], - to empty this field : ", prompt) ;
			strptr = getpass(tmp1) ;
			strcpy(tmp, strptr) ;
			if (tmp[0] == '-')
			{
				str[0] = '\0' ;
				return ;
			}
		}
		else
		{
			printf("Please enter %s [%s] : ", prompt, str) ;
			gets(tmp) ;
		}
E 4
		if (tmp[0] == '\0')
		{
			return ;
		}
		else
		{
			strcpy(str, tmp) ;
			return ;
		}
	}
}
I 2


getuname(str)
char * str ;
{
	uid_t uid ;
	struct passwd *pwent ;
	uid = getuid() ;
	pwent = getpwuid(uid) ;
	if (pwent == NULL)
		return ;
	strcpy(str, pwent->pw_name) ;
}
E 2
E 1
