24 |
use Carp; |
use Carp; |
25 |
use Email::Auth::AddressHash; |
use Email::Auth::AddressHash; |
26 |
use Email::Simple; |
use Email::Simple; |
27 |
|
use Email::Address; |
28 |
use Data::Dumper; |
use Data::Dumper; |
29 |
|
|
30 |
=head1 NAME |
=head1 NAME |
52 |
passwd => '', |
passwd => '', |
53 |
debug => 1, |
debug => 1, |
54 |
verbose => 1, |
verbose => 1, |
55 |
|
hash_len => 8, |
56 |
); |
); |
57 |
|
|
58 |
|
Parametar C<hash_len> defined length of hash which will be added to each |
59 |
|
outgoing e-mail message. |
60 |
|
|
61 |
=cut |
=cut |
62 |
|
|
63 |
sub new { |
sub new { |
78 |
relationships => 1, |
relationships => 1, |
79 |
) || croak "can't init Class::DBI::Loader"; |
) || croak "can't init Class::DBI::Loader"; |
80 |
|
|
81 |
|
$self->{'hash_len'} ||= 8; |
82 |
|
|
83 |
$self ? return $self : return undef; |
$self ? return $self : return undef; |
84 |
} |
} |
85 |
|
|
86 |
|
|
87 |
|
=head2 new_list |
88 |
|
|
89 |
|
Create new list |
90 |
|
|
91 |
|
$nos->new_list( |
92 |
|
list => 'My list", |
93 |
|
email => 'my-list@example.com', |
94 |
|
); |
95 |
|
|
96 |
|
Returns ID of newly created list. |
97 |
|
|
98 |
|
=cut |
99 |
|
|
100 |
|
sub new_list { |
101 |
|
my $self = shift; |
102 |
|
|
103 |
|
my $arg = {@_}; |
104 |
|
|
105 |
|
confess "need list name" unless ($arg->{'list'}); |
106 |
|
confess "need list email" unless ($arg->{'list'}); |
107 |
|
|
108 |
|
my $l = $self->_get_list($arg->{'list'}) || |
109 |
|
$self->_add_list( @_ ) || |
110 |
|
return undef; |
111 |
|
|
112 |
|
return $l->id; |
113 |
|
} |
114 |
|
|
115 |
|
|
116 |
=head2 add_member_to_list |
=head2 add_member_to_list |
117 |
|
|
118 |
Add new member to list |
Add new member to list |
141 |
my $list = $self->_get_list($list_name) || croak "list $list_name doesn't exist"; |
my $list = $self->_get_list($list_name) || croak "list $list_name doesn't exist"; |
142 |
|
|
143 |
if (! Email::Valid->address($email)) { |
if (! Email::Valid->address($email)) { |
144 |
carp "SKIPPING $name <$email>\n" if ($self->{'verbose'}); |
carp "SKIPPING $name <$email>\n"; |
145 |
return 0; |
return 0; |
146 |
} |
} |
147 |
|
|
152 |
|
|
153 |
my $this_user = $users->find_or_create({ |
my $this_user = $users->find_or_create({ |
154 |
email => $email, |
email => $email, |
|
full_name => $name, |
|
155 |
}) || croak "can't find or create member\n"; |
}) || croak "can't find or create member\n"; |
156 |
|
|
157 |
|
if ($name && $this_user->full_name ne $name) { |
158 |
|
$this_user->full_name($name || ''); |
159 |
|
$this_user->update; |
160 |
|
} |
161 |
|
|
162 |
my $user_on_list = $user_list->find_or_create({ |
my $user_on_list = $user_list->find_or_create({ |
163 |
user_id => $this_user->id, |
user_id => $this_user->id, |
164 |
list_id => $list->id, |
list_id => $list->id, |
177 |
|
|
178 |
$nos->add_message_to_list( |
$nos->add_message_to_list( |
179 |
list => 'My list', |
list => 'My list', |
180 |
message => 'From: My list <mylist@example.com> |
message => 'Subject: welcome to list |
|
To: John A. Doe <john.doe@example.com> |
|
181 |
|
|
182 |
This is example message |
This is example message |
183 |
', |
', |
185 |
|
|
186 |
On success returns ID of newly created (or existing) message. |
On success returns ID of newly created (or existing) message. |
187 |
|
|
188 |
|
Only required header in e-mail is C<Subject:>. C<From:> and C<To:> headers |
189 |
|
will be automatically generated, but if you want to use own headers, just |
190 |
|
include them in messages. |
191 |
|
|
192 |
=cut |
=cut |
193 |
|
|
194 |
sub add_message_to_list { |
sub add_message_to_list { |
279 |
print "=> $to_email\n"; |
print "=> $to_email\n"; |
280 |
|
|
281 |
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; |
282 |
my $auth = Email::Auth::AddressHash->new( $secret, 10 ); |
my $auth = Email::Auth::AddressHash->new( $secret, $self->{'hash_len'} ); |
283 |
|
|
284 |
my $hash = $auth->generate_hash( $to_email ); |
my $hash = $auth->generate_hash( $to_email ); |
285 |
|
|
297 |
$sent->create({ |
$sent->create({ |
298 |
message_id => $m->message_id, |
message_id => $m->message_id, |
299 |
user_id => $u->user_id, |
user_id => $u->user_id, |
300 |
|
hash => $hash, |
301 |
}); |
}); |
302 |
$sent->dbi_commit; |
$sent->dbi_commit; |
303 |
} |
} |
313 |
|
|
314 |
Receive single message for list's inbox. |
Receive single message for list's inbox. |
315 |
|
|
316 |
my $ok = $nos->inbox_message($message); |
my $ok = $nos->inbox_message( |
317 |
|
list => 'My list', |
318 |
|
message => $message, |
319 |
|
); |
320 |
|
|
321 |
=cut |
=cut |
322 |
|
|
323 |
sub inbox_message { |
sub inbox_message { |
324 |
my $self = shift; |
my $self = shift; |
325 |
|
|
326 |
my $message = shift || return; |
my $arg = {@_}; |
327 |
|
|
328 |
|
return unless ($arg->{'message'}); |
329 |
|
croak "need list name" unless ($arg->{'list'}); |
330 |
|
|
331 |
|
my $m = Email::Simple->new($arg->{'message'}) || croak "can't parse message"; |
332 |
|
|
333 |
|
my $to = $m->header('To') || die "can't find To: address in incomming message\n"; |
334 |
|
|
335 |
|
my @addrs = Email::Address->parse( $to ); |
336 |
|
|
337 |
|
die "can't parse To: $to address\n" unless (@addrs); |
338 |
|
|
339 |
|
my $hl = $self->{'hash_len'} || confess "no hash_len?"; |
340 |
|
|
341 |
|
my $hash; |
342 |
|
|
343 |
|
foreach my $a (@addrs) { |
344 |
|
if ($a->address =~ m/\+([a-f0-9]{$hl})@/) { |
345 |
|
$hash = $1; |
346 |
|
last; |
347 |
|
} |
348 |
|
} |
349 |
|
|
350 |
|
croak "can't find hash in e-mail $to\n" unless ($hash); |
351 |
|
|
352 |
|
my $sent = $self->{'loader'}->find_class('sent'); |
353 |
|
|
354 |
|
# will use null if no matching message_id is found |
355 |
|
my $message_id = $sent->search( hash => $hash )->first->message_id; |
356 |
|
|
357 |
my $m = new Email::Simple->new($message); |
print "message_id: $message_id\n"; |
358 |
|
|
359 |
|
warn "inbox is not yet implemented"; |
360 |
} |
} |
361 |
|
|
362 |
|
|