home *** CD-ROM | disk | FTP | other *** search
/ ftp.wwiv.com / ftp.wwiv.com.zip / ftp.wwiv.com / pub / OFFLINE / UQWK18.ZIP / UQWK18.TAR / nntpclient.c < prev    next >
C/C++ Source or Header  |  1994-01-21  |  6KB  |  244 lines

  1.  
  2. /*
  3.  * 
  4.  * This software is Copyright 1991 by Stan Barber. 
  5.  * 
  6.  * Permission is hereby granted to copy, reproduce, redistribute or otherwise
  7.  * use this software as long as: there is no monetary profit gained
  8.  * specifically from the use or reproduction of this software, it is not
  9.  * sold, rented, traded or otherwise marketed, and this copyright notice is
  10.  * included prominently in any copy made. 
  11.  *
  12.  * The author make no claims as to the fitness or correctness of this software
  13.  * for any use whatsoever, and it is provided as is. Any use of this software
  14.  *is at the user's own risk. 
  15.  */
  16.  
  17.  
  18. /*
  19.  * The functions in this file come from rn-1.4.4
  20.  * They have been modified to work with uqwk.
  21.  * They are used to read news articles from a remote
  22.  * news server.
  23.  *
  24.  *  -- Ken Whedbee   3/31/93
  25.  *
  26.  */
  27.  
  28. #include <stdio.h>
  29. #include "nntp.h"
  30.  
  31. #define Nullfp Null(FILE *)
  32. #define MAXFILENAME 128
  33. #define SERVER_FILE "/local/lib/news/server"    /* news server file */
  34.  
  35.  
  36. char ser_line[NNTP_STRLEN];
  37. int openart;
  38.  
  39. extern  char    *getserverbyfile ();
  40. extern  int     server_init ();
  41. extern  void    put_server ();
  42. extern  int     get_server ();
  43. extern  void    close_server ();
  44.  
  45. void connect_nntp();
  46. FILE *getactive_nntp(); 
  47. void group_nntp();
  48. FILE *nntpopen();
  49. int nntp_get();
  50.  
  51.  
  52. void
  53. connect_nntp()
  54. {
  55.     char *server;
  56.     int response;
  57.  
  58.     /* open connection to server if appropriate */
  59.  
  60.     server = getserverbyfile(SERVER_FILE);
  61.     if (server == NULL) {
  62.     fprintf(stderr, "Can't get the name of the news server from %s\n",
  63.         SERVER_FILE);
  64.     fprintf(stderr,
  65.       "Either fix this file, or put NNTPSERVER in your environment.\n");
  66.     exit(1);
  67.     }
  68.  
  69.     response = server_init(server);
  70.     if (response < 0) {
  71.     fprintf(stderr,
  72.         "Couldn't connect to %s news server, try again later.\n",
  73.         server);
  74.     exit(1);
  75.     }
  76.  
  77.     if (handle_server_response(response, server) < 0)
  78.     exit(1);
  79.  
  80.     /* This is for INN */
  81.     put_server ("mode reader");
  82.     nntp_get (ser_line, sizeof(ser_line));
  83.  
  84. }
  85.  
  86.  
  87.  
  88. /* open active file, etc. */
  89.  
  90. FILE
  91. *getactive_nntp()
  92. {
  93.     char active_name[MAXFILENAME];
  94.     register FILE *actfp;
  95.  
  96.     /* open the active file */
  97.  
  98.     put_server("LIST");        /* tell server we want the active file */
  99.     nntp_get(ser_line, sizeof(ser_line));
  100.     if (*ser_line != CHAR_OK) {        /* and then see if that's ok */
  101.     fprintf(stdout, "Can't get active file from server: \n%s\n", ser_line);
  102.     exit(1);
  103.     }
  104.                                         /* make a temporary name */
  105.     sprintf(active_name,"/tmp/rrnact.%d",getpid());
  106.  
  107.     actfp = fopen(active_name, "w+");    /* and get ready */
  108.  
  109.     if (actfp == (FILE *)NULL) {
  110.     printf("Cant open %s\n",active_name), fflush(stdout);
  111.     exit(1);
  112.     }
  113.  
  114.     while (1) {
  115.     if (nntp_get(ser_line, sizeof(ser_line)) < 0) {
  116.         printf("Can't get active file from server\n");
  117.         exit(1);
  118.     }
  119.     if (ser_line[0] == '.')        /* while there's another line */
  120.         break;            /* get it and write it to */
  121.     fputs(ser_line, actfp);
  122.     putc('\n', actfp);
  123.     }
  124.  
  125.     if (ferror(actfp)) {
  126.     printf("Error writing to active file %s.\n", active_name), fflush(stdout);
  127.     exit(1);
  128.     }
  129.     if (fseek(actfp,0L,0) == -1) {    /* just get to the beginning */
  130.     printf("Error seeking in active file.\n"), fflush(stdout);
  131.     exit(1);
  132.     }
  133.  
  134.     return actfp;             /* return active file ptr */
  135. }
  136.  
  137.  
  138. void
  139. group_nntp(ngname)    /* select newsgroup to read from */
  140. char *ngname;
  141. {
  142.  
  143.     sprintf(ser_line, "GROUP %s", ngname);
  144.     put_server(ser_line);
  145.     if (nntp_get(ser_line, sizeof(ser_line)) < 0) {
  146.     fprintf(stderr, "\nrrn: Unexpected close of server socket.\n");
  147.     exit(1);
  148.     }
  149.     if (*ser_line != CHAR_OK) {
  150.     if (atoi(ser_line) != ERR_NOGROUP){
  151.         fprintf(stderr, "\nrrn: server response to GROUP %s:\n%s\n",
  152.             ngname, ser_line);
  153.         exit(1);
  154.     }
  155.     }
  156.  
  157. }
  158.  
  159.  
  160.  
  161.  
  162. /**
  163.  **  example usage:
  164.  **
  165.  **  artfp = nntpopen(artnum,GET_ARTICLE);
  166.  **
  167.  **/
  168.  
  169. FILE 
  170. *nntpopen(artnum,function)
  171. int artnum;
  172. int function;
  173. {
  174.     char artname[MAXFILENAME];        /* filename of current article */
  175.     FILE *artfp;
  176.  
  177.     if (artnum < 1)
  178.     return (FILE *)NULL;
  179.  
  180.     sprintf(artname,"/tmp/rrn%ld.%ld", (long) artnum, getpid());
  181.     artfp = fopen(artname, "w+");    /* create the temporary article */
  182.     if (artfp == (FILE *)NULL) {
  183.     unlink(artname);
  184.     return (FILE *)NULL;
  185.     }
  186.     switch (function){
  187.         case GET_STATUS:
  188.         function = GET_HEADER;    /* fall through */
  189.         case GET_HEADER:
  190.         sprintf(ser_line, "HEAD %ld", (long)artnum);
  191.         break;
  192.         case GET_ARTICLE:
  193.         sprintf(ser_line, "ARTICLE %ld", (long)artnum);
  194.         break;
  195.     }        
  196.     put_server(ser_line);        /* ask the server for the article */
  197.     if (nntp_get(ser_line, sizeof(ser_line)) < 0) {
  198.     fprintf(stderr, "\nrrn: Unexpected close of server socket.\n");
  199.     exit(1);
  200.     }
  201.     if (*ser_line == CHAR_FATAL) {    /* Fatal error */
  202.         fprintf(stderr,"\nrrn: %s\n",ser_line);
  203.         exit(1);
  204.     }
  205.     if (*ser_line != CHAR_OK) {        /* and get it's reaction */
  206.     fclose(artfp);
  207.     artfp = (FILE *)NULL;
  208.     unlink(artname);
  209.         return (FILE *)NULL;
  210.     }
  211.     for (;;) {
  212.         if (nntp_get(ser_line, sizeof(ser_line)) < 0) {
  213.         fprintf(stderr, "\nrrn: Unexpected close of server socket.\n");
  214.         exit(1);
  215.     }
  216.     if (ser_line[0] == '.' && ser_line[1] == '\0')
  217.         break;
  218.     fputs((ser_line[0] == '.' ? ser_line + 1 : ser_line), artfp);
  219.     putc('\n', artfp);
  220.     }
  221.     if (function == GET_HEADER)
  222.      putc('\n', artfp); /* req'd blank line after header */
  223.     fseek(artfp, 0L, 0);        /* Then get back to the start */
  224.     openart = artnum;
  225. /*  printf("got article %ld\n",(long) artnum); */
  226.  
  227.     return artfp;            /* and return either fp or NULL */
  228. }
  229.  
  230.  
  231.  
  232.  
  233. int 
  234. nntp_get(buf, len)
  235. char *buf;
  236. int  len;
  237. {
  238.      int n;
  239.  
  240.      n = get_server(buf, len);
  241.  
  242.      return n;
  243. }
  244.