home *** CD-ROM | disk | FTP | other *** search
/ Beijing Paradise BBS Backup / PARADISE.ISO / software / BBSDOORW / UUPC11XT.ZIP / RN / RNMAIL.C < prev    next >
Encoding:
C/C++ Source or Header  |  1992-11-21  |  6.1 KB  |  285 lines

  1. /*
  2.  * This routine implements rnmail, the rn function that replies to
  3.  * an article.
  4.  *
  5.  * We expect a command like like:
  6.  *
  7.  * <rndir>/rnmail -h %h
  8.  *
  9.  *
  10.  * (<rndir> is the directory that holds the rn utilities (e.g. d:/lib/uupc/news/rn)
  11.  *  %h      is the name of the header file (which might contain the article)
  12.  * )
  13.  *
  14.  */
  15.  
  16. #include <stdio.h>
  17. #include <io.h>
  18. #include <conio.h>
  19. #include <stdlib.h>
  20. #include <process.h>
  21. #include <errno.h>
  22. #include <string.h>
  23.  
  24. #ifdef __TURBOC__
  25. #include <dir.h>
  26. #else
  27. #define MAXDIR FILENAME_MAX
  28. #endif
  29.  
  30. #include <time.h>
  31.  
  32. #include "version.h"
  33.  
  34. #define FALSE 0
  35. #define FROMLINE "From: "
  36. #define DATELINE "Date: "
  37. #define REPLYTOLINE "Reply-To: "
  38.  
  39. #define MAIL_IDENT "\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01"
  40.  
  41. extern char *tempdir;
  42. extern char *domain;
  43. extern char *mailbox;
  44. extern char *replyto;
  45. extern char *E_filesent;
  46. extern char *E_homedir;
  47. extern char *E_editor;
  48. extern char *E_signature;
  49.  
  50.  
  51.  
  52. char *fail_reasons[] = {
  53.    "Arg list too long",
  54.    "Invalid modeflag",
  55.    "Program not found",
  56.    "File not a program",
  57.    "Not enough memory",
  58.    "Unknown reason"
  59. };
  60.  
  61.  
  62. char *getval(char *nam, char *def);
  63. void hostinit(int);
  64.  
  65.  
  66. int spawn_fail(int code)
  67. {
  68.  
  69.    switch (code)
  70.    {
  71.       case E2BIG:return 0;
  72.     case EINVAL:
  73.       return 1;
  74.     case ENOENT:
  75.       return 2;
  76.     case ENOEXEC:
  77.       return 3;
  78.     case ENOMEM:
  79.       return 4;
  80.     default:
  81.       return 5;
  82.    }
  83. }
  84.  
  85.  
  86. int
  87.   main(argc, argv)
  88.    int argc;
  89.    char **argv;
  90. {
  91.  
  92.    char temp_reply[MAXDIR];  /* File name of temp
  93.                               * file to send to rmail */
  94.  
  95.    /* (also used to hold the name of the editor */
  96.    char *editor;
  97.    char *default_editor;
  98.  
  99.    int i;
  100.    int j;
  101.  
  102.    char t;
  103.  
  104.    FILE *f_edit;
  105.    FILE *f_reply;
  106.    FILE *f_filesent;
  107.    char buff[BUFSIZ];
  108.  
  109.    struct tm *now;
  110.    long tnow;
  111.    char *months[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
  112.  
  113.    hostinit(FALSE);
  114.  
  115.    banner(argv);
  116.    printf("rnmail for rn %s\n", RN_VERSION);
  117.  
  118.    if (argc < 3)
  119.    {
  120.       printf("\nInvalid invocation of rnmail\n");
  121.       return 1;
  122.    }
  123.  
  124.  
  125.    t = 'E';
  126.    while (t != 'S')
  127.    {
  128.       if (t == 'E')
  129.       {
  130.          if (E_editor != NULL)
  131.             default_editor = E_editor;
  132.          else
  133.             default_editor = "vi";
  134.          strcpy(temp_reply, getval("VISUAL", getval("EDITOR", default_editor)));
  135.          editor = strchr(temp_reply, ' ');
  136.          if (editor != NULL)
  137.          {
  138.             editor[0] = '\0';/* get rid of that damn
  139.                               * %s */
  140.          }
  141.          editor = temp_reply;
  142.          j = spawnlp(P_WAIT, editor, editor, argv[2], NULL);
  143.          if (j == -1)
  144.          {
  145.             printf("Unable to spawn %s (%s)\n", editor,
  146.                    fail_reasons[spawn_fail(errno)]);
  147.             return 1;
  148.          }
  149.       }
  150.       else if (t == 'L')
  151.       {
  152.       }
  153.       else if (t == 'A')
  154.       {
  155.          exit(1);
  156.       }
  157.  
  158.       printf("\nAbort, Send, List, Edit? ");
  159.       t = toupper(getche());
  160.       printf("\n");
  161.    }
  162.  
  163. /* OK.  Time to ship it.  Append the From:, Date:, and, if necessary,
  164.    the Reply-To: lines.
  165. */
  166.    strcpy(temp_reply, tempdir);
  167.    strcat(temp_reply, "/rnmail.tmp");
  168.  
  169.    f_reply = fopen(temp_reply, "wb");
  170.    strcpy(buff, FROMLINE);
  171.    strcat(buff, mailbox);
  172.    strcat(buff, "@");
  173.    strcat(buff, domain);
  174.    fprintf(f_reply, "%s\n", buff);
  175.  
  176.    tnow = time(NULL);
  177.    now = gmtime(&tnow);
  178.    fprintf(f_reply, "%s%d %s %d %2.2d:%2.2d:%2.2d GMT\n", DATELINE, now->tm_mday,
  179.            months[now->tm_mon], now->tm_year,
  180.            now->tm_hour, now->tm_min,
  181.            now->tm_sec);
  182.  
  183.    if (replyto)
  184.    {
  185.       fprintf(f_reply, "%s%s\n", REPLYTOLINE, replyto);
  186.    }
  187.  
  188.    f_edit = fopen(argv[2], "rb");
  189.  
  190.    while ((i = fread(buff, sizeof (char), sizeof (char), f_edit)) != 0)
  191.    {
  192.       fwrite(buff, sizeof (char), i, f_reply);
  193.    }
  194.    fclose(f_edit);
  195.  
  196.    if (E_signature != NULL)
  197.    {
  198.       t = ' ';
  199.       while ((t != 'Y') && (t != 'N'))
  200.       {
  201.          printf("\nAppend signature? (yn) ");
  202.          t = toupper(getche());
  203.       }
  204.       printf("\n");
  205.       if (t == 'Y')
  206.       {
  207.          f_edit = fopen(E_signature, "rt");
  208.          if (f_edit != NULL)
  209.          {
  210.             fprintf(f_reply, "--\n");
  211.             while ((i = fread(buff, sizeof (char), sizeof (char), f_edit)) != 0)
  212.             {
  213.                fwrite(buff, sizeof (char), i, f_reply);
  214.             }
  215.             fclose(f_edit);
  216.          }
  217.       }
  218.    }
  219.    fclose(f_reply);
  220.  
  221.  
  222.    j = spawnlp(P_WAIT, "rmail", "rmail", "-t", "-x", "1", "-f", temp_reply, NULL);
  223.  
  224. /* If the user is saving their mail, append this one to FileSent */
  225.    if (j == 0)
  226.    {
  227.       if (E_filesent)
  228.       {
  229.          /* Use buff to build name (we are tight on space) */
  230.          /* Append the home directory unless the user specified the root */
  231.          if (((E_filesent[0] == '/') || (E_filesent[0] == '\\')) ||
  232.                ((E_filesent[1] == ':') &&
  233.                 ((E_filesent[2] == '/') || (E_filesent[2] == '\\'))))
  234.          {
  235.             strcpy(buff, E_filesent);
  236.          }
  237.          else
  238.          {
  239.             strcpy(buff, E_homedir);
  240.             strcat(buff, "/");
  241.             strcat(buff, E_filesent);
  242.          }
  243.  
  244.          f_filesent = fopen(buff, "ab");
  245.          if (f_filesent != NULL)
  246.          {
  247.             fprintf(f_filesent, "%s\n", MAIL_IDENT);
  248.             f_reply = fopen(temp_reply, "rb");
  249.             while ((i = fread(buff, sizeof (char), sizeof (char), f_reply)) != 0)
  250.             {
  251.                fwrite(buff, sizeof (char), i, f_filesent);
  252.             }
  253.          }
  254.          fclose(f_filesent);
  255.          fclose(f_reply);
  256.       }
  257.    }
  258.    else
  259.    {
  260.       if (j == -1)
  261.       {
  262.          printf("Unable to spawn %s (%s)\n", "rmail",
  263.                 fail_reasons[spawn_fail(errno)]);
  264.  
  265.       }
  266.    }
  267.  
  268.    unlink(temp_reply);
  269.  
  270.    return 0;
  271. }
  272.  
  273.  
  274.  
  275. char *
  276.   getval(nam, def)
  277.    char *nam, *def;
  278. {
  279.    char *val;
  280.  
  281.    if ((val = getenv(nam)) == NULL || !*val)
  282.       val = def;
  283.    return val;
  284. }
  285.