commit:3ec189dd94c711da1328ad6c96f4bce73543c31c
author:Chip Black
committer:Chip Black
date:Sat Dec 10 01:55:05 2011 -0600
parents:
Initial commit
diff --git a/README.md b/README.md
line changes: +35/-0
index 0000000..2c8e131
--- /dev/null
+++ b/README.md
@@ -0,0 +1,35 @@
+= miniblerg - a tiny hack of a perl script to post to Blërg.
+
+Miniblerg is a half hour of hacking for a simple goal: log in to
+[Blërg](http://blerg.cc/) and post something from the command-line.
+I used Perl and the excellent LWP library.  Like most half-hour hacks,
+it's not clean or pretty, but it gets the job done. :]
+
+== Installation
+
+miniblerg can be used from anywhere, but it requires Perl >5.10 and the
+JSON module.
+
+== Configuration
+
+Create a file called .miniblerg.rc in your home directory.  It only
+needs one directive, username.  It should look like this:
+
+username = foo
+
+Which, of course, is your Blërg username.
+
+== Usage
+
+ * miniblerg login
+   - interactively log in to your account
+ * miniblerg logout
+   - log out of your account
+ * miniblerg post
+   - post to Blërg either through an interactive editor or by reading
+     from stdin
+
+== Files
+
+miniblerg stores your login cookie in ~/.miniblerg.cookies.  These
+cookies stick around between invocations (but are only user readable).

diff --git a/miniblerg b/miniblerg
line changes: +82/-0
index 0000000..808a210
--- /dev/null
+++ b/miniblerg
@@ -0,0 +1,82 @@
+#!/usr/bin/perl
+use LWP::UserAgent;
+use strict;
+use v5.10;
+
+my $baseurl = 'http://blerg.cc/';
+my $rc = "$ENV{HOME}/.miniblerg.rc";
+my $cookies = "$ENV{HOME}/.miniblerg.cookies";
+
+open OPTIONS, $rc;
+my %options = map { chomp; split /\s*=\s*/ } <OPTIONS>;
+close OPTIONS;
+
+my $agent = LWP::UserAgent->new(
+	cookie_jar => {
+		file => $cookies,
+		ignore_discard => 1
+	}
+);
+
+sub blerg_call {
+	my ($name, $params) = @_;
+
+	my $res = $agent->post($baseurl . $name, $params);
+
+	return $res->decoded_content =~ /success/;
+}
+
+my $op = shift;
+
+given ($op) {
+	when ('login') {
+		print "Password: ";
+		system('stty -echo');
+		my $password = <STDIN>;
+		system('stty echo');
+		chomp $password;
+		print "\n";
+
+		blerg_call('login', {
+			username => $options{username},
+			password => $password
+		}) or die "Could not log in\n";
+		$agent->cookie_jar->save;
+		chmod 0600, $cookies;
+	}
+	when('logout') {
+		blerg_call('logout', {
+			username => $options{username}
+		});
+		unlink $cookies;
+	}
+	when ('post') {
+		my $data;
+
+		if (-t STDIN) {
+			my $editor = $ENV{EDITOR} // $ENV{VISUAL};
+			system "touch /tmp/miniblerg.post.$$";
+			system "$editor /tmp/miniblerg.post.$$";
+			open DATA, "/tmp/miniblerg.post.$$";
+			$data = join('', <DATA>);
+			close DATA;
+		} else {
+			$data .= $_ while <STDIN>;
+		}
+		chomp $data;
+
+		if (!$data) {
+			print "Aborting empty post\n";
+			exit 1;
+		}
+
+		blerg_call('put', {
+			username => $options{username},
+			data => $data
+		}) or die "Could not post\n";
+
+		END {
+			unlink "/tmp/miniblerg.post.$$";
+		}
+	}
+}