File: mkrfcmsg.c

package info (click to toggle)
ifmail 2.14tx8.10-32
  • links: PTS, VCS
  • area: main
  • in suites: forky, trixie
  • size: 4,056 kB
  • sloc: ansic: 30,328; perl: 4,955; yacc: 839; makefile: 716; sh: 424; cpp: 235; lex: 206; awk: 24
file content (1275 lines) | stat: -rw-r--r-- 35,238 bytes parent folder | download | duplicates (11)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
/* ### Modified by P.Saratxaga on 12 Sep 1995 ###
 * - some hacking to handle non standard MSGID's
 * - PUDDLE_GATE repairs the Newsgroups lines from the gate used at
 *   puddle.fidonet.org, which puts spaces in Newsgroups header
 * - Added PCBOARD_GATE
 * - added ALLOW_CONTROL and charset handling
 * - added FORCE_REPYLTO_LOCAL
 * - added charset support (see Changelog for details)
 * - corrected a bug about ^aRFCID
 * - added recognition for "^aMSGID <localid@some.where.org> 0124587" created
 *   by fidogate.
 * - No "X-Comment-To: " is generated if the value is "All"
 * - added #ifdef RNEWSB; when compiled with it, the result of ifnews -N is an
 *   rnews readable file.
 * - REFERENCES_MSC96 to compile whith Marc Schaeffer's ref_interface.h
 */
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <time.h>
#include <ctype.h>
#include "lutil.h"
#include "xutil.h"
#include "mkrfcmsg.h"
#include "ftnmsg.h"
#include "rfcmsg.h"
#include "areas.h"
#include "falists.h"
#include "config.h"
#include "needed.h"
#include "charset.h"
#include "charconv.h"
#include "mime.h"
#ifdef REFERENCES_MSC96
#include "ref_interface.h"
extern ref_private_t *ref_dbase;
#endif
#ifdef RESTAMP_OLD_POSTINGS
extern long restampolder;
#endif

#define BOUNDARY 79
#define MAXPATH 73

#ifndef LEVEL
#define LEVEL	1
#endif

int num_echo=0,num_mail=0;
extern char *version;
extern FILE *nb;
extern faddr pktfrom;
extern int notransports;

extern char *rfcdate(time_t);
extern unsigned INT32 sequencer(void);
extern unsigned INT16 crc(char*);
extern char *lookup(char *);
extern char *backalias(faddr *);
#ifdef GATEBAU_MSGID
extern unsigned long crcgatebau(char*);
char *gatebaudate(time_t);
#endif

extern FILE *expipe(char*,char*,char*);
extern int exclose(FILE*);
extern char *compose_flags(int,char *);
extern int pgpsigned;
#ifdef DIRTY_CHRS
extern int dirtyoutcode;
#endif

#ifdef GATEBAU_MSGID
char *rfcmsgid(msgid,bestaka,mode)
char *msgid;
faddr *bestaka;
int mode; /* 0=ifmail-style, 1=gatebau-style */
#else
char *rfcmsgid(msgid,bestaka)
char *msgid;
faddr *bestaka;
#endif
{
	static char *buf,*p,*q,*r;
#ifdef REFERENCES_MSC96
	char buf2[1024];
#endif
	unsigned long id=0L;
	faddr *ta=NULL;

/* +40 for the additionnal stuff we need to write, should be enough */
	buf=xmalloc(strlen(msgid)+40);

	if ((r=strrchr(msgid,'\n'))) *r='\0'; 
/* sometimes there is "^aMSGID: 1:23/45@@domain 152ad589" */
	if ((p = strstr(msgid, "@@"))) {
#ifdef GATEBAU_MSGID
		if (mode==1) {
			sprintf(buf,"<MSGID_%s@fidonet.org>",
				qp_encode(msgid,1));
			if (r) *r='\n';
			return buf;
		} else {
#endif
			*p='\0';
			strcat(msgid, p+1);
#ifdef GATEBAU_MSGID
		}
#endif
	}
/* other times there is "^aMSGID: 1:23/45@ 152ad589" */
	else if ((p = strstr(msgid,"@ "))) {
#ifdef GATEBAU_MSGID
		if (mode==1) {
			sprintf(buf,"<MSGID_%s@fidonet.org>",
				qp_encode(msgid,1));
			if (r) *r='\n';
			return buf;
		} else {
#endif
			*p='\0';
			strcat(msgid,p+1);
#ifdef GATEBAU_MSGID
		}
#endif
	}
/* here we have a parseable address */
	if ((p=strrchr(msgid,' ')))
	{
		*p='\0';
		sscanf(p+1,"%lx",&id);
		ta=parsefnode(msgid);
		*p=' ';
	}
	if (id != 0L)
	{
#ifdef GATEBAU_MSGID
		/* if we only check for (ta) a Message-ID like
		 * <123456.7890@internet.domain> will be recognized as
		 * a fidonet one (ta->node=123456, ta->point=7890,
		 * ta->domain="internet", but ta->net=0) which obviously
		 * isn't the case. By cheking also (ta->net) we avoid that
		 */
		if ((ta) && (ta->net) && (mode==1))
		{
			if (((ta->zone) > 0) && ((ta->zone) < 7))
				sprintf(buf,"<MSGID_%s@fidonet.org>",
					qp_encode(msgid,1));

			else sprintf(buf,"<MSGID_%s@%s.ftn>",
					qp_encode(msgid,1),
					(bestaka->domain)?bestaka->domain:"nodomain");
		}
		else
#endif
		/* if we only check for (ta) a Message-ID like
		 * <123456.7890@internet.domain> will be recognized as
		 * a fidonet one (ta->node=123456, ta->point=7890,
		 * ta->domain="internet", but ta->net=0) which obviously
		 * isn't the case. By cheking also (ta->net) we avoid that
		 */
		if ((ta) && (ta->net))
		{
			sprintf(buf,"<%lu@%s.ftn>",id,ascinode(ta,0x1f));
#ifdef REFERENCES_MSC96
			if (refdbm)
			{
				sprintf(buf2,"%s",buf);
				ref_get(ref_dbase,buf2,buf,1024);
			}
#endif
		}
		else
		{
			p=xstrcpy(msgid);
			if ((q=strchr(p,' '))) *q='\0';
/* ### Modified by P.Saratxaga on 18 Aug 1995 */
			if (strstr(p,"@")) {
	/* "mid__<local@domain>" are generated by gigo */
				if (!strncmp(p,"mid__<",6))
				{
					sprintf(buf,"%s",p+6);
					while ((q=strstr(buf,">_<")))
						*(q+1)=' ';
				}
	/* "mid__local@domain" are also generated by gigo */
				else if (!strncmp(p,"mid__",5))
					sprintf(buf,"<%s>",p+5);
	/* "wgmid$<local@domain>" */
				else if (!strncmp(p,"wgmid$<",7))
					sprintf(buf,"%s",p+6);
	/* in case we have "<local@domain>" */
			        else if (!strncmp(p,"<",1))
					sprintf(buf,"%s",p);
	/* or "local@domain" */
				else sprintf(buf,"<%s>",p);
				while ((q = strchr(buf, '@')) != strrchr(buf, '@'))
				{
					/* we (still) have more than one @ */
					*q = '%';
				}
			}
			else
			{
				sprintf(buf,"<%lu@%s>",id,p);
#ifdef REFERENCES_MSC96
				if (refdbm)
				{
					sprintf(buf2,"%s",buf);
					ref_get(ref_dbase,buf2,buf,1024);
				}
#endif
			}
			free(p);
		}
	}
#ifdef GATEBAU_MSGID
	else if (mode==1)
	{
		sprintf(buf,"<MSGID_%s@fidonet.org>",
			qp_encode(msgid,1));
	}
#endif
	else
	{
		sprintf(buf,"<%lu@%s.ftn>",(unsigned long)sequencer(),
			ascinode(bestaka,0x1f));
	}
	tidy_faddr(ta);
	if (r) *r='\n';
	return buf;
}

