]> Pileus Git - vpaste/blob - index.cgi
no-text test
[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 -v "bnd=$1" '{
29                 if ($0 == "--"bnd"\r")     { st=1;     }
30                 if ($0 == "--"bnd"--\r")   { st=0;     }
31                 if (st == 2)               { print $0; }
32                 if ($0 == "\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 -nXZ -i NONE -u vimrc \
71                         '+set bexpr= fde= fdt= fex= inde= inex= key= pa= pexpr' \
72                         '+set iconstring= ruf= stl= tal= titlestring=' \
73                         '+set noml'     \
74                         '+2d|'$trim     \
75                         '+%s/\r//g'     \
76                         '+TOhtml'       \
77                         "+sav! $output" \
78                         '+qall!'        \
79                         "$tmp"
80
81                 header text/html
82                 cat "$output" 
83                 rm "$tmp" "$output"
84         else
85                 header text/plain
86                 sed "$trim" "$input"
87         fi
88 }
89
90
91 # Upload handler
92 function do_upload {
93         output="$(mktemp db/XXXXX)"
94         uri="$url$(basename "$output")${QUERY_STRING:+"?"}"
95         text=$(cut_file "$1")
96         if [ -z "$text" ]; then
97                 header text/plain
98                 echo "No text pasted"
99                 exit
100         fi
101         (get_modeline; echo "$text") > "$output"
102         echo "Status: 302 Found"
103         echo "Location: $uri"
104         header text/plain
105         echo "$uri"
106 }
107
108 # Default index page
109 function do_help {
110 filetypes=$(
111         ls /usr/share/vim/vim*/syntax/ /home/andy/.vim/after/syntax/ |
112         sed -n '/^\(syntax\|manual\|synload\|2html\|colortest\|hitest\).vim$/d; s/.vim$//p' |
113         sort | uniq
114 )
115 uploads=$(ls -t db | head -n 5)
116
117 header text/html
118 cat - <<EOF
119 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
120   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
121 <html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
122         <head>
123                 <title>vpaste.net - Vim based pastebin</title>
124                 <meta http-equiv="Content-type" content="text/html;charset=UTF-8" />
125                 <style type="text/css">
126                         * { margin:0; padding:0; }
127                         body { margin:1em; }
128                         h4 { margin:1em 0 0 0; }
129                         blockquote,dd,dl,p,pre,ul { margin:0 0 0 2em; }
130                         dt { font-weight:bold; padding:0.5em 0 0 0; }
131                         blockquote { width:50em; font-size:small; }
132                         span { font-family:monospace; }
133                 </style>
134         </head>
135         <body>
136                 <form id="form" method="post" action="?" enctype="multipart/form-data">
137                 <div style="margin:0 0 1.5em 0;">
138                 <textarea name="text" cols="80" rows="25" style="width:100%; height:20em;"></textarea>
139                 <select onchange="document.getElementById('form').action =
140                         document.location + '?ft=' + this.value;">
141                 <option value="" selected="selected" disabled="disabled">Filetype</option>
142                 <option value="">None</option>
143                 $(for ft in $filetypes; do
144                         echo "<option>$ft</option>"
145                 done)
146                 </select>
147                 <input type="submit" value="Paste" />
148                 </div>
149                 </form>
150
151                 <h4>NAME</h4>
152                 <p>vpaste: Vim based pastebin</p>
153
154                 <h4>SYNOPSIS</h4>
155                 <div>
156                 <pre> vpaste file [option=value,..]</pre>
157                 <pre> &lt;command&gt; | vpaste [option=value,..]</pre>
158                 <br />
159                 <pre> &lt;command&gt; | curl -F 'x=&lt;-' $url[?option=value,..]</pre>
160                 <br />
161                 <pre> :map vp :exec "w !vpaste ft=".&amp;ft&lt;CR&gt;</pre>
162                 <pre> :vmap vp &lt;ESC&gt;:exec "'&lt;,'&gt;w !vpaste ft=".&amp;ft&lt;CR&gt;</pre>
163                 </div>
164
165                 <h4>DESCRIPTION</h4>
166                 <p>Add <b>?[option[=value],..]</b> to make your text a rainbow.</p>
167                 <p>Options specified when uploading are used as defaults.</p>
168
169                 <h4>OPTIONS</h4>
170                 <dl>
171                 <dt>ft, filetype={filetype}</dt>
172                 <dd>A filetype to use for highlighting, see above menu for supported types</dd>
173                 <dt>bg, background={light|dark}</dt>
174                 <dd>Background color to use for the page</dd>
175                 <dt>et, expandtab</dt>
176                 <dd>Expand tabs to spaces</dd>
177                 <dt>ts, tabstop=[N]</dt>
178                 <dd>Number of spaces to use for tabs when <b>et</b> is set</dd>
179                 <dt>...</dt>
180                 <dd>See :help modeline for more information</dd>
181                 </dl>
182
183                 <h4>BUGS</h4>
184                 <ul>
185                 <li>Using strange filetypes (ft=2html) may result in strange output.</li>
186                 <li><a href="mailto:andy753421@gmail.com?subject=vpaste bug">Other?</a></li>
187                 </ul>
188
189                 <h4>SOURCE</h4>
190                 <ul>
191                 <li><a href="vpaste?ft=sh">vpaste</a></li>
192                 <li><a href="index.cgi?ft=sh">index.cgi</a>
193                     <a href="vimrc?ft=vim">vimrc</a>
194                     <a href="htaccess?ft=apache">htaccess</a></li>
195                 <li><a href="2html-et.patch?ft=diff">2html-et.patch</a></li>
196                 <li><a href="https://lug.rose-hulman.edu/svn/misc/trunk/htdocs/vpaste/">Subversion</a></li>
197                 </ul>
198
199                 <h4>LATEST UPLOADS</h4>
200                 <ul>$(for upload in ${uploads[@]}; do
201                         echo -n "<li>"
202                         echo -n "<span>$upload</span> "
203                         echo -n "<a href='$upload'>text</a> "
204                         echo -n "<a href='$upload?'>rainbow</a>"
205                         echo "</li>"
206                 done)</ul>
207         </body>
208 </html>
209 EOF
210 }
211
212 # Main
213 url="http://$HTTP_HOST${REQUEST_URI/\?*}"
214 pathinfo="${REQUEST_URI/*\/}"
215 pathinfo="${pathinfo/\?*}"
216
217 if [ "$pathinfo" ]; then
218         do_print "$pathinfo"
219 elif [ "$CONTENT_TYPE" ]; then
220         do_upload "${CONTENT_TYPE/*boundary\=/}"
221 else
222         do_help
223 fi