#ifndef __LIB_H
#define __LIB_H TRUE

/*Setup for Mac  - repeated as part of nntp.c*/
#ifdef __MWERKS__ /*RHP we will take this to mean we are making a Mac version*/
#include "Mac.h"
#endif

#define MEMDEBUG TRUE
#ifdef MEMDEBUG
#include "memdebug.h"
#endif

#ifdef VMS
#ifndef SYSTEM_TYPE
#define SYSTEM_TYPE "VMS"
#endif
#endif

#ifdef unix
#ifndef SYSTEM_TYPE
#define SYSTEM_TYPE "UNIX"
#endif
#endif

#ifdef WIN32
#undef SYSTEM_TYPE
#define SYSTEM_TYPE "Windows NT"
#endif

#ifdef ALPHANT
#undef SYSTEM_TYPE
#define SYSTEM_TYPE "Alpha NT"
#endif

/*RHP do same for mac I suppose*/
#ifdef Mac
#undef SYSTEM_TYPE
#define SYSTEM_TYPE "Mac OS"
#endif

#ifdef OS2
#undef SYSTEM_TYPE
#define SYSTEM_TYPE "OS2"
#endif

#ifdef NETWARE
#undef SYSTEM_TYPE
#define SYSTEM_TYPE "Netware"
#endif

#ifdef NETWARE
#define SHORT_NAMES TRUE
#endif




#ifndef GOT_TIME
#include <time.h>
#endif
#include "emsg.h"
#include "conf.h"
#include "str.h"
#ifndef TRUE
#define FALSE 0
#define TRUE !FALSE
#endif
#define BFSZ 600
#define BFSZ2 1600
#ifndef O_BINARY
#define O_BINARY 0
#endif

#define U_RW 0770

#define EOL "\015\012"
#ifdef WIN32
#define ASCII_FILES
#endif
#ifdef VMS
#define ASCII_FILES
#endif
#ifdef OS2
#define ASCII_FILES
#endif

#ifdef NETWARE
#define ASCII_FILES
#endif

#ifdef ASCII_FILES
#define F_APPEND "ab"
#define F_READ "rb"
#define F_WRITE "wb"
#else
#define F_APPEND "a"
#define F_READ "r"
#define F_WRITE "w"
#endif
int ncmd_group_finish(int chan);
void ncmd_backgnd(void);
int is_expiring(void);
int is_limited(void);
void lib_set_flags(int f);
int lib_wild_total(void);
int key_setextend(void);
int myspawn(char *cmd);
int ishdr(void);
void lib_msleep(int x);

