3 # Copyright (C) 2009-2012 Andy Spencer
5 # This program is free software: you can redistribute it and/or modify it under
6 # the terms of the GNU Affero General Public License as published by the Free
7 # Software Foundation, either version 3 of the License, or (at your option) any
10 # This program is distributed in the hope that it will be useful, but WITHOUT
11 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 # FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
15 # Remove url codings from stdin
16 function get_modeline {
17 echo "$QUERY_STRING" |
18 sed -e 's/%\([0-9A-F][0-9A-F]\)/\\\\\x\1/g; s/[,&?]/ /g' |
22 get_modeline | awk -v "key=$1" 'BEGIN{RS=" "; FS="="}; $1 ~ key {print $2}'
25 # Extract an uploaded file from standard input
26 # $1 is the name of the input to extract
28 bnd="${CONTENT_TYPE/*boundary\=/}"
29 awk -v "want=$1" -v "bnd=$bnd" '
32 # reset based on boundaries
33 $0 == "--"bnd"" { st=1; next; }
34 $0 == "--"bnd"--" { st=0; next; }
35 $0 == "--"bnd"--\r" { st=0; next; }
37 # search for wanted file
38 st == 1 && $0 ~ "^Content-Disposition:.* name=\""want"\"" { st=2; next; }
39 st == 1 && $0 == "" { st=9; next; }
41 # wait for newline, then start printing
42 st == 2 && $0 == "" { st=3; next; }
44 ' | head -c $((128*1024)) # Limit size to 128K
47 # Print out a generic header
49 echo "Content-Type: $1; charset=UTF-8"
50 if [[ "$HTTP_ACCEPT_ENCODING" == *'gzip'* ]]; then
51 echo "Content-Encoding: gzip"
59 # Print plain message and exit
61 while [ "$1" == '-h' ]; do
62 shift; echo "$1"; shift
69 # List previous pastes
75 BEGIN { rows=4; cols=60; }
76 FNR==1 { gsub(/.*\//, "", FILENAME);
80 /\S/ { i++; printf "%."cols"s\n", $0 }
81 i>=rows { i=0; print ""; nextfile }
90 # Format a file for viewing
92 if [ -f "./$1" ]; then
94 elif [ -f "db/$1" ]; then
96 trim='1,/^$/d' # sed command to remove cruft
98 message -h 'Status: 404 Not Found' \
102 # Check for raw paste
103 if [[ "$QUERY_STRING" == 'raw'* ||
104 "$REQUEST_URI" != *'?'* &&
105 ( "$input" != 'db/'* ||
106 "$HTTP_ACCEPT" != *'html'* ) ]]; then
112 # Create a temp file with the provided modeline
115 sed "\$avim: $(get_modeline)" "$input" > "$tmp"
117 # - I have some plugins in ~/.vim
118 # - Run ex in screen to trick it into thinking that it
119 # has a real terminal, note that we also have to set
120 # term=xterm-256color in vimrc
122 screen -D -m ex -nXZ -i NONE -u vimrc \
123 '+sil! set fde= fdt= fex= inde= inex= key= pa= pexpr=' \
124 '+sil! set iconstring= ruf= stl= tal=' \
125 "+sil! set titlestring=$1\ -\ vpaste.net" \
143 spam=$(echo -n "$body" | cut_file "ignoreme")
144 text=$(echo -n "$body" | cut_file "(text|x)")
145 [ ! -z "$spam" ] && message "Spam check.."
146 [ -z "$text" ] && message "No text pasted"
148 # Format and save message
149 output="$(mktemp db/XXXXX)"
150 cat >"$output" <<-EOF
159 uri="$url$(basename "$output")"
160 message -h 'Status: 302 Found' \
161 -h "Location: $uri" \
168 ls /usr/share/vim/vim*/syntax/ /home/andy/.vim/syntax/ |
169 sed -n '/^\(syntax\|manual\|synload\|2html\|colortest\|hitest\).vim$/d; s/.vim$//p' |
172 uploads=$(ls -t db | head -n 5)
173 filetype=$(get_param '^(ft|filet(y(pe?)?)?)$')
174 vpaste='<a href="vpaste?ft=sh">vpaste</a>'
178 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
179 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
180 <html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
182 <title>vpaste.net - Vim based pastebin</title>
183 <meta http-equiv="Content-type" content="text/html;charset=UTF-8" />
184 <meta name="description" content="vpaste: Vim based pastebin" />
185 <meta name="keywords" content="vpaste,paste,pastebin,vim" />
186 <meta name="google-site-verification" content="OvHF73zD7osJ1VSq9rJxnMFlja36944ud6CiP_iXQnI" />
187 <style type="text/css">
190 body { margin: 4em 8em 4em 8em;
191 font-family: sans-serif; }
193 textarea { width: 100%;
194 margin-bottom: 0.5em; }
195 .buttons { float: left; }
196 .links { float: right; }
197 .links * { text-decoration: none;
198 margin-left: 0.5em; }
199 .box { display: none;
202 border-top: solid 1px #888; }
204 h1 { margin-top: 1.0em;
206 ul,dd,dl,p { margin: 0 0 0 2em; }
207 dt { font-weight: bold;
208 padding: 0.5em 0 0 0; }
209 span { font-family: monospace; }
210 .cmds dd { font-family: monospace; }
212 <script type="text/javascript">
215 var boxes = document.getElementsByClassName('box')
216 for (var i = 0; i < boxes.length; i++) {
218 if (box.id == id && box.style.display != 'block')
219 box.style.display = 'block'
221 box.style.display = "none"
224 function autoshow() {
225 var id = document.location.toString().replace(/.*#/, '')
226 var box = document.getElementById(id)
227 if (box) box.style.display = "block"
233 <body onload="autoshow()">
234 <form id="form" method="post" action="" enctype="multipart/form-data">
236 <input style="display:none" type="text" name="ignoreme" value="" />
237 <textarea name="text" cols="80" rows="25"></textarea>
239 <div class="buttons">
240 <select onchange="document.getElementById('form').action =
241 document.location + '?ft=' + this.value;">
242 <option value="" disabled="disabled">Filetype</option>
243 <option value="">None</option>
244 $(for ft in $filetypes; do
246 [ "$ft" = "$filetype" ] &&
247 echo ' selected="selected"'
251 <input type="submit" value="Paste" />
254 <a href="">vpaste</a> <span>-</span>
255 <a href="#usage" onclick="show('usage' )">Usage</a>
256 <a href="#devel" onclick="show('devel' )">Development</a>
257 <a href="#uploads" onclick="show('uploads')">Uploads</a>
261 <div class="box" id="usage">
264 <dt>From a shell</dt>
265 <dd> $vpaste file [option=value,..]</dd>
266 <dd> <command> | $vpaste [option=value,..]</dd>
269 <dd> :map vp :exec "w !vpaste ft=".&ft<CR></dd>
270 <dd> :vmap vp <ESC>:exec "'<,'>w !vpaste ft=".&ft<CR></dd>
273 <dd> <command> | curl -F 'text=<-' $url[?option=value,..]</dd>
277 <p>Add <b>?option[=value],..</b> to make your text a rainbow.</p>
278 <p>Options specified when uploading are saved as defaults.</p>
281 <dt>bg, background={light|dark}</dt>
282 <dd>Background color to use for the page</dd>
283 <dt>et, expandtab</dt>
284 <dd>Expand tabs to spaces</dd>
285 <dt>fdm, foldmethod=(syntax|indent)</dt>
286 <dd>Turn on dynamic code folding</dd>
287 <dt>ft, filetype={filetype}</dt>
288 <dd>A filetype to use for highlighting, see above menu for supported types</dd>
290 <dd>Add line numbers</dd>
291 <dt>ts, tabstop=[N]</dt>
292 <dd>Number of spaces to use for tabs when <b>et</b> is set</dd>
294 <dd>See :help modeline for more information</dd>
298 <div class="box" id="devel">
300 <p>Copyright © 2009-2012
301 Andy Spencer <andy753421@gmail.com></p>
302 <p>See individual files for licenses</p>
306 <li><a href="vpaste?ft=sh">vpaste</a></li>
307 <li><a href="index.cgi?ft=sh">index.cgi</a>
308 <a href="vimrc?ft=vim">vimrc</a>
309 <a href="htaccess?ft=apache">htaccess</a>
310 <a href="robots.txt?ft=robots">robots.txt</a>
311 <a href="sitemap.xml?ft=xml">sitemap.xml</a></li>
312 <li><a href="2html.patch?ft=diff">2html.patch</a></li>
313 <li><a href="https://lug.rose-hulman.edu/svn/misc/trunk/htdocs/vpaste/">Subversion</a></li>
318 <li>Using strange filetypes (ft=2html) may result in strange output.</li>
319 <li><a href="mailto:andy753421@gmail.com?subject=vpaste bug">Other?</a></li>
323 <div class="box" id="uploads">
324 <h1>Recent Uploads</h1>
325 <ul>$(for upload in ${uploads[@]}; do
327 echo -n "<span>$upload</span> "
328 echo -n "<a href='$upload?raw'>text</a> "
329 echo -n "<a href='$upload'>rainbow</a>"
333 <p><a href="ls">list all</a></p>
334 <p><a href="head">sample all</a></p>
343 url="http://$HTTP_HOST${REQUEST_URI/\?*}"
344 pathinfo="${REQUEST_URI/*\/}"
345 pathinfo="${pathinfo/\?*}"
347 if [ "$pathinfo" = ls ]; then
349 elif [ "$pathinfo" = head ]; then
351 elif [ "$pathinfo" ]; then
353 elif [ "$CONTENT_TYPE" ]; then