net-netrc 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/lib/net/ftp-netrc.rb +76 -0
  2. data/lib/net/netrc.rb +71 -2
  3. metadata +3 -2
@@ -0,0 +1,76 @@
1
+ # = net/ftp-netrc.rb - Net::FTP / Net::Netrc integration
2
+ #
3
+ # Copyright (c) 2005 Robert J. Showalter
4
+ #
5
+ # This library is distributed under the terms of the Ruby license.
6
+ # You may freely distribute or modify this library.
7
+ #
8
+ # This module extends the Net::FTP#login method to use Net::Netrc
9
+ # to lookup login information if a nil username is passed.
10
+ #
11
+ # Example:
12
+ #
13
+ # require 'net/ftp-netrc' # (brings in net/ftp and net/netrc)
14
+ #
15
+ # ftp = Net::FTP.new('myhost')
16
+ # ftp.login(nil)
17
+ # ftp.last_response
18
+ # => 230 User myuser logged in.
19
+ #
20
+ # $Id: ftp-netrc.rb,v 1.1 2005/12/19 16:25:52 bshow Exp $
21
+
22
+ require 'net/ftp'
23
+ require 'net/netrc'
24
+
25
+ module Net
26
+
27
+ class FTP
28
+
29
+ alias_method :orig_connect, :connect # :nodoc:
30
+ alias_method :orig_login, :login # :nodoc:
31
+
32
+ # cache host name for later use by login
33
+ def connect(host, port = FTP_PORT) # :nodoc:
34
+ @host = host
35
+ orig_connect(host, port)
36
+ end
37
+
38
+ #
39
+ # Logs in to the remote host. The session must have been previously
40
+ # connected.
41
+ #
42
+ # If +user+ is nil, Net::Netrc#locate is used to lookup login information
43
+ # based on the host name supplied to Net::Netrc#connect.
44
+ #
45
+ # If +user+ is the string "anonymous" and the +password+ is nil, a password
46
+ # of user@host is synthesized. If the +acct+ parameter is not nil, an FTP
47
+ # ACCT command is sent following the successful login. Raises an exception
48
+ # on error (typically Net::FTPPermError).
49
+ #
50
+ # Example:
51
+ #
52
+ # require 'net/ftp-netrc' # (brings in net/ftp and net/netrc)
53
+ #
54
+ # ftp = Net::FTP.new('myhost')
55
+ # ftp.login(nil)
56
+ # ftp.last_response
57
+ # => 230 User myuser logged in.
58
+ #
59
+ def login(user = "anonymous", passwd = nil, acct = nil)
60
+ if user.nil?
61
+ rc = Net::Netrc.locate(@host)
62
+ if rc
63
+ user = rc.login
64
+ passwd = rc.password
65
+ acct = rc.account
66
+ else
67
+ user = ''
68
+ passwd = ''
69
+ end
70
+ end
71
+ orig_login(user, passwd, acct)
72
+ end
73
+
74
+ end
75
+
76
+ end
data/lib/net/netrc.rb CHANGED
@@ -7,16 +7,79 @@
7
7
  #
8
8
  # See Net::Netrc for usage.
9
9
  #
10
- # $Id: netrc.rb,v 1.1 2005/12/15 15:09:01 bshow Exp $
10
+ # $Id: netrc.rb,v 1.3 2005/12/19 16:42:01 bshow Exp $
11
11
 
12
12
  require 'etc'
13
13
 
14
14
  module Net
15
15
 
16
+ # Net::Netrc provides an interface to the ftp(1) .netrc file containing login
17
+ # information for FTP (or other) servers.
18
+ #
19
+ # == Example Usage
20
+ #
21
+ # require 'net/netrc'
22
+ #
23
+ # rc = Net::Netrc.locate('ftp.example.com') or
24
+ # raise ".netrc missing or no entry found"
25
+ # puts rc.login
26
+ # puts rc.password
27
+ # puts rc.name
28
+ #
29
+ # == The .netrc File
30
+ #
31
+ # The .netrc file is a plain text file containing login information. It is
32
+ # typically located in the user's home directory. (See #rcname for specific
33
+ # details on how the .netrc file is located.)
34
+ #
35
+ # On Unix platforms, the .netrc must be owned by the process' effective
36
+ # user id and must not be group- or world-writable, or a SecurityError
37
+ # will be raised.
38
+ #
39
+ # The .netrc file contains whitespace-separated tokens. Tokens containing
40
+ # whitespace must be enclosed in double quotes. The following tokens are
41
+ # recognized:
42
+ #
43
+ # [machine _name_]
44
+ # Identifies a remote machine name. #locate searches sequentially for
45
+ # a matching +machine+ token. Once a match is found, subsequent tokens
46
+ # are processed until either EOF is reached or another +machine+
47
+ # (or +default+) token is parsed.
48
+ #
49
+ # [login _name_]
50
+ # Identifies remote user name.
51
+ #
52
+ # [password _string_]
53
+ # Supplies remote password.
54
+ #
55
+ # [account _string_]
56
+ # Supplies an additional account password.
57
+ #
58
+ # [macdef _name_]
59
+ # Begins a macro definition, which ends with the next blank line
60
+ # encountered. Ignored by Net::Netrc.
61
+ #
62
+ # [default]
63
+ # Defines default account information. The login information here
64
+ # will be returned if a matching +machine+ token is not found
65
+ # during parsing. If supplied, +default+ must appear after any
66
+ # +machine+ entries.
67
+ #
68
+ # == Sample .netrc file
69
+ #
70
+ # The following is an example of a .netrc file:
71
+ #
72
+ # machine host1.austin.century.com
73
+ # login fred
74
+ # password bluebonnet
75
+ #
76
+ # default login john password ranger
77
+ #
78
+
16
79
  class Netrc
17
80
 
18
81
  VERSION_MAJOR = 0
19
- VERSION_MINOR = 1
82
+ VERSION_MINOR = 2
20
83
  VERSION_PATCH = 0
21
84
  VERSION = "#{VERSION_MAJOR}.#{VERSION_MINOR}.#{VERSION_PATCH}"
22
85
 
@@ -113,6 +176,12 @@ module Net
113
176
  # the matching entry for that name, or the default entry. If
114
177
  # no match is found and no default entry exists, nil is returned.
115
178
  #
179
+ # The returned object's #machine, #login, #password, and #account
180
+ # attributes will be set to the corresponding values from the .netrc file
181
+ # entry. #machine will be nil if the +default+ .netrc entry was used. The
182
+ # other attributes will be nil if the corresponding token in the .netrc
183
+ # file was not present.
184
+ #
116
185
  # +io+ is a previously-opened IO object. If not supplied,
117
186
  # #rcopen is called to locate and open the .netrc file. +io+
118
187
  # will be closed when this method returns.
metadata CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.8.11
3
3
  specification_version: 1
4
4
  name: net-netrc
5
5
  version: !ruby/object:Gem::Version
6
- version: 0.1.0
7
- date: 2005-12-15 00:00:00 -05:00
6
+ version: 0.2.0
7
+ date: 2005-12-19 00:00:00 -05:00
8
8
  summary: Net::Netrc provides ftp(1)-style .netrc parsing
9
9
  require_paths:
10
10
  - lib
@@ -31,6 +31,7 @@ authors:
31
31
  files:
32
32
  - lib/net
33
33
  - lib/net/netrc.rb
34
+ - lib/net/ftp-netrc.rb
34
35
  - test/test_netrc.rb
35
36
  test_files:
36
37
  - test/test_netrc.rb