]> Pileus Git - vpaste/blob - index.cgi
updating vpaste
[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 urldecode {
17         sed -e 's/%\([0-9A-F][0-9A-F]\)/\\\\\x\1/g; s/[,&]/ /g' | xargs echo -e
18 }
19
20 # Extract an uploaded file from standard input
21 # $1 is the boundary delimiter for the file
22 function cut_file {
23         awk "
24                 /--$1/ {st=1};
25                 st==2  {print \$0};
26                 /$1--/ {st=0};
27                 /^\\r$/ && st==1 {st=2};
28         " | head -c -2
29         # Remove trailing ^M's that come with CGI
30 }
31
32 # Print out a generic header
33 function start_html {
34         echo "Content-Type: text/html; charset=UTF-8"
35         echo
36 }
37
38 # Print out a generic header
39 function start_text {
40         echo "Content-Type: text/plain; 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         else
51                 echo "Status: 404"
52                 start_text
53                 echo "File '$1' not found"
54                 return
55         fi
56
57         modeline="$(echo $QUERY_STRING | urldecode)"
58
59         if [ -z "$modeline" ]; then
60                 start_text
61                 cat "$input"
62         else
63                 # I have some plugins in ~/.vim
64                 #
65                 # Run vimhi.sh in screen to trick it into thinking
66                 # that it has a real terminal, not that we also have to
67                 # set term=xterm-256color in vimrc
68                 output="$(mktemp)"
69                 HOME=/home/andy \
70                 screen -D -m ./vimhi.sh "$input" "$output" "$modeline"
71                 start_html
72                 cat "$output" 
73         fi
74 }
75
76
77 # Upload handler
78 function do_upload {
79         output="$(mktemp db/XXXXX)"
80         uri="$SCRIPT_URI$(basename "$output")"
81         cut_file "$1" > "$output"
82         start_text
83         echo "$uri"
84 }
85
86 # Default index page
87 function do_help {
88 filetypes=$(
89         ls /usr/share/vim/vim{72,files}/syntax/ /home/andy/.vim/after/syntax/ |
90         sed -n 's/.vim$//p' | sort | uniq
91 )
92 uploads=$(ls -t db | head -n 5 | sed "s!^!$SCRIPT_URI!")
93
94 start_html
95 cat - <<EOF
96 <html>
97         <head>
98                 <style>
99                 * { margin:0; padding:0; }
100                 body { margin:1em; }
101                 h4 { margin:1em 0 0 0; }
102                 p,ul,dl,dd,pre,blockquote { margin:0 0 0 2em; }
103                 dt { font-weight:bold; padding:0.5em 0 0 0; }
104                 blockquote { height:2pc; width:30em; font-size:smaller; overflow:hidden; }
105                 blockquote:hover { height:auto; width:auto; }
106                 </style>
107         </head>
108         <body>
109                 <h4>NAME</h4>
110                 <p>vpaste: Vim enabled pastebin</p>
111
112                 <h4>SYNOPSIS</h4>
113                 <pre>&lt;command&gt; | curl -F 'x=<-' $SCRIPT_URI</pre>
114
115                 <h4>DESCRIPTION</h4>
116                 <p>Add <b>?option[=value] ..</b> to make your text a rainbow.</p>
117
118                 <h4>OPTIONS</h4>
119                 <dl>
120                 <dt>ft, filetype={filetype}</dt>
121                 <dd>A filetype to use for highlighting, see FILETYPES</dd>
122                 <dt>bg, background={light|dark}</dt>
123                 <dd>Background color to use for the page</dd>
124                 <dt>et, expandtab</dt>
125                 <dd>Expand tabs to spaces</dd>
126                 <dt>ts, tabstop=[N]</dt>
127                 <dd>Number of spaces to use for tabs when <b>et</b> is set</dd>
128                 <dt>...</dt>
129                 <dd>See :help modeline for more information</dd>
130                 </dl>
131
132                 <h4>FILETYPES</h4>
133                 <blockquote><u>[+]</u> $filetypes</blockquote>
134
135                 <h4>SOURCE</h4>
136                 <ul>
137                 <li><a href="vpaste?ft=sh">vpaste</a>
138                 <li><a href="index.cgi?ft=sh">index.cgi</a>
139                 <li><a href="vimhi.sh?ft=sh">vimhi.sh</a>
140                 <li><a href="vimrc?ft=vim">vimrc</a>
141                 <li><a href="htaccess?ft=apache">htaccess</a>
142                 <li><a href="2html-et.patch?ft=diff">2html-et.patch</a>
143                 </ul>
144
145                 <h4>LATEST UPLOADS</h4>
146                 <ul>$(for uri in ${uploads[@]}; do
147                         echo "<li><a href='$uri'>$uri</a>"
148                 done)</ul>
149         </body>
150 </html>
151 EOF
152 }
153
154 # Main
155 pathinfo="${SCRIPT_URL/*vpaste\/}"
156 boundary="${CONTENT_TYPE/*boundary\=/}"
157
158 if [ "$pathinfo" ]; then
159         do_print "$pathinfo"
160 elif [ "$CONTENT_TYPE" ]; then
161         do_upload "$boundary"
162 else
163         do_help
164 fi