ftp_paradise 1.3.8

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of ftp_paradise might be problematic. Click here for more details.

Files changed (118) hide show
  1. checksums.yaml +7 -0
  2. data/README.md +309 -0
  3. data/bin/create_remote_directory +9 -0
  4. data/bin/ftp_get +17 -0
  5. data/bin/ftp_upload +42 -0
  6. data/bin/ftp_upload_binary +18 -0
  7. data/bin/iftp +7 -0
  8. data/bin/remote_remove +28 -0
  9. data/doc/README.gen +292 -0
  10. data/doc/TODO_FOR_FTP_PARADISE_PROJECT.md +79 -0
  11. data/ftp_paradise.gemspec +114 -0
  12. data/lib/ftp_paradise.rb +5 -0
  13. data/lib/ftp_paradise/base/cliner.rb +21 -0
  14. data/lib/ftp_paradise/base/colours.rb +83 -0
  15. data/lib/ftp_paradise/base/prototype.rb +169 -0
  16. data/lib/ftp_paradise/base/reset.rb +29 -0
  17. data/lib/ftp_paradise/colours/colours.rb +141 -0
  18. data/lib/ftp_paradise/colours/use_colours.rb +74 -0
  19. data/lib/ftp_paradise/configuration/configuration.rb +49 -0
  20. data/lib/ftp_paradise/connection/README.md +1 -0
  21. data/lib/ftp_paradise/connection/connection.rb +35 -0
  22. data/lib/ftp_paradise/connection/constants.rb +46 -0
  23. data/lib/ftp_paradise/connection/data.rb +148 -0
  24. data/lib/ftp_paradise/connection/debug.rb +80 -0
  25. data/lib/ftp_paradise/connection/directory_handling.rb +271 -0
  26. data/lib/ftp_paradise/connection/do_login.rb +108 -0
  27. data/lib/ftp_paradise/connection/download.rb +86 -0
  28. data/lib/ftp_paradise/connection/file_handling.rb +172 -0
  29. data/lib/ftp_paradise/connection/ftp_object.rb +21 -0
  30. data/lib/ftp_paradise/connection/initialize.rb +86 -0
  31. data/lib/ftp_paradise/connection/initialize_a_new_net_ftp_object_with_this_url.rb +20 -0
  32. data/lib/ftp_paradise/connection/is_connected.rb +46 -0
  33. data/lib/ftp_paradise/connection/misc.rb +472 -0
  34. data/lib/ftp_paradise/connection/notify.rb +71 -0
  35. data/lib/ftp_paradise/connection/password.rb +47 -0
  36. data/lib/ftp_paradise/connection/port.rb +33 -0
  37. data/lib/ftp_paradise/connection/remote_pwd.rb +72 -0
  38. data/lib/ftp_paradise/connection/remote_url.rb +163 -0
  39. data/lib/ftp_paradise/connection/remove.rb +143 -0
  40. data/lib/ftp_paradise/connection/reset.rb +75 -0
  41. data/lib/ftp_paradise/connection/run.rb +18 -0
  42. data/lib/ftp_paradise/connection/set_array_available_hosts.rb +27 -0
  43. data/lib/ftp_paradise/connection/set_input.rb +18 -0
  44. data/lib/ftp_paradise/connection/show.rb +153 -0
  45. data/lib/ftp_paradise/connection/sync_ftp_object_onto_the_main_namespace.rb +24 -0
  46. data/lib/ftp_paradise/connection/transfer_mode.rb +162 -0
  47. data/lib/ftp_paradise/connection/upload.rb +253 -0
  48. data/lib/ftp_paradise/connection/use_default_dataset.rb +41 -0
  49. data/lib/ftp_paradise/connection/username.rb +42 -0
  50. data/lib/ftp_paradise/constants/constants.rb +19 -0
  51. data/lib/ftp_paradise/constants/misc.rb +57 -0
  52. data/lib/ftp_paradise/constants/namespace.rb +14 -0
  53. data/lib/ftp_paradise/constants/newline.rb +14 -0
  54. data/lib/ftp_paradise/constants/roebe.rb +27 -0
  55. data/lib/ftp_paradise/constants/roebe_ftp_constants.rb +219 -0
  56. data/lib/ftp_paradise/entry/entry.rb +293 -0
  57. data/lib/ftp_paradise/gui/gtk/constants.rb +58 -0
  58. data/lib/ftp_paradise/gui/gtk/ftp_bindings.rb +1149 -0
  59. data/lib/ftp_paradise/interactive_ftp/constants.rb +103 -0
  60. data/lib/ftp_paradise/interactive_ftp/directory_handling.rb +215 -0
  61. data/lib/ftp_paradise/interactive_ftp/help.rb +50 -0
  62. data/lib/ftp_paradise/interactive_ftp/initialize.rb +27 -0
  63. data/lib/ftp_paradise/interactive_ftp/interactive_ftp.rb +995 -0
  64. data/lib/ftp_paradise/interactive_ftp/main_loop.rb +50 -0
  65. data/lib/ftp_paradise/interactive_ftp/menu.rb +788 -0
  66. data/lib/ftp_paradise/interactive_ftp/misc.rb +208 -0
  67. data/lib/ftp_paradise/interactive_ftp/mode.rb +124 -0
  68. data/lib/ftp_paradise/interactive_ftp/readline.rb +115 -0
  69. data/lib/ftp_paradise/interactive_ftp/remove.rb +97 -0
  70. data/lib/ftp_paradise/interactive_ftp/reset.rb +90 -0
  71. data/lib/ftp_paradise/interactive_ftp/run.rb +22 -0
  72. data/lib/ftp_paradise/interactive_ftp/show.rb +184 -0
  73. data/lib/ftp_paradise/interactive_ftp/upload.rb +90 -0
  74. data/lib/ftp_paradise/interactive_ftp/user_input.rb +53 -0
  75. data/lib/ftp_paradise/project/project.rb +62 -0
  76. data/lib/ftp_paradise/requires/common_basic_requires.rb +13 -0
  77. data/lib/ftp_paradise/requires/common_external_requires.rb +9 -0
  78. data/lib/ftp_paradise/requires/require_the_constants.rb +7 -0
  79. data/lib/ftp_paradise/requires/require_the_ftp_paradise_project.rb +18 -0
  80. data/lib/ftp_paradise/requires/require_the_ftp_paradise_project_with_the_GUI_bindings.rb +10 -0
  81. data/lib/ftp_paradise/requires/require_the_toplevel_methods.rb +24 -0
  82. data/lib/ftp_paradise/toplevel_methods/can_connect_to_remote_site.rb +29 -0
  83. data/lib/ftp_paradise/toplevel_methods/clear_user_dataset.rb +28 -0
  84. data/lib/ftp_paradise/toplevel_methods/connect.rb +49 -0
  85. data/lib/ftp_paradise/toplevel_methods/create_file.rb +18 -0
  86. data/lib/ftp_paradise/toplevel_methods/data.rb +31 -0
  87. data/lib/ftp_paradise/toplevel_methods/delete.rb +23 -0
  88. data/lib/ftp_paradise/toplevel_methods/determine_user_dataset_from_this_hash.rb +37 -0
  89. data/lib/ftp_paradise/toplevel_methods/e.rb +16 -0
  90. data/lib/ftp_paradise/toplevel_methods/ftp_object.rb +270 -0
  91. data/lib/ftp_paradise/toplevel_methods/get_files.rb +24 -0
  92. data/lib/ftp_paradise/toplevel_methods/is_directory.rb +33 -0
  93. data/lib/ftp_paradise/toplevel_methods/is_on_roebe.rb +20 -0
  94. data/lib/ftp_paradise/toplevel_methods/login_name.rb +49 -0
  95. data/lib/ftp_paradise/toplevel_methods/opn.rb +24 -0
  96. data/lib/ftp_paradise/toplevel_methods/password.rb +48 -0
  97. data/lib/ftp_paradise/toplevel_methods/port.rb +41 -0
  98. data/lib/ftp_paradise/toplevel_methods/rds.rb +18 -0
  99. data/lib/ftp_paradise/toplevel_methods/remote_url.rb +57 -0
  100. data/lib/ftp_paradise/toplevel_methods/time.rb +45 -0
  101. data/lib/ftp_paradise/toplevel_methods/upload.rb +29 -0
  102. data/lib/ftp_paradise/toplevel_methods/upload_this_binary_file.rb +58 -0
  103. data/lib/ftp_paradise/version/version.rb +19 -0
  104. data/lib/ftp_paradise/www/public/css/style.css +3 -0
  105. data/lib/ftp_paradise/www/sinatra_web_interface.rb +242 -0
  106. data/lib/ftp_paradise/www/views/index.slim +3 -0
  107. data/lib/ftp_paradise/www/views/layout.slim +11 -0
  108. data/lib/ftp_paradise/www/web_interface.cgi +35 -0
  109. data/lib/ftp_paradise/yaml/automatically_connect_on_startup_of_the_interactive_ftp_shell.yml +1 -0
  110. data/lib/ftp_paradise/yaml/debug.yml +1 -0
  111. data/lib/ftp_paradise/yaml/open_in_default_editor.yml +1 -0
  112. data/lib/ftp_paradise/yaml/show_full_names.yml +1 -0
  113. data/lib/ftp_paradise/yaml/use_colours.yml +1 -0
  114. data/test/testing_ftp_paradise.rb +94 -0
  115. data/test/testing_minimal_pure_net_ftp_example_to_connect.rb +28 -0
  116. data/test/testing_the_ftp_connection_component.rb +70 -0
  117. data/test/testing_upload_a_local_directory.rb +10 -0
  118. metadata +315 -0
