%# -*-html-*-
%#
%# Pastebin creation webservice for Odin
%#
%# (c) 2015 Mark Wooding
%#
%#----- Licensing notice ----------------------------------------------------
%#
%# This file is part of the `odin.gg' service, `odin-cgi'.
%#
%# `odin-cgi' is free software; you can redistribute it and/or modify
%# it under the terms of the GNU Affero General Public License as
%# published by the Free Software Foundation; either version 3 of the
%# License, or (at your option) any later version.
%#
%# `odin-cgi' is distributed in the hope that it will be useful,
%# but WITHOUT ANY WARRANTY; without even the implied warranty of
%# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
%# GNU Affero General Public License for more details.
%#
%# You should have received a copy of the GNU Affero General Public
%# License along with `odin-cgi'; if not, see
%# .
%#
<%perl>
my $tag = $m->dhandler_arg;
sub set_handoff_cookie ($$%) {
my ($tag, $edit, %attr) = @_;
Odin::bake_cookie $r, "odin-handoff.$tag=$edit",
-path => "$Odin::PASTEBIN_PATH/", %attr;
}
sub hasuff ($) {
my ($edit) = @_;
return $Odin::COOKIE{"odin-cookie-probe"} ? "" : "?honc=$edit";
}
Odin::bake_cookie $r, "odin-cookie-probe=t";
my %props = (
lang => $lang, title => $title,
content => Odin::tidy_pastebin_content $content
);
if (length $tag) {
if (!defined $edit) {
my $db = Odin::open_db;
Odin::get_pastebin $db, $tag, my %old;
if ($op eq "raw") {
$r->content_type("text/plain; charset=utf8");
$m->print($old{content});
} else {
$edit = $Odin::COOKIE{"odin-handoff.$tag"};
$m->comp("%show", tag => $tag,
honc => $honc, edit => $edit // $honc, %old);
}
} else {
if ($op eq "del") {
Odin::delete_pastebin $tag, $edit;
set_handoff_cookie $tag, "nil", -max_age => 5;
$m->redirect("$Odin::PASTEBIN/");
} else {
set_handoff_cookie $tag, $edit;
my $editp = Odin::update_pastebin $tag, $edit, %props;
if ($editp) {
$m->redirect("$Odin::PASTEBIN/$tag" . hasuff $edit);
} else {
$m->comp("%edit", tag => $tag, edit => $edit, %props);
}
}
}
} elsif (defined $content) {
($tag, $edit) = Odin::new_pastebin %props;
set_handoff_cookie $tag, $edit;
$m->redirect("$Odin::PASTEBIN/$tag" . hasuff $edit);
} else {
Odin::path_info($r) =~ m:/$:
or $m->redirect("$Odin::PASTEBIN/", 301);
$m->comp("%edit");
}
%perl>
%#
<%args>
$content => undef
$edit => undef
$lang => undef
$title => undef
$honc => undef
$op => "edit"
%args>
%#
<%def .notfound>
<&| SELF:error, title => "not found", status => 404 &>\
tag ‘<% $tag %>’ not found
&>
<%args>
$tag
%args>
%def>
%#
<%def .badpaste>
<&| SELF:error, status => 400 &>\
invalid paste content
&>
<%args>
$tag
%args>
%def>
%#
<%def .badhash>
<&| SELF:error, status => 403 &>\
incorrect edit key
&>
<%args>
$tag
%args>
%def>
%#
<%once>
use utf8;
use Digest::SHA qw(sha256_hex);
use Odin;
%once>