/* check address for localness, substitute alises and replace it *in place* */

void substitute(buf)
char *buf;
{
	faddr *fa;
	char *l,*r,*p=NULL;
	int inquotes,inbrackets;

	debug(6,"to address before subst: \"%s\"",buf);
	if ((l=strchr(buf,'<')) && (r=strchr(buf,'>')) && (l < r))
	{
		l++;
		*r='\0';
	}
	else l=buf;
	while (*l == ' ') l++;
	for (r=l,inquotes=0,inbrackets=0;*r;r++) switch (*r)
	{
	case '"':	inquotes=(!inquotes); break;
	case ',':
	case ' ':	if (!inquotes && !inbrackets) *r='\0'; break;
	case '(':	if (!inquotes) inbrackets++; break;
	case ')':	if (!inquotes && inbrackets) inbrackets--; break;
	default:	break;
	}
	if ((fa=parsefaddr(l)))
	{
		debug(6,"it is an ftn address: %s",ascfnode(fa,0x7f));
		if (is_local(fa))
		{
			debug(6,"it is local");
			sprintf(buf,"%s",fa->name);
			if (!strchr(buf,'@') && (p=strrchr(buf,'%')))
				*p='@';
			if (!strchr(buf,'@'))
			{
				if ((p=lookup(buf)))
					strcpy(buf,p);
				else if (!strcasecmp(buf,"sysop"))
					strcpy(buf,"postmaster");	
				else
					sprintf(buf,"%s",ascinode(fa,0x7f));
			}
		}
		else
		{
			debug(6,"it is not local");
			sprintf(buf,"%s",ascinode(fa,0x7f));
		}
		tidy_faddr(fa);
	}
	else 
	{
		debug(6,"it is not ftn address");
		for (r=buf;*l;l++,r++) *r=*l;
		*r='\0';
	}
	if (buf[0] == '\0') strcpy(buf,"postmaster");
	debug(6,"to address after  subst: \"%s\"",buf);
	return;
}

/* from, to, subj, orig, mdate, flags, file, offset, kluges */

int mkrfcmsg(f,t,subj,orig,mdate,flags,fp,endoff,kmsg)
faddr *f,*t;
char *subj,*orig;
time_t mdate;
int flags;
FILE *fp;
off_t endoff;
rfcmsg *kmsg;
{
	char buf[BUFSIZ];
	char *p,*q,*l,*r,*b;
	char *newsgroup=NULL,*distribution=NULL;
	int incode, outcode;
	char *moderator=NULL;
	int modtype=0;
	char c;
	int count,rrq;
	rfcmsg *msg=NULL,*tmsg;
	FILE *pip;
	struct stat stbuf;
	int newsmode,pass,rc;
	faddr *ta,*bestaka;
	fa_list *ftnpath=NULL,*tfa,*rlist;
	time_t now;
	int lines;
	char lineshdr[16];
	moderator_list *tmpml;
#ifdef GATEBAU_MSGID
	int gatebaumode=0;
#endif

	incode = outcode = CHRS_NOTSET;

/*	if ((magicname == NULL) ||
	    ((t->name) && (strcasecmp(magicname,t->name) == 0))) */
		msg=parsrfc(fp);
	bestaka=bestaka_s(f);

	p=hdr("CHRS",kmsg);
	if (p == NULL) p=hdr("CHARSET",kmsg);
	if (p == NULL) p=hdr("CODEPAGE",kmsg);
	if (p) outcode=readchrs(p);
	else
#ifdef STUPID_CHRS_DETECT
                outcode=defaultftnchar;
#else
	{
		p=hdr("Content-Type",msg);
		if (p == NULL) p=hdr("RFC-Content-Type",kmsg);
		if (p == NULL) p=hdr("Content-Type",kmsg);
		if (p) outcode=readcharset(p);
#ifndef JE
		else if ((hdr("Message-ID",msg)) ||
			 (hdr("RFC-Message-ID",kmsg)) ||
			 (hdr("Message-ID",kmsg)) ||
			 (hdr("RFCID",kmsg)) ||
			 (hdr("ORIGID",kmsg)) ||
			 ((hdr("MSGID",kmsg)) &&
#ifdef GATEBAU_MSGID
			  (!chkftnmsgid(rfcmsgid(hdr("MSGID",kmsg),bestaka,gatebaumode)))))
#else
			  (!chkftnmsgid(rfcmsgid(hdr("MSGID",kmsg),bestaka)))))
#endif
				outcode=defaultrfcchar;
		else outcode=defaultftnchar;
#endif
	}
#endif
#ifdef TERMAIL_HACK
	p=hdr("PID",kmsg);
	if ((p) && (!strncmp(p,"TerMail",7)) && (outcode==defaultrfcchar))
		outcode=defaultftnchar;
#endif
#ifdef DIRTY_CHRS
	if (dirtyoutcode != CHRS_NOTSET)
		outcode=dirtyoutcode;
#endif

	if (pgpsigned) incode=outcode;
