home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1 / Nebula One.iso / Mail / tnextmail / Source / main.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-08-06  |  2.8 KB  |  122 lines

  1. /* $Id: main.c,v 1.3 91/05/25 16:14:52 cap Exp $
  2.  * Trivial NeXTMail - send messages with attachments from non-NeXT machines.
  3.  * by Chris Paris (cap@cs.cmu.edu)
  4.  * This code is in the public domain.
  5.  */
  6.  
  7. #include <stdio.h>
  8. #ifndef USG
  9. #include <sys/param.h>
  10. #endif
  11. #include "config.h"
  12. #include "tnextmail.h"
  13.  
  14. #define TMP_TEMPLATE "/tmp/tnmXXXXXX"
  15.  
  16. char tmpdir_name[MAXPATHLEN];
  17. char rtf_name[MAXPATHLEN];
  18. char subject_buffer[1024];
  19. char *message_subject = subject_buffer;
  20. int rtfpos;            /* printing characters in RTF output */
  21. int fatalerrors;        /* number of fatal errors */
  22.  
  23. extern char *mktemp();
  24.  
  25. main(argc, argv)
  26.      int argc;
  27.      char *argv[];
  28. {
  29.     FILE *rtf_file;
  30.     extern char tar_uu_name[];
  31.  
  32.     parseargs(argc, argv);
  33.     if (message_subject[0] == '\0' && isatty(fileno(stdin)))
  34.     ask_subject();
  35.     
  36.     /* create the temporary directory in which everything goes */
  37.     strcpy(tmpdir_name, TMP_TEMPLATE);
  38.     mktemp(tmpdir_name);
  39.     if (mkdir(tmpdir_name, 0700) != 0) {
  40.     perror(tmpdir_name);
  41.     exit(1);
  42.     }
  43.     /* open the main RTF message file */
  44.     sprintf(rtf_name, "%s/%s", tmpdir_name, RTF_NAME);
  45.     if ((rtf_file = fopen(rtf_name, "w")) == NULL) {
  46.     perror(rtf_name);
  47.     exit(1);
  48.     }
  49.     rtf_start(rtf_file);
  50.     msgtortf(stdin, rtf_file);
  51.     rtf_end(rtf_file);
  52.     fclose(rtf_file);
  53.     
  54.     check_file_access();    /* make sure all attachments are readable */
  55.     if (fatalerrors) {
  56.     cleanup(tmpdir_name, rtf_name, tar_uu_name);
  57.     if (fatalerrors == 1)
  58.         fprintf(stderr, "There was 1 fatal error, my friend.\n");
  59.     else
  60.         fprintf(stderr, "There were %d fatal errors, my friend.\n",
  61.             fatalerrors);
  62.     exit(fatalerrors);
  63.     }
  64.     batch_message(tmpdir_name);
  65.     mail_message(tmpdir_name);
  66.     cleanup(tmpdir_name, rtf_name, tar_uu_name);
  67.     exit(0);
  68. }
  69.  
  70. /* parseargs - look for arguments on the command line */
  71.  
  72. parseargs(argc, argv)
  73.      int argc;
  74.      char *argv[];
  75. {
  76.     extern char **tolist;
  77.     extern char *optarg;
  78.     extern int optind;
  79.     int c, errflg = 0;
  80.  
  81.     while ((c = getopt(argc, argv, "s:")) != EOF)
  82.     switch (c) {
  83.       case 's':
  84.         message_subject = optarg;
  85.         break;
  86.       default:
  87.         errflg++;
  88.     }
  89.     if (optind == argc)        /* no addressee specified */
  90.     errflg++;
  91.     if (errflg) {
  92.     fprintf(stderr, "Usage: %s [-s subject] to-addr ...\n", argv[0]);
  93.     exit(1);
  94.     }
  95.     tolist = &argv[optind];
  96. }
  97.  
  98. /* cleanup - remove the temporary files we may have created */
  99.  
  100. cleanup(tmpdir_name, rtf_name, tar_uu_name)
  101.      char tmpdir_name[], rtf_name[], tar_uu_name[];
  102. {
  103.     unlink(rtf_name);
  104.     unlink(tar_uu_name);
  105.     if (rmdir(tmpdir_name))
  106.     perror(tmpdir_name);
  107. }
  108.  
  109. /* ask_subject - ask the user for a subject */
  110.  
  111. ask_subject()
  112. {
  113.     int len;
  114.     
  115.     printf("Subject: ");
  116.     fflush(stdout);
  117.     fgets(message_subject, sizeof subject_buffer - 1, stdin);
  118.     /* remove the \n */
  119.     if (len = strlen(message_subject))
  120.     message_subject[len - 1] = '\0';
  121. }
  122.