/*
 * Copyright (c) 1998,1999,2000  Kazushi (Jam) Marukawa
 * All rights of my changes are reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice in the documentation and/or other materials provided with
 *    the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

/* $Orig-Id: leafnode.h,v 1.13 1997/07/20 00:34:23 agulbra Exp $ */

#ifndef LEAFNODE_H
#define LEAFNODE_H

/* I wish the world were a happy place */
#ifndef TRUE
#define TRUE (1)
#endif
#ifndef FALSE
#define FALSE (0)
#endif

#include <sys/types.h>
#include <errno.h>
#include <sys/errno.h>

/* uncomment this if you get errors

extern int sys_nerr;
extern char* sys_errlist[];
extern int errno;

also complain to your vendor */

#define HAVE_STRDUP

#ifdef ultrix
#undef HAVE_STRDUP
#endif

#ifndef HAVE_STRDUP
char* strdup(const char*);
#endif


/* limits.h is supposed to contain PATH_MAX, we include sys/param.h too */
#include <limits.h>
#ifndef PATH_MAX
#include <sys/param.h>
#define PATH_MAX MAXPATHLEN
#endif

/* add LOG_NEWS where it doesn't exist */
#include <syslog.h>
#if !defined(LOG_NFACILITIES)
#error "Leafnode does not compile with BSD 4.2 syslog"
#endif
#if !defined(LOG_NEWS)
#define LOG_NEWS LOG_DAEMON
#endif
#if !defined(LOG_CONS)
#define LOG_CONS 0 /* if it isn't supported, make do without */
#endif

#include <stdio.h> /* FILE* */


/*
 * System values.  These values are default values.  User can change them
 * from configuration files.
 */

#define TIMEOUT_OPEN    (60U)	/* timeout for open nntp connection */
#define TIMEOUT_READ	(14400U)/* timeout for read from nntp */
#define TIMEOUT_LONG	(7)	/* don't fetch threads that nobody've read */
				/* in this many days */
#define TIMEOUT_SHORT	(2)	/* don't fetch threads that have been "read" */
				/* only once in this many days */
#define TIMEOUT_ACTIVE	(7)	/* don't fetch actives after this many days */

/* verbosity level, for fetch and later texpire */
extern int verbose;

/* handy malloc wrappers from util.c */

extern void* critmalloc(size_t, const char* message);
extern void* critrealloc(void*, size_t, const char* message);

/* handy string functions */

extern char* skipspaces(char*);
extern char* skipnonspaces(char*);
extern char* skipre(char*); /* skip Re:/RE^n:/re: */
extern void strlower(char*); /* lowerize all string */
extern int myfnmatch(const char* pattern, const char* str);

/* handy structure and functions to use char* as reuseable buffer */

struct string {
    int size;
    char* str;
};

extern void setstring(struct string* p, const char* str);

/* structure and functions to treat array of char* as a list of strings */

struct strlist {
    int size;
    int num;
    char** strarray;
};

extern void addstr(struct strlist* p, char* str);
extern void clearstrlist(struct strlist* p);

/* structure and functions to treat array of regex_t */

struct patterns {
    int num;
    int icase;
    void* patarray;
};

extern void initpatterns(struct patterns*, struct strlist* patlist, int icase);
extern int matchpatterns(struct patterns*, char*);
extern void clearpatterns(struct patterns*);


/* remove a message-ided article. */
extern void removearts(const char* msgid);

/* reads one header, regardless of length */
extern char* getaheader(FILE* f, const char* header);

/* newsgroup management */

struct newsgroup {
    struct newsgroup* left;
    struct newsgroup* right;
    unsigned long first;
    unsigned long last;
    unsigned long server;
    int alive;
    int newarticles;
    char* desc;
    char* name;
};


extern void nntpactive(void);
extern void correctnewsdesc(void);
extern void readserver(void);
extern void writeserver(void);

/* structure to treat articles */

struct article {
    struct newsgroup* ng;
    unsigned long art;
};

struct articles {
    int size;
    int num;
    struct article* arts;
};

/* servers structure */

struct server {
    char* servername;		/* upstream news server */
    char* viahost;		/* hostname to connect */
    int port;			/* port number to connect */
    char* preconnect;		/* string executed before make a connection */
    struct strlist newsgroups;	/* news groups will be fetched */
    struct strlist filteredngs;	/* news groups will be filtered */
    int postable;		/* the number of postable hosts in the servers
				   list if this server is postable.  Otherwise,
				   just 0 */
    char* username;		/* authinfo user name */
    char* password;		/* authinfo password */
    unsigned timeout_open;	/* timeout for open nntp connection */
    unsigned timeout_read;	/* timeout for read from nntp */
    struct server* next;
};

extern struct server* servers;

/* translation from message-id to article number, used in fetch and expire */
extern void clearidtree(void);
extern void insertmsgid(const char* msgid, unsigned long art);
extern unsigned long findmsgid(const char* msgid);

/* xover stuff */

struct xoverinfo {
    char* text;
    int mallocd;
    int exists;
};

extern struct xoverinfo* xoverinfo;
extern unsigned long xfirst;
extern unsigned long xlast;