/*	else incode=getincode(outcode); */
	                                
	if (kmsg && !strcmp(kmsg->key,"AREA"))
	{
		ngdist(kmsg->val,&newsgroup,&distribution,&moderator,&modtype);
#ifdef JE
		if (!pgpsigned) areacharset(kmsg->val, &incode, &outcode);
#endif
#ifdef GATEBAU_MSGID
		gatebaumode=areagatebau(kmsg->val);
#endif
		if (!newsgroup)
		{
			tidyrfc(msg);
			return 0;
		}
		newsmode=1;
		if ((modtype==1) &&
		    (!hdr("Approved",msg)) &&
		    (!hdr("RFC-Approved",kmsg)) &&
		    (!hdr("Approved",kmsg))) 
			newsmode=0;
	}
	else newsmode=0;
	debug(5,"Got %s message",newsmode?"echo":"netmail");
	if (outcode==CHRS_NOTSET)
#ifdef STUPID_CHRS_DETECT
                outcode=defaultftnchar;
#else
	{
		if ((hdr("Message-ID",msg)) ||
		    (hdr("RFC-Message-ID",kmsg)) ||
		    (hdr("Message-ID",kmsg)) ||
		    (hdr("RFCID",kmsg)) ||
		    (hdr("ORIGID",kmsg)) ||
		    ((hdr("MSGID",kmsg)) &&
#ifdef GATEBAU_MSGID
		     (!chkftnmsgid(rfcmsgid(hdr("MSGID",kmsg),bestaka,gatebaumode)))))
#else
		     (!chkftnmsgid(rfcmsgid(hdr("MSGID",kmsg),bestaka)))))
#endif
			outcode=defaultrfcchar;
		else outcode=defaultftnchar;
	}
