/* 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 INDEBUG 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 id_ask(int x) {}
void id_read(int chan, char *bf, int sz) {}

void emsg(char * arg_list, ...)
{
 	va_list arg_ptr;
 	char *format, output[8000];
	int len;
 	va_start(arg_ptr, arg_list);
 	format = arg_list;
	vsprintf(output, format, arg_ptr);
	emsg_do(D_ERROR,output);
}
void imsgf(char * arg_list, ...)
{
 	va_list arg_ptr;
 	char *format, output[8000];
	int len;
 	va_start(arg_ptr, arg_list);
 	format = arg_list;
	vsprintf(output, format, arg_ptr);
	emsg_do(D_ERROR,output);
}
void wmsg(char * arg_list, ...)
{
 	va_list arg_ptr;
 	char *format, output[8000];
	int len;
 	va_start(arg_ptr, arg_list);
 	format = arg_list;
	vsprintf(output, format, arg_ptr);
	emsg_do(D_WARN,output);
}
void imsg(char * arg_list, ...)
{
 	va_list arg_ptr;
 	char *format, output[8000];
	int len;
 	va_start(arg_ptr, arg_list);
 	format = arg_list;
	vsprintf(output, format, arg_ptr);
	emsg_do(D_INFO,output);
}
static FILE *flog;
static char logname[500];
void dmsg(char * arg_list, ...)
{
 	va_list arg_ptr;
 	char *format, output[8000];
	int len;
 	va_start(arg_ptr, arg_list);
 	format = arg_list;
	vsprintf(output, format, arg_ptr);
#ifdef INDEBUGx
	if (flog==NULL) flog = fopen(logname,"a");
   	fprintf(flog,"%s",output);
	fclose(flog); flog=NULL;
#endif
/*	emsg_do(D_DEBUG,output);*/
/*	fflush(flog);*/
}
void filemsg(char * arg_list, ...)
{
 	va_list arg_ptr;
 	char *format, output[8000];
	int len;
	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);
}

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)
{
	time_t t;
	char bf[8000];
	char tstr[30];
	struct tm *tt; 


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

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

	printf("%s: %s",tstr+8,s); 
#ifndef INDEBUG
	return; 
#endif

	if (flog==NULL) flog = fopen(logname,"a");
   	fprintf(flog,"%s: %s",tstr+8,s);
#ifdef INDEBUG
	fclose(flog); flog = NULL;
#endif
	return;

}	
void log_flush(void)
{
	if (flog!=NULL) fclose(flog);
	flog = NULL;	
}
void sig_init(void)
{
#ifdef unix
#ifndef WIN32
	signal(SIGPIPE,SIG_IGN);
#endif
#endif
}
int diskuse_get(void)
{
	return 100000;
}
char * group_name(int x)
{
	return "(nogroupname)";
}
char * group_namelc(int x)
{
	return "(nogroupname)";
}
char *group_status(void){return NULL;}
String *hist_status(void){return NULL;}
#ifdef WIN32
char *nt_pcname(void) {}
char *nt_other(void) {}
#endif
int ihavein_clear(int i) {return 0;}

void dnews_manager_report(char *s)
{
}
#ifdef WIN32
int post_local(String *xitem, char *addr, int ismod)
{
	return TRUE;
}
#endif
void dnews_exit(char *reason, char *file, int line)
{
	stress_exit();
}
int do_ncmd(int chan, char *s)
{
	return TRUE;
}
int ncmd_getihave(int chan, char *s, int sz)
{
	return TRUE;
}
int ncmd_getpost(int chan, char *s, int sz)
{
	return TRUE;
}
void emsg_flush(void)
{
	
}
int group_find(char *s)
{
	return -1;
}
int group_match(char *wild, char *grp)
{
	return TRUE;
}
int cmd_finish(int chan) {}
char *chan_busystr(int x) {return "";}

int match_wild(char *wild, char *grp)
{
	return TRUE;
}
String *item_head(int group, int num){return NULL;}
String *item_body(int group, int num){return NULL;}
int suck_getitem(char *s, int sz) { return TRUE;}
int suck_getitem_status(char *s, int sz) { return TRUE;}
int suck_getitem_next(char *s, int sz) { return TRUE;}
int suck_getlist(char *s, int sz) { return TRUE;}
void stats_cmd(char *s) {}
void stats_end(char *s) {}
void stats_feed(char *name, int delay, int nreject, int npost, int ndup, double size, double ratio) {}
/*
char *nt_pcname(void) {return "";}
char *nt_other(void) {return "";}
*/
int group_getgroupid(int i) {return i;}
#ifdef NEED_STRERROR
extern int sys_nerr;
extern char *sys_errlist[];
char *strerror(int x)
{
      if ((x<sys_nerr) && (x>=0)) return sys_errlist[x];
/*
        return "No error message";
*/
}
#endif


/* Dummy routines for things that don't get linked in all the time */
int dnews_memdebug(void)
{
	return 0;
}
void echan_set(int x)
{
}
int lib_checkspace(void)
{
	return 1;
}
char *hist_status_s(void)
{
		return "";
}
/*
*/
char *slave_port(void)
{
	return "";
}
int slave_n(void)
{
	return 0;
}
int is_slave(void)
{
	return 0;
}
char *db_spool_dir(int d)
{
	return "";
}
int db_total_space(void)
{
	return 100;
}
char *new_make(int months, int users, int flags, char *host)
{
	return "";
}
int new_test(char *k, char *host, int *users, int *flags, char *expires)
{
	return 1;
}
int stats_clock(void)
{
	return 1;
}
void stats_add(char *s, int start_time, int start_clock)
{

}
