/* Copyright (C) 1995 NetWin World, New Zealand, All rights reserved. */
/* Mostly stub routines needed for link that don't do anything, but some
   are functional */

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <signal.h>
#include "emsg.h"
#include "group.h"
#include "item.h"
#include "dnews.h"
#include "nntp.h"
#include "suck.h"
#include "chan.h"
#include "str.h"
#include "sary.h"
#include "ncmd.h"
#include "lib.h"
/* #define EMSG_DEBUG TRUE*/
void emsg_do(int lvl, char *s); 
int debug_lvl;
extern int nchan; 
#define check(c) if ((c<0) || (c>=MAX_CHAN)) {emsg("chan: bad channel\n"); abort();}
void chan_newchan(int c) {}
int chan_isfree(int c){return 1;}

void log_flush(void);
int cmd_finish(int chan) {return 1;}
chan_ihaveonly(int c)
{
	return 0;
}
char *chan_elimit(void)
{
	return "";
}
int chan_nusers(void) {return 0;}
void chan_init(int c)
{
}
void sponser_enable_free(void)
{
	
}
int chan_canpost(int c)
{
	return TRUE;
}
int chan_isready(int c)
{
	return 1;
}

char *chan_getipname(int c)
{
	return "noipname";
}
/* setipnum must have been called first */
char *chan_getipnum(int n)
{
	return "xxx";
}
int chan_outspeed(int c)
{
	return 1000000;
}
int chan_isihave(int c)
{
	return 0;
}

void chan_setipname(int c, char *s)
{
} 
void chan_setipnum(int c, char *s)
{
}
int ihavein_clear(int i) {return 0;}

#define MAXREC 30000
#define INSIZE MAXREC
#define LF "\012"
void channel_read(int c)
{
	int nn,rlen;
	char *s1,*s2;
	char rec[MAXREC];
	static int n[MAX_CHAN];
	static char *buf[MAX_CHAN]; /* Increase to 8001 when debuging finished*/ 
	static int want;

	if (buf[c]==NULL) buf[c] = (char *) mymalloc(INSIZE);

	want = INSIZE-1-n[c];
	if (want>2000) want = 2000; /* Limit the size of chunks we process */
	if (want<=0) {
		emsg("chan: Record too big in item, throwing it away, tough\n");
		n[c] = 0;
	}
	nn = nntp_read(c, buf[c]+n[c],want);
	if (nn==0) wmsg("fatal: Read zero bytes, asked for %d bytes \n",want);
	if (nn>0) n[c] +=nn;
	if (nn<0) {chan_close(c); n[c] = 0;}
	else if (n[c]>0)  {
		s1 = buf[c];
		s1[n[c]] = 0;	
		s2 = strstr(s1,LF);
		if (s2==NULL) return;
		for (;s2!=NULL;) {
			rlen = s2-s1+1;
			if (rlen>=(MAXREC-100)) {
				emsg("Record too big %d \n",rec);
				rlen = MAXREC-100;
			}
			ncpy(rec,s1,rlen); 
			dmsg("chanrec: Got rec n[%d]=%d (%d) \n",c,n[c],rlen);
			n[c] -= rlen;
			if (rec[rlen-2]!='\r') { /* add CR if missing */
				strcpy(rec+rlen-1,EOL); rlen++;
			}
			chan_read(c,rec,rlen);
			s1 = s2 + 1;
			s2 = strstr(s1,LF);		
		}
		memmove(buf[c],s1,strlen(s1)+1); 
	}
}
int emsg(char * arg_list, ...)
{
 	va_list arg_ptr;
 	char *format, output[8000];
 	va_start(arg_ptr, arg_list);
 	format = arg_list;
	vsprintf(output, format, arg_ptr);
	emsg_do(D_ERROR,output);
	log_flush();
	return 0;
}
int wmsg(char * arg_list, ...)
{
 	va_list arg_ptr;
 	char *format, output[8000];
 	va_start(arg_ptr, arg_list);
 	format = arg_list;
	vsprintf(output, format, arg_ptr);
	emsg_do(D_WARN,output);
	return 0;
}
int imsg(const char * arg_list, ...)
{
 	va_list arg_ptr;
 	char *format, output[8000];
 	va_start(arg_ptr, arg_list);
 	format = (char *) arg_list;
	vsprintf(output, format, arg_ptr);
	emsg_do(D_INFO,output);
	return 0;
}
int imsgf(char * arg_list, ...)
{
 	va_list arg_ptr;
 	char *format, output[8000];
 	va_start(arg_ptr, arg_list);
 	format = arg_list;
	vsprintf(output, format, arg_ptr);
	emsg_do(D_INFO,output);
	return 0;
}

