/[notice-sender]/trunk/Nos.pm
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Diff of /trunk/Nos.pm

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 75 by dpavlin, Wed Aug 24 21:27:40 2005 UTC revision 85 by dpavlin, Wed Aug 31 16:53:21 2005 UTC
# Line 27  use Email::Simple; Line 27  use Email::Simple;
27  use Email::Address;  use Email::Address;
28  use Mail::DeliveryStatus::BounceParser;  use Mail::DeliveryStatus::BounceParser;
29  use Class::DBI::AbstractSearch;  use Class::DBI::AbstractSearch;
30    use SQL::Abstract;
31  use Mail::Alias;  use Mail::Alias;
32  use Cwd qw(abs_path);  use Cwd qw(abs_path);
33    
# Line 116  sub new { Line 117  sub new {
117    
118          $self->{'hash_len'} ||= 8;          $self->{'hash_len'} ||= 8;
119    
         $self->{'loader'}->find_class('received')->set_sql(  
                 'received' => qq{  
                         select  
                                 received.id as id,  
                                 lists.name as list,  
                                 users.ext_id as ext_id,  
                                 users.email as email,  
                                 bounced,received.date as date  
                         from received  
                         join lists on lists.id = list_id  
                         join users on users.id = user_id  
                 },  
         );  
   
120          $self ? return $self : return undef;          $self ? return $self : return undef;
121  }  }
122    
# Line 722  sub inbox_message { Line 709  sub inbox_message {
709    
710  Returns all received messages for given list or user.  Returns all received messages for given list or user.
711    
712   my @received = $nos->received_message(   my @received = $nos->received_messages(
713          list => 'My list',          list => 'My list',
714          email => "john.doe@example.com",          email => "john.doe@example.com",
715            from_date => '2005-01-01 10:15:00',
716            to_date => '2005-01-01 12:00:00',
717            message => 0,
718   );   );
719    
720  This method is used by C<sender.pl> when receiving e-mail messages.  If don't specify C<list> or C<email> it will return all received messages.
721    Results will be sorted by received date, oldest first.
722    
723    Other optional parametars include:
724    
725    =over 10
726    
727    =item from_date
728    
729    Date (in ISO format) for lower limit of dates received
730    
731    =item to_date
732    
733    Return just messages older than this date
734    
735    =item message
736    
737    Include whole received message in result. This will probably make result
738    array very large. Use with care.
739    
740    =back
741    
742    Date ranges are inclusive, so results will include messages sent on
743    particular date specified with C<date_from> or C<date_to>.
744    
745    Each element in returned array will have following structure:
746    
747     my $row = {
748            id => 42,                       # unique ID of received message
749            list => 'My list',              # useful if filtering by email
750            ext_id => 9999,                 # ext_id from message sender
751            email => 'jdoe@example.com',    # e-mail of message sender
752            bounced => 0,                   # true if message is bounce
753            date => '2005-08-24 18:57:24',  # date of receival in ISO format
754     }
755    
756    If you specified C<message> option, this hash will also have C<message> key
757    which will contain whole received message.
758    
759  =cut  =cut
760    
761  sub received_messages {  sub received_messages {
762          my $self = shift;          my $self = shift;
763    
764          my $arg = {@_};          my $arg = {@_} if (@_);
765    
766          croak "need list name or email" unless ($arg->{'list'} || $arg->{'email'});  #       croak "need list name or email" unless ($arg->{'list'} || $arg->{'email'});
767    
768          $arg->{'list'} = lc($arg->{'list'});          my $sql = qq{
769          $arg->{'email'} = lc($arg->{'email'});                          select
770                                    received.id as id,
771                                    lists.name as list,
772                                    users.ext_id as ext_id,
773                                    users.email as email,
774            };
775            $sql .= qq{             message,} if ($arg->{'message'});
776            $sql .= qq{
777                                    bounced,received.date as date
778                            from received
779                            join lists on lists.id = list_id
780                            join users on users.id = user_id
781            };
782    
783          my $rcvd = $self->{'loader'}->find_class('received')->search_received();          my $order = qq{ order by date asc };
784    
785          return $rcvd;          my $where;
786    
787            $where->{'lists.name'} = lc($arg->{'list'}) if ($arg->{'list'});
788            $where->{'users.email'} = lc($arg->{'email'}) if ($arg->{'email'});
789            $where->{'received.date'} = { '>=', $arg->{'date_from'} } if ($arg->{'date_from'});
790            $where->{'received.date'} = { '<=', $arg->{'date_to'} } if ($arg->{'date_to'});
791    
792            # hum, yammy one-liner
793            my($stmt, @bind)  = SQL::Abstract->new->where($where);
794    
795            my $dbh = $self->{'loader'}->find_class('received')->db_Main;
796    
797            my $sth = $dbh->prepare($sql . $stmt . $order);
798            $sth->execute(@bind);
799            return $sth->fetchall_hash;
800  }  }
801    
802    
# Line 813  sub _add_aliases { Line 866  sub _add_aliases {
866          $self_path =~ s#/[^/]+$##;          $self_path =~ s#/[^/]+$##;
867          $self_path =~ s#/t/*$#/#;          $self_path =~ s#/t/*$#/#;
868    
869          $target .= qq#| cd $self_path && ./sender.pl --inbox="$list"#;          $target .= qq#"| cd $self_path && ./sender.pl --inbox='$list'"#;
870    
871            # remove hostname from email to make Postfix's postalias happy
872            $email =~ s/@.+//;
873    
874          if ($a->exists($email)) {          if ($a->exists($email)) {
875                  $a->update($email, $target) or croak "can't update alias ".$a->error_check;                  $a->update($email, $target) or croak "can't update alias ".$a->error_check;
# Line 1063  sub AddMemberToList { Line 1119  sub AddMemberToList {
1119    
1120          if ($_[0] !~ m/^HASH/) {          if ($_[0] !~ m/^HASH/) {
1121                  return $nos->add_member_to_list(                  return $nos->add_member_to_list(
1122                          list => $_[0], email => $_[1], name => $_[2], ext_id => $_[4],                          list => $_[0], email => $_[1], name => $_[2], ext_id => $_[3],
1123                  );                  );
1124          } else {          } else {
1125                  return $nos->add_member_to_list( %{ shift @_ } );                  return $nos->add_member_to_list( %{ shift @_ } );
# Line 1139  sub AddMessageToList { Line 1195  sub AddMessageToList {
1195          }          }
1196  }  }
1197    
 =head1 UNIMPLEMENTED FUNCTIONS  
   
 This is a stub for documentation of unimplemented functions.  
   