#endif
	if (pgpsigned) incode=outcode;
	else if (incode==CHRS_NOTSET) incode=getincode(outcode);

	/* fsc-0038 defines "^aDOMAIN: othernet 99:12/34 fidonet 2:293/2219" */
	if ((p=hdr("DOMAIN",kmsg)) && (!strchr(p,'@')))
	{
		strncpy(buf,p,sizeof(buf)-1);
		buf[sizeof(buf)-1]='\0';
		l=strtok(buf," \n");
		p=strtok(NULL," \n");
		r=strtok(NULL," \n");
		q=strtok(NULL," \n");
		if ((ta=parsefnode(p)))
		{
			t->point=ta->point;
			t->node=ta->node;
			t->net=ta->net;
			t->zone=ta->zone;
			tidy_faddr(ta);
		}
		t->domain=xstrcpy(l);
		if ((ta=parsefnode(q)))
		{
			f->point=ta->point;
			f->node=ta->node;
			f->net=ta->net;
			f->zone=ta->zone;
			tidy_faddr(ta);
		}
		f->domain=xstrcpy(r);
	}
	else if ((p=hdr("INTL",kmsg)))
	{
		strncpy(buf,p,sizeof(buf)-1);
		buf[sizeof(buf)-1]='\0';
		l=strtok(buf," \n");
		r=strtok(NULL," \n");
		if ((ta=parsefnode(l)))
		{
			t->point=ta->point;
			t->node=ta->node;
			t->net=ta->net;
			t->zone=ta->zone;
			if (ta->domain)
			{
				if (t->domain) free(t->domain);
				t->domain=ta->domain;
				ta->domain=NULL;
			}
			tidy_faddr(ta);
		}
		if ((ta=parsefnode(r)))
		{
			f->point=ta->point;
			f->node=ta->node;
			f->net=ta->net;
			f->zone=ta->zone;
			if (ta->domain)
			{
				if (f->domain) free(f->domain);
				f->domain=ta->domain;
				ta->domain=NULL;
			}
			tidy_faddr(ta);
		}
	}

        /* fidogate generates "^aDOMAIN: Z2@fidonet" */
        if ((f->domain==NULL) && ((p=hdr("DOMAIN",kmsg)) && (q=strchr(p,'@'))))
        {
                *q='\0';
             /* f->zone=atoi(p+1); */
                f->domain=xstrcpy(q+1);
                *q='@';
        }

	if ((p=hdr("FMPT",kmsg))) f->point=atoi(p);
	if ((p=hdr("TOPT",kmsg))) t->point=atoi(p);

	debug(5,"final from: %s",ascfnode(f,0x1f));
	debug(5,"final   to: %s",ascfnode(t,0x1f));

	if (!newsmode)
	{
		p=hdr("Resent-To",msg);
		if (p == NULL) p=hdr("To",msg);
		if (p == NULL) p=hdr("RFC-Resent-To",kmsg);
		if (p == NULL) p=hdr("RFC-To",kmsg);
	/*	if (p && is_local(t)) */
		if ((p) && (is_local(t)) && ((magicname == NULL) ||
		        ((t->name) && (strcasecmp(magicname,t->name) == 0))))
		{
			while (*p == ' ') p++;
			strncpy(buf,p,sizeof(buf)-1);
			if (*(p=buf+strlen(buf)-1) == '\n') *p='\0';
		}
		else if (modtype==1)  sprintf(buf,"%s",moderator);
		else sprintf(buf,"%s",ascinode(t,0x7f));
		substitute(buf);
		loginf("mail from %s to %s",ascfnode(f,0x7f),buf);
	}

	if ((newsmode) && (nb == NULL))
	{
		if (notransports)
		{
			nb=fopen(tempnam("/tmp/ifmail","N."),"w");
#ifndef RNEWSB
			fprintf(nb,"#!/bin/sh\n%s <<__EOF__\n",S(rnews));
#endif
		}
		else
		{
			setenv("UU_MACHINE",ascinode(&pktfrom,0x0f),1);
			nb=expipe(rnews,NULL,NULL);
			unsetenv("UU_MACHINE");
		}
		if (nb == NULL)
		{
			logerr("$Could non open (pipe) output for news");
			tidyrfc(msg);
			return 2;
		}
	}

	if (newsmode)
	{
		if ((pip=tmpfile()) == NULL)
		{
			logerr("$Cannot open second temporary file");
			tidyrfc(msg);
			return 3;
		}
	}
	else
	{
#ifdef ALLOW_RETURNPATH
		p=hdr("Return-Path",msg);
		if (p == NULL) p=hdr("RFC-Return-Path",kmsg);
		if (p == NULL) p=hdr("Return-Path",kmsg);
		if (p)
		debug(2,"expipe(\"%s\",\"%s\",\"%s\")",
			S(sendmail),p,buf);
		else
#endif
		debug(2,"expipe(\"%s\",\"%s\",\"%s\")",
			S(sendmail),ascinode(f,0x7f),buf);
		if (notransports)
		{
			pip=fopen(tempnam("/tmp/ifmail","M."),"w");
#ifdef ALLOW_RETURNPATH
		if (p)
			fprintf(pip,"#!/bin/sh\nF=%s\nT=%s\n%s <<__EOF__\n",
				p,buf,S(sendmail));
		else
#endif
			fprintf(pip,"#!/bin/sh\nF=%s\nT=%s\n%s <<__EOF__\n",
				ascinode(f,0x7f),buf,S(sendmail));
		}
#ifdef ALLOW_RETURNPATH
		else if (p)
			pip=expipe(sendmail,p,buf);	
#endif
		else pip=expipe(sendmail,ascinode(f,0x7f),buf);
		if (pip == NULL)
		{
			logerr("$Could non open (pipe) output for mail");
			tidyrfc(msg);
			return 2;
		}
		if (modtype==1) newsmode=1;
	}

	if (newsmode)
	{
		num_echo++;
		if ((p=hdr("Path",msg)) == NULL)
			p=hdr("RFC-Path",kmsg);
		rlist=NULL;
		fill_rlist(&rlist,p);
		for (tmsg=kmsg;tmsg;tmsg=tmsg->next)
			if (strcasecmp(tmsg->key,"SPTH") == 0)
				fill_list(&ftnpath,tmsg->val,&rlist);
		for (tmsg=kmsg;tmsg;tmsg=tmsg->next)
			if (strcasecmp(tmsg->key,"PATH") == 0)
				fill_list(&ftnpath,tmsg->val,&rlist);
		tidy_falist(&rlist);

		fprintf(pip,"Path: ");
/* ### Modified by P.Saratxaga on 7 Aug 1995 */
/* I comment out this line, to avoid the repetition of my machine name 
   (is the news server that has to add it) */
	/*	fprintf(pip,"%s!",ascinode(bestaka,0x07)); */
/* ### */
		fprintf(pip,"%s!",ascinode(&pktfrom,0x07));
		if (ftnpath)
			for (tfa=ftnpath->next;tfa;tfa=tfa->next)
				fprintf(pip,"%s!",ascinode(tfa->addr,0x1f));
		tidy_falist(&ftnpath);

		if (p)
		{
			while (isspace(*p)) p++;
			fprintf(pip,"%s",p);
		}
		else fprintf(pip,"not-for-mail\n");

#ifdef PUDDLE_GATE
		if ((p=hdr("Newsgroups",msg)))
/* The gate at puddle.fidonet.org put spaces in Newsgroups header */
		{
			if ((strstr(p,", ")))
			{
				while ((r = strchr(p, ' ')))
                	        {
					*r = '\0';
					strcat(p,r+1);
                	        }
			}
		}
#else
		p=hdr("Newsgroups",msg);
#endif
		if (p == NULL) p=hdr("RFC-Newsgroups",kmsg);
		if (p == NULL) p=hdr("Newsgroups",kmsg);
		if (p)
		{
			while (*p && isspace(*p)) p++;
			if ((areas(p,0))) {
				fprintf(pip,"Newsgroups: %s",p);
			} else {
				fprintf(pip,"Newsgroups: %s\n",newsgroup);
				fprintf(pip,"X-Origin-Newsgroups: %s",p);
			}
		}
		else fprintf(pip,"Newsgroups: %s\n",newsgroup);

		if ((p=hdr("Distribution",msg))) fprintf(pip,"Distribution:%s",p);
		else if ((p=hdr("RFC-Distribution",kmsg))) fprintf(pip,"Distribution: %s",p);
		else if ((p=hdr("Distribution",kmsg))) fprintf(pip,"Distribution: %s",p);
		else if (distribution) fprintf(pip,"Distribution: %s\n",distribution);

		p=hdr("Comment-To",msg);
		if (p == NULL) p=hdr("X-Comment-To",msg);
		if (p == NULL) p=hdr("To",msg);
		if ((p) && (strcasecmp(p,"All\n")))
			fprintf(pip,"X-Comment-To:%s",hdrconv(p,outcode,incode));
		else
		{
			if (p == NULL) p=hdr("RFC-X-Comment-To",kmsg);
			if (p == NULL) p=hdr("RFC-Comment-To",kmsg);
			if (p == NULL) p=hdr("RFC-To",kmsg);
			if ((p) && (strcasecmp(p,"All\n")))
 				fprintf(pip,"X-Comment-To: %s",hdrconv(p,outcode,incode));
			else if ((t->name) && (strcasecmp(t->name,"All")))
				fprintf(pip,"X-Comment-To: %s\n",hdrconv(t->name,outcode,incode));
		}


		for (tmpml=approve;tmpml;tmpml=tmpml->next) {
			if ((strncmp(newsgroup,tmpml->prefix,
					strlen(tmpml->prefix)) == 0)) {
				modtype=2;
				moderator=xstrcpy(tmpml->address);
				break;
			}
		}

		if ((p=hdr("Approved",msg))) fprintf(pip,"Approved:%s",p);
		else if ((p=hdr("RFC-Approved",kmsg))) fprintf(pip,"Approved: %s",p);
		else if ((p=hdr("Approved",kmsg))) fprintf(pip,"Approved: %s",p);
		else if (modtype==2) fprintf(pip,"Approved: %s\n",moderator);
	}
	else
	{
		num_mail++;
		time(&now);
#ifdef NEED_UUCPFROM
		fprintf(pip,"From %s!",ascinode(f,0x3f));
		fprintf(pip,"%s %s",ascinode(f,0x40),ctime(&mdate));
#endif
#if (LEVEL > 1)
		fprintf(pip,"Received: from %s ",ascinode(f,0x3f));
		fprintf(pip,"by %s\n",ascinode(bestaka,0x3f));
		fprintf(pip,"\twith FTN (ifmail v.%s) id AA%u; %s\n",
			version,getpid(),rfcdate(now));
#endif

		for (tmsg=kmsg;tmsg;tmsg=tmsg->next)
			if (!strcasecmp(tmsg->key,"RFC-Received"))
				fprintf(pip,"%s: %s",tmsg->key+4,tmsg->val);
		for (tmsg=msg;tmsg;tmsg=tmsg->next)
			if (!strcasecmp(tmsg->key,"Received"))
				fprintf(pip,"%s:%s",tmsg->key,tmsg->val);

		if ((p=hdr("Apparently-To",msg))) fprintf(pip,"Apparently-To:%s",p);
		else if ((p=hdr("RFC-Apparently-To",kmsg))) fprintf(pip,"Apparently-To: %s",p);
		else if ((p=hdr("Apparently-To",kmsg))) fprintf(pip,"Apparently-To: %s",p);
		else if ((is_local(t))) fprintf(pip,"Apparently-To: %s\n",buf);

		if (flags & FLG_RRQ) rrq=1;
		else rrq=0;
		if (rrq && !hdr("RFC-Return-Receipt-To",kmsg) &&
		    !hdr("Return-Receipt-To",msg) &&
		    !hdr("RFC-Notice-Requested-Upon-Delivery-To",kmsg) &&
		    !hdr("Notice-Requested-Upon-Delivery-To",msg))
		{
                        fprintf(pip,"Notice-Requested-Upon-Delivery-To: %s\n",buf);
		}


		if (t->name == NULL) t->name=xstrcpy("Postmaster");
		p=hdr("Resent-To",msg);
		if (p == NULL) p=hdr("To",msg);
		if (p)
			fprintf(pip,"To:%s",p);
		else
		{
			if (p == NULL) p=hdr("RFC-Resent-To",kmsg);
			if (p == NULL) p=hdr("RFC-To",kmsg);
			if (p) fprintf(pip,"To: %s",p);
			else if (modtype==1) fprintf(pip,"To: %s\n",moderator);
			else fprintf(pip,"To: %s\n",ascinode(t,0xff));
		}
	}

	if ((p=hdr("From",msg))) fprintf(pip,"From:%s",hdrconv(p,outcode,incode));
	else if ((p=hdr("RFC-From",kmsg))) fprintf(pip,"From: %s",hdrconv(p,outcode,incode));
	else if ((p=hdr("From",kmsg))) fprintf(pip,"From: %s",hdrconv(p,outcode,incode));
