]> Pileus Git - vpaste/blob - index.cgi
xhtml validation
[vpaste] / index.cgi
1 #!/bin/bash
2
3 # Copyright (C) 2009 Andy Spencer
4
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
9
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU General Public License for more details.
14
15 # Remove url codings form stdin
16 function get_modeline {
17         modeline=$(
18                 echo "$QUERY_STRING" | 
19                 sed -e 's/%\([0-9A-F][0-9A-F]\)/\\\\\x\1/g; s/[,&]/ /g' |
20                 xargs echo -e
21         )
22         echo "vim: $modeline"
23 }
24
25 # Extract an uploaded file from standard input
26 # $1 is the boundary delimiter for the file
27 function cut_file {
28         awk "
29                 /--$1/ {st=1};
30                 st==2  {print \$0};
31                 /$1--/ {st=0};
32                 /^\\r$/ && st==1 {st=2};
33         " | head -c -2 | head -c $((128*1024))
34         # Remove trailing ^M's that come with CGI
35         # Limit size to 128K
36 }
37
38 # Print out a generic header
39 function header {
40         echo "Content-Type: $1; charset=UTF-8"
41         echo
42 }
43
44 # Format a file for viewing 
45 function do_print {
46         if [ -f "$1" ]; then
47                 input="$1"
48         elif [ -f "db/$1" ]; then
49                 input="db/$1"
50                 trim='1d' # sed command to remove cruft
51         else
52                 echo "Status: 404 Not Found"
53                 header text/plain
54                 echo "File '$1' not found"
55                 return
56         fi
57
58
59         if [[ "$REQUEST_URI" == *'?'* ]]; then
60                 # Create a temp file with the provided modeline
61                 output="$(mktemp)"
62                 tmp="$(mktemp)"
63                 sed "1a$(get_modeline)" "$input" > "$tmp"
64
65                 # - I have some plugins in ~/.vim
66                 # - Run ex in screen to trick it into thinking that it
67                 #   has a real terminal, not that we also have to set
68                 #   term=xterm-256color in vimrc
69                 HOME=/home/andy \
70                 screen -D -m ex -Z -u vimrc \
71                         '+2d|'$trim     \
72                         '+%s/\r//g'     \
73                         '+TOhtml'       \
74                         "+sav! $output" \
75                         '+qall!'        \
76                         "$tmp"
77
78                 header text/html
79                 cat "$output" 
80                 rm "$tmp" "$output"
81         else
82                 header text/plain
83                 sed "$trim" "$input"
84         fi
85 }
86
87
88 # Upload handler
89 function do_upload {
90         output="$(mktemp db/XXXXX)"
91         uri="$url$(basename "$output")${QUERY_STRING:+"?"}"
92         (get_modeline; cut_file "$1") > "$output"
93         echo "Status: 302 Found"
94         echo "Location: $uri"
95         header text/plain
96         echo "$uri"
97 }
98
99 # Default index page
100 function do_help {
101 filetypes=$(
102         ls /usr/share/vim/vim{72,files}/syntax/ /home/andy/.vim/after/syntax/ |
103         sed -n 's/.vim$//p' | sort | uniq
104 )
105 uploads=$(ls -t db | head -n 5 | sed "s!^!$url!")
106
107 header text/html
108 cat - <<EOF
109 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
110   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
111 <html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
112         <head>
113                 <title>vpaste.net - Vim based pastebin</title>
114                 <meta http-equiv="Content-type" content="text/html;charset=UTF-8" />
115                 <style type="text/css">
116                         * { margin:0; padding:0; }
117                         body { margin:1em; }
118                         h4 { margin:1em 0 0 0; }
119                         blockquote,dd,dl,p,pre,ul { margin:0 0 0 2em; }
120                         dt { font-weight:bold; padding:0.5em 0 0 0; }
121                         blockquote { width:50em; font-size:small; }
122                 </style>
123         </head>
124         <body>
125                 <form id="form" method="post" action="?" enctype="multipart/form-data">
126                 <div style="margin:0 0 1.5em 0;">
127                 <textarea name="text" cols="80" rows="25" style="width:100%; height:20em;"></textarea>
128                 <select onchange="document.getElementById('form').action =
129                         document.location + '?ft=' + this.value;">
130                 <option value="" selected="selected" disabled="disabled">Filetype</option>
131                 <option value="">None</option>
132                 $(for ft in $filetypes; do
133                         echo "<option>$ft</option>"
134                 done)
135                 </select>
136                 <input type="submit" value="Paste" />
137                 </div>
138                 </form>
139
140                 <h4>NAME</h4>
141                 <p>vpaste: Vim based pastebin</p>
142
143                 <h4>SYNOPSIS</h4>
144                 <div>
145                 <pre> vpaste file [option=value,..]</pre>
146                 <pre> &lt;command&gt; | vpaste [option=value,..]</pre>
147                 <br />
148                 <pre> &lt;command&gt; | curl -F 'x=&lt;-' $url[?option=value,..]</pre>
149                 <br />
150                 <pre> :map vp :exec "w !vpaste ft=".&amp;ft&lt;CR&gt;</pre>
151                 <pre> :vmap vp &lt;ESC&gt;:exec "'&lt;,'&gt;w !vpaste ft=".&amp;ft&lt;CR&gt;</pre>
152                 </div>
153
154                 <h4>DESCRIPTION</h4>
155                 <p>Add <b>?[option[=value],..]</b> to make your text a rainbow.</p>
156                 <p>Options specified when uploading are used as defaults.</p>
157
158                 <h4>OPTIONS</h4>
159                 <dl>
160                 <dt>ft, filetype={filetype}</dt>
161                 <dd>A filetype to use for highlighting, see above menu for supported types</dd>
162                 <dt>bg, background={light|dark}</dt>
163                 <dd>Background color to use for the page</dd>
164                 <dt>et, expandtab</dt>
165                 <dd>Expand tabs to spaces</dd>
166                 <dt>ts, tabstop=[N]</dt>
167                 <dd>Number of spaces to use for tabs when <b>et</b> is set</dd>
168                 <dt>...</dt>
169                 <dd>See :help modeline for more information</dd>
170                 </dl>
171
172                 <h4>SOURCE</h4>
173                 <ul>
174                 <li><a href="vpaste?ft=sh">vpaste</a></li>
175                 <li><a href="index.cgi?ft=sh">index.cgi</a>
176                     <a href="vimrc?ft=vim">vimrc</a>
177                     <a href="htaccess?ft=apache">htaccess</a></li>
178                 <li><a href="2html-et.patch?ft=diff">2html-et.patch</a></li>
179                 <li><a href="https://lug.rose-hulman.edu/svn/misc/trunk/htdocs/vpaste/">Subversion</a></li>
180                 </ul>
181
182                 <h4>LATEST UPLOADS</h4>
183                 <ul>$(for uri in ${uploads[@]}; do
184                         echo "<li><a href='$uri'>$uri</a></li>"
185                 done)</ul>
186         </body>
187 </html>
188 EOF
189 }
190
191 # Main
192 url="http://$HTTP_HOST/$SCRIPT_URI"
193 pathinfo="${REQUEST_URI/*\/}"
194 pathinfo="${pathinfo/\?*}"
195
196 if [ "$pathinfo" ]; then
197         do_print "$pathinfo"
198 elif [ "$CONTENT_TYPE" ]; then
199         do_upload ${CONTENT_TYPE/*boundary\=/}
200 else
201         do_help
202 fi