void feed_idle(void);
int slave_post(int c,String *xitem);
void slave_xoverhit(int g);
void slave_xactive(int xchan, char *in);
char *file_kfile(void);
void slave_xcmd(int chan, char *in);
void nntp_setlocalip(int c,char *s);
void expire_groups(char *s);
void expire_pile_load(void);
int db_expired(int i);
double expire_pile_get(int i);
void expire_pile_del(int gid, int num);
void nntp_set_welcome(char *s);
char *feed_smtpname(void);
int min_get(int g);
int tomail_nntp_cmd(char *cmd, char *uname, String *result);
void iffree2(void *x,int t);
int thout_n(void);
void filter_idle(void);
char *tomail_status(void);
char *getmail_status(void);
void slave_xhit(int chan, char *in);
int dnewsauth(char *user, char *pass, char *usergrps, int bfsz);
int db_show_pile(int chan);
int lib_cdate(char *s);
char *file_dnews_config(void);
char *file_xwork(char *fname, int x);
void nntp_exit(void);
int max_strbf(void);
int suck_expire_groups(char *s);
unsigned int lib_length_file(char *fname);
void set_isuucp(int x);
char *str_todot(char *s);
char *str_todash(char *s);
int fx_total(void);
int max_items(void);
int suck_getactive(char *s);
int suck_getactive(char *s);
int hdr_matchgroup(char *gname);
char *get_wordnolf(char *s,int n);
void rej_rotate(void);
char *struntab(char *s);
void do_index_unpause(void); 
void do_index_pause(void); 
void slave_flushstart(int chan, char *in);
void set_egroups(char *xlist);
int tomail_feed(char *xsubj, char *xref,int grps[], int ngrps, char *cheat, int len);
void slave_flushstop(int chan, char *in);
void slave_xshutdown(int chan, char *in);
void slave_xsetslave(int chan, char *in);
void slave_fixgroup(int grp, int from, int to, int range, int cflag, int mflag);
int feed_isfeeder(void);
void slave_tokids(char *bf);
void slave_flushsnd(void);
void db_idle_slow(void);
void check_groups_any(void);
int is_debug(void);
int db_malloc_groups(void);
void wcache_init(void);
int hist_forget(char *msgid);
void hcache_init(void);
void ncmd_set_gap(void);
int item_test_read(int chan, char *gname);
int users_touch(void);
void wcache_write_one(int x);
void wcache_write_all(void);
double expire_pile_days(int i);
int conf_setapass(char *pass);
int pop_cache_freeone(char *user);
int pop_cache_freeall(void);
int user_cache_free_single(char *user);
int mem_wasted(void);
int ncmd_chan_init(int chan);
int id_check(int chan);
void db_setpart(int x);
int db_get_age(int bkt);
int db_add_idx(int fid, int grp,int num,int pnt, int len);
int part_write_complete(void);
int part_callback(int fid,int grp,int num,int pnt,int len);
int users_idle(void);


int fx_changedf(int handle);
int hdr_prefetch(void);
int delay_finish(int chan);
int ncmd_post(int chan, char *in);
int hque_suck(int grp, int chan );
int item_setuucp(int x);
int lib_thread_add(int x);
int lib_nthread(void);
void slave_killslaves(void);
void hque_bkgnd(void) ;
void slave_checkpause(void);
int fts_reload(void);
int group_nodownload(void);


void slave_shutdown(void);
void slave_flush(int t) ;
void lib_sleep(int x);
int slave_post(int c,String *xitem);
void slave_timeout(int c);
void ncmd_timeout(int c);
void chan_purge2(void);
void item_rejectmid(char *mid);
int lib_gmtimex(void);
int lib_crc(char *s);
int lib_crc2(char *xin);
void stat_start(int x);
void stat_end(int x);
int lib_file_mod(char *s);
int set_ignoreage(int x);
char *file_exe(char *s);
char *get_rest2(char *s,int n);
int lib_crc32(char *xin);
int lib_ncrc32(char *xin,int len);
void big_save(int h);
void lib_set_flags(int f);
void trace_add(char *fname, int line);
int lib_ispause(void);
#define vtrace() trace_add(__FILE__,__LINE__)
void db_trimgroup(char *gname, int listid[], int nlist);
int suck_listgroup(char *bf, int sz);

void readonly_readsetcached(void);
int readonly_post(int chan, String *xitem);
void readonly_check(void);
void active_catchup(void);
int is_mastermaster(void);
void readonly_readxhit(void);
int is_reader(void);
int is_readmaster(void);
int big_maxsz(void);
int lib_mail_file2(char *to, char *subj, char *fname);
char *key_reason(void);
void lib_logmem(void);
char *expire_err(void);
void db_setpatching(int x);
void expire_failed(char *s);
int trim_file(int chan, char *fname);
void readonly_pause(void);
void readonly_unpause(void);
int is_readonly(void);
int is_master(void);
int ras_dial(char *site);
void ras_exit(void);
int ras_hangup(void);
void set_rej_silent(int x);

int cmdq_add(char *s);
int cmdq_do(void);
int cmdq_len(void);
char * cmdq_name(int i);
char *cmdq_pop(void);



int expire_ispile(void);
int expire_npiles(void);
void expire_load(void);