/* return 1 if newsdesc is a legal news description, 0 else */
extern int legalnewsdesc(const char* newsdesc);
/* An rfc type date */
extern const char* rfctime(void);

/* changes (and optionally creates) directory */
extern int chdirgroup(const struct newsgroup* ng, int creatf);

/* the strings in config.c */

extern const char* spooldir;
extern const char* libdir;
extern const char* defspooldir;
extern const char* deflibdir;
extern const char* version;
extern const char* lockfile;
extern void initconfig(void);
extern const char* getconfigfname(void);
extern const char* getinfofname(void);
extern const char* getinfonewfname(void);
extern const char* getactivefname(const char* servername);
extern const char* getserverfname(const char* servername);
extern const char* getservernewfname(const char* servername);
extern const char* getinterestingdname(void);
extern const char* getinterestingngfname(const struct newsgroup* ng);
extern const char* getinterestingngdotfname(const struct newsgroup* ng);
extern const char* getoutgoingdname(void);
extern const char* getoutgoingfname(const char* fname);
extern const char* getfailedpostingsdname(void);
extern const char* getfailedpostingsfname(const char* fname);
extern const char* getngdname(const struct newsgroup* ng);
extern const char* getartfname(const struct newsgroup* ng,
			       const unsigned long art);
extern const char* getmsgiddname(const unsigned int hash);
extern const char* getmsgidfname(const char* msgid);


/* my name, and my naming myself */
extern char* fqdn;
extern void whoami(void);

/* timeout values */
extern long timeout_short;
extern long timeout_long;
extern long timeout_active;

/*
 * articles not read or written since this expire time get deleted generally.
 * articles in particular newsgroups are checked by groupexpire time
 * (struct articles is used to represent newsgroup name and time).
 */
extern time_t expire;
struct groupexpire {
    char* group;
    unsigned long xtime;
};

struct groupexpires {
    int size;
    int num;
    struct groupexpire* array;
};

extern void addexpire(struct groupexpires* p, const struct groupexpire* a);
extern struct groupexpires groupexpires;

/* max number of articles to read on one group in one go */
extern int artlimit;
/* max number of articles to read on one group the first time */
extern int initiallimit;
/* max dates of articles to read on one group in one go */
extern int maxold;
/* max lines of articles to read on one group in one go */
extern int maxlines;
/* min lines of articles to read on one group in one go */
extern int minlines;
/* max bytes of articles to read on one group in one go */
extern int maxbytes;
/* max the number of newsgroups which articles corss-posted to read on
   one group in one go */
extern int maxgroups;

/* subject pattern of article which will be filtered */
extern struct strlist killsubject;
extern struct strlist killsubjecti;
/* from pattern of article which will be filtered */
extern struct strlist killfrom;
extern struct strlist killfromaddress;

extern char last_command[1025];
extern char lineout[1025];

extern void readexpire(void);
extern unsigned long lookup_expire(char* group);
extern void free_expire(void);

/* read the config file */
extern void readconfig(void);

/* expire stuff */
extern struct newsgroup* getexpire(struct newsgroup*, char*);

/* strip more than one spaces */
extern void stripspace(char* p);

/* header infomation */
struct header_info {
    char* path;		/* strings for each header.  "XXX: " part is stripped */
    char* msgid;
    char* from;
    char* newsgroups;
    char* subject;
    char* date;
    char* references;
    char* lines;
    char* bytes;
    char* xref;
    char* supersedes;
    int n_lines;	/* exactly counted lines */
    int n_bytes;	/* exactly counted bytes as terminated by CRLF */
    char* all;		/* all headers except xref */
};

/* activutil.c */
extern struct newsgroup* active;
extern void insertgroup(const char* name,
			unsigned long first, unsigned long last,
			const char* desc, unsigned long server);
extern struct newsgroup* findgroup(const char* name);
extern void lockactive(void);
extern void unlockactive(void);
extern void readactive(void);
extern void writeactive(void);

/* artutil.c */
extern FILE* buildpseudoart(const char* grp);
extern struct header_info* parse_header(FILE* fp);
extern struct header_info* parse_all(FILE* fp);
extern void free_header_info(struct header_info* p);
extern int storep(unsigned long artno, const struct header_info*);
extern void store(const char* filename, FILE* filehandle,
		  char* newsgroups, const struct header_info*);

/* nntputil.c */
extern FILE* nntpin;
extern FILE* nntpout;
extern void putaline(void);
extern int nntpreply(void);
extern int nntpconnect(void);
extern int nntpconnectauth(void);
extern void nntpclose(void);
extern int nntpreconnect(void);
extern char* getaline(FILE* f);

/* xnntputil.c */
extern void addarticle(struct articles* p, const struct article* a);
extern struct articles* findarticles(const char* msgid);

/* xoverutil.c */
extern int legalxoverline(const char* xover);
extern char* getxoverline(const char* filename);
extern int getxover(void);

/* util.c */
extern void mysyslog(int priority, const char* message, ...);

#endif


syntax highlighted by Code2HTML, v. 0.9.1