upgrade_bulk_spool.pl.in 9.55 KB
Newer Older
1
2
3
#! --PERL--
# -*- indent-tabs-mode: nil; -*-
# vim:ft=perl:et:sw=4
sikeda's avatar
sikeda committed
4
# $Id$
5
6
7
8
9
10

# 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
11
# Copyright (c) 2011, 2012, 2013, 2014, 2015, 2016, 2017 GIP RENATER
12
13
14
# 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>.
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#
# 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/>.

29
use lib split(/:/, $ENV{SYMPALIB} || ''), '--modulesdir--';
30
31
32
33
34
35
36
37
38
39
use strict;
use warnings;
use English qw(-no_match_vars);
use Getopt::Long;
use MIME::Base64 qw();
use Pod::Usage;

use Sympa::Bulk;
use Conf;
use Sympa::Constants;
40
use Sympa::DatabaseManager;
41
use Sympa::Log;
42
use Sympa::Spool;
43
44
45
46
47

my %options;
unless (GetOptions(\%options, 'help|h', 'dry_run', 'version|v')) {
    pod2usage(-exitval => 1, -output => \*STDERR);
}
48
if ($options{'help'}) {
49
    pod2usage(0);
50
} elsif ($options{'version'}) {
51
52
53
54
    printf "Sympa %s\n", Sympa::Constants::VERSION;
    exit 0;
}

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

57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# Load sympa.conf
unless (Conf::load(Conf::get_sympa_conf(), 'no_db')) {
    die sprintf
        'Unable to load Sympa configuration, file %s or one of the virtual host robot.conf files contain errors. Exiting',
        Conf::get_sympa_conf();
}

# Set the User ID & Group ID for the process
$GID = $EGID = (getgrnam(Sympa::Constants::GROUP))[2];
$UID = $EUID = (getpwnam(Sympa::Constants::USER))[2];
# Required on FreeBSD to change ALL IDs (effective UID + real UID + saved UID)
POSIX::setuid((getpwnam(Sympa::Constants::USER))[2]);
POSIX::setgid((getgrnam(Sympa::Constants::GROUP))[2]);
# Check if the UID has correctly been set (useful on OS X)
unless (($GID == (getgrnam(Sympa::Constants::GROUP))[2])
    && ($UID == (getpwnam(Sympa::Constants::USER))[2])) {
    die
        "Failed to change process user ID and group ID. Note that on some OS Perl scripts can't change their real UID. In such circumstances Sympa should be run via sudo.";
}
# Sets the UMASK
77
78
79
umask oct $Conf::Conf{'umask'};

# We won't open log: Output messages to STDERR.
80
81
82
83
84

process();
exit 0;

