chiark / gitweb /
mason/pastebin/: Add `New' links to the `show' and `edit' pages.
[odin-cgi] / mason / pastebin / dhandler
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
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
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 {
31               $edit = $Odin::COOKIE{"odin-handoff.$tag"};
32               $m->comp("%show", tag => $tag,
33                        honc => $honc, edit => $edit // $honc, %old);
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 {
41               set_handoff_cookie $tag, $edit;
42               my $editp = Odin::update_pastebin $tag, $edit, %props;
43               if ($editp) {
44                 $m->redirect("$Odin::PASTEBIN/$tag" . hasuff $edit);
45               } else {
46                 $m->comp("%edit", tag => $tag, edit => $edit, %props);
47               }
48             }
49           }
50         } elsif (defined $content) {
51           ($tag, $edit) = Odin::new_pastebin %props;
52           set_handoff_cookie $tag, $edit;
53           $m->redirect("$Odin::PASTEBIN/$tag" . hasuff $edit);
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
66         $honc => undef
67         $op => "edit"
68 </%args>
69 %#
70 <%def .notfound>
71 <&| SELF:error, title => "not found", status => 404 &>\
72 tag &lsquo;<% $tag %>&rsquo; not found
73 </&>
74 <%args>
75         $tag
76 </%args>
77 </%def>
78 %#
79 <%def .badpaste>
80 <&| SELF:error, status => 400 &>\
81 invalid paste content
82 </&>
83 <%args>
84         $tag
85 </%args>
86 </%def>
87 %#
88 <%def .badhash>
89 <&| SELF:error, status => 403 &>\
90 incorrect edit key
91 </&>
92 <%args>
93         $tag
94 </%args>
95 </%def>
96 %#
97 <%once>
98         use utf8;
99         use Digest::SHA qw(sha256_hex);
100         use Odin;
101 </%once>