#ifdef PCBOARD_GATE
	else if ((p=hdr("X-PcBoard-FROM",msg)))
		if (f->name) {
			while (isspace(*p)) p++;
			p[strlen(p)-1]='\0';
			fprintf(pip,"From: %s <%s>\n",
				hdrconv(f->name,outcode,incode),
				p);
		}
		else fprintf(pip,"From:%s",p);
#endif
	else if ((hdr("REPLYADDR",kmsg)) && (p=xstrcpy(hdr("REPLYADDR",kmsg))))
	{
		if (*(r=p+strlen(p)-1) == '\n') *(r--)='\0';
		while (isspace(*r)) *(r--)='\0';
		q=xstrcpy(hdr("X-RealName",msg));
		if (q == NULL) q=xstrcpy(hdr("RealName",msg));
		if (q == NULL) q=xstrcpy(hdr("X-RealName",kmsg));
		if (q == NULL) q=xstrcpy(hdr("RealName",kmsg));
		if (q)
		{
			if (*(r=q+strlen(q)-1) == '\n') *(r--)='\0';
			while (isspace(*r)) *(r--)='\0';
			for (l=q;isspace(*l);) l++;
			if ((*l == '\"') && (*r == '\"'))
			{
				l++;
				*r--='\0';
			}
			fprintf(pip,"From: \"%s\" <%s>\n",hdrconv(l,outcode,incode),p);
			free(q);
		}
		else if (f->name) fprintf(pip,"From: \"%s\" <%s>\n",hdrconv(f->name,outcode,incode),p);
		else fprintf(pip,"From: %s\n",p);
		free(p);
	}
	if (p) fprintf(pip,"X-FTN-Sender: %s\n",hdrconv(ascinode(f,0xff),outcode,incode));
	else fprintf(pip,"From: %s\n",hdrconv(ascinode(f,0xff),outcode,incode));

	if ((p=hdr("Reply-To",msg))) fprintf(pip,"Reply-To:%s",p);
	else if ((p=hdr("RFC-Reply-To",kmsg))) fprintf(pip,"Reply-To: %s",p);
	else if ((p=hdr("Reply-To",kmsg))) fprintf(pip,"Reply-To: %s",p);
	else if (((p=backalias(f))) && myfqdn)
		fprintf(pip,"Reply-To: %s@%s\n",p,myfqdn);
	else if ((p=hdr("REPLYADDR",kmsg))) fprintf(pip,"Reply-To: %s",p);
	else if ((p=hdr("REPLYTO",kmsg))) fprintf(pip,"Reply-To: %s\n",ascinode(parsefaddr(p),0xff));

	if ((p=hdr("Date",msg))) fprintf(pip,"Date:%s",p);
	else if ((p=hdr("RFC-Date",kmsg))) fprintf(pip,"Date: %s",p);
	else if ((p=hdr("Date",kmsg))) fprintf(pip,"Date: %s",p);
	else if (newsmode) {
#ifdef RESTAMP_FUTURE_POSTINGS
		if(mdate > time(&now)) {
			loginf("Future posting: %s", rfcdate(mdate));
			fprintf(pip,"Date: %s\n", rfcdate(now));
			fprintf(pip,"X-Origin-Date: %s\n", rfcdate(mdate));
		} else
#endif
#ifdef RESTAMP_OLD_POSTINGS
		if(mdate < 31532400) {	/* 1971/1/1 */
		    loginf("Bad date from %s", ascfnode(f,0x3f));
		    fprintf(pip,"Date: %s\n", rfcdate(now));
		    fprintf(pip,"X-Bad-Date: ignored\n");
		} else
		if((mdate < time(&now)-14*24*60*60) &&
		   (mdate > time(&now)-restampolder*24*60*60)) {
			loginf("Article too old, restamped: %s", rfcdate(mdate));
			fprintf(pip,"Date: %s\n", rfcdate(now));
			fprintf(pip,"X-Origin-Date: %s\n", rfcdate(mdate));
		} else
#endif
		fprintf(pip,"Date: %s\n",rfcdate(mdate));
	}
	else fprintf(pip,"Date: %s\n",rfcdate(mdate));

	if ((p=hdr("Subject",msg))) fprintf(pip,"Subject:%s",hdrconv(p,outcode,incode));
	else if ((p=hdr("RFC-Subject",kmsg))) fprintf(pip,"Subject: %s",hdrconv(p,outcode,incode));
	else if ((p=hdr("Subject",kmsg))) fprintf(pip,"Subject: %s",hdrconv(p,outcode,incode));
#ifdef PCBOARD_GATE
	else if ((p=hdr("X-PcBoard-SUBJECT",msg))) fprintf(pip,"Subject:%s",hdrconv(p,outcode,incode));
