@Zavexeon/

Ruby Shell (Progress Halted)

Ruby

A Ruby Shell I am creating, still in progress.

fork
loading
Files
  • main.rb
  • hello
  • README.txt
  • rps.rb
  • ruby_storage_file.rb
  • stuff.rb
main.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
require 'base64'

$ver_string = "v1.2.1"
$listed_commands = {
  "help" => "displays list of commands and functions",
  "chronos" => "displays current time",
  "exit" => "terminates shell",
  "new_user" => "creates new user",
  "write" => "writes new file with given contents and extension",
  "read" => "reads file and displays it's contents",
  "load" => "loads .rb file and runs it's contents",
  "delete" => "deletes file and it's contents",
  "root" => "displays all stored files",
  "docs" => "displays docs for shell",
  "clear" => "clears screen",
  "version" => "displays shell version",
}

unless File.exists?('ruby_storage_file.rb')
  File.write('ruby_storage_file.rb', '$users = Hash.new
$files = Hash.new')
  require_relative 'ruby_storage_file.rb'
else; require_relative 'ruby_storage_file.rb' end

def sys_m(message, tag = "system")
  puts "rb(#{tag})>#{message}"
end

def clear; 100.times { puts "\n" } end

def verify_user
  @lock = $sytem_lock
  if @lock == true
    puts "SYSTEM IS LOCKED. PLEASE CONTACT CREATOR."
    sleep(5)
    clear
    File.open('ruby_storage_file.rb', 'a') { |file|
      file.puts "$sytem_lock=false"
    }
  end
  @access = false
  @failed = nil
  until @access == true
    if @failed == 5
      File.open('ruby_storage_file.rb', 'a') { |file|
        file.puts "$sytem_lock = true"
      }
      abort("EXCEEDED ATTEMPT LIMIT. SYSTEM LOCKED.")
    end
    print "Username:"
    @username = gets.chomp
    print "Password:"
    @password = gets.chomp
    if $users.key?(@username) == false
      puts "Incorrect username or password."
      unless @failed == nil; @failed += 1 end
    else
      @user = Base64.decode64($users[@username])
      if @user == @password
        clear
        sys_m("Successfully logged into #{@username}.")
        @access = true
        $logged_on = @username
      else
        puts "Incorrect username or password."
        unless @failed == nil; @failed += 1 end
      end
    end
  end
end

$temp_user_ary = Array.new

def new_user(username = nil, password = nil)
  @completed = false
  if username == nil
    until @completed == true
      print "New username:"
      @new_username = gets.chomp
      print "New password:"
      @new_password = gets.chomp
      if $users.key?(@new_username) == true
        puts "Username already exists."
      elsif $temp_user_ary.include?(@new_username) == true
        puts "Username already exists."
      else
        @secure_password = Base64.encode64(@new_password)
        File.open('ruby_storage_file.rb', 'a') { |file|
          file.puts "\n$users[\"#{@new_username}\"] = \"#{@secure_password}\""
          file.close
        }
        $temp_user_ary.push(@new_username)
        clear
        sys_m("Successfully created new user #{@new_username}.")
        @completed = true
      end
    end
  else
    @new_username = username
    @new_password = password
    if $users.key?(@new_username) == true
      puts "Username already exists."
    elsif $temp_user_ary.include?(@new_username) == true
      puts "Username already exists."
    else
      @secure_password = encode(@new_password)
      File.open('ruby_storage_file.rb', 'a') { |file|
        file.puts "\n$users[\"#{@new_username}\"] = \"#{@secure_password}\""
        file.close
      }
      $temp_user_ary.push(@new_username)
      clear
      sys_m("Successfully created new user #{@new_username}.")
      @completed = true
    end
  end
end

$temp_file_ary = Array.new

def write_file(file_name = nil, contents = nil)
  @stamp = Time.now
  @creator = $logged_on
  if file_name == nil
    print "Enter new file name:"
    @file_name = gets.chomp
    if File.exists?(@file_name)
      puts "File already exists."
    elsif $temp_file_ary.include?(@file_name)
      puts "File already exists."
    else
      print "Enter contents:"
      $/ = "\n\n"
      @contents = gets.chomp
      file = File.open(@file_name, 'w')
      file.puts @contents
      file.close
      puts "Successfully wrote file #{@file_name}"
      File.open('ruby_storage_file.rb', 'a') { |file|
        file.puts "\n$files[\"#{@file_name}\"] = \"#{$logged_on}\""
        file.close
      }
    end
  else
    @file_name = file_name
    @contents = contents
    if File.exists?(@file_name)
      puts "File already exists."
    elsif $temp_file_ary.include?(@file_name)
      puts "File already exists."
      File.open('ruby_storage_file.rb', 'a') { |file|
        file.puts "\n$files[\"#{@file_name}\"] = \"#{$logged_on}\""
        file.close
      }
    else
      file = File.open(@file_name, 'w')
      file.puts @contents
      file.close
      puts "Successfully wrote file #{@file_name}"
      File.open('ruby_storage_file.rb', 'a') { |file|
        file.puts "\n$files[\"#{@file_name}\"] = \"#{$logged_on}\""
        file.close
      }
    end
  end
  $temp_file_ary.push(@file_name)
  $/ = "\n"
end

def read_file(file_name = nil)
  if file_name == nil
    print "Enter name of file to read:"
    @file_to_read = gets.chomp
    if File.exists?(@file_to_read)
      if $files[@file_to_read] == $logged_on
        File.open(@file_to_read, "r") do |file|
          file.each do |line|
            puts line
          end
        end
      else; puts "File does not belong to current user."       end
    else
      puts "File does not exist."
    end
  else
    @file_to_read = file_name
    if File.exists?(@file_to_read)
      if $files[@file_to_read] == $logged_on
        File.open(@file_to_read, "r") do |file|
          file.each do |line|
            puts line
          end
        end
      else; puts "File does not belong to current user."       end
    else; puts "File does not exist."     end
  end
end

def load_file(file_name = nil)
  if file_name == nil
    print "Enter name of file to load(.rb):"
    @file_to_load = gets.chomp
    unless $files[@file_to_read] != $logged_on
      if File.exists?(@file_to_load)
        if File.extname(@file_to_load) != ".rb"
          "Cannot load because it is not a .rb file."
        else
          if @file_to_load == "main.rb"
            puts "Cannot load #{@file_to_load} because it is used by the shell."
          elsif @file_to_load == "ruby_storage_file.rb"
            puts "Cannot load #{@file_to_load} because it is used by the shell."
          else
            clear
            load @file_to_load
          end
        end
      else; puts "File does not exist."       end
    else; puts "File does not belong to current user."     end
  else
    @file_to_load = file_name
    unless $files[@file_to_read] != $logged_on
      if File.exists?(@file_to_load)
        if File.extname(@file_to_load) != ".rb"
          "Cannot load because it is not a .rb file."
        else
          if @file_to_load == "main.rb"
            puts "Cannot load #{@file_to_load} because it is used by the shell."
          elsif @file_to_load == "ruby_storage_file.rb"
            puts "Cannot load #{@file_to_load} because it is used by the shell."
          else
            clear
            load @file_to_load
          end
        end
      else; puts "File does not exist."       end
    else; puts "File does not belong to current user."     end
  end
end

def delete(file_name = nil)
  if file_name == nil
    print "Enter name of file to delete:"
    @file_to_delete = gets.chomp
    if @file_to_delete == "main.rb"
      puts "Cannot delete #{@file_to_delete} because it is used by shell."
    elsif @file_to_delete == "ruby_storage_file.rb"
      puts "Cannot delete #{@file_to_delete} because it is used by shell."
    else
      if $files[@file_to_delete] == $logged_on
        File.delete(@file_to_delete)
        puts "Successfully deleted #{@file_to_delete}"
      else 
        puts "File does not belong to current user."  
      end
    end
  else
    @file_to_delete = file_name
    if @file_to_delete == "main.rb"
      puts "Cannot delete #{@file_to_delete} because it is used by shell."
    elsif @file_to_delete == "ruby_storage_file.rb"
      puts "Cannot delete #{@file_to_delete} because it is used by shell."
    else
      if $files[@file_to_delete] == $logged_on
        File.delete(@file_to_delete)
        puts "Successfully deleted #{@file_to_delete}"
      else 
        puts "File does not belong to current user."  
      end
    end
  end
end

def root
  @root = Dir.glob("*")
  @root.each do |file|
    puts "  -->#{file}"
  end
end

unless $users.empty? == true
  turn = false
  until turn == true
    clear
    puts "(1)Create new user
(2)Log into existing user"
    print "Option number:"; option = gets.chomp
    if option == "1"
      clear
      new_user
      turn = true
    elsif option == "2"
      clear
      verify_user
      turn = true
    else
      clear
      puts "Invalid option."
    end
  end
else
  puts "No users stored in shell. Please create new user."
  new_user
end
sys_m("Shell is ready for operations.")

class Command
  def initialize(command, arg1 = nil, arg2 = nil, arg3 = nil)
    @command = command
    case @command
    when "help"
      $listed_commands.each do |com, desc|
        puts "	#{com} : #{desc}"
      end
    when "chronos"; puts "  #{Time.now}"
    when "exit"; clear; abort("Shell terminated.")
    when "new_user"
      if arg1 == nil; new_user;       else new_user(arg1, arg2) end
    when "write"
      if arg1 == nil; write_file;       else write_file(arg1, arg2) end
    when "read"
      if arg1 == nil; read_file;       else read_file(arg1) end
    when "load"
      if arg1 == nil; load_file;       else load_file(arg1) end
    when "delete"
      if arg1 == nil; delete;       else delete(arg1) end
    when "root"; root
    when "docs"
      if File.exists?('README.txt')
        File.open('README.txt', "r") do |file|
          file.each do |line|
            puts line
          end
        end
      else; puts "README.txt is missing."       end
    when "clear"; clear
    when "version"; puts "  shell #{$ver_string}"
    else; sys_m("\"#{@command}\" is not a listed command", "error")     end
  end
end

loop do
  print "rb>"; command = gets.chomp.split("|")
  Command.new(command[0], command[1], command[2], command[3])
end
ruby 2.5.5p157 (2019-03-15 revision 67260) [x86_64-linux]