invite.pm 5.63 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
13
# Copyright 2018 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>.
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#
# 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::Request::Handler::invite;

use strict;
use warnings;
use Time::HiRes qw();

use Sympa;
use Sympa::Language;
use Sympa::Log;
use Sympa::Scenario;

39
use base qw(Sympa::Request::Handler);
40
41
42
43

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

44
45
46
47
use constant _action_scenario => 'invite';
use constant _action_regexp   => qr'reject|request_auth|do_it'i;
use constant _context_class   => 'Sympa::List';

48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# Old name: Sympa::Commands::invite().
sub _twist {
    my $self    = shift;
    my $request = shift;

    my $list    = $request->{context};
    my $which   = $list->{'name'};
    my $robot   = $list->{'domain'};
    my $sender  = $request->{sender};
    my $email   = $request->{email};
    my $comment = $request->{gecos};

    my $sympa = Sympa::get_address($robot, 'sympa');

    $language->set_lang($list->{'admin'}{'lang'});

    if ($list->is_list_member($email)) {
        $self->add_stash($request, 'user', 'already_subscriber',
66
            {'email' => $email, 'listname' => $list->{'name'}});
67
68
69
70
71
72
73
74
75
76
        $log->syslog(
            'err',
            'INVITE command rejected; user "%s" already member of list "%s"',
            $email,
            $which
        );
        return undef;
    }

    # Is the guest user allowed to subscribe in this list?
77
    # Emulating subscription privilege of target user.
78
    my $result =
79
80
        Sympa::Scenario::request_action($list, 'subscribe', 'md5',
        {sender => $email});
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
    my $action;
    $action = $result->{'action'} if ref $result eq 'HASH';

    unless (defined $action) {
        my $error = "Unable to evaluate scenario 'subscribe' for list $which";
        Sympa::send_notify_to_listmaster(
            $list,
            'mail_intern_error',
            {   error  => $error,
                who    => $sender,
                action => 'Command process',
            }
        );
        $self->add_stash($request, 'intern');
        return undef;
    }

98
    if ($action =~ /\Areject\b/i) {
99
100
        $log->syslog(
            'info',
101
            'INVITE %s %s from %s refused, not allowed (%.2f seconds, %d subscribers)',
102
103
104
105
106
107
            $which,
            $email,
            $sender,
            Time::HiRes::time() - $self->{start_time},
            $list->get_total()
        );
108
109
        $self->add_stash($request, 'auth', $result->{'reason'},
            {'email' => $email, template => $result->{'tt2'}});
110
        return undef;
111
    } else {
112
113
        my $spool_req     = Sympa::Spool::Auth->new;
        my $req_subscribe = Sympa::Request->new(
114
            context => $list,
115
            action  => 'subscribe',
116
            email   => $email,
117
            sender  => $sender,
118
        );
119
120
        my $keyauth = $spool_req->store($req_subscribe);

121
        my $cmd_line = $req_subscribe->cmd_line(canonic => 1);
122
123
124
125
126
127
        unless (
            Sympa::send_file(
                $list, 'invite', $email,
                {   user         => {email => $email, gecos => $comment},
                    requested_by => $sender,
                    keyauth      => $keyauth,
128
                    cmd          => $cmd_line,
129
                    # Compat. <= 6.2.14.
130
                    subject => sprintf('AUTH %s %s', $keyauth, $cmd_line),
131
132
133
                }
            )
            ) {
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
            $log->syslog('notice', 'Unable to send template "invite" to %s',
                $email);
            my $error = sprintf 'Unable to send template "invite" to %s',
                $email;
            Sympa::send_notify_to_listmaster(
                $list,
                'mail_intern_error',
                {   error  => $error,
                    who    => $sender,
                    action => 'Command process',
                }
            );
            $self->add_stash($request, 'intern');
            return undef;
        }
        $log->syslog(
            'info',
151
            'INVITE %s %s from %s accepted, auth requested (%.2f seconds, %d subscribers)',
152
153
154
155
156
157
158
159
160
161
162
163
164
165
            $which,
            $email,
            $sender,
            Time::HiRes::time() - $self->{start_time},
            $list->get_total()
        );
        $self->add_stash($request, 'notice', 'invite', {'email' => $email});
    }

    return 1;
}

1;
__END__
sikeda's avatar
sikeda committed
166

sikeda's avatar
sikeda committed
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
=encoding utf-8

=head1 NAME

Sympa::Request::Handler::invite - invite request handler

=head1 DESCRIPTION

Invites someone to subscribe a list by sending him
template 'invite'.

Subscription request of target user is stored into held request spool.

=head1 SEE ALSO

L<Sympa::Request::Handler>, L<Sympa::Spool::Auth>.

=head1 HISTORY

=cut