git ssb

0+

cel / ledger-scripts



Tree: f1521af2d73e96acaa4e96dfc60dbfc50373845a

Files: f1521af2d73e96acaa4e96dfc60dbfc50373845a / claws-filter

5305 bytesRaw
1# vi: syntax=perl
2#
3# Write ledger entries upon receiving emails about transactions
4
5use strict;
6use Date::Parse;
7use POSIX qw(strftime);
8use MIME::Base64;
9
10sub write_debug {
11 open FILE, '>', 'clawfilter.out';
12 print FILE shift;
13 close FILE;
14}
15
16sub write_entry {
17 my ($payee, $amount, $account_to, $account_from, $amount_from) = @_;
18 my $date = header('date');
19 my $ledger_file = $ENV{LEDGER_FILE} // "/home/cel/Documents/Finances/journal.ledger";
20
21 open LEDGER, '>>', $ledger_file
22 or die("Failed to open journal $ledger_file: $! in $ENV{PWD}");
23 $date = strftime "%Y/%m/%d", localtime(str2time($date));
24 printf LEDGER "\n%s %s", $date, $payee;
25 printf LEDGER "\n %-32s %14s", $account_to, $amount;
26 if ($amount_from) {
27 printf LEDGER "\n %-32s %14s\n", $account_from, $amount_from;
28 } else {
29 printf LEDGER "\n %s\n", $account_from;
30 }
31 close LEDGER;
32}
33
34sub dividend_havelock {
35 my $body = body();
36 if (my ($ticker, $amount) = $body =~ /You have received a dividend payment from fund: (.*?)\n.*Total Dividend: (.*?) *(?:BTC)?\n/s) {
37 write_entry "$ticker Dividend", "$amount BTC", "Assets:Broker:Havelock", "Income:Dividends";
38 mark_as_read();
39 }
40}
41
42sub dividend_cryptostocks {
43 my $body = body();
44 if (my ($ticker, $amount) = $body =~ /\((.*?)\) has paid a dividend .*? received a total dividend payment of (.*)/) {
45 write_entry "$ticker Dividend", $amount, "Assets:Broker:Cryptostocks", "Income:Dividends";
46 mark_as_read();
47 }
48}
49
50sub payment_paypal {
51 my $paypal_account = 'Assets:Broker:PayPal';
52 my $body = body();
53 if (header('Content-Transfer-Encoding') eq 'base64') {
54 $body = decode_base64($body);
55 }
56 my $subject = header('Subject');
57 my ($amount, $payee);
58
59 if ((($payee) = $subject =~ /(?:Receipt for [Yy]our|You have authorized a) [Pp]ayment to (.*)$/)
60 && (($amount) = $body =~ /Total: =24(.*?) USD/)) {
61 write_entry $payee, '$'.$amount, 'Expenses', $paypal_account;
62
63 } elsif ((($payee) = $subject =~ /Receipt for your donation to (.*)$/)
64 && (($amount) = $body =~ /Total: =24(.*?) USD/)) {
65 write_entry $payee, '$'.$amount, 'Expenses:Donations', $paypal_account;
66
67 } elsif (($amount, $payee) = $body =~ /You sent a (?:mobile )?payment for (.*?) USD to (.*?)\./) {
68 write_entry $payee, $amount, 'Expenses', $paypal_account;
69
70 } elsif (($amount, $payee) = $body =~ /Amount:=24(.*?) USD\s*To:(.*?)[\r\n]/) {
71 write_entry $payee, '$'.$amount, 'Expenses', $paypal_account;
72
73 # non-USD
74 } elsif ((my ($to_amount, $payee) = $body =~ /You sent a payment of =..(.*?) to (.*?)\./) &&
75 (($amount) = $body =~ /From=20amount: =24(.*?) USD[\r\n]/)) {
76 write_entry $payee, $to_amount, 'Expenses', $paypal_account, '$-'.$amount;
77
78 } elsif (($payee, $amount) = $subject =~ /(.*) sent you (.*) USD/) {
79 my $description = $payee;
80 if (my ($note) = $body =~ /[\r\n]Note from .*?:(.*)[\r\n]/) {
81 $description .= " ; $note";
82 }
83 write_entry $description, $amount, $paypal_account, 'Income';
84
85 } else {
86 return;
87 }
88 mark_as_read();
89}
90
91sub payment_sent_dwolla {
92 my ($base64) = body() =~ /\n\n(.*?)\n\n/s;
93 return unless $base64;
94 my $text = decode_base64($base64);
95 if (my ($amount, $payee) = $text =~ /You have successfully sent (\$.*?) to (.*)\./) {
96 write_entry $payee, $amount, "Expenses", "Assets:Broker:Dwolla";
97 mark_as_read();
98 }
99}
100
101sub payment_sent_coinbase {
102 my $amount = shift;
103 my ($description) = body() =~ /<p>Attached message:<\/p>\s*<blockquote>(.*?)<br>/s;
104 $description ||= 'Withdraw from Coinbase';
105
106 write_entry $description, $amount, "Expenses", "Assets:Bitcoin:Coinbase";
107 mark_as_read();
108}
109
110sub payment_venmo {
111 my $venmo_account = 'Assets:Broker:Venmo';
112 my ($payee, $amount, $type) = @_;
113 my ($note) = body() =~ /<!-- note -->\s*<div>\s*<p>(.*?)<\/p>/s;
114 my $description = "$payee ; $note";
115 if ($type > 0) {
116 write_entry $description, $amount, $venmo_account, 'Income';
117 } else {
118 write_entry $description, $amount, 'Expenses', $venmo_account;
119 }
120 mark_as_read();
121}
122
123my ($from) = extract_addresses(header('from'));
124my ($subject) = header('subject');
125
126exit if (!$from || !$subject);
127
128dividend_havelock
129 if ($from =~ /\@havelockinvestments\.com$/ &&
130 $subject eq '[Havelock Investments] Dividend Received');
131
132dividend_cryptostocks
133 if ($from eq 'customerservice@cryptostocks.com' &
134 $subject eq 'Dividend payment received');
135
136payment_paypal
137 if (($from eq 'member@paypal.com' && $subject =~ /sent you/) ||
138 ($from eq 'service@paypal.com' &&
139 ($subject =~ /^You sent/ ||
140 $subject =~ /^Receipt for/ ||
141 $subject =~ /^You have authorized/)));
142
143payment_sent_dwolla
144 if ($from eq 'no-reply@dwolla.com' &&
145 $subject =~ /^You have sent money/);
146
147payment_sent_coinbase $1
148 if ($from eq 'contact@coinbase.com' &&
149 $subject =~ /^You just sent (.*? BTC) /);
150
151if ($from eq 'venmo@venmo.com') {
152 payment_venmo $1, $2, -1
153 if ($subject =~ /^You paid (.*) (.*?)$/);
154
155 payment_venmo $1, $2, 1
156 if ($subject =~ /^(.*) paid you (.*?)$/);
157}
158

Built with git-ssb-web