summaryrefslogtreecommitdiff
path: root/misc/news/newsgate
blob: 96080f5ab16f8de700dccaaf0576d967c19c0ab5 (plain)
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
# feed this into perl
	eval 'exec perl -S $0 ${1+"$@"}'
		if $running_under_some_shell;

#
# $Id$
#
#  Copyright (c) 1990-2006, Raphael Manfredi
#  
#  You may redistribute only under the terms of the Artistic License,
#  as specified in the README file that comes with the distribution.
#  You may reuse parts of this distribution only within the terms of
#  that same Artistic License; a copy of which may be found at the root
#  of the source tree for mailagent 3.0.
#
# $Log: newsgate,v $
# Revision 3.0.1.2  2001/03/13 13:17:03  ram
# patch71: the in-reply-to field is now computed via a regexp
#
# Revision 3.0.1.1  2001/01/10 16:59:58  ram
# patch69: created
#

#
# This program is meant to be run by INN upon reception of a message
# on any of the local ml.* groups, which is where I spool some of
# mailing lists I selectively read.
#
# It takes an article on stdin, looks at its Newsgroups line, check
# the ones we know how to forward to a mailing list by consulting
# an ml.map file, and builds a mail message sent to all the lists at
# once (when crossposting).
#
# The program leaves an X-Mailer trace to be able to trace errors back to
# that program, and also to prevent loops: the server name where the article
# is processed is inserted in the X-Mailer line.
#

use strict;

(my $me = $0) =~ s|.*/(.*)|$1|;

my $VERSION = "1.0";
my $ML_MAP = "/etc/news/ml.map";			# Mailing list map
my $SENDMAIL = "/usr/sbin/sendmail";		# Change flags if changing MTA
my $LEVEL = "info";
my $LOG = "";

use Getopt::Long;
&usage unless GetOptions(
	'level=s'	=> \$LEVEL,
	'map=s'		=> \$ML_MAP,
	'logfile=s'	=> \$LOG,
	'h|help'	=> \&usage,
);

sub usage {
	print STDERR <<EOM;
Usage: $me [-h] [-level dbglevel] [-logfile path] [-map file] <article
  -h      : show this help message
  -level  : set debugging level (syslog priority name, default is "$LEVEL")
  -logfile: write logs to file, not to syslog
  -map    : map file (defaults to $ML_MAP)
EOM
	exit 1;
}

use Log::Agent;
my $driver;

if (length $LOG) {
	require Log::Agent::Driver::File;
	$driver = Log::Agent::Driver::File->make(
		-prefix		=> $me,
		-showpid	=> 1,
		-file		=> $LOG,
	);
} else {
	require Log::Agent::Driver::Syslog;
	$driver = Log::Agent::Driver::Syslog->make(
		-prefix		=> $me,
		-facility	=> "news",
		-showpid	=> 1,
		-logopt		=> "ndelay",
	);
}

logconfig(
	-driver => $driver,
	-level => lc($LEVEL),
);

use Sys::Hostname;

#
# Parse header
#

my $header;
my $skipping_header = 0;
my %header;
my $field;

while (<STDIN>) {
	last if /^$/;
	if (
		/^Path:/i			||
		/^Distribution:/i	||
		/^Followup-To:/i	||
		/^Xref:/i			||
		/^X-Server-.*:/i	||
		/^Originator:/i		||
		/^Expires:/i		||
		/^Summary:/i		||
		/^Keywords:/i		||
		/^Lines:/i			||
		/^Resent-\w+:/i		||
		/^To:/i				||
		/^Cc:/i
	) {
		$skipping_header = 1;
		next;
	}
	if (/^\s/) {
		next if $skipping_header;
		s/^\s+/ /;
		chop;
		$header{$field} .= $_ if $field ne '';
		logwarn "bad continuation in header, line $." if $field eq '';
	} elsif (($field, my $value) = /^([\w-]+)\s*:\s*(.*)/) {
		$skipping_header = 0;
		$field =~ s/(\w+)/\u\L$1/g;		# Normalize spelling
		$header{$field} .= " " if exists $header{$field};
		$header{$field} .= $value;
	} else {
		chop;
		logerr "ignoring bad header line $.: $_";
		next;
	}

	# Those are recorded in %header but not propagated through mail
	next if
		$field eq 'Newsgroups'		||
		$field =~ /^Nntp-.*/		||
		$field eq 'Sender';

	$header .= $_;
}

my ($from) = (parse_address($header{From} || $header{Sender}))[0];
logsay "from $from";
logdie "hostile address $from" if $from =~ /'`";<>&\|/;

