/* Copyright (C) 1995 NetWin World, New Zealand, All rights reserved. */
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "dnews.h"
#include "conf.h"
#include "lib.h"
#include "emsg.h"
#include "nntp.h"
#ifndef TRUE
#define FALSE 0
#define TRUE (!FALSE)
#endif
char *reg_address(void)   ;
char *reg_address2(void) ;
char *reg_address3(void);
char *hide_data(char *a, char *b, char *c);
char *unhide_data(char *s);
int debug_lvl;
char *test_mid(int i);
char dnews_conf[BFSZ];
int do_register(void);
int main(int argc, char *argv[])
{
        char bf[61000];
        static char bf2[300];
        char out[1001];
	int xfrom,j;
	char mid[200];
        char myname[300];
        char *s;
        char aline[8000];
        int c;
	int xlen = 0;
	int start_time;
        int i,n;
#ifdef OS2x
	setvbuf(stdin,NULL,_IONBF,1000);
	setvbuf(stdout,NULL,_IONBF,1000);
#endif

	if (argc<2) {
		printf("Usage: testfeed 200 x.y.z\n");
		return 0;
	}
        nntp_startup();
	xfrom = atoi(argv[1]);
        strcpy(myname,argv[2]);
	nntp_alwaysblock();
        c = nntp_open(myname);
        if (c<0) {
                goto the_end;
        }
        nntp_setblock(c,TRUE); /* Set to blocking IO */

        n = nntp_read(c,out,1000);
        out[n] = 0;
        out[n+1] = 0;
        printf("Welcome: %s",out);
	start_time = lib_gmtime();
	for (i=0; i<10; i++) {
		strcpy(mid,test_mid(i+xfrom));
		sprintf(bf,"TAKETHIS %s\r\n",mid); nntp_write_s(c,bf,strlen(bf));
		xlen = 0;
		sprintf(bf+xlen,"Subject: test ignore\r\n"); xlen += strlen(bf+xlen);
		sprintf(bf+xlen,"From: noone@test.site\r\n"); xlen += strlen(bf+xlen);
		sprintf(bf+xlen,"Message-ID: %s\r\n",mid); xlen += strlen(bf+xlen);
		sprintf(bf+xlen,"Distribution: local\r\n"); xlen += strlen(bf+xlen);
		sprintf(bf+xlen,"Date: 5 Mar 1997 21:22:23 GMT\r\n"); xlen += strlen(bf+xlen);
		sprintf(bf+xlen,"Path: newsfeed.internetmci.com!news.vec.net!news.sprint.link!\r\n"); xlen += strlen(bf+xlen);
		sprintf(bf+xlen,"Newsgroups: alt.test\r\n\r\n"); xlen += strlen(bf+xlen);
	        nntp_write_s(c,bf,strlen(bf)); 
		strcpy(bf,"");
		for (j=0; j<50; j++) {
			strcat(bf,"**************************************************\r\n");		
		}
		nntp_write_s(c,bf,strlen(bf)); 
		if (i==5) {
			memset(bf,'x',32000);
			bf[32000] = 0;
		}
		strcat(bf,"\r\n.\r\n");
		nntp_write_s(c,bf,strlen(bf)); 
	}
	strcpy(bf,"junk_command\r\n");
	nntp_write_s(c,bf,strlen(bf)); 

        
	strcpy(bf,"quit\015\012");
        nntp_write_s(c,bf,strlen(bf)); 

        n = nntp_read(c,out,1000);
        for (;n>=0;) {
                out[n] = 0;
                out[n+1] = 0;
                printf("%s",out);
		if (strstr(out,"500")!=NULL) break;
                n = nntp_read(c,out,1000);
              
        }
	printf("Elapsed time %d seconds %g/second\n",lib_gmtime()-start_time,(double) 10.0/(lib_gmtime()-start_time));
the_end:
        return 1;
}
char *group_name(int i)
{
        return "(none)";
}

char *test_mid(int i)
{
	static char bf[1000];
	sprintf(bf,"<%d.%d@marc.cri.nz>",i,i*i);
	return bf;
}
void dmsg(char * arg_list, ...)
{
        return;
}
void filemsg(char * arg_list, ...)
{
        va_list arg_ptr;
        char *format, output[1000];
        va_start(arg_ptr, arg_list);
        format = arg_list;
        vsprintf(output, format, arg_ptr);
        perror("File error:");
        printf("%s",output);

}
void imsg(char * arg_list, ...)
{
        va_list arg_ptr;
        char *format, output[1000];
        va_start(arg_ptr, arg_list);
        format = arg_list;
        vsprintf(output, format, arg_ptr);
/* printf("%s",output); */
}
void wmsg(char * arg_list, ...)
{
        va_list arg_ptr;
        char *format, output[1000];
        va_start(arg_ptr, arg_list);
        format = arg_list;
        vsprintf(output, format, arg_ptr);
        printf("%s",output);
}
void emsg(char * arg_list, ...)
{
        va_list arg_ptr;
        char *format, output[1000];
        va_start(arg_ptr, arg_list);
        format = arg_list;
        vsprintf(output, format, arg_ptr);
        printf("%s",output);
}
#ifndef unix
int lib_mail_file(char *to, char *subj, char *fname)
{
        return TRUE;
}
int lib_freespace(char *s)
{
        return 100000;
}
#endif
int diskuse_get(void)
{
        return 0;
}
char *nt_pcname(void);

char *reg_address3(void)
{
        static char bf[512];
        strcpy(bf,config_get(C_RESELL_ADDRESS2));
        if (strlen(bf)==0) strcpy(bf,"FAX: +64-9630-0689 ");
        return bf;
}
char *lib_findfile(char *x)
{
        return NULL;
}
void dnews_manager_report(char *s)
{
        
}
int post_local(String *xitem, char *addr, int ismod)
{
        return TRUE;
}
void dnews_exit(char *reason, char *file, int line)
{
        printf("Crashing\n");
        exit(1);
}



#ifdef WIN32
#define KEY_COMPUTER_NAME "SYSTEM\\CurrentControlSet\\Control\\ComputerName\\ComputerName"
#include <windows.h>
#include <winnt.h>
char *nt_pcname(void)
{
    HKEY MyKey;
        static    char Line[64];
    int  linelen = 64;
    DWORD type_of_entry, result;

        RegCreateKey(HKEY_LOCAL_MACHINE, KEY_COMPUTER_NAME, &MyKey);
        result = RegQueryValueEx(MyKey, "ComputerName", 0, &type_of_entry, 
                (unsigned char *)Line, &linelen);
        RegCloseKey(MyKey);
        if (result == ERROR_SUCCESS && type_of_entry == REG_SZ) {
                return Line;
        }
}

#endif


