Aliases.pm 7.31 KB
Newer Older
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
# -*- indent-tabs-mode: nil; -*-
# vim:ft=perl:et:sw=4
# $Id$

# Sympa - SYsteme de Multi-Postage Automatique
#
# Copyright 2017 The Sympa Community. See the AUTHORS.md file at the top-level
# directory of this distribution and at
# <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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# Check listname.
sub check_new_listname {
    my $listname = shift;
    my $robot_id = shift;

    die 'bug in logic. Ask developer'
        unless defined $listname and length $listname;

    $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});
        }
    }

120
    # Avoid "sympa", "listmaster", "bounce" and "bounce+XXX".
121
    if (   $listname eq Conf::get_robot_conf($robot_id, 'email')
122
123
124
        or $listname eq Conf::get_robot_conf($robot_id, 'listmaster_email')
        or $listname eq 'bounce'
        or 0 == index($listname, 'bounce+')) {
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
        $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;
}

153
154
155
156
157
1;
__END__

=encoding utf-8

158
=head1 NAME
159
160
161

Sympa::Aliases - Base class for alias management

162
163
164
165
166
167
168
169
170
171
172
173
=head1 SYNOPSIS

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

174
175
=head1 DESCRIPTION 

176
This module is the base class for subclasses to manage list aliases of Sympa.
177
178
179
180
181

=head2 Methods

=over

182
=item new ( $type, [ key =E<gt> value, ... ] )
183
184
185
186

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

187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
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.

211
212
=item check ($listname, $robot_id)

213
214
I<Instance method>, I<overridable>.
Checks if the addresses of requested list exist already.
215
216
217
218
219
220
221
222

Parameters:

=over

=item $listname

Name of the list.
223
Mandatory.
224
225
226
227
228
229
230
231
232

=item $robot_id

List's robot.

=back

Returns:

233
234
235
236
237
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>.
238
239
240

=item add ($list)

241
I<Instance method>, I<overridable>.
242
243
244
245
246
247
248
249
250
251
252
253
254
255
Installs aliases for the list $list.

Parameters:

=over

=item $list

An instance of L<Sympa::List>.

=back

Returns:

256
257
258
259
260
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>.
261
262
263

=item del ($list)

264
I<Instance method>, I<overridable>.
265
266
267
268
269
270
271
272
273
274
275
276
277
278
Removes aliases for the list $list.

Parameters:

=over

=item $list

An instance of L<Sympa::List>.

=back

Returns:

279
280
281
282
283
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>.
284
285
286

=back

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

325
326
327
328
329
330
=head1 SEE ALSO

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

331
332
333
334
335
336
337
338
339
=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