Metropoli BBS
VIEWER: artfetch.c MODE: TEXT (ASCII)
/*
 * This OS/2 port was hacked by Harald Kipp from the
 *
 *      nntpxfer
 *
 *      Brian Kantor, UCSD 1986
 *      Stan Barber, November 7, 1989
 *
 * Bug reports related to THIS modified version should be sent to
 *
 *  harald@os2point.ping.de
 *  harald@sesam.com
 *  Fido: 2:2448/434
 *
 */

#define OS2
#include <types.h>
#include <netinet\in.h>
#include <sys\socket.h>
#include <netdb.h>

#undef min
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <conio.h>
#include <io.h>

#include <tcpconn.h>
#include <lprintf.h>

#include "chanx.h"
#include "config.h"

static long batchsize = 0L;

/************************************************************************/
/*                                                                      */
/*  Returns the number of bytes on success, 0 on verflow, -1 on error   */
/************************************************************************/
static int swallow(int s, char *buf, size_t bufsiz, long *nbytes)
{
    size_t cc;
    char *line = malloc(2048);

    while(bufsiz) {
        if(recv_bline(s, line) > 0) {
            if(*line == '.') {
                if(*(line + 1) == '\0')
                    break;
                strcpy(buf, line + 1);
            }
            else
                strcpy(buf, line);
            cc = strlen(buf);
            buf += cc;
            *buf++ = '\n';
            cc++;
            *buf = '\0';
            *nbytes += cc;

            if(cc < bufsiz)
                bufsiz -= cc;
            else
                bufsiz = 0;
        }
        else {
            bufsiz = -1;
            break;
        }

    }
    free(line);
    return(bufsiz);
}

/************************************************************************/
/*                                                                      */
/*  Returns 0 on success, -1 on error                                   */
/************************************************************************/
int artfetch(int s, FILE * bfp)
{
    int  result = 0;
    long rv = 0;
    size_t bufsiz = 32767;
    char *buf;
    char *tname;
    FILE *tfp;

    if(cfg.timelimit && time(NULL) >= cfg.timelimit) {
        lprintf("Connection time limit reached");
        return(-1);
    }
    if(cfg.sizelimit && batchsize >= cfg.sizelimit) {
        lprintf("Batch size limit reached");
        return(-1);
    }

    /*
     * Try to read the complete article into memory
     */
    buf = malloc(bufsiz);
    if((result = swallow(s, buf, bufsiz - 2048, &rv)) == 0) {
        tname = tempnam(NULL, "CNX");

        if((tfp = fopen(tname, "w+t")) != NULL) {
            fputs(buf, tfp);
            while(recv_bline(s, buf) > 0) {
                char *line = buf;
                if(*line == '.' && *(++line) == '\0')
                    break;
                rv += strlen(line) + 1;
                fputs(line, tfp);
                fputc('\n', tfp);
            }
            fprintf(bfp, "#! rnews %ld \n", rv);
            batchsize += rv;
            rewind(tfp);
            while (fgets(buf, sizeof(buf), tfp))
                fputs(buf, bfp);
            fclose(tfp);
            if(unlink(tname))
                lperror(tname);
        }
        else
            lperror(tname);
        free(tname);
    }
    /*
     * If the article doesn't fit into our memory buffer
     * then create a temporary file.
     */
    else if(result != -1) {
        fprintf(bfp, "#! rnews %ld \n", rv);
        batchsize += rv;
        fputs(buf, bfp);
        result = 0;
    }
    free(buf);

    return(result);
}
[ RETURN TO DIRECTORY ]