Aliases.pm 7.17 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
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
# 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});
        }
    }

    if (   $listname eq Conf::get_robot_conf($robot_id, 'email')
        or $listname eq Conf::get_robot_conf($robot_id, 'listmaster_email')) {
        $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;
}

150
151
152
153
154
1;
__END__

=encoding utf-8

155
=head1 NAME
156
157
158

Sympa::Aliases - Base class for alias management

159
160
161
162
163
164
165
166
167
168
169
170
=head1 SYNOPSIS

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

171
172
=head1 DESCRIPTION 

173
This module is the base class for subclasses to manage list aliases of Sympa.
174
175
176
177
178

=head2 Methods

=over

179
=item new ( $type, [ key =E<gt> value, ... ] )
180
181
182
183

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

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

208
209
=item check ($listname, $robot_id)

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

Parameters:

=over

=item $listname

Name of the list.
220
Mandatory.
221
222
223
224
225
226
227
228
229

=item $robot_id

List's robot.

=back

Returns:

230
231
232
233
234
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>.
235
236
237

=item add ($list)

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

Parameters:

=over

=item $list

An instance of L<Sympa::List>.

=back

Returns:

253
254
255
256
257
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>.
258
259
260

=item del ($list)

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

Parameters:

=over

=item $list

An instance of L<Sympa::List>.

=back

Returns:

276
277
278
279
280
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>.
281
282
283

=back

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

322
323
324
325
326
327
=head1 SEE ALSO

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

328
329
330
331
332
333
334
335
336
=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