]> Pileus Git - vpaste/blob - index.cgi
updates for vpaste.net
[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
34         # Remove trailing ^M's that come with CGI
35 }
36
37 # Print out a generic header
38 function header {
39         echo "Content-Type: $1; charset=UTF-8"
40         echo
41 }
42
43 # Format a file for viewing 
44 function do_print {
45         if [ -f "$1" ]; then
46                 input="$1"
47         elif [ -f "db/$1" ]; then
48                 input="db/$1"
49                 trim='1d' # sed command to remove cruft
50         else
51                 echo "Status: 404 Not Found"
52                 header text/plain
53                 echo "File '$1' not found"
54                 return
55         fi
56
57
58         if [[ "$REQUEST_URI" == *'?'* ]]; then
59                 # Create a temp file with the provided modeline
60                 output="$(mktemp)"
61                 tmp="$(mktemp)"
62                 sed "1a$(get_modeline)" "$input" > "$tmp"
63
64                 # - I have some plugins in ~/.vim
65                 # - Run ex in screen to trick it into thinking that it
66                 #   has a real terminal, not that we also have to set
67                 #   term=xterm-256color in vimrc
68                 HOME=/home/andy \
69                 screen -D -m ex -u vimrc \
70                         '+2d|'$trim     \
71                         '+%s/\r//g'     \
72                         '+TOhtml'       \
73                         "+sav! $output" \
74                         '+qall!'        \
75                         "$tmp"
76
77                 header text/html
78                 cat "$output" 
79                 rm "$tmp" "$output"
80         else
81                 header text/plain
82                 sed "$trim" "$input"
83         fi
84 }
85
86
87 # Upload handler
88 function do_upload {
89         output="$(mktemp db/XXXXX)"
90         uri="$url$(basename "$output")${QUERY_STRING:+"?"}"
91         (get_modeline; cut_file "$1") > "$output"
92         echo "Status: 302 Found"
93         echo "Location: $uri"
94         header text/plain
95         echo "$uri"
96 }
97
98 # Default index page
99 function do_help {
100 filetypes=$(
101         ls /usr/share/vim/vim{72,files}/syntax/ /home/andy/.vim/after/syntax/ |
102         sed -n 's/.vim$//p' | sort | uniq
103 )
104 uploads=$(ls -t db | head -n 5 | sed "s!^!$url!")
105
106 header text/html
107 cat - <<EOF
108 <html>
109         <head>
110                 <style>
111                 * { margin:0; padding:0; }
112                 body { margin:1em; }
113                 h4 { margin:1em 0 0 0; }
114                 p,ul,dl,dd,pre,blockquote { margin:0 0 0 2em; }
115                 dt { font-weight:bold; padding:0.5em 0 0 0; }
116                 blockquote { width:50em; font-size:small; }
117                 </style>
118         </head>
119         <body>
120                 <form id="form" method="post" action="?" enctype="multipart/form-data">
121                 <textarea name="text" style="width:100%; height:20em;"></textarea>
122                 <br>
123                 <select onchange="document.getElementById('form').action =
124                         document.location + '?ft=' + this.value;">
125                 <option value="" selected="selected" disabled="disabled">Filetype</option>
126                 <option value="">None</option>
127                 $(for ft in $filetypes; do
128                         echo "<option>$ft</option>"
129                 done)
130                 </select>
131                 <input type="submit" value="Paste">
132                 </form>
133                 <br>
134
135                 <h4>NAME</h4>
136                 <p>vpaste: Vim enabled pastebin</p>
137
138                 <h4>SYNOPSIS</h4>
139                 <pre> vpaste file [option=value,..]</pre>
140                 <pre> &lt;command&gt; | vpaste [option=value,..]</pre>
141                 <br>
142                 <pre> &lt;command&gt; | curl -F 'x=<-' $url[?option=value,..]</pre>
143                 <br>
144                 <pre>:map vp :exec "w !vpaste ft=".&ft&lt;CR&gt;</pre>
145                 <pre>:vmap vp &lt;ESC&gt;:exec "'&lt;,'&gt;w !vpaste ft=".&ft&lt;CR&gt;</pre>
146
147                 <h4>DESCRIPTION</h4>
148                 <p>Add <b>?[option[=value],..]</b> to make your text a rainbow.</p>
149                 <p>Options specified when uploading are used as defaults.</p>
150
151                 <h4>OPTIONS</h4>
152                 <dl>
153                 <dt>ft, filetype={filetype}</dt>
154                 <dd>A filetype to use for highlighting, see above menu for supported types</dd>
155                 <dt>bg, background={light|dark}</dt>
156                 <dd>Background color to use for the page</dd>
157                 <dt>et, expandtab</dt>
158                 <dd>Expand tabs to spaces</dd>
159                 <dt>ts, tabstop=[N]</dt>
160                 <dd>Number of spaces to use for tabs when <b>et</b> is set</dd>
161                 <dt>...</dt>
162                 <dd>See :help modeline for more information</dd>
163                 </dl>
164
165                 <h4>SOURCE</h4>
166                 <ul>
167                 <li><a href="vpaste?ft=sh">vpaste</a>
168                 <li><a href="index.cgi?ft=sh">index.cgi</a>
169                     <a href="vimrc?ft=vim">vimrc</a>
170                     <a href="htaccess?ft=apache">htaccess</a>
171                 <li><a href="2html-et.patch?ft=diff">2html-et.patch</a>
172                 </ul>
173
174                 <h4>LATEST UPLOADS</h4>
175                 <ul>$(for uri in ${uploads[@]}; do
176                         echo "<li><a href='$uri'>$uri</a>"
177                 done)</ul>
178         </body>
179 </html>
180 EOF
181 }
182
183 # Main
184 url="http://$HTTP_HOST/$SCRIPT_URI"
185 pathinfo="${REQUEST_URI/*\/}"
186 pathinfo="${pathinfo/\?*}"
187
188 if [ "$pathinfo" ]; then
189         do_print "$pathinfo"
190 elif [ "$CONTENT_TYPE" ]; then
191         do_upload ${CONTENT_TYPE/*boundary\=/}
192 else
193         do_help
194 fi