]> Pileus Git - vpaste/blob - index.cgi
Fix html, update vimrc
[vpaste] / index.cgi
1 #!/bin/bash
2
3 # Copyright (C) 2009-2011 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
22 # Extract an uploaded file from standard input
23 #   $1 is the name of the input to extract
24 function cut_file {
25         bnd="${CONTENT_TYPE/*boundary\=/}"
26         awk -v "want=$1" -v "bnd=$bnd" '
27                 BEGIN { RS="\r\n" }
28
29                 # reset based on boundaries
30                 $0 == "--"bnd""     { st=1; next; }
31                 $0 == "--"bnd"--"   { st=0; next; }
32                 $0 == "--"bnd"--\r" { st=0; next; }
33
34                 # search for wanted file
35                 st == 1 && $0 ~  "^Content-Disposition:.* name=\""want"\"" { st=2; next; }
36                 st == 1 && $0 == "" { st=9; next; }
37
38                 # wait for newline, then start printing
39                 st == 2 && $0 == "" { st=3; next; }
40                 st == 3             { print $0    }
41         ' | head -c $((128*1024)) # Limit size to 128K
42 }
43
44 # Print out a generic header
45 function header {
46         echo "Content-Type: $1; charset=UTF-8"
47         echo
48 }
49
50 # Print plain message and exit
51 function message {
52         while [ "$1" == '-h' ]; do
53                 shift; echo "$1"; shift
54         done
55         header text/plain
56         echo "$*"
57         exit
58 }
59
60 # List previous pastes
61 function do_cmd {
62         header text/plain
63         case "$1" in
64         head)
65                 for i in $(ls -t db/*); do
66                         basename $i
67                         basename $i | sed 's/./-/g'
68                         sed '1,/^$/d; /^\s*$/d' $i | sed -n '1,5s/\(.\{0,60\}\).*/\1/p'
69                         echo
70                 done
71                 ;;
72         ls)
73                 ls -t db | column
74                 ;;
75         esac
76 }
77
78 # Format a file for viewing
79 function do_print {
80         if [ -f "./$1" ]; then
81                 input="$1"
82         elif [ -f "db/$1" ]; then
83                 input="db/$1"
84                 trim='1,/^$/d' # sed command to remove cruft
85         else
86                 message -h 'Status: 404 Not Found' \
87                         "File '$1' not found"
88         fi
89
90         # Check for raw paste
91         if [[ "$QUERY_STRING" == 'raw'* ||
92               "$REQUEST_URI"  != *'?'* &&
93               "$HTTP_ACCEPT"  != *'html'* ]]; then
94                 header text/plain
95                 sed "$trim" "$input"
96                 exit
97         fi
98
99         # Create a temp file with the provided modeline
100         output="$(mktemp)"
101         tmp="$(mktemp)"
102         sed "\$avim: $(get_modeline)" "$input" > "$tmp"
103
104         # - I have some plugins in ~/.vim
105         # - Run ex in screen to trick it into thinking that it
106         #   has a real terminal, note that we also have to set
107         #   term=xterm-256color in vimrc
108         HOME=/home/andy \
109         screen -D -m ex -nXZ -i NONE -u vimrc \
110                 '+sil! set fde= fdt= fex= inde= inex= key= pa= pexpr=' \
111                 '+sil! set iconstring= ruf= stl= tal=' \
112                 "+sil! set titlestring=$1\ -\ vpaste.net" \
113                 '+sil! set noml'     \
114                 '+sil! $d|'$trim     \
115                 '+sil! %s/\r//g' \
116                 '+sil! TOhtml'       \
117                 "+sav! $output" \
118                 '+qall!'        \
119                 "$tmp"
120
121         header text/html
122         cat "$output"
123         rm "$tmp" "$output"
124 }
125
126
127 # Upload handler
128 function do_upload {
129         body=$(cat -)
130         spam=$(echo -n "$body" | cut_file "ignoreme")
131         text=$(echo -n "$body" | cut_file "(text|x)")
132         [ ! -z "$spam" ] && message "Spam check.."
133         [   -z "$text" ] && message "No text pasted"
134
135         # Format and save message
136         output="$(mktemp db/XXXXX)"
137         cat >"$output" <<-EOF
138                 vim: $(get_modeline)
139                 Date: $(date -R)
140                 From: $REMOTE_ADDR
141
142                 $text
143         EOF
144
145         # Redirect user
146         uri="$url$(basename "$output")"
147         message -h 'Status: 302 Found' \
148                 -h "Location: $uri"    \
149                 "$uri"
150 }
151
152 # Default index page
153 function do_help {
154         filetypes=$(
155                 ls /usr/share/vim/vim*/syntax/ /home/andy/.vim/syntax/ |
156                 sed -n '/^\(syntax\|manual\|synload\|2html\|colortest\|hitest\).vim$/d; s/.vim$//p' |
157                 sort | uniq
158         )
159         uploads=$(ls -t db | head -n 5)
160
161         header text/html
162         cat <<-EOF
163         <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
164           "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
165         <html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
166             <head>
167                 <title>vpaste.net - Vim based pastebin</title>
168                 <meta http-equiv="Content-type" content="text/html;charset=UTF-8" />
169                 <meta name="description" content="vpaste: Vim based pastebin" />
170                 <meta name="keywords" content="vpaste,paste,pastebin,vim" />
171                 <style type="text/css">
172                     * { margin:0; padding:0; }
173                     body { margin:1em; }
174                     h4 { margin:1em 0 0 0; }
175                     blockquote,dd,dl,p,pre,ul { margin:0 0 0 2em; }
176                     dt { font-weight:bold; padding:0.5em 0 0 0; }
177                     blockquote { width:50em; font-size:small; }
178                     span { font-family:monospace; }
179                 </style>
180             </head>
181             <body>
182                 <form id="form" method="post" action="" enctype="multipart/form-data">
183                 <div style="margin:0 0 1.5em 0;">
184                 <input style="display:none" type="text" name="ignoreme" value="" />
185                 <textarea name="text" cols="80" rows="25" style="width:100%; height:20em;"></textarea>
186                 <select onchange="document.getElementById('form').action =
187                     document.location + '?ft=' + this.value;">
188                 <option value="" selected="selected" disabled="disabled">Filetype</option>
189                 <option value="">None</option>
190                 $(for ft in $filetypes; do
191                     echo "<option>$ft</option>"
192                 done)
193                 </select>
194                 <input type="submit" value="Paste" />
195                 </div>
196                 </form>
197
198                 <h4>NAME</h4>
199                 <p>vpaste: Vim based pastebin</p>
200
201                 <h4>SYNOPSIS</h4>
202                 <div>
203                 <pre> vpaste file [option=value,..]</pre>
204                 <pre> &lt;command&gt; | vpaste [option=value,..]</pre>
205                 <br />
206                 <pre> &lt;command&gt; | curl -F 'text=&lt;-' $url[?option=value,..]</pre>
207                 <br />
208                 <pre> :map vp :exec "w !vpaste ft=".&amp;ft&lt;CR&gt;</pre>
209                 <pre> :vmap vp &lt;ESC&gt;:exec "'&lt;,'&gt;w !vpaste ft=".&amp;ft&lt;CR&gt;</pre>
210                 </div>
211
212                 <h4>DESCRIPTION</h4>
213                 <p>Add <b>?option[=value],..</b> to make your text a rainbow.</p>
214                 <p>Options specified when uploading are stored as defaults.</p>
215
216                 <h4>OPTIONS</h4>
217                 <dl>
218                 <dt>ft, filetype={filetype}</dt>
219                 <dd>A filetype to use for highlighting, see above menu for supported types</dd>
220                 <dt>fdm, foldmethod=(syntax|indent)</dt>
221                 <dd>Turn on dynamic code folding</dd>
222                 <dt>bg, background={light|dark}</dt>
223                 <dd>Background color to use for the page</dd>
224                 <dt>et, expandtab</dt>
225                 <dd>Expand tabs to spaces</dd>
226                 <dt>ts, tabstop=[N]</dt>
227                 <dd>Number of spaces to use for tabs when <b>et</b> is set</dd>
228                 <dt>...</dt>
229                 <dd>See :help modeline for more information</dd>
230                 </dl>
231
232                 <h4>BUGS</h4>
233                 <ul>
234                 <li>Using strange filetypes (ft=2html) may result in strange output.</li>
235                 <li><a href="mailto:andy753421@gmail.com?subject=vpaste bug">Other?</a></li>
236                 </ul>
237
238                 <h4>SOURCE</h4>
239                 <ul>
240                 <li><a href="vpaste?ft=sh">vpaste</a></li>
241                 <li><a href="index.cgi?ft=sh">index.cgi</a>
242                     <a href="vimrc?ft=vim">vimrc</a>
243                     <a href="htaccess?ft=apache">htaccess</a>
244                     <a href="robots.txt?ft=robots">robots.txt</a>
245                     <a href="sitemap.xml?ft=xml">sitemap.xml</a></li>
246                 <li><a href="2html.patch?ft=diff">2html.patch</a></li>
247                 <li><a href="https://lug.rose-hulman.edu/svn/misc/trunk/htdocs/vpaste/">Subversion</a></li>
248                 </ul>
249
250                 <h4>LATEST UPLOADS</h4>
251                 <ul>$(for upload in ${uploads[@]}; do
252                     echo -n "<li>"
253                     echo -n "<span>$upload</span> "
254                     echo -n "<a href='$upload?raw'>text</a> "
255                     echo -n "<a href='$upload'>rainbow</a>"
256                     echo "</li>"
257                 done)
258                 </ul>
259                 <p><a href="ls">list all</a></p>
260                 <p><a href="head">sample all</a></p>
261             </body>
262         </html>
263         EOF
264 }
265
266 # Main
267 PATH=/bin:/usr/bin
268 url="http://$HTTP_HOST${REQUEST_URI/\?*}"
269 pathinfo="${REQUEST_URI/*\/}"
270 pathinfo="${pathinfo/\?*}"
271
272 if [ "$pathinfo" = ls ]; then
273         do_cmd ls
274 elif [ "$pathinfo" = head ]; then
275         do_cmd head
276 elif [ "$pathinfo" ]; then
277         do_print "$pathinfo"
278 elif [ "$CONTENT_TYPE" ]; then
279         do_upload
280 else
281         do_help
282 fi