1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327
|
/* nntpclient.c
*/
/* This software is copyrighted as detailed in the LICENSE file. */
#include "EXTERN.h"
#include "common.h"
#ifdef SUPPORT_NNTP
#include "nntpinit.h"
#include "INTERN.h"
#include "nntpclient.h"
time_t last_command_diff;
char* savestr _((char*));
#ifndef USE_DEBUGGING_MALLOC
char* safemalloc _((MEM_SIZE));
#endif
#ifdef NNTP_HANDLE_TIMEOUT
int nntp_handle_timeout _((void));
#endif
int nntp_handle_nested_lists _((void));
#ifdef NNTP_HANDLE_AUTH_ERR
int nntp_handle_auth_err _((void));
#endif
int
nntp_connect(machine,verbose)
char* machine;
bool_int verbose;
{
int response;
if (nntplink.rd_fp)
return 1;
if (nntplink.flags & NNTP_FORCE_AUTH_NEEDED)
nntplink.flags |= NNTP_FORCE_AUTH_NOW;
nntplink.flags |= NNTP_NEW_CMD_OK;
#if 0
try_to_connect:
#endif
if (verbose) {
printf("Connecting to %s...",machine);
fflush(stdout);
}
switch (response = server_init(machine)) {
case NNTP_GOODBYE_VAL:
if (atoi(ser_line) == response) {
char tmpbuf[LBUFLEN];
if (verbose)
printf("failed: %s\n",&ser_line[4]);
else {
sprintf(tmpbuf,"News server \"%s\" is unavailable: %s\n",
machine,&ser_line[4]);
nntp_init_error(tmpbuf);
}
response = 0;
break;
}
case -1:
if (verbose)
printf("failed.\n");
else {
sprintf(ser_line,"News server \"%s\" is unavailable.\n",machine);
nntp_init_error(ser_line);
}
response = 0;
break;
case NNTP_ACCESS_VAL:
if (verbose)
printf("access denied.\n");
else {
sprintf(ser_line,
"This machine does not have permission to use the %s news server.\n\n",
machine);
nntp_init_error(ser_line);
}
response = -1;
break;
case NNTP_NOPOSTOK_VAL:
if (verbose)
printf("Done (but no posting).\n\n");
response = 1;
break;
case NNTP_POSTOK_VAL:
if (verbose)
printf("Done.\n") FLUSH;
response = 1;
break;
default:
if (verbose)
printf("unknown response: %d.\n",response);
else {
sprintf(ser_line,"\nUnknown response code %d from %s.\n",
response,machine);
nntp_init_error(ser_line);
}
response = 0;
break;
}
#if 0
if (!response) {
if (handle_no_connect() > 0)
goto try_to_connect;
}
#endif
return response;
}
char*
nntp_servername(name)
char* name;
{
FILE* fp;
if (FILE_REF(name) && (fp = fopen(name, "r")) != NULL) {
while (fgets(ser_line, sizeof ser_line, fp) != NULL) {
if (*ser_line == '\n' || *ser_line == '#')
continue;
if ((name = index(ser_line, '\n')) != NULL)
*name = '\0';
name = ser_line;
break;
}
fclose(fp);
}
return name;
}
int
nntp_command(bp)
char* bp;
{
time_t now;
#if defined(DEBUG) && defined(FLUSH)
if (debug & DEB_NNTP)
printf(">%s\n", bp) FLUSH;
#endif
#if defined(NNTP_HANDLE_TIMEOUT) || defined(NNTP_HANDLE_AUTH_ERR)
strcpy(last_command, bp);
# ifdef NNTP_HANDLE_TIMEOUT
if (!nntplink.rd_fp)
return nntp_handle_timeout();
# endif
# ifdef NNTP_HANDLE_AUTH_ERR
if (nntplink.flags & NNTP_FORCE_AUTH_NOW) {
nntplink.flags &= ~NNTP_FORCE_AUTH_NOW;
return nntp_handle_auth_err();
}
# endif
#endif
if (!(nntplink.flags & NNTP_NEW_CMD_OK)) {
int ret = nntp_handle_nested_lists();
if (ret <= 0)
return ret;
}
if (fprintf(nntplink.wr_fp, "%s\r\n", bp) < 0
|| fflush(nntplink.wr_fp) < 0)
#ifdef NNTP_HANDLE_TIMEOUT
return nntp_handle_timeout();
#else
return -2;
#endif
now = time((time_t*)NULL);
last_command_diff = now - nntplink.last_command;
nntplink.last_command = now;
return 1;
}
int
nntp_check()
{
int ret;
int len = 0;
read_it:
#ifdef HAS_SIGHOLD
sighold(SIGINT);
#endif
errno = 0;
ret = (fgets(ser_line, sizeof ser_line, nntplink.rd_fp) == NULL)? -2 : 0;
#ifdef HAS_SIGHOLD
sigrelse(SIGINT);
#endif
if (ret < 0) {
if (errno == EINTR)
goto read_it;
strcpy(ser_line, "503 Server closed connection.");
}
#ifdef NNTP_HANDLE_TIMEOUT
if (len == 0 && atoi(ser_line) == NNTP_TMPERR_VAL
&& nntp_allow_timeout && last_command_diff > 60) {
ret = nntp_handle_timeout();
switch (ret) {
case 1:
len = 1;
goto read_it;
case 0: /* We're quitting, so pretend it's OK */
strcpy(ser_line, "205 Ok");
break;
default:
break;
}
}
else
#endif
if (*ser_line <= NNTP_CLASS_CONT && *ser_line >= NNTP_CLASS_INF)
ret = 1; /* (this includes NNTP_CLASS_OK) */
else if (*ser_line == NNTP_CLASS_FATAL)
ret = -1;
/* Even though the following check doesn't catch all possible lists, the
* bit will get set right when the caller checks nntp_at_list_end(). */
if (atoi(ser_line) == NNTP_LIST_FOLLOWS_VAL)
nntplink.flags &= ~NNTP_NEW_CMD_OK;
else
nntplink.flags |= NNTP_NEW_CMD_OK;
len = strlen(ser_line);
if (len >= 2 && ser_line[len-1] == '\n' && ser_line[len-2] == '\r')
ser_line[len-2] = '\0';
#if defined(DEBUG) && defined(FLUSH)
if (debug & DEB_NNTP)
printf("<%s\n", ser_line) FLUSH;
#endif
#ifdef NNTP_HANDLE_AUTH_ERR
if (atoi(ser_line) == NNTP_AUTH_NEEDED_VAL) {
ret = nntp_handle_auth_err();
if (ret > 0)
goto read_it;
}
#endif
return ret;
}
bool
nntp_at_list_end(s)
char* s;
{
if (!s || (*s == '.' && (s[1] == '\0' || s[1] == '\r'))) {
nntplink.flags |= NNTP_NEW_CMD_OK;
return 1;
}
nntplink.flags &= ~NNTP_NEW_CMD_OK;
return 0;
}
/* This returns 1 when it reads a full line, 0 if it reads a partial
* line, and -2 on EOF/error. The maximum length includes a spot for
* the null-terminator, and we need room for our "\r\n"-stripping code
* to work right, so "len" MUST be at least 3.
*/
int
nntp_gets(bp, len)
char* bp;
int len;
{
int ch, n = 0;
char* cp = bp;
#ifdef HAS_SIGHOLD
sighold(SIGINT);
#endif
if (nntplink.trailing_CR) {
*cp++ = '\r';
len--;
nntplink.trailing_CR = 0;
}
while (1) {
if (len == 1) {
if (cp[-1] == '\r') {
/* Hold a trailing CR until next time because we may need
* to strip it if it is followed by a newline. */
cp--;
nntplink.trailing_CR = 1;
}
break;
}
do {
errno = 0;
ch = fgetc(nntplink.rd_fp);
} while (errno == EINTR);
if (ch == EOF) {
nntplink.flags |= NNTP_NEW_CMD_OK;
n = -2;
break;
}
if (ch == '\n') {
if (cp != bp && cp[-1] == '\r')
cp--;
n = 1;
break;
}
*cp++ = ch;
len--;
}
*cp = '\0';
#ifdef HAS_SIGHOLD
sigrelse(SIGINT);
#endif
return n;
}
void
nntp_close(send_quit)
bool_int send_quit;
{
if (send_quit && nntplink.wr_fp != NULL && nntplink.rd_fp != NULL) {
if (nntp_command("QUIT") > 0)
nntp_check();
}
/* the nntp_check() above might have closed these already. */
if (nntplink.wr_fp != NULL) {
fclose(nntplink.wr_fp);
nntplink.wr_fp = NULL;
}
if (nntplink.rd_fp != NULL) {
fclose(nntplink.rd_fp);
nntplink.rd_fp = NULL;
}
nntplink.flags |= NNTP_NEW_CMD_OK;
}
#endif /* SUPPORT_NNTP */
|