package Egg::View::Mail::Plugin::SaveBody;
# Masatoshi Mizuno E<lt>lusheE<64>cpan.orgE<gt>
# $Id: 285 2008-02-28 04:20:55Z lushe $
use strict;
use warnings;

our $VERSION= '0.01';

sub _setup {
	my($class, $e)= @_;
	   || die __PACKAGE__. q{ - I want setup 'Egg::Model::FsaveDate'.};
	no strict 'refs';  ## no critic.
	no warnings 'redefine';
	if ($class->isa('Egg::View::Mail::Plugin::Lot')) {
		require Digest::SHA1;
		$class->mk_accessors(qw/ lot_name savebodys /);
		*{"${class}::__send"}     = \&___send_lot;
		*{"${class}::__save_body"}= \&___save_body_lot;
	} else {
		*{"${class}::__send"}     = sub {};
		*{"${class}::__save_body"}= \&___save_body_proc;
sub send {
	my $self= shift;
	my $data= $_[0] ? ($_[1] ? {@_}: $_[0]): return $self->next::method(@_);
sub mail_send {
	my($self, $data)= @_;
sub ___send_lot {
	my($self, $data)= @_;
	my $to= $data->{to} || $self->config->{to} || return 0;
	$self->lot_name( Digest::SHA1::sha1_hex($to. '-save') );
	$self->savebodys({}) unless $self->savebodys;
sub ___save_body_lot {
	my($self, $data)= @_;
	return 0 if $self->savebodys->{$self->lot_name};
	$self->savebodys->{$self->lot_name}= 1;
sub ___save_body_proc {
	my($self, $data)= @_;
	my $output_path= $self->e->model('fsavedate')->save
	      ( ${$data->{body}}, ($data->{save_body_path} || undef) );



=head1 NAME

Egg::View::Mail::Plugin::SaveBody? - The content of the transmission of mail is preserved in the file. 


  package MyApp::View::Mail::MyComp;
  use base qw/ Egg::View::Mail::Base /;


It is MAIL plugin to preserve the content of the transmission of mail in the file.

When 'SaveBody' is passed to 'setup_plugin' method, it is built in.

It is necessary to set up it and L<Egg::Model::FsaveDate>.

  % vi /path/to/MyApp/lib/MyApp/
   MODEL => ['FsaveDate'],

Some behavior changes if L<Egg::View::Mail::Plugin::Lot> is built in.

A large amount of files of the same content are made when preserving it with 
L<Egg::View::Mail::Plugin::Lot> at the transmission though the content of mail
is always usually preserved.
Then, if the destination looks similar, the preservation of the content of mail
is finished once.
The problem of no preservation of the content of the following transmission etc.
happens when another content is sent to the same destination in the same process
because this is not in the content of mail and is checked by it in the destination.

When 'save_body_path' is set by the argument and the configuration of 'send' 
method, it comes to be preserved in a place different from the place that 
L<Egg::Model::FsaveDate> originally preserves.

    body => .......,
    save_body_path => '/path/to/output',

=head1 METHODS

=head2 send, mail_send

It competes simultaneously with other components that use these methods when 
using it. Please adjust the order of building in.


=head2 is_savebody

PATH to the preserved file is stored.

After 'send' method is called, it comes to be able to take this out.

  $mail->send( to=> '.....', body => '......' );
  print $mail->is_savebody . '¤ËÊݸ¤µ¤ì¤Þ¤·¤¿¡£';

=head2 lot_name

It is a method of the setup when using it at the same time as 

ID of SHA1 generated with the value of 'to' is stored.

=head2 savebodys

It is a method of the setup when using it at the same time as 

Already it has transmitted or the data for the judgment has already been stored.

=head1 SEE ALSO


=head1 AUTHOR

Masatoshi Mizuno E<lt>lusheE<64>cpan.orgE<gt>


Copyright (C) 2008 Bee Flag, Corp. E<lt>L<>E<gt>, All Rights Reserved.

This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself, either Perl version 5.8.6 or,
at your option, any later version of Perl 5 you may have available.