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

# Sympa - SYsteme de Multi-Postage Automatique
#
# Copyright (c) 1997, 1998, 1999 Institut Pasteur & Christophe Wolfhugel
# Copyright (c) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
# 2006, 2007, 2008, 2009, 2010, 2011 Comite Reseau des Universites
10
# Copyright (c) 2011, 2012, 2013, 2014, 2015, 2016, 2017 GIP RENATER
11
12
# Copyright 2017, 2020, 2021 The Sympa Community. See the
# AUTHORS.md file at the top-level directory of this distribution and at
13
# <https://github.com/sympa-community/sympa.git>.
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#
# 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::Robot;

use strict;
use warnings;
use Encode qw();

34
use Sympa;
35
use Conf;
36
use Sympa::DatabaseManager;
37
use Sympa::Language;
38
use Sympa::ListDef;
39
use Sympa::Log;
40
use Sympa::Tools::Data;
41
use Sympa::Tools::File;
42
use Sympa::Tools::Text;
43
44

my $language = Sympa::Language->instance;
45
my $log      = Sympa::Log->instance;
46
47
48
49
50
51
52
53
54
55

## Database and SQL statement handlers
my ($sth, @sth_stack);

our %list_of_topics = ();
## Last modification times
our %mtime;

our %listmaster_messages_stack;

56
# MOVED: Use Sympa::send_file(), or Sympa::Message::Template::new() with
57
# Sympa::Mailer::send_message().
58
# sub send_global_file($tpl, $who, $robot, $context, $options);
59

60
61
# MOVED: Use Sympa::send_notify_to_listmaster() or
# Sympa::Spool::Listmaster::flush().
62
# sub send_notify_to_listmaster($operation, $robot, $data, $checkstack, $purge);
63
64

## Is the user listmaster
65
66
# MOVED: Use Sympa::is_listmaster().
#sub is_listmaster;
67
68
69
70
71
72

## get idp xref to locally validated email address
sub get_netidtoemail_db {
    my $robot   = shift;
    my $netid   = shift;
    my $idpname = shift;
73
    $log->syslog('debug', '(%s, %s)', $netid, $idpname);
74
75
76
77
78

    my ($l, %which, $email);

    push @sth_stack, $sth;

79
    my $sdm = Sympa::DatabaseManager->instance;
80
    unless (
81
82
83
84
85
86
87
88
        $sdm
        and $sth = $sdm->do_prepared_query(
            q{SELECT email_netidmap
              FROM netidmap_table
              WHERE netid_netidmap = ? and serviceid_netidmap = ? and
                    robot_netidmap = ?},
            $netid, $idpname,
            $robot
89
        )
Luc Didry's avatar
Luc Didry committed
90
    ) {
91
        $log->syslog(
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
            'err',
            'Unable to get email address from netidmap_table for id %s, service %s, robot %s',
            $netid,
            $idpname,
            $robot
        );
        return undef;
    }

    $email = $sth->fetchrow;

    $sth->finish();

    $sth = pop @sth_stack;

    return $email;
}

## set idp xref to locally validated email address
sub set_netidtoemail_db {
    my $robot   = shift;
    my $netid   = shift;
    my $idpname = shift;
    my $email   = shift;
116
    $log->syslog('debug', '(%s, %s, %s)', $netid, $idpname, $email);
117
118
119

    my ($l, %which);

120
    my $sdm = Sympa::DatabaseManager->instance;
121
    unless (
122
123
124
125
126
        $sdm
        and $sdm->do_prepared_query(
            q{INSERT INTO netidmap_table
              (netid_netidmap, serviceid_netidmap, email_netidmap,
               robot_netidmap)
127
128
129
130
131
132
133
134
135
              SELECT ?, ?, ?, ?
              FROM dual
              WHERE NOT EXISTS (
                SELECT 1
                FROM netidmap_table
                WHERE netid_netidmap = ? AND serviceid_netidmap = ? AND
                      email_netidmap = ? AND robot_netidmap = ?
              )},
            $netid, $idpname, $email, $robot,
136
            $netid, $idpname, $email, $robot
137
        )
Luc Didry's avatar
Luc Didry committed
138
    ) {
139
        $log->syslog(
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
            'err',
            'Unable to set email address %s in netidmap_table for id %s, service %s, robot %s',
            $email,
            $netid,
            $idpname,
            $robot
        );
        return undef;
    }

    return 1;
}

## Update netidmap table when user email address changes
sub update_email_netidmap_db {
    my ($robot, $old_email, $new_email) = @_;

    unless (defined $robot
        && defined $old_email
        && defined $new_email) {
160
        $log->syslog('err', 'Missing parameter');
161
162
163
        return undef;
    }

164
    my $sdm = Sympa::DatabaseManager->instance;
165
    unless (
166
167
168
169
170
171
172
        $sdm
        and $sdm->do_prepared_query(
            q{UPDATE netidmap_table
              SET email_netidmap = ?
              WHERE email_netidmap = ? AND robot_netidmap = ?},
            $new_email,
            $old_email, $robot
173
        )
Luc Didry's avatar
Luc Didry committed
174
    ) {
175
        $log->syslog(
176
177
178
179
180
181
182
183
184
185
186
187
            'err',
            'Unable to set new email address %s in netidmap_table to replace old address %s for robot %s',
            $new_email,
            $old_email,
            $robot
        );
        return undef;
    }

    return 1;
}