1198  =head2 MessagesReceived  =head2 MessagesReceived
1199    
1200    Return statistics about received messages.
1201    
1202   my @result = MessagesReceived(   my @result = MessagesReceived(
1203          list => 'My list',          list => 'My list',
1204          email => 'jdoe@example.com',          email => 'jdoe@example.com',
1205            from_date => '2005-01-01 10:15:00',
1206            to_date => '2005-01-01 12:00:00',
1207            message => 0,
1208   );   );
1209    
1210  You can specify just C<list> or C<email> or any combination of those.  You must specify C<list> or C<email> or any combination of those two. Other
1211    parametars are optional.
1212    
1213  It will return array of hashes with following structure:  For format of returned array element see C<received_messages>.
   
  {  
         id => 42,                       # unique ID of received message  
         list => 'My list',              # useful only of filtering by email  
         ext_id => 9999,                 # ext_id from message user  
         email => 'jdoe@example.com',    # e-mail of user  
         bounced => 0,                   # true value if message is bounce  
         date => '2005-08-24 18:57:24',  # date of recival in ISO format  
  }  
   
 =head2 MessagesReceivedByDate  
   
 =head2 MessagesReceivedByDateWithContent  
   
 =head2 ReceivedMessasgeContent  
   
 Return content of received message.  
   
  my $mail_body = ReceivedMessageContent( id => 42 );  
1214    
1215  =cut  =cut
1216    
1217    sub MessagesReceived {
1218            my $self = shift;
1219    
1220            if ($_[0] !~ m/^HASH/) {
1221                    die "need at least list or email" unless (scalar @_ < 2);
1222                    return \@{ $nos->received_messages(
1223                            list => $_[0], email => $_[1],
1224                            from_date => $_[2], to_date => $_[3],
1225                            message => $_[4]
1226                    ) };
1227            } else {
1228                    my $arg = shift;
1229                    die "need list or email argument" unless ($arg->{'list'} || $arg->{'email'});
1230                    return \@{ $nos->received_messages( %{ $arg } ) };
1231            }
1232    }
1233    
1234  ###  ###
1235    

Legend:
Removed from v.75  
changed lines
  Added in v.85

  ViewVC Help
Powered by ViewVC 1.1.26