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