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