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