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