Aliases.pm 7.5 KB
Newer Older
1
2
3
4
5
6
# -*- indent-tabs-mode: nil; -*-
# vim:ft=perl:et:sw=4
# $Id$

# Sympa - SYsteme de Multi-Postage Automatique
#
7
8
# Copyright 2017, 2018 The Sympa Community. See the AUTHORS.md file at the
# top-level directory of this distribution and at
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# <https://github.com/sympa-community/sympa.git>.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

package Sympa::Aliases;

use strict;
use warnings;
use English qw(-no_match_vars);

30
use Conf;
31
use Sympa::Constants;
32
use Sympa::List;
33
use Sympa::Log;
34
use Sympa::Regexps;
35
36
37

my $log = Sympa::Log->instance;

IKEDA Soji's avatar
IKEDA Soji committed
38
39
# Sympa::Aliases is the proxy class of subclasses.
# The constructor may be overridden by _new() method.
40
sub new {
41
42
43
44
45
46
47
48
49
50
51
    my $class   = shift;
    my $type    = shift;
    my %options = @_;

    return undef unless $type;

    # Special cases:
    # - To disable aliases management, specify "none" as $type.
    # - "External" module is used for full path to program.
    # - However, "Template" module is used instead of obsoleted program
    #   alias_manager.pl.
IKEDA Soji's avatar
IKEDA Soji committed
52
    return $class->_new if $type eq 'none';
53
54
55
56
57
58

    if ($type eq Sympa::Constants::SBINDIR() . '/alias_manager.pl') {
        $type = 'Sympa::Aliases::Template';
    } elsif (0 == index $type, '/' and -x $type) {
        $options{program} = $type;
        $type = 'Sympa::Aliases::External';
59
60
    }

61
62
63
64
65
66
67
68
69
70
71
    # Returns appropriate subclasses.
    if ($type !~ /[^\w:]/) {
        $type = sprintf 'Sympa::Aliases::%s', $type unless $type =~ /::/;
        unless (eval sprintf('require %s', $type)
            and $type->isa('Sympa::Aliases')) {
            $log->syslog(
                'err', 'Unable to use %s module: %s',
                $type, $EVAL_ERROR || 'Not a Sympa::Aliases class'
            );
            return undef;
        }
IKEDA Soji's avatar
IKEDA Soji committed
72
        return $type->_new(%options);
73
74
75
76
77
    }

    return undef;
}

IKEDA Soji's avatar
IKEDA Soji committed
78
79
80
81
82
83
84
sub _new {
    my $class   = shift;
    my %options = @_;

    return bless {%options} => $class;
}

85
sub check {0}
86

87
sub add {0}
88

89
sub del {0}
90

91
92
93
94
95
# Check listname.
sub check_new_listname {
    my $listname = shift;
    my $robot_id = shift;

96
97
98
99
    unless (defined $listname and length $listname) {
        $log->syslog('err', 'No listname');
        return ('user', 'listname_needed');
    }
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121

    $listname = lc $listname;

    my $listname_re = Sympa::Regexps::listname();
    unless (defined $listname
        and $listname =~ /^$listname_re$/i
        and length $listname <= Sympa::Constants::LIST_LEN()) {
        $log->syslog('err', 'Incorrect listname %s', $listname);
        return ('user', 'incorrect_listname', {bad_listname => $listname});
    }

    my $regx = Conf::get_robot_conf($robot_id, 'list_check_regexp');
    if ($regx) {
        if ($listname =~ /^(\S+)-($regx)$/) {
            $log->syslog('err',
                'Incorrect listname %s matches one of service aliases',
                $listname);
            return ('user', 'listname_matches_aliases',
                {new_listname => $listname});
        }
    }

122
    # Avoid "sympa", "listmaster", "bounce" and "bounce+XXX".
123
    if (   $listname eq Conf::get_robot_conf($robot_id, 'email')
124
        or $listname eq Conf::get_robot_conf($robot_id, 'listmaster_email')
125
126
127
128
129
130
        or $listname eq Conf::get_robot_conf($robot_id, 'bounce_email_prefix')
        or 0 == index(
            $listname,
            Conf::get_robot_conf($robot_id, 'bounce_email_prefix') . '+'
        )
    ) {
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
        $log->syslog('err',
            'Incorrect listname %s matches one of service aliases',
            $listname);
        return ('user', 'listname_matches_aliases',
            {new_listname => $listname});
    }

    # Check listname on SMTP server.
    my $aliases =
        Sympa::Aliases->new(Conf::get_robot_conf($robot_id, 'alias_manager'));
    my $res = $aliases->check($listname, $robot_id) if $aliases;
    unless (defined $res) {
        $log->syslog('err', 'Can\'t check list %.128s on %s',
            $listname, $robot_id);
        return ('intern');
    }

    # Check this listname doesn't exist already.
    if ($res or Sympa::List->new($listname, $robot_id, {'just_try' => 1})) {
        $log->syslog('err',
            'Could not create list %s: list on %s already exist',
            $listname, $robot_id);
        return ('user', 'list_already_exists', {new_listname => $listname});
    }

    return;
}

