Metropoli BBS
VIEWER: query.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
 *
 */

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

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

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

/************************************************************************/
/*                                                                      */
/************************************************************************/
int query_ids(int s, FILE *fp, char *ngs, long ldate, long ltime, char *dstrb)
{
    int nart = 0;
    int skipped = 0;
    int dupes = 0;
    char ngbuf[512];
    char *ngnext = NULL;
    char *ngend = NULL;
    char *nglist = NULL;
    FILE *fpnrc = NULL;
    int usenewsrc = strchr(ngs, '/') || strchr(ngs, '\\');

    if(usenewsrc) {
        if((fpnrc = xopen(ngs, "rt")) == NULL) {
            lperror(ngs);
            return(-1);
        }
    }
    else
        nglist = strdup(ngs);


    for(;;) {
        if(usenewsrc) {
            ngbuf[0] = '\0';
            while(strlen(ngbuf) < 256 && fgets(buf, 256, fpnrc)) {
                if((ngend = strchr(buf, ':')) == NULL)
                    continue;
                *ngend = '\0';
                if(ngbuf[0])
                    strcat(ngbuf, ",");
                strcat(ngbuf, buf);
            }
            ngnext = ngbuf;
        }
        else {
            if(ngend)
                ngnext = ngend;
            else
                ngnext = nglist;
            if(strlen(ngnext) > 128) {
                ngend = ngnext + 128;
                while(*ngend != ',')
                    ngend--;
                *ngend++ = '\0';
            }
            else
                ngend = strchr(ngnext, '\0');
        }

        if(*ngnext == '\0')
            break;

        if (*dstrb)
            sprintf(buf, "NEWNEWS %s %06lu %06lu GMT <%s>", ngnext, ldate, ltime, dstrb);
        else
            sprintf(buf, "NEWNEWS %s %06lu %06lu GMT", ngnext, ldate, ltime);
        if(so_printf(s, "%s\r\n", buf) < 0) {
            if(nglist)
                free(nglist);
            return(-1);
        }
        lprintf("%s", buf);

        if(recv_bline(s, buf) <= 0) {
            if(nglist)
                free(nglist);
            return(-1);
        }
        if (buf[0] != '2') {
            lprintf("Server error: %s", buf);
            if(nglist)
                free(nglist);
            return(-1);
        }

        for (;;) {
            if(recv_bline(s, buf) <= 0) {
                if(nglist)
                    free(nglist);
                return(-1);
            }
            if (!strcmp(buf, "."))
                break;
            if(skipped) {
                skipped++;
                continue;
            }
            if (gethistart(cfg.historyfile, buf, NULL))
                dupes++;
            else {
                nart++;
                if(fprintf(fp, "%s\n", buf) <= 0) {
                    printf(" - Error writing ids to file\n");
                    lprintf("Error writing ids to file");
                    nart = 0;
                    break;
                }
                if((nart % 100) == 0)
                    printf("\r%d", nart);
            }
        }
    }

    if(nglist)
        free(nglist);

    if(nart)
        printf("\r%d", nart);
    else
        printf("No");
    printf(" new article(s)");
    if(dupes) {
        printf(" and %d dupe(s) found", dupes);
        lprintf("%d duplicate article(s)", dupes);
    }
    if(skipped) {
        printf(", %d article(s) skipped", skipped);
        lprintf("%d article(s) skipped", skipped);
    }
    printf("\n");

    return(nart);
}


/************************************************************************/
/*                                                                      */
/************************************************************************/
static int send_id(int s, FILE *fpid, int mode, int *miss)
{
    int  rc = 1;
    char *cp;

    for(;;) {
        if(mode & 1) {
            if(fgets(buf, sizeof(buf), fpid) == NULL) {
                rc = 0;
                break;
            }
            if((cp = strchr(buf, '\n')) == NULL)
                continue;
            *cp = '\0';
            if(buf[0] == '\0')
                continue;
            if(so_printf(s, "ARTICLE %s\r\n", buf) < 0) {
                rc = -1;
                break;
            }
        }
        if(mode & 2) {
            if(recv_bline(s, buf) <= 0) {
                rc = -1;
                break;
            }
            if(buf[0] == '2') {
                rc = 1;
                break;
            }
            if(buf[0] == '4') {
                lprintf("Article missing: %s", buf);
                (*miss)++;
            }
            else
                lprintf("Server error: %s", buf);
        }
        if(mode != 3)
            break;
    }
    return(rc);
}

/************************************************************************/
/*                                                                      */
/************************************************************************/
int query_arts(int s, FILE *fpid, FILE *fpbt)
{
    int nart = 0;
    int miss = 0;

    printf("No ");
    if(DOMOD(MOD_ENHANCE)) {
        if(send_id(s, fpid, 1, &miss) == 1) {
            int pend = 1;
            while(send_id(s, fpid, 3, &miss) == 1) {
                if(artfetch(s, fpbt)) {
                    omitupdate = 1;
                    keepids = 1;
                    pend = 0;
                    break;
                }
                printf("\r%d ", ++nart);
            }
            if(pend && send_id(s, fpid, 2, &miss) == 1) {
                if(artfetch(s, fpbt) == 0)
                    printf("\r%d ", ++nart);
            }
        }
    }
    else {
        while(send_id(s, fpid, 3, &miss) == 1) {
            if(artfetch(s, fpbt)) {
                omitupdate = 1;
                keepids = 1;
                break;
            }
            printf("\r%d ", ++nart);
        }
    }
    printf("article(s) retrieved");

    if(miss) {
        printf(" and %d missing", miss);
        lprintf("%d article(s) missing", miss);
    }
    printf("\n");

    return(nart);
}

/************************************************************************/
/*                                                                      */
/************************************************************************/
int query_list(int s, FILE *fp, char *query)
{
    int items = 0;

    if(so_printf(s, query) < 0)
        return(-1);

    if(recv_bline(s, buf) <= 0)
        return(-1);
    if (buf[0] != '2') {
        lprintf("Server error: %s", buf);
        return(-1);
    }

    for (;;) {
        if(recv_bline(s, buf) <= 0)
            return(-1);
	if (!strcmp(buf, "."))
            break;
        items++;
        if(fprintf(fp, "%s\n", buf) <= 0) {
            printf(" - Error writing list to file\n");
            lprintf("Error writing list to file");
            items = 0;
            break;
        }
        if((items % 100) == 0)
            putchar('.');
    }
    printf("\n");

    return(items);
}
[ RETURN TO DIRECTORY ]