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 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641
|
/* nntp.c
*/
/* This software is copyrighted as detailed in the LICENSE file. */
#include "EXTERN.h"
#include "common.h"
#include "list.h"
#include "util.h"
#include "util2.h"
#include "init.h"
#include "trn.h"
#include "hash.h"
#include "ngdata.h"
#include "nntpclient.h"
#include "datasrc.h"
#include "INTERN.h"
#include "nntp.h"
#include "nntp.ih"
#include "EXTERN.h"
#include "rcln.h"
#include "cache.h"
#include "bits.h"
#include "head.h"
#include "term.h"
#include "final.h"
#include "artio.h"
#include "rcstuff.h"
#ifdef SUPPORT_NNTP
int
nntp_list(type, arg, len)
char* type;
char* arg;
int len;
{
int ret;
#ifdef DEBUG /*$$*/
if (len && (debug & 1) && strcaseEQ(type,"active"))
return -1;
#endif
if (len)
sprintf(ser_line, "LIST %s %.*s", type, len, arg);
else if (strcaseEQ(type,"active"))
strcpy(ser_line, "LIST");
else
sprintf(ser_line, "LIST %s", type);
if (nntp_command(ser_line) <= 0)
return -2;
if ((ret = nntp_check()) <= 0)
return ret? ret : -1;
if (!len)
return 1;
if ((ret = nntp_gets(ser_line, sizeof ser_line)) < 0)
return ret;
#if defined(DEBUG) && defined(FLUSH)
if (debug & DEB_NNTP)
printf("<%s\n", ser_line) FLUSH;
#endif
if (nntp_at_list_end(ser_line))
return 0;
return 1;
}
void
nntp_finish_list()
{
int ret;
do {
while ((ret = nntp_gets(ser_line, sizeof ser_line)) == 0) {
/* A line w/o a newline is too long to be the end of the
** list, so grab the rest of this line and try again. */
while ((ret = nntp_gets(ser_line, sizeof ser_line)) == 0)
;
if (ret < 0)
return;
}
} while (ret > 0 && !nntp_at_list_end(ser_line));
}
/* try to access the specified group */
int
nntp_group(group, gp)
char* group;
NGDATA* gp;
{
sprintf(ser_line, "GROUP %s", group);
if (nntp_command(ser_line) <= 0)
return -2;
switch (nntp_check()) {
case -2:
return -2;
case -1:
case 0: {
int ser_int = atoi(ser_line);
if (ser_int != NNTP_NOSUCHGROUP_VAL
&& ser_int != NNTP_SYNTAX_VAL) {
if (ser_int != NNTP_AUTH_NEEDED_VAL && ser_int != NNTP_ACCESS_VAL
&& ser_int != NNTP_AUTH_REJECT_VAL) {
fprintf(stderr, "\nServer's response to GROUP %s:\n%s\n",
group, ser_line);
return -1;
}
}
return 0;
}
}
if (gp) {
long count, first, last;
(void) sscanf(ser_line,"%*d%ld%ld%ld",&count,&first,&last);
/* NNTP mangles the high/low values when no articles are present. */
if (!count)
gp->abs1st = gp->ngmax+1;
else {
gp->abs1st = (ART_NUM)first;
gp->ngmax = (ART_NUM)last;
}
}
return 1;
}
/* check on an article's existence */
int
nntp_stat(artnum)
ART_NUM artnum;
{
sprintf(ser_line, "STAT %ld", (long)artnum);
if (nntp_command(ser_line) <= 0)
return -2;
return nntp_check();
}
/* check on an article's existence by its message id */
ART_NUM
nntp_stat_id(msgid)
char* msgid;
{
long artnum;
sprintf(ser_line, "STAT %s", msgid);
if (nntp_command(ser_line) <= 0)
return -2;
artnum = nntp_check();
if (artnum > 0 && sscanf(ser_line, "%*d%ld", &artnum) != 1)
artnum = 0;
return (ART_NUM)artnum;
}
ART_NUM
nntp_next_art()
{
long artnum;
if (nntp_command("NEXT") <= 0)
return -2;
artnum = nntp_check();
if (artnum > 0 && sscanf(ser_line, "%*d %ld", &artnum) != 1)
artnum = 0;
return (ART_NUM)artnum;
}
/* prepare to get the header */
int
nntp_header(artnum)
ART_NUM artnum;
{
sprintf(ser_line, "HEAD %ld", (long)artnum);
if (nntp_command(ser_line) <= 0)
return -2;
return nntp_check();
}
/* copy the body of an article to a temporary file */
void
nntp_body(artnum)
ART_NUM artnum;
{
char* artname;
artname = nntp_artname(artnum, FALSE); /* Is it already in a tmp file? */
if (artname) {
if (body_pos >= 0)
nntp_finishbody(FB_DISCARD);
artfp = fopen(artname,"r");
if (artfp && fstat(fileno(artfp),&filestat) == 0)
body_end = filestat.st_size;
return;
}
artname = nntp_artname(artnum, TRUE); /* Allocate a tmp file */
if (!(artfp = fopen(artname, "w+"))) {
fprintf(stderr, "\nUnable to write temporary file: '%s'.\n",
artname);
finalize(1); /*$$*/
}
#ifndef MSDOS
chmod(artname, 0600);
#endif
/*artio_setbuf(artfp);$$*/
if (parsed_art == artnum)
sprintf(ser_line, "BODY %ld", (long)artnum);
else
sprintf(ser_line, "ARTICLE %ld", (long)artnum);
if (nntp_command(ser_line) <= 0)
finalize(1); /*$$*/
switch (nntp_check()) {
case -2:
case -1:
finalize(1); /*$$*/
case 0:
fclose(artfp);
artfp = NULL;
errno = ENOENT; /* Simulate file-not-found */
return;
}
body_pos = 0;
if (parsed_art == artnum) {
fwrite(headbuf, 1, strlen(headbuf), artfp);
htype[PAST_HEADER].minpos = body_end = (ART_POS)ftell(artfp);
}
else {
char b[NNTP_STRLEN];
ART_POS prev_pos = body_end = 0;
while (nntp_copybody(b, sizeof b, body_end+1) > 0) {
if (*b == '\n' && body_end - prev_pos < sizeof b)
break;
prev_pos = body_end;
}
}
fseek(artfp, 0L, 0);
nntplink.flags &= ~NNTP_NEW_CMD_OK;
}
long
nntp_artsize()
{
return body_pos < 0 ? body_end : -1;
}
static int
nntp_copybody(s, limit, pos)
char* s;
int limit;
ART_POS pos;
{
int len;
bool had_nl = TRUE;
int found_nl;
while (pos > body_end || !had_nl) {
found_nl = nntp_gets(s, limit);
if (found_nl < 0)
strcpy(s,"."); /*$$*/
if (had_nl) {
if (nntp_at_list_end(s)) {
fseek(artfp, (long)body_pos, 0);
body_pos = -1;
return 0;
}
if (s[0] == '.')
safecpy(s,s+1,limit);
}
len = strlen(s);
if (found_nl)
strcpy(s+len, "\n");
fputs(s, artfp);
body_end = ftell(artfp);
had_nl = found_nl;
}
return 1;
}
int
nntp_finishbody(bmode)
int bmode;
{
char b[NNTP_STRLEN];
if (body_pos < 0)
return 0;
if (bmode == FB_DISCARD) {
/*printf("Discarding the rest of the article...\n") FLUSH; $$*/
#if 0
/* Implement this if flushing the data becomes possible */
nntp_artname(openart, -1); /* Or something... */
openart = 0; /* Since we didn't finish the art, forget its number */
#endif
}
else
if (bmode == FB_OUTPUT) {
#ifdef VERBOSE
IF(verbose)
printf("Receiving the rest of the article..."), fflush(stdout);
ELSE
#endif
#ifdef TERSE
printf("Receiving..."), fflush(stdout);
#endif
}
if (body_end != body_pos)
fseek(artfp, (long)body_end, 0);
if (bmode != FB_BACKGROUND)
nntp_copybody(b, sizeof b, (ART_POS)0x7fffffffL);
else {
while (nntp_copybody(b, sizeof b, body_end+1)) {
if (input_pending())
break;
}
if (body_pos >= 0)
fseek(artfp, (long)body_pos, 0);
}
if (bmode == FB_OUTPUT)
erase_line(0); /* erase the prompt */
return 1;
}
int
nntp_seekart(pos)
ART_POS pos;
{
if (body_pos >= 0) {
if (body_end < pos) {
char b[NNTP_STRLEN];
fseek(artfp, (long)body_end, 0);
nntp_copybody(b, sizeof b, pos);
if (body_pos >= 0)
body_pos = pos;
}
else
body_pos = pos;
}
return fseek(artfp, (long)pos, 0);
}
ART_POS
nntp_tellart()
{
return body_pos < 0 ? (ART_POS)ftell(artfp) : body_pos;
}
char*
nntp_readart(s, limit)
char* s;
int limit;
{
if (body_pos >= 0) {
if (body_pos == body_end) {
if (nntp_copybody(s, limit, body_pos+1) <= 0)
return NULL;
if (body_end - body_pos < limit) {
body_pos = body_end;
return s;
}
fseek(artfp, (long)body_pos, 0);
}
s = fgets(s, limit, artfp);
body_pos = ftell(artfp);
if (body_pos == body_end)
fseek(artfp, (long)body_pos, 0); /* Prepare for coming write */
return s;
}
return fgets(s, limit, artfp);
}
/* This is a 1-relative list */
static int maxdays[] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
time_t
nntp_time()
{
char* s;
int year, month, day, hh, mm;
time_t ss;
if (nntp_command("DATE") <= 0)
return -2;
if (nntp_check() <= 0)
return time((time_t*)NULL);
s = rindex(ser_line, ' ') + 1;
month = (s[4] - '0') * 10 + (s[5] - '0');
day = (s[6] - '0') * 10 + (s[7] - '0');
hh = (s[8] - '0') * 10 + (s[9] - '0');
mm = (s[10] - '0') * 10 + (s[11] - '0');
ss = (s[12] - '0') * 10 + (s[13] - '0');
s[4] = '\0';
year = atoi(s);
/* This simple algorithm will be valid until the year 2100 */
if (year % 4)
maxdays[2] = 28;
else
maxdays[2] = 29;
if (month < 1 || month > 12 || day < 1 || day > maxdays[month]
|| hh < 0 || hh > 23 || mm < 0 || mm > 59
|| ss < 0 || ss > 59)
return time((time_t*)NULL);
for (month--; month; month--)
day += maxdays[month];
ss = ((((year-1970) * 365 + (year-1969)/4 + day - 1) * 24L + hh) * 60
+ mm) * 60 + ss;
return ss;
}
int
nntp_newgroups(t)
time_t t;
{
struct tm *ts;
ts = gmtime(&t);
sprintf(ser_line, "NEWGROUPS %02d%02d%02d %02d%02d%02d GMT",
ts->tm_year % 100, ts->tm_mon+1, ts->tm_mday,
ts->tm_hour, ts->tm_min, ts->tm_sec);
if (nntp_command(ser_line) <= 0)
return -2;
return nntp_check();
}
int
nntp_artnums()
{
if (datasrc->flags & DF_NOLISTGROUP)
return 0;
if (nntp_command("LISTGROUP") <= 0)
return -2;
if (nntp_check() <= 0) {
datasrc->flags |= DF_NOLISTGROUP;
return 0;
}
return 1;
}
#if 0
int
nntp_rover()
{
if (datasrc->flags & DF_NOXROVER)
return 0;
if (nntp_command("XROVER 1-") <= 0)
return -2;
if (nntp_check() <= 0) {
datasrc->flags |= DF_NOXROVER;
return 0;
}
return 1;
}
#endif
ART_NUM
nntp_find_real_art(after)
ART_NUM after;
{
ART_NUM an;
if (last_cached > after || last_cached < absfirst
|| nntp_stat(last_cached) <= 0) {
if (nntp_stat_id("") > after)
return 0;
}
while ((an = nntp_next_art()) > 0) {
if (an > after)
return an;
if (after - an > 10)
break;
}
return 0;
}
char*
nntp_artname(artnum, allocate)
ART_NUM artnum;
bool_int allocate;
{
static ART_NUM artnums[MAX_NNTP_ARTICLES];
static time_t artages[MAX_NNTP_ARTICLES];
time_t now, lowage;
int i, j;
if (!artnum) {
for (i = 0; i < MAX_NNTP_ARTICLES; i++) {
artnums[i] = 0;
artages[i] = 0;
}
return NULL;
}
now = time((time_t*)NULL);
for (i = j = 0, lowage = now; i < MAX_NNTP_ARTICLES; i++) {
if (artnums[i] == artnum) {
artages[i] = now;
return nntp_tmpname(i);
}
if (artages[i] <= lowage)
lowage = artages[j = i];
}
if (allocate) {
artnums[j] = artnum;
artages[j] = now;
return nntp_tmpname(j);
}
return NULL;
}
char*
nntp_tmpname(ndx)
int ndx;
{
static char artname[20];
sprintf(artname,"rrn.%ld.%d",our_pid,ndx);
return artname;
}
int
nntp_handle_nested_lists()
{
if (strcaseEQ(last_command,"quit"))
return 0; /*$$ flush data needed? */
if (nntp_finishbody(FB_DISCARD))
return 1;
fprintf(stderr,"Programming error! Nested NNTP calls detected.\n");
return -1;
}
int
nntp_handle_timeout()
{
static bool handling_timeout = FALSE;
char last_command_save[NNTP_STRLEN];
if (strcaseEQ(last_command,"quit"))
return 0;
if (handling_timeout)
return -1;
handling_timeout = TRUE;
strcpy(last_command_save, last_command);
nntp_close(FALSE);
datasrc->nntplink = nntplink;
if (nntp_connect(datasrc->newsid, 0) <= 0)
return -2;
datasrc->nntplink = nntplink;
if (in_ng && nntp_group(ngname, (NGDATA*)NULL) <= 0)
return -2;
if (nntp_command(last_command_save) <= 0)
return -1;
strcpy(last_command, last_command_save); /*$$ Is this really needed? */
handling_timeout = FALSE;
return 1;
}
void
nntp_server_died(dp)
DATASRC* dp;
{
MULTIRC* mp = multirc;
close_datasrc(dp);
dp->flags |= DF_UNAVAILABLE;
unuse_multirc(mp);
if (!use_multirc(mp))
multirc = NULL;
fprintf(stderr,"\n%s\n", ser_line);
get_anything();
}
/* nntp_readcheck -- get a line of text from the server, interpreting
** it as a status message for a binary command. Call this once
** before calling nntp_read() for the actual data transfer.
*/
#ifdef SUPPORT_XTHREAD
long
nntp_readcheck()
{
/* try to get the status line and the status code */
switch (nntp_check()) {
case -2:
return -2;
case -1:
case 0:
return rawbytes = -1;
}
/* try to get the number of bytes being transfered */
if (sscanf(ser_line, "%*d%ld", &rawbytes) != 1)
return rawbytes = -1;
return rawbytes;
}
#endif
/* nntp_read -- read data from the server in binary format. This call must
** be preceeded by an appropriate binary command and an nntp_readcheck call.
*/
#ifdef SUPPORT_XTHREAD
long
nntp_read(buf, n)
char* buf;
long n;
{
/* if no bytes to read, then just return EOF */
if (rawbytes < 0)
return 0;
#ifdef HAS_SIGHOLD
sighold(SIGINT);
#endif
/* try to read some data from the server */
if (rawbytes) {
n = fread(buf, 1, n > rawbytes ? rawbytes : n, nntplink.rd_fp);
rawbytes -= n;
} else
n = 0;
/* if no more left, then fetch the end-of-command signature */
if (!rawbytes) {
char buf[5]; /* "\r\n.\r\n" */
fread(buf, 1, 5, nntplink.rd_fp);
rawbytes = -1;
}
#ifdef HAS_SIGHOLD
sigrelse(SIGINT);
#endif
return n;
}
#endif /* SUPPORT_XTHREAD */
#endif /* SUPPORT_NNTP */
|