/[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 60 by dpavlin, Tue Jun 21 21:24:10 2005 UTC revision 76 by dpavlin, Wed Aug 24 22:11:00 2005 UTC
# Line 16  our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all' Line 16  our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'
16  our @EXPORT = qw(  our @EXPORT = qw(
17  );  );
18    
19  our $VERSION = '0.5';  our $VERSION = '0.8';
20    
21  use Class::DBI::Loader;  use Class::DBI::Loader;
22  use Email::Valid;  use Email::Valid;
# 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 Mail::Alias;
31    use Cwd qw(abs_path);
32    
33    
34  =head1 NAME  =head1 NAME
# Line 60  encoded) or anything else. Line 62  encoded) or anything else.
62  It will just queue your e-mail message to particular list (sending it to  It will just queue your e-mail message to particular list (sending it to
63  possibly remote Notice Sender SOAP server just once), send it out at  possibly remote Notice Sender SOAP server just once), send it out at
64  reasonable rate (so that it doesn't flood your e-mail infrastructure) and  reasonable rate (so that it doesn't flood your e-mail infrastructure) and
65  track replies.  keep track replies.
66    
67  It is best used to send smaller number of messages to more-or-less fixed  It is best used to send small number of messages to more-or-less fixed
68  list of recipients while allowing individual responses to be examined.  list of recipients while allowing individual responses to be examined.
69  Tipical use include replacing php e-mail sending code with SOAP call to  Tipical use include replacing php e-mail sending code with SOAP call to
70  Notice Sender. It does support additional C<ext_id> field for each member  Notice Sender. It does support additional C<ext_id> field for each member
# Line 70  which can be used to track some unique i Line 72  which can be used to track some unique i
72  particular user.  particular user.
73    
74  It comes with command-line utility C<sender.pl> which can be used to perform  It comes with command-line utility C<sender.pl> which can be used to perform
75  all available operation from scripts (see C<perldoc sender.pl>).  all available operation from scripts (see C<sender.pl --man>).
76  This command is also useful for debugging while writing client SOAP  This command is also useful for debugging while writing client SOAP
77  application.  application.
78    
# Line 114  sub new { Line 116  sub new {
116    
117          $self->{'hash_len'} ||= 8;          $self->{'hash_len'} ||= 8;
118    
119            $self->{'loader'}->find_class('received')->set_sql(
120                    'received' => qq{
121                            select
122                                    received.id as id,
123                                    lists.name as list,
124                                    users.ext_id as ext_id,
125                                    users.email as email,
126                                    bounced,received.date as date
127                            from received
128                            join lists on lists.id = list_id
129                            join users on users.id = user_id
130                    },
131            );
132    
133          $self ? return $self : return undef;          $self ? return $self : return undef;
134  }  }
135    
136    
137  =head2 new_list  =head2 create_list
138    
139  Create new list. Required arguments are name of C<list> and  Create new list. Required arguments are name of C<list>, C<email> address
140  C<email> address.  and path to C<aliases> file.
141    
142   $nos->new_list(   $nos->create_list(
143          list => 'My list',          list => 'My list',
144          from => 'Outgoing from comment',          from => 'Outgoing from comment',
145          email => 'my-list@example.com',          email => 'my-list@example.com',
146            aliases => '/etc/mail/mylist',
147            archive => '/path/to/mbox/archive',
148   );   );
149    
150  Returns ID of newly created list.  Returns ID of newly created list.
# Line 135  Calls internally C<_add_list>, see detai Line 153  Calls internally C<_add_list>, see detai
153    
154  =cut  =cut
155    
156  sub new_list {  sub create_list {
157          my $self = shift;          my $self = shift;
158    
159          my $arg = {@_};          my $arg = {@_};
# Line 154  sub new_list { Line 172  sub new_list {
172  }  }
173    
174    
175    =head2 drop_list
176    
177    Delete list from database.
178    
179     my $ok = drop_list(
180            list => 'My list'
181            aliases => '/etc/mail/mylist',
182     );
183    
184    Returns false if list doesn't exist.
185    
186    =cut
187    
188    sub drop_list {
189            my $self = shift;
190    
191            my $args = {@_};
192    
193            croak "need list to delete" unless ($args->{'list'});
194    
195            $args->{'list'} = lc($args->{'list'});
196    
197            my $aliases = $args->{'aliases'} || croak "need path to aliases file";
198    
199            my $lists = $self->{'loader'}->find_class('lists');
200    
201            my $this_list = $lists->search( name => $args->{'list'} )->first || return;
202    
203            $self->_remove_alias( email => $this_list->email, aliases => $aliases);
204    
205            $this_list->delete || croak "can't delete list\n";
206    
207            return $lists->dbi_commit || croak "can't commit";
208    }
209    
210    
211  =head2 add_member_to_list  =head2 add_member_to_list
212    
213  Add new member to list  Add new member to list
# Line 227  List all members of some list. Line 281  List all members of some list.
281          list => 'My list',          list => 'My list',
282   );   );
283    
284  Returns array of hashes with user informations like this:  Returns array of hashes with user information like this:
285    
286   $member = {   $member = {
287          name => 'Dobrica Pavlinusic',          name => 'Dobrica Pavlinusic',
# Line 343  sub delete_member_from_list { Line 397  sub delete_member_from_list {
397          my $this_user = $user->search( email => $args->{'email'} )->first || croak "can't find user: ".$args->{'email'};          my $this_user = $user->search( email => $args->{'email'} )->first || croak "can't find user: ".$args->{'email'};
398          my $this_list = $list->search( name => $args->{'list'} )->first || croak "can't find list: ".$args->{'list'};          my $this_list = $list->search( name => $args->{'list'} )->first || croak "can't find list: ".$args->{'list'};
399    
400          my $this_user_list = $user_list->search_where( list_id => $this_list->id, user_id => $this_list->id )->first || return;          my $this_user_list = $user_list->search_where( list_id => $this_list->id, user_id => $this_user->id )->first || return;
401    
402          $this_user_list->delete || croak "can't delete user from list\n";          $this_user_list->delete || croak "can't delete user from list\n";
403    
# Line 435  Send e-mail using SMTP server at 127.0.0 Line 489  Send e-mail using SMTP server at 127.0.0
489    
490  =back  =back
491    
492    Any other driver name will try to use C<Email::Send::that_driver> module.
493    
494  Default sleep wait between two messages is 3 seconds.  Default sleep wait between two messages is 3 seconds.
495    
496    This method will return number of succesfully sent messages.
497    
498  =cut  =cut
499    
500  sub send_queued_messages {  sub send_queued_messages {
# Line 449  sub send_queued_messages { Line 507  sub send_queued_messages {
507          my $sleep = $arg->{'sleep'};          my $sleep = $arg->{'sleep'};
508          $sleep ||= 3 unless defined($sleep);          $sleep ||= 3 unless defined($sleep);
509    
510            # number of messages sent o.k.
511            my $ok = 0;
512    
513          my $email_send_driver = 'Email::Send::IO';          my $email_send_driver = 'Email::Send::IO';
514          my @email_send_options;          my @email_send_options;
515    
516          if (lc($driver) eq 'smtp') {          if (lc($driver) eq 'smtp') {
517                  $email_send_driver = 'Email::Send::SMTP';                  $email_send_driver = 'Email::Send::SMTP';
518                  @email_send_options = ['127.0.0.1'];                  @email_send_options = ['127.0.0.1'];
519            } elsif ($driver && $driver ne '') {
520                    $email_send_driver = 'Email::Send::' . $driver;
521          } else {          } else {
522                  warn "dumping all messages to STDERR\n";                  warn "dumping all messages to STDERR\n";
523          }          }
# Line 489  sub send_queued_messages { Line 552  sub send_queued_messages {
552                          if ($sent->search( message_id => $m->message_id, user_id => $u->user_id )) {                          if ($sent->search( message_id => $m->message_id, user_id => $u->user_id )) {
553                                  print "SKIP $to_email message allready sent\n";                                  print "SKIP $to_email message allready sent\n";
554                          } else {                          } else {
555                                  print "=> $to_email\n";                                  print "=> $to_email ";
556    
557                                  my $secret = $m->list_id->name . " " . $u->user_id->email . " " . $m->message_id;                                  my $secret = $m->list_id->name . " " . $u->user_id->email . " " . $m->message_id;
558                                  my $auth = Email::Auth::AddressHash->new( $secret, $self->{'hash_len'} );                                  my $auth = Email::Auth::AddressHash->new( $secret, $self->{'hash_len'} );
# Line 515  sub send_queued_messages { Line 578  sub send_queued_messages {
578                                  $m_obj->header_set('X-Nos-Hash', $hash);                                  $m_obj->header_set('X-Nos-Hash', $hash);
579    
580                                  # really send e-mail                                  # really send e-mail
581                                    my $sent_status;
582    
583                                  if (@email_send_options) {                                  if (@email_send_options) {
584                                          send $email_send_driver => $m_obj->as_string, @email_send_options;                                          $sent_status = send $email_send_driver => $m_obj->as_string, @email_send_options;
585                                  } else {                                  } else {
586                                          send $email_send_driver => $m_obj->as_string;                                          $sent_status = send $email_send_driver => $m_obj->as_string;
587                                  }                                  }
588    
589                                  $sent->create({                                  croak "can't send e-mail: $sent_status\n\nOriginal e-mail follows:\n".$m_obj->as_string unless ($sent_status);
590                                          message_id => $m->message_id,                                  my @bad;
591                                          user_id => $u->user_id,                                  @bad = @{ $sent_status->prop('bad') } if (eval { $sent_status->can('prop') });
592                                          hash => $hash,                                  croak "failed sending to ",join(",",@bad) if (@bad);
593                                  });  
594                                  $sent->dbi_commit;                                  if ($sent_status) {
595    
596                                            $sent->create({
597                                                    message_id => $m->message_id,
598                                                    user_id => $u->user_id,
599                                                    hash => $hash,
600                                            });
601                                            $sent->dbi_commit;
602    
603                                            print " - $sent_status\n";
604    
605                                            $ok++;
606                                    } else {
607                                            warn "ERROR: $sent_status\n";
608                                    }
609    
610                                  if ($sleep) {                                  if ($sleep) {
611                                          warn "sleeping $sleep seconds\n";                                          warn "sleeping $sleep seconds\n";
# Line 539  sub send_queued_messages { Line 618  sub send_queued_messages {
618                  $m->dbi_commit;                  $m->dbi_commit;
619          }          }
620    
621            return $ok;
622    
623  }  }
624    
625  =head2 inbox_message  =head2 inbox_message
# Line 637  sub inbox_message { Line 718  sub inbox_message {
718  #       print "message_id: ",($message_id || "not found")," -- $is_bounce\n";  #       print "message_id: ",($message_id || "not found")," -- $is_bounce\n";
719  }  }
720    
721    =head2 received_messages
722    
723    Returns all received messages for given list or user.
724    
725     my @received = $nos->received_message(
726            list => 'My list',
727            email => "john.doe@example.com",
728     );
729    
730    Each element in returned array will have following structure:
731    
732     {
733            id => 42,                       # unique ID of received message
734            list => 'My list',              # useful only of filtering by email
735            ext_id => 9999,                 # ext_id from message user
736            email => 'jdoe@example.com',    # e-mail of user
737            bounced => 0,                   # true value if message is bounce
738            date => '2005-08-24 18:57:24',  # date of recival in ISO format
739     }
740    
741    
742    =cut
743    
744    sub received_messages {
745            my $self = shift;
746    
747            my $arg = {@_};
748    
749            croak "need list name or email" unless ($arg->{'list'} || $arg->{'email'});
750    
751            $arg->{'list'} = lc($arg->{'list'});
752            $arg->{'email'} = lc($arg->{'email'});
753    
754            my @out;
755    
756            my $sth = $self->{'loader'}->find_class('received')->sql_received;
757            $sth->execute();
758            return $sth->fetchall_hash;
759    }
760    
761    
762  =head1 INTERNAL METHODS  =head1 INTERNAL METHODS
763    
764  Beware of dragons! You shouldn't need to call those methods directly.  Beware of dragons! You shouldn't need to call those methods directly.
765    
766    
767    =head2 _add_aliases
768    
769    Add or update alias in C</etc/aliases> (or equivalent) file for selected list
770    
771     my $ok = $nos->add_aliases(
772            list => 'My list',
773            email => 'my-list@example.com',
774            aliases => '/etc/mail/mylist',
775            archive => '/path/to/mbox/archive',
776    
777     );
778    
779    C<archive> parametar is optional.
780    
781    Return false on failure.
782    
783    =cut
784    
785    sub _add_aliases {
786            my $self = shift;
787    
788            my $arg = {@_};
789    
790            foreach my $o (qw/list email aliases/) {
791                    croak "need $o option" unless ($arg->{$o});
792            }
793    
794            my $aliases = $arg->{'aliases'};
795            my $email = $arg->{'email'};
796            my $list = $arg->{'list'};
797    
798            unless (-e $aliases) {
799                    warn "aliases file $aliases doesn't exist, creating empty\n";
800                    open(my $fh, '>', $aliases) || croak "can't create $aliases: $!";
801                    close($fh);
802                    chmod 0777, $aliases || warn "can't change permission to 0777";
803            }
804    
805            die "FATAL: aliases file $aliases is not writable\n" unless (-w $aliases);
806    
807            my $a = new Mail::Alias($aliases) || croak "can't open aliases file $aliases: $!";
808    
809            my $target = '';
810    
811            if (my $archive = $arg->{'archive'}) {
812                    $target .= "$archive, ";
813    
814                    if (! -e $archive) {
815                            warn "please make sure that file $archive is writable for your e-mail user (defaulting to bad 777 permission for now)";
816    
817                            open(my $fh, '>', $archive) || croak "can't create archive file $archive: $!";
818                            close($fh);
819                            chmod 0777, $archive || croak "can't chmod archive file $archive to 0777: $!";
820                    }
821            }
822    
823            # resolve my path to absolute one
824            my $self_path = abs_path($0);
825            $self_path =~ s#/[^/]+$##;
826            $self_path =~ s#/t/*$#/#;
827    
828            $target .= qq#| cd $self_path && ./sender.pl --inbox="$list"#;
829    
830            if ($a->exists($email)) {
831                    $a->update($email, $target) or croak "can't update alias ".$a->error_check;
832            } else {
833                    $a->append($email, $target) or croak "can't add alias ".$a->error_check;
834            }
835    
836            #$a->write($aliases) or croak "can't save aliases $aliases ".$a->error_check;
837    
838            return 1;
839    }
840    
841  =head2 _add_list  =head2 _add_list
842    
843  Create new list  Create new list
# Line 650  Create new list Line 846  Create new list
846          list => 'My list',          list => 'My list',
847          from => 'Outgoing from comment',          from => 'Outgoing from comment',
848          email => 'my-list@example.com',          email => 'my-list@example.com',
849            aliases => '/etc/mail/mylist',
850   );   );
851    
852  Returns C<Class::DBI> object for created list.  Returns C<Class::DBI> object for created list.
# Line 668  sub _add_list { Line 865  sub _add_list {
865    
866          my $name = lc($arg->{'list'}) || confess "can't add list without name";          my $name = lc($arg->{'list'}) || confess "can't add list without name";
867          my $email = lc($arg->{'email'}) || confess "can't add list without e-mail";          my $email = lc($arg->{'email'}) || confess "can't add list without e-mail";
868            my $aliases = lc($arg->{'aliases'}) || confess "can't add list without path to aliases file";
869    
870          my $from_addr = $arg->{'from'};          my $from_addr = $arg->{'from'};
871    
872          my $lists = $self->{'loader'}->find_class('lists');          my $lists = $self->{'loader'}->find_class('lists');
873    
874            $self->_add_aliases(
875                    list => $name,
876                    email => $email,
877                    aliases => $aliases,
878            ) || warn "can't add alias $email for list $name";
879    
880          my $l = $lists->find_or_create({          my $l = $lists->find_or_create({
881                  name => $name,                  name => $name,
882                  email => $email,                  email => $email,
# Line 691  sub _add_list { Line 896  sub _add_list {
896  }  }
897    
898    
899    
900  =head2 _get_list  =head2 _get_list
901    
902  Get list C<Class::DBI> object.  Get list C<Class::DBI> object.
# Line 711  sub _get_list { Line 917  sub _get_list {
917          return $lists->search({ name => lc($name) })->first;          return $lists->search({ name => lc($name) })->first;
918  }  }
919    
920    
921    =head2 _remove_alias
922    
923    Remove list alias
924    
925     my $ok = $nos->_remove_alias(
926            email => 'mylist@example.com',
927            aliases => '/etc/mail/mylist',
928     );
929    
930    Returns true if list is removed or false if list doesn't exist. Dies in case of error.
931    
932    =cut
933    
934    sub _remove_alias {
935            my $self = shift;
936    
937            my $arg = {@_};
938    
939            my $email = lc($arg->{'email'}) || confess "can't remove alias without email";
940            my $aliases = lc($arg->{'aliases'}) || confess "can't remove alias without list";
941    
942            my $a = new Mail::Alias($aliases) || croak "can't open aliases file $aliases: $!";
943    
944            if ($a->exists($email)) {
945                    $a->delete($email) || croak "can't remove alias $email";
946            } else {
947                    return 0;
948            }
949    
950            return 1;
951    
952    }
953    
954  ###  ###
955  ### SOAP  ### SOAP
956  ###  ###
# Line 735  methods below). Line 975  methods below).
975    
976  my $nos;  my $nos;
977    
978    
979    =head2 new
980    
981    Create new SOAP object
982    
983     my $soap = new Nos::SOAP(
984            dsn => 'dbi:Pg:dbname=notices',
985            user => 'dpavlin',
986            passwd => '',
987            debug => 1,
988            verbose => 1,
989            hash_len => 8,
990            aliases => '/etc/aliases',
991     );
992    
993    If you are writing SOAP server (like C<soap.cgi> example), you will need to
994    call this method once to make new instance of Nos::SOAP and specify C<dsn>
995    and options for it.
996    
997    =cut
998    
999  sub new {  sub new {
1000          my $class = shift;          my $class = shift;
1001          my $self = {@_};          my $self = {@_};
1002    
1003            croak "need aliases parametar" unless ($self->{'aliases'});
1004    
1005          bless($self, $class);          bless($self, $class);
1006    
1007          $nos = new Nos( @_ ) || die "can't create Nos object";          $nos = new Nos( @_ ) || die "can't create Nos object";
# Line 746  sub new { Line 1010  sub new {
1010  }  }
1011    
1012    
1013  =head2 NewList  =head2 CreateList
1014    
1015   $message_id = NewList(   $message_id = CreateList(
1016          list => 'My list',          list => 'My list',
1017          from => 'Name of my list',          from => 'Name of my list',
1018          email => 'my-list@example.com'          email => 'my-list@example.com'
# Line 756  sub new { Line 1020  sub new {
1020    
1021  =cut  =cut
1022    
1023  sub NewList {  sub CreateList {
1024          my $self = shift;          my $self = shift;
1025    
1026            my $aliases = $nos->{'aliases'} || croak "need 'aliases' argument to new constructor";
1027    
1028          if ($_[0] !~ m/^HASH/) {          if ($_[0] !~ m/^HASH/) {
1029                  return $nos->new_list(                  return $nos->create_list(
1030                          list => $_[0], from => $_[1], email => $_[2],                          list => $_[0], from => $_[1], email => $_[2],
1031                            aliases => $aliases,
1032                  );                  );
1033          } else {          } else {
1034                  return $nos->new_list( %{ shift @_ } );                  return $nos->create_list( %{ shift @_ }, aliases => $aliases );
1035          }          }
1036  }  }
1037    
1038    
1039    =head2 DropList
1040    
1041     $ok = DropList(
1042            list => 'My list',
1043     );
1044    
1045    =cut
1046    
1047    sub DropList {
1048            my $self = shift;
1049    
1050            my $aliases = $nos->{'aliases'} || croak "need 'aliases' argument to new constructor";
1051    
1052            if ($_[0] !~ m/^HASH/) {
1053                    return $nos->drop_list(
1054                            list => $_[0],
1055                            aliases => $aliases,
1056                    );
1057            } else {
1058                    return $nos->drop_list( %{ shift @_ }, aliases => $aliases );
1059            }
1060    }
1061    
1062  =head2 AddMemberToList  =head2 AddMemberToList
1063    
1064   $member_id = AddMemberToList(   $member_id = AddMemberToList(
# Line 814  sub ListMembers { Line 1104  sub ListMembers {
1104                  $list_name = $_[0]->{'list'};                  $list_name = $_[0]->{'list'};
1105          }          }
1106    
1107          return $nos->list_members( list => $list_name );          return [ $nos->list_members( list => $list_name ) ];
1108    }
1109    
1110    
1111    =head2 DeleteMemberFromList
1112    
1113     $member_id = DeleteMemberFromList(
1114            list => 'My list',
1115            email => 'e-mail@example.com',
1116     );
1117    
1118    =cut
1119    
1120    sub DeleteMemberFromList {
1121            my $self = shift;
1122    
1123            if ($_[0] !~ m/^HASH/) {
1124                    return $nos->delete_member_from_list(
1125                            list => $_[0], email => $_[1],
1126                    );
1127            } else {
1128                    return $nos->delete_member_from_list( %{ shift @_ } );
1129            }
1130  }  }
1131    
1132    
1133  =head2 AddMessageToList  =head2 AddMessageToList
1134    
1135   $message_id = AddMessageToList(   $message_id = AddMessageToList(
# Line 838  sub AddMessageToList { Line 1151  sub AddMessageToList {
1151          }          }
1152  }  }
1153    
1154    =head1 UNIMPLEMENTED FUNCTIONS
1155    
1156    This is a stub for documentation of unimplemented functions.
1157    
1158    =head2 MessagesReceived
1159    
1160     my @result = MessagesReceived(
1161            list => 'My list',
1162            email => 'jdoe@example.com',
1163     );
1164    
1165    You can specify just C<list> or C<email> or any combination of those.
1166    
1167    For format of returned array element see C<received_messages>.
1168    
1169    =head2 MessagesReceivedByDate
1170    
1171    =head2 MessagesReceivedByDateWithContent
1172    
1173    =head2 ReceivedMessasgeContent
1174    
1175    Return content of received message.
1176    
1177     my $mail_body = ReceivedMessageContent( id => 42 );
1178    
1179    =cut
1180    
1181    
1182    
1183    
1184  ###  ###
1185    
1186    =head1 NOTE ON ARRAYS IN SOAP
1187    
1188    Returning arrays from SOAP calls is somewhat fuzzy (at least to me). It
1189    seems that SOAP::Lite client thinks that it has array with one element which
1190    is array of hashes with data.
1191    
1192  =head1 EXPORT  =head1 EXPORT
1193    
1194  Nothing.  Nothing.

Legend:
Removed from v.60  
changed lines
  Added in v.76

  ViewVC Help
Powered by ViewVC 1.1.26