Skip to content

Instantly share code, notes, and snippets.

@kbsriram kbsriram/kludge.head Secret
Created Apr 28, 2014

Embed
What would you like to do?
<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8" />
<title>Note from KB Sriram</title>
<meta name="author" content="KB Sriram"/>
<style>
.hideme { display: none; }
</style>
</head>
<body>
<div class="hideme">
use strict;
# Cheap kludge to create a signed note on paste.debian.net, and
# then post it as a webmention to someone.
#
# Error checking? um....
#
# Some strong assumptions about the sort of system you run, and that
# you have gpg, curl etc installed.
use LWP::Simple;
use URI;
use File::Temp qw/ tempfile /;
my $KLUDGE_ROOT = "/Users/kbs/personal/kludgemention";
my $NAME = "KB Sriram";
my $ICON = "http://kbsriram.com/kbs.png";
my $SITE = "http://kbsriram.com";
my $KEYID = "kbsriram.com";
sub runme {
my ($cmd) = @_;
system("$cmd");
if ($? == -1) {
die "failed to run $cmd : $!\n";
}
elsif ($? & 127) {
die "$cmd died with $?\n";
}
elsif ($? != 0) {
die "$cmd exited with $?\n";
}
}
my $orig = shift;
die "kludge.pl <url-of-original-site> [webmention-url]\n"
unless $orig;
my $mention = shift;
if (!defined($mention)) {
print "Fetching $orig\n";
my $content = get($orig);
# Hopelessly kludgy approximation to the answer we want.
if ($content =~ /(<link[^>]*rel\s*=\s*"[a-z\.:\/ ]*webmention[a-z\.:\/ ]*"[^>]*>)/) {
my $interesting = $1;
if ($interesting =~ /href\s*=\s*"([^"]+)"/) {
$mention = URI->new_abs($1, $orig)->as_string;
}
}
}
if (!defined($mention)) {
print "Too dumb to figure out webmention - please provide it as second arg.\n";
exit 1;
}
# print "webmention url = $mention\n";
print "Enter note, end with a single '.'\n";
my $content = "";
while (<>) {
chomp;
if ($_ eq '.') { last; }
$content = $content."\n".$_;
}
my ($tmpfh, $tmpname) = tempfile();
select ($tmpfh);
print <<DONE;
</div>
<article class="h-entry h-as-note">
<section class="reply-context">In reply to:
<a class="u-in-reply-to h-cite" href="$orig">this post</a></section>
<div class="p-name e-content">
$content
</div>
<header class="p-author h-card">
<img class="u-photo" src="${ICON}" alt="${NAME}"/>
<a class="p-name u-url" href="${SITE}">${NAME}</a></header>
</article>
<div class="hideme">
DONE
select(STDOUT);
close($tmpfh);
runme("gpg --clearsign -u ${KEYID} $tmpname");
runme("cat ${KLUDGE_ROOT}/kludge.head ${tmpname}.asc ${KLUDGE_ROOT}/kludge.tail > ${tmpname}.html");
print "\n\n: Signed content at ${tmpname}.html\n\n";
print "Now posting to paste.debian.net\n";
open(CURL, "curl -sS http://paste.debian.net --form lang=html --form expire=8640000 --form private=1 --form upload=\@${tmpname}.html|")
|| die "Could not run curl : $!\n";
my $target = '';
while (<CURL>) {
chomp;
if (/href="hidden\/([a-f0-9]+)\/"/) {
$target = $1;
}
print;
}
if ($target eq '') {
die "Woops - could not figure out debian paste url.\n";
exit 1;
}
print "\n\nNow posting webmention\n";
runme("curl -sS -d 'source=http://paste.debian.net/plainh/${target}\&target=$orig' $mention");
unlink ${tmpname}, "${tmpname}.asc", "${tmpname}.html";
print "Cleaned up tmp files, all done.\n";
</div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.