Change soft-fail to use the config, rather than env
[rbx.git] / tools / rubuildius / bin / nopaste
blobc6dc2657581c927717563410047b3a982098fe5f
1 #!/usr/bin/ruby -w
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
10 require 'cgi'
11 require 'net/http'
12 require 'optparse'
13 require 'ostruct'
14 require 'uri'
16 class CmdLine
17 def self.parse(args)
18 options = OpenStruct.new
19 options.lang = 'Plain Text'
21 opts = OptionParser.new do |opts|
22 opts.banner = "Usage: nopaste [options]"
23 opts.separator ""
24 opts.separator "Options:"
26 opts.on("-l", "--language LANG",
27 "set language (defaults to \"Plain Text\")") do |x|
28 options.lang = x
29 end
31 opts.on("-d", "--description DESCRIPTION",
32 "set description (defaults to \"stdin\" or filename)") do |x|
33 options.desc = x
34 end
36 opts.on("-n", "--nick NICK",
37 "set nick (defaults to your username)") do |x|
38 options.nick = x
39 end
41 opts.on("-t", "--txturl",
42 "return url for the plain text file") do
43 options.txturl = true
44 end
46 opts.on("-x", "--xcut",
47 "nopaste from X selection (using xclip or xcut)") do
48 options.xcut = true
49 end
51 opts.on("--debug",
52 "enable debug output") do
53 options.debug = true
54 end
56 opts.on_tail("--help", "show this help") do
57 puts opts
58 exit
59 end
61 opts.on_tail("--version", "show version information") do
62 puts "nopaste " + $version
63 exit
64 end
65 end
67 opts.parse!(args)
68 options
69 end
70 end
72 def e(s)
73 return $zero+" error: "+s
74 end
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,
80 [ "cvt_tabs=No",
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' })
87 if $options.debug
88 STDERR.puts response.inspect
89 response.each_header {|k,v| STDERR.printf "%s: %s\n", k, v}
90 STDERR.puts response.body
91 end
93 case response
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")
97 else
98 u = $url.merge(response['location'])
99 u.path = u.path.sub(/html$/, 'txt') if $options.txturl
100 return u
102 when Net::HTTPSuccess
103 # strange
104 return e("rafb.net sent an unexpected HTML response")
105 else
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)
118 urls = []
120 if $options.desc.to_s.empty?
121 if $options.xcut
122 $options.desc = 'xcut'
123 elsif ARGV.empty?
124 $options.desc = 'stdin'
125 else
126 $options.desc = ARGV[0]
130 if $options.nick.to_s.empty?
131 $options.nick = ENV['USER'] || 'unknown'
134 begin
135 if $options.xcut
136 buf = %x{xclip -o 2>/dev/null || xcut -p 2>/dev/null}
137 urls << nopaste($options.nick, $options.desc, buf, $options.lang)
138 elsif ARGV.empty?
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?
143 begin
144 IO.popen("xclip 2>/dev/null", "w") {|p| p.print urls.map {|u| u.to_s}.join(' ') }
145 rescue Errno::EPIPE
146 begin
147 IO.popen("xcut 2>/dev/null", "w") {|p| p.print urls.map {|u| u.to_s}.join(' ') }
148 rescue Errno::EPIPE
152 rescue
153 puts urls # whatever we've accumulated already
154 puts e("something bad happened, stack trace follows")
155 raise
158 puts urls
159 exit 1 if urls.join("\n") =~ /^(?!http:)/