net-netrc-0.2.2/0000775000175000017500000000000011747551435012130 5ustar sugisuginet-netrc-0.2.2/metadata.yml0000664000175000017500000000242711747551435014440 0ustar sugisugi--- !ruby/object:Gem::Specification name: net-netrc version: !ruby/object:Gem::Version version: 0.2.2 platform: ruby authors: - Bob Showalter autorequire: net/netrc bindir: bin cert_chain: [] date: 2009-09-23 00:00:00 -04:00 default_executable: dependencies: - !ruby/object:Gem::Dependency name: Platform type: :runtime version_requirement: version_requirements: !ruby/object:Gem::Requirement requirements: - - ">=" - !ruby/object:Gem::Version version: 0.3.0 version: description: email: showaltb@gmail.com executables: [] extensions: [] extra_rdoc_files: [] files: - lib/net/ftp-netrc.rb - lib/net/netrc.rb - test/test_netrc.rb has_rdoc: true homepage: http://net-netrc.rubyforge.org licenses: [] post_install_message: rdoc_options: [] require_paths: - lib required_ruby_version: !ruby/object:Gem::Requirement requirements: - - ">=" - !ruby/object:Gem::Version version: "0" version: required_rubygems_version: !ruby/object:Gem::Requirement requirements: - - ">=" - !ruby/object:Gem::Version version: "0" version: requirements: [] rubyforge_project: rubygems_version: 1.3.5 signing_key: specification_version: 3 summary: Net::Netrc provides ftp(1)-style .netrc parsing test_files: - test/test_netrc.rb net-netrc-0.2.2/test/0000775000175000017500000000000011747551435013107 5ustar sugisuginet-netrc-0.2.2/test/test_netrc.rb0000644000175000017500000000570311747551435015611 0ustar sugisugi$:.unshift File.join(File.dirname(__FILE__), "../lib") require 'net/netrc' require 'test/unit' class TestNetrc < Test::Unit::TestCase if Net::Netrc::IS_WIN32 SAMPLE_NETRC = File.join(File.dirname(__FILE__), '_netrc') else SAMPLE_NETRC = File.join(File.dirname(__FILE__), '.netrc') end def setup @path = SAMPLE_NETRC File.open(@path, 'w') do |f| f << <NETRC is set, it is used # as the name of the .netrc file. Otherwise, a search # is made for .netrc (and _netrc on Windows) in the # following locations. The first existing file found # will be returned. # # - User's home directory as returned by Etc.getpwuid # - ENV['HOME'] directory # # On Windows platforms, the following additional locations # are checked: # - ENV['USERPROFILE'] # - ENV['HOMEPATH'] # - ENV['HOMEDRIVE'] + ENV['HOMEDIR'] def Netrc.rcname # use file indicated by NETRC environment variable if defined return ENV['NETRC'] if ENV['NETRC'] dirs = [] files = ['.netrc'] # build candidate list of directories to check pw = Etc.getpwuid dirs << pw.dir if pw dirs << ENV['HOME'] if IS_WIN32 dirs << ENV['USERPROFILE'] dirs << ENV['HOMESHARE'] dirs << ENV['HOMEDRIVE'] + ENV['HOMEPATH'] || '' if ENV['HOMEDRIVE'] files << '_netrc' end # return first found file dirs.compact.each do |dir| files.each do |file| name = File.join(dir, file) return name if File.exist?(name) end end # nothing found nil end # opens .netrc file, returning File object if successful. # +name+ is the name of the .netrc file to open. If omitted, # #rcname is used to locate the file. # # returns nil if the file does not exist. # # On non-Windows platforms, raises SecurityError if the file # is not owned by the current user or if it is readable or # writable by other than the current user. def Netrc.rcopen(name = nil) name ||= rcname or return nil return nil unless File.exist?(name) unless IS_WIN32 s = File.stat(name) raise SecurityError, "Not owner: #{name}" unless s.owned? raise SecurityError, "Bad permissions: #{name}" if s.mode & 077 != 0 end File.open(name, 'r') end # given a machine name, returns a Net::Netrc object containing # the matching entry for that name, or the default entry. If # no match is found and no default entry exists, nil is returned. # # The returned object's #machine, #login, #password, and #account # attributes will be set to the corresponding values from the .netrc file # entry. #machine will be nil if the +default+ .netrc entry was used. The # other attributes will be nil if the corresponding token in the .netrc # file was not present. # # +io+ is a previously-opened IO object. If not supplied, # #rcopen is called to locate and open the .netrc file. +io+ # will be closed when this method returns. def Netrc.locate(mach, io = nil) need_close = false if io.nil? io = rcopen or return nil need_close = true end entry = nil key = nil inmacdef = false begin while line = io.gets if inmacdef inmacdef = false if line.strip.empty? next end toks = line.scan(/"((?:\\.|[^"])*)"|((?:\\.|\S)+)/).flatten.compact toks.each { |t| t.gsub!(/\\(.)/, '\1') } while toks.length > 0 tok = toks.shift if key entry = new if key == 'machine' && tok == mach entry.send "#{key}=", tok if entry key = nil end case tok when 'default' return entry if entry entry = new when 'machine' return entry if entry key = 'machine' when 'login', 'password', 'account' key = tok when 'macdef' inmacdef = true break end end end ensure io.close if need_close end entry end end end net-netrc-0.2.2/lib/net/ftp-netrc.rb0000644000175000017500000000371011747551435015712 0ustar sugisugi# = net/ftp-netrc.rb - Net::FTP / Net::Netrc integration # # Copyright (c) 2005-2009 Bob Showalter # # This library is distributed under the terms of the Ruby license. # You may freely distribute or modify this library. # # This module extends the Net::FTP#login method to use Net::Netrc # to lookup login information if a nil username is passed. # # Example: # # require 'net/ftp-netrc' # (brings in net/ftp and net/netrc) # # ftp = Net::FTP.new('myhost') # ftp.login(nil) # ftp.last_response # => 230 User myuser logged in. require 'net/ftp' require 'net/netrc' module Net class FTP alias_method :orig_connect, :connect # :nodoc: alias_method :orig_login, :login # :nodoc: # cache host name for later use by login def connect(host, port = FTP_PORT) # :nodoc: @host = host orig_connect(host, port) end # # Logs in to the remote host. The session must have been previously # connected. # # If +user+ is nil, Net::Netrc#locate is used to lookup login information # based on the host name supplied when the connection was established. # # If +user+ is the string "anonymous" and the +password+ is nil, a password # of user@host is synthesized. If the +acct+ parameter is not nil, an FTP # ACCT command is sent following the successful login. Raises an exception # on error (typically Net::FTPPermError). # # Example: # # require 'net/ftp-netrc' # (brings in net/ftp and net/netrc) # # ftp = Net::FTP.new('myhost') # ftp.login(nil) # ftp.last_response # => 230 User myuser logged in. # def login(user = "anonymous", passwd = nil, acct = nil) if user.nil? rc = Net::Netrc.locate(@host) if rc user = rc.login passwd = rc.password acct = rc.account else user = '' passwd = '' end end orig_login(user, passwd, acct) end end end