mason/pastebin/dhandler: Pick a better status code for `.badhash'.
[odin-cgi] / mason / pastebin / dhandler
CommitLineData
be24e9af
MW
1<%perl>
2 my $tag = $m->dhandler_arg;
3
4 sub set_handoff_cookie ($$%) {
5 my ($tag, $edit, %attr) = @_;
6 Odin::bake_cookie $r, "odin-handoff.$tag=$edit",
7 -path => "$Odin::PASTEBIN_PATH/", %attr;
8 }
9
7e455e62
MW
10 sub hasuff ($) {
11 my ($edit) = @_;
12 return $Odin::COOKIE{"odin-cookie-probe"} ? "" : "?honc=$edit";
13 }
14
15 Odin::bake_cookie $r, "odin-cookie-probe=t";
16
be24e9af
MW
17 my %props = (
18 lang => $lang, title => $title,
19 content => Odin::tidy_pastebin_content $content
20 );
21
22 if (length $tag) {
23
24 if (!defined $edit) {
25 my $db = Odin::open_db;
26 Odin::get_pastebin $db, $tag, my %old;
27 if ($op eq "raw") {
28 $r->content_type("text/plain; charset=utf8");
29 $m->print($old{content});
30 } else {
f8e94fc6 31 $edit = $Odin::COOKIE{"odin-handoff.$tag"};
7e455e62
MW
32 $m->comp("%show", tag => $tag,
33 honc => $honc, edit => $edit // $honc, %old);
be24e9af
MW
34 }
35 } else {
36 if ($op eq "del") {
37 Odin::delete_pastebin $tag, $edit;
38 set_handoff_cookie $tag, "nil", -max_age => 5;
39 $m->redirect("$Odin::PASTEBIN/");
40 } else {
be24e9af 41 set_handoff_cookie $tag, $edit;
f8e94fc6 42 my $editp = Odin::update_pastebin $tag, $edit, %props;
7e455e62
MW
43 if ($editp) {
44 $m->redirect("$Odin::PASTEBIN/$tag" . hasuff $edit);
45 } else {
46 $m->comp("%edit", tag => $tag, edit => $edit, %props);
47 }
be24e9af
MW
48 }
49 }
50 } elsif (defined $content) {
51 ($tag, $edit) = Odin::new_pastebin %props;
52 set_handoff_cookie $tag, $edit;
7e455e62 53 $m->redirect("$Odin::PASTEBIN/$tag" . hasuff $edit);
be24e9af
MW
54 } else {
55 Odin::path_info($r) =~ m:/$:
56 or $m->redirect("$Odin::PASTEBIN/", 301);
57 $m->comp("%edit");
58 }
59</%perl>
60%#
61<%args>
62 $content => undef
63 $edit => undef
64 $lang => undef
65 $title => undef
7e455e62 66 $honc => undef
be24e9af
MW
67 $op => "edit"
68</%args>
69%#
70<%def .notfound>
71<&| SELF:error, title => "not found", status => 404 &>\
72tag &lsquo;<% $tag %>&rsquo; not found
73</&>
74<%args>
75 $tag
76</%args>
77</%def>
78%#
79<%def .badhash>
f5224f75 80<&| SELF:error, status => 403 &>\
be24e9af
MW
81incorrect edit key
82</&>
83<%args>
84 $tag
85</%args>
86</%def>
87%#
88<%once>
89 use utf8;
90 use Digest::SHA qw(sha256_hex);
91 use Odin;
92</%once>