159
160
161
162
163
1;
__END__

=encoding utf-8

164
=head1 NAME
165
166
167

Sympa::Aliases - Base class for alias management

168
169
170
171
172
173
174
175
176
177
178
179
=head1 SYNOPSIS

  package Sympa::Aliases::FOO;
  
  use base qw(Sympa::Aliases);
  
  sub check { ... }
  sub add { ... }
  sub del { ... }
  
  1;

180
181
=head1 DESCRIPTION 

182
This module is the base class for subclasses to manage list aliases of Sympa.
183
184
185
186
187

=head2 Methods

=over

188
=item new ( $type, [ key =E<gt> value, ... ] )
189
190
191
192

I<Constructor>.
Creates new instance of L<Sympa::Aliases>.

193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
Returns one of appropriate subclasses according to $type:

=over

=item C<'none'>

No aliases management.

=item Full path to executable

Use external program to manage aliases.
See L<Sympa::Aliases::External>.

=item Name of subclass

Use a subclass C<Sympa::Aliases::I<name>> to manage aliases.

=back

For invalid types returns C<undef>.

Optional C<I<key> =E<gt> I<value>> pairs are included in the instance as
hash entries.

217
218
=item check ($listname, $robot_id)

219
220
I<Instance method>, I<overridable>.
Checks if the addresses of requested list exist already.
221
222
223
224
225
226
227
228

Parameters:

=over

=item $listname

Name of the list.
229
Mandatory.
230
231
232
233
234
235
236
237
238

=item $robot_id

List's robot.

=back

Returns:

239
240
241
242
243
True value if one of addresses exists.
C<0> if none found.
C<undef> if something wrong happened.

By default, this method always returns C<0>.
244
245
246

=item add ($list)

247
I<Instance method>, I<overridable>.
248
249
250
251
252
253
254
255
256
257
258
259
260
261
Installs aliases for the list $list.

Parameters:

=over

=item $list

An instance of L<Sympa::List>.

=back

Returns:

262
263
264
265
266
C<1> if installation succeeded.
C<0> if there were no aliases to be installed.
C<undef> if not applicable.

By default, this method always returns C<0>.
267
268
269

=item del ($list)

270
I<Instance method>, I<overridable>.
271
272
273
274
275
276
277
278
279
280
281
282
283
284
Removes aliases for the list $list.

Parameters:

=over

=item $list

An instance of L<Sympa::List>.

=back

Returns:

285
286
287
288
289
C<1> if removal succeeded.
C<0> if there were no aliases to be removed.
C<undef> if not applicable.

By default, this method always returns C<0>.
290
291
292

=back

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
=head2 Function

=over

=item check_new_listname ( $listname, $robot )

I<Function>.
Checks if a new listname is allowed.

TBD.

Parameteres:

=over

=item $listname

A list name to be checked.

=item $robot

Robot context.

=back

Returns:

If check fails, an array including information of errors.
If it succeeds, empty array.

B<Note>:
This should be used to check name of list to be created.
Names of existing lists may not necessarily pass checks by this function.

This function was added on Sympa 6.2.37b.2.

=back

331
332
333
334
335
336
=head1 SEE ALSO

L<Sympa::Aliases::CheckSMTP>,
L<Sympa::Aliases::External>,
L<Sympa::Aliases::Template>.

337
338
339
340
341
342
343
344
345
=head1 HISTORY

F<alias_manager.pl> as a program to automate alias management appeared on
Sympa 3.1b.13.

L<Sympa::Aliases> module as an OO-based class appeared on Sympa 6.2.23b,
and it obsoleted F<alias_manager.pl>.

=cut