[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

SF.net SVN: ledger-smb: [1790] trunk/LedgerSMB



Revision: 1790
          http://ledger-smb.svn.sourceforge.net/ledger-smb/?rev=1790&view=rev
Author:   tetragon
Date:     2007-10-18 18:46:57 -0700 (Thu, 18 Oct 2007)

Log Message:
-----------
Rearrange LSMB::Mailer a bit to make templating simpler
Adjust backup function to work with LSMB::M rearrangement

Modified Paths:
--------------
    trunk/LedgerSMB/AM.pm
    trunk/LedgerSMB/Mailer.pm

Modified: trunk/LedgerSMB/AM.pm
===================================================================
--- trunk/LedgerSMB/AM.pm	2007-10-19 01:23:17 UTC (rev 1789)
+++ trunk/LedgerSMB/AM.pm	2007-10-19 01:46:57 UTC (rev 1790)
@@ -1656,6 +1656,7 @@
 
     my ( $self, $myconfig, $form ) = @_;
 
+    $form->{file} ||= lc "$myconfig->{templates}/$form->{template}.$form->{format}";
     $self->check_template_name( \%$myconfig, \%$form );
     open( TEMPLATE, '<', "$form->{file}" )
       or $form->error("$form->{file} : $!");
@@ -1679,6 +1680,7 @@
 
     my ( $self, $myconfig, $form ) = @_;
 
+    $form->{file} ||= lc "$myconfig->{templates}/$form->{template}.$form->{format}";
     $self->check_template_name( \%$myconfig, \%$form );
     open( TEMPLATE, '>', "$form->{file}" )
       or $form->error("$form->{file} : $!");
@@ -2093,25 +2095,36 @@
     # compress backup if gzip defined
     my $suffix = "c";
 
+	##SC: START Testing changes
+	$myconfig->{name} = "test";
+	$myconfig->{email} = '..hidden..';
+	$myconfig->{dbport} = 5432;
+	$myconfig->{dbuser} = 'seneca';
+	$myconfig->{dbhost} = 'localhost';
+	$myconfig->{dbname} = 'ledgersmb-taxtest';
+	##SC: END Testing changes
     if ( $form->{media} eq 'email' ) {
         print OUT
 qx(PGPASSWORD="$myconfig->{dbpasswd}" pg_dump -U $myconfig->{dbuser} -h $myconfig->{dbhost} -Fc -p $myconfig->{dbport} $myconfig->{dbname});
         close OUT;
         use LedgerSMB::Mailer;
-        $mail = new LedgerSMB::Mailer;
+        $mail = new LedgerSMB::Mailer(
+            to => qq|"$myconfig->{name}" <$myconfig->{email}>|,
+            from => qq|"$myconfig->{name}" <$myconfig->{email}>|,
+            subject => "LedgerSMB Backup / $globalDBname-$form->{dbversion}-$t[5]$t[4]$t[3].sql$suffix",
+            message => qq|
+This PostgreSQL backup can be restored using the pg_restore command.
 
-        $mail->{to}   = qq|"$myconfig->{name}" <$myconfig->{email}>|;
-        $mail->{from} = qq|"$myconfig->{name}" <$myconfig->{email}>|;
-        $mail->{subject} =
-"LedgerSMB Backup / $globalDBname-$form->{dbversion}-$t[5]$t[4]$t[3].sql$suffix";
-        @{ $mail->{attachments} } = ($tmpfile);
-        $mail->{version} = $form->{version};
-        $mail->{fileid}  = "$boundary.";
-        $mail->{format}  = "plain";
-        $mail->{format}  = "octet-stream";
+-- 
+LedgerSMB|,
+            );
 
-        $myconfig->{signature} =~ s/\\n/\n/g;
-        $mail->{message} = "-- \n$myconfig->{signature}";
+        $mail->attach(
+            'file' => $tmpfile,
+            'filename' => $tmpfile,
+            'strip' => "$boundary.",
+            'mimetype' => 'application/octet-stream',
+            );
 
         $err = $mail->send;
     }

Modified: trunk/LedgerSMB/Mailer.pm
===================================================================
--- trunk/LedgerSMB/Mailer.pm	2007-10-19 01:23:17 UTC (rev 1789)
+++ trunk/LedgerSMB/Mailer.pm	2007-10-19 01:46:57 UTC (rev 1790)
@@ -1,127 +1,173 @@
-#=====================================================================
-# LedgerSMB
-# Small Medium Business Accounting software
-# http://www.ledgersmb.org/
-#
-# Copyright (C) 2006
-# This work contains copyrighted information from a number of sources all used
-# with permission.
-#
-# This file contains source code included with or based on SQL-Ledger which
-# is Copyright Dieter Simader and DWS Systems Inc. 2000-2005 and licensed
-# under the GNU General Public License version 2 or, at your option, any later
-# version.  For a full list including contact information of contributors,
-# maintainers, and copyright holders, see the CONTRIBUTORS file.
-#
-# Original Copyright Notice from SQL-Ledger 2.6.17 (before the fork):
-# Copyright (C) 2002
-#
-#  Author: DWS Systems Inc.
-#     Web: http://www.sql-ledger.org
-#
-#  Contributors:
-#
-# Original Author and copyright holder:
-# Dieter Simader ..hidden..
-#======================================================================
-#
-# This file has undergone whitespace cleanup.
-#
-#======================================================================
-#
-# mailer package
-#
-#======================================================================
+=head1 NAME
 
+LedgerSMB::Mailer   Mail output for LedgerSMB
+
+=head1 SYNOPSIS
+
+=head1 COPYRIGHT
+
+ #====================================================================
+ # LedgerSMB
+ # Small Medium Business Accounting software
+ # http://www.ledgersmb.org/
+ #
+ # Copyright (C) 2006
+ # This work contains copyrighted information from a number of sources
+ # all used with permission.
+ #
+ # This file contains source code included with or based on SQL-Ledger
+ # which # is Copyright Dieter Simader and DWS Systems Inc. 2000-2005
+ # and licensed under the GNU General Public License version 2 or, at
+ # your option, any later version.  For a full list including contact
+ # information of contributors, maintainers, and copyright holders,
+ # see the CONTRIBUTORS file.
+ #
+ # Original Copyright Notice from SQL-Ledger 2.6.17 (before the fork):
+ # Copyright (C) 2002
+ #
+ #  Author: DWS Systems Inc.
+ #	 Web: http://www.sql-ledger.org
+ #
+ #  Contributors:
+ #
+ # Original Author and copyright holder:
+ # Dieter Simader ..hidden..
+ #====================================================================
+
+=head1 METHODS
+
+=cut
+
 package LedgerSMB::Mailer;
 
+use warnings;
+use strict;
+use Carp;
+
 use Encode;
 use MIME::Lite;
-use MIME::Base64;
 use LedgerSMB::Sysconfig;
 
+our $VERSION = '0.13';
+
+=head2 LedgerSMB::Mailer->new([%args])
+
+Create a new Mailer object.  If any arguments are passed in, a message
+that uses them will be automatically prepared.
+
+=cut
+
 sub new {
-    my ($type) = @_;
-    my $self = {};
+	my $type = shift;
+	my $self = {};
+	bless $self, $type;
 
-    bless $self, $type;
+	$self->prepare_message(@_) if @_;
+
+	$self;
 }
 
-sub send {
-    my ($self) = @_;
+=head2 $mail->prepare_message
 
-    my $domain = $self->{from};
-    my $boundary = time;
-    $boundary = "LSMB-$boundary";
-    $domain =~ s/(.*?\@|>)//g;
-    my $msg_id = "..hidden..";
+=cut
 
-    $self->{contenttype} = "text/plain" unless $self->{contenttype};
+sub prepare_message {
+	my $self = shift;
+	my %args = @_;
 
-    for (qw(from to cc bcc)) {
-        $self->{$_} =~ s/\&lt;/</g;
-        $self->{$_} =~ s/\&gt;/>/g;
-        $self->{$_} =~ s/(\/|\\|\$)//g;
-    }
+	# Populate message fields
+	for my $key (keys %args) {
+		$self->{$key} = $args{$key};
+	}
 
-    my $msg = MIME::Lite->new(
-        'From'    => $self->{from},
-        'To'      => $self->{to},
-        'Cc'      => $self->{cc},
-        'Bcc'     => $self->{bcc},
-        'Subject' => Encode::encode('MIME-Header', $self->{subject}),
-        'Type'    => 'TEXT',
-        'Data'    => Encode::encode_utf8($self->{message}),
-        'Encoding'    => '8bit',
-        'Message-ID'    => $msg_id,
-    );
-    $msg->attr( 'Content-Type' => $self->{contenttype} );
-    $msg->attr( 'Content-Type.charset' => 'UTF-8' ) if
-        $self->{contenttype} =~ m#^text/#;
-    $msg->add( 'Disposition-Notification-To' => $self->{from} )
-      if $self->{notify};
-    $msg->replace( 'X-Mailer' => "LedgerSMB $self->{version}" );
-    $msg->binmode(':utf8');
+	my $domain = $self->{from};
+	$domain =~ s/(.*?\@|>)//g;
+	my $boundary = time;
+	$boundary = "LSMB-$boundary";
+	my $msg_id = "..hidden..";
 
-    if ( @{ $self->{attachments} } ) {
-        foreach my $attachment ( @{ $self->{attachments} } ) {
+	$self->{contenttype} = "text/plain" unless $self->{contenttype};
 
-            my $application =
-              ( $attachment =~ /(^\w+$)|\.(html|text|txt|sql)$/ )
-              ? "text"
-              : "application";
-            my $type = "$attachment/$self->{format}";
-            $type .= '; charset="UTF-8"' if $attachment eq 'text';
+	for (qw(from to cc bcc subject)) {
+		next unless $self->{$_};
+		$self->{$_} =~ s/(\/|\\|\$)//g;
+		$self->{$_} =~ s/([\n\r\f])/$1 /g;
+	}
 
-            my $filename = $attachment;
+	$self->{_message} = MIME::Lite->new(
+		'From' => $self->{from},
+		'To' => $self->{to},
+		'Cc'  => $self->{cc},
+		'Bcc'  => $self->{bcc},
+		'Subject' => Encode::encode('MIME-Header', $self->{subject}),
+		'Type' => 'TEXT',
+		'Data' => Encode::encode_utf8($self->{message}),
+		'Encoding' => '8bit',
+		'Message-ID' => $msg_id,
+	);
+	$self->{_message}->attr( 'Content-Type' => $self->{contenttype} );
+	$self->{_message}->attr( 'Content-Type.charset' => 'UTF-8' ) if
+		$self->{contenttype} =~ m#^text/#;
+	# Annoy people with read receipt requests
+	$self->{_message}->add( 'Disposition-Notification-To' => $self->{from} )
+	  if $self->{notify};
+	$self->{_message}->binmode(':utf8');
+}
 
-            # strip path
-            $filename =~ s/(.*\/|$self->{fileid})//g;
-            $msg->attach(
-                'Type'        => $type,
-                'Path'        => $attachment,
-                'Filename'    => $filename,
-                'Disposition' => 'attachment',
-            );
-        }
+=head2 $mail->attach
 
-    }
+=cut
 
-    if ( ${LedgerSMB::Sysconfig::smtphost} ) {
-        $msg->send(
-            'smtp',
-            ${LedgerSMB::Sysconfig::smtphost},
-            Timeout => ${LedgerSMB::Sysconfig::smtptimeout}
-        ) || return $!;
-    }
-    else {
-        $msg->send( 'sendmail', ${LedgerSMB::Sysconfig::sendmail} )
-          || return $!;
-    }
+sub attach {
+	my $self = shift;
+	my %args = @_;
 
-    return "";
+	carp "Message not prepared" unless ref $self->{_message};
 
+	# strip path from output name
+	my $file = $args{filename};
+	my $strip = quotemeta $args{strip};
+	$file =~ s/(.*\/|$strip)//g;
+
+	my @data;
+	if ($args{data}) {
+		@data = ('Data', $args{data});
+	} else {
+		@data = ('Path', $args{filename});
+	}
+
+	$self->{_message}->attach(
+		'Type' => $args{mimetype},
+		'Filename' => $file,
+		'Disposition' => 'attachment',
+		@data,
+		);
 }
 
+=head2 $mail->send
+
+Sends a prepared message using the method configured in ledgersmb.conf.
+
+=cut
+
+sub send {
+	my $self = shift;
+	carp "Message not prepared" unless ref $self->{_message};
+
+	$self->{_message}->replace( 'X-Mailer' => "LedgerSMB::Mailer $VERSION" );
+	if ( ${LedgerSMB::Sysconfig::smtphost} ) {
+		$self->{_message}->send(
+			'smtp',
+			${LedgerSMB::Sysconfig::smtphost},
+			Timeout => ${LedgerSMB::Sysconfig::smtptimeout}
+			) || return $!;
+	} else {
+		$self->{_message}->send(
+			'sendmail',
+			${LedgerSMB::Sysconfig::sendmail}
+			) || return $!;
+	}
+}
+
 1;
 


This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.