/* :ts=4
 * Usenet header definitions (see ARPA Internet RFCs 1036 nee 850 & 822;
 *	for a second opinion, see The Hideous Name by Pike & Weinberger).
 *
 * Headers are parsed and modified and copied in one pass.
 * Nevertheless, the code is in pieces: hdrdefs.c, hdrcommon.c,
 * hdrparse.c, hdrmunge.c.
 *
 *	$Log$
 */

#include <stdio.h>
#ifdef unix
# include <sys/types.h>
#endif /* unix */
#include "libc.h"
#include "news.h"
#include "headers.h"
#include "hdrint.h"			/* may define "CONST" or REALSTDC */
#ifdef REALSTDC
# include <stddef.h>		/* defines offsetof */
#endif						/* REALSTDC */

#if 0
#ifndef offsetof
#define offsetof(type, mem) ((char *)&((type *)NULL)->mem - (char *)NULL)
#endif
#endif

/* "mandatory" headers (also From:, Date:) */
static CONST char msgnm[] =	"Message-ID:";	/* for rejection */
static CONST char ngsnm[] =	"Newsgroups:";	/* filing, clone for Xref */
static CONST char pathnm[] =	"Path:";	/* rejection, extend (damn) */
static CONST char subjnm[] =	"Subject:";	/* for ctl. msgs. */

/* optional headers */
static CONST char appnm[] =	"Approved:";	/* for mod. groups */
static CONST char ctlnm[] =	"Control:";	/* ctl. msg.; NCMP */
static CONST char etctlnm[] = "Also-Control:";	/* hybrid ctl. msg.; NCMP */
static CONST char expnm[] =	"Expires:";	/* for history */
static CONST char distrnm[] =	"Distribution:";	/* for transmission */
static CONST char sendnm[] =	"Sender:";	/* for mod. groups */
static CONST char xrefnm[] =	"Xref:";	/* to *replace* (damn!)*/

/* obsolete "useful" headers */
static CONST char artnm[] =	"Article-I.D.:";	/* obs. Message-ID: */

/* obsolete useless headers: delete them all on contact */
static CONST char datercvnm[] = "Date-Received:";
static CONST char rcvnm[] =	"Received:";	/* obsolete Date-Received: */
static CONST char postnm[] =	"Posted:";	/* obsolete Date: */
static CONST char postversnm[] = "Posting-Version:";
static CONST char rlyversnm[] = "Relay-Version:";
static CONST char illobjnm[] = "Illegal-Object:";	/* zmailer bitching */

static CONST struct hdrdef msghdr = {
	msgnm, STRLEN(msgnm), offsetof(struct headers, h_msgid) };
static CONST struct hdrdef ngshdr = {
	ngsnm, STRLEN(ngsnm), offsetof(struct headers, h_ngs) };
CONST struct hdrdef pathhdr = {
	pathnm, STRLEN(pathnm), offsetof(struct headers, h_path) };
static CONST struct hdrdef subjhdr = {
	subjnm, STRLEN(subjnm), offsetof(struct headers, h_subj) };

static CONST struct hdrdef apphdr = {
	appnm, STRLEN(appnm), offsetof(struct headers, h_approved) };
static CONST struct hdrdef ctlhdr = {					/* NCMP */
	ctlnm, STRLEN(ctlnm), offsetof(struct headers, h_ctlcmd) };	/* NCMP */
static CONST struct hdrdef etctlhdr = {					/* NCMP */
	etctlnm, STRLEN(etctlnm), offsetof(struct headers,h_etctlcmd) };/*NCMP*/
static CONST struct hdrdef exphdr = {
	expnm, STRLEN(expnm), offsetof(struct headers, h_expiry) };
static CONST struct hdrdef distrhdr = {
	distrnm, STRLEN(distrnm), offsetof(struct headers, h_distr) };
static CONST struct hdrdef sendhdr = {
	sendnm, STRLEN(sendnm), offsetof(struct headers, h_sender) };
CONST struct hdrdef xrefhdr = { xrefnm, STRLEN(xrefnm), -1 };

static CONST struct hdrdef arthdr = {
	artnm, STRLEN(artnm), offsetof(struct headers, h_artid) };

static CONST struct hdrdef datrcvhdr = { datercvnm, STRLEN(datercvnm), -1 };
static CONST struct hdrdef rcvhdr = { rcvnm, STRLEN(rcvnm), -1 };
static CONST struct hdrdef psthdr = { postnm, STRLEN(postnm), -1 };
static CONST struct hdrdef pstvrshdr = { postversnm, STRLEN(postversnm),-1};
static CONST struct hdrdef rlyvrshdr = { rlyversnm, STRLEN(rlyversnm), -1 };
static CONST struct hdrdef illobjhdr = { illobjnm, STRLEN(illobjnm), -1 };

	/* these are parsed into a struct headers */
CONST struct hdrdef *parsehdrs[] = {
	&msghdr,
	&arthdr,		/* obsolete */
	&ngshdr,
	&pathhdr,		/* modified by hdrmunge.c (emithdr()) */
	&subjhdr,
	/* start optional headers */
	&apphdr,
	&ctlhdr,		/* NCMP */
	&etctlhdr,		/* NCMP */
	&distrhdr,
	&exphdr,
	&sendhdr,
	NULL
};
/*
 * the following noxious headers are deleted on contact because neighbours
 * still send them and they are big.  in an ideal world, they wouldn't be
 * sent and thus we wouldn't need to delete them.
 * It is tempting to delete Article-I.D.: too, but it may be too soon for that.
 */
CONST struct hdrdef *hdrvilest[] = {
	&xrefhdr,		/* regenerated by fileart() if needed */
	&datrcvhdr,
	&rcvhdr,
	&psthdr,
	&pstvrshdr,
	&rlyvrshdr,
	&illobjhdr,
	NULL,
};

boolean headdebug = NO;

void
hdrdebug(state)
int state;
{
	headdebug = state;
}

void
hdrinit(hdrs)			/* zero all elements of hdrs */
register struct headers *hdrs;
{
	hdrs->h_subj = NULL;
	hdrs->h_ngs = NULL;
	hdrs->h_distr = NULL;
	hdrs->h_ctlcmd = NULL;		/* NCMP */
	hdrs->h_etctlcmd = NULL;	/* NCMP */
	hdrs->h_approved = NULL;
	hdrs->h_msgid = NULL;
	hdrs->h_artid = NULL;
	hdrs->h_expiry = NULL;
	hdrs->h_path = NULL;
	hdrs->h_sender = NULL;
}

void
freeheaders(hdrs)		/* free (assumed) malloced storage */
register struct headers *hdrs;
{
	nnfree(&hdrs->h_subj);
	nnfree(&hdrs->h_ngs);
	nnfree(&hdrs->h_distr);
	nnfree(&hdrs->h_ctlcmd);	/* NCMP */
	nnfree(&hdrs->h_etctlcmd);	/* NCMP */
	nnfree(&hdrs->h_approved);
	nnfree(&hdrs->h_msgid);
	nnfree(&hdrs->h_artid);
	nnfree(&hdrs->h_expiry);
	nnfree(&hdrs->h_path);
	nnfree(&hdrs->h_sender);
}
