home *** CD-ROM | disk | FTP | other *** search
/ PDA Software Library / pdasoftwarelib.iso / PSION / COMMS / PSIONMAI / PMFULLSO / SUNMAIL / TALKTOP2.C < prev    next >
Encoding:
C/C++ Source or Header  |  1995-07-06  |  4.8 KB  |  247 lines

  1. /* this is a library of routines to read / write config files
  2.    and deal with passwords etc. */
  3. /* the file format is
  4.    username
  5.    password
  6.    pop server name
  7.    number of output file to start with
  8.    delete flag
  9.    extract from start or end of mail file 
  10.    verbose flag
  11.    max size of email to retrieve
  12.    max number of emails to retrieve
  13.    pop server name (e.g. pop-3)
  14.    pop protocol name (e.g. tcp)
  15.    */
  16.  
  17.  
  18. #include "talktopop.h"
  19. #include <stdio.h> 
  20. #include <string.h> 
  21. #include <stdlib.h>
  22. #include <pwd.h>
  23. extern char popserver [] ;
  24. extern char popname [] ;
  25. extern char protname [] ;
  26. extern char uname [] ;
  27. extern char passwd [] ;
  28. extern int filestartno ;
  29. extern int dodel ;
  30. extern int direction ;
  31. extern int verbose ;
  32. extern int maxsize ;
  33. extern int maxemail ;
  34. extern int hnset, unset, pwset ;
  35.  
  36. confinit()
  37. {
  38.     strcpy(popserver, "") ;
  39.     strcpy(popname, POPNAME) ;
  40.     strcpy(protname, PROTNAME) ;
  41.     strcpy(uname, "") ;
  42.     strcpy(passwd, "") ;
  43.     filestartno = 0 ;
  44.     dodel - FALSE ;
  45.     direction = TRUE ;
  46.     verbose = FALSE ;
  47.     maxsize = MAXSIZE ;
  48.     maxemail = MAXCOUNT ;
  49.     hnset = unset = pwset = FALSE ;
  50. }
  51.  
  52. readconf()
  53. {
  54.     FILE * fd ;
  55.     char  tmp[1024] ;
  56.     int ret ;
  57.     if ((fd = fopen(POPCONF, "r")) == NULL)
  58.         return ;
  59.     if (getline(fd, uname) == TRUE) /* username */
  60.         unset = TRUE ;
  61.     if (getline(fd, passwd) == TRUE) /* password */
  62.         pwset = TRUE ;
  63.     if (getline(fd, popserver) == TRUE) /* server name */
  64.         hnset = TRUE ;
  65.     if (getline(fd, tmp) == TRUE) /* output file start number */
  66.         filestartno = atoi(tmp) ;
  67.     if (getline(fd, tmp) == TRUE) /* delete flag */
  68.     {
  69.         if (tmp[0] == 'T')
  70.             dodel = TRUE ;
  71.         else
  72.             dodel = FALSE ;
  73.     }
  74.     if (getline(fd, tmp) == TRUE) /* start from begining or end of mail file */
  75.     {
  76.         if (tmp[0] == 'T')
  77.             direction = TRUE ;
  78.         else
  79.             direction = FALSE ;
  80.     }
  81.     if (getline(fd, tmp) == TRUE) /* be verbose or not */
  82.     {
  83.         if (tmp[0] == 'T')
  84.             verbose = TRUE ;
  85.         else
  86.             verbose = FALSE ;
  87.     }
  88.     if (getline(fd, tmp) == TRUE) /* max email size */
  89.         maxsize = atoi(tmp) ;
  90.     if (getline(fd, tmp) == TRUE) /* max emails to retrieve */
  91.         maxemail = atoi(tmp) ;
  92.     getline(fd, popname) ;
  93.     getline(fd, protname) ;
  94.     fclose(fd) ;
  95. }
  96.  
  97. writeconf()
  98. {
  99.     FILE * fd ;
  100.     if ((fd = fopen(POPCONF, "w")) == NULL)
  101.     {
  102.         printf("ERROR, Cant open %s to write pop output file\n") ;
  103.         return ;
  104.     }
  105.     fprintf(fd, "%s\n", uname) ;
  106.     fprintf(fd, "%s\n", passwd) ;
  107.     fprintf(fd, "%s\n", popserver) ;
  108.     fprintf(fd, "%d\n", filestartno) ;
  109.     if (dodel == TRUE)
  110.         fprintf(fd, "TRUE\n") ;
  111.     else
  112.         fprintf(fd, "FALSE\n") ;
  113.     if (direction == TRUE)
  114.         fprintf(fd, "TRUE\n");
  115.     else
  116.         fprintf(fd, "FALSE\n") ;
  117.     if (verbose == TRUE)
  118.         fprintf(fd, "TRUE\n") ;
  119.     else
  120.         fprintf(fd, "FALSE\n") ;
  121.     fprintf(fd, "%d\n", maxsize) ;
  122.     fprintf(fd, "%d\n", maxemail) ;
  123.     fprintf(fd, "%s\n", popname) ;
  124.     fprintf(fd, "%s\n", protname) ;
  125.     fclose(fd) ;
  126. }
  127. getyn(prompt, current)
  128. char * prompt ;
  129. int * current ;
  130. {
  131.     char tmp[1024] ;
  132.     printf("Please enter %s [%s]\n", prompt, (*current == TRUE) ? "TRUE" : "FALSE") ;
  133.     gets(tmp) ;
  134.     if (tmp[0] == '\0')
  135.     {
  136.         return ;
  137.     }
  138.     else
  139.     {
  140.         *current = ((tmp[0] == 't') || (tmp[0] == 'T')) ? TRUE : FALSE ;
  141.         return ;
  142.     }
  143. }
  144.     
  145. getline(fd, str)
  146. FILE * fd ;
  147. char * str ;
  148. {
  149.     int len ;
  150.     char * ret ;
  151.     char tmp[1024] ;
  152.     ret = fgets(tmp, 1024, fd) ;
  153.     if (ret == NULL)
  154.         return(FALSE) ;
  155.     /* remove the newline */
  156.     len = strlen(tmp) ;
  157.     if (tmp[len-1] == '\n')
  158.         tmp[len-1] = '\0' ;
  159.     if (len -1 <= 0)
  160.         return(FALSE) ;
  161.     /* we have a string with data in it */
  162.     strcpy(str, tmp) ;
  163.     return(TRUE) ;
  164. }
  165.  
  166. getno(number, prompt)
  167. int * number ;
  168. char * prompt;
  169. {
  170.     char tmp[1024] ;
  171.     printf("Please enter %s [%d]\n", prompt, *number) ;
  172.     gets(tmp) ;
  173.     if (tmp[0] == '\0')
  174.     {
  175.         return ;
  176.     }
  177.     else
  178.     {
  179.         *number = atoi(tmp) ;
  180.         return ;
  181.     }
  182. }
  183.  
  184. getstr(str, prompt, noecho)
  185. int noecho ;
  186. char * str, * prompt ;
  187. {
  188.     char tmp[1024], tmp1[1024] ;
  189.     char * strptr ;
  190.     if (str[0] == '\0')
  191.     {
  192.         sprintf(tmp1, "Please enter %s : ", prompt) ;
  193.         if (noecho == TRUE)
  194.         {
  195.             strptr = getpass(tmp1) ;
  196.             strcpy(str, strptr) ;
  197.         }
  198.         else
  199.         {
  200.             printf("%s", tmp1) ;
  201.             gets(str) ;
  202.         }
  203.         return ;
  204.     }
  205.     else
  206.     {
  207.         if (noecho == TRUE)
  208.         {
  209.             sprintf(tmp1, "Please enter %s [exiting value not pronted for security reasons], - to empty this field : ", prompt) ;
  210.             strptr = getpass(tmp1) ;
  211.             strcpy(tmp, strptr) ;
  212.             if (tmp[0] == '-')
  213.             {
  214.                 str[0] = '\0' ;
  215.                 return ;
  216.             }
  217.         }
  218.         else
  219.         {
  220.             printf("Please enter %s [%s] : ", prompt, str) ;
  221.             gets(tmp) ;
  222.         }
  223.         if (tmp[0] == '\0')
  224.         {
  225.             return ;
  226.         }
  227.         else
  228.         {
  229.             strcpy(str, tmp) ;
  230.             return ;
  231.         }
  232.     }
  233. }
  234.  
  235.  
  236. getuname(str)
  237. char * str ;
  238. {
  239.     uid_t uid ;
  240.     struct passwd *pwent ;
  241.     uid = getuid() ;
  242.     pwent = getpwuid(uid) ;
  243.     if (pwent == NULL)
  244.         return ;
  245.     strcpy(str, pwent->pw_name) ;
  246. }
  247.