home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 35 Internet / 35-Internet.zip / snews-20.zip / POST.C < prev    next >
C/C++ Source or Header  |  1992-08-02  |  11KB  |  368 lines

  1. /*
  2.     SNEWS 2.0
  3.  
  4.     snews - posting stuff
  5.  
  6.  
  7.     Copyright (C) 1991  John McCombs, Christchurch, NEW ZEALAND
  8.                         john@ahuriri.gen.nz
  9.                         PO Box 2708, Christchurch, NEW ZEALAND
  10.  
  11.     This program is free software; you can redistribute it and/or modify
  12.     it under the terms of the GNU General Public License, version 1, as
  13.     published by the Free Software Foundation.
  14.  
  15.     This program is distributed in the hope that it will be useful,
  16.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  17.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  18.     GNU General Public License for more details.
  19.  
  20.     See the file COPYING, which contains a copy of the GNU General
  21.     Public License.
  22.  
  23.  */
  24.  
  25.  
  26.  
  27. #include "defs.h"
  28. #include "snews.h"
  29.  
  30. #include <io.h>
  31. #include <time.h>
  32.  
  33.  
  34.  
  35.  
  36. /*------------------------------- post an article --------------------------*/
  37. void post(TEXT *tx, char *gx)
  38. {
  39.     /*
  40.      *  Ask for the data we are not given in the arguments,
  41.      *  let him edit the article, then post it
  42.      */
  43.  
  44.     FILE *article, *sig;
  45.     LINE *ln;
  46.     char buf[MAXLINE];
  47.     char fn[256], newsgroups[256], subject[256];
  48.     char sig_fn[80];
  49.     char author[80], msg_id[256];
  50.     int  ch;
  51.  
  52.     strcpy(fn, my_stuff.news_dir);
  53.     strcat(fn, "article");
  54.     unlink(fn);
  55.     strcpy(newsgroups, gx);
  56.  
  57.     strcpy(author, "");
  58.     strcpy(msg_id, "");
  59.  
  60.     if ((article = fopen(fn, "w+")) != NULL) {
  61.  
  62.         if (strlen(newsgroups) == 0) {
  63.  
  64.             /* if no groups ask for them */
  65.             lmessage("Newsgroups? ");
  66.             gets(newsgroups);
  67.  
  68.         } else {
  69.  
  70.             /* there is a group ask if he wants to change */
  71.             sprintf(buf, "Group: %s, post to *this* group (y/n)?", newsgroups);
  72.             message(buf);
  73.             while (((ch = getch()) != 'y') && (ch != 'n'));
  74.  
  75.             if (ch == 'n') {
  76.                 lmessage("Newsgroups? ");
  77.                 gets(newsgroups);
  78.             }
  79.  
  80.         }
  81.  
  82.         /* check for a valid newsgroups line */
  83.         if (newsgroups_valid(newsgroups)) {
  84.  
  85.             /* add the quoted message */
  86.             /* are we quoting */
  87.             if (tx != NULL) {
  88.  
  89.                 if (strlen(tx->subject) == 0) {
  90.                     lmessage("Subject? ");
  91.                     gets(subject);
  92.                 }
  93.                 else
  94.                     if ( strnicmp(tx->subject, "Re: ", 4) != 0 )
  95.                         sprintf(subject, "Re: %s", tx->subject);
  96.                     else
  97.                         strcpy(subject, tx->subject);
  98.  
  99.                 message("Quote article (y/n)? ");
  100.                 while (((ch = getch()) != 'y') && (ch != 'n'));
  101.  
  102.                 if (ch == 'y') {
  103.  
  104.                     get_his_stuff(tx, author, msg_id);
  105.                     fprintf(article, "%s writes in article %s:\n", author, msg_id);
  106.                     ln = tx->start;
  107.                     while (ln != NULL) {
  108.                         fprintf(article, "> %s", ln->data);
  109.                         ln = ln->next;
  110.                     }
  111.                 }
  112.             }
  113.             else {
  114.                 lmessage("Subject? ");
  115.                 gets(subject);
  116.             }
  117.  
  118.             /* append the signature if there is one */
  119.             strcpy(sig_fn, my_stuff.home);
  120.             strcat(sig_fn, my_stuff.signature);
  121.             if ((sig = fopen(sig_fn, "rt")) != NULL) {
  122.                 while (fgets(buf, 79, sig) != NULL)
  123.                     fputs(buf, article);
  124.                 fclose(sig);
  125.             }
  126.  
  127.  
  128.             fclose(article);
  129.             sprintf(buf, my_stuff.editor, fn);
  130.             system(buf);
  131.             article = fopen(fn, "rt");
  132.  
  133.             sprintf(buf, "Post this article to %s (y/n)? ", newsgroups);
  134.             message(buf);
  135.             while (((ch = getch()) != 'y') && (ch != 'n'));
  136.             gotoxy(1,PAGE_SIZE);
  137.  
  138.             if (ch == 'y')
  139.                 post_it(article, newsgroups, subject, "world", msg_id);
  140.  
  141.         }
  142.  
  143.         fclose(article);
  144.         /* unlink(fn); /* keep it around like rn */
  145.  
  146.     } else {
  147.         message("*** couldn't open temp article file - press any key ***");
  148.         getch();
  149.     }
  150.  
  151. }
  152.  
  153.  
  154.  
  155. /*--------------------------- post an article ---------------------------*/
  156. void post_it(FILE *article, char *newsgroups, char *subject, char *dist,
  157.                   char *msg_id)
  158. {
  159.     /*
  160.      *  Post an article.  The article is passed as an open file,
  161.      *  with the other header data.  The header is added to the file
  162.      *  and the article sent.
  163.      */
  164.  
  165.     FILE   *tmp, *local, *log;
  166.     char   buf[256], *p;
  167.     char   ng[256], timestr[64];
  168.     char   d_name[20], x_name[20];
  169.     char   short_d_name[20], short_x_name[20];
  170.     char   remotegroups[256];
  171.     int    ct, seq;
  172.     time_t t;
  173.     struct tm *gmt, *tmnow;
  174.     long   where;
  175.     ACTIVE *gp;
  176.  
  177.     static char *dow[] = {
  178.         "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
  179.     };
  180.     static char *mth[] = {
  181.         "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep",
  182.         "Oct", "Nov", "Dec"
  183.     };
  184.  
  185.     ct = 0;
  186.  
  187.     seq = post_sequence();
  188.     sprintf(d_name, "D.%s%04x", my_stuff.my_site, seq);
  189.     sprintf(x_name, "X.%s%04x", my_stuff.my_site, seq);
  190.  
  191.     sprintf(short_d_name, "D.%.3s", my_stuff.my_site);
  192.     sprintf(short_x_name, "X.%.3s", my_stuff.my_site);
  193.  
  194.     /* count the lines */
  195.     rewind(article);
  196.     while (fgets(buf, 255, article) != NULL)
  197.         ct++;
  198.  
  199.     /*
  200.      *  Make the final article file.  This posting stuff is a horrible
  201.      *  kludge (blush).  If you get a around to fixing this, please
  202.      *  post me a copy :)
  203.      */
  204.     if ((tmp = fopen(short_d_name, "w+b")) != NULL) {
  205.  
  206.         remotegroups[0] = 0;
  207.         strcpy(ng, newsgroups);
  208.         p = strtok(ng, " ,");
  209.         while (p != NULL) {
  210.             if (!is_local_group(p)) {
  211.                 if (remotegroups[0])
  212.                     strcat(remotegroups, ",");
  213.                 strcat(remotegroups, p);
  214.             }
  215.             p = strtok(NULL, " ,");
  216.         }
  217.  
  218.         fprintf(tmp, "Path: %s!%s\n", my_stuff.my_site, my_stuff.user);
  219.         fprintf(tmp, "From: %s@%s (%s)\n", my_stuff.user, my_stuff.my_domain,
  220.             my_stuff.my_name);
  221.         fprintf(tmp, "Newsgroups: %s\n", newsgroups);
  222.         fprintf(tmp, "Subject: %s\n", subject);
  223.         fprintf(tmp, "Distribution: %s\n", remotegroups[0] ? dist : "local");
  224.  
  225.         fprintf(tmp, "Message-ID: <%ldsnx@%s>\n", time(&t), my_stuff.my_domain);
  226.  
  227.         if (strlen(msg_id) > 0)
  228.             fprintf(tmp, "References: %s\n", msg_id);
  229.  
  230.         time(&t);
  231.         gmt = gmtime(&t);
  232.         fprintf(tmp, "Date: %s, %02d %s %02d %02d:%02d:%02d GMT\n",
  233.                 dow[gmt->tm_wday],
  234.                 gmt->tm_mday, mth[gmt->tm_mon], (gmt->tm_year % 100),
  235.                 gmt->tm_hour, gmt->tm_min, gmt->tm_sec);
  236.  
  237.         fprintf(tmp, "Organization: %s\n", my_stuff.my_organisation);
  238.         fprintf(tmp, "Lines: %d\n", ct);
  239.  
  240.         strcpy(buf, my_stuff.news_dir);
  241.         strcat(buf, "headers");
  242.     if ((local = fopen(buf, "r")) != NULL) {
  243.             while (fgets(buf, 255, local) != NULL)
  244.                 fputs(buf, tmp);
  245.         fclose(local);
  246.     }
  247.  
  248.     fputs("\n", tmp);
  249.  
  250.         /* copy the rest */
  251.         rewind(article);
  252.         while (fgets(buf, 255, article) != NULL) {
  253.             fputs(buf, tmp);
  254.         }
  255.  
  256.         /* ok, now post it locally */
  257.         strcpy(ng, newsgroups);
  258.         p = strtok(ng, " ,");
  259.         while (p != NULL) {
  260.  
  261.             if (fseek(tmp, 0L, SEEK_SET) != 0) {
  262.                 fprintf(stderr, "cannot rewind temp file\n");
  263.                 exit(1);
  264.             }
  265.  
  266.             local = open_out_file(p);
  267.             where = ftell(local);
  268.             while (fgets(buf, 255, tmp) != NULL)  {
  269.                 fputs(buf, local);
  270.             }
  271.             fprintf(local, "\n@@@@END\n");
  272.             fclose(local);
  273.  
  274.             /* save the data in the index file */
  275.             local = open_index_file(p);
  276.             gp = find_news_group(p);
  277.             fprintf(local,"%08ld %08ld %09ld %s\n", where, gp->hi_num, t,
  278.                 subject);
  279.             fclose(local);
  280.  
  281.             p = strtok(NULL, " ,");
  282.         }
  283.  
  284.  
  285.         /* finally log it */
  286.         strcpy(buf, my_stuff.news_dir);
  287.         strcat(buf, "post.log");
  288.         if ((log = flockopen(buf, "at")) != NULL) {
  289.             fprintf(log, "\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01"
  290.                          "\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\n");
  291.         tmnow = localtime(&t);
  292.         strftime(timestr, sizeof(timestr), "%a %b %d %H:%M:%S %z %Y", gmt);
  293.         fprintf(log, "From %s!%s %s\n", my_stuff.my_site, my_stuff.user, 
  294.             timestr);
  295.  
  296.             fseek(tmp, 0L, SEEK_SET);
  297.             while (fgets(buf, 255, tmp) != NULL)  {
  298.                fputs(buf, log);
  299.             }
  300.  
  301.             fclose(log);
  302.         }
  303.  
  304.         fclose(tmp);
  305.  
  306.         /* now the the X... file */
  307.         if (remotegroups[0])
  308.             if ((tmp = fopen(short_x_name, "wb")) != NULL) {
  309.                 fprintf(tmp, "U news %s\n", my_stuff.my_site);
  310.                 fprintf(tmp, "Z\n");
  311.                 fprintf(tmp, "F %s\n", d_name);
  312.                 fprintf(tmp, "I %s\n", d_name);
  313.                 fprintf(tmp, "C rnews\n");
  314.                 fclose(tmp);
  315.  
  316.                 sprintf(buf, "uucp -C %s %s!%s 2>nul", short_d_name, 
  317.             my_stuff.mail_server, d_name);
  318.                 gotoxy(1,PAGE_SIZE); clreol();
  319.                 system(buf);
  320.                 sprintf(buf, "uucp -C %s %s!%s 2>nul", short_x_name, 
  321.             my_stuff.mail_server, x_name);
  322.                 gotoxy(1,PAGE_SIZE); clreol();
  323.                 system(buf);
  324.  
  325.             } else {
  326.                 message("*** couldn't open temp post file - press any key ***");
  327.                 getch();
  328.             }
  329.  
  330.         unlink(short_d_name);
  331.         unlink(short_x_name);
  332.  
  333.     } else {
  334.         message("*** couldn't open temp post file - press any key ***");
  335.         getch();
  336.     }
  337. }
  338.  
  339.  
  340.  
  341. /*------------------------ parse and check newsgroups ---------------------*/
  342. int newsgroups_valid(char *ng)
  343. {
  344.     /*
  345.      *  This routine parses the newsgroup list, and checks them for
  346.      *  validity
  347.      */
  348.  
  349.     char buf[256], msg[128], *p;
  350.  
  351.     strcpy(buf, ng);
  352.  
  353.     if ((p = strtok(buf, ", ")) == NULL)
  354.         return(FALSE);
  355.  
  356.     while (p != NULL) {
  357.         if (!check_valid_post_group(p)) {
  358.             sprintf(msg, "*** can't post to %s - press any key ***", p);
  359.             message(msg);
  360.             getch();
  361.             return(FALSE);
  362.         }
  363.         p = strtok(NULL, ", ");
  364.     }
  365.  
  366.     return(TRUE);
  367. }
  368.