/[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

Annotation of /trunk/Nos.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 32 - (hide annotations)
Mon May 16 22:32:58 2005 UTC (18 years, 9 months ago) by dpavlin
File size: 7691 byte(s)
uniqe hashes are now really unique for each list-user-message pair

1 dpavlin 20 package Nos;
2    
3     use 5.008;
4     use strict;
5     use warnings;
6    
7     require Exporter;
8    
9     our @ISA = qw(Exporter);
10    
11     our %EXPORT_TAGS = ( 'all' => [ qw(
12     ) ] );
13    
14     our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
15    
16     our @EXPORT = qw(
17     );
18    
19 dpavlin 29 our $VERSION = '0.3';
20 dpavlin 20
21     use Class::DBI::Loader;
22     use Email::Valid;
23     use Email::Send;
24     use Carp;
25 dpavlin 29 use Email::Auth::AddressHash;
26     use Email::Simple;
27     use Data::Dumper;
28 dpavlin 20
29     =head1 NAME
30    
31     Nos - Notice Sender core module
32    
33     =head1 SYNOPSIS
34    
35     use Nos;
36     my $nos = new Nos();
37    
38     =head1 DESCRIPTION
39    
40     Core module for notice sender's functionality.
41    
42     =head1 METHODS
43    
44     =head2 new
45    
46     Create new instance specifing database, user, password and options.
47    
48     my $nos = new Nos(
49     dsn => 'dbi:Pg:dbname=notices',
50     user => 'dpavlin',
51     passwd => '',
52     debug => 1,
53     verbose => 1,
54     );
55    
56     =cut
57    
58     sub new {
59     my $class = shift;
60     my $self = {@_};
61     bless($self, $class);
62    
63 dpavlin 22 croak "need at least dsn" unless ($self->{'dsn'});
64    
65 dpavlin 20 $self->{'loader'} = Class::DBI::Loader->new(
66     debug => $self->{'debug'},
67     dsn => $self->{'dsn'},
68     user => $self->{'user'},
69     password => $self->{'passwd'},
70     namespace => "Nos",
71     # additional_classes => qw/Class::DBI::AbstractSearch/,
72     # additional_base_classes => qw/My::Stuff/,
73     relationships => 1,
74 dpavlin 22 ) || croak "can't init Class::DBI::Loader";
75 dpavlin 20
76     $self ? return $self : return undef;
77     }
78    
79 dpavlin 30
80 dpavlin 23 =head2 add_member_to_list
81    
82     Add new member to list
83    
84     $nos->add_member_to_list(
85     list => "My list",
86     email => "john.doe@example.com",
87     name => "John A. Doe",
88     );
89    
90     C<name> parametar is optional.
91    
92 dpavlin 27 Return member ID if user is added.
93 dpavlin 23
94     =cut
95    
96     sub add_member_to_list {
97     my $self = shift;
98    
99     my $arg = {@_};
100    
101 dpavlin 30 my $email = $arg->{'email'} || croak "can't add user without e-mail";
102 dpavlin 23 my $name = $arg->{'name'} || '';
103 dpavlin 30 my $list_name = $arg->{'list'} || croak "need list name";
104 dpavlin 23
105 dpavlin 30 my $list = $self->_get_list($list_name) || croak "list $list_name doesn't exist";
106    
107 dpavlin 23 if (! Email::Valid->address($email)) {
108 dpavlin 29 carp "SKIPPING $name <$email>\n" if ($self->{'verbose'});
109 dpavlin 23 return 0;
110     }
111    
112 dpavlin 29 carp "# $name <$email>\n" if ($self->{'verbose'});
113 dpavlin 23
114     my $users = $self->{'loader'}->find_class('users');
115     my $user_list = $self->{'loader'}->find_class('user_list');
116    
117     my $this_user = $users->find_or_create({
118     email => $email,
119     full_name => $name,
120     }) || croak "can't find or create member\n";
121    
122     my $user_on_list = $user_list->find_or_create({
123     user_id => $this_user->id,
124     list_id => $list->id,
125     }) || croak "can't add user to list";
126    
127     $list->dbi_commit;
128     $this_user->dbi_commit;
129     $user_on_list->dbi_commit;
130    
131 dpavlin 27 return $this_user->id;
132 dpavlin 23 }
133    
134 dpavlin 29 =head2 add_message_to_list
135 dpavlin 24
136     Adds message to one list's queue for later sending.
137    
138 dpavlin 29 $nos->add_message_to_list(
139 dpavlin 24 list => 'My list',
140     message => 'From: My list <mylist@example.com>
141     To: John A. Doe <john.doe@example.com>
142    
143     This is example message
144     ',
145     );
146    
147     On success returns ID of newly created (or existing) message.
148    
149     =cut
150    
151 dpavlin 29 sub add_message_to_list {
152 dpavlin 24 my $self = shift;
153    
154     my $args = {@_};
155    
156     my $list_name = $args->{'list'} || confess "need list name";
157     my $message_text = $args->{'message'} || croak "need message";
158    
159 dpavlin 29 my $m = Email::Simple->new($message_text) || croak "can't parse message";
160    
161 dpavlin 32 unless( $m->header('Subject') ) {
162     warn "message doesn't have Subject header\n";
163     return;
164     }
165 dpavlin 29
166 dpavlin 24 my $lists = $self->{'loader'}->find_class('lists');
167    
168     my $this_list = $lists->search(
169     name => $list_name,
170     )->first || croak "can't find list $list_name";
171    
172     my $messages = $self->{'loader'}->find_class('messages');
173    
174     my $this_message = $messages->find_or_create({
175     message => $message_text
176     }) || croak "can't insert message";
177    
178     $this_message->dbi_commit() || croak "can't add message";
179    
180     my $queue = $self->{'loader'}->find_class('queue');
181    
182     $queue->find_or_create({
183     message_id => $this_message->id,
184     list_id => $this_list->id,
185     }) || croak "can't add message ",$this_message->id," to list ",$this_list->id, ": ",$this_list->name;
186    
187     $queue->dbi_commit || croak "can't add message to list ",$this_list->name;
188    
189     return $this_message->id;
190     }
191    
192    
193 dpavlin 22 =head2 send_queued_messages
194 dpavlin 20
195 dpavlin 22 Send queued messages or just ones for selected list
196 dpavlin 20
197 dpavlin 24 $nos->send_queued_messages("My list");
198 dpavlin 20
199 dpavlin 21 =cut
200 dpavlin 20
201 dpavlin 22 sub send_queued_messages {
202 dpavlin 21 my $self = shift;
203 dpavlin 20
204 dpavlin 22 my $list_name = shift;
205 dpavlin 20
206 dpavlin 22 my $lists = $self->{'loader'}->find_class('lists');
207     my $queue = $self->{'loader'}->find_class('queue');
208     my $user_list = $self->{'loader'}->find_class('user_list');
209     my $sent = $self->{'loader'}->find_class('sent');
210 dpavlin 20
211 dpavlin 22 my $my_q;
212     if ($list_name ne '') {
213     my $l_id = $lists->search_like( name => $list_name )->first ||
214     croak "can't find list $list_name";
215     $my_q = $queue->search_like( list_id => $l_id ) ||
216     croak "can't find list $list_name";
217     } else {
218     $my_q = $queue->retrieve_all;
219     }
220 dpavlin 20
221 dpavlin 22 while (my $m = $my_q->next) {
222     next if ($m->all_sent);
223 dpavlin 20
224 dpavlin 22 print "sending message ",$m->message_id," enqueued on ",$m->date," to list ",$m->list_id->name,"\n";
225     my $msg = $m->message_id->message;
226 dpavlin 20
227 dpavlin 22 foreach my $u ($user_list->search(list_id => $m->list_id)) {
228 dpavlin 20
229 dpavlin 29 my $to_email = $u->user_id->email;
230    
231 dpavlin 32 my ($from,$domain) = split(/@/, $u->list_id->email, 2);
232    
233 dpavlin 22 if ($sent->search( message_id => $m->message_id, user_id => $u->user_id )) {
234 dpavlin 29 print "SKIP $to_email message allready sent\n";
235 dpavlin 22 } else {
236 dpavlin 32 print "=> $to_email\n";
237 dpavlin 20
238 dpavlin 32 my $secret = $m->list_id->name . " " . $u->user_id->email . " " . $m->message_id;
239     my $auth = Email::Auth::AddressHash->new( $secret, 10 );
240    
241 dpavlin 29 my $hash = $auth->generate_hash( $to_email );
242 dpavlin 20
243 dpavlin 32 my $from = $u->list_id->name . " <" . $from . "+" . $hash . ( $domain ? "@" . $domain : '' ). ">";
244 dpavlin 29 my $to = $u->user_id->full_name . " <$to_email>";
245    
246 dpavlin 32 my $m_obj = Email::Simple->new($msg) || croak "can't parse message";
247 dpavlin 29
248 dpavlin 32 $m_obj->header_set('From', $from) || croak "can't set From: header";
249     $m_obj->header_set('To', $to) || croak "can't set To: header";
250 dpavlin 29
251 dpavlin 22 # FIXME do real sending :-)
252 dpavlin 32 send IO => $m_obj->as_string;
253 dpavlin 22
254     $sent->create({
255     message_id => $m->message_id,
256     user_id => $u->user_id,
257     });
258     $sent->dbi_commit;
259     }
260     }
261     $m->all_sent(1);
262     $m->update;
263     $m->dbi_commit;
264     }
265    
266 dpavlin 20 }
267    
268 dpavlin 29 =head2 inbox_message
269    
270     Receive single message for list's inbox.
271    
272     my $ok = $nos->inbox_message($message);
273    
274     =cut
275    
276     sub inbox_message {
277     my $self = shift;
278    
279     my $message = shift || return;
280    
281     my $m = new Email::Simple->new($message);
282    
283     }
284    
285    
286 dpavlin 30 =head1 INTERNAL METHODS
287    
288     Beware of dragons! You shouldn't need to call those methods directly.
289    
290     =head2 _add_list
291    
292     Create new list
293    
294     my $list_obj = $nos->_add_list(
295     list => 'My list',
296     email => 'my-list@example.com',
297     );
298    
299     Returns C<Class::DBI> object for created list.
300    
301     =cut
302    
303     sub _add_list {
304     my $self = shift;
305    
306     my $arg = {@_};
307    
308     my $name = $arg->{'list'} || confess "can't add list without name";
309     my $email = $arg->{'email'} || confess "can't add list without e-mail";
310    
311     my $lists = $self->{'loader'}->find_class('lists');
312    
313     my $l = $lists->find_or_create({
314     name => $name,
315     email => $email,
316     });
317    
318     croak "can't add list $name\n" unless ($l);
319    
320     $l->dbi_commit;
321    
322     return $l;
323    
324     }
325    
326    
327     =head2 _get_list
328    
329     Get list C<Class::DBI> object.
330    
331     my $list_obj = $nos->check_list('My list');
332    
333     Returns false on failure.
334    
335     =cut
336    
337     sub _get_list {
338     my $self = shift;
339    
340     my $name = shift || return;
341    
342 dpavlin 31 my $lists = $self->{'loader'}->find_class('lists') || confess "can't find lists class";
343 dpavlin 30
344 dpavlin 31 return $lists->search({ name => $name })->first;
345 dpavlin 30 }
346    
347    
348 dpavlin 25 =head1 EXPORT
349 dpavlin 20
350 dpavlin 27 Nothing.
351 dpavlin 20
352     =head1 SEE ALSO
353    
354     mailman, ezmlm, sympa, L<Mail::Salsa>
355    
356 dpavlin 25
357 dpavlin 20 =head1 AUTHOR
358    
359     Dobrica Pavlinusic, E<lt>dpavlin@rot13.orgE<gt>
360    
361 dpavlin 25
362 dpavlin 20 =head1 COPYRIGHT AND LICENSE
363    
364     Copyright (C) 2005 by Dobrica Pavlinusic
365    
366     This library is free software; you can redistribute it and/or modify
367     it under the same terms as Perl itself, either Perl version 5.8.4 or,
368     at your option, any later version of Perl 5 you may have available.
369    
370    
371     =cut

  ViewVC Help
Powered by ViewVC 1.1.26