home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / misc / volume3 / pcmail / part03 / cmail.c next >
C/C++ Source or Header  |  1989-02-03  |  4KB  |  150 lines

  1. /*++
  2. /* NAME
  3. /*    cmail 1
  4. /* SUMMARY
  5. /*    report if there are unread messages
  6. /* PROJECT
  7. /*    pc-mail
  8. /* PACKAGE
  9. /*    cmail
  10. /* SYNOPSIS
  11. /*    cmail [-p password]
  12. /* DESCRIPTION
  13. /*    cmail reports if there are unread mail messages in the
  14. /*    pc-mail spool directory.
  15. /*    
  16. /*    If the -p option is present, cmail first invokes the cico
  17. /*    program to contact the mail server host.
  18. /*
  19. /*    Typically cmail is run immediately after system startup,
  20. /*    while you are getting your first cup of coffee.
  21. /*
  22. /*    The program returns a nonzero exit status if it could not 
  23. /*    find any mail.
  24. /* COMMANDS
  25. /*    cico    file transfer program
  26. /*    rmail    processes new mail
  27. /* FILES
  28. /*    Various files in the spool directory
  29. /*
  30. /*    LOGFILE system status messages
  31. /*    n<seqno> mail messages
  32. /*    h<seqno> header line of new mail
  33. /*    o<seqno> header line of old mail
  34. /* DIAGNOSTICS
  35. /*    Error messages in case the environment is not properly set up.
  36. /* BUGS
  37. /*    Invites people to put their mail password into the autoexec file.
  38. /* AUTHOR(S)
  39. /*    W.Z. Venema
  40. /*    Eindhoven University of Technology
  41. /*    Department of Mathematics and Computer Science
  42. /*    Den Dolech 2, P.O. Box 513, 5600 MB Eindhoven, The Netherlands
  43. /* CREATION DATE
  44. /*    Sun Apr  3 19:34:57 MET 1988
  45. /* LAST MODIFICATION
  46. /*    Wed Apr  6 00:18:45 MET 1988
  47. /* VERSION/RELEASE
  48. /*    1.3
  49. /*--*/
  50.  
  51. #include <signal.h>
  52.  
  53. #include "defs.h"
  54. #include "dir.h"
  55. #include "path.h"
  56. #include "status.h"
  57.  
  58. hidden void parse_args();        /* forward declarations */
  59. hidden void usage();
  60. hidden void error();
  61. hidden int newmail();
  62.  
  63. hidden char *password = 0;        /* set by the -p option */
  64.  
  65. main(argc,argv)
  66. int argc;
  67. char **argv;
  68. {
  69.     signal(SIGINT,SIG_IGN);        /* disable ctrl-c */
  70.     parse_args(argc,argv);        /* parse command args */
  71.     if (pathinit())            /* check path info */
  72.     error("cmail: bad MAILDIR environment variable");
  73.     if (password && *password && 
  74.     invokelp(CICO,"-p",password,(char *)0) == E_NOPROG)
  75.     error("cmail: cannot execute the CICO program");
  76.     if (invokelp(RMAIL,(char *)0) == E_NOPROG)
  77.     error("cmail: cannot execute the RMAIL program");
  78.     exit(newmail() == 0);                /* look for new mail */
  79. }
  80.  
  81. /* parse_args - process command-line arguments */
  82.  
  83. hidden void parse_args(argc,argv)
  84. int argc;
  85. char **argv;
  86. {
  87.     while (--argc && *++argv && **argv == '-') {    /* process options */
  88.     switch (*++*argv) {
  89.     case 'p':                    /* call cico first */
  90.         if (--argc == 0)
  91.         usage("missing password");
  92.         password = *++argv;
  93.         break;
  94.     default:                    /* unknown option */
  95.         usage(strcons("invalid option: -%s",*argv));
  96.         break;
  97.     }
  98.     }
  99.  
  100.     /* check for extraneous arguments */
  101.  
  102.     if (argc > 0)
  103.     usage(strcons("unexpected argument: %s",*argv));
  104. }
  105.  
  106. /* scan for new unread mail */
  107.  
  108. hidden int newmail()
  109. {
  110.     register int dd;
  111.     int pfxlen = sizeof(HDRPFX)-1;
  112.     char *f;
  113.     FILE *fp;
  114.     char from[BUFSIZ];
  115.     register int msgcount = 0;
  116.  
  117.     /*
  118.     * Scan the spool directory for unread messages and extract
  119.     * the originator address from the corresponding meta file.
  120.     */
  121.  
  122.     for (dd = opendir(maildir); f = readdir(dd); /* void */) {
  123.     int seqno;
  124.     if (strncmp(f,HDRPFX,pfxlen) == 0 && sscanf(f+pfxlen,"%d",&seqno)) {
  125.         if (fp = fopen(new_meta(seqno),"r")) {
  126.         fgets(from,BUFSIZ,fp);
  127.         printf("You have new mail from %s",from);
  128.         msgcount++;
  129.         fclose(fp);
  130.         }
  131.     }
  132.     }
  133.     closedir(dd);
  134.     return(msgcount);
  135. }
  136.  
  137. hidden void error(str)
  138. char *str;
  139. {
  140.     fprintf(stderr,"%s\n",str);
  141.     exit(1);
  142. }
  143.  
  144. hidden void usage(str)
  145. char *str;
  146. {
  147.     fprintf(stderr,"%s\nusage: cmail [-p password]\n",str);
  148.     exit(1);
  149. }
  150.