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

  1. /* $Header: E:\SRC\UUPC\RN\RCS/ARTIO.C 1.1 1992/11/21 06:14:58 ahd Exp $
  2.  *
  3.  * $Log: ARTIO.C $
  4.  * Revision 1.1  1992/11/21  06:14:58  ahd
  5.  * Initial
  6.  *
  7.  *
  8.  *    Rev 1.0   18 Nov 1990  0:21:48
  9.  * Initial revision.
  10.  * Revision 4.3.2.5  90/03/22  23:04:04  sob
  11.  * Fixes provided by Wayne Davison <drivax!davison>
  12.  *
  13.  * Revision 4.3.2.4  89/11/27  01:29:57  sob
  14.  * Altered NNTP code per ideas suggested by Bela Lubkin
  15.  * <filbo@gorn.santa-cruz.ca.us>
  16.  *
  17.  * Revision 4.3.2.3  89/11/26  22:55:31  sob
  18.  * Add nntpopen() and nntpclose() routines to cut down on size of rrn
  19.  *
  20.  * Revision 4.3.2.2  89/11/08  01:17:12  sob
  21.  * Added changes to insure that this will compile for RN or RRN with no
  22.  * changes to the source code.
  23.  *
  24.  * Revision 4.3.2.1  89/11/06  00:07:25  sob
  25.  * Added RRN support from NNTP 1.5
  26.  *
  27.  * Revision 4.3  85/05/01  11:35:39  lwall
  28.  * Baseline for release with 4.3bsd.
  29.  *
  30.  */
  31.  
  32. #include "EXTERN.h"
  33. #include "common.h"
  34. #include "rn.h"
  35.  
  36. #ifdef SERVER
  37. #include "server.h"
  38. #endif
  39.  
  40. #include "INTERN.h"
  41. #include "artio.h"
  42.  
  43. /*--------------------------------------------------------------------*/
  44. /*                     UUPC/extended header files                     */
  45. /*--------------------------------------------------------------------*/
  46.  
  47. #include "importng.h"
  48.  
  49. void
  50.   artio_init()
  51. {
  52.    ;
  53. }
  54.  
  55. /* open an article, unless it's already open */
  56.  
  57. FILE *
  58.   artopen(artnum)
  59.    ART_NUM artnum;
  60. {
  61.  
  62. #ifdef SERVER
  63.    nntpopen(artnum, ARTICLE);
  64. #else
  65.  
  66. #ifdef msdos
  67.    char tempname[FILENAME_MAX];
  68. #endif
  69.  
  70.    char artname[FILENAME_MAX];         /* filename of current article */
  71.    if (artnum < 1)
  72.       return Nullfp;
  73.    if (openart == artnum)
  74.    {                         /* this article is already open? */
  75.       fseek(artfp, 0L, 0);   /* just get to the beginning */
  76.       return artfp;          /* and say we succeeded */
  77.    }
  78.    if (artfp != Nullfp)
  79.    {                         /* it was somebody else? */
  80.       fclose(artfp);         /* put them out of their misery */
  81.       openart = 0;           /* and remember them no more */
  82.    }
  83.  
  84.    /* produce the name of the article */
  85.  
  86. #ifdef msdos
  87.  
  88.    ImportNewsGroup( artname, ngname, artnum);
  89.  
  90. #else
  91.    sprintf(artname, "%ld", (long) artnum);
  92. #endif
  93.  
  94.    if ((artfp = fopen(artname, "r")) != Nullfp)
  95.    {                         /* if we can open it */
  96.       openart = artnum;      /* remember what we did here */
  97.    }
  98. #endif                       /* SERVER */
  99.  
  100. #ifdef LINKART
  101.    {
  102.       char tmpbuf[FILENAME_MAX];
  103.       char *s;
  104.  
  105.       if (fstat(artfp->_file, &filestat))
  106.          return artfp;
  107.       if (filestat.st_size < (sizeof tmpbuf))
  108.       {
  109.          fgets(tmpbuf, (sizeof tmpbuf), artfp);
  110.          stripcr(tmpbuf);
  111.          if (*tmpbuf == '/')
  112.          {                   /* is a "link" to another article */
  113.             fclose(artfp);
  114.             if (s = index(tmpbuf, '\n'))
  115.                *s = '\0';
  116.             if (!(artfp = fopen(tmpbuf, "r")))
  117.                openart = 0;
  118.             else
  119.             {
  120.                if (*linkartname)
  121.                   free(linkartname);
  122.                linkartname = savestr(tmpbuf);
  123.             }
  124.          }
  125.          else
  126.             fseek(artfp, 0L, 0);        /* get back to the beginning */
  127.       }
  128.    }
  129. #endif
  130.  
  131.    return artfp;             /* and return either fp or NULL */
  132. }
  133.  
  134.  
  135. #ifdef SERVER
  136.  
  137. static char artname [FILENAME_MAX]; /* filename of current article */
  138.  
  139. FILE *
  140.   nntpopen(artnum, function)
  141.    ART_NUM artnum;
  142.    ART_PART function;
  143. {
  144.    char ser_line[256];
  145.  
  146.    if (artnum < 1)
  147.       return Nullfp;
  148.  
  149.    if ((openart == artnum) && (openpart >= function))
  150.    {                         /* this article is already open? */
  151.       fseek(artfp, 0L, 0);   /* just get to the beginning */
  152.       return artfp;          /* and say we succeeded */
  153.    }
  154.    if (artfp != Nullfp)
  155.    {                         /* it was somebody else? */
  156.       fclose(artfp);         /* put them out of their misery */
  157.       nntpclose();
  158.       openart = 0;           /* and remember them no more */
  159.    }
  160.  
  161.    mktempname( artname, "TXT" );
  162.  
  163.    artfp = fopen(artname, "w+");        /* create the temporary article */
  164.    if (artfp == Nullfp)
  165.    {
  166.       UNLINK(artname);
  167.       return Nullfp;
  168.    }
  169.    switch (function)
  170.    {
  171.     case STAT:
  172.       function = HEAD;       /* fall through */
  173.     case HEAD:
  174.       sprintf(ser_line, "HEAD %ld", (long) artnum);
  175.       break;
  176.     case ARTICLE:
  177.       sprintf(ser_line, "ARTICLE %ld", (long) artnum);
  178.       break;
  179.    }
  180.    put_server(ser_line);     /* ask the server for
  181.                               * the article */
  182.    if (get_server(ser_line, sizeof (ser_line)) < 0)
  183.    {
  184.       fprintf(stderr, "rrn: Unexpected close of server socket.\n");
  185.       finalize(1);
  186.    }
  187.    if (*ser_line != CHAR_OK)
  188.    {                         /* and get it's reaction */
  189.       fclose(artfp);
  190.       artfp = Nullfp;
  191.       UNLINK(artname);
  192.       errno = ENOENT;        /* Simulate file-not-found */
  193.       return Nullfp;
  194.    }
  195.  
  196.    for (;;)
  197.    {
  198.       if (get_server(ser_line, sizeof (ser_line)) < 0)
  199.       {
  200.          fprintf(stderr, "rrn: Unexpected close of server socket.\n");
  201.          finalize(1);
  202.       }
  203.       if (ser_line[0] == '.' && ser_line[1] == '\0')
  204.          break;
  205.       fputs((ser_line[0] == '.' ? ser_line + 1 : ser_line), artfp);
  206.       putc('\n', artfp);
  207.    }
  208.    openpart = function;
  209.    if (function == HEAD)
  210.       putc('\n', artfp);     /* req'd blank line after header */
  211.    fseek(artfp, 0L, 0);      /* Then get back to the start */
  212.    openart = artnum;
  213.    return artfp;             /* and return either fp or NULL */
  214. }
  215.  
  216. void
  217.   nntpclose()
  218. {
  219.    UNLINK(artname);
  220. }
  221.  
  222. #endif
  223.