3 # nopaste -- quick script in the spirit of eatpaste, to generate nopaste urls.
4 # See http://www.rafb.net/paste/ for more information.
6 # Copyright 2005,2007 Aron Griffis <agriffis n01se.net>
7 # Released under the GNU General Public License v2
18 options
= OpenStruct
.new
19 options
.lang
= 'Plain Text'
21 opts
= OptionParser
.new
do |opts
|
22 opts
.banner
= "Usage: nopaste [options]"
24 opts
.separator
"Options:"
26 opts
.on("-l", "--language LANG",
27 "set language (defaults to \"Plain Text\")") do |x
|
31 opts
.on("-d", "--description DESCRIPTION",
32 "set description (defaults to \"stdin\" or filename)") do |x
|
36 opts
.on("-n", "--nick NICK",
37 "set nick (defaults to your username)") do |x
|
41 opts
.on("-t", "--txturl",
42 "return url for the plain text file") do
46 opts
.on("-x", "--xcut",
47 "nopaste from X selection (using xclip or xcut)") do
52 "enable debug output") do
56 opts
.on_tail("--help", "show this help") do
61 opts
.on_tail("--version", "show version information") do
62 puts
"nopaste " + $version
73 return $zero+" error: "+s
76 def nopaste(nick
, desc
, text
, lang
= "Plain Text")
77 cxn
= Net
::HTTP::Proxy($proxy.host
, $proxy.port
, $proxy_user, $proxy_pass
78 ).start($url.host
, $url.port
) { |cxn
|
79 response
= cxn
.request_post($url.path
,
81 "lang=#{CGI::escape(lang)}",
82 "nick=#{CGI::escape(nick)}",
83 "desc=#{CGI::escape(desc)}",
84 "text=#{CGI::escape(text)}" ].join('&'),
85 { 'Content-Type' => 'application/x-www-form-urlencoded' })
88 STDERR.puts response
.inspect
89 response
.each_header
{|k
,v
| STDERR.printf
"%s: %s\n", k
, v
}
90 STDERR.puts response
.body
94 when Net
::HTTPRedirection
95 if response
['location'] =~
/toofast.html/
96 return e("rafb.net says you're pasting too fast. Try again in 10 seconds")
98 u
= $url.merge(response
['location'])
99 u
.path
= u
.path
.sub(/html$/, 'txt') if $options.txturl
102 when Net
::HTTPSuccess
104 return e("rafb.net sent an unexpected HTML response")
106 return e("rafb.net says #{response.code} #{response.message}")
111 $proxy = URI
.parse(ENV['http_proxy'] || '')
112 $proxy_user, $proxy_pass = nil, nil
113 $proxy_user, $proxy_pass = $proxy.userinfo
.split(/:/) if $proxy.userinfo
114 $url = URI
.parse("http://www.rafb.net/paste/paste.php")
115 $version = '$Revision: 2835 $'.split(' ')[1]
116 $zero = $0.sub(/.*\//, '')
117 $options = CmdLine
.parse(ARGV)
120 if $options.desc
.to_s
.empty
?
122 $options.desc
= 'xcut'
124 $options.desc
= 'stdin'
126 $options.desc
= ARGV[0]
130 if $options.nick
.to_s
.empty
?
131 $options.nick
= ENV['USER'] || 'unknown'
136 buf
= %x{xclip -o 2>/dev/null || xcut -p 2>/dev/null}
137 urls << nopaste($options.nick, $options.desc, buf, $options.lang)
139 urls << nopaste($options.nick, $options.desc, gets(nil), $options.lang)
142 urls << nopaste($options.nick, $options.desc, gets(nil), $options.lang) until ARGV.empty?
144 IO.popen("xclip 2>/dev/null", "w") {|p| p.print urls.map {|u| u.to_s}.join(' ') }
147 IO.popen("xcut 2>/dev/null", "w") {|p| p.print urls.map {|u| u.to_s}.join(' ') }
153 puts urls # whatever we've accumulated already
154 puts e("something bad happened, stack trace follows")
159 exit 1 if urls.join("\n") =~ /^(?!http:)/