/*#define EMSG_DEBUG */

int dmsg(char * arg_list, ...)
{
#ifndef EMSG_DEBUG
	return 0; 
#else
 	va_list arg_ptr;
 	char *format, output[8000];
 	va_start(arg_ptr, arg_list);
 	format = arg_list;

	vsprintf(output, format, arg_ptr);
	emsg_do(D_DEBUG,output);
	return 0;
#endif
}
int filemsg(char * arg_list, ...)
{
 	va_list arg_ptr;
 	char *format, output[8000];
	char bf[500];

	strcpy(bf,strerror(errno));

 	va_start(arg_ptr, arg_list);
 	format = arg_list;
	vsprintf(output, format, arg_ptr);
	emsg_do(D_ERROR,output);
	emsg("file: %s \n",bf);
	return 0;
}

static FILE *flog;
static char logname[500];
void log_open(char *s)
{
	strcpy(logname,s);
	if (flog!=NULL) fclose(flog);
	flog = fopen(logname,"w");
	if (flog==NULL) {
		perror("Can't write to log file"); 
		printf("Log file %s \n",logname);
	}
}
void emsg_do(int lvl, char *s)
{
	static int nlines;
	time_t t;
	static time_t last_flush;
	char tstr[30];
	struct tm *tt; 

	nlines++;

	t = time(NULL);
	tt = localtime(&t);

	strcpy(tstr,ctime(&t));
	tstr[19] = 0;

	if (nlines>50000) {
		log_flush();
		flog = fopen(logname,"w");
		fprintf(flog,"Log file rotated with %d lines\n",nlines);
		nlines = 0;
	}
	if (flog==NULL) flog = fopen(logname,"a");
	if (flog==NULL) {printf("%s: %s",tstr+8,s); return;}
	else fprintf(flog,"%s: %s",tstr+8,s);
	if (time(NULL) - last_flush > 10) {
	 	log_flush();
		last_flush = time(NULL);
	}

#ifdef EMSG_DEBUG
	log_flush();
#endif
}	
void log_flush(void)
{
	if (flog!=NULL) fclose(flog);
	flog = NULL;	
}
void sig_init(void)
{
#ifdef unix
#ifndef WIN32
#ifndef OS2
	signal(SIGPIPE,SIG_IGN);
#endif
#endif
#endif
}
int diskuse_get(void)
{
	return 100000;
}
char *group_name_x(int x, char *file, int line)
{
	return "(nogroupname)";
}
char * group_namelc_x(int x, char *file, int line)
{
	return "(nogroupname)";
}
int group_getgroupid(int x) {return x;}
void dnews_manager_report(char *s)
{
}
int chan_cantalk(int c)
{ return TRUE; }
void access_read(void) {}

char *group_status(void){return NULL;}
String *hist_status(void){return NULL;}

void chan_close(int c)
{
	nntp_close(c);
}
#ifdef OS2
#define NEED_POST 1
#endif
#ifdef WIN32
#define NEED_POST 1
#endif

#ifdef NEED_POST
int xpost_local(String *xitem, char *addr, int ismod)
{
	return TRUE;
}
#endif
void stats_cmd(char *s) {}
void stats_end(char *s) {}
#ifdef NEED_STRERROR
char *strerror(int x)
{
/*	if ((x<sys_nerr) && (x>=0)) return sys_errlist[x];
*/
	return "No error message";
}
#endif
int lib_msec(void)
{
	return 2;
}
int chan_ismore(int x)
{
	return FALSE;
}