blib/lib/News/Gateway.pm untested

filter: Statementstmt 0.0% 0 / 92 Branchbran 0.0% 0 / 30 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 0.0% 0 / 13 Podpod 0.0% 0 / 8 totaltotal 0.0% 0 / 153 CC 16 SCAR 33.0
CC
16
Cov
0%
AUTOLOAD
72.0
apply
20.0
new
12.0
CRAP 27.0
1 # News::Gateway -- Mail and news gatewaying toolkit. -*- perl -*-
2 # $Id: Gateway.in,v 0.43 1998/04/12 17:26:47 eagle Exp $
3 #
4 # Copyright 1997, 1998 by Russ Allbery <rra@stanford.edu>
5 #
6 # This program is free software; you can redistribute it and/or modify it
7 # under the same terms as Perl itself.
8 #
9 # "Obviously unlike you people, I don't have time to edit the newsgroups
10 # line for every single article I post."
11 # -- markl@cs.yale.edu, in the midst of a huge
12 # flamewar about COBOL crossposted to too many
13 # newsgroups including alt.folklore.computers.
14  
15 ############################################################################
16 # Modules, declarations, and site configuration
17 ############################################################################
18  
19 package News::Gateway;
20 require 5.003;
21  
22 use News::Article ();
23  
24 use strict;
25 use vars qw($AUTOLOAD $FAILCODE %HOOKS $VERSION);
26  
27 # The current News::Gateway version number, which is not the same as the RCS
28 # revision of this file. Picked up during build time.
29 $VERSION = 0.42;
30  
31 # Exit code that should be used to indicate permanent failure. This should
32 # be 100 for qmail and probably be 64 for sendmail (EX_USAGE), although you
33 # may want to pick some other sendmail status (there are so many to choose
34 # from....).
35 $FAILCODE = 64;
36  
37  
38 ############################################################################
39 # Gateway module hooks
40 ############################################################################
41  
42 # News::Gateway implements a generic robomoderation framework that provides
43 # a series of setup and utility functions and the means for various modules
44 # to be easily plugged into a generic framework. All article checks and
45 # rewrites are performed by modules, not this core code. The following
46 # table is used to associate modules with configuration file directives so
47 # that configuration information can be passed to the modules, as well as to
48 # tell News::Gateway what modules exist.
49 #
50 # The general syntax is as follows:
51 #
52 # module1 => ['config1', 'config2'],
53 # module2 => ['config3'],
54 # module3 => [],
55 #
56 # This registers message mangling hooks for module1, module2, and module3,
57 # and also sets up a callback module1_conf that's called for configuration
58 # lines starting with 'config1' or 'config2' and a callback module2_conf
59 # that's called for configuration lines starting with 'config3'.
60  
61 # @@ Begin automatically generated section -- do not edit.
62 %HOOKS = (anykeyword => [],
63 bodyheaders => ['bodyheaders'],
64 cleanbody => [],
65 crosspost => ['crosspost', 'followup'],
66 headers => ['header'],
67 keywords => ['keywords'],
68 mailpath => [],
69 mailtonews => [],
70 moosesign => ['pgpkey'],
71 mungeids => ['mungeids'],
72 newsgroups => ['group'],
73 newstomail => ['newstomail'],
74 nobinaries => [],
75 previoushop => [],
76 whitelist => ['whitelist']);
77 # @@ End automatically generated section -- do not edit.
78  
79  
80 ############################################################################
81 # Gateway module loading
82 ############################################################################
83  
84 # We can't quite use AutoLoader, although we're going to do something very
85 # similar, because the name of the file to be imported isn't going to be the
86 # same as the called sub. We're being very strange, tricky, and subtle.
87 # The name of the module to load will be the sub we tried to call, with any
88 # trailing underscore and letters after an underscore stripped off and a .al
89 # appended. We'll look in the standard autoload directory for it. Most of
90 # this code is derived from AutoLoader.pm.
91 sub AUTOLOAD {
92 my ($package, $module) = ($AUTOLOAD =~ /(.*)::([^:]+)$/);
93 $package =~ s%::%/%;
94 $module =~ s/_[^_]+$//;
95  
96 # If our package has been loaded, which it should have been, we can
97 # derive the name of the directory our modules are in from it.
98 # Otherwise, we'll do a full @INC search for the module we're looking
99 # for.
100 my $name = $INC{"$package.pm"};
101 if ($name) {
102 $name =~ s%^(.*)$package\.pm$%$1auto/$package/$module.al%;
103 undef $name unless (-r $name);
104 }
105 unless ($name) { $name = "auto/$package/$module.al" }
106  
107 # Now comes the fun. We try to load the module. If the routine trying
108 # to be autoloaded is DESTROY or import, we generate a null one on the
109 # fly to avoid annoying error messages at shutdown or use time.
110 #
111 # We have to handle a few things specially here. First, if the sub that
112 # we're trying and failing to autoload is mail_error() (which may be
113 # called by error()), then we have a major problem and need to avoid
114 # calling more methods. Second, if the first argument to the sub isn't
115 # a reference (meaning that they're not calling a method), we can't then
116 # call our error() method since we don't have an object. (This should
117 # really be fixed by making error() robust in that case....)
118 my $save = $@;
119 eval { require $name };
120 if ($@) {
121 $@ =~ s/ at .*\n//;
122 if (substr ($AUTOLOAD, -9) eq '::DESTROY'
123 || substr ($AUTOLOAD, -8) eq '::import') {
124 *$AUTOLOAD = sub {};
125 } elsif (substr ($AUTOLOAD, -12) eq '::mail_error') {
126 warn "Autoload of $AUTOLOAD failed: $@\n";
127 warn "Unable to autoload error methods, aborting\n";
128 exit $FAILCODE;
129 } elsif (not ref $_[0]) {
130 warn "Autoload of $AUTOLOAD failed: $@\n";
131 warn "Attempted to autoload non-method, aborting\n";
132 exit $FAILCODE;
133 } else {
134 $_[0]->error ("Autoload of $AUTOLOAD failed: $@");
135 }
136 }
137 $@ = $save;
138 goto &$AUTOLOAD;
139 }
140  
141  
142 ############################################################################
143 # Creation and initialization of a new gateway
144 ############################################################################
145  
146 # This method creates a new gateway object. It takes arguments specifying
147 # whether we're running interactively (if we are, errors can be dealt with
148 # by dying; otherwise, we need to send mail), the maintainer (administrative
149 # contact address) which defaults to the user we're running as if none is
150 # given, and the envelope sender that should be used for any and all replies
151 # to articles.
152 sub new {
153 my $that = shift;
154 my $class = ref $that || $that;
155 my ($interactive, $maintainer, $envelope) = @_;
156 unless ($maintainer) { $maintainer = (getpwuid ($<))[0] }
157 unless ($envelope) { $envelope = $maintainer }
158 my $self = {
159 confhooks => {},
160 mesghooks => [],
161 envelope => $envelope,
162 interactive => $interactive,
163 maintainer => $maintainer
164 };
165 bless ($self, $class);
166 }
167  
168 # Takes a list of modules as arguments and installs all of the callbacks for
169 # those modules (as determined by the %HOOKS hash defined above). The
170 # arguments to this constructor can just be a list of modules, or can be an
171 # intermixed list of modules and module/argument pairs. Arguments must be
172 # anonymous arrays, and if arguments are present for a module, they're
173 # passed in to the _init method of the module.
174 sub modules {
175 my $self = shift;
176 my $module;
177 while (defined ($module = shift)) {
178 unless (defined $HOOKS{$module}) {
179 $self->error ("Unknown module $module");
180 }
181 my @hook = @{$HOOKS{$module}};
182 if (ref $_[0]) {
183 my $method = $module . '_init';
184 $self->$method (@{+shift});
185 }
186 push (@{$$self{mesghooks}}, $module);
187 for (@hook) { push (@{$$self{confhooks}{$_}}, $module) }
188 }
189 }
190  
191  
192 ############################################################################
193 # Message handling
194 ############################################################################
195  
196 # Read in the article from a source, returning undef if this fails (either
197 # because no data is available or because the size limits are exceeded) and
198 # the number of bytes read if it succeeds. This is mostly just a wrapper
199 # around the News::Article constructor.
200 sub read {
201 my $self = shift;
202 $$self{article} = News::Article->new (@_);
203 }
204  
205 # Apply the given modules, or if none are given, apply all of the ones we
206 # have pending. Each hook should return undef on success and a string
207 # indicating the error message on failure; as soon as any hook fails, this
208 # method aborts and returns a list consisting of the name of the module that
209 # failed and the error message in an array context or the message prepended
210 # with the name of the module in a scalar context.
211 sub apply {
212 my $self = shift;
213 my @modules = @_ ? @_ : @{$$self{mesghooks}};
214 for (@modules) {
215 my $module = $_ . '_mesg';
216 my $error = $self->$module ();
217 if ($error) { return wantarray ? ($_, $error) : "$_: $error" }
218 }
219 }
220  
221  
222 ############################################################################
223 # Error handling
224 ############################################################################
225  
226 # Handle a fatal script error. This shouldn't be used for article
227 # rejections, only for syntax errors, fatal system problems, and the like.
228 # If we're interactive, we just die with the error message; otherwise, we
229 # call mail_error().
230 sub error {
231 my ($self, $error) = @_;
232 if ($$self{interactive}) {
233 die $error . "\n";
234 } else {
235 $self->mail_error ($error);
236 }
237 }
238  
239  
240 ############################################################################
241 # Accessors
242 ############################################################################
243  
244 # Return a reference to the underlying article.
245 sub get_article { $_[0]{article} }
246  
247 # Replace the current article with a new News::Article reference.
248 sub set_article { $_[0]{article} = $_[1] }
249  
250 1;