-
Notifications
You must be signed in to change notification settings - Fork 0
/
mastodon.pl
executable file
·182 lines (148 loc) · 4.66 KB
/
mastodon.pl
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
#!/usr/bin/perl -w
use strict;
use warnings;
use autodie;
use Data::Dumper; $Data::Dumper::Indent = 1;
use Config::Tiny;
use File::Basename qw{ dirname };
use LWP::UserAgent;
use JSON qw{ decode_json };
use DateTime::Format::RFC3339;
use HTML::Strip;
use HTML::Entities qw{ encode_entities };
use MIME::Entity;
use Mail::IMAPClient;
use Digest::MD5 qw{ md5_hex };
use Encode qw{ encode };
my $CONFIG_FILE = dirname($0) . '/mastodon.ini';
# at least two ways to get an token to access your account:
# 1. follow the API guide to get an application/client set up and then login:
# https://docs.joinmastodon.org/client/token/
# https://docs.joinmastodon.org/client/authorized/
# 2. create application via instance UI which will also set up an authenticated
# access token for your account
# https://aus.social/settings/applications
my $config = Config::Tiny->read($CONFIG_FILE)
or die Config::Tiny->errstr();
##
# directly hit the endpoint to get JSON of timeline
my $ua = LWP::UserAgent->new('agent' => 'popcorncx-imap-gateway/0.1');
$ua->default_header('Authorization' => 'Bearer ' . $config->{'mastodon'}->{'access_token'});
my $hs = HTML::Strip->new('emit_spaces' => 0);
my $dtf = DateTime::Format::RFC3339->new();
##
# limit of 1000 should be way over the top for checking every few hours
my $url = sprintf('https://%s/api/v1/timelines/home?limit=1000', $config->{'mastodon'}->{'instance'});
if ( $config->{'_'}->{'since_id'} )
{
$url .= sprintf('&since_id=%s', $config->{'_'}->{'since_id'});
}
my $response = $ua->get($url);
if ( not $response->is_success() )
{
die $url . q{: } . $response->status_line();
}
my $timeline = eval {
decode_json( $response->decoded_content() )
};
if ( my $error = $@ )
{
die "Cannot parse JSON: $@";
}
my @updates = ();
POST: foreach my $post ( @{$timeline} )
{
my $update = {
'id' => $post->{'id'},
'name' => ( $post->{'account'}->{'display_name'} || $post->{'account'}->{'username'} ),
'content' => $post->{'content'},
'attachments' => $post->{'media_attachments'},
'url' => $post->{'url'},
'time' => $dtf->parse_datetime( $post->{'created_at'} )->epoch(),
};
if ( my $reblog = $post->{'reblog'} )
{
$update->{'name'} .= ' / ' . ( $reblog->{'account'}->{'display_name'} || $reblog->{'account'}->{'username'} );
$update->{'content'} = $reblog->{'content'};
$update->{'url'} = $reblog->{'url'};
}
push @updates, $update;
}
exit if not @updates;
##
my $imap = Mail::IMAPClient->new(
'Server' => $config->{'imap'}->{'server'},
'Port' => 993,
'Ssl' => 1,
'User' => $config->{'imap'}->{'username'},
'Password' => $config->{'imap'}->{'password'},
) or die $@;
POST: foreach my $update ( sort { $a->{'id'} <=> $b->{'id'} } @updates )
{
my @content = (
encode('UTF-8', $update->{'content'}),
'<hr />',
);
if ( my @attachments = @{ $update->{'attachments'} || [] } )
{
foreach my $attachment ( @attachments )
{
if ( $attachment->{'type'} eq 'image' )
{
push @content, sprintf(
'<p><a href="%s"><img src="%s" alt="%s" /></a></p>',
encode_entities( $attachment->{'url'} ),
encode_entities( $attachment->{'preview_url'} ),
encode_entities( $attachment->{'description'} || q{} ),
);
}
else
{
push @content, sprintf(
'<p><a href="%s">%s</a></p>',
encode_entities( $attachment->{'url'} ),
encode_entities( $attachment->{'description'} || $attachment->{'url'} ),
);
}
}
push @content, '<hr />';
}
push @content, sprintf(
'<p>URL: <a href="%s">%s</a></p>',
encode_entities( $update->{'url'} ),
encode_entities( $update->{'url'} ),
);
my $message_id = md5_hex(
time(), $update->{'time'}, @content, rand(),
) . '.' . $config->{'imap'}->{'address'};
my $subject = $hs->parse( $update->{'content'} );
if ( length($subject) > 72 )
{
$subject = substr($subject, 0, 72) . '...';
}
my $entity = MIME::Entity->build(
'From' => encode('MIME-Header', $update->{'name'}) . ' <' . $config->{'imap'}->{'address'} . '>',
'To' => $config->{'imap'}->{'address'},
'Subject' => encode('MIME-Header', $subject),
'Date' => Mail::IMAPClient->Rfc822_date( $update->{'time'} ),
'Message-Id' => $message_id,
'Data' => \@content,
'Type' => 'text/html',
'Encoding' => '-SUGGEST',
'Charset' => 'UTF-8',
);
foreach my $folder ( split( ',', $config->{'imap'}->{'folder'} ) )
{
$imap->append_string(
$folder,
$entity->as_string(),
undef,
Mail::IMAPClient->Rfc3501_datetime( $update->{'time'} ),
) or die $imap->LastError();
}
if ( $update->{'id'} > $config->{'_'}->{'since_id'} )
{
$config->{'_'}->{'since_id'} = $update->{'id'};
$config->write($CONFIG_FILE);
}
}