-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadvenge
320 lines (251 loc) · 8.81 KB
/
advenge
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
=head1 NAME
advenge
=head1 DESCRIPTION
the advenge server plugin
=head1 CONFIG
It takes one required parameter, a directory which is to contain the
configuration DirDB.
advenge /var/advenge/config
=head1 STATUS
this is a work in progress prototype that may be replaced with an
envisioned "Perry" per recipient system which will be abstracted
out of what is done here.
=cut
use DirDB;
#no warnings;
my %AConfig;
my %Acache;
my %Acache2;
my $CacheFlushCounter = 0;
sub register {
my ($self, $qp, @args) = @_;
die "Invalid advenge args: '@args'" unless @args == 1;
my $dir = shift @args;
warn "advenge config dir [$dir]";
die "Invalid advenge args: $dir not a directory" unless -d $dir;
$dir =~ m{^([\w/.-]+)$} or die "FUNNY CHARAXCTER IN <$dir>";
tie %AConfig, DirDB => "$1";
}
sub hook_disconnect {
my ($self) = @_;
return DECLINED;
=head1 TODO: on disconnect
possibly reverse a transaction that is partially completed, or issue a refund
=cut
}
sub Advenge::Sender::NewSenderObject {
my $PNA = $_[1];
$PNA =~ /(\d+\.\d+\.\d+\.\d+)/ or do {
warn "NOT SET UP FOR IPv6 (got $PNA)";
$_[1] = '999.999.999.999';
goto &Advenge::Sender::NewSenderObject;
};
bless {
#RA => $_[2],
#PNA => $_[0]->qp->connection->remote_ip
ADDRESS => $_[0], PNA =>$1
}, 'Advenge::Sender'
}
sub Advenge::Sender::InvalidSender{
my $obj = shift;
$obj->{denymessage} = "random failure for testing purposes, please try again";
return ( rand(100) < 2)
}
sub Advenge::Sender::WillPay{
my ($obj, $price, $cur) = @_;
0 # at this time; FIXME
};
sub hook_mail {
my ($self, $transaction, $address, %param) = @_;
my $SenderObj = $self->qp->connection->notes('advenge_sender',Advenge::Sender::NewSenderObject($address->format,$self->qp->connection->remote_ip));
$self->qp->connection->notes('advenge_rcpts',0);
$self->qp->connection->notes('advenge_wl',0);
$self->qp->connection->notes('advenge_check_data',0);
$SenderObj->InvalidSender and return DENYSOFT,$SenderObj->{denymessage};
return DECLINED;
}
sub Advenge::Recipient::NewRecipient {
my $address = shift;
my $user = $address->user;
my $host = $address->host;
exists $AConfig{DOMAINS}{$host} or return undef;
my $PRC = $AConfig{DOMAINS}{$host}{$user} || $AConfig{DOMAINS}{"$host.catchall"};
$PRC or return undef;
bless {
PRICE => 99,
CURRENCYDESCRIPTION => 'Troy Ounces of Tipjarium',
CUR => 'TJ90',
user => $user, host => $host, address => "$address",
( $PRC =~ /^([A-Z0123456789_.]+)\s*(.*)/gm )
}, 'Advenge::Recipient'
}
our $POBJ;
sub Advenge::Recipient::PendingAlready{
my $obj = shift;
my $senderaddress = shift;
my $QUAD = shift; # should be an untainted dotted quad.
$senderaddress =~ s/<(.+)>/$1/;
my ($recipUser, $recipDomain) = @$obj{qw/user host/};
for ($recipDomain,$recipUser,$senderaddress){
length $_ or die "SHORT";
s/([^\[email protected]])/join (ord($1),'%','%')/ge;
};
my ($DrecipDomain,$DrecipUser,$Dsenderaddress) = map {
/([\w@\-%.]+)/ or die "STRANGENESS";
($1); # DETAINTED
} ($recipDomain,$recipUser,$senderaddress);
$obj->{self}->log(LOGINFO, "checking PENDING {$DrecipDomain}{$DrecipUser}{$Dsenderaddress} via $QUAD");
my $total = $AConfig{PENDING}{$DrecipDomain}{$DrecipUser}{$Dsenderaddress}{$QUAD}++;
$POBJ = $AConfig{PENDING}{$DrecipDomain}{$DrecipUser}{$Dsenderaddress};
$total;
}
sub Advenge::Recipient::check_data {
my $obj = shift;
warn "checking data";
return 0;
$obj->{CHECK_DATA} or return 1;
# do something with magic phrases
my $mpsuffix = 1;
my $transaction = shift;
my @MPs;
while($obj->{"MP$mpsuffix"}){
push @MPs, qr/$obj->{"MP".$mpsuffix++}/i;
};
$transaction->body_resetpos;
my $lastline = '';
my $linecounter = 0;
while (defined(my $line = $transaction->body_getline)) {
my $this = "$line $lastline";
$this =~ s/\s+/ /g;
for (@MPs){
$this =~ m/$_/ and return 1
};
$lastline = $line;
$linecounter++ > 100 and return 0;
};
return 0;
};
our $WLreason;
sub Advenge::Recipient::whitelistp {
my $obj = shift;
my $SenderObj = shift;
$WLreason = "not filtering";
exists $obj->{WLFILTER} or return 1;
$WLreason = "trusted server";
exists $obj->{join '-', 'HOST-WL', $SenderObj->{PNA}} and return 1;
$WLreason = "friends list";
exists $obj->{join '-', 'PEER-WL', $SenderObj->{ADDRESS}} and return 1;
$WLreason = "too expensive";
my ($price, $cur) = @$obj{qw/PRICE CUR/};
$SenderObj->WillPay($price,$cur) or return 0;
$WLreason = "insufficient funds";
$SenderObj->Debit($price,$cur) or return 0;
$obj->Credit($price,$cur);
$WLreason = "Paid $price $cur";
1;
};
sub Advenge::Recipient::forwardto {
my $obj = shift;
my $f2 = $obj->{FORWARDTO} or return undef;
# support SPF macros here?
$f2 =~ s/%l/$obj->{user}/g;
$f2 =~ s/%d/$obj->{host}/g;
"<$f2>" =~ /<([^<]*)>/ and return $1;
die "WEIRDNESS";
}
sub hook_rcpt { # FIXME: extend to support payment.
my ($self, $transaction, $address, %param) = @_;
my $frcpts;
($frcpts=$self->qp->connection->notes('advenge_rcpts')) or $self->qp->connection->notes('advenge_rcpts',$frcpts=[]);
return (DECLINED) if $self->qp->connection->relay_client();
my $SenderObj = $self->qp->connection->notes('advenge_sender');
my $addr = $address->format;
my $RecipObj = Advenge::Recipient::NewRecipient( $address );
$RecipObj or return(DENY, "no recipient record for $addr");
$RecipObj->{ERROR} and return( DENY,$RecipObj->{ERROR});
my $f2 = $RecipObj->forwardto;
$f2 or return(DENYSOFT, "no forwarding record on file for $address at this time");
$self->log(LOGINFO, "if allowed, will forward to $f2");
if( $RecipObj->whitelistp($SenderObj)){
$self->qp->connection->notes('advenge_wl',1);
}else{
my $message = <<MSG;
not whitelisted: $WLreason
++++++++
only one non-whitelisted recipient is allowed per message, and
it must be the only recipient.
++++++++
You may be able to buy your way on to $RecipObj->{address}'s
whitelist however, by funding an advenge account with more
than $RecipObj->{PRICE} $RecipObj->{CURRENCYDESCRIPTION}
and setting your advenge.com delivery payment options accordingly.
++++++++
It is also possible that the recipient will approve receiving
messages from $SenderObj->{ADDRESS}
or (for mailing lists) arriving via $SenderObj->{PNA}
during the initial new contact period.
++++++++
MSG
$self->qp->connection->notes('advenge_MSG',$message);
if(@$frcpts or $self->qp->connection->notes('advenge_check_data')){
return(DENYSOFT,$message);
};
$self->qp->connection->notes('advenge_check_data',$RecipObj);
$self->qp->connection->notes('advenge_forward_to',$f2);
return OK, $message;
};
push @$frcpts, $f2;
return OK, $WLreason;
}
sub hook_data {
my ($self, $transaction) = @_;
my $RecipObj = $self->qp->connection->notes('advenge_check_data');
if (
$RecipObj and
$RecipObj->{self} = $self and
$RecipObj->PendingAlready(
$self->qp->connection->notes('advenge_sender')->{ADDRESS},
$self->qp->connection->notes('advenge_sender')->{PNA}
)
){
return DENYSOFT, "recipient has been informed of this sender's presence\n"
. $self->qp->connection->notes('advenge_MSG');
};
$transaction->notes('advenge_PENDINGOBJ',$POBJ);
undef $POBJ;
DECLINED
};
sub hook_data_post {
my ($self, $transaction) = @_;
my $frcpts;
$frcpts = $self->qp->connection->notes('advenge_rcpts');
ref $frcpts or do {
$self->log(LOGINFO, "no advenge recipients defined");
return DECLINED;
};
$self->log(LOGINFO, "desire forwarding to @$frcpts");
return (DECLINED) if $self->qp->connection->relay_client();
my $OriginalRecipients = join ' ', map { $_->address } $transaction->recipients;
if($self->qp->connection->notes('advenge_check_data') ){
chomp(my $FromHeader = $transaction->header->get('From'));
chomp(my $SubjectLine = $transaction->header->get('Subject'));
if($self->qp->connection->notes('advenge_check_data')->check_data($transaction)){
push @{$frcpts}, $self->qp->connection->notes('advenge_check_data')->forwardto;
}else{
my $message = $self->qp->connection->notes('advenge_MSG');
$transaction->notes('advenge_PENDINGOBJ')->{SUBJECTLINE} = $SubjectLine;
return(DENYSOFT, <<POSTMESSAGE)
++++++
your message was not automatically approved
$OriginalRecipients will be apprised that
$FromHeader is trying to contact them with a subject line
$SubjectLine
$message
POSTMESSAGE
}
};
$transaction->header->add('X-Advenge-Original-Recipients', $OriginalRecipients,0);
$transaction->recipients( map { s/<+//; s/>+//; Qpsmtpd::Address->new("<$_>") } @$frcpts);
$self->log(LOGINFO, " actually forwarding to @{[$transaction->recipients ]}");
return DECLINED;
}