-
Notifications
You must be signed in to change notification settings - Fork 25
/
ilbot.psgi
executable file
·208 lines (191 loc) · 6.76 KB
/
ilbot.psgi
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
#!/usr/bin/perl
use lib 'lib'; # just for local testing
# TO BE REPLACED BY THE INSTALLER
use 5.010;
use Ilbot::Config '/home/moritz/src/ilbot/config/';
use Ilbot::Frontend;
use Ilbot::Backend::SQL;
use Ilbot::Backend::Cached;
use Ilbot::Date qw/today/;
use Date::Simple qw/date/;
use Encode qw/decode_utf8 encode_utf8/;
use JSON;
use constant DAY => 24 * 3600;
# I don't know what the p5 porters where thinking
# when they enabled this warning by default
no if $] >= 5.018, 'warnings', "experimental::smartmatch";
use Config::File qw/read_config_file/;
use Data::Dumper;
use Plack::Request;
use Ilbot::Config;
my $frontend = _frontend();
# some channels like #perl.pl contain dots,
# be we can't generally allow dots in channel names,
# because then robots.txt etc. would be handled that way.
my $channel_re = join '|', '[^./]+',
map quotemeta,
grep /\W/,
map { s/^#+//; $_ }
@{ $frontend->backend->channels };
$channel_re = qr{(?:$channel_re)};
my $handle_json = sub {
my $req = shift;
state $json_fe = _json_frontend();
my $error;
given ($req->path_info) {
when ( qr{ ^/$ }x ) {
$s = $json_fe->index;
}
when ( qr{ ^/ ($channel_re) /? $ }x ) {
$s = $json_fe->channel_index(channel => $1, out_fh => $OUT)
or $error = 'No such channel';
}
when ( qr! ^/ ($channel_re) / (\d{4}-\d{2}-\d{2}) $ !x ) {
$s = $json_fe->day(
channel => $1,
day => $2,
) or $error = 'No such channel or day';
}
};
return ($s, $error);
};
my $app = sub {
my $env = shift;
$frontend->ping();
my $req = Plack::Request->new($env);
my $want_json = $req->headers->header('Accept') eq 'application/json';
if ($want_json) {
state $json_headers = ['Content-Type', 'application/json; charset=UTF-8'];
my ($res, $error) = $handle_json->($req);
if ($res) {
# TODO: Content-Type header
return [200, $json_headers, [encode_json $res]];
}
else {
return [404, $json_headers, [encode_json { error => $error || 'URL not known' }]];
}
}
my $s;
my @header;
my $base_url = config(www => 'base_url');
my $protocol = config(www => 'protocol');
given ($req->path_info) {
when ( qr{ ^/$ }x ) {
$s = $frontend->index;
push @header, "Cache-Control", 'max-age=' . (5 * DAY);
}
when (qr!^/e/($channel_re)/(\d{4}-\d{2}-\d{2})/summary\z!) {
$s = $frontend->summary_ids(channel => "#$1", day => $2);
return [200, ['Content-Type', 'application/json'], [$s]];
}
when (qr!^/e/($channel_re)/(\d{4}-\d{2}-\d{2})/ajax/(\d+)\z!) {
$s = $frontend->day(
channel => $1,
day => $2,
after_id => $3,
);
return [200, ['Content-Type', 'application/json'], [
encode_json({
text => $s,
still_today => $2 eq today() ? JSON::true : JSON::false,
}),
]];
# TODO: return value!
}
when ( qr{ ^/e/summary }x ) {
my $p = $req->body_parameters;
my %actions;
for my $a (qw(check uncheck)) {
if ($p->{$a} =~ /^([0-9]+(?:\.[0-9]+)*)\z/) {
$actions{$a} = [ split /\./, $1 ];
}
}
if ($req->method eq 'POST' && keys(%actions)) {
$frontend->update_summary(%actions);
return [201, [], []];
}
}
when ( qr{ ^/ ($channel_re) /?$}x ) {
$s = $frontend->channel_index(channel => $1, out_fh => $OUT)
or return [404, ['Content-Type' => 'text/plain'], ['No such channel']];
}
when ( qr{ ^/ ($channel_re) /search/?$}x ) {
my $p = $req->query_parameters;
$s = $frontend->search(
channel => $1,
q => decode_utf8(scalar($p->{q})),
nick => scalar($p->{nick}),
offset => scalar($p->{offset}),
)
or return [404, ['Content-Type' => 'text/plain'], ['No such channel']];
}
when ( qr{ ^/ ($channel_re) /today $}x ) {
my $url = join '', "$protocol://",
$env->{HTTP_HOST},
"$base_url$1/",
today();
return [302, [Location => $url ], []];
}
when ( qr{ ^/ ([^./]+) /yesterday $}x ) {
my $url = join '', "$protocol://",
$env->{HTTP_HOST},
"$base_url$1/",
date(today()) - 1;
return [302, [Location => $url ], []];
}
when ( qr! ^/ ($channel_re) / (\d{4}-\d{2}-\d{2}) ( (?: /summary)? ) $!x ) {
push @header, "Cache-Control", 'max-age=' . (30 * DAY)
if !$3 && $2 ne today();
$s = $frontend->day(
channel => $1,
day => $2,
summary => !! $3,
)
or return [404, ['Content-Type' => 'text/plain'], ['No such channel/day']];
}
when ( qr! ^/ ($channel_re) / (\d{4}-\d{2}-\d{2}) /text $!x ) {
push @header, "Cache-Control", 'max-age=' . (30 * DAY)
if $2 ne today();
$s = $frontend->day_text(
channel => $1,
day => $2,
)
or return [404, ['Content-Type' => 'text/plain'], ['No such channel/day']];
return [200, ["Content-Type" => "text/plain; charset=UTF-8"], [encode_utf8 $s]];
}
default {
return [404, [], []];
}
}
my $h = $frontend->http_header( accept => $env->{HTTP_ACCEPT} );
$h = [ @header, @$h ] if @header;
close $out_fh;
return [200, $h, [encode_utf8 $s]];
};
my $c = \&config;
my $rate = config('www' => 'throttle');
use Plack::Builder;
if ($rate) {
$app = builder {
enable 'Throttle::Lite',
limits => "$rate req/hour",
backend => 'Simple',
routes => qr{.},
;
$app;
};
}
$app = builder {
enable "Plack::Middleware::Static",
path => qr{^/(?:robots\.txt|s/|\.well-known/)},
root => $c->(www => 'static_path');
if ( -d $c->('config_root') . '/d' ) {
enable "Plack::Middleware::Static",
path => qr[^/($channel_re)/\d{4}-\d{2}-\d{2}/?],
pass_through => 1,
content_type => 'text/html; charset=UTF-8',
root => $c->('config_root') . '/d';
}
$app;
};
# vim: ft=perl