| 1 | package Mail::Audit::Centipaid; | ||
| 2 | use Mail::Audit; | ||
| 3 | use vars q(@VERSION); | ||
| 4 | $VERSION = '1.0'; | ||
| 5 | 1; | ||
| 6 | |||
| 7 | |||
| 8 | # Mail::Audit::Centipaid | ||
| 9 | # | ||
| 10 | # Written by Adonis El Fakih (adonis /at/ aynacorp dot com) Copyright 2003 | ||
| 11 | # based on Apache::Cetipaid v1.3 | ||
| 12 | # | ||
| 13 | # This module allows mail users / administrators to charge for incoming | ||
| 14 | # mail using centipaid's internet stamps (centipix). This modules was | ||
| 15 | # inspired by the design of AMDPmail.com which proposes an alternate mail | ||
| 16 | # delivery cycle that does many new cool things including charging epostage | ||
| 17 | # as a mode of controlling spam | ||
| 18 | # | ||
| 19 | # centipaid offers a micropayment solution that allows users | ||
| 20 | # to pay using an internet stamp. For more information on the | ||
| 21 | # micro-payment system please visit http://www.centipaid.com/ | ||
| 22 | # | ||
| 23 | # | ||
| 24 | # This module may be distributed under the GPL v2 or later. | ||
| 25 | # | ||
| 26 | # | ||
| 27 | |||
| 28 | |||
| 29 | package Mail::Audit; | ||
| 30 | |||
| 31 | |||
| 32 | use 5.005; | ||
| 33 | |||
| 34 | use IO::Socket; | ||
| 35 | use Net::hostent; | ||
| 36 | |||
| 37 | |||
| 38 | |||
| 39 | |||
| 40 | sub need_to_pay($$$$$) { | ||
| 41 | my $https = shift(@_); | ||
| 42 | my $payto = shift(@_); | ||
| 43 | my $amount = shift(@_); | ||
| 44 | my $lang = shift(@_); | ||
| 45 | my $email = shift(@_); | ||
| 46 | |||
| 47 | $msg = qq {$https?payto=$payto&amount=$amount&mode=mail&email=$email}; | ||
| 48 | return $msg; | ||
| 49 | } | ||
| 50 | |||
| 51 | sub check_mail { | ||
| 52 | |||
| 53 | my $self = shift; | ||
| 54 | die "bad %conf key/values !" if (@_ % 2); | ||
| 55 | my %conf = @_ ; | ||
| 56 | my $debug = $conf{debug}; | ||
| 57 | my $payto = $conf{acct} || 0; | ||
| 58 | my $https = $conf{https} || 0; | ||
| 59 | my $server = $conf{authserver} || 0; | ||
| 60 | my $port = $conf{authport} || 0; | ||
| 61 | my $pass = $conf{pass} || 0; | ||
| 62 | my $amount = $conf{amount} || 0; | ||
| 63 | my $email = $conf{email} || $self->to; | ||
| 64 | my $prefix = "$$ Mail::Audit::Centipaid"; | ||
| 65 | my $connect_to ="$server:$port"; | ||
| 66 | my @header = split("\n",$self->header); | ||
| 67 | my $item_body = $self->body; | ||
| 68 | my @body = @$item_body; | ||
| 69 | my $rcvcount = 0; | ||
| 70 | my $rcpt = 0; | ||
| 71 | |||
| 72 | print $body; | ||
| 73 | |||
| 74 | # loop through the oconfiguration hash and print it | ||
| 75 | # if it is requiered | ||
| 76 | foreach my $key (keys %conf) { | ||
| 77 | print "DEBUG: $key = $conf{$key}\n" if $debug; | ||
| 78 | } | ||
| 79 | |||
| 80 | |||
| 81 | # loop through the header first to see if it has a | ||
| 82 | # compliant AMDP mail header AMDP-PAYMENT-RCPT | ||
| 83 | print "DEBUG: Checking SMTP header\n" if $debug; | ||
| 84 | for (@header) { | ||
| 85 | |||
| 86 | if ( /AMDP-PAYMENT-RCPT:(.*)/ ) { | ||
| 87 | $rcpt = $1; | ||
| 88 | #clean rcpt from any spaces.. | ||
| 89 | $rcpt =~ s/\s+//g; | ||
| 90 | last; | ||
| 91 | } | ||
| 92 | |||
| 93 | print "HEADER: $_\n" if $debug; | ||
| 94 | $rcvcount++; # Any further Received lines won't be the first. | ||
| 95 | } | ||
| 96 | |||
| 97 | |||
| 98 | #if no receipt in header then check the body | ||
| 99 | unless($rcpt) { | ||
| 100 | # loop through the body next to see if it has a | ||
| 101 | # compliant AMDP mail receipt AMDP-PAYMENT-RCPT | ||
| 102 | print "DEBUG: Checking mail BODY\n" if $debug; | ||
| 103 | for (@body) { | ||
| 104 | chomp; | ||
| 105 | |||
| 106 | if ( /AMDP-PAYMENT-RCPT:\s*(\S+)\s*[!^<]/ || /AMDP-PAYMENT-RCPT:\s*(\S+)/) { | ||
| 107 | $rcpt = $1; | ||
| 108 | #clean rcpt from any spaces.. | ||
| 109 | $rcpt =~ s/\s+//g; | ||
| 110 | last; | ||
| 111 | } | ||
| 112 | |||
| 113 | |||
| 114 | print "BODY: $_\n" if $debug; | ||
| 115 | $rcvcount++; # Any further Received lines won't be the first. | ||
| 116 | } | ||
| 117 | } | ||
| 118 | |||
| 119 | if ( $rcpt ) { | ||
| 120 | |||
| 121 | print "DEBUG: Found AMDP receipt $rcpt\n" if $debug; | ||
| 122 | print "DEBUG: Checking with CENTIPAID at $connect_to\n" if $debug; | ||
| 123 | |||
| 124 | my $auth_server = IO::Socket::INET->new("$connect_to"); | ||
| 125 | my $crlf = "\015\012"; | ||
| 126 | unless ( $auth_server ) { | ||
| 127 | print "DEBUG: Could not connect to $connect_to\n" if $debug; | ||
| 128 | return (1,"Can not connect to $connect_to"); | ||
| 129 | } | ||
| 130 | |||
| 131 | $auth_server->autoflush(1); | ||
| 132 | print $auth_server "PAYTO:$payto"."$crlf"; | ||
| 133 | print $auth_server "PASS:$pass"."$crlf"; | ||
| 134 | print $auth_server "RCPT:$rcpt"."$crlf"; | ||
| 135 | |||
| 136 | # format the amount in a way that we make sure it becomes a float | ||
| 137 | $amount = sprintf("%.6f",$amount); | ||
| 138 | while (<$auth_server>) { | ||
| 139 | chomp; | ||
| 140 | my $received = $_; | ||
| 141 | |||
| 142 | |||
| 143 | print "DEBUG: CLIENT:$received\n" if $debug; | ||
| 144 | |||
| 145 | if ($received =~ /^250 OK PAID(.+)/) { | ||
| 146 | # format the paid in a way that we make sure it becomes a float | ||
| 147 | my $paid = sprintf("%.6f",$1); | ||
| 148 | print "DEBUG: Paid [$paid] Amount [$amount]\n" if $debug; | ||
| 149 | |||
| 150 | # if the amount paid is greater or equal to what | ||
| 151 | # is requiered then it is ok | ||
| 152 | if ( $paid >= $amount ) { | ||
| 153 | print "DEBUG: $paid == $amount\n" if $debug; | ||
| 154 | print "DEBUG: epostage verified\n" if $debug; | ||
| 155 | |||
| 156 | return (0,"OK"); # success | ||
| 157 | |||
| 158 | }# if paid == amount | ||
| 159 | |||
| 160 | }#end if 250 | ||
| 161 | |||
| 162 | |||
| 163 | if ($received =~ /^500/) { | ||
| 164 | # if we get a 500 code then it was an invalid receipt | ||
| 165 | print "DEBUG: Invalid transaction for receipt $rcpt\n" if $debug; | ||
| 166 | |||
| 167 | #send a payment slip | ||
| 168 | $msg = need_to_pay($https,$payto,$amount,$lang,$email); | ||
| 169 | print "DEBUG: need_to_pay called\n" if $debug; | ||
| 170 | return (1,$msg); | ||
| 171 | |||
| 172 | }#end if 500 | ||
| 173 | |||
| 174 | |||
| 175 | }# end while | ||
| 176 | |||
| 177 | } else { | ||
| 178 | |||
| 179 | $msg = need_to_pay($https,$payto,$amount,$lang,$email); | ||
| 180 | return (1,$msg); | ||
| 181 | |||
| 182 | } #end of rcpt found | ||
| 183 | |||
| 184 | } | ||
| 185 | |||
| 186 | 1; | ||
| 187 | |||
| 188 | __END__ |