pastr_it 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. data/bin/pastr-it +14 -0
  2. data/lib/pastr_it.rb +109 -0
  3. metadata +66 -0
data/bin/pastr-it ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+ begin
3
+ require "pastr_it"
4
+ rescue LoadError
5
+ require "rubygems"
6
+ begin
7
+ require "pastr_it"
8
+ rescue LoadError
9
+ $:.unshift(File.dirname(__FILE__) + "/../lib")
10
+ require "pastr_it"
11
+ end
12
+ end
13
+
14
+ PastrIt.pastr_it(ARGV)
data/lib/pastr_it.rb ADDED
@@ -0,0 +1,109 @@
1
+ require "optparse"
2
+ class PastrIt
3
+ VERSION = '0.1.2'
4
+ REALM = 'Pastr Registered User'
5
+ PasteLink = "http://pastr.it/new"
6
+ attr_accessor :password, :filename, :title, :network, :channel, :language, :username
7
+ def initialize(args = nil)
8
+ @network, @username = "Freenode", ENV["USER"]
9
+ @args = args || ARGV
10
+ parse_args
11
+ end
12
+
13
+ def parse_args
14
+ return @opts if @opts
15
+ @opts = OptionParser.new
16
+ @opts.banner = "\nUsage: pastr-it [options]\n"
17
+ @opts.on("-u", "--username USERNAME", "Your username (Default: #{@username})") { |foo| @username = foo }
18
+ @opts.on("-c", "--channel CHANNEL", "IRC Channel for this Pastr (Default: same as username)") { |foo| @channel = foo }
19
+ @opts.separator "\tWhen using your username as the channel argument, the paste will be private"
20
+ @opts.on("-n", "--network NETWORK", "IRC Network for this Pastr (Default: #{network})") { |foo| @network = foo }
21
+ @opts.on("-l", "--language LANGUAGE", "Language to use for syntax highlighting") { |foo| @language = foo }
22
+ @opts.on("-t", "--title TITLE", "Title of this paste (Default: 'Pastr by #{username}')") { |foo| @title = foo }
23
+ @opts.on("-f", "--file FILENAME", "Read paste_body from FILENAME (otherwise reads from stdin)") { |foo| @filename = foo }
24
+ @opts.separator "\tTo paste from STDIN (instead of a file), you need to auth with a ~/.netrc"
25
+
26
+ @opts.on_tail("-h", "--help", "Show this message") do
27
+ puts @opts
28
+ exit
29
+ end
30
+ @opts.parse!(@args)
31
+ @channel ||= @username
32
+ if @filename.nil? and STDIN.isatty
33
+ $stderr.puts "No Input on STDIN and no filename given, what am I supposed to paste?"
34
+ exit 1
35
+ end
36
+ end
37
+
38
+ def pastr_it
39
+ if File.file?(netrc = ENV["HOME"] + "/.netrc")
40
+ if p_auth = File.readlines(netrc).detect { |line| line.match(/^machine\s+pastr\.it/) }
41
+ if uname = p_auth.match(/login\s+(\S+)/)
42
+ unless @username != ENV["USER"]
43
+ puts "Using username from ~/.netrc" if $DEBUG
44
+ @username = uname[1]
45
+ end
46
+ end
47
+ if pwd = p_auth.match(/password\s+(\S+)/) and uname[1] == @username
48
+ unless @password
49
+ puts "Using password from ~/.netrc" if $DEBUG
50
+ @password = pwd[1]
51
+ end
52
+ end
53
+ end
54
+ end
55
+ unless @password
56
+ if STDIN.isatty
57
+ begin
58
+ print "Enter Password: "
59
+ system("stty -echo")
60
+ @password = $stdin.readline.chomp
61
+ system("stty echo")
62
+ print "\n"
63
+ rescue
64
+ system("stty echo")
65
+ end
66
+ else
67
+ STDERR.puts "Error: STDIN is not a tty (you supplied your paste through STDIN instead of -f FILE)"
68
+ STDERR.puts "Piping only works when using a ~/.netrc to supply login info, as the Password prompt needs STDIN to be a tty"
69
+ exit 1
70
+ end
71
+ end
72
+ form = {'network' => network, 'channel' => channel, 'paste_body' => paste_body}
73
+ form["title"] = title if title
74
+ form["language"] = language if language
75
+ require "httpclient"
76
+ client = HTTPClient.new(ENV["HTTP_PROXY"])
77
+ # Have to do this to get a valid cookie with the server before we auth (lame)
78
+ res = client.get("http://pastr.it")
79
+ if res.status != 200
80
+ puts "Cannot access http://pastr.it. Webserver said Status: #{res.status} -> #{res.reason}"
81
+ exit 1
82
+ end
83
+ # Now set auth and post
84
+ client.set_auth(PasteLink, username.strip, password.strip)
85
+ res = client.post(PasteLink, form)
86
+ if res.status != 200
87
+ puts "An error occurred posting to#{PasteLink}. Webserver said Status: #{res.status} -> #{res.reason}"
88
+ exit 1
89
+ end
90
+ puts res.content
91
+ end
92
+
93
+ def paste_body
94
+ return @paste_body if @paste_body
95
+ if filename
96
+ raise "#{filename} does not exist or is not readable" unless File.file?(filename)
97
+ @paste_body = File.read(filename)
98
+ else
99
+ @paste_body = $stdin.read
100
+ end
101
+ end
102
+
103
+ def self.pastr_it(args)
104
+ me = PastrIt.new(args)
105
+ me.parse_args
106
+ me.pastr_it
107
+ end
108
+ end
109
+ #:et;ts=2;sw=2;foldmethod=indent
metadata ADDED
@@ -0,0 +1,66 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: pastr_it
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.2
5
+ platform: ruby
6
+ authors:
7
+ - Jayson Vaughn
8
+ - Michael Fellinger
9
+ - Kevin Berry
10
+ - TJ Vanderpoel
11
+ autorequire:
12
+ bindir: bin
13
+ cert_chain: []
14
+
15
+ date: 2009-04-09 00:00:00 -05:00
16
+ default_executable:
17
+ dependencies:
18
+ - !ruby/object:Gem::Dependency
19
+ name: httpclient
20
+ type: :runtime
21
+ version_requirement:
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: "0"
27
+ version:
28
+ description:
29
+ email: admins@rubyists.com
30
+ executables:
31
+ - pastr-it
32
+ extensions: []
33
+
34
+ extra_rdoc_files: []
35
+
36
+ files:
37
+ - lib/pastr_it.rb
38
+ - bin/pastr-it
39
+ has_rdoc: false
40
+ homepage: http://code.rubyists.com/projects/pastrtoo
41
+ post_install_message: "\n See pastr-it -h for usage. \n A pastr.it account is required for use. \n You can register for an account by messaging Pastr on Freenode:\n /msg Pastr .register <somepassword>\n "
42
+ rdoc_options: []
43
+
44
+ require_paths:
45
+ - lib
46
+ required_ruby_version: !ruby/object:Gem::Requirement
47
+ requirements:
48
+ - - ">="
49
+ - !ruby/object:Gem::Version
50
+ version: "0"
51
+ version:
52
+ required_rubygems_version: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ version: "0"
57
+ version:
58
+ requirements: []
59
+
60
+ rubyforge_project:
61
+ rubygems_version: 1.2.0
62
+ signing_key:
63
+ specification_version: 2
64
+ summary: A command line program to paste text to http://pastr.it
65
+ test_files: []
66
+