3 # Copyright (C) 2009-2013 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' \
19 -e 's/[^a-zA-Z0-9.:=_\-]/ /g' |
23 get_modeline | awk -v "key=$1" 'BEGIN{RS=" "; FS="="}; $1 ~ key {print $2}'
26 # Extract an uploaded file from standard input
27 # $1 is the name of the input to extract
29 bnd="${CONTENT_TYPE/*boundary\=/}"
30 awk -v "want=$1" -v "bnd=$bnd" '
33 # reset based on boundaries
34 $0 == "--"bnd"" { st=1; next; }
35 $0 == "--"bnd"--" { st=0; next; }
36 $0 == "--"bnd"--\r" { st=0; next; }
38 # search for wanted file
39 st == 1 && $0 ~ "^Content-Disposition:.* name=\""want"\"" { st=2; next; }
40 st == 1 && $0 == "" { st=9; next; }
42 # wait for newline, then start printing
43 st == 2 && $0 == "" { st=3; next; }
45 ' | head -c $((128*1024)) # Limit size to 128K
48 # Respond to a request
50 local allow ctype heads files texts gzip h f
54 -c) ctype="$2"; shift ;;
55 -h) heads=("${heads[@]}" "$2"); shift ;;
56 -f) files=("${files[@]}" "$2"); shift ;;
57 *) texts=("${texts[@]}" "$1"); ;;
62 # Check if the browser supports gzip
63 if [[ "$HTTP_ACCEPT_ENCODING" == *'gzip'* ]]; then
69 echo "Content-Type: $ctype; charset=UTF-8"
71 echo "Content-Type: text/plain; charset=UTF-8"
74 echo "Content-Encoding: gzip"
77 echo "Access-Control-Allow-Origin: *"
78 echo "Access-Control-Allow-Headers: Content-Type"
80 for h in "${heads[@]}"; do
85 # Output text messages
88 echo "${texts[@]}" | gzip
97 for f in "${files[@]}"; do
98 if [[ "$gzip" && "$f" != *'.gz' ]]; then
100 elif [[ ! "$gzip" && "$f" == *'.gz' ]]; then
109 # Gzip remaining stream
115 # Format and output a file
117 # Create a temp file with the provided modeline
119 sed -e "1ivim: $(get_modeline)" \
120 -e "\$avim: $(get_modeline)" "$1" > "$tmp"
122 # Determine cache name
123 md5="$(cat index.cgi vimrc "$tmp" /usr/bin/ex | md5sum -b)"
124 out="cache/${md5% *}.htm"
127 # Cache the file, if needed
128 if [ ! -f "$zip" ]; then
129 # - I have some plugins in ~/.vim
130 # - Run ex in pty to trick it into thinking that it
131 # has a real terminal, note that we also have to set
132 # term=xterm-256color in vimrc
134 /home/vpaste/bin/pty \
135 /usr/bin/ex -nXZ -i NONE -u vimrc \
136 '+sil! set fde= fdt= fex= inde= inex= key= pa= pexpr=' \
137 '+sil! set iconstring= ruf= stl= tal=' \
138 "+sil! set titlestring=$1\ -\ vpaste.net" \
145 "$tmp" >/dev/null 2>&1
151 respond -y -c "text/html" -f "$zip"
154 # List previous pastes
162 awk -v 'rows=4' -v 'cols=60' '
163 FNR==1 { gsub(/.*\//, "", FILENAME);
167 /\S/ { i++; printf "%."cols"s\n", $0 }
169 ENDFILE { i=0; print "" }
173 ls -l --time-style='+%Y %m' db |
174 awk -v 'hdr=Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec' '
175 BEGIN { printf "%64s\n", hdr }
176 NR>1 { cnt[$6+0][$7+0]++ }
177 END { for (y in cnt) {
179 for (m=1; m<=12; m++)
180 printf "%5s", cnt[y][m]
186 # Format a file for viewing
188 if [ -f "./$1" ]; then
190 elif [ -f "db/$1" ]; then
192 trim='1,/^$/d' # sed command to remove cruft
194 respond -h 'Status: 404 Not Found' \
195 "File '$1' not found"
198 # Check for javascript
199 if [[ "$input" == 'embed.js' &&
200 "$HTTP_ACCEPT" != *'html'* ]]; then
201 respond -c text/javascript -f "$input"
204 # Check for raw paste
205 if [[ "$QUERY_STRING" == 'raw'* ||
206 "$REQUEST_URI" != *'?'* &&
207 ( "$input" != 'db/'* ||
208 "$HTTP_ACCEPT" != *'html'* ) ]]; then
215 format "$input" "$trim"
218 # Format a file for viewing
226 spam=$(echo -n "$body" | cut_file "ignoreme")
227 text=$(echo -n "$body" | cut_file "(text|x)")
228 bans=$(echo -n "$REMOTE_ADDR" | grep -f blacklist)
229 [ ! -z "$spam" ] && respond "Spam check.."
230 [ ! -z "$bans" ] && respond "You have been banned"
231 [ -z "$text" ] && respond "No text pasted"
233 # Format and save message
234 output="$(mktemp db/XXXXX)"
235 cat >"$output" <<-EOF
239 User-Agent: $HTTP_USER_AGENT
245 uri="$url$(basename "$output")"
246 respond -h 'Status: 302 Found' \
247 -h "Location: $uri" \
254 ls /usr/share/vim/vim*/syntax/ /home/andy/.vim/syntax/ |
255 sed -n '/^\(syntax\|manual\|synload\|2html\|colortest\|hitest\).vim$/d; s/.vim$//p' |
258 uploads=$(ls -t db 2>/dev/null | head -n 5)
259 filetype=$(get_param '^(ft|filet(y(pe?)?)?)$')
260 vpaste='<a href="vpaste?ft=sh">vpaste</a>'
261 pileus='http://pileus.org/tools/vpaste'
262 gitweb='http://pileus.org/git/?p=vpaste'
263 mailman='http://pileus.org/mailman/listinfo/dev'
264 repo='git://pileus.org/vpaste'
268 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
269 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
270 <html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
272 <title>vpaste.net - Vim based pastebin</title>
273 <meta http-equiv="Content-type" content="text/html;charset=UTF-8" />
274 <meta name="description" content="vpaste: Vim based pastebin" />
275 <meta name="keywords" content="vpaste,paste,pastebin,vim" />
276 <meta name="google-site-verification" content="OvHF73zD7osJ1VSq9rJxnMFlja36944ud6CiP_iXQnI" />
277 <meta name="viewport" content="width=device-width, initial-scale=1.0" />
278 <style type="text/css">
281 body { font-family: sans-serif; }
282 input { padding: 2px 6px 3px 6px; }
283 a { text-decoration: none; }
285 textarea { width: 100%; }
286 .box { display: none;
287 border-top: solid 1px #888; }
289 h1 { margin-top: 1.0em;
291 ul,dd,dl,p { margin: 0 0 0 2em; }
292 dt { font-weight: bold;
293 padding: 0.5em 0 0 0; }
294 .upload { font-family: monospace; }
295 .cmds dd { font-family: monospace; }
297 <style type="text/css" media="screen and (min-device-width:800px)">
298 body { margin: 4em 8em; }
299 textarea { margin-bottom: 0.5em; }
300 .buttons { float: left; }
301 .links { float: right; }
302 .links * { margin-left: 0.5em; }
306 <style type="text/css" media="screen and (max-device-width:800px)">
307 textarea, input, select, .links
310 margin-bottom: 0.75em; }
311 body { margin: 1em; }
312 textarea { height: 17em; }
313 select { height: 2em; }
314 input { height: 3em; }
315 .links { text-align: center; }
316 .links a { margin: 0em 0.25em; }
317 #vpaste { font-size: large; }
318 .sep { display: block;
322 <script type="text/javascript">
325 var boxes = document.getElementsByClassName('box')
326 for (var i = 0; i < boxes.length; i++) {
328 if (box.id == id && box.style.display != 'block')
329 box.style.display = 'block'
331 box.style.display = "none"
334 function autoshow() {
335 var id = document.location.toString().replace(/.*#/, '')
336 var box = document.getElementById(id)
337 if (box) box.style.display = "block"
343 <body onload="autoshow()">
344 <form id="form" method="post" action="" enctype="multipart/form-data">
346 <input style="display:none" type="text" name="ignoreme" value="" />
347 <textarea name="text" cols="80" rows="25"></textarea>
349 <div class="buttons">
350 <select onchange="document.getElementById('form').action =
351 document.location + '?ft=' + this.value;">
352 <option value="" disabled="disabled">Filetype</option>
353 <option value="">None</option>
354 $(for ft in $filetypes; do
356 [ "$ft" = "$filetype" ] &&
357 echo ' selected="selected"'
361 <input type="submit" value="Paste" />
364 <a href="" id="vpaste">vpaste</a> <span class="sep">-</span>
365 <a href="#usage" onclick="show('usage' )">Usage</a>
366 <a href="#devel" onclick="show('devel' )">Development</a>
367 <a href="#uploads" onclick="show('uploads')">Uploads</a>
371 <div class="box" id="usage">
374 <dt>From a shell</dt>
375 <dd> $vpaste file [option=value,..]</dd>
376 <dd> <command> | $vpaste [option=value,..]</dd>
379 <dd> :map vp :exec "w !vpaste ft=".&ft<CR></dd>
380 <dd> :vmap vp <ESC>:exec "'<,'>w !vpaste ft=".&ft<CR></dd>
383 <dd> <command> | curl -F 'text=<-' $url[?option=value,..]</dd>
387 <p>Add <b>?option[=value],..</b> to make your text a rainbow.</p>
388 <p>Options specified when uploading are saved as defaults.</p>
391 <dt>bg, background={light|dark}</dt>
392 <dd>Background color to use for the page</dd>
393 <dt>et, expandtab</dt>
394 <dd>Expand tabs to spaces</dd>
395 <dt>fdm, foldmethod=(syntax|indent)</dt>
396 <dd>Turn on dynamic code folding</dd>
397 <dt>ft, filetype={filetype}</dt>
398 <dd>A filetype to use for highlighting, see above menu for supported types</dd>
400 <dd>Add line numbers</dd>
401 <dt>ts, tabstop=[N]</dt>
402 <dd>Number of spaces to use for tabs when <b>et</b> is set</dd>
404 <dd>See :help modeline for more information</dd>
408 <div class="box" id="devel">
410 <p>Copyright © 2009-2013
411 Andy Spencer <andy753421@gmail.com></p>
412 <p>See individual files for licenses</p>
417 <dd><a href="vpaste?ft=sh">vpaste</a>
418 <a href="embed.js?ft=javascript">embed.js</a></dd>
420 <dd><a href="index.cgi?ft=sh">index.cgi</a>
421 <a href="vimrc?ft=vim">vimrc</a>
422 <a href="htaccess?ft=apache">htaccess</a>
423 <a href="robots.txt?ft=robots">robots.txt</a>
424 <a href="sitemap.xml?ft=xml">sitemap.xml</a>
425 <a href="blacklist?raw">blacklist</a></dd>
427 <dd><a href="2html.patch?ft=diff">2html.patch</a></dd>
429 <dd><a href="$pileus">homepage</a>
430 <a href="$gitweb">gitweb</a><br />
431 <code>git clone $repo</code></dd>
436 <li>Using strange filetypes (ft=2html) may result in strange output.</li>
437 <li>Other issues can be sent to the pileus.org
438 <a href="$mailman">mailing list</a>.</li>
442 <div class="box" id="uploads">
443 <h1>Recent Uploads</h1>
444 <ul>$(for upload in ${uploads[@]}; do
446 echo -n "<span class=\"upload\">$upload</span> "
447 echo -n "<a href='$upload?raw'>text</a> "
448 echo -n "<a href='$upload'>rainbow</a>"
452 <p><a href="ls">list all</a></p>
453 <p><a href="head">sample all</a></p>
454 <p><a href="stat">statistics</a></p>
463 url="http://$HTTP_HOST${REQUEST_URI/\?*}"
464 pathinfo="${REQUEST_URI/*\/}"
465 pathinfo="${pathinfo/\?*}"
467 if [ "$pathinfo" = ls ]; then
469 elif [ "$pathinfo" = head ]; then
471 elif [ "$pathinfo" = stat ]; then
473 elif [ "$pathinfo" = view ]; then
475 elif [ "$pathinfo" ]; then
477 elif [ "$CONTENT_TYPE" ]; then