guestbook-ng/guestbook-ng.pl

198 lines
5.6 KiB
Perl
Raw Permalink Normal View History

2021-12-04 00:11:37 -05:00
#!/usr/bin/env perl
# Dec 2021
# Daniel Bowling <swaggboi@slackware.uk>
use Mojolicious::Lite -signatures;
2021-12-04 18:34:35 -05:00
use Mojo::Pg;
2021-12-31 22:32:39 -05:00
use List::Util qw{shuffle};
2022-01-02 02:01:45 -05:00
use Regexp::Common qw{URI};
2022-01-11 19:51:45 -05:00
use Number::Format qw{format_number};
use WebService::Discord::Webhook;
2021-12-31 22:32:39 -05:00
# Load the model
2021-12-04 02:26:25 -05:00
use lib 'lib';
use GuestbookNg::Model::Message;
2022-02-09 21:17:49 -05:00
use GuestbookNg::Model::Counter;
2021-12-04 02:26:25 -05:00
2021-12-04 18:34:35 -05:00
# Plugins
plugin 'Config';
2021-12-11 21:02:54 -05:00
plugin 'TagHelpers::Pagination';
2024-07-04 01:17:58 -04:00
plugin AssetPack => {pipes => [qw{Css Combine}]};
2021-12-04 18:34:35 -05:00
# Helpers
helper pg => sub {
my $env = app->mode() eq 'development' ? 'dev_env' : 'prod_env';
state $pg = Mojo::Pg->new(app->config->{$env}{'pg_string'});
2021-12-04 18:34:35 -05:00
};
2021-12-04 23:06:05 -05:00
helper message => sub {
state $message = GuestbookNg::Model::Message->new(pg => shift->pg)
2021-12-04 23:06:05 -05:00
};
2022-02-09 21:17:49 -05:00
helper counter => sub {
state $counter = GuestbookNg::Model::Counter->new(pg => shift->pg)
};
# Routes
2021-12-18 23:03:53 -05:00
under sub ($c) {
# Opt out of Google FLoC
# https://paramdeo.com/blog/opting-your-website-out-of-googles-floc-network
$c->res->headers->header('Permissions-Policy', 'interest-cohort=()');
unless ($c->session('counted')) {
$c->counter->increment_visitor_count();
$c->session(
2022-04-16 18:55:22 -04:00
expires => time() + 1800,
counted => 1
);
}
2022-01-01 23:37:41 -05:00
2022-01-08 22:01:51 -05:00
$c->stash(status => 403) if $c->flash('error');
2022-01-08 21:21:22 -05:00
2022-04-03 21:04:09 -04:00
$c->stash(
post_count => format_number($c->message->get_post_count),
2022-04-08 13:10:52 -04:00
this_url => $c->req->url->to_abs(),
2022-04-03 21:04:09 -04:00
visitor_count => format_number($c->counter->get_visitor_count)
);
2022-01-11 20:15:53 -05:00
2021-12-18 23:03:53 -05:00
1;
};
2022-04-09 00:52:16 -04:00
get '/' => sub ($c) { $c->redirect_to(page => {page => 'view'}) };
2022-01-12 15:18:34 -05:00
2021-12-19 00:43:18 -05:00
any [qw{GET POST}], '/sign' => sub ($c) {
my $v;
$v = $c->validation() if $c->req->method eq 'POST';
2022-01-08 21:21:22 -05:00
2022-04-09 15:35:24 -04:00
if ($v && $v->has_data) {
2021-12-31 23:05:34 -05:00
my $name = $c->param('name') || 'Anonymous';
my $url = $c->param('url');
my $message = $c->param('message');
2022-01-02 17:53:43 -05:00
my $spam =
!$c->param('answer') ? 1 :
$message =~ /$RE{URI}{HTTP}{-scheme => qr<https?>}/ ? 1 :
0;
2022-01-02 02:01:45 -05:00
2022-01-08 21:21:22 -05:00
$v->required('name' )->size(1, 63);
$v->required('message')->size(2, 2000);
$v->optional('url', 'not_empty')->size(1, 255)
->like(qr/$RE{URI}{HTTP}{-scheme => qr<https?>}/);
2022-04-09 15:35:24 -04:00
if ($v->has_error) {
$c->stash(status => 400)
}
else {
2022-07-22 11:06:29 -04:00
$c->message->create_post($name, $message, $url, $spam);
if ($spam) {
$c->flash(error => 'This message was flagged as spam')
}
# Send this notification if there's a Webhook URL
2022-07-22 00:56:57 -04:00
elsif (app->mode() eq 'production' && -s '.tom.url') {
2022-07-22 11:06:29 -04:00
my ($new_message_id, $url_file, $url, $webhook);
2022-07-22 13:10:18 -04:00
eval {
$new_message_id = $c->message->get_last_message_id();
open($url_file, '.tom.url');
chomp($url = <$url_file>);
$webhook = WebService::Discord::Webhook->new(
url => $url,
verify_SSL => 1
);
$webhook->execute(
'content',
"https://guestbook.swagg.net/message/$new_message_id"
);
} or do {
$@ //= 'unknown error';
say "WEBHOOK URL $url FAILED: $@";
}
}
2022-04-09 00:52:16 -04:00
return $c->redirect_to(page => {page => 'view'});
2022-01-01 23:37:41 -05:00
}
}
2022-04-09 15:35:24 -04:00
# Throw a 400 for POST with null body too
elsif ($v) {
$c->stash(status => 400)
}
2022-01-08 21:21:22 -05:00
# Try to randomize things for the CAPTCHA challenge. The
# string 'false' actually evaluates to true so this is an
# attempt to confuse a (hypothetical) bot that would try to
# select what it thinks is the right answer
my @answers = shuffle(0, 'false', undef);
2022-04-03 17:11:02 -04:00
my $right_answer_label = "I'm ready to sign (choose this one)";
2022-01-08 21:21:22 -05:00
my @wrong_answer_labels = shuffle(
2022-04-03 17:11:02 -04:00
"I don't want to sign (wrong answer)",
"This is spam/I'm a bot, do not sign"
2022-01-08 21:21:22 -05:00
);
$c->stash(
answers => \@answers,
right_answer_label => $right_answer_label,
wrong_answer_labels => \@wrong_answer_labels
);
$c->render();
};
group {
under '/message';
2022-04-04 21:21:11 -04:00
get '/:message_id', [message_id => qr/[0-9]+/] => sub ($c) {
my $message_id = $c->param('message_id');
my @view_post = $c->message->get_post_by_id($message_id);
2022-04-04 21:21:11 -04:00
$c->stash(status => 404) unless $view_post[0];
$c->stash(view_post => @view_post);
$c->render();
};
};
group {
2022-04-07 23:04:09 -04:00
under '/:page', [page => ['spam', 'view']];
get '/:number', [number => qr/[0-9]+/], {number => 1} => sub ($c) {
my $base_path = $c->url_for(number => undef);
2022-04-07 23:04:09 -04:00
my $this_page = $c->param('number');
2022-04-08 12:59:15 -04:00
my $last_page = $base_path eq '/view'
? $c->message->get_last_page()
: $c->message->get_last_page('spam');
2022-04-08 12:59:15 -04:00
my $view_posts = $base_path eq '/view'
? $c->message->get_posts($this_page)
: $c->message->get_spam($this_page);
$c->stash(status => 404) unless @$view_posts[0];
$c->stash(
view_posts => $view_posts,
this_page => $this_page,
last_page => $last_page,
base_path => $base_path
);
$c->render();
};
};
2021-12-04 18:34:35 -05:00
# Send it
2021-12-05 03:32:22 -05:00
app->secrets(app->config->{'secrets'}) || die $@;
2021-12-12 01:50:07 -05:00
app->message->max_posts(app->config->{'max_posts'})
if app->config->{'max_posts'};
2022-04-03 21:04:09 -04:00
app->pg->migrations->from_dir('migrations')->migrate(8);
2021-12-12 01:50:07 -05:00
2021-12-22 23:18:35 -05:00
app->asset->process('swagg.css', 'css/swagg.css');
2021-12-04 00:11:37 -05:00
app->start();