[vhffs-dev] [2084] Reworked MHonArc mailing lists archives

[ Thread Index | Date Index | More vhffs.org/vhffs-dev Archives ]


Revision: 2084
Author:   gradator
Date:     2012-03-01 01:16:36 +0100 (Thu, 01 Mar 2012)
Log Message:
-----------
Reworked MHonArc mailing lists archives

Modified Paths:
--------------
    trunk/vhffs-api/src/Vhffs/Robots/MailingList.pm
    trunk/vhffs-robots/Makefile.am

Added Paths:
-----------
    trunk/vhffs-robots/src/mailinglist_mhonarc.pl

Removed Paths:
-------------
    trunk/vhffs-robots/src/listengine_publicarchives.pl

Modified: trunk/vhffs-api/src/Vhffs/Robots/MailingList.pm
===================================================================
--- trunk/vhffs-api/src/Vhffs/Robots/MailingList.pm	2012-02-29 20:49:16 UTC (rev 2083)
+++ trunk/vhffs-api/src/Vhffs/Robots/MailingList.pm	2012-03-01 00:16:36 UTC (rev 2084)
@@ -80,4 +80,164 @@
 	return 1;
 }
 
+sub mhonarc_archives {
+	require File::Path;
+	require Template;
+
+	my $ml = shift;
+	return undef unless defined $ml;
+
+	my $vhffs = $ml->get_main;
+	my $listengineconfig = $vhffs->get_config->get_listengine;
+	my $mhonarcconfig = '%VHFFS_BOTS_DIR%/misc/mhonarc.config';
+
+	my $publicdir = $listengineconfig->{'datadir'}.'/public/'.$ml->get_domain.'/'.$ml->get_localpart;
+	my $archivedir = $listengineconfig->{'datadir'}.'/archives/'.$ml->get_domain.'/'.$ml->get_localpart;
+
+	# delete previous public archives (if available) if there is no public archives for this list
+	unless( $ml->get_open_archive )  {
+		File::Path::remove_tree($publicdir);	
+		return 1;
+	}
+
+	File::Path::make_path( $publicdir, { mode => 0755, error => \my $errors } );
+	if(@$errors) {
+		Vhffs::Robots::vhffs_log( $vhffs, 'An error occured while creating mailing list archives '.$publicdir.' directory: '.join(', ', @$errors) );
+		return undef;
+	}
+
+	# -- index : main page
+	my $template = new Template({
+		INCLUDE_PATH => '%VHFFS_BOTS_DIR%/misc/',
+	});
+
+	my $monthly_data = [];
+	my $vars = {
+		list => $ml,
+		monthly_data => $monthly_data
+	};
+
+	opendir( my $listdir, $archivedir ) or return undef;
+	my @years = readdir( $listdir );
+	closedir( $listdir );
+	foreach my $year ( reverse sort @years ) {
+		next if $year =~ /^\..*$/;
+
+		my $archivesyeardir = $archivedir.'/'.$year;
+		opendir( my $yeardir, $archivesyeardir );
+		unless( defined $yeardir ) {
+			Vhffs::Robots::vhffs_log( $vhffs, 'Unable to open '.$archivesyeardir.' directory: '.$! );
+			return undef;;
+		}
+
+		my $publicyeardir = $publicdir.'/'.$year;
+		mkdir($publicyeardir, 0755);
+		unless( -d $publicyeardir ) {
+			Vhffs::Robots::vhffs_log( $vhffs, 'Unable to create '.$publicyeardir.' directory: '.$! );
+			return undef;
+		}
+
+		my @months = readdir( $yeardir );
+		closedir( $yeardir );
+		foreach my $month ( reverse sort @months ) {
+			next if $month =~ /^\..*$/;
+
+			my $archivesmonthdir = $archivesyeardir.'/'.$month;
+			opendir( my $monthdir, $archivesmonthdir );
+			unless( defined $monthdir ) {
+				Vhffs::Robots::vhffs_log( $vhffs, 'Unable to open '.$monthdir.' directory: '.$! );
+				return undef;
+			}
+
+			my $publicmonthdir = $publicyeardir.'/'.$month;
+			mkdir($publicmonthdir, 0755);
+			unless( -d $publicmonthdir ) {
+				Vhffs::Robots::vhffs_log( $vhffs, 'Unable to create '.$publicmonthdir.' directory: '.$! );
+				return undef;
+			}
+
+			# TODO: as we need to parse folder recursively, remove the glob
+			my $childpid = open( my $output, '-|', 'mhonarc', '-add', '-quiet', '-rc', $mhonarcconfig, '-definevar', 'MAIN-TITLE='.$ml->get_domain.'/'.$ml->get_localpart, '-outdir', $publicmonthdir, glob($archivesmonthdir.'/*/*') );
+			if($childpid) {
+				# read process output and print
+				while(<$output>) { print $_; }
+
+				# wait for the child to finish
+				waitpid( $childpid, 0 );
+			}
+
+			# -- index : part
+			my $month = {
+				year => $year,
+				month => $month
+			};
+
+			my $mailnb = 0;
+			my $mailsize = 0;
+			my @glob;
+
+			my @days = readdir ( $monthdir);
+			closedir( $monthdir );
+			foreach my $day ( @days ) {
+				next if $day =~ /^\..*$/;
+
+				my $daypath = $archivesmonthdir.'/'.$day;
+
+				opendir( my $daydir, $daypath );
+				unless( defined $daydir ) {
+					Vhffs::Robots::vhffs_log( $vhffs, 'Unable to open '.$daydir.' directory: '.$! );
+					return undef;
+				}
+
+				my @mails = readdir ( $daydir );
+				closedir( $daydir );
+				foreach my $mail ( @mails ) {
+					next if $mail =~ /^\..*$/;
+
+					my $mailpath = $daypath.'/'.$mail;
+
+					push @glob, $mailpath; 
+					if( @glob >= 100 ) {
+						my $childpid = open( my $output, '-|', 'mhonarc', '-add', '-quiet', '-rc', $mhonarcconfig, '-definevar', 'MAIN-TITLE='.$ml->get_domain.'/'.$ml->get_localpart, '-outdir', $publicmonthdir, @glob );
+						if($childpid) {
+						# read process output and print
+						while(<$output>) { print $_; }
+
+							# wait for the child to finish
+							waitpid( $childpid, 0 );
+						}
+
+						@glob = ();
+					}
+					$mailnb++;
+
+					my (undef,undef,undef,undef,undef,undef,undef,$size,undef,undef,undef,undef,undef) = stat( $mailpath );
+					$mailsize += $size;
+				}
+			}
+
+			# Handle remaining mails
+			if( @glob ) {
+				my $childpid = open( my $output, '-|', 'mhonarc', '-add', '-quiet', '-rc', $mhonarcconfig, '-definevar', 'MAIN-TITLE='.$ml->get_domain.'/'.$ml->get_localpart, '-outdir', $publicmonthdir, @glob );
+				if($childpid) {
+				# read process output and print
+				while(<$output>) { print $_; }
+
+					# wait for the child to finish
+					waitpid( $childpid, 0 );
+				}
+			}
+
+			my $totalsize = sprintf('%d', $mailsize/1000 );
+			$month->{number} = $mailnb;
+			$month->{size} = $totalsize.'kB';
+
+			push @$monthly_data, $month;
+		}
+	}
+
+	$template->process( 'mhonarc.indexmain.tt', $vars, $publicdir.'/index.html' );
+	return 1;
+}
+
 1;

