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

  1. /*
  2.  * This routine implements postnews, the rn function that actually posts an
  3.  * article.  It is called by Pnews.
  4.  *
  5.  * We expect a command like like:
  6.  *
  7.  * postnews art_file
  8.  *
  9.  *
  10.  *  art_file       is the name of the file which contains the article
  11.  *
  12.  */
  13.  
  14. #include <stdio.h>
  15. #include <io.h>
  16. #include <conio.h>
  17. #include <stdlib.h>
  18. #include <process.h>
  19. #include <string.h>
  20.  
  21. #ifdef __TURBOC__
  22. #include <dir.h>
  23. #else
  24. #define MAXDIR FILENAME_MAX
  25. #endif
  26.  
  27. #include <time.h>
  28.  
  29. /*
  30.  *  The article in art_file is ready to go except for some "minor"
  31.  *  additions.
  32.  *
  33.  */
  34.  
  35.  
  36. #define FALSE 0
  37. #define TRUE 1
  38.  
  39. #define SFILENAME "seqf"
  40.  
  41. #define FROMLINE   "From: "
  42. #define DATELINE   "Date: "
  43. #define PATHLINE   "Path: "
  44. #define MESSAGEID  "Message-ID: "
  45. #define REPLYTO    "Reply-To: "
  46.  
  47. #define NEWSGROUPS "Newsgroups: "
  48. #define SUBJECT    "Subject: "
  49.  
  50. #define MAIL_IDENT "\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01"
  51.  
  52. extern char *tempdir;
  53. extern char *domain;
  54. extern char *mailbox;
  55. extern char *E_signature;
  56. extern char *E_homedir;
  57. extern char *nodename;
  58. extern char *confdir;
  59. extern char *replyto;
  60. extern char *newserver;
  61. extern char *mailserv;
  62. extern char *name;
  63.  
  64.  
  65. int DeliverNews(char *input, char *path);
  66.  
  67.  
  68. int
  69.   main(argc, argv)
  70.    int argc;
  71.    char **argv;
  72. {
  73.  
  74.    char temp_file[MAXDIR];   /* File name of temp
  75.                               * file to send  */
  76.    char author_copy[MAXDIR];
  77.  
  78.    char *ac_env;
  79.  
  80.    int i;
  81.    int j;
  82.  
  83.    int saw_subject;
  84.    int saw_newsgroups;
  85.    int abandon_ship;
  86.    int full_line;
  87.  
  88.    char t;
  89.    char *s;
  90.  
  91.    FILE *seqfile_fp;
  92.    long seq;
  93.  
  94.    FILE *f_edit;
  95.    FILE *f_reply;
  96.    char buff[BUFSIZ];
  97.  
  98.    struct tm *now;
  99.    long tnow;
  100.    char *months[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
  101.  
  102.  
  103.    hostinit(FALSE);
  104.  
  105. /* Get the next sequence number for use in the Messge-ID: */
  106.  
  107.    strcpy(temp_file, confdir);
  108.    strcat(temp_file, "/");
  109.    strcat(temp_file, SFILENAME);
  110.  
  111.    if ((seqfile_fp = fopen(temp_file, "rt")) != NULL)
  112.    {
  113.       fscanf(seqfile_fp, "%ld", &seq);
  114.       fclose(seqfile_fp);
  115.    }
  116.    else
  117.    {
  118.       printf("getseq: can't find %s, faking it (with 1)\n", temp_file);
  119.       seq = 1;
  120.    };
  121.  
  122.  
  123.    strcpy(temp_file, tempdir);
  124.    strcat(temp_file, "/postnews.tmp");
  125.    unlink(temp_file);        /* Might as well get rid
  126.                               * of one (if it exists) */
  127.  
  128.  
  129. /* OK.  Time to ship it.  Massage the headers.
  130.    First, emit the ones we must add.
  131. */
  132.  
  133.  
  134.    f_reply = fopen(temp_file, "wb");
  135.  
  136.    strcpy(buff, PATHLINE);
  137.    strcat(buff, nodename);
  138.    strcat(buff, "!");
  139.    strcat(buff, mailbox);
  140.    fprintf(f_reply, "%s\n", buff);
  141.  
  142.    strcpy(buff, FROMLINE);
  143.    strcat(buff, mailbox);
  144.    strcat(buff, "@");
  145.    strcat(buff, domain);
  146.    strcat(buff, " (");
  147.    strcat(buff, name);
  148.    strcat(buff, ")");
  149.    fprintf(f_reply, "%s\n", buff);
  150.  
  151.    tnow = time(NULL);
  152.    now = gmtime(&tnow);
  153.    fprintf(f_reply, "%s%d %s %d %2.2d:%2.2d:%2.2d GMT\n", DATELINE, now->tm_mday,
  154.            months[now->tm_mon], now->tm_year,
  155.            now->tm_hour, now->tm_min,
  156.            now->tm_sec);
  157.  
  158.  
  159.  
  160.    strcpy(buff, MESSAGEID);
  161.    sprintf(&(buff[strlen(buff)]), "<%ld@%s>", seq, nodename);
  162.    fprintf(f_reply, "%s\n", buff);
  163.  
  164.    if (replyto != NULL)
  165.    {
  166.       strcpy(buff, REPLYTO);
  167.       strcat(buff, replyto);
  168.       fprintf(f_reply, "%s\n", buff);
  169.    }
  170.  
  171. /* Now open the file the user edited and copy the headers (except
  172.    for the empty ones.  Also, insure that there is a subject and that
  173.    the Newsgroups: header is there.  I should also check that the
  174.    Newsgroups are valid but ...
  175. */
  176.  
  177.    f_edit = fopen(argv[1], "rb");
  178.  
  179.    saw_subject = FALSE;
  180.    saw_newsgroups = FALSE;
  181.    abandon_ship = FALSE;
  182.  
  183.    while (fgets(buff, sizeof (buff), f_edit))
  184.    {
  185.       i = strlen(buff);
  186.       if (((i == 2) && (buff[0] == '\r') && (buff[1] == '\n')) ||
  187.             ((i == 1) && (buff[0] == '\n')) ||
  188.             (i == 0))
  189.       {
  190.          /* Finished with the headers */
  191.          break;
  192.       }
  193.       full_line = FALSE;
  194.       if ((buff[i - 2] == '\r') && (buff[i - 1] == '\n'))
  195.       {
  196.          /* Repair line if it uses DOS rules */
  197.          buff[i - 2] = '\n';
  198.          buff[i - 1] = '\0';
  199.          i--;
  200.       }
  201.  
  202.       if (buff[i - 1] == '\n')
  203.       {
  204.          full_line = TRUE;
  205.          buff[--i] = '\0';
  206.       }
  207.       else
  208.       {
  209.          printf("Encountered a header line longer than %d characters\n", BUFSIZ);
  210.          printf("That capability is not implemented\n");
  211.          printf("Things are going to break real soon now\n");
  212.       }
  213.  
  214.       if (strnicmp(buff, SUBJECT, strlen(SUBJECT)) == 0)
  215.       {
  216.          saw_subject = TRUE;
  217.       }
  218.       else if (strnicmp(buff, NEWSGROUPS, strlen(NEWSGROUPS)) == 0)
  219.       {
  220.          saw_newsgroups = TRUE;
  221.       }
  222.       else
  223.       {
  224.          /* It's a header that we don't care about the format. Just make sure
  225.           * that it is not empty */
  226.          s = strchr(buff, ':');
  227.          if (s == NULL)
  228.          {
  229.             printf("Badly formed header - %.40s%s\n", buff, (strlen(buff) > 40) ? "..." : " ");
  230.             abandon_ship = TRUE;
  231.             break;
  232.          }
  233.          s++;                /* Skip the colon */
  234.          while (*s != '\0')
  235.          {
  236.             if (*s == ' ')
  237.                s++;
  238.             else
  239.                break;
  240.          }
  241.          if (*s == '\0')
  242.          {
  243.             /* Empty header, throw it away */
  244.             continue;
  245.          }
  246.       }
  247.  
  248.       fprintf(f_reply, "%s\n", buff);
  249.    }
  250.  
  251. /* Finished with the headers.  Just copy the rest of the beast */
  252.  
  253.    if (abandon_ship)
  254.    {
  255.       printf("Posting abandoned.  Press any key to continue.");
  256.       getch();
  257.       return 1;
  258.    }
  259.  
  260.    if (!saw_subject || !saw_newsgroups)
  261.    {
  262.       printf("Ill formed article.  You must have a Subject: and a Newsgroups: line.\n");
  263.       printf("Posting abandoned.  Press any key to continue.");
  264.       getch();
  265.       return 1;
  266.    }
  267.  
  268.    fprintf(f_reply, "\n");
  269.  
  270.    while (fgets(buff, sizeof (buff), f_edit))
  271.    {
  272.       i = strlen(buff);
  273.       if ((buff[i - 2] == '\r') && (buff[i - 1] == '\n'))
  274.       {
  275.          /* Repair line if it uses DOS rules */
  276.          buff[i - 2] = '\n';
  277.          buff[i - 1] = '\0';
  278.          i--;
  279.       }
  280.  
  281.       if (buff[i - 1] == '\n')
  282.       {
  283.          full_line = TRUE;
  284.          buff[--i] = '\0';
  285.       }
  286.       fprintf(f_reply, "%s\n", buff);
  287.    }
  288.  
  289.  
  290.  
  291.    fclose(f_edit);
  292.  
  293. /* And finally, worry about the .signature file */
  294.    if (E_signature != NULL)
  295.    {
  296.       f_edit = fopen(E_signature, "rt");
  297.       if (f_edit != NULL)
  298.       {
  299.          fprintf(f_reply, "--\n");
  300.          while ((i = fread(buff, sizeof (char), sizeof (buff), f_edit)) != 0)
  301.          {
  302.             j = fwrite(buff, sizeof (char), i, f_reply);
  303.             if (i != j)
  304.             {
  305.                printf("Postnews:  Error copying %s\n", E_signature);
  306.             }
  307.          }
  308.       }
  309.    }
  310.    fclose(f_edit);
  311.    fclose(f_reply);
  312.  
  313.  
  314. /* OK.  Let's ship it.
  315.  *
  316.  */
  317.  
  318.    DeliverNews(temp_file, (newserver ? newserver : mailserv));
  319.  
  320. /*
  321.  *  OK.  Save a copy if the author so wishes.
  322.  *
  323.  */
  324.    ac_env = getenv("AUTHORCOPY");
  325.    if (ac_env != NULL)
  326.    {
  327.       /* build the file name (in author_copy) */
  328.       author_copy[0] = '\0';
  329.       if ((strlen(ac_env) > 2) && (ac_env[1] == ':'))
  330.       {
  331.          strncpy(author_copy, ac_env, 2);
  332.          author_copy[2] = '\0';
  333.          ac_env += 2;
  334.       }
  335.       if (ac_env[0] == '~')
  336.       {
  337.          ac_env += 1;
  338.          if ((strlen(author_copy) != 0) && (E_homedir[1] == ':'))
  339.          {
  340.             printf("Pnews: Error in AUTHORCOPY -- two directories\n");
  341.             author_copy[0] = '\0';
  342.          }
  343.          strcat(author_copy, E_homedir);
  344.       }
  345.       strcat(author_copy, ac_env);
  346.       f_reply = fopen(author_copy, "at");
  347.       if (f_reply == NULL)
  348.       {
  349.          printf("Unable to open %s\n", author_copy);
  350.       }
  351.       else
  352.       {
  353.          f_edit = fopen(temp_file, "rt");
  354.          if (f_edit == NULL)
  355.          {
  356.             printf("Unable to open %s", temp_file);
  357.          }
  358.          else
  359.          {
  360.             fprintf(f_reply, "%s\n", MAIL_IDENT);
  361.             while ((i = fread(buff, sizeof (char), sizeof (buff), f_edit)) != 0)
  362.             {
  363.                j = fwrite(buff, sizeof (char), i, f_reply);
  364.                if (i != j)
  365.                {
  366.                   printf("Postnews:  Error copying %s\n", temp_file);
  367.                }
  368.             }
  369.  
  370.          }
  371.       }
  372.       fclose(f_edit);
  373.       fclose(f_reply);
  374.    }
  375.  
  376.  
  377.  
  378.    unlink(temp_file);
  379.  
  380.    return 0;
  381. }
  382.