void dnews_exit(char *a, char *b, int l);
#define dnews_ex(xx) dnews_exit(xx,__FILE__,__LINE__)
char *xtrunc(char *s);
int lib_remove(char *s);
void cvt_numfile2(int x, int *fg, int *fi) ;
void diskuse_add(int totlen);
void time_start(char *s);
void time_end(char *s);
void slave_bucket(int n, int dsk, int pile);
void time_show(void);
int pop_backgnd(void);
char *chan_showcon(void);
void user_clear(void);
void gage_choose(int grp[],int ngrp);
int dbi_changed(int handle);


int chan_nloggedin(char *username);
void db_purge_scan_test(int fi);
int cmd_showgroup(int chan, char *gname, char *d1, char *d2);
void show_outbf(char *s, int len);
char *str_trunc(char *s, int len);
void cmd_pause(int t, char *reason);
#define UPGRADE_DAT file_log("upgrade.dat")
int lib_rmdir(char *s);
char *host_details(void);
int is_slave(void);
void slave_open(void);
char *get_wordq(char *s,int n);
int slave_n(void);
char *mystrchrnc(const char *s,int c);
int dbf_nfree(void);
void ex_start(char *name);
void ex_end(char *name);
void slave_reindex(int t);
int group_match_none(void);
char *mystrstrnc(const char *s, const char *w);
char *str_decodelf(char *s);

char *ex_stat_name(int i);
int ex_stat_total(int i);
int ex_stat_n(void);
void slave_fixgroup(int grp, int from, int to, int range, int cflag, int mflag);
void slave_reload(void);
void slave_group_new(char *s, char *user, char *mod, char *title, int gid);
int token_quote(char *s, char *p[], int tmax);

void db_exit(void);
char *nntp_hostip(void);
void nntp_xxsetup(int x);
void ncmd_tell(int x);

int db_flush(void);
int db_idle(void); 
char *file_xov_new(void)  ;
char *file_xover(int grp);
void lib_setpaused(void);
void stats_show_changes(void);
char *lib_hostnamex(void);

void cmd_rules_group(int chan, char *gname, char *action);
void cmd_rules_trace(int chan, char *rulefile, char *fname);
void cmd_rules_reload(int chan);

char *big_get_chunk(int h, int from, int len, int *rlen);
void lib_grow_memory(void);
int lib_str_start(void);
int mem_total(void);
int nntp_nsockets(void);
void dns_clear(void);
char *lib_email_date(void);
char *lib_unescape(char *s);
char *lib_escape(char *s);
void *mykeep(int sz);
int nntp_curspeedout(int c);
char *mystrkeep(char *s);
int post_nsilent(void);
char *lib_timezone(void);
int nntp_getidle2(int *since);

enum {
	X_WRITEHIST,X_WRITEXOV,X_DBFLUSH,X_MCOMPACT
	,X_DBADD,X_DBFEED
	,X_FLUSH1,X_FLUSH10,X_FLUSH60,X_FLUSH180,X_FLUSH800
	,X_SLAVESND,X_CANCEL,X_DBOPEN
	,X_DBADDD
	,X_DB_SEEK
	,X_DB_WRITE
	,X_NNTP_WRITE
	,X_CHAN_READ
	,X_LOOP
	,X_NNTP_READBF
	,X_IHAVE_DATA
	,X_DOREST
	,X_CHREAD
	,X_IDATA
	,X_IHAVE_DO
	,X_DBDEL
	,X_XSORT
	,X_DBTRIM
	,X_BSORT
	,X_ART_GET
	,X_ART_SEND
	,X_UNCOMP
	,X_XOPEN
	,X_PART
	,X_PART_SEND
	,X_PART_EXPIRE
	,X_DB_ADD2
	,X_PART_CLEAR
	,X_PART_STORE
};
int part_check(char *subject, char *mid, char *item, int len);
void lib_mkfile(char *s);
FILE* myfopen(const char *filename, const char *mode) ;
void close_on_exec(int fd);
int spam_hostok(int chan);
int nocem_load(void);
void db_cancel_start(void);
int ncmd_dsearch_test(int chan, char *word, int n);
int is_silent(void);
void xstats_clear(void);
void emsg_close(void);
void db_flush_quick(void);
int post_article(int chan,String *anitem, char *s);
char *part_status(void);

