From e483280925267b2294f85e1d824759d1f51b641c Mon Sep 17 00:00:00 2001 From: Gwyn Date: Sun, 18 Jan 2026 21:46:04 -0500 Subject: [PATCH] Start! --- zerdapub.psgi | 54 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 zerdapub.psgi diff --git a/zerdapub.psgi b/zerdapub.psgi new file mode 100644 index 0000000..b498022 --- /dev/null +++ b/zerdapub.psgi @@ -0,0 +1,54 @@ +#!/usr/bin/perl +use strict; +use warnings; +use Plack::Builder; +use Plack::Request; +use Plack::Middleware::ReverseProxy; +use Plack::App::File; +use File::Path qw(make_path); +use Fcntl qw(:flock); +use POSIX qw(strftime); +use warnings; +use WWW::Telegram::BotAPI; + +my $bot = WWW::Telegram::BotAPI->new(token => 'bot-token'); + + +sub save_customer { + my ($cinput, $cip) = @_; + return unless defined $cinput && length $cinput; + make_path('customers'); + open my $fh, '>>', 'customers/customers.txt' or die $!; + flock $fh, LOCK_EX; + my $ts = strftime('%Y-%m-%d %H:%M:%S', localtime); + print $fh "$cip :: $ts\t$cinput\n\n"; + close $fh; + $bot->sendMessage({chat_id => 'teleuser', text => "At $ts\n$cip\n$cinput",}); +} +my $root = './public'; +my $static_app = Plack::App::File->new(root => $root)->to_app; +my $index_app = Plack::App::File->new(file => "$root/index.html")->to_app; +my $thank_you_app = Plack::App::File->new(file => "$root/thank-you.html")->to_app; +builder { + enable "Plack::Middleware::ReverseProxy"; + sub { + my $env = shift; + my $req = Plack::Request->new($env); + + # Handle form POST + if ($req->method eq 'POST' && $req->path eq '/submit') { + my $cinput = $req->parameters->{cinput}; + my $cip = $env->{REMOTE_ADDR}; + save_customer($cinput, $cip); + return [ + 303, + [ Location => '/thank-you.html' ], + [] + ]; + } + my $res = $static_app->($env); + return $res if $res->[0] != 404; + return $thank_you_app->($env) if $req->path eq '/thank-you.html'; + return $index_app->($env); + }; +};