Initial commit for shombler

This commit is contained in:
bluesaxman 2021-05-05 14:35:36 -06:00
commit 3ec4225c36
5 changed files with 131 additions and 0 deletions

29
README.md Normal file
View File

@ -0,0 +1,29 @@
### SHoMBler (Self Hosted MicroBlogger)
## What is Shombler?
Shombler is a light weight, hyper minimalistic, cli based microbloging platform.
## Why?
Why not? Its geared to those who both want to control their own data, avoid censorship, and spend a lot of time in a command line environment. Why load up a browser, and use some privately controled platform that sells your data, when you can just enter a simple command, and not be controled by some big tech nightmare.
## Whatabout the social aspect?
What about it? The web should never have been socialized in this developers oppinion. There is and will never be a commenting functionality built into Shombler. If you really feel like you must type at eachother and have your little flame wars, just link to the other persons post in your post, or fork this or something if you really must.
## Ok well, hows it work?
Shombler is composed of 3 parts: Input, Storage, Presentation
* Input: fairly self explainitory, a script that takes your input and stores it in...
* Storage: a sqlite database designed to basically just hold entries and their dates (no character limit like some platforms btw, go hog wild... within reason.
* Presentation: A backend script, designed to be accessable by a webserver, that provides a read only API spitting out entries according to the request given to it. This will be given as JSON, which should make it very portable, it should easily be dropable into any old(or new) website ether using the included code snipet, or you can roll your own, the documentation should be very easy to understand and writing your own access should be very stright forward as long as you know some sort of front end language.
## Sounds complicated... Got a tutorial?
I mean, not really but I can write one eventually.
## Great! Lets get started, wait.. your git project is empty, wtf???
Sorry, prewriting this readme, not that anyone will see this, as I will have removed it by the time anyone is aware of the projects exsistance. But you know, for the git history, in case someone is weird and decides to read it.

32
shomble.pl Executable file
View File

@ -0,0 +1,32 @@
#!/usr/bin/perl -w
use strict;
use warnings;
use DBI;
my $username = ""; # Username to include in posts
my $dblocation = "./shomble/posts.db"; #needs to point to the relative or absolute
# system path of the posts.db file.
my $post = "";
my $db = DBI->connect("DBI:SQLite:dbname=$dblocation", "", "", { RaiseError => 1}) or die $DBI::errstr;
TRYAGAIN:
print "Ok, what would you like to submit?\n>";
$post = <STDIN>;
chomp $post;
print "Confirming this is your post:\n".$post."\n Is this correct? (you must type y or yes to submit)\n>";
if ( <STDIN> =~ /y(es)?/i ) {
#this needs to be called right before the post is submitted to the database.
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime();
my $timeStamp = $mday."/".$mon."/".($year + 1900)." ".$hour.":".$min.":".$sec;
my $statmentHandle = $db->prepare('INSERT INTO posts VALUES( ?1, ?2, ?3)') or die $DBI::errstr;
$statmentHandle->execute( $timeStamp, $username, $post ) or die $DBI::errstr;
print "Post submitted, thank you for using Shombler!";
} else {
goto TRYAGAIN;
}
$db->disconnect();

BIN
shomble/empty_posts.db Normal file

Binary file not shown.

70
shomble/get.pl Executable file
View File

@ -0,0 +1,70 @@
#!/usr/bin/perl -w
use strict;
use warnings;
use DBI;
use DBD::SQLite::Constants qw/:file_open/;
use JSON;
use Data::Dumper;
my $dbpath = "./posts.db";
my $json = JSON->new;
sub get_info {
my $clientIP = ($ENV{"REMOTE_ADDR"} or "0.0.0.0");
my $proto = ($ENV{"REQUEST_SCHEME"} or "http");
my $query = (split(/\?/,($ENV{"QUERY_STRING"} or "")))[0];
my $referrer = ($ENV{"HTTP_REFERER"} or "");
return ($proto,$clientIP,$query,$referrer);
}
sub http_status {
my ($status,$content,$target) = @_;
my $header = "";
$header .= "status: ".$status."\r\n";
$header .= "Location: ".$target."\r\n" if $target;
$header .= "Content-Type: ".$content."\r\n" if $content;
$header .= "\r\n";
return $header;
}
sub soft_die {
print http_status(500,"text/json");
print '{"error":"'.shift.'"}';
exit;
}
sub clean_input {
my $input = shift;
unless ($input) { return ""; }
if ($input =~ m!%2F!) { print "Location: /hax\r\n\r\n"; exit; }
$input =~ s!%(..)!chr hex $1!ge;
$input =~ s!\+! !g;
return $input;
}
my $db = DBI->connect("DBI:SQLite:dbname=$dbpath", "", "", { RaiseError => 1, sqlite_open_flags => SQLITE_OPEN_READONLY }) or soft_die($DBI::errstr);
my $sth;
my @request = get_info();
my @get_params = split("&",clean_input($request[2]));
my $directive = shift(@get_params);
if ( "rand" eq $directive ) {
# select a random post
$sth = $db->prepare('SELECT * FROM posts ORDER BY RANDOM() LIMIT 1');
} elsif ( "last" eq $directive ) {
# select the last post only
$sth = $db->prepare('SELECT * FROM posts ORDER BY DATE DESC LIMIT 1');
} elsif ( "range" eq $directive ) {
# select range starting with lastest as 1
my $low = $get_params[0];
my $high = $get_params[1];
$sth = $db->prepare('SELECT * FROM posts ORDER BY DATE DESC LIMIT '.$low.', '.$high);
} else {
# select the last 10 posts
$sth = $db->prepare('SELECT * FROM posts ORDER BY DATE DESC LIMIT 10');
}
$sth->execute or soft_die($sth->errstr);
print http_status(200,"text/json");
print $json->encode( $sth->fetchall_arrayref({}));

BIN
shomble/posts.db Normal file

Binary file not shown.