home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume27 / mthreads / part01 / nntpclient.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-11-20  |  3.6 KB  |  171 lines

  1. /* $Id: nntpclient.c,v 3.0 1991/11/22 04:12:21 davison Trn $
  2. */
  3. /* The authors make no claims as to the fitness or correctness of this software
  4.  * for any use whatsoever, and it is provided as is. Any use of this software
  5.  * is at the user's own risk. 
  6.  */
  7.  
  8. #include "EXTERN.h"
  9. #include "common.h"
  10.  
  11. #ifdef USE_NNTP
  12.  
  13. #include "INTERN.h"
  14. #include "nntpclient.h"
  15.  
  16. #define CANTPOST    \
  17.     "NOTE:  This machine does not have permission to post articles.\n"
  18. #define CANTUSE        \
  19.     "This machine does not have permission to use the %s news server.\n"
  20.  
  21. int
  22. nntp_connect()
  23. {
  24.     char *server, filebuf[128];
  25.     int response;
  26.  
  27.     if ((server = getenv("NNTPSERVER")) == Nullch)
  28.     server = SERVER_NAME;
  29.     if (server[0] == '/') {
  30.     register FILE *fp;
  31.     if ((fp = fopen(server, "r")) != Nullfp) {
  32.         server = Nullch;
  33.         while (fgets(filebuf, sizeof filebuf, fp) != Nullch) {
  34.         if (*filebuf == '\n' || *filebuf == '#')
  35.             continue;
  36.         if ((server = index(filebuf, '\n')) != Nullch)
  37.             *server = '\0';
  38.         server = filebuf;
  39.         break;
  40.         }
  41.         fclose(fp);
  42.     } else
  43.         server = Nullch;
  44.     if (server == Nullch) {
  45.         sprintf(ser_line, "\
  46. Couldn't get name of news server from %s\n\
  47. Either fix this file, or put NNTPSERVER in your environment.\n", SERVER_NAME);
  48.         report_error(ser_line);
  49.         return 0;
  50.     }
  51.     }
  52.  
  53.     switch (response = server_init(server)) {
  54.     case NNTP_GOODBYE_VAL:
  55.     if (atoi(ser_line) == response) {
  56.         char tmpbuf[LBUFLEN];
  57.         sprintf(tmpbuf,"News server %s unavailable: %s\n",server,&ser_line[4]);
  58.         report_error(tmpbuf);
  59.         return 0;
  60.     }
  61.     case -1:
  62.     sprintf(ser_line,"News server %s unavailable, try again later.\n",server);
  63.     report_error(ser_line);
  64.     return 0;
  65.     case NNTP_ACCESS_VAL:
  66.     sprintf(ser_line,CANTUSE,server);
  67.     report_error(ser_line);
  68.     return 0;
  69.     case NNTP_NOPOSTOK_VAL:
  70.     advise(CANTPOST);
  71.     /* FALL THROUGH */
  72.     case NNTP_POSTOK_VAL:
  73.     break;
  74.     default:
  75.     sprintf(ser_line,"Unknown response code %d from %s.\n", response, server);
  76.     report_error(ser_line);
  77.     return 0;
  78.     }
  79.     return 1;
  80. }
  81.  
  82. void
  83. nntp_command(buf)
  84. char *buf;
  85. {
  86. #if defined(DEBUG) && defined(FLUSH)
  87.     if (debug & DEB_NNTP)
  88.     printf(">%s\n", buf) FLUSH;
  89. #endif
  90.     fprintf(ser_wr_fp, "%s\r\n", buf);
  91.     fflush(ser_wr_fp);
  92. }
  93.  
  94. char
  95. nntp_check(strict)
  96. bool_int strict;
  97. {
  98.     int n;
  99.  
  100. #ifdef HAS_SIGHOLD
  101.     sighold(SIGINT);
  102. #endif
  103.     n = (fgets(ser_line, sizeof ser_line, ser_rd_fp) == NULL)? -1 : 0;
  104. #ifdef HAS_SIGHOLD
  105.     sigrelse(SIGINT);
  106. #endif
  107.     if (n < 0)
  108. #ifdef fatal_error
  109.     fatal_error("\nUnexpected close of server socket.\n");
  110. #else
  111.     return NNTP_CLASS_FATAL;
  112. #endif
  113.     n = strlen(ser_line);
  114.     if (n >= 2 && ser_line[n-1] == '\n' && ser_line[n-2] == '\r')
  115.     ser_line[n-2] = '\0';
  116. #if defined(DEBUG) && defined(FLUSH)
  117.     if (debug & DEB_NNTP)
  118.     printf("<%s\n", ser_line) FLUSH;
  119. #endif
  120. #ifdef fatal_error
  121.     if (strict && *ser_line == NNTP_CLASS_FATAL) {    /* Fatal error */
  122.     char tmpbuf[LBUFLEN];
  123.     sprintf(tmpbuf,"\n%s\n",ser_line);
  124.     fatal_error(tmpbuf);
  125.     }
  126. #endif
  127.     return *ser_line;
  128. }
  129.  
  130. int
  131. nntp_gets(buf, len)
  132. char *buf;
  133. int  len;
  134. {
  135.     int n;
  136.  
  137. #ifdef HAS_SIGHOLD
  138.     sighold(SIGINT);
  139. #endif
  140.     n = (fgets(buf, len, ser_rd_fp) == NULL)? -1 : 0;
  141. #ifdef HAS_SIGHOLD
  142.     sigrelse(SIGINT);
  143. #endif
  144.     if (n < 0)
  145. #ifdef fatal_error
  146.     fatal_error("\nUnexpected close of server socket.\n");
  147. #else
  148.     return -1;
  149. #endif
  150.     n = strlen(buf);
  151.     if (n >= 2 && buf[n-1] == '\n' && buf[n-2] == '\r')
  152.     buf[n-2] = '\0';
  153.     return 0;
  154. }
  155.  
  156. void
  157. nntp_close()
  158. {
  159.     if (ser_wr_fp != NULL && ser_rd_fp != NULL) {
  160.     nntp_command("QUIT");
  161.     fclose(ser_wr_fp);
  162.     ser_wr_fp = NULL;
  163.  
  164.     nntp_check(FALSE);
  165.     fclose(ser_rd_fp);
  166.     ser_rd_fp = NULL;
  167.     }
  168. }
  169.  
  170. #endif /* USE_NNTP */
  171.