#!/usr/pkg/bin/perl
# -*- indent-tabs-mode: nil; -*-
# vim:ft=perl:et:sw=4
# $Id: mod2html.pl.in 12121 2015-04-29 03:02:50Z sikeda $

use lib '/usr/pkg/sympa/bin';
use strict;
use warnings;
use English qw(-no_match_vars);

use Sympa::Archive;
use Conf;
use Sympa::Constants;
use Sympa::DatabaseManager;
use Sympa::Log;
use Sympa::Message;
use Sympa::Spool;

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

unless (Conf::load(Sympa::Constants::CONFIG)) {
    die "Can't load Sympa configuration file";
}
$log->openlog(
    $Conf::Conf{'syslog'},
    $Conf::Conf{'log_socket_type'},
    service => 'sympa/mod2html'
);

if ($Conf::Conf{'db_name'} and $Conf::Conf{'db_type'}) {
    unless (Sympa::DatabaseManager::probe_db()) {
        die "Sympa can't connect to database";
    }
}    #  to check availabity of Sympa database

# Set the UserID & GroupID for the process
$GID = $EGID = (getgrnam(Sympa::Constants::GROUP))[2];
$UID = $EUID = (getpwnam(Sympa::Constants::USER))[2];

# Sets the UMASK
umask(oct($Conf::Conf{'umask'}));

my $modspool = $Conf::Conf{'queuemod'};

## Loads message list
unless ($modspool and opendir SPOOL, $modspool) {
    die "Unable to read spool";
}

foreach my $msg_file (sort grep(!/^\./, readdir SPOOL)) {
    next if $msg_file =~ /^\./;

    my $metadata = Sympa::Spool::unmarshal_metadata(
        $modspool, $msg_file,
        qr{\A([^\s\@]+)(?:\@([\w\.\-]+))?_([^_]+)\z},
        [qw(localpart domainpart modkey)]
    );
    # moderation spool keeps messages only with List context.
    unless ($metadata and ref($metadata->{context}) eq 'Sympa::List') {
        next;
    }

    my $message = Sympa::Message->new_from_file($modspool . '/' . $msg_file,
        %$metadata);
    next unless $message;
    my $list = $message->{context};

    print "Creating HTML version for $modspool/$msg_file\n";

    my $destination_dir =
          $Conf::Conf{'viewmail_dir'} . '/mod/'
        . $list->get_list_id() . '/'
        . $metadata->{'modkey'};
    Sympa::Archive::html_format(
        $message,
        'destination_dir' => $destination_dir,
        'attachement_url' => join(
            '/', '..', 'viewmod', $list->{'name'}, $metadata->{'modkey'}
        ),
    );
}
closedir SPOOL;

__END__

=encoding utf-8

=head1 NAME

mod2html, mod2html.pl - Generates HTML view of messages to be moderated

=head1 SYNOPSIS

  mod2html.pl

=head1 DESCRIPTION

Until Sympa release 3.4.3.1, HTML view of moderated messages was created by
wwsympa.fgci, when needed.
It is now created by sympa.pl when the message is received.

This script will create all missing HTML files.

=head1 CONFIGURATION PARAMETERS

Following site configuration parameters in F</usr/pkg/etc/sympa/sympa.conf> will be referred.

=over

=item mhonarc

Path to MHonArc mail2html plugin.

=item queuemod

Directory for moderation spool.

=item viewmail_dir

Directory containing HTML file generated by MHonArc while displaying messages other than archives.

=back

=head1 HISTORY

mod2html.pl appeared on Sympa 3.4.4.

6.2a.32 or earlier stored HTML view into moderation spool.
6.2b has dedicated directory specified by viewmail_dir parameter.

=cut
