]> Pileus Git - vpaste/blob - index.cgi
Cleanup modelines
[vpaste] / index.cgi
1 #!/bin/bash
2
3 # Copyright (C) 2009-2013 Andy Spencer
4 #
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
8 # later version.
9 #
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
13 # details.
14
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' |
20         xargs echo -e
21 }
22 function get_param {
23         get_modeline | awk -v "key=$1" 'BEGIN{RS=" "; FS="="}; $1 ~ key {print $2}'
24 }
25
26 # Extract an uploaded file from standard input
27 #   $1 is the name of the input to extract
28 function cut_file {
29         bnd="${CONTENT_TYPE/*boundary\=/}"
30         awk -v "want=$1" -v "bnd=$bnd" '
31                 BEGIN { RS="\r\n" }
32
33                 # reset based on boundaries
34                 $0 == "--"bnd""     { st=1; next; }
35                 $0 == "--"bnd"--"   { st=0; next; }
36                 $0 == "--"bnd"--\r" { st=0; next; }
37
38                 # search for wanted file
39                 st == 1 && $0 ~  "^Content-Disposition:.* name=\""want"\"" { st=2; next; }
40                 st == 1 && $0 == "" { st=9; next; }
41
42                 # wait for newline, then start printing
43                 st == 2 && $0 == "" { st=3; next; }
44                 st == 3             { print $0    }
45         ' | head -c $((128*1024)) # Limit size to 128K
46 }
47
48 # Respond to a request
49 function respond {
50         local allow ctype heads files texts gzip h f
51         while [ "$1" ]; do
52                 case $1 in
53                         -y) allow="true"; ;;
54                         -c) ctype="$2"; shift ;;
55                         -h) heads=("${heads[@]}" "$2"); shift ;;
56                         -f) files=("${files[@]}" "$2"); shift ;;
57                         *)  texts=("${texts[@]}" "$1"); ;;
58                 esac
59                 shift
60         done
61
62         # Check if the browser supports gzip
63         if [[ "$HTTP_ACCEPT_ENCODING" == *'gzip'* ]]; then
64                 gzip=true
65         fi
66
67         # Output header
68         if [ "$ctype" ]; then
69                 echo "Content-Type: $ctype; charset=UTF-8"
70         else
71                 echo "Content-Type: text/plain; charset=UTF-8"
72         fi
73         if [ "$gzip" ]; then
74                 echo "Content-Encoding: gzip"
75         fi
76         if [ "$allow" ]; then
77                 echo "Access-Control-Allow-Origin: *"
78                 echo "Access-Control-Allow-Headers: Content-Type"
79         fi
80         for h in "${heads[@]}"; do
81                 echo "$h"
82         done
83         echo
84
85         # Output text messages
86         if [ "$texts" ]; then
87                 if [ "$gzip" ]; then
88                         echo "${texts[@]}" | gzip
89                 else
90                         echo "${texts[@]}"
91                 fi
92                 exit
93         fi
94
95         # Output body files
96         if [ "$files" ]; then
97                 for f in "${files[@]}"; do
98                         if   [[   "$gzip" && "$f" != *'.gz' ]]; then
99                                 gzip < "$f"
100                         elif [[ ! "$gzip" && "$f" == *'.gz' ]]; then
101                                 zcat < "$f"
102                         else
103                                 cat "$f"
104                         fi
105                 done
106                 exit
107         fi
108
109         # Gzip remaining stream
110         if [ "$gzip" ]; then
111                 exec 1> >(gzip)
112         fi
113 }
114
115 # Format and output a file
116 function format {
117         # Create a temp file with the provided modeline
118         tmp="$(mktemp)"
119         sed "\$avim: $(get_modeline)" "$1" > "$tmp"
120
121         # Determine cache name
122         md5="$(cat index.cgi vimrc "$tmp" /usr/bin/ex | md5sum -b)"
123         out="cache/${md5% *}.htm"
124         zip="$out.gz"
125
126         # Cache the file, if needed
127         if [ ! -f "$zip" ]; then
128                 # - I have some plugins in ~/.vim
129                 # - Run ex in pty to trick it into thinking that it
130                 #   has a real terminal, note that we also have to set
131                 #   term=xterm-256color in vimrc
132                 HOME=/home/andy \
133                 /home/vpaste/bin/pty \
134                 /usr/bin/ex -nXZ -i NONE -u vimrc \
135                         '+sil! set fde= fdt= fex= inde= inex= key= pa= pexpr=' \
136                         '+sil! set iconstring= ruf= stl= tal=' \
137                         "+sil! set titlestring=$1\ -\ vpaste.net" \
138                         '+sil! set noml' \
139                         '+sil! $d|'$2    \
140                         '+sil! %s/\r//g' \
141                         '+sil! TOhtml'   \
142                         "+sav! $out"     \
143                         '+qall!'         \
144                         "$tmp" >/dev/null 2>&1
145                 gzip "$out"
146         fi
147         rm "$tmp"
148
149         # Output the file
150         respond -y -c "text/html" -f "$zip"
151 }
152
153 # List previous pastes
154 function do_cmd {
155         respond
156         case "$1" in
157         ls)
158                 ls -t db | column
159                 ;;
160         head)
161                 awk -v 'rows=4' -v 'cols=60' '
162                         FNR==1      { gsub(/.*\//, "", FILENAME);
163                                       print FILENAME
164                                       print "-----" }
165                         FNR==1,/^$/ { next }
166                         /\S/        { i++; printf "%."cols"s\n", $0 }
167                         i>=rows     { nextfile  }
168                         ENDFILE     { i=0; print ""  }
169                 ' $(ls -t db/*)
170                 ;;
171         stat)
172                 ls -l --time-style='+%Y %m' db |
173                 awk -v 'hdr=Jan  Feb  Mar  Apr  May  Jun  Jul  Aug  Sep  Oct  Nov  Dec' '
174                         BEGIN { printf "%64s\n", hdr }
175                         NR>1  { cnt[$6+0][$7+0]++ }
176                         END   { for (y in cnt) {
177                                   printf "%4d", y
178                                   for (m=1; m<=12; m++)
179                                     printf "%5s", cnt[y][m]
180                                   printf "\n" } }'
181                 ;;
182         esac
183 }
184
185 # Format a file for viewing
186 function do_print {
187         if [ -f "./$1" ]; then
188                 input="$1"
189         elif [ -f "db/$1" ]; then
190                 input="db/$1"
191                 trim='1,/^$/d' # sed command to remove cruft
192         else
193                 respond -h 'Status: 404 Not Found' \
194                         "File '$1' not found"
195         fi
196
197         # Check for javascript
198         if [[ "$input" == 'embed.js' &&
199               "$HTTP_ACCEPT" != *'html'* ]]; then
200                 respond -c text/javascript -f "$input"
201         fi
202
203         # Check for raw paste
204         if [[ "$QUERY_STRING" == 'raw'* ||
205               "$REQUEST_URI"  != *'?'* &&
206               ( "$input"       != 'db/'* ||
207                 "$HTTP_ACCEPT" != *'html'* ) ]]; then
208                 respond
209                 sed "$trim" "$input"
210                 exit
211         fi
212
213         # Output the file
214         format "$input" "$trim"
215 }
216
217 # Format a file for viewing
218 function do_view {
219         format -
220 }
221
222 # Upload handler
223 function do_upload {
224         body=$(cat -)
225         spam=$(echo -n "$body" | cut_file "ignoreme")
226         text=$(echo -n "$body" | cut_file "(text|x)")
227         bans=$(echo -n "$REMOTE_ADDR" | grep -f blacklist)
228         [ ! -z "$spam" ] && respond "Spam check.."
229         [ ! -z "$bans" ] && respond "You have been banned"
230         [   -z "$text" ] && respond "No text pasted"
231
232         # Format and save message
233         output="$(mktemp db/XXXXX)"
234         cat >"$output" <<-EOF
235                 vim: $(get_modeline)
236                 Date: $(date -R)
237                 From: $REMOTE_ADDR
238                 User-Agent: $HTTP_USER_AGENT
239
240                 $text
241         EOF
242
243         # Redirect user
244         uri="$url$(basename "$output")"
245         respond -h 'Status: 302 Found' \
246                 -h "Location: $uri"    \
247                 "$uri"
248 }
249
250 # Default index page
251 function do_help {
252         filetypes=$(
253                 ls /usr/share/vim/vim*/syntax/ /home/andy/.vim/syntax/ |
254                 sed -n '/^\(syntax\|manual\|synload\|2html\|colortest\|hitest\).vim$/d; s/.vim$//p' |
255                 sort | uniq
256         )
257         uploads=$(ls -t db 2>/dev/null | head -n 5)
258         filetype=$(get_param '^(ft|filet(y(pe?)?)?)$')
259         vpaste='<a href="vpaste?ft=sh">vpaste</a>'
260         pileus='http://pileus.org/tools/vpaste'
261         gitweb='http://pileus.org/git/?p=vpaste'
262         mailman='http://pileus.org/mailman/listinfo/dev'
263         repo='git://pileus.org/vpaste'
264
265         respond -c text/html
266         cat <<-EOF
267         <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
268           "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
269         <html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
270                 <head>
271                         <title>vpaste.net - Vim based pastebin</title>
272                         <meta http-equiv="Content-type" content="text/html;charset=UTF-8" />
273                         <meta name="description" content="vpaste: Vim based pastebin" />
274                         <meta name="keywords" content="vpaste,paste,pastebin,vim" />
275                         <meta name="google-site-verification" content="OvHF73zD7osJ1VSq9rJxnMFlja36944ud6CiP_iXQnI" />
276                         <style type="text/css">
277                                 *          { margin: 0;
278                                              padding: 0; }
279                                 body       { margin: 4em 8em 4em 8em;
280                                              font-family: sans-serif; }
281                                 input      { padding: 2px 6px 3px 6px; }
282                                 /* Items */
283                                 textarea   { width: 100%;
284                                              margin-bottom: 0.5em; }
285                                 .buttons   { float: left; }
286                                 .links     { float: right; }
287                                 .links *   { text-decoration: none;
288                                              margin-left: 0.5em; }
289                                 .box       { display: none;
290                                              clear: both;
291                                              margin-top: 2.7em;
292                                              border-top: solid 1px #888; }
293                                 /* box contents */
294                                 h1         { margin-top: 1.0em;
295                                              font-size: larger; }
296                                 ul,dd,dl,p { margin: 0 0 0 2em; }
297                                 dt         { font-weight: bold;
298                                              padding: 0.5em 0 0 0; }
299                                 span       { font-family: monospace; }
300                                 .cmds dd   { font-family: monospace; }
301                         </style>
302                         <script type="text/javascript">
303                                 //<![CDATA[
304                                 function show(id) {
305                                         var boxes = document.getElementsByClassName('box')
306                                         for (var i = 0; i < boxes.length; i++) {
307                                                 var box = boxes[i]
308                                                 if (box.id == id && box.style.display != 'block')
309                                                         box.style.display = 'block'
310                                                 else
311                                                         box.style.display = "none"
312                                         }
313                                 }
314                                 function autoshow() {
315                                         var id  = document.location.toString().replace(/.*#/, '')
316                                         var box = document.getElementById(id)
317                                         if (box) box.style.display = "block"
318                                 }
319                                 //]]>
320                         </script>
321                 </head>
322
323                 <body onload="autoshow()">
324                         <form id="form" method="post" action="" enctype="multipart/form-data">
325                                 <div>
326                                         <input style="display:none" type="text" name="ignoreme" value="" />
327                                         <textarea name="text" cols="80" rows="25"></textarea>
328                                 </div>
329                                 <div class="buttons">
330                                         <select onchange="document.getElementById('form').action =
331                                                           document.location + '?ft=' + this.value;">
332                                                 <option value="" disabled="disabled">Filetype</option>
333                                                 <option value="">None</option>
334                                                 $(for ft in $filetypes; do
335                                                         echo "<option$(
336                                                         [ "$ft" = "$filetype" ] &&
337                                                                 echo ' selected="selected"'
338                                                         )>$ft</option>"
339                                                 done)
340                                         </select>
341                                         <input type="submit" value="Paste" />
342                                 </div>
343                                 <div class="links">
344                                         <a href="">vpaste</a> <span>-</span>
345                                         <a href="#usage"   onclick="show('usage'  )">Usage</a>
346                                         <a href="#devel"   onclick="show('devel'  )">Development</a>
347                                         <a href="#uploads" onclick="show('uploads')">Uploads</a>
348                                 </div>
349                         </form>
350
351                         <div class="box" id="usage">
352                                 <h1>Pasting</h1>
353                                 <dl class="cmds">
354                                         <dt>From a shell</dt>
355                                         <dd> $vpaste file [option=value,..]</dd>
356                                         <dd> &lt;command&gt; | $vpaste [option=value,..]</dd>
357
358                                         <dt>From Vim</dt>
359                                         <dd> :map vp :exec "w !vpaste ft=".&amp;ft&lt;CR&gt;</dd>
360                                         <dd> :vmap vp &lt;ESC&gt;:exec "'&lt;,'&gt;w !vpaste ft=".&amp;ft&lt;CR&gt;</dd>
361
362                                         <dt>With curl</dt>
363                                         <dd> &lt;command&gt; | curl -F 'text=&lt;-' $url[?option=value,..]</dd>
364                                 </dl>
365
366                                 <h1>Options</h1>
367                                 <p>Add <b>?option[=value],..</b> to make your text a rainbow.</p>
368                                 <p>Options specified when uploading are saved as defaults.</p>
369
370                                 <dl>
371                                         <dt>bg, background={light|dark}</dt>
372                                         <dd>Background color to use for the page</dd>
373                                         <dt>et, expandtab</dt>
374                                         <dd>Expand tabs to spaces</dd>
375                                         <dt>fdm, foldmethod=(syntax|indent)</dt>
376                                         <dd>Turn on dynamic code folding</dd>
377                                         <dt>ft, filetype={filetype}</dt>
378                                         <dd>A filetype to use for highlighting, see above menu for supported types</dd>
379                                         <dt>nu, number</dt>
380                                         <dd>Add line numbers</dd>
381                                         <dt>ts, tabstop=[N]</dt>
382                                         <dd>Number of spaces to use for tabs when <b>et</b> is set</dd>
383                                         <dt>...</dt>
384                                         <dd>See :help modeline for more information</dd>
385                                 </dl>
386                         </div>
387
388                         <div class="box" id="devel">
389                                 <h1>License</h1>
390                                 <p>Copyright © 2009-2013
391                                    Andy Spencer &lt;andy753421@gmail.com&gt;</p>
392                                 <p>See individual files for licenses</p>
393
394                                 <h1>Source code</h1>
395                                 <dl>
396                                         <dt>Client</dt>
397                                         <dd><a href="vpaste?ft=sh">vpaste</a>
398                                             <a href="embed.js?ft=javascript">embed.js</a></dd>
399                                         <dt>Server</dt>
400                                         <dd><a href="index.cgi?ft=sh">index.cgi</a>
401                                             <a href="vimrc?ft=vim">vimrc</a>
402                                             <a href="htaccess?ft=apache">htaccess</a>
403                                             <a href="robots.txt?ft=robots">robots.txt</a>
404                                             <a href="sitemap.xml?ft=xml">sitemap.xml</a>
405                                             <a href="blacklist?raw">blacklist</a></dd>
406                                         <dt>Patches</dt>
407                                         <dd><a href="2html.patch?ft=diff">2html.patch</a></dd>
408                                         <dt>Development</dt>
409                                         <dd><a href="$pileus">homepage</a>
410                                             <a href="$gitweb">gitweb</a><br />
411                                             <code>git clone $repo</code></dd>
412                                 </dl>
413
414                                 <h1>Bugs</h1>
415                                 <ul>
416                                         <li>Using strange filetypes (ft=2html) may result in strange output.</li>
417                                         <li>Other issues can be sent to the pileus.org
418                                             <a href="$mailman">mailing list</a>.</li>
419                                 </ul>
420                         </div>
421
422                         <div class="box" id="uploads">
423                                 <h1>Recent Uploads</h1>
424                                 <ul>$(for upload in ${uploads[@]}; do
425                                     echo -n "<li>"
426                                     echo -n "<span>$upload</span> "
427                                     echo -n "<a href='$upload?raw'>text</a> "
428                                     echo -n "<a href='$upload'>rainbow</a>"
429                                     echo "</li>"
430                                 done)
431                                 </ul>
432                                 <p><a href="ls">list all</a></p>
433                                 <p><a href="head">sample all</a></p>
434                                 <p><a href="stat">statistics</a></p>
435                         </div>
436                 </body>
437         </html>
438         EOF
439 }
440
441 # Main
442 PATH=/bin:/usr/bin
443 url="http://$HTTP_HOST${REQUEST_URI/\?*}"
444 pathinfo="${REQUEST_URI/*\/}"
445 pathinfo="${pathinfo/\?*}"
446
447 if [ "$pathinfo" = ls ]; then
448         do_cmd ls
449 elif [ "$pathinfo" = head ]; then
450         do_cmd head
451 elif [ "$pathinfo" = stat ]; then
452         do_cmd stat
453 elif [ "$pathinfo" = view ]; then
454         do_view
455 elif [ "$pathinfo" ]; then
456         do_print "$pathinfo"
457 elif [ "$CONTENT_TYPE" ]; then
458         do_upload
459 else
460         do_help
461 fi