#!/bin/bash # Copyright (C) 2009 Andy Spencer # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # Remove url codings form stdin function urldecode { sed -e 's/%\([0-9A-F][0-9A-F]\)/\\\\\x\1/g; s/[,&]/ /g' | xargs echo -e } # Extract an uploaded file from standard input # $1 is the boundary delimiter for the file function cut_file { awk " /--$1/ {st=1}; st==2 {print \$0}; /$1--/ {st=0}; /^\\r$/ && st==1 {st=2}; " | head -c -2 # Remove trailing ^M's that come with CGI } # Print out a generic header function start_html { echo "Content-Type: text/html; charset=UTF-8" echo } # Print out a generic header function start_text { echo "Content-Type: text/plain; charset=UTF-8" echo } # Format a file for viewing function do_print { if [ -f "$1" ]; then input="$1" elif [ -f "db/$1" ]; then input="db/$1" else echo "Status: 404" start_text echo "File '$1' not found" return fi modeline="$(echo $QUERY_STRING | urldecode)" if [ -z "$modeline" ]; then start_text cat "$input" else # I have some plugins in ~/.vim # # Run vimhi.sh in screen to trick it into thinking # that it has a real terminal, not that we also have to # set term=xterm-256color in vimrc output="$(mktemp)" HOME=/home/andy \ screen -D -m ./vimhi.sh "$input" "$output" "$modeline" start_html cat "$output" fi } # Upload handler function do_upload { output="$(mktemp db/XXXXX)" uri="$SCRIPT_URI$(basename "$output")" cut_file "$1" > "$output" start_text echo "$uri" } # Default index page function do_help { filetypes=$( ls /usr/share/vim/vim{72,files}/syntax/ /home/andy/.vim/after/syntax/ | sed -n 's/.vim$//p' | sort | uniq ) uploads=$(ls -t db | head -n 5 | sed "s!^!$SCRIPT_URI!") start_html cat - <

NAME

vpaste: Vim enabled pastebin

SYNOPSIS

<command> | curl -F 'x=<-' $SCRIPT_URI

DESCRIPTION

Add ?option[=value] .. to make your text a rainbow.

OPTIONS

ft, filetype={filetype}
A filetype to use for highlighting, see FILETYPES
bg, background={light|dark}
Background color to use for the page
et, expandtab
Expand tabs to spaces
ts, tabstop=[N]
Number of spaces to use for tabs when et is set
...
See :help modeline for more information

FILETYPES

[+] $filetypes

SOURCE

LATEST UPLOADS

EOF } # Main pathinfo="${SCRIPT_URL/*vpaste\/}" boundary="${CONTENT_TYPE/*boundary\=/}" if [ "$pathinfo" ]; then do_print "$pathinfo" elif [ "$CONTENT_TYPE" ]; then do_upload "$boundary" else do_help fi