char *get_word_long(char *s,int n); /*comp.os.vms 001 000 m*/
void stats_clear(void);
void nntp_accept(int hhh, char *ipnum, char *lclip);
char *lib_strdate2(int offset);
char *lib_samepath(char  *s, char *fname);
int part_hist(char *mid);
int stats_clock(void);
void stats_add(char *s, int start_time, int start_clock);
void stats_feed(char *name, int delay, int nreject, int npost, int ndup, double size, double ratio);
char *lib_trim_msg(char *s);
unsigned int lib_filesize(char *s);
void lib_dir_split(char *orig, char *dir, char *wild);
int strcmpnc(char *s1, char *s2);
char *lib_safe(char *s);
int xfclose(FILE *f);
int xopen(char *fname, int mode, int access);
char *lib_findfile(char *s);
FILE *xfopen(char *fname, char *mode);
int get_zone(char *s);
void lib_recheck_space(void);
int parsedate(char *s);
int lib_date(char *s);
char *lib_hour(void);
int lib_gmtime(void);
int lib_localtime(void);
struct tm *lib_split_time(int *t);
int str_hash(char *s, int max);
int strncmpnc(const char *s1, const char *s2, int len);
#ifdef VMS
char *strdup(char *s);
#endif
char *strupr(char *s);
char *strlwr(char *s);
int expire_quanta(void);
int suck_spawned_expire(void);
char *get_word(char *s, int n);
void cvt_numfile(int x, int *fg, int *fi);
void debug_setlvl(char *s);
int lib_msec(void);
char *sys_gdir(int g);
char *sys_gname(int g, int item);
char *sys_xname(int g, int item);
char *sys_iname(int g, int item);
char *sys_nname(int g, int item);
unsigned int file_length(int f);
char *ncpy(char *dst, char *src, int len);
int match_wild(char *wild, char *s);
char *get_rest(char *in, int n);
void lib_createdir(char *s);
char *lib_hostname(void);
char *lib_messageid(void);
char *lib_strdate(void);
void config_load(void);
char *config_get(int x);
char *config_getu(int x);
int token_split(char *src, char *p[], char *sep, int tmax);
int token_split2(char *src, char *p[], char *sep, int tmax);
int lib_exists(char *fname);
char *file_config(char *s);
char *file_hist(char *s);
char *file_work(char *s);
char *file_log(char *s);
char *file_spool(int n, char *s);  /* Spool disk n */
char *filter_status(void);

int lib_mail_file(char *to, char *subj, char *fname);
int lib_mail_msg(char *to, char *subj, char *fname);
char *lib_getlicense(void);
char *str_trimspace(char *s);

void mem_flush(void);
int open_log(char *fname, int access, int aa);
FILE *fopen_log(char *fname, char *mde);
int close_log(int handle);
int fclose_log(FILE *stream);
int lib_freespace(char *s);
int lib_checkspace(void);
char *lib_status(void);
void fix_address(char *in);
int lib_pused(void);
int is_expring(void);

char *expire_pile_groups(int p);
void dnews_manager_report(char *s);
String *dnews_status(void);
char *lib_now(void);
int diskuse_get(void);
int big_setgrow(int x);

int lib_gmt_to_local(int x);
int lib_local_to_gmt(int x);
void lib_tzinit(void);
char *lib_trim(char *s);
char *lib_trimtabs(char *s);
char *str_trimtabs(char *s);
int lib_append(char *src, char *dst);
char *feed_fname(char *name, char *ext);

int dnews_setkey(char *s);
int dnews_getkey(char *bf);
int dnews_checkkey(int x);
/*
#define lib_gmtime() my_lib_gmtime(__FILE__,__LINE__)
int my_lib_gmtime(char *file, int line);
*/
void debug_init(void);
int xclose(int x);
int lib_test_files(void);
void iffree(void *x);
void die(char *s);
int lib_locked(void);
String *nntp_show_dns(void);