@@ -0,0 +1,86 @@
1
+ #!/usr/bin/ruby -w
2
+ # Encoding: UTF-8
3
+ # frozen_string_literal: true
4
+ # =========================================================================== #
5
+ # require 'ftp_paradise/connection/download.rb'
6
+ # =========================================================================== #
7
+ module FtpParadise
8
+
9
+ class Connection < Base
10
+
11
+ # ========================================================================= #
12
+ # === download_binary_file
13
+ #
14
+ # Use this method to download a binary file, by calling the method
15
+ # getbinaryfile(). You can download as many files as you want to
16
+ # through this method, but we expect this method to be a binary
17
+ # file.
18
+ #
19
+ # Specific usage example:
20
+ #
21
+ # ftp?.download_binary_file('photos_2009-03-29.zip', 'ftp_photos/photos.zip')
22
+ #
23
+ # ========================================================================= #
24
+ def download_binary_file(
25
+ i,
26
+ be_verbose = be_verbose?
27
+ )
28
+ if i.is_a? Array
29
+ i.each {|entry| download_binary_file(entry) }
30
+ else
31
+ ftp?.getbinaryfile(i) # Delegate towards .getbinaryfile()
32
+ if be_verbose
33
+ e "Downloaded file #{sfile(i)}."
34
+ end
35
+ end
36
+ end; alias getbinaryfile download_binary_file # === getbinaryfile
37
+
38
+ # ========================================================================= #
39
+ # === download_remote_file (download tag)
40
+ #
41
+ # Use this method to download a remote file. If you are sure to have
42
+ # a binary file, the method download_binary_file() could be used
43
+ # instead.
44
+ #
45
+ # gettextfile(remotefile, localfile = File.basename(remotefile)) {|line| ...}
46
+ # ========================================================================= #
47
+ def download_remote_file(
48
+ i, be_verbose = be_verbose?
49
+ )
50
+ if i.is_a? Array # Handle arrays first.
51
+ i.each {|entry| download_remote_file(entry, be_verbose) }
52
+ else
53
+ if i =~ /^\d+$/ # if is a number
54
+ update_file_listing
55
+ i = return_remote_files[i.to_i - 1].first
56
+ end
57
+ if i == '*' # Here we want to download all files, as we passed in '*'.
58
+ i = []
59
+ update_file_listing
60
+ return_remote_files.each { |a,b| i << FtpParadise.rds(a) }
61
+ end
62
+ if be_verbose
63
+ notify_the_user_about(i, __method__)
64
+ end
65
+ set_file(i) # This assigns to @internal_hash[:file].
66
+ begin # We can use @file because we called set_file() before.
67
+ ftp_object?.gettextfile(file?)
68
+ basename = File.basename(file?)
69
+ e "Finished downloading `#{sandybrown(file?)}`"
70
+ e 'into `'+sdir(rds(Dir.pwd+'/'+basename))+'`.'
71
+ return file?
72
+ rescue Net::FTPPermError => error
73
+ pp error
74
+ e 'An exception occurred in the method '+sfancy('download()')+'.'
75
+ e 'The error was a '+sfancy('Permission Error')+', the input '\
76
+ 'to the method was: `'+simp(i)+'`.'
77
+ e 'Is the file that you are trying to download really '\
78
+ 'existing on the remote server?'
79
+ return false
80
+ end
81
+ end
82
+ end; alias download download_remote_file # === download
83
+ alias download_this_remote_file download_remote_file # === download_this_remote_file
84
+ alias download_this_file download_remote_file # === download_this_file
85
+
86
+ end; end
@@ -0,0 +1,172 @@
1
+ #!/usr/bin/ruby -w
2
+ # Encoding: UTF-8
3
+ # frozen_string_literal: true
4
+ # =========================================================================== #
5
+ # require 'ftp_paradise/connection/file_handling.rb'
6
+ # =========================================================================== #
7
+ module FtpParadise
8
+
9
+ class Connection < Base
10
+
11
+ # ========================================================================= #
12
+ # === create_remote_file (touch tag)
13
+ #
14
+ # This "touches" a remote file.
15
+ # ========================================================================= #
16
+ def create_remote_file(
17
+ i,
18
+ be_verbose = be_verbose?
19
+ )
20
+ if i.is_a? Array
21
+ i.each {|entry| create_remote_file(entry, be_verbose) }
22
+ else
23
+ FileUtils.touch(i)
24
+ if be_verbose
25
+ notify_the_user_about(i, __method__)
26
+ end
27
+ upload_this_text_file(i, :be_quiet)
28
+ File.delete(i) if File.zero? i # And get rid of that file too.
29
+ end
30
+ end; alias touch create_remote_file # === touch
31
+
32
+ # ========================================================================= #
33
+ # === fancy_remote_listing
34
+ #
35
+ # This method will obtain the (remote) directory listing and then
36
+ # display it.
37
+ # ========================================================================= #
38
+ def fancy_remote_listing(
39
+ be_verbose = be_verbose?
40
+ )
41
+ update_file_listing
42
+ case be_verbose
43
+ when :be_silent,
44
+ :be_quiet
45
+ be_verbose = false
46
+ when :also_show_filesize
47
+ be_verbose = true
48
+ end
49
+ if be_verbose
50
+ e ('=' * 55)+' '+FtpParadise.yellow('REMOTE FILES')+' '+('=' * 12)
51
+ if remote_directory_is_empty?
52
+ e "Remote `#{sdir(remote_path?)}` is empty."
53
+ e 'No files or directories can be found therein.'
54
+ else
55
+ e 'Remote content of `'+sdir(remote_path?)+'` is:'+N+N
56
+ e 'Index Filename '\
57
+ ' Filesize'
58
+ show_remote_file_listing
59
+ end
60
+ cliner
61
+ end
62
+ return remote_directory_content
63
+ end
64
+
65
+ # ========================================================================= #
66
+ # === rename
67
+ #
68
+ # This method will attempt to rename a file on the remote server.
69
+ #
70
+ # Note that this is equivalent to moving a file, which is why the alias
71
+ # move_file can also be used. Any move-file actioni s simply a rename()
72
+ # action.
73
+ #
74
+ # The first argument passed to this method should be the name of an
75
+ # existing (remote) file. The second second argument should be the
76
+ # new name of that file, or its full (remote) path.
77
+ #
78
+ # Also note that it seems as if the FTP protocol requires the full
79
+ # target location, otherwise errors such as the following might
80
+ # happen:
81
+ #
82
+ # ftp.rb in `getresp': 451 Rename/move failure: Is a directory (Net::FTPTempError)
83
+ #
84
+ # The command used by .rename() command should be equivalent to this code:
85
+ #
86
+ # SITE mv oldpath newpath
87
+ #
88
+ # Documentation for the functionality can be found here:
89
+ #
90
+ # http://ruby-doc.org/stdlib/libdoc/net/ftp/rdoc/Net/FTP.html#method-i-rename
91
+ #
92
+ # ========================================================================= #
93
+ def rename(
94
+ from,
95
+ to,
96
+ be_verbose = be_verbose?
97
+ )
98
+ to = to.first if to.is_a? Array
99
+ if from.is_a? Array # Support batch-transfer.
100
+ from.each {|entry| rename(entry, to) }
101
+ else
102
+ from = rds(from)
103
+ to = rds(to)
104
+ begin
105
+ ftp_object?.rename(from, to)
106
+ if be_verbose
107
+ opnn; e "Renamed the remote entry `#{sfancy(from)}"\
108
+ "` to `#{sfancy(to)}`."
109
+ end
110
+ rescue Net::FTPTempError => error
111
+ e "An error (#{simp('Net::FTPTempError')}) occurred "\
112
+ "in the method #{simp('rename()')}:"
113
+ pp error
114
+ end
115
+ end
116
+ end; alias rename_file rename # === rename_file
117
+ alias move_file rename # === move_file
118
+ alias rename_remote_file rename # === rename_remote_file
119
+
120
+ # ========================================================================= #
121
+ # === update_remote_file_listing
122
+ # ========================================================================= #
123
+ def update_remote_file_listing(
124
+ be_verbose = be_verbose?
125
+ )
126
+ case be_verbose
127
+ when :be_quiet
128
+ be_verbose = false
129
+ end
130
+ if is_connected?
131
+ begin
132
+ # =================================================================== #
133
+ # Make use of a method that will skip a few entries, such as
134
+ # "." or "..".
135
+ # =================================================================== #
136
+ @internal_hash[:raw_entries] = return_slightly_sanitized_entries_ignoring_a_few
137
+ rescue Exception => error
138
+ opnn; e swarn('An error happened in the method '\
139
+ 'update_remote_file_listing(), at line: '+__LINE__.to_s+'.')
140
+ pp error.class
141
+ pp error
142
+ end
143
+ else
144
+ if be_verbose
145
+ opnn; e 'Can not update the remote file listing '\
146
+ 'because we are not connected.'
147
+ end
148
+ end
149
+ end; alias update_file_listing update_remote_file_listing # === update_file_listing
150
+ alias update_raw_listing update_remote_file_listing # === update_raw_listing
151
+ alias get_listing update_remote_file_listing # === get_listing
152
+ alias get_remote_listing update_remote_file_listing # === get_remote_listing
153
+ alias update update_remote_file_listing # === update
154
+
155
+ # ========================================================================= #
156
+ # === does_this_remote_file_exist?
157
+ # ========================================================================= #
158
+ def does_this_remote_file_exist?(i)
159
+ i = File.basename(i).strip
160
+ array = return_all_remote_files
161
+ if array.is_a?(Array) and
162
+ array.first.is_a?(String)
163
+ array.map! {|entry|
164
+ entry = FtpParadise::Entry.new(entry)
165
+ }
166
+ array.select! {|entry| entry.is_a_file? }
167
+ array.map! {|entry| entry.filename? }
168
+ end
169
+ return array.include?(i)
170
+ end
171
+
172
+ end; end
@@ -0,0 +1,21 @@
1
+ #!/usr/bin/ruby -w
2
+ # Encoding: UTF-8
3
+ # frozen_string_literal: true
4
+ # =========================================================================== #
5
+ # require 'ftp_paradise/connection/ftp_object.rb'
6
+ # =========================================================================== #
7
+ module FtpParadise
8
+
9
+ class Connection < FtpParadise::Base
10
+
11
+ # ========================================================================= #
12
+ # === ftp_object?
13
+ # ========================================================================= #
14
+ def ftp_object?
15
+ @ftp_object
16
+ end; alias net_ftp? ftp_object? # === net_ftp?
17
+ alias ftp? ftp_object? # === ftp?
18
+ alias ftp ftp_object? # === ftp?
19
+ alias ftp_object ftp_object? # === ftp_object
20
+
21
+ end; end
@@ -0,0 +1,86 @@
1
+ #!/usr/bin/ruby -w
2
+ # Encoding: UTF-8
3
+ # frozen_string_literal: true
4
+ # =========================================================================== #
5
+ # require 'ftp_paradise/connection/initialize.rb'
6
+ # =========================================================================== #
7
+ require 'ftp_paradise/base/prototype.rb'
8
+ require 'ftp_paradise/connection/constants.rb' # <- For the constant RUN_ALREADY
9
+ require 'ftp_paradise/connection/ftp_object.rb'
10
+ require 'ftp_paradise/connection/reset.rb'
11
+ require 'ftp_paradise/connection/run.rb'
12
+ require 'ftp_paradise/connection/set_input.rb'
13
+ require 'ftp_paradise/connection/remote_url.rb'
14
+ require 'ftp_paradise/connection/use_default_dataset.rb'
15
+
16
+ module FtpParadise
17
+
18
+ class Connection < FtpParadise::Base
19
+
20
+ # ========================================================================= #
21
+ # === initialize
22
+ #
23
+ # The first argument to this method should be the remote host's URL.
24
+ # ========================================================================= #
25
+ def initialize(
26
+ i = ARGV,
27
+ run_already = RUN_ALREADY
28
+ )
29
+ reset
30
+ # ======================================================================= #
31
+ # - If we pass in a Hash as the first argument, then we will use
32
+ # set_data(), otherwise we will set individually it all individually.
33
+ # ======================================================================= #
34
+ if i.is_a? Hash
35
+ set_data(i)
36
+ else
37
+ set_input(i)
38
+ end
39
+ case i
40
+ when :dont_run_yet,
41
+ :do_not_run_yet
42
+ run_already = false
43
+ end
44
+ case run_already
45
+ when :dont_run_yet
46
+ run_already = false
47
+ end
48
+ if run_already.is_a? Hash
49
+ _ = run_already
50
+ if _.has_key? :connect_to_this_host
51
+ set_this_host(
52
+ _.delete(:connect_to_this_host)
53
+ )
54
+ end
55
+ run_already = RUN_ALREADY # Restore to the default in this case.
56
+ end
57
+ if block_given?
58
+ yielded = yield
59
+ case yielded
60
+ # ===================================================================== #
61
+ # === :do_not_run_yet
62
+ # ===================================================================== #
63
+ when :do_not_run_yet
64
+ run_already = false
65
+ # ===================================================================== #
66
+ # The "default" dataset defaults to my personal FTP login dataset.
67
+ # ===================================================================== #
68
+ when :use_default_dataset,
69
+ :default_dataset,
70
+ :default
71
+ use_default_dataset
72
+ end
73
+ end
74
+ run if run_already
75
+ end
76
+
77
+ # ========================================================================= #
78
+ # === initialize_a_new_net_ftp_object_with_this_remote_url
79
+ #
80
+ # This method is the only one allowed to create a new Net::FTP instance.
81
+ # ========================================================================= #
82
+ def initialize_a_new_net_ftp_object_with_this_remote_url(i)
83
+ @ftp_object = Net::FTP.new(i) # ftp = Net::FTP.new(YAML.load_file(ENV['FTP_YAML_FILE'])['freehosting']['url'])
84
+ end; alias initialize_a_new_net_ftp_object initialize_a_new_net_ftp_object_with_this_remote_url # === initialize_a_new_net_ftp_object
85
+
86
+ end; end
@@ -0,0 +1,20 @@
1
+ #!/usr/bin/ruby -w
2
+ # Encoding: UTF-8
3
+ # frozen_string_literal: true
4
+ # =========================================================================== #
5
+ # require 'ftp_paradise/connection/initialize_a_new_net_ftp_object_with_this_url.rb'
6
+ # =========================================================================== #
7
+ module FtpParadise
8
+
9
+ class Connection < Base
10
+
11
+ require 'net/ftp'
12
+
13
+ # ========================================================================= #
14
+ # === initialize_a_new_net_ftp_object_with_this_url
15
+ # ========================================================================= #
16
+ def initialize_a_new_net_ftp_object_with_this_url(this_url)
17
+ @ftp_object = Net::FTP.new(this_url)
18
+ end
19
+
20
+ end; end
@@ -0,0 +1,46 @@
1
+ #!/usr/bin/ruby -w
2
+ # Encoding: UTF-8
3
+ # frozen_string_literal: true
4
+ # =========================================================================== #
5
+ # require 'ftp_paradise/connection/is_connected.rb'
6
+ # =========================================================================== #
7
+ module FtpParadise
8
+
9
+ class Connection < FtpParadise::Base
10
+
11
+ require 'ftp_paradise/connection/ftp_object.rb'
12
+
13
+ # ========================================================================= #
14
+ # === closed?
15
+ #
16
+ # Returns true if the connection to our remote host is closed.
17
+ # ========================================================================= #
18
+ def closed?
19
+ begin
20
+ ftp_object?.closed? if ftp_object?
21
+ rescue Net::FTPConnectionError
22
+ true
23
+ end
24
+ end
25
+
26
+ # ========================================================================= #
27
+ # === open?
28
+ #
29
+ # This method can also be used to determine whether we are still
30
+ # connected to the remote site.
31
+ #
32
+ # This method will return true if the connection is open.
33
+ #
34
+ # If you want to determine whether you are still connected to the
35
+ # remote host, you can alias use the alias are_we_connected? - it
36
+ # may read nicer.
37
+ # ========================================================================= #
38
+ def open?
39
+ !closed?
40
+ end; alias are_we_connected? open? # === are_we_connected?
41
+ alias is_connected? open? # === is_connected?
42
+ alias is_connected open? # === is_connected
43
+ alias connected? open? # === connected?
44
+ alias did_we_connect? open? # === did_we_connect?
45
+
46
+ end; end
@@ -0,0 +1,472 @@
1
+ #!/usr/bin/ruby -w
2
+ # Encoding: UTF-8
3
+ # frozen_string_literal: true
4
+ # =========================================================================== #
5
+ # require 'ftp_paradise/connection/misc.rb'
6
+ # =========================================================================== #
7
+ module FtpParadise
8
+
9
+ class Connection < Base
10
+
11
+ require 'ftp_paradise/connection/directory_handling.rb'
12
+ require 'ftp_paradise/connection/download.rb'
13
+ require 'ftp_paradise/toplevel_methods/clear_user_dataset.rb'
14
+
15
+ # ========================================================================= #
16
+ # === dataset?
17
+ # ========================================================================= #
18
+ def dataset?
19
+ FtpParadise.data?
20
+ end
21
+
22
+ # ========================================================================= #
23
+ # === raw_list
24
+ # ========================================================================= #
25
+ def raw_list(i = '*')
26
+ ftp_object?.list(i)
27
+ end
28
+
29
+ # ========================================================================= #
30
+ # === return_slightly_sanitized_entries_ignoring_a_few
31
+ # ========================================================================= #
32
+ def return_slightly_sanitized_entries_ignoring_a_few(
33
+ i = '*'
34
+ )
35
+ ftp_object?.list(i).reject {|entry|
36
+ entry.to_s.strip.empty? or entry.end_with?(' .') or entry.end_with?(' ..')
37
+ }.map {|line|
38
+ if line.end_with? ':' # <- Some remote FTP-hosts use ':' for a directory.
39
+ line[-1,1] = '/'
40
+ end
41
+ line
42
+ }
43
+ end; alias list return_slightly_sanitized_entries_ignoring_a_few # === list
44
+
45
+ # ========================================================================= #
46
+ # === nlst?
47
+ #
48
+ # Returns an array of filenames in the remote directory. This will
49
+ # yield less information than the "ls -l" variant, aka list().
50
+ #
51
+ # Documentation:
52
+ # http://ruby-doc.org/stdlib/libdoc/net/ftp/rdoc/Net/FTP.html#method-i-nlst
53
+ # ========================================================================= #
54
+ def nlst?
55
+ ftp_object?.nlst
56
+ end; alias nlst nlst? # === nlst
57
+
58
+ # ========================================================================= #
59
+ # === list? (list tag)
60
+ #
61
+ # Returns an array of file information in the directory (the output
62
+ # is like `ls -l`). If a block is given, it iterates through the
63
+ # listing.
64
+ # ========================================================================= #
65
+ def list?(i = '*')
66
+ # ======================================================================= #
67
+ # We will also sanitize this a bit.
68
+ # ======================================================================= #
69
+ @internal_hash[:raw_entries] = return_slightly_sanitized_entries_ignoring_a_few
70
+ return @internal_hash[:raw_entries] # And return the result here.
71
+ end; alias ls list? # === ls
72
+ alias populate_raw_entries list? # === populate_raw_entries
73
+
74
+ # ========================================================================= #
75
+ # === passive=
76
+ # ========================================================================= #
77
+ def passive=(i = true)
78
+ if ftp_object?
79
+ ftp_object?.passive = i
80
+ else
81
+ e 'No ftp-object has been instantiated yet.'
82
+ end
83
+ end
84
+
85
+ # ========================================================================= #
86
+ # === list_content (list tag, ls tag)
87
+ #
88
+ # List available data.
89
+ # ========================================================================= #
90
+ def list_content
91
+ report_current_remote_dir
92
+ result = list?
93
+ cliner { pp result }
94
+ _ = result.first[0, 1]
95
+ case _
96
+ when 'd'
97
+ e "Directory #{result.first}"
98
+ # @array_directories
99
+ when 'l'
100
+ e "Link #{result.first}"
101
+ else
102
+ e "File #{result.first}"
103
+ end
104
+ end; alias do_list_content list_content # === list
105
+ alias listing? list_content # === listing?
106
+
107
+ # ========================================================================= #
108
+ # === file_listing_as_entries?
109
+ # ========================================================================= #
110
+ def file_listing_as_entries?
111
+ _ = remote_file_listing?
112
+ if _
113
+ return _.map {|entry| FtpParadise::Entry.new(entry) }
114
+ end
115
+ return nil
116
+ end
117
+
118
+ # ========================================================================= #
119
+ # === empty?
120
+ # ========================================================================= #
121
+ def empty?
122
+ array_remote_files.empty?
123
+ end
124
+
125
+ # ========================================================================= #
126
+ # === find_matches_for
127
+ #
128
+ # This method will return an array of files and directories that match
129
+ # to the provided input (which we will assume to be a query).
130
+ # ========================================================================= #
131
+ def find_matches_for(i)
132
+ i = i.delete '*'
133
+ update_file_listing
134
+ _ = return_remote_directory_content.map(&:first)
135
+ _ = _.grep(/#{i}/)
136
+ return _
137
+ end
138
+
139
+ # ========================================================================= #
140
+ # === output_sorted_by_time
141
+ #
142
+ # This method will output the remote content in a time-sorted manner.
143
+ # ========================================================================= #
144
+ def output_sorted_by_time
145
+ _ = sanitized_remote_directory_content?
146
+ sorted_result = _.sort_by {|array|
147
+ array.last
148
+ }.reverse
149
+ sorted_result.each_with_index {|array, index| index += 1
150
+ file_or_directory = array[1]
151
+ name_of_the_file = array.first
152
+ if file_or_directory == 'directory'
153
+ name_of_the_file << '/' unless name_of_the_file.end_with? '/'
154
+ end
155
+ index = simp( (index.to_s+') ').rjust(6))
156
+ e index+sfancy(name_of_the_file)
157
+ }
158
+ end
159
+
160
+ # ========================================================================= #
161
+ # === last_response
162
+ #
163
+ # The server's last response is available through this method here.
164
+ # ========================================================================= #
165
+ def last_response
166
+ ftp_object?.last_response
167
+ end; alias last_response? last_response # === last_response?
168
+ alias last_server_response? last_response # === last_server_response?
169
+
170
+ # ========================================================================= #
171
+ # === raw_entries?
172
+ # ========================================================================= #
173
+ def raw_entries?
174
+ @internal_hash[:raw_entries]
175
+ end; alias remote_file_listing? raw_entries? # === remote_file_listing?
176
+ alias file_listing? raw_entries? # === file_listing?
177
+ alias raw_listing? raw_entries? # === raw_listing?
178
+
179
+ # ========================================================================= #
180
+ # === return_all_remote_files
181
+ #
182
+ # This method will return an Array with all remote files.
183
+ # ========================================================================= #
184
+ def return_all_remote_files
185
+ results = nil
186
+ if file_listing?
187
+ results = file_listing?.select {|entry|
188
+ if entry.is_a?(String) and !entry.empty?
189
+ entry = FtpParadise::Entry.new(entry)
190
+ end
191
+ entry.is_a_file?
192
+ }
193
+ end
194
+ return results
195
+ end; alias remote_file_listing? return_all_remote_files # === remote_file_listing?
196
+ alias all_remote_files? return_all_remote_files # === all_remote_files?
197
+ alias array_remote_files return_all_remote_files # === array_remote_files
198
+ alias return_remote_files return_all_remote_files # === return_remote_files
199
+ alias return_remote_file_listing return_all_remote_files # === return_remote_file_listing
200
+ alias remote_files? return_all_remote_files # === remote_files
201
+ alias files? return_all_remote_files # === files?
202
+
203
+ # ========================================================================= #
204
+ # === status?
205
+ #
206
+ # Report the status here. This is equivalent to the STAT command.
207
+ # ========================================================================= #
208
+ def status?
209
+ ftp_object?.status.to_s
210
+ end; alias status status? # === status
211
+
212
+ # ========================================================================= #
213
+ # === return_local_directories
214
+ #
215
+ # Return all local directories.
216
+ # ========================================================================= #
217
+ def return_local_directories
218
+ Dir['*'].select {|entry| File.directory?(entry) }
219
+ end
220
+
221
+ # ========================================================================= #
222
+ # === modification_time_of?
223
+ #
224
+ # This method will give us the last modification time of a remote
225
+ # file, by issuing the mdtm() command.
226
+ #
227
+ # The format returned will be in YYYMMDDhhmmss.
228
+ #
229
+ # If you require a parsed Time instance, you can use mtime()
230
+ # on the ftp object.
231
+ #
232
+ # Symbols could also be used, such as "pp modification_time_of? :a".
233
+ # ========================================================================= #
234
+ def modification_time_of?(i)
235
+ if i.is_a? Array
236
+ i.each {|entry| modification_time_of(entry) } # <- Recursive call.
237
+ else
238
+ ftp_object?.mdtm(i.to_s)
239
+ end
240
+ end; alias mdtm modification_time_of? # === mdtm
241
+
242
+ # ========================================================================= #
243
+ # === set_file
244
+ #
245
+ # Use only this to modify the @file variable.
246
+ # ========================================================================= #
247
+ def set_file(i = nil)
248
+ @internal_hash[:file] = i
249
+ end
250
+
251
+ # ========================================================================= #
252
+ # === is_file?
253
+ #
254
+ # This method will check whether the input is a file or not.
255
+ # It will call the method .is_directory?()
256
+ # ========================================================================= #
257
+ def is_file?(i)
258
+ !is_a_directory?(i)
259
+ end
260
+
261
+ # ========================================================================= #
262
+ # === logged_in?
263
+ # ========================================================================= #
264
+ def logged_in?
265
+ ftp_object?.instance_variable_get '@logged_in'
266
+ end
267
+
268
+ # ========================================================================= #
269
+ # === available_hosts?
270
+ # ========================================================================= #
271
+ def available_hosts?
272
+ @internal_hash[:array_available_hosts]
273
+ end; alias array_available_hosts available_hosts? # === array_available_hosts
274
+ alias array_available_hosts? available_hosts? # === array_available_hosts?
275
+
276
+ # ========================================================================= #
277
+ # === open_timeout?
278
+ # ========================================================================= #
279
+ def open_timeout?
280
+ ftp_object?.open_timeout.to_s if ftp_object?.respond_to? :open_timeout
281
+ end
282
+
283
+ # ========================================================================= #
284
+ # === feedback_login_hosts
285
+ #
286
+ # Feedback all available login hosts, based on the information stored
287
+ # in the Array @array_available_hosts.
288
+ # ========================================================================= #
289
+ def feedback_login_hosts
290
+ opnn; e 'The followings hosts are easily available:'
291
+ e
292
+ available_hosts?.each { |host| e " - #{sfancy(host)}" }
293
+ e
294
+ end
295
+
296
+ # ========================================================================= #
297
+ # === help?
298
+ # ========================================================================= #
299
+ def help?
300
+ ftp_object?.help
301
+ end; alias help help? # === help
302
+
303
+ # ========================================================================= #
304
+ # === close (close tag)
305
+ #
306
+ # Close the connection here.
307
+ #
308
+ # Further operations will be impossible until a new connection
309
+ # is opened up again, via connect().
310
+ # ========================================================================= #
311
+ def close
312
+ ftp_object?.close
313
+ end
314
+
315
+ # ========================================================================= #
316
+ # === mlsd
317
+ # ========================================================================= #
318
+ def mlsd(i)
319
+ ftp_object?.mlsd(i)
320
+ end
321
+
322
+ # ========================================================================= #
323
+ # === site
324
+ # ========================================================================= #
325
+ def site(i = nil)
326
+ ftp_object?.site(i)
327
+ end
328
+
329
+ # ========================================================================= #
330
+ # === file?
331
+ # ========================================================================= #
332
+ def file?
333
+ @internal_hash[:file]
334
+ end
335
+
336
+ # ========================================================================= #
337
+ # === system_command
338
+ #
339
+ # This will return system information from the remote host.
340
+ #
341
+ # Stuff like:
342
+ # UNIX Type: L8
343
+ # ========================================================================= #
344
+ def system_command
345
+ ftp_object?.system
346
+ end; alias system system_command # === system
347
+
348
+ # ========================================================================= #
349
+ # === report_host_port_user_name_and_password
350
+ # ========================================================================= #
351
+ def report_host_port_user_name_and_password
352
+ ljust = 15
353
+ e '- Now trying to log in to '+simp(host?)+
354
+ ' (Port: '+swarn(port?)+') using:'
355
+ e ' '+('Login Name: ').ljust(ljust)+sfancy(user_name?)
356
+ e ' '+('Password: ').ljust(ljust)+sfancy(password?)
357
+ end
358
+
359
+ # ========================================================================= #
360
+ # === opnn
361
+ # ========================================================================= #
362
+ def opnn
363
+ super(NAMESPACE)
364
+ end
365
+
366
+ # ========================================================================= #
367
+ # === read_timeout=
368
+ #
369
+ # Setter-method for the #read_timeout attribute.
370
+ # ========================================================================= #
371
+ def read_timeout=(i)
372
+ ftp_object?.read_timeout = i
373
+ end
374
+
375
+ # ========================================================================= #
376
+ # === size?
377
+ #
378
+ # This method will return the size of the given (remote) filename.
379
+ # ========================================================================= #
380
+ def size?(i)
381
+ ftp_object?.size(i)
382
+ end; alias size size? # === size
383
+
384
+ # ========================================================================= #
385
+ # === padding?
386
+ # ========================================================================= #
387
+ def padding?
388
+ @internal_hash[:padding]
389
+ end
390
+
391
+ # ========================================================================= #
392
+ # === be_verbose?
393
+ # ========================================================================= #
394
+ def be_verbose?
395
+ @internal_hash[:be_verbose]
396
+ end
397
+
398
+ # ========================================================================= #
399
+ # === server_welcome_message?
400
+ #
401
+ # This method will return the remote server's welcome message.
402
+ # ========================================================================= #
403
+ def server_welcome_message?
404
+ ftp_object?.welcome
405
+ end; alias welcome? server_welcome_message? # === welcome?
406
+ alias welcome server_welcome_message? # === welcome
407
+
408
+ # ========================================================================= #
409
+ # === noop
410
+ # ========================================================================= #
411
+ def noop
412
+ ftp_object?.noop
413
+ end
414
+
415
+ # ========================================================================= #
416
+ # === last_response_code?
417
+ #
418
+ # Return the server's last response code.
419
+ # ========================================================================= #
420
+ def last_response_code?
421
+ ftp_object?.last_response_code
422
+ end; alias last_response_code last_response_code? # === last_response_code
423
+
424
+ # ========================================================================= #
425
+ # === quit (quit tag)
426
+ #
427
+ # Use this to quit (and disconnect) from the FTP Connection again.
428
+ # ========================================================================= #
429
+ def quit
430
+ begin
431
+ if is_connected?
432
+ if ftp_object?
433
+ FtpParadise.clear_user_dataset
434
+ ftp_object?.quit
435
+ end
436
+ end
437
+ rescue Exception => error
438
+ opnn; e "An error happened in the method #{sfancy(__method__)}()"
439
+ pp error
440
+ end
441
+ end
442
+
443
+ # ========================================================================= #
444
+ # === []
445
+ # ========================================================================= #
446
+ def [](i)
447
+ update_file_listing
448
+ if i =~ /^\d+$/ # If is a number.
449
+ i = remote_file_listing.size if i.to_i > remote_file_listing.size
450
+ i = remote_file_listing[i.to_i - 1].first
451
+ end
452
+ return i
453
+ end
454
+
455
+ # ========================================================================= #
456
+ # === sendcmd
457
+ #
458
+ # Sends a command and returns the response of the server.
459
+ #
460
+ # This has to be rescued, because there are lots of different errors
461
+ # possible.
462
+ # ========================================================================= #
463
+ def sendcmd(i)
464
+ begin
465
+ ftp_object?.sendcmd(i)
466
+ rescue Exception => error
467
+ pp error
468
+ e '(This error may be due to the remote file not existing.)'
469
+ end
470
+ end
471
+
472
+ end; end