#endif /* PCBOARD_GATE */
	else if (subj && (strspn(subj," \t\n\r") != strlen(subj)))
		fprintf(pip,"Subject: %s\n",hdrconv(subj,outcode,incode));
	else fprintf(pip,"Subject: <none>\n");

	if ((p=hdr("Message-ID",msg)))
		fprintf(pip,"Message-ID:%s",p);
	else if ((p=hdr("RFC-Message-ID",kmsg)))
		fprintf(pip,"Message-ID: %s",p);
	else if ((p=hdr("Message-ID",kmsg)))
		fprintf(pip,"Message-ID: %s",p);
	else if ((p=hdr("RFCID",kmsg)))
		if ((p[0]=='<')) {
			/* "^aRFCID: <local@machine>" */
			if ((p[strlen(p)-2]=='>'))
				fprintf(pip,"Message-ID: %s",p);
			/* "^aRFCID: <local@machine" */
			/* I saw it on some IntToss gated articles
			 * it seems to be a bug on the program or something
			 * like that, because in the majority of IntToss gated
			 * articles it is "^aRFCID: local@machine"
			 */
			else { p[strlen(p)-1]='\0';
				fprintf(pip,"Message-ID: %s>\n",p);
			}
		}
		/* "^aRFCID: local@machine" */
		else { 
			p[strlen(p)-1]='\0'; 
			fprintf(pip,"Message-ID: <%s>\n",p);
		}
	else if ((p=hdr("ORIGID",kmsg)))
		fprintf(pip,"Message-ID: %s",p);
	else if ((p=hdr("MSGID",kmsg)))
#ifndef GATEBAU_MSGID
		fprintf(pip,"Message-ID: %s\n",rfcmsgid(p,bestaka));
#else
		fprintf(pip,"Message-ID: %s\n",rfcmsgid(p,bestaka,gatebaumode));
	else if (gatebaumode==1)
	{
		strcpy(buf,f->name);
		if ((p=strrchr(buf,'\n'))) *p='\0';
		strcat(buf,t->name);
		if ((p=strrchr(buf,'\n'))) *p='\0';
		strcat(buf,subj);
		if ((p=strrchr(buf,'\n'))) *p='\0';
		if (((f->zone) > 0) && ((f->zone) < 7))
			fprintf(pip,"Message-ID: <NOMSGID_%d=3A%d=2F%d.%d_%s_%08lx@fidonet.org>\n",
				f->zone,f->net,f->node,f->point,
				gatebaudate(mdate),
				crcgatebau(buf));

		else fprintf(pip,"Message-ID: <NOMSGID_%d=3A%d=2F%d.%d_%s_%08lx@%s.ftn>\n",
			f->zone,f->net,f->node,f->point,
			gatebaudate(mdate),
			crcgatebau(buf),
			(f->domain)?f->domain:"nodomain");
	}
#endif
	else fprintf(pip,"Message-ID: <%lu@%s.ftn>\n",
			mdate^(subj?crc(subj):0L),
			ascinode(f,0x1f));

	if (newsmode)
	{
		if ((p=hdr("References",msg)))
			fprintf(pip,"References:%s",p);
		else if ((p=hdr("RFC-References",kmsg)))
			fprintf(pip,"References: %s",p);
		else if ((p=hdr("References",kmsg)))
			fprintf(pip,"References: %s",p);
		else if ((p=hdr("ORIGREF",kmsg)))
			fprintf(pip,"References: %s",p);
		else if ((p=hdr("REPLY",kmsg)))
#ifndef GATEBAU_MSGID
			fprintf(pip,"References: %s\n",rfcmsgid(p,bestaka));
#else
			fprintf(pip,"References: %s\n",rfcmsgid(p,bestaka,gatebaumode));
#endif
	}
	else
	{
		if ((p=hdr("In-Reply-To",msg)))
			fprintf(pip,"In-Reply-To:%s",p);
		else if ((p=hdr("RFC-In-Reply-To",kmsg)))
			fprintf(pip,"In-Reply-To: %s",p);
		else 
		{
			if ((p=hdr("REPLY",kmsg)))
				fprintf(pip,"In-Reply-To: %s\n",
#ifndef GATEBAU_MSGID
					rfcmsgid(p,bestaka));
#else
					rfcmsgid(p,bestaka,gatebaumode));
#endif
		}
	}

	if ((p=hdr("Organization",msg)))
		fprintf(pip,"Organization:%s",hdrconv(p,outcode,incode));
	else if ((p=hdr("RFC-Organization",kmsg)))
		fprintf(pip,"Organization: %s",hdrconv(p,outcode,incode));
	else if ((p=hdr("Organization",kmsg)))
		fprintf(pip,"Organization: %s",hdrconv(p,outcode,incode));
	else if (orig) 
		fprintf(pip,"Organization: %s\n",hdrconv(orig,outcode,incode));

	if ((p=hdr("Supersedes",msg)))
		fprintf(pip,"Supersedes:%s",p);
	else if ((p=hdr("RFC-Supersedes",kmsg)))
		fprintf(pip,"Supersedes: %s",p);
	else if ((p=hdr("Supersedes",kmsg)))
		fprintf(pip,"Supersedes: %s",p);
	else if ((p=hdr("ACUPDATE",kmsg)) && (strstr(p,"MODIFY")))
#ifndef GATEBAU_MSGID
			fprintf(pip,"Supersedes: %s\n",rfcmsgid(p+8,bestaka));
#else
			fprintf(pip,"Supersedes: %s\n",rfcmsgid(p+8,bestaka,gatebaumode));
#endif

#ifdef ALLOW_CONTROL
	if ((p=hdr("Control",msg)))
                fprintf(pip,"Control:%s",p);
        else if ((p=hdr("RFC-Control",kmsg)))
                fprintf(pip,"Control: %s",p);
        else if ((p=hdr("Control",kmsg)))
                fprintf(pip,"Control: %s",p);
        else if ((p=hdr("ACUPDATE",kmsg)) && (strstr(p,"DELETE")))
#ifndef GATEBAU_MSGID
                        fprintf(pip,"Control: cancel %s\n",rfcmsgid(p+8,bestaka));
#else
			fprintf(pip,"Control: cancel %s\n",rfcmsgid(p+8,bestaka,gatebaumode));
#endif
#endif
        writechrs(incode,pip,0);
	if (incode != outcode) writechrs(outcode,pip,2);
	writecharset(incode,pip,msg,kmsg);

	if (newsmode) {
        if ((p=hdr("X-Newsreader",msg)))
                fprintf(pip,"X-Newsreader: %s",p);
        else if ((p=hdr("RFC-X-Newsreader",kmsg)))
                fprintf(pip,"X-Newsreader: %s",p);
        else if ((p=hdr("X-Newsreader",kmsg)))
                fprintf(pip,"X-Newsreader: %s",p);
        else if ((p=hdr("PID",kmsg)))
                        fprintf(pip,"X-Newsreader: %s",p);

	} else {
        if ((p=hdr("X-Mailer",msg)))
                fprintf(pip,"X-Mailer:%s",p);
        else if ((p=hdr("RFC-X-Mailer",kmsg)))
                fprintf(pip,"X-Mailer: %s",p);
        else if ((p=hdr("X-Mailer",kmsg)))
                fprintf(pip,"X-Mailer: %s",p);
        else if ((p=hdr("PID",kmsg)))
                        fprintf(pip,"X-Mailer: %s",p);
	}