188
189
my $default_topics_visibility = 'noconceal';

190
191
192
# Loads the list of topics if updated.
# The topic names "others" and "topicsless" are reserved words therefore
# ignored.  Note: "other" is not reserved and may be used.
193
194
sub load_topics {
    my $robot = shift;
195
    $log->syslog('debug2', '(%s)', $robot);
196

197
    my $conf_file = Sympa::search_fullpath($robot, 'topics.conf');
198
199

    unless ($conf_file) {
200
        $log->syslog('err', 'No topics.conf defined');
201
        return;
202
203
204
205
206
207
    }

    my $topics = {};

    ## Load if not loaded or changed on disk
    if (!$list_of_topics{$robot}
208
209
        or Sympa::Tools::File::get_mtime($conf_file) >
        $mtime{'topics'}{$robot}) {
210
211
212
213
214

        ## delete previous list of topics
        %list_of_topics = ();

        unless (-r $conf_file) {
215
            $log->syslog('err', 'Unable to read %s', $conf_file);
216
            return;
217
218
        }

219
220
        my $config_content = Sympa::Tools::Text::slurp($conf_file);
        unless (defined $config_content) {
221
            $log->syslog('err', 'Unable to open config file %s', $conf_file);
222
            return;
223
224
225
226
227
        }

        ## Rough parsing
        my $index = 0;
        my (@rough_data, $topic);
228
        foreach my $line (split /(?<=\n)(?=\n|.)/, $config_content) {
229
            if ($line =~ /\A(others|topicsless)\s*\z/i) {
230
231
                # "others" and "topicsless" are reserved words. Ignore.
                next;
232
            } elsif ($line =~ /^([\-\w\/]+)\s*$/) {
233
234
                $index++;
                $topic = {
235
                    'name'  => lc($1),
236
237
                    'order' => $index
                };
238
            } elsif ($line =~ /^([\w\.]+)\s+(.+\S)\s*$/) {
239
                next unless defined $topic->{'name'};
240
241

                $topic->{$1} = $2;
242
            } elsif ($line =~ /^\s*$/) {
243
244
245
246
247
248
249
250
251
252
253
254
255
                next unless defined $topic->{'name'};

                push @rough_data, $topic;
                $topic = {};
            }
        }

        ## Last topic
        if (defined $topic->{'name'}) {
            push @rough_data, $topic;
            $topic = {};
        }

256
        $mtime{'topics'}{$robot} = Sympa::Tools::File::get_mtime($conf_file);
257
258

        unless ($#rough_data > -1) {
259
            $log->syslog('notice', 'No topic defined in %s', $conf_file);
260
            return;
261
262
263
264
265
266
267
        }

        ## Analysis
        foreach my $topic (@rough_data) {
            my @tree = split '/', $topic->{'name'};

            if ($#tree == 0) {
268
                my $title = _load_topics_get_title($topic);
269
270
                $list_of_topics{$robot}{$tree[0]}{'title'} = $title;
                $list_of_topics{$robot}{$tree[0]}{'visibility'} =
271
                    $topic->{'visibility'} || $default_topics_visibility;
272
273
274
                $list_of_topics{$robot}{$tree[0]}{'order'} =
                    $topic->{'order'};
            } else {
Luc Didry's avatar
Luc Didry committed
275
276
                my $subtopic = join('/', @tree[1 .. $#tree]);
                my $title = _load_topics_get_title($topic);
277
278
                my $visibility =
                    $topic->{'visibility'} || $default_topics_visibility;
279
                $list_of_topics{$robot}{$tree[0]}{'sub'}{$subtopic} =
280
                    _add_topic($subtopic, $title, $visibility);
281
282
283
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
322
323
324
325
326
327
328
329
330
331
332
333
334
            }
        }

        ## Set undefined Topic (defined via subtopic)
        foreach my $t (keys %{$list_of_topics{$robot}}) {
            unless (defined $list_of_topics{$robot}{$t}{'title'}) {
                $list_of_topics{$robot}{$t}{'title'} = {'default' => $t};
            }
        }
    }

    ## Set the title in the current language
    my $lang = $language->get_lang;
    foreach my $top (keys %{$list_of_topics{$robot}}) {
        my $topic = $list_of_topics{$robot}{$top};
        foreach my $l (Sympa::Language::implicated_langs($lang)) {
            if (exists $topic->{'title'}{$l}) {
                $topic->{'current_title'} = $topic->{'title'}{$l};
            }
        }
        unless (exists $topic->{'current_title'}) {
            if (exists $topic->{'title'}{'gettext'}) {
                $topic->{'current_title'} =
                    $language->gettext($topic->{'title'}{'gettext'});
            } else {
                $topic->{'current_title'} = $topic->{'title'}{'default'}
                    || $top;
            }
        }

        foreach my $subtop (keys %{$topic->{'sub'}}) {
            foreach my $l (Sympa::Language::implicated_langs($lang)) {
                if (exists $topic->{'sub'}{$subtop}{'title'}{$l}) {
                    $topic->{'sub'}{$subtop}{'current_title'} =
                        $topic->{'sub'}{$subtop}{'title'}{$l};
                }
            }
            unless (exists $topic->{'sub'}{$subtop}{'current_title'}) {
                if (exists $topic->{'sub'}{$subtop}{'title'}{'gettext'}) {
                    $topic->{'sub'}{$subtop}{'current_title'} =
                        $language->gettext(
                        $topic->{'sub'}{$subtop}{'title'}{'gettext'});
                } else {
                    $topic->{'sub'}{$subtop}{'current_title'} =
                           $topic->{'sub'}{$subtop}{'title'}{'default'}
                        || $subtop;
                }
            }
        }
    }

    return %{$list_of_topics{$robot}};
}

335
336
# Old name: _get_topic_titles().
sub _load_topics_get_title {
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
    my $topic = shift;

    my $title;
    foreach my $key (%{$topic}) {
        if ($key =~ /^title\.gettext$/i) {
            $title->{'gettext'} = $topic->{$key};
        } elsif ($key =~ /^title\.(\S+)$/i) {
            my $lang = $1;
            # canonicalize lang if possible.
            $lang = Sympa::Language::canonic_lang($lang) || $lang;
            $title->{$lang} = $topic->{$key};
        } elsif ($key =~ /^title$/i) {
            $title->{'default'} = $topic->{$key};
        }
    }

    return $title;
}

## Inner sub used by load_topics()
sub _add_topic {
358
    my ($name, $title, $visibility) = @_;
359
360
361
362
    my $topic = {};

    my @tree = split '/', $name;
    if ($#tree == 0) {
363
        return {'title' => $title, 'visibility' => $visibility};
364
365
    } else {
        $topic->{'sub'}{$name} =
366
            _add_topic(join('/', @tree[1 .. $#tree]), $title, $visibility);
367
368
369
370
        return $topic;
    }
}

371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
sub topic_keys {
    my $robot_id = shift;

    my %topics = Sympa::Robot::load_topics($robot_id);
    return map {
        my $topic = $_;
        if ($topics{$topic}->{sub}) {
            (   $topic,
                map { $topic . '/' . $_ } sort keys %{$topics{$topic}->{sub}}
            );
        } else {
            ($topic);
        }
    } sort keys %topics;
}

sub topic_get_title {
    my $robot_id = shift;
Luc Didry's avatar
Luc Didry committed
389
    my $topic    = shift;
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422

    my $tinfo = {Sympa::Robot::load_topics($robot_id)};
    return unless %$tinfo;

    my @ttitles;
    my @tpaths = split '/', $topic;

    while (1) {
        my $t = shift @tpaths;
        unless (exists $tinfo->{$t}) {
            @ttitles = ();
            last;
        } elsif (not @tpaths) {
            push @ttitles, (_topic_get_title($tinfo->{$t}) || $t);
            last;
        } elsif (not $tinfo->{$t}->{sub}) {
            @ttitles = ();
            last;
        } else {
            push @ttitles, (_topic_get_title($tinfo->{$t}) || $t);
            $tinfo = $tinfo->{$t}->{sub};
        }
    }

    return @ttitles if wantarray;
    return join ' / ', @ttitles;
}

sub _topic_get_title {
    my $titem = shift;

    return undef unless $titem and exists $titem->{title};

Luc Didry's avatar
Luc Didry committed
423
424
    foreach my $lang (Sympa::Language::implicated_langs($language->get_lang))
    {
425
426
427
428
429
430
431
432
433
434
435
436
        return $titem->{title}->{$lang}
            if $titem->{title}->{$lang};
    }
    if ($titem->{title}->{gettext}) {
        return $language->gettext($titem->{title}->{gettext});
    } elsif ($titem->{title}->{default}) {
        return $titem->{title}->{default};
    } else {
        return undef;
    }
}

437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
=over 4

=item list_params

I<Getter>.
Returns hashref to list parameter information.

=back

=cut

# Old name: tools::get_list_params().
sub list_params {
    my $robot_id = shift;

452
    my $pinfo = Sympa::Tools::Data::clone_var(\%Sympa::ListDef::pinfo);
453
454
    $pinfo->{lang}{format} = [Sympa::get_supported_languages($robot_id)];

455
    my @topics = Sympa::Robot::topic_keys($robot_id);
456
457
458
459
460
    $pinfo->{topics}{format} = [@topics];
    # Compat.
    $pinfo->{topics}{file_format} = sprintf '(%s)(,(%s))*',
        join('|', @topics),
        join('|', @topics);
461
462
463
464

    return $pinfo;
}

465
1;