home *** CD-ROM | disk | FTP | other *** search
- /* 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>
- #include <stdlib.h>
- #include <pwd.h>
- 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 ;
- {
- char tmp[1024], tmp1[1024] ;
- char * strptr ;
- if (str[0] == '\0')
- {
- sprintf(tmp1, "Please enter %s : ", prompt) ;
- if (noecho == TRUE)
- {
- strptr = getpass(tmp1) ;
- strcpy(str, strptr) ;
- }
- else
- {
- printf("%s", tmp1) ;
- gets(str) ;
- }
- return ;
- }
- else
- {
- 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) ;
- }
- if (tmp[0] == '\0')
- {
- return ;
- }
- else
- {
- strcpy(str, tmp) ;
- return ;
- }
- }
- }
-
-
- getuname(str)
- char * str ;
- {
- uid_t uid ;
- struct passwd *pwent ;
- uid = getuid() ;
- pwent = getpwuid(uid) ;
- if (pwent == NULL)
- return ;
- strcpy(str, pwent->pw_name) ;
- }
-