#ifdef GATEBAU_MSGID
	if (gatebaumode==1)
#ifdef XGATEWAY_STRICT
	if ((p=hdr("X-GATEWAY",kmsg)))
	{
		if ((q=strchr(p,'\n'))) *q='\0';
		fprintf(pip,"X-Gateway: %s, FIDO .. %s [ifmail %s]\n",
			p,myfqdn,version);
		if (q) *q='\n';
	}
#else
	fprintf(pip,"X-Gateway: FIDO .. %s [ifmail %s]\n",myfqdn,version);
#endif
#endif

	for (tmsg=msg;tmsg;tmsg=tmsg->next)
	{
		if (strcasecmp(tmsg->key,"X-Body-Start") &&
#ifdef PCBOARD_GATE
		    strcasecmp(tmsg->key,"X-PcBoard-FROM") &&
		    strcasecmp(tmsg->key,"X-PcBoard-SUBJECT") &&
		    strcasecmp(tmsg->key,"X-PcBoard-PACKOUT") &&
#endif
#ifdef ALLOW_CONTROL
		    strcasecmp(tmsg->key,"Control") &&
#endif
		    strcasecmp(tmsg->key,"Supersedes") &&
		    strcasecmp(tmsg->key,"Mime-Version") &&
		    strcasecmp(tmsg->key,"Content-Type") &&
		    strcasecmp(tmsg->key,"Content-Lenght") &&
		    strcasecmp(tmsg->key,"Content-Transfer-Encoding") &&
		    strcasecmp(tmsg->key,"Lines") &&
		    strcasecmp(tmsg->key,"Path") &&
		    strcasecmp(tmsg->key,"Received") &&
		    strcasecmp(tmsg->key,"From") &&
		    strcasecmp(tmsg->key,"To") &&
		    strcasecmp(tmsg->key,"Comment-To") &&
		    strcasecmp(tmsg->key,"X-Comment-To") &&
		    strcasecmp(tmsg->key,"Date") &&
		    strcasecmp(tmsg->key,"Subject") &&
		    strcasecmp(tmsg->key,"Reply-To") &&
		    strcasecmp(tmsg->key,"In-Reply-To") &&
		    strcasecmp(tmsg->key,"References") &&
		    strcasecmp(tmsg->key,"Organization") &&
		    strcasecmp(tmsg->key,"X-Mailer") &&
		    strcasecmp(tmsg->key,"X-Newsreader") &&
		    (strcasecmp(tmsg->key,"Newsgroups") || !newsmode) &&
		    strcasecmp(tmsg->key,"Apparently-To") &&
		    strcasecmp(tmsg->key,"Distribution") &&
		    strcasecmp(tmsg->key,"Approved") &&
		    strcasecmp(tmsg->key,"Message-ID"))
			fprintf(pip,"%s:%s",tmsg->key,hdrconv(tmsg->val,outcode,incode));
	}

	if ((p=compose_flags(flags,hdr("FLAGS",kmsg))))
	{
		fprintf(pip,"X-FTN-FLAGS:%s\n",p);
		free(p);
	}

	for (tmsg=kmsg;tmsg;tmsg=tmsg->next)
	{
		if (strcasecmp(tmsg->key,"INTL") &&
		    strcasecmp(tmsg->key,"FMPT") &&
		    strcasecmp(tmsg->key,"TOPT") &&
		    strcasecmp(tmsg->key,"FLAGS") &&
		    strcasecmp(tmsg->key,"CHARSET") &&
		    strcasecmp(tmsg->key,"CHRS") &&
                    strcasecmp(tmsg->key,"CODEPAGE") &&
		    strcasecmp(tmsg->key,"ORIGCHRS") &&
#ifdef FORCE_REPLYTO_LOCAL
		    strcasecmp(tmsg->key,"REPLYTO") &&
		    strcasecmp(tmsg->key,"REPLYADDR") &&
#endif
	/* RFC: is used by fidogate to tell how completly RFC headers were
	gated (0=no headers at all; 1=some headers; 2=all headers) */
		    strcasecmp(tmsg->key,"RFC") &&  
		    strcasecmp(tmsg->key,"RFCID") &&
		    strcasecmp(tmsg->key,"ORIGID") &&
		    strcasecmp(tmsg->key,"ORIGREF") &&
		    strcasecmp(tmsg->key,"X-GATEWAY") &&
		    strcasecmp(tmsg->key,"Lines") &&
	/*	    strcmp(tmsg->key,"Path") &&		*/
		    strcasecmp(tmsg->key,"PATH") &&
		    strcasecmp(tmsg->key,"Received") &&
		    strcasecmp(tmsg->key,"From") &&
		    strcasecmp(tmsg->key,"To") &&
		    strcasecmp(tmsg->key,"Comment-To") &&
		    strcasecmp(tmsg->key,"X-Comment-To") &&
		    strcasecmp(tmsg->key,"Date") &&
		    strcasecmp(tmsg->key,"Subject") &&
		    strcasecmp(tmsg->key,"Reply-To") &&
		    strcasecmp(tmsg->key,"In-Reply-To") &&
		    strcasecmp(tmsg->key,"References") &&
		    strcasecmp(tmsg->key,"Organization") &&
		    strcasecmp(tmsg->key,"X-Mailer") &&
		    strcasecmp(tmsg->key,"X-Newsreader") &&
		    (strcasecmp(tmsg->key,"Newsgroups") || !newsmode) &&
		    strcasecmp(tmsg->key,"Apparently-To") &&
		    strcasecmp(tmsg->key,"Message-ID") &&
		    strcasecmp(tmsg->key,"Mime-Version") &&
		    strcasecmp(tmsg->key,"Content-Type") &&
		    strcasecmp(tmsg->key,"Content-Lenght") &&
		    strcasecmp(tmsg->key,"Content-Transfer-Encoding") &&
#ifdef ALLOW_CONTROL
		    strcasecmp(tmsg->key,"RFC-Control") &&
#endif
		    strcasecmp(tmsg->key,"RFC-Supersedes") &&
		    strcasecmp(tmsg->key,"RFC-Mime-Version") &&
		    strcasecmp(tmsg->key,"RFC-Content-Type") &&
		    strcasecmp(tmsg->key,"RFC-Content-Lenght") &&
		    strcasecmp(tmsg->key,"RFC-Content-Transfer-Encoding") &&
		    strcasecmp(tmsg->key,"RFC-Lines") &&
		    strcasecmp(tmsg->key,"RFC-Path") &&
		    strcasecmp(tmsg->key,"RFC-Received") &&
		    strcasecmp(tmsg->key,"RFC-From") &&
		    strcasecmp(tmsg->key,"RFC-To") &&
		    strcasecmp(tmsg->key,"RFC-Comment-To") &&
		    strcasecmp(tmsg->key,"RFC-X-Comment-To") &&
		    strcasecmp(tmsg->key,"RFC-Date") &&
		    strcasecmp(tmsg->key,"RFC-Subject") &&
		    strcasecmp(tmsg->key,"RFC-Reply-To") &&
		    strcasecmp(tmsg->key,"RFC-In-Reply-To") &&
		    strcasecmp(tmsg->key,"RFC-References") &&
		    strcasecmp(tmsg->key,"RFC-Organization") &&
		    strcasecmp(tmsg->key,"RFC-X-Mailer") &&
		    strcasecmp(tmsg->key,"RFC-X-Newsreader") &&
		    (strcasecmp(tmsg->key,"RFC-Newsgroups") || !newsmode) &&
		    strcasecmp(tmsg->key,"RFC-Apparently-To") &&
		    strcasecmp(tmsg->key,"RFC-Distribution") &&
		    strcasecmp(tmsg->key,"RFC-Approved") &&
		    strcasecmp(tmsg->key,"RFC-Message-ID"))
			if (!strncmp(tmsg->key,"RFC-",4))
				fprintf(pip,"%s: %s",tmsg->key+4,hdrconv(tmsg->val,outcode,incode));
			else if ((!strncasecmp(tmsg->key,"X-",2)) ||
				 (!strncasecmp(tmsg->key,"NNTP-",5)))
				fprintf(pip,"%s: %s",tmsg->key,hdrconv(tmsg->val,outcode,incode));
			else if ((!strncasecmp(tmsg->key,"ZC-",3)))
				fprintf(pip,"X-%s: %s",tmsg->key,tmsg->val);
			else if ((!strcasecmp(tmsg->key,"Origin")) ||
				 (!strcasecmp(tmsg->key,"MOOD"))) 
				fprintf(pip,"X-FTN-%s: %s",tmsg->key,hdrconv(tmsg->val,outcode,incode));
			else
				fprintf(pip,"X-FTN-%s: %s",tmsg->key,tmsg->val);
	}

	if (newsmode)
	{
		fa_list *tmpl,*ptl=NULL;
		char sbe[16];
		int seenlen=0,oldnet;

		for (tmsg=kmsg;tmsg;tmsg=tmsg->next)
		if (!strcmp(tmsg->key,"PATH"))
		{
			fill_path(&ptl,tmsg->val);
		}
		sprintf(sbe,"%s",ascfnode(bestaka,0x06));
		fill_path(&ptl,sbe);
		uniq_list(&ptl);

		/* ensure it will not match for the first entry */
		oldnet=ptl->addr->net-1;
		fprintf(pip,"X-FTN-PATH:");
		for (tmpl=ptl;tmpl;tmpl=tmpl->next)
		{
			if (tmpl->addr->net == oldnet)
				sprintf(sbe," %u",tmpl->addr->node);
			else
				sprintf(sbe," %u/%u",tmpl->addr->net,
					tmpl->addr->node);
			oldnet=tmpl->addr->net;
			seenlen+=strlen(sbe);
			if (seenlen > MAXPATH)
			{
				seenlen=0;
				fprintf(pip,"\nX-FTN-PATH:");
				sprintf(sbe," %u/%u",tmpl->addr->net,
					tmpl->addr->node);
				seenlen=strlen(sbe);
			}
			fprintf(pip,"%s",sbe);
		}
		fprintf(pip,"\n");

	/*	fprintf(pip,"X-FTN-PATH: %s\n",ascfnode(bestaka,0x06)); */
		if ((hdr("X-FTN-SPTH",msg)))
			fprintf(pip,"X-FTN-SPTH: %s\n",ascfnode(bestaka,0x1f));
	}

	fprintf(pip,"\n");
	lines=0;
	if ((p=hdr("X-Body-Start",msg)))
	{
		lines++;
		fputs(strkconv(p, outcode, incode), pip);
	}
	pass=1;
	count=0;
	while(fgets(buf,sizeof(buf)-1,fp) && pass)
	{
		if (ftell(fp) > endoff)
		{
			debug(5,"line \"%s\" past message end %ld",
				buf,(long)endoff);
			pass=0;
		}
		if (pass)
		{
			p=buf;
			b=NULL;
			while ((c=*p++))
			{
				switch (c)
				{
				case ' ':	b=p-1; break;
				case '\n':	b=NULL; count=0; lines++; break;
				}
				if ((count++ > BOUNDARY) && (!pgpsigned))
				{
					if (b) 
					{
						*b='\n';
						p=b+1;
						b=NULL;
						lines++;
						count=0;
					}
				}
			}
			fputs(strkconv(buf, outcode, incode), pip);
		}
	}

	if ((modtype==1) &&
	    (!hdr("Approved",msg)) &&
	    (!hdr("RFC-Approved",kmsg)) &&
	    (!hdr("Approved",kmsg)))
		newsmode=0;

	if (newsmode)
	{
		sprintf(lineshdr,"Lines: %d\n",lines);
		rewind(pip);
		fstat(fileno(pip),&stbuf);
		fprintf(nb,"#! rnews %lu\n",
			(unsigned long)stbuf.st_size+strlen(lineshdr));
		while (fgets(buf,sizeof(buf)-1,pip) && (buf[0] != '\n'))
			fputs(buf,nb);
		fputs(lineshdr,nb);
		fputs(buf,nb);
		while (fgets(buf,sizeof(buf)-1,pip))
			fputs(buf,nb);
	}
	if ((
	     (notransports || newsmode)?(rc=fclose(pip)):(rc=exclose(pip))
	   ))
	{
		logerr("$close of transport pipe or tmp file returned %d",rc);
	}
	tidyrfc(msg);
	if (rc < 0) rc=-rc;
	return rc;
}

#ifdef GATEBAU_MSGID
/* converts the date to a string "YYMMDD_HHMMSS" */
char *gatebaudate(now)
time_t now;
{
	static char buf[40];
	struct tm ptm;

	if (!now) time(&now);
	ptm=*localtime(&now);

	sprintf(buf,"%02d%02d%02d_%02d%02d%02d",
		ptm.tm_year%100,(ptm.tm_mon)+1,ptm.tm_mday,
		ptm.tm_hour,ptm.tm_min,ptm.tm_sec);
	return(buf);
}
#endif