#!/usr/bin/ruby -w
#
# nopaste -- quick script in the spirit of eatpaste, to generate nopaste urls.
# See http://www.rafb.net/paste/ for more information.
#
# Copyright 2005 Aron Griffis <aron@griffis1.net>
# Released under the GNU General Public License v2
#

require 'cgi'
require 'net/http'
require 'optparse'
require 'ostruct'
require 'uri'

$proxy = URI.parse(ENV['http_proxy'] || '')
$proxy_user, $proxy_pass = nil, nil
$proxy_user, $proxy_pass = $proxy.userinfo.split(/:/) if $proxy.userinfo
$url = URI.parse("http://www.rafb.net/paste/paste.php")
$version = '$Revision: 1992 $'.split(' ')[1]

class CmdLine
  def self.parse(args)
    options = OpenStruct.new
    options.lang = 'Plain Text'

    opts = OptionParser.new do |opts|
      opts.banner = "Usage: nopaste [options]"
      opts.separator ""
      opts.separator "Options:"

      opts.on("-l", "--language LANG", 
              "set language (defaults to \"Plain Text\")") do |x|
        options.lang = x
      end

      opts.on("-d", "--description DESCRIPTION", 
              "set description (defaults to \"stdin\" or filename)") do |x|
        options.desc = x
      end

      opts.on("-n", "--nick NICK", 
              "set nick (defaults to your username))") do |x|
        options.nick = x
      end

      opts.on("-x", "--xcut", 
              "nopaste from X selection (using xclip or xcut)") do
        options.xcut = true 
      end

      opts.on_tail("--help", "show this help") do
        puts opts
        exit 
      end

      opts.on_tail("--version", "show version information") do 
        puts "nopaste " + $version
        exit
      end
    end

    opts.parse!(args)
    options
  end
end

def nopaste(nick, desc, text, lang = "Plain Text")
  cxn = Net::HTTP::Proxy($proxy.host, $proxy.port, $proxy_user, $proxy_pass
                        ).start($url.host, $url.port) { |cxn|
    response = cxn.request_post($url.path, 
      [ "cvt_tabs=No",
        "lang=#{CGI::escape(lang)}",
        "nick=#{CGI::escape(nick)}",
        "desc=#{CGI::escape(desc)}",
        "text=#{CGI::escape(text)}" ].join('&'),
      { 'Content-Type' => 'application/x-www-form-urlencoded' })
    return $url.merge(response['location'])
  }
end

options = CmdLine.parse(ARGV)
urls = []

if options.desc.to_s.empty?
  if options.xcut
    options.desc = 'xcut'
  elsif ARGV.empty?
    options.desc = 'stdin'
  else
    options.desc = ARGV[0]
  end
end

if options.nick.to_s.empty?
  options.nick = ENV['USER'] || 'unknown'
end

if options.xcut
  buf = %x{xclip -o 2>/dev/null || xcut -p 2>/dev/null}
  urls << nopaste(options.nick, options.desc, buf, options.lang)
elsif ARGV.empty?
  urls << nopaste(options.nick, options.desc, gets(nil), options.lang)
end

urls << nopaste(options.nick, options.desc, gets(nil), options.lang) until ARGV.empty?
begin
  IO.popen("xclip 2>/dev/null", "w") {|p| p.print urls.map {|u| u.to_s}.join(' ') }
rescue Errno::EPIPE
  begin
    IO.popen("xcut 2>/dev/null", "w") {|p| p.print urls.map {|u| u.to_s}.join(' ') }
  rescue Errno::EPIPE
  end
end

if urls.to_s == "http://www.rafb.net/paste/toofast.html"
  puts 'You must wait at least 10 seconds between each paste! Try again in 10 seconds.'
  exit 1
end

puts urls
