add a reply-to header so as to be less confusing and actually work right, now that...
[p5sagit/Promulger.git] / lib / Promulger / Dispatch.pm
CommitLineData
f5baca29 1package Promulger::Dispatch;
2use strict;
3use warnings;
4
8ad89cb2 5use Email::Address;
f5baca29 6use Email::Simple;
7# XXX allow the user to specify their own Email::Sender::Transport -- apeiron,
8# 2010-03-13
9use Email::Sender::Simple qw(sendmail);
9f8395b9 10# XXX not yet -- apeiron, 2010-06-25
11#use Mail::Verp;
f5baca29 12
21baaef0 13use Promulger::Config;
14
15# XXX no bounce parsing yet -- apeiron, 2010-03-13
f5baca29 16sub dispatch {
8ad89cb2 17 my ($message) = @_;
21baaef0 18 my $config = Promulger::Config->config;
f5baca29 19
20 my $email = Email::Simple->new($message);
21 my $recipient = $email->header('To');
8ad89cb2 22 my $local_user = user_for_address($recipient);
f5baca29 23 my $sender = $email->header('From');
24 my $subject = $email->header('Subject');
25
8ad89cb2 26 my $list = Promulger::List->resolve($local_user);
f5baca29 27 unless($list) {
28 reject($recipient, $sender);
8ad89cb2 29 return;
f5baca29 30 }
31
8ad89cb2 32 if($local_user =~ /-request$/) {
33 handle_request($list, $sender, $local_user, $subject, $config);
34 return;
f5baca29 35 }
36
37 # they don't have a request for us, so they want to post a message
38 post_message($list, $email, $config);
8ad89cb2 39 return;
f5baca29 40}
41
42sub handle_request {
21baaef0 43 my ($list, $sender, $recipient, $subject) = @_;
f5baca29 44
8ad89cb2 45 my $sender_address = bare_address($sender);
46 if($subject =~ /^\s*subscribe/i) {
47 $list->subscribe($sender_address)
48 or already_subscribed($list, $recipient, $sender_address);
49 } elsif($subject =~ /^\s*unsubscribe/i) {
50 $list->unsubscribe($sender_address)
51 or not_subscribed($list, $recipient, $sender_address);
f5baca29 52 }
53}
54
55sub post_message {
8ad89cb2 56 my ($list, $email, $config) = @_;
f5baca29 57
58 my $sender = $email->header('From');
8ad89cb2 59 my $sender_address = bare_address($sender);
f5baca29 60 my $recipient = $email->header('To');
61
8ad89cb2 62 unless($list->accept_posts_from($sender_address) && $list->active) {
63 reject($recipient, $sender);
64 return;
65 }
f5baca29 66
67 # they're allowed to post (subscribed or not), the list is active. let's do
68 # this thing.
69
70 # XXX no MIME or other fancy handling for now -- apeiron, 2010-03-13
71 my $body = $email->body;
8ad89cb2 72 for my $subscriber (keys %{$list->subscribers}) {
73 #my $verped_from = Mail::Verp->encode($recipient, $subscriber);
f5baca29 74 # XXX we let the MTA create the message-id for us for now -- apeiron,
75 # 2010-03-13
76 my $new_message = Email::Simple->create(
77 header => [
e9b4d3bc 78 From => $sender_address,
79 To => $subscriber,
80 Subject => $email->header('Subject'),
81 'Reply-to' => $recipient,
f5baca29 82 ],
83 body => $body,
84 );
85 # XXX no queuing or job distribution for now beyond what the MTA provides
86 # -- apeiron, 2010-03-13
87 sendmail($new_message);
88 }
89}
90
21baaef0 91# XXX make this actually not suck -- apeiron, 2010-03-13
92sub reject {
8ad89cb2 93 my ($recipient, $sender) = @_;
21baaef0 94 my $email = Email::Simple->create(
95 header => [
96 From => $recipient,
97 To => $sender,
98 Subject => 'Rejected',
99 ],
100 body => <<BODY,
101Sorry, your message to $recipient has been denied.
102BODY
103 );
104 sendmail($email);
105}
106
107sub not_subscribed {
8ad89cb2 108 my ($list, $recipient, $sender) = @_;
21baaef0 109 my $email = Email::Simple->create(
8ad89cb2 110 # XXX need admin address
21baaef0 111 header => [
8ad89cb2 112 From => $recipient,
21baaef0 113 To => $sender,
114 Subject => 'Not subscribed',
115 ],
116 body => <<BODY,
8ad89cb2 117Sorry, you are not subscribed to $list.
21baaef0 118BODY
119 );
120 sendmail($email);
121}
122
123sub already_subscribed {
8ad89cb2 124 my ($list, $recipient, $sender) = @_;
21baaef0 125 my $email = Email::Simple->create(
126 header => [
8ad89cb2 127 From => $recipient,
21baaef0 128 To => $sender,
129 Subject => 'Already subscribed',
130 ],
131 body => <<BODY,
8ad89cb2 132Sorry, you are already subscribed to $list.
21baaef0 133BODY
134 );
135 sendmail($email);
136}
f5baca29 137
8ad89cb2 138sub bare_address {
139 my ($full_addr) = @_;
140 my ($addr_obj) = Email::Address->parse($full_addr);
141 return $addr_obj->address;
142}
143
144sub user_for_address {
145 my ($full_addr) = @_;
146 my ($addr_obj) = Email::Address->parse($full_addr);
147 return $addr_obj->user;
148}
149
f5baca29 150'http://www.shadowcat.co.uk/blog/matt-s-trout/oh-subdispatch-oh-subdispatch/';