/* Copyright (C) 1995 EGLE Magic, New Zealand, All rights reserved. */

/* Stand alone nntpsend.c, this would work with any server as it accesses
   the server via nntp rather than poking around in the files itself.


Common faults it must deal with:

	Disk space running out:		Restart later at same place
	Article not wanted: 		Junk it
	Unknown error			????
	
	
*/

#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

int main(int argc, char *argv[])
{
	if (argc<3) {
		printf("Useage: nntpsend file.feed destination.site\n");
		return 0;
	}
	nntpsend(argv[1],argv[2]);
	return 1;
}
#define BFSZ 500
char site[BFSZ];
int nntpsend(char *fname, char *xsite)
{
	/* Add _sending onto the file name */
	/* Don't delete it until it's all sent. */
	/* First try and send the current _sending file. */
	/* Should make this more resiliant */

	char sname[BFSZ];
	strcpy(site,xsite);

	strcpy(sname,fname); strcat(sname,"_sending");
	f = fopen(sname,"r");
	if (f==NULL) {
		if (rename(fname,sname)!=0) {
			printf("Rename failed %s %s \n",fname,sname);
			return FALSE;
		}
		f = fopen(sname,"r");
	}
	if (f==NULL) {perror("Can't open sending file"); return FALSE;}
	for (;!feof(f);) {
		fgets(bf,BFSZ-10,f);
		if (!send_line(bf)) return FALSE;
	}	
	fclose(f);
	if (remove(sname)<0) perror("Remove failed");
	return TRUE;
}
/* Only return false if the nntp connection fails */
/* next level up should retry 10 times */
static int cfrom= -1;
static int cto= -1
/*msgid group:num f:retry */
/*
chan_write_s(chan,"480 transfer permission denied\015\012");
chan_write_s(chan,"436 sorry disk full - try again later\015\012");
chan_write_s(chan,"435 article not wanted - do not send it\015\012");
chan_write_s(chan,"335 send article to be transferred.  End with <CR-LF>.<CR-LF>\015\012");

I'd like to stream, 
*/
int send_line(char *s)
{
	if (cfrom<0) cfrom = send_open(from_site);
	if (cto<0) cto = send_open(to_site);
	if (cfrom<0 || cto<0) return GIVEUP;
	
	fcmd(cfrom,"
	fcmd(cto,"ihave %s",msgid);
	getstatus(cto);
	if (nntp_code==480) return GIVEUP;
	if (nntp_code==435) return NEXT;
	if (nntp_code==436) return LATER;
	if (nntp_code!=335) return NEXT;
	fcmd(cfrom,"group %s",gname);
	getstatus(cfrom);
	if (nntp_code!=xxx) return NEXT;
	fcmd(cfrom,"article %s",num);
	getstatus(cfrom);
	if (nntp_code!=xxx) return NEXT;
	for (;;) {
		s = get_line(cfrom);
		put_line(cto,s);
		if (strcmp(s,".\015\012")==0) break;
	}
	put_line(cto,NULL); /* Flush output buffer */	
	getstatus(cto);	
	if (nntp_code==480) return GIVEUP;
	if (nntp_code==435) return NEXT;
	if (nntp_code==436) return LATER;
	if (nntp_code!=335) return NEXT;
	return OK;
}
char *get_line(int chan)
{
	static char *bf[2];
	static int blen;
	bf[0] = malloc(20000);

again:	
	s = strstr(bf,EOL);
	if (s==NULL) {
		blen = strlen(bf);
		want = 2000;
		if (blen>10000) {
			bf[0] = 0;
			return NULL;
		}
		n = nntp_read(chan,bf+blen,want);
		if (n<0) return NULL;
		bf[blen+n] = 0;
		goto again;
	}
	
}
	
