chiark / gitweb /
mason/pastebin/%show: Don't flush FS buffers in `filter' child.
[odin-cgi] / mason / pastebin / dhandler
1 %# -*-html-*-
2 %#
3 %# Pastebin creation webservice for Odin
4 %#
5 %# (c) 2015 Mark Wooding
6 %#
7 %#----- Licensing notice ----------------------------------------------------
8 %#
9 %# This file is part of the `odin.gg' service, `odin-cgi'.
10 %#
11 %# `odin-cgi' is free software; you can redistribute it and/or modify
12 %# it under the terms of the GNU Affero General Public License as
13 %# published by the Free Software Foundation; either version 3 of the
14 %# License, or (at your option) any later version.
15 %#
16 %# `odin-cgi' is distributed in the hope that it will be useful,
17 %# but WITHOUT ANY WARRANTY; without even the implied warranty of
18 %# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 %# GNU Affero General Public License for more details.
20 %#
21 %# You should have received a copy of the GNU Affero General Public
22 %# License along with `odin-cgi'; if not, see
23 %# <http://www.gnu.org/licenses/>.
24 %#
25 <%perl>
26         my $tag = $m->dhandler_arg;
27
28         sub set_handoff_cookie ($$%) {
29           my ($tag, $edit, %attr) = @_;
30           Odin::bake_cookie $r, "odin-handoff.$tag=$edit",
31             -path => "$Odin::PASTEBIN_PATH/", %attr;
32         }
33
34         sub hasuff ($) {
35           my ($edit) = @_;
36           return $Odin::COOKIE{"odin-cookie-probe"} ? "" : "?honc=$edit";
37         }
38
39         Odin::bake_cookie $r, "odin-cookie-probe=t";
40
41         my %props = (
42           lang => $lang, title => $title,
43           content => Odin::tidy_pastebin_content $content
44         );
45
46         if (length $tag) {
47
48           if (!defined $edit) {
49             my $db = Odin::open_db;
50             Odin::get_pastebin $db, $tag, my %old;
51             if ($op eq "raw") {
52               $r->content_type("text/plain; charset=utf8");
53               $m->print($old{content});
54             } else {
55               $edit = $Odin::COOKIE{"odin-handoff.$tag"};
56               $m->comp("%show", tag => $tag,
57                        honc => $honc, edit => $edit // $honc, %old);
58             }
59           } else {
60             if ($op eq "del") {
61               Odin::delete_pastebin $tag, $edit;
62               set_handoff_cookie $tag, "nil", -max_age => 5;
63               $m->redirect("$Odin::PASTEBIN/");
64             } else {
65               set_handoff_cookie $tag, $edit;
66               my $editp = Odin::update_pastebin $tag, $edit, %props;
67               if ($editp) {
68                 $m->redirect("$Odin::PASTEBIN/$tag" . hasuff $edit);
69               } else {
70                 $m->comp("%edit", tag => $tag, edit => $edit, %props);
71               }
72             }
73           }
74         } elsif (defined $content) {
75           ($tag, $edit) = Odin::new_pastebin %props;
76           set_handoff_cookie $tag, $edit;
77           $m->redirect("$Odin::PASTEBIN/$tag" . hasuff $edit);
78         } else {
79           Odin::path_info($r) =~ m:/$:
80             or $m->redirect("$Odin::PASTEBIN/", 301);
81           $m->comp("%edit");
82         }
83 </%perl>
84 %#
85 <%args>
86         $content => undef
87         $edit => undef
88         $lang => undef
89         $title => undef
90         $honc => undef
91         $op => "edit"
92 </%args>
93 %#
94 <%def .notfound>
95 <&| SELF:error, title => "not found", status => 404 &>\
96 tag &lsquo;<% $tag %>&rsquo; not found
97 </&>
98 <%args>
99         $tag
100 </%args>
101 </%def>
102 %#
103 <%def .badpaste>
104 <&| SELF:error, status => 400 &>\
105 invalid paste content
106 </&>
107 <%args>
108         $tag
109 </%args>
110 </%def>
111 %#
112 <%def .badhash>
113 <&| SELF:error, status => 403 &>\
114 incorrect edit key
115 </&>
116 <%args>
117         $tag
118 </%args>
119 </%def>
120 %#
121 <%once>
122         use utf8;
123         use Digest::SHA qw(sha256_hex);
124         use Odin;
125 </%once>