sub process {
85
86
    my $bulk = Sympa::Bulk->new;

87
88
89
    my $sdm = Sympa::DatabaseManager->instance
        or die 'Can\'t connect to database';

90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
    # Checking if bulkmailer_table exits
    my @tables;
    my $list_of_tables;
    if ($list_of_tables = $sdm->get_tables()) {
        @tables = @{$list_of_tables};
    } else {
        @tables = ();
    }
    my $found = 0;
    foreach my $t (@tables) {
        if ($t eq "bulkmailer_table") {
            $found = 1;
            last;
        }
    }
    if ($found == 0) {
        $log->syslog(
            'notice',
            'bulkmailer table not found in database, bulk spool of your Sympa is probably up-to-date'
        );
        exit 0;
    }

113
    my $sth = $sdm->do_prepared_query(
114
        q{SELECT *
115
116
          FROM bulkmailer_table
          WHERE returnpath_bulkmailer IS NOT NULL}
117
118
119
120
121
122
123
    );
    unless ($sth) {
        die
            'Cannot execute SQL query.  Database is inaccessible or bulk spool of your Sympa is up-to-date';
    }
    my ($row_mailer, $row_spool);
    while ($row_mailer = $sth->fetchrow_hashref('NAME_lc')) {
124
        if ($row_mailer->{lock_bulkmailer}) {
125
            $log->syslog(
126
127
128
129
130
131
132
133
                'info',
                'Packet %s is locked.  Skipping',
                $row_mailer->{packetid_bulkmailer}
            );
            next;
        }
        my $packetid = $row_mailer->{packetid_bulkmailer};

134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
        my ($list, $robot_id);
        $list = Sympa::List->new(
            $row_mailer->{listname_bulkmailer},
            $row_mailer->{robot_bulkmailer},
            {just_try => 1}
        );
        if ($list) {
            $robot_id = $list->{'domain'};
        } else {
            $robot_id = $row_mailer->{robot_bulkmailer} || '*';
        }
        my $packet_priority =
            Conf::get_robot_conf($robot_id, 'sympa_packet_priority');

        # Fetch message
        my $messagekey = $row_mailer->{messagekey_bulkmailer};
150
        my $sth2       = $sdm->do_prepared_query(
151
            q{SELECT *
152
153
              FROM bulkspool_table
              WHERE messagekey_bulkspool = ?},
154
155
156
157
158
            $messagekey
        );
        unless ($sth2) {
            die 'Fatal: Cannot execute SQL query';
        }
159
        $row_spool = $sth2->fetchrow_hashref('NAME_lc');
160
161
        $sth2->finish;
        unless ($row_spool) {
162
            $log->syslog('err', '%s: No messages found.  Skipping',
163
164
165
166
167
168
169
170
171
172
173
174
175
                $messagekey);
            $sth2->finish;
            next;
        }

        my $msg_string =
            MIME::Base64::decode($row_spool->{message_bulkspool});
        my $message = Sympa::Message->new(
            $msg_string,
            context => ($list || $robot_id),
            messagekey => $messagekey
        );

176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
        printf
            "---\nMessage:       %s\nPacket:        %s\nDelivery Date: %s\nRecipients:    %s\nContext:       %s\@%s\n\nMessage-ID:    %s\nFrom:          %s\nSubject:       %s\n",
            $messagekey, $packetid,
            POSIX::strftime('%Y-%m-%d %H:%M:%S',
            localtime $row_mailer->{delivery_date_bulkmailer}),
            ($row_mailer->{receipients_bulkmailer} || 'NONE'),
            ($row_mailer->{listname_bulkmailer}    || ''),
            $row_mailer->{robot_bulkmailer},
            ($row_spool->{messageid_bulkspool} || ''),
            ($message->get_header('From', ', ') || ''),
            ($message->{decoded_subject} || '');

        print "Migrate this message? [yN] ";
        my $answer = <STDIN>;
        chomp $answer;
        next unless lc $answer eq 'y';

        # Update message and messagekey.

195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
        $message->{date} = $row_mailer->{delivery_date_bulkmailer}  || time;
        $message->{time} = $row_mailer->{reception_date_bulkmailer} || time;

        $message->{priority} = $row_mailer->{priority_message_bulkmailer};
        $message->{packet_priority} = $packet_priority;
        if ($packet_priority lt $row_mailer->{priority_packet_bulkmailer}) {
            $message->{tag} = 'z';
        } else {
            $message->{tag} = '0';
        }

        $message->{shelved}{dkim_sign} = 1
            if $row_spool->{dkim_privatekey_bulkspool};
        $message->{message_id} = $row_spool->{messageid_bulkspool}
            if $row_spool->{messageid_bulkspool};
        $message->{envelope_sender} = $row_mailer->{returnpath_bulkmailer};
        $message->{shelved}{tracking} = 'verp'
            if $row_mailer->{verp_bulkmailer};
        $message->{shelved}{tracking} = $row_mailer->{tracking_bulkmailer}
            if $row_mailer->{tracking_bulkmailer};
        $message->{shelved}{merge} = 1 if $row_mailer->{merge_bulkmailer};

        # Not a typo: column name was recEipients_bulkmailer.
        my $rcpt_string = $row_mailer->{receipients_bulkmailer};
219
        my $rcpt = [split /,/, $rcpt_string];
220
221

        my $marshalled;
222
        unless ($options{dry_run}) {
223
            $marshalled =
224
                $bulk->store($message, $rcpt, tag => $message->{tag});
225
        } else {
226
            $marshalled = Sympa::Spool::marshal_metadata(
227
228
229
230
231
232
233
                $message,
                '%s.%s.%d.%f.%s@%s_%s,?????,????',
                [   qw(priority packet_priority date time localpart domainpart tag)
                ]
            );
        }
        unless ($marshalled) {
234
            $log->syslog(
235
236
237
238
239
240
241
                'err',
                'Packet %s of message %s could not be migrated into new spool',
                $row_mailer->{packetid_bulkmailer},
                $message
            );
            next;
        } else {
242
            $log->syslog(
243
                'notice',
244
245
246
247
                'Packet %s of message %s was migrated into new spool as <%s>',
                $row_mailer->{packetid_bulkmailer},
                $message,
                $marshalled
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
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
            );
        }
    }
}
__END__

=encoding utf-8

=head1 NAME

upgrade_bulk_spool, upgrade_bulk_spool.pl - Migrating messages in bulk tables

=head1 SYNOMSIS

  upgrade_bulk_spool.pl [ --dry_run ]

=head1 DESCRIPTION

On Sympa earlier than 6.2, messages for bulk sending were stored into
bulk spool based on database tables.
Recent release of Sympa stores outbound messages into the spool based on
filesystem or sends them by Mailer directly.
This program migrates messages with old format in appropriate spool.

=head1 OPTIONS

=over

=item --dry_run

Shows what will be done but won't really perform upgrade process.

=back

=head1 RETURN VALUE

This program exits with status 0 if processing secceeded.
Otherwise exits with non-zero status.

=head1 CONFIGURATION OPTIONS

Following site configuration parameters in F<--CONFIG--> or
robot configuration parameters in C<robot.conf> are referred.

=over

=item db_type, db_name etc.

=item queuebulk

=item sympa_packet_priority

=item umask

=back

=head1 SEE ALSO

L<sympa.conf(5)>, L<Sympa::Bulk>, L<Sympa::Message>.

=head1 HISTORY

upgrade_bulk_spool.pl appeared on Sympa 6.2.

=cut