1 /*
2  * Usenet header definitions (see ARPA Internet RFCs 1036 nee 850 & 822;
3  *	for a second opinion, see The Hideous Name by Pike & Weinberger).
4  *
5  * Headers are parsed and modified and copied in one pass.
6  * Nevertheless, the code is in pieces: hdrdefs.c, hdrparse.c, hdrmunge.c.
7  */
8 
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <stddef.h>		/* must define offsetof */
12 #include <sys/types.h>
13 
14 #include "libc.h"
15 #include "news.h"
16 #include "headers.h"
17 #include "hdrint.h"
18 
19 /* required headers */
20 static char msgnm[] =	"Message-ID:";	/* rejection of dup.s */
21 static char ngsnm[] =	"Newsgroups:";	/* filing, clone for Xref */
22 static char pathnm[] =	"Path:";	/* rejection, extend (damn) */
23 static char subjnm[] =	"Subject:";	/* for ctl. msgs. */
24 static char datenm[] =	"Date:";	/* rejection of stale art.s */
25 static char fromnm[] =	"From:";	/* only required; no use */
26 
27 /* optional headers */
28 static char appnm[] =	"Approved:";	/* mod. groups */
29 static char ctlnm[] =	"Control:";	/* ctl. msg.; NCMP */
30 static char etctlnm[] =	"Also-Control:"; /* hybrid ctl. msg.; NCMP */
31 static char expnm[] =	"Expires:";	/* history */
32 static char distrnm[] =	"Distribution:";	/* transmission */
33 static char sendnm[] =	"Sender:";	/* mod. groups */
34 static char xrefnm[] =	"Xref:";	/* to *replace* (damn!)*/
35 
36 /* obsolete "useful" headers */
37 static char artnm[] =	"Article-I.D.:";	/* obs. Message-ID: */
38 
39 static struct hdrdef msghdr = {
40 	msgnm, STRLEN(msgnm), offsetof(struct headers, h_msgid) };
41 static struct hdrdef ngshdr = {
42 	ngsnm, STRLEN(ngsnm), offsetof(struct headers, h_ngs) };
43 struct hdrdef pathhdr = {
44 	pathnm, STRLEN(pathnm), offsetof(struct headers, h_path) };
45 static struct hdrdef subjhdr = {
46 	subjnm, STRLEN(subjnm), offsetof(struct headers, h_subj) };
47 static struct hdrdef datehdr = {
48 	datenm, STRLEN(datenm), offsetof(struct headers, h_date) };
49 static struct hdrdef fromhdr = {
50 	fromnm, STRLEN(fromnm), offsetof(struct headers, h_from) };
51 
52 static struct hdrdef apphdr = {
53 	appnm, STRLEN(appnm), offsetof(struct headers, h_approved) };
54 static struct hdrdef ctlhdr = {					/* NCMP */
55 	ctlnm, STRLEN(ctlnm), offsetof(struct headers, h_ctlcmd) }; /* NCMP */
56 static struct hdrdef etctlhdr = {				/* NCMP */
57 	etctlnm, STRLEN(etctlnm), offsetof(struct headers, h_etctlcmd) }; /* NCMP */
58 static struct hdrdef exphdr = {
59 	expnm, STRLEN(expnm), offsetof(struct headers, h_expiry) };
60 static struct hdrdef distrhdr = {
61 	distrnm, STRLEN(distrnm), offsetof(struct headers, h_distr) };
62 static struct hdrdef sendhdr = {
63 	sendnm, STRLEN(sendnm), offsetof(struct headers, h_sender) };
64 struct hdrdef xrefhdr = {
65 	xrefnm, STRLEN(xrefnm), offsetof(struct headers, h_xref) };
66 
67 static struct hdrdef arthdr = {
68 	artnm, STRLEN(artnm), offsetof(struct headers, h_artid) };
69 
70 /* these are parsed into a struct headers */
71 
72 /* putting opthdrs first makes some broken compilers happier */
73 hdrlist opthdrs = {
74 	&arthdr,		/* obsolete */
75 	&apphdr,
76 	&ctlhdr,		/* NCMP */
77 	&etctlhdr,		/* NCMP */
78 	&distrhdr,
79 	&exphdr,
80 	&sendhdr,
81 	&xrefhdr,		/* for -b only */
82 	NULL
83 };
84 hdrlist reqdhdrs = {
85 	&msghdr,
86 	&ngshdr,
87 	&pathhdr,		/* modified by hdrmunge.c (emithdr()) */
88 	&subjhdr,
89 	&datehdr,
90 	&fromhdr,
91 	NULL
92 };
93 
94 /*
95  * the following headers are deleted on contact because neighbours
96  * still send them.  in an ideal world, they wouldn't be
97  * sent and thus we wouldn't need to delete them.
98  */
99 hdrlist hdrvilest = {
100 	&xrefhdr,		/* regenerated by fileart() if needed */
101 	NULL,
102 };
103 
104 boolean headdebug = NO;
105 
106 void
hdrdebug(state)107 hdrdebug(state)
108 int state;
109 {
110 	headdebug = state;
111 }
112 
113 void
hdrinit(hdrs)114 hdrinit(hdrs)			/* zero all elements of hdrs */
115 register struct headers *hdrs;
116 {
117 	hdrs->h_subj = NULL;
118 	hdrs->h_ngs = NULL;
119 	hdrs->h_distr = NULL;
120 	hdrs->h_ctlcmd = NULL;		/* NCMP */
121 	hdrs->h_etctlcmd = NULL;	/* NCMP */
122 	hdrs->h_approved = NULL;
123 	hdrs->h_msgid = NULL;
124 	hdrs->h_artid = NULL;
125 	hdrs->h_expiry = NULL;
126 	hdrs->h_path = NULL;
127 	hdrs->h_sender = NULL;
128 	hdrs->h_from = NULL;
129 	hdrs->h_date = NULL;
130 	hdrs->h_xref = NULL;
131 }
132 
133 void
freeheaders(hdrs)134 freeheaders(hdrs)		/* free (assumed) malloced storage */
135 register struct headers *hdrs;
136 {
137 	nnfree(&hdrs->h_subj);
138 	nnfree(&hdrs->h_ngs);
139 	nnfree(&hdrs->h_distr);
140 	nnfree(&hdrs->h_ctlcmd);	/* NCMP */
141 	nnfree(&hdrs->h_etctlcmd);	/* NCMP */
142 	nnfree(&hdrs->h_approved);
143 	nnfree(&hdrs->h_msgid);
144 	nnfree(&hdrs->h_artid);
145 	nnfree(&hdrs->h_expiry);
146 	nnfree(&hdrs->h_path);
147 	nnfree(&hdrs->h_sender);
148 	nnfree(&hdrs->h_from);
149 	nnfree(&hdrs->h_date);
150 	nnfree(&hdrs->h_xref);
151 }
152