Modified: trunk/vhffs-robots/Makefile.am
===================================================================
--- trunk/vhffs-robots/Makefile.am	2012-02-29 20:49:16 UTC (rev 2083)
+++ trunk/vhffs-robots/Makefile.am	2012-03-01 00:16:36 UTC (rev 2084)
@@ -25,9 +25,9 @@
 	src/dns.pl \
 	src/group.pl \
 	src/group_quota.pl \
-	src/listengine_publicarchives.pl \
 	src/mail.pl \
 	src/mailinglist.pl \
+	src/mailinglist_mhonarc.pl \
 	src/mysql.pl \
 	src/mysql_dump.pl \
 	src/object_cleanup.pl \

Deleted: trunk/vhffs-robots/src/listengine_publicarchives.pl
===================================================================
--- trunk/vhffs-robots/src/listengine_publicarchives.pl	2012-02-29 20:49:16 UTC (rev 2083)
+++ trunk/vhffs-robots/src/listengine_publicarchives.pl	2012-03-01 00:16:36 UTC (rev 2084)
@@ -1,167 +0,0 @@
-#!%PERL% -w 
-
-# TODO: Rework that....
-
-use strict;
-use utf8;
-
-use lib '%VHFFS_LIB_DIR%';
-use Vhffs::Main;
-use Vhffs::Group;
-use Vhffs::Functions;
-use Vhffs::Services::MailingList;
-use Vhffs::Constants;
-use Vhffs::Robots;
-use File::Path;
-use Template;
-
-my $vhffs = init Vhffs::Main;
-die "Cannot init vhffs" unless defined $vhffs;
-
-my $listdir;
-my $outputlistdir;
-my @years;
-my $year;
-my $yearpath;
-my $outputyeardir;
-my @months;
-my $month;
-my $monthpath;
-my $outputmonthdir;
-my $cmd;
-
-my $listengineconfig = $vhffs->get_config->get_listengine;
-die "Cannot find listengine configuration" unless $listengineconfig;
-
-my $archivedir = $listengineconfig->{'datadir'}."/archives";
-my $outputdir = $listengineconfig->{'datadir'}."/public";
-
-my $miscdir = "/usr/lib/vhffs/bots/misc/";
-
-my $configmhonarc = $miscdir."/mhonarc.config";
-die "mhonarc configuration file unavailable" unless( -f $configmhonarc );
-
-File::Path::make_path( $outputdir, { mode => 0755 } );
-die "Cannot create ".$outputdir." directory\n" unless( -d $outputdir );
-
-Vhffs::Robots::lock( $vhffs , "listenginearchives" );
-
-my $lists = Vhffs::Services::MailingList::getall( $vhffs , Vhffs::Constants::ACTIVATED , undef );
-foreach my $list ( @{$lists} )
-{
-	next if ( !defined $list->get_domain  ||  !defined $list->get_localpart  ||  $list->get_domain eq ""  ||  $list->get_localpart eq "" );
-
-	# delete previous public archives (if available) if there is no public archives for this list
-	if ( $list->get_open_archive == 0 )  {
-		my $publicdir = $outputdir."/".$list->get_domain."/".$list->get_localpart;
-		if ( -d $publicdir)  {
-			File::Path::remove_tree($publicdir);
-		}
-		next;
-	}
-
-	$listdir = $archivedir."/".$list->get_domain."/".$list->get_localpart;
-	next unless( opendir( LISTDIR , $listdir ) );
-
-	$outputlistdir = $outputdir."/".$list->get_domain;
-	mkdir($outputlistdir, 0755) or die("Unable to create $outputlistdir directory: $!\n") unless(-d $outputlistdir);
-
-	$outputlistdir = $outputdir."/".$list->get_domain."/".$list->get_localpart;
-	mkdir($outputlistdir, 0755) or die("Unable to create $outputlistdir directory: $!\n") unless(-d $outputlistdir);
-
-	# -- index : main page
-	my $template = new Template({
-		INCLUDE_PATH => $miscdir,
-	});
-
-	my $vars = {
-		list => $list
-	};
-	my $monthly_data = [];
-
-	@years = readdir( LISTDIR );
-	foreach $year ( reverse sort @years )
-	{
-		next if( $year =~ /^\..*$/ );
-
-		$yearpath = $listdir."/".$year;
-
-		unless( defined opendir( YEARDIR , $yearpath ) ) {
-			warn "Unable to open $yearpath: $!\n";
-			next;
-		}
-
-		$outputyeardir = $outputlistdir."/".$year;
-		mkdir($outputyeardir, 0755) or die("Unable to create $outputyeardir directory: $!\n") unless(-d $outputyeardir);
-
-		@months = readdir( YEARDIR );
-		foreach $month ( reverse sort @months )
-		{
-			next if( $month =~ /^\..*$/ );
-
-			$monthpath = $yearpath."/".$month;
-
-			$outputmonthdir = $outputyeardir."/".$month;
-			mkdir($outputmonthdir, 0755) or die("Unable to create $outputmonthdir directory: $!\n") unless(-d $outputmonthdir);
-
-			# TODO: as we need to parse folder recursively, remove the glob
-			my $childpid = open( my $output, '-|', 'mhonarc', '-add', '-quiet', '-rc', $configmhonarc, '-definevar', 'MAIN-TITLE='.$list->get_domain.'/'.$list->get_localpart, '-outdir', $outputmonthdir, glob($monthpath.'/*/*') );
-			if($childpid) {
-				# read process output and print
-				while(<$output>) { print $_; }
-
-				# wait for the child to finish
-				waitpid( $childpid, 0 );
-			}
-
-			# -- index : part
-			my $month = {
-				year => $year,
-				month => $month
-			};
-
-			if( defined opendir( MONTHDIR , $monthpath ) )  {
-
-				my $mailnb = 0;
-				my $mailsize = 0;
-
-				my @days = readdir ( MONTHDIR );
-				foreach my $day ( @days )
-				{
-					next if( $day =~ /^\..*$/ );
-
-					my $daypath = $monthpath."/".$day;
-
-					next if( !defined opendir( DAYDIR , $daypath ) );
-
-					my @mails = readdir ( DAYDIR );
-					foreach my $mail ( @mails )
-					{
-						next if( $mail =~ /^\..*$/ );
-
-						$mailnb++;
-
-						my $size;
-						(undef,undef,undef,undef,undef,undef,undef,$size,undef,undef,undef,undef,undef) = stat( $daypath."/".$mail );
-						$mailsize += $size;
-					}
-				}
-
-				my $totalsize = sprintf("%d", $mailsize/1024 );
-				$month->{number} = $mailnb;
-				$month->{size} = $totalsize.'KB';
-			}
-
-			push @$monthly_data, $month;
-		}
-
-		closedir ( YEARDIR );
-	}
-
-	$vars->{monthly_data} = $monthly_data;
-	$template->process( 'mhonarc.indexmain.tt', $vars, "$outputlistdir/index.html" );
-
-	closedir( LISTDIR );
-}
-
-Vhffs::Robots::unlock( $vhffs , "listenginearchives" );

