--- trunk/Nos.pm 2006/09/21 10:49:00 87 +++ jifty-dbi/lib/Nos.pm 2006/12/19 10:32:18 92 @@ -16,9 +16,8 @@ our @EXPORT = qw( ); -our $VERSION = '0.8'; +our $VERSION = '0.9_00'; -use Class::DBI::Loader; use Email::Valid; use Email::Send; use Carp; @@ -26,11 +25,13 @@ use Email::Simple; use Email::Address; use Mail::DeliveryStatus::BounceParser; -use Class::DBI::AbstractSearch; -use SQL::Abstract; use Mail::Alias; use Cwd qw(abs_path); +use Jifty::DBI::Handle; +use lib 'lib'; +use Nos::Lists; + =head1 NAME @@ -90,23 +91,40 @@ debug => 1, verbose => 1, hash_len => 8, + full_hostname_in_aliases => 0, ); Parametar C defines length of hash which will be added to each outgoing e-mail message to ensure that replies can be linked with sent e-mails. +C will turn on old behaviour (not supported by Postfix +postalias) to include full hostname in aliases file. + + =cut sub new { - my $class = shift; - my $self = {@_}; + my $class = shift; + my $self = {@_}; bless($self, $class); - croak "need at least dsn" unless ($self->{'dsn'}); + croak "need at least dsn" unless ($self->{dsn}); + + my (undef,$driver,$dbname) = split(/:/, $self->{dsn}); + $dbname =~ s!^dbname=!!; + + $self->{h} = Jifty::DBI::Handle->new(); + $self->{h}->connect( + driver => $driver, + database => $dbname, + host => 'localhost', + user => $self->{user}, + password => $self->{passwd}, + ); $self->{'loader'} = Class::DBI::Loader->new( debug => $self->{'debug'}, - dsn => $self->{'dsn'}, + dsn => $self->{'dsn'}, user => $self->{'user'}, password => $self->{'passwd'}, namespace => "Nos", @@ -866,7 +884,7 @@ $target .= qq#"| cd $self_path && ./sender.pl --inbox='$list'"#; # remove hostname from email to make Postfix's postalias happy - $email =~ s/@.+//; + $email =~ s/@.+// if (not $self->{full_hostname_in_aliases}); if ($a->exists($email)) { $a->update($email, $target) or croak "can't update alias ".$a->error_check; @@ -874,7 +892,7 @@ $a->append($email, $target) or croak "can't add alias ".$a->error_check; } - #$a->write($aliases) or croak "can't save aliases $aliases ".$a->error_check; +# $a->write($aliases) or croak "can't save aliases $aliases ".$a->error_check; return 1; } @@ -899,6 +917,22 @@ =cut +sub find_or_create { + my $self = shift; + my $obj = shift; + my %args = {@_}; + + my ( $id, $msg ) = $obj->load_by_cols(%args); + unless ( $obj->{values}->{id} ) { + warn "find_or_CREATE(",dump( \%args ), ")"; + return $obj->create(%args); + } + + warn "FIND_or_create(",dump( \%args ), ") = $id"; + return $id; +} + + sub _add_list { my $self = shift; @@ -910,7 +944,7 @@ my $from_addr = $arg->{'from'}; - my $lists = $self->{'loader'}->find_class('lists'); + my $lists = Nos::Lists->new( handle => $self->{h} ); $self->_add_aliases( list => $name, @@ -918,7 +952,7 @@ aliases => $aliases, ) || warn "can't add alias $email for list $name"; - my $l = $lists->find_or_create({ + my $l = $self->find_or_create($lists, { name => $name, email => $email, }); @@ -1038,8 +1072,8 @@ =cut sub new { - my $class = shift; - my $self = {@_}; + my $class = shift; + my $self = {@_}; croak "need aliases parametar" unless ($self->{'aliases'}); @@ -1236,6 +1270,20 @@ seems that SOAP::Lite client thinks that it has array with one element which is array of hashes with data. +=head1 PRIVATE METHODS + +Documented here because tests use them + +=head2 _nos_object + + my $nos = $nos->_nos_object; + +=cut + +sub _nos_object { + return $nos; +} + =head1 EXPORT Nothing.