int pf_open(void);
int pf_clear(void);
int pf_show(int chan) ;
int pf_age(int n);
int pf_addone(char *ip, int nadd);

int nt_setkey(char *s);
int nt_getkey(char *ex, int nchan);
int post_local(String *xitem, char *addr, int ismod);
void stats_finish(char *s);


/*RHP mac needs it too*/
#ifdef Mac
char *strdup(char *s);
#endif

/*RHP missed two prototypes*/
char *dirfix(char *s);
char *fast_unix(char *bf, int g, int item, char *name);


char *nt_pcname(void);
char *nt_other(void);
char *get_wordv(char *s,int n);
int lib_fixtime(time_t t);

#ifndef CLOCKS_PER_SEC
#define CLOCKS_PER_SEC	  1
#endif




String *lib_showmem(void);
void mem_log(int type, int amnt);
char *p_show_blockn(int n);
int spam_postdelay(char *ipnum);
int spam_postadd(char *ipnum);
void spam_postclear(void) ;
void spam_postage(void)	 ;
char *spam_postshow(void);

char *xstrdup(char *s);
void xstrfree(char *s);
void r_free(void *xx);
void *r_malloc(int sz);

char *mystrstr(const char *s1, const char *s2);
int ihavein_clear(int i);
void lib_doevents(void);
char *mystrchr(const char *s,int c);
char *new_make(int months, int users, int flags, char *host);
int new_test(char *k, char *host, int *users, int *flags, char *expires);
int db_rcheck(char *kin, char *host, int *users, int *flags, char *expires);
char *nocrlf(char *s);

/*#define mystrdup(x) d_mystrdup(x,__FILE__,__LINE__)
char *d_mystrdup(char *s, char *file, int line);
*/

enum {MSTATE_FAIL = -1, MSTATE_NORMAL, MSTATE_DONE};

char *big_name(int h);
int big_add(int h, char *s, int sz);
void big_close(int h);
int big_free(int h);
void big_fail(int h) ;
void big_init(int h);
int big_len(int h);
String *big_get(int h);


void pi_end(void);
int my_file_length(int f, int fl);
int myread(int handle, void *buffer, unsigned int count);
int mywrite(int handle, char *buffer, unsigned int count, int flen);
long mylseek(int handle, long offset, int origin);
int myopen(const char *fname, int oflag, int pmode, int gid, int num);
int myclose(int handle);


/* I think most are broken when used with long strings, both strstr and
strchr sometimes include a call to 'strlen' which kills performance  */
#ifndef GOODSTRSTR
#define BADSTRSTR TRUE
#define BADSTRCHR TRUE
#endif 

#ifdef BADSTRSTR
#define strstr mystrstr
#define strchr mystrchr
#endif


#endif

int pass_hash(int n, char *s);

int nocem_background(void);
int nocem_filter(String *item, char *grps[], int ngrps);
int pgp_verify(String *xitem, char *issuer);
int post_moderated(String *anitem) ;
void nocem_test(int chan, char *fname);
void set_expire_mode(int mode) ;
int get_expire_mode(void);
int is_spawn_expire(void);
int bit_isset(char *mask, int x);
void bit_set(char *mask, int x);
void bit_set2(char *mask, int x, int v);
int parts_subject(char *subject, char *subj, char *fname, int *parti, int *partmax);
int myfclosep( FILE **stream );
#define fclosep(xxx) myfclosep(&(xxx))
#define SPAM_BRIEF file_work("spam.brief")

void user_cache_free(void);
int user_cache_add(char * user, char *pass,char *groups[],int ngroups);
int user_cache_lookup(char *user, char *pass, char *groups[], int *ngroups, int maxg);
int filter_isok(char *item, int len);
void filter_close(void);
#ifdef WIN32
#define SPAWN_FILTER 1
#endif
#ifdef isunix
#define SPAWN_FILTER 1
#endif