Copied: trunk/vhffs-robots/src/mailinglist_mhonarc.pl (from rev 2080, trunk/vhffs-robots/src/listengine_publicarchives.pl)
===================================================================
--- trunk/vhffs-robots/src/mailinglist_mhonarc.pl	                        (rev 0)
+++ trunk/vhffs-robots/src/mailinglist_mhonarc.pl	2012-03-01 00:16:36 UTC (rev 2084)
@@ -0,0 +1,55 @@
+#!%PERL% -w 
+# Copyright (c) vhffs project and its contributors
+# All rights reserved.
+#
+# Redistribution and use in source and binary forms, with or without 
+# modification, are permitted provided that the following conditions 
+# are met:
+#
+# 1. Redistributions of source code must retain the above copyright 
+#   notice, this list of conditions and the following disclaimer.
+#2. Redistributions in binary form must reproduce the above copyright
+#   notice, this list of conditions and the following disclaimer in 
+#   the documentation and/or other materials provided with the 
+#   distribution.
+#3. Neither the name of vhffs nor the names of its contributors 
+#   may be used to endorse or promote products derived from this 
+#   software without specific prior written permission.
+#
+#THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
+#"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
+#LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 
+#FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 
+#COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 
+#INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 
+#BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 
+#LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 
+#CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 
+# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 
+# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
+# POSSIBILITY OF SUCH DAMAGE.
+
+use strict;
+use utf8;
+
+use lib '%VHFFS_LIB_DIR%';
+use Vhffs::Robots::MailingList;
+
+my $vhffs = init Vhffs::Main;
+exit 1 unless defined $vhffs;
+
+my $listengineconfig = $vhffs->get_config->get_listengine;
+die 'Cannot find listengine configuration' unless defined $listengineconfig;
+
+my $mhonarcconfig = '%VHFFS_BOTS_DIR%/misc/mhonarc.config';
+die 'MHonArc configuration file unavailable' unless -f $mhonarcconfig;
+
+Vhffs::Robots::lock( $vhffs, 'mailinglistarchives' );
+
+my $lists = Vhffs::Services::MailingList::getall( $vhffs, Vhffs::Constants::ACTIVATED );
+foreach ( @{$lists} ) {
+	Vhffs::Robots::MailingList::mhonarc_archives( $_ );
+}
+
+Vhffs::Robots::unlock( $vhffs, 'mailinglistarchives' );
+exit 0;


Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/