my @newsgroups = map { lc($_) } split(/,\s*/, $header{Newsgroups});
logdie "no Newsgroups line found" unless @newsgroups;
logsay "newsgroups @newsgroups";

#
# Last reference is the replied-to message, if it's a reply at all
#

my ($in_reply_to) = $header{References} =~ /(<[^>]+>)\s*$/;

#
# Parse newsgroup mapping file
#

my %addr;

open(MAP, $ML_MAP) || logdie "can't open map file $ML_MAP: $!";
while (<MAP>) {
	chop;
	next if /^#/ || /^\s*$/;
	my ($ng, $addr) = split(/\s+/);
	$addr{lc($ng)} = $addr;
}
close MAP;

#
# Determine destinations.
#

my @to;
foreach my $ng (@newsgroups) {
	push @to, $addr{$ng} if exists $addr{$ng};
}

unless (@to) {
	logtrc 'info', "no mailing list target found";
	exit 0;
}
logsay "sending to @to";

#
# Open mailer, now that we know we have something to send
#

my $host = hostname;
my ($cname, $ip) = hostinfo($host);
my $now = mta_date();
my $phost = $header{'Nntp-Posting-Host'};
my $origin = $phost ? "[NNTP at $phost]" : "<origin unknown>";

open(MAILER, "|$SENDMAIL -t -odb -oi -f $from") || logdie "cannot fork: $!";
print MAILER <<EOH;
Received: from $cname [$ip]
	via news $origin ($me $VERSION);
	$now
EOH
print MAILER "To: ", join(", ", @to), "\n";
print MAILER $header;
print MAILER "In-Reply-To: $in_reply_to\n" if length $in_reply_to;
print MAILER "X-Mailer: $me $VERSION at $cname\n\n";	# To prevent loops
while (<STDIN>) {
	print MAILER;
}
close MAILER;
logdie "mailer error while sending to @to" if $?;

exit 0;		# All done

# Compute cname and IP address of host
sub hostinfo {
	my ($host) = @_;
	my ($name, $aliases, $addrtype, $length, @addr) = gethostbyname($host);
	my $addr = join ".", unpack("C4", $addr[0]);
	return ($name, $addr);
}

# Return date in MTA format -- stolen from mailagent
sub mta_date {
	my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
	my ($gmmin, $gmhour, $gmyday) = (gmtime(time))[1,2,7];
	my @days   = qw(Sun Mon Tue Wed Thu Fri Sat);
	my @months = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec);

	# Compute delta in minutes between local time and GMT
	$yday = -1 if $gmyday == 0 && $yday >= 364;
	$gmyday = -1 if $yday == 0 && $gmyday >= 364;
	$gmhour += 24 if $gmyday > $yday;
	my $dhour = ($gmyday < $yday) ? $hour + 24 : $hour;
	my $dmin = ($dhour * 60 + $min) - ($gmhour * 60 + $gmmin);

	# Must convert delta into +/-HHMM format
	my $d = 100 * int($dmin / 60) + (abs($dmin) % 60) * ($dmin > 0 ? 1 : -1);

	sprintf "%s, %2d %s %4d %02d:%02d:%02d %+05d",
		$days[$wday], $mday, $months[$mon], 1900+$year, $hour, $min, $sec, $d;
}

# Parse RFC822 address -- stolen from mailagent
# Returns (address, comment)
sub parse_address {
	local ($_) = @_;						# The address to be parsed
	my $comment;
	my $internet;
	if (/^\s*(.*?)\s*<(\S+)>[^()]*$/) {		# comment <address>
		$comment = $1;
		$internet = $2;
		$comment =~ s/^"(.*)"/$1/;			# "comment" -> comment
		($internet, $comment);
	} elsif (/^\s*([^()]+?)\s*\((.*)\)/) {	# address (comment) 
		$comment = $2;
		$internet = $1;
		# Construct '<address> (comment)' is invalid but... priority to <>
		# This will also take care of "comment" <address> (other-comment)
		$internet =~ /<(\S+)>/ && ($internet = $1);
		($internet, $comment);
	} elsif (/^\s*<(\S+)>\s*(.*)/) {		# <address> ...garbage...
		($1, $2);
	} elsif (/^\s*\((.*)\)\s*<?(.*)>?/) {	# (comment) [address or <address>]
		($2, $1);
	} else {								# plain address, grab first word
		/^\s*(\S+)\s*(.*)/;
		($1, $2);
	}
}