Skip to content

Instantly share code, notes, and snippets.

@thomasjslone
Last active February 27, 2023 20:16
Show Gist options
  • Save thomasjslone/3a920bf9898c9d0fdc5426559eded511 to your computer and use it in GitHub Desktop.
Save thomasjslone/3a920bf9898c9d0fdc5426559eded511 to your computer and use it in GitHub Desktop.
## NAME: ossy V1.3
## RELEASE: @ 2017.1.27 by TTF INC.
def map_dir(dir) # return an array of two arrays, file and folder paths of every file in given directory and every subdirectory and file with in it
if File.directory?(dir) ; remaining = [dir] ; found_files = [] ; file_size = [] ; found_folders = [] ; until remaining.length == 0 ; cur_dir = remaining[0].to_s ; remaining.delete_at(0) ; cont = Dir.entries(cur_dir) ; cont.delete(".") ; cont.delete("..") ; cont.each do |obj| ; if File.file?(cur_dir.to_s + "/" + obj.to_s) ; found_files << cur_dir.to_s + "/" + obj.to_s ; file_size << File.size?(found_files[-1]) ; elsif File.directory?(cur_dir.to_s + "/" + obj.to_s) ; found_folders << cur_dir.to_s + "/" + obj.to_s ; remaining << cur_dir.to_s + "/" + obj.to_s ; end ; end ; end ; return [file_size,found_files,found_folders] ; else ; "Invalid Directory: " + dir.to_s ; end
end
def splice(str,b,e) #get the point between the first pair instance of tags from the given string, html scraping anyone?
res = [] ; loop do ; p = str[/#{b}(.*?)#{e}/m, 1].to_s ; if p.to_s.length == 0 ; break ; else ; res << p ; str = str.split(b + p + e)[-1].to_s ; end ; end ; return res
end
def rands(l) ## returns a random string of letters symbols and numbers of a defined length
key = "" ; l.times { key << rand(10**8..10**9).to_s(36).to_s[rand(6).to_i].to_s } ; key.to_s
end
def fnum(n) ## put commas in large numbers for human readability
str = '' ; s = n.to_s.split("").reverse ; i=0 ; s.each do |nc| ; if i == 2 ; i=0 ; str << nc.to_s + "," ; else ; str << nc.to_s ; i+=1 ; end ; end ; if str.to_s[-1].to_s == "," ; str = str.reverse.to_s.split("")[1..-1].join("").to_s ; else ; str = str.reverse.to_s ; end ; return str
end
def list_spacer(spaces,left,right) ## give two arrays of words of varying lengths and determine how far they should be spaced apart when listed down verticially, feed the string back in as an array of string lines to stack rows, do as many as you want, note that the character counter isnt reliable on all systems, later to implement character pixel math to get better approximations of how many spaces are needed to keep rows even
width = spaces .to_i+ 5 ; str = '' ; left.each do |l| ; spacer = "" ; t = width - l.length.to_i ; t.to_i.times { spacer << " " } ; str << l.to_s + spacer.to_s + right[left.index(l)].to_s + "\n" ; end ; return str.to_s
end
def numerize(str) ## turn any string into a large positive number, you can only use strings with keyboard characters, all other symbols will be lost in the decoding operation
if str == "" ; return 0 ; else ; chbytes = ["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", "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", "48", "49", "50", "51", "52", "53", "54", "55", "56", "57", "126", "96", "33", "64", "35", "36", "37", "94", "38", "42", "40", "41", "45", "95", "61", "43", "91", "93", "123", "125", "59", "58", "34", "39", "92", "47", "124", "63", "60", "62", "44", "46", "32", "9", "10"] ; ch_inds = [] ; str.to_s.split("").each { |ch| ch_inds << chbytes.index(ch.to_s.ord.to_s) } ; enc = [] ; ch_inds.each do |ch| ; code = ch.to_i + 1 ; if code.to_s.length == 1 ; code = "0" + code.to_s ; else ; code = code.to_s ; end ; enc << code.to_s ; end ; enc = enc.join("").to_s ; if enc.to_s[0].to_s == "0" ; enc = enc[1..-1].to_s ; end ; return enc.to_s ; end
end
def denumerize(str) ## get your old string back from the numbers you generated
kbchars = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z","A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z","0","1","2","3","4","5","6","7","8","9","~","`","!","@","#","$","%","^","&","*","(",")","-","_","=","+","[","]","{","}",";",":","\"","'","\\","/","|","?","<",">",",","."," ","\t","\n"] ; if str.to_s.length.odd? ; str = "0" + str.to_s ; end ; str_codes = [] ; i = 0 ; hold = "" ; str.to_s.split("").each do |num| ; if i.to_i == 0 ; hold << num.to_s ; i += 1 ; elsif i.to_i == 1 ; hold << num.to_s ; i = 0 ; str_codes << hold.to_s ; hold = "" ; end ; end ; str = str_codes ; str_codes = [] ; str.each do |c| ; str_codes << c.to_i - 1 ; end ; dec_str = "" ; str_codes.each { |c| dec_str << kbchars[c.to_i].to_s } ; dec_str.to_s
end
def exponate(n) ## take the given number and calculate up for exponets which would result in it, stay below five digits unless you got a bad ass cpu and irb/ruby.exe configured to use it
c=2 ; e=2 ; r = [0,0] ; until c > n ; e = 2 ; until e >= n ; if c**e == n ; return [c,e] ; end ; e += 1 ; end ; c += 1 ; end
end
def factors(n) ## get two numbers that which multiplied together would result in the given value, stay below 4 digits again unless your cpu isnt the one only poor people can afford
p = [2] ; vn = 2 ; until vn == n ; vn += 1 ; p << vn ; end ; p.delete_at(-1) ; f1 = 0 ; f2 = 0 ; pd = [] ; p.each do |pn| ; s = n.to_f / pn.to_f ; if s.to_s[-2..-1].to_s == ".0" ; pd << pn ; end ; end ; pd.each do |p| ; if p * p == n ; f1, f2 = p, p ; else ; cd = pd ; cd.delete(p) ; cd.each do |pr| ; if p * pr == n ; f1, f2 = p, pr ; break ; end ; end ; end ; end ; return [f1,f2]
end
class VDrive #V1.2.3
## Virtual multidisk file system with internal/host file system storage and encryption/password protection, import/export files/folders/disks to/from host file system
##
##devnote: complete methods: read_file, write_file, file_exist?, delete_file, rename_file, folder_exist?, ftype?, create_folder, read_folder, map_folder, delete_folder, mount, unmount, save_disk, load_disk, size, format_disk
### Methods: ###############################################
### FILES ##################################################
##
## - file_exist?(STRING) path returns boolean
## - read_file(STRING) path returns string contained in file
## - write_file(STRING,STRING) path, content returns string written, if file does not exist it will be created, otherwise it will be overwritten
## - *insert_file(STRING,INTEGER,STRING) path, pos, str returns true if file written
## - *append_file(STRING,STRING)
## - *bug*copy_file(STRING,STRING) path, newpath returns true
## - rename_file(STRING,STRING) path, newname returns true
## - delete_file(STRING) path returns true
## - *untested*import_file(STRING,STRING) vdrive_path, os_filepath return location of imported file
## - *untested*export_file(STRING,STRING) vdrive_path, os_filepath returns os path file exported to
## - ftype?(STRING) path returns "FILE" , "FOLDER" , "FASLE"
## - *search_file(STRING,STRING) path, tag, returns an array of found positions if string is found, returns false otherwise
##
### FOLDERS ###############################################
##
## - folder_exist?(STRING) path returns boolean
## - create_folder(STRING) path returns true
## - read_folder(STRING) PATH returns an array of file and folder paths in the topdir folder given
## - map_folder(STRRING) path returns array of files and folder paths from the given folder and all its subfolders
## - *copy_folder(STRING,STRING) path, newpatj returns true
## - *rename_folder(STRING,STRING) path, newname returns true
## - *rewritten, untested*delete_folder(STRING) path returns true
## - *import_folder(STRING,STRING) vdrive_path, os_folderpath returns location of folder imported
## - *export_folder(STRING,STRING) vdrive_path, os_filepath
## - *search_folder(STRING,STRING) path, string, returns an array of filepaths of folder contents with names matching the search string
##
###DISK ################################################
##
## - mountable? returns an array of mountable volumes
## - mounted? returns an array of mounted volumes
## - mount(vol) STRING returns true if mounted a real disk
## - unmount(vol) STRING returns true if unmounted a real disk
## - *untested*new_disk(vol) STRING returns true if disk name was acceptbable and disk was made
## - save_disk(vol) STRING returns true if a disk was saved
## - load_disk(vol) STRING returns true if a disk was loaded
## - *copy_disk(vol,nvol) STRING returns true if disk was copied
## - *rename_disk(vol,nvol) STRING, STRING returns true if disk could be renamed
## - *untested*delete_disk(vol) STRING returns true if a disk was deleted
## - *untested*export_disk(vol,path,pw) STRING, STRING, STRING returns true
## - *untested*import_disk(path,pw) os_filepath returns true
## - disk_size(vol)
## - *search_disk(vol,str) path, string returns an array of paths of objects found in disk with names matching the search string
## - *search_file_system(str) string returns an array of paths of objects with names matching search string
## - format_disk(vol) string returns true
## - *format_file_system(pw) string
## - *rewritten, untested*set_password(pw,npw) string, string
##
######################################################
def initialize
@mounted_volumes = []
@mounted_disks = []
###DISK DATA BELOW###
@disks = ["2284661601191923151804942024206658575955595358625862595358575860585859555857585958565955585658585856585959545856932284661601191923151804665857595559535862586259535857586058565856586158616767672284"]
@volumes = ["v"]
@auto_save = true
@password = "19766714291309020380"
@access_mode = "normal"
@storage_mode = "internal"
@fsys_external_path = ""
###DISK DATA ABOVE###
if @volumes.length > 0
@volumes.each { |d| mount(d) }
end
if @mounted_volumes.length > 0
@mounted_volumes
else
true
end
end
def file_exist?(loc)
if @mounted_volumes.include?(loc[0].to_s)
found = false ; i = 0
@mounted_disks[@mounted_volumes.index(loc[0].to_s)][0].each do |f|
if f.to_s.split("@")[0].to_s + "/" + f.to_s.split("@")[1].to_s == loc.to_s
found = i ; break
end
i += 1
end
found
else
"Disk '" + vol.to_s + ":/' is not mounted or is an invalid volume label."
end
end
def folder_exist?(loc)
if @mounted_volumes.include?(loc[0].to_s)
if @mounted_disks[@mounted_volumes.index(loc[0].to_s)][1].include?(loc)
@mounted_disks[@mounted_volumes.index(loc[0].to_s)][1].index(loc)
else
false
end
else
"Disk '" + vol.to_s + ":/' is not mounted or is an invalid volume label."
end
end
def ftype?(loc)
if @mounted_volumes.include?(loc[0].to_s)
if folder_exist?(loc).ia_a? Integer
"FOLDER"
elsif file_exist?(loc).is_a? Integer
"FILE"
else
"FALSE"
end
else
"Disk '" + vol.to_s + ":/' is not mounted or is an invalid volume label."
end
end
def read_file(loc)
if @mounted_volumes.include?(loc[0].to_s)
c = file_exist?(loc)
if c.is_a? Integer
denumerize(@mounted_disks[@mounted_volumes.index(loc[0].to_s)][0][c].to_s.split("@")[2].to_s).to_s
else
"FILE NO EXIST ERROR: " + loc.to_s
end
else
"Disk '" + loc[0].to_s + ":/' is not mounted or is an invalid volume label."
end
end
def write_file(loc,dat)
if @mounted_volumes.include?(loc[0].to_s)
if folder_exist?(loc.to_s.split("/")[0..-2].join("/")).is_a? Integer
c = file_exist?(loc)
if c.is_a? Integer ## overwrite existing file
@mounted_disks[@mounted_volumes.index(loc[0].to_s)][0][c] = @mounted_disks[@mounted_volumes.index(loc[0].to_s)][0][c].to_s.split("@")[0..1].join("@").to_s + "@" + numerize(dat.to_s).to_s
else ## write to new file
@mounted_disks[@mounted_volumes.index(loc[0].to_s)][0] << loc.to_s.split("/")[0..-2].join("/").to_s + "@" + loc.to_s.split("/")[-1].to_s + "@" + numerize(dat.to_s).to_s
end
else
"PARENT FOLDER NO-EXIST: " + loc.to_s.split("/")[0..-2].join("/").to_s
end
else
"Disk '" + loc[0].to_s + ":/' is not mounted or is an invalid volume label."
end
end
def delete_file(loc)
if @mounted_volumes.include?(loc[0].to_s)
c = file_exist?(loc)
if c.is_a? Integer
@mounted_disks[@mounted_volumes.index(loc[0].to_s)][0].delete_at(c) ; if @auto_save ; save_disk(loc[0].to_s) ; end ; true
else
"FILE NON-EXISTENT: " + loc.to_s
end
else
"Disk '" + loc[0].to_s + ":/' is not mounted or is an invalid volume label."
end
end
def rename_file(loc,newname)
if @mounted_volumes.include?(loc[0].to_s)
c = file_exist?(loc)
if c.is_a? Integer
f = @mounted_disks[@mounted_volumes.index(loc[0].to_s)][0][c].to_s.split("@")
nf = f[0].to_s + "@" + newname.to_s + "@" + f[2].to_s
@mounted_disks[@mounted_volumes.index(loc[0].to_s)][0][c] = nf.to_s
if @auto_save ; save_disk(loc[0].to_s) ; end
true
else
"FILE NON-EXISTENT: " + loc.to_s
end
else
"Disk '" + loc[0].to_s + ":/' is not mounted or is an invalid volume label."
end
end
def copy_file(loc,nloc)
if @mounted_volumes.include?(loc[0].to_s)
c = file_exist?(loc)
if c.is_a? Integer
f = @mounted_disks[@mounted_volumes.index(loc[0].to_s)][0][c].to_s ; n = f.to_s.split("@")[1].to_s
if folder_exist?(nloc.to_s)
if file_exist?(nloc.to_s + "/" + n.to_s)
v = 0 ; ok = false
until ok ; v += 1
if nloc.include?(".")
## currently a bug causes the number to be put at the end rather than between the name and extension, if used outside this context the code works as intended, wtf is different here?
nn = nloc.to_s + "/" + n.to_s.split(".")[0..-2].join(".").to_s + v.to_s + "." + n.to_s.split(".")[-1].to_s
else
nn = nloc.to_s + "/" + n.to_s + v.to_s
end
if file_exist?(nn) == false ; ok = true ; end
end
f = nn.to_s.split("/")[0..-2].join("/").to_s + "@" + nn.to_s.split("/")[-1].to_s + "@" + f.split("@")[2].to_s
@mounted_disks[@mounted_volumes.index(loc[0].to_s)][0] << f.to_s
else
nf = nloc.to_s + "@" + f.split("@")[1].to_s + "@" + f.split("@")[2].to_s
@mounted_disks[@mounted_volumes.index(loc[0].to_s)][0] << nf
end
if @auto_save ; save_disk(loc[0].to_s) ; end ; "File was copied to: " + nn.to_s
else
"PARENT FOLDER NON-EXISTENT: " + nloc.to_s
end
else
"FILE NON-EXISTENT: " + loc.to_s
end
else
"Disk '" + loc[0].to_s + ":/' is not mounted or is an invalid volume label."
end
end
def move_file(loc,nloc)
if @mounted_volumes.include?(loc[0].to_s)
c = file_exist?(loc)
if c.is_a? Integer
f = @mounted_disks[@mounted_volumes.index(loc[0].to_s)][0][c].to_s ; n = f.to_s.split("@")[1].to_s
if folder_exist?(nloc.to_s)
if file_exist?(nloc.to_s + "/" + n.to_s)
v = 0 ; ok = false
until ok ; v += 1
if nloc.include?(".")
## currently a bug causes the number to be put at the end rather than between the name and extension, if used outside this context the code works as intended, wtf is different here?
nn = nloc.to_s + "/" + n.to_s.split(".")[0..-2].join(".").to_s + v.to_s + "." + n.to_s.split(".")[-1].to_s
else
nn = nloc.to_s + "/" + n.to_s + v.to_s
end
if file_exist?(nn) == false ; ok = true ; end
end
f = nn.to_s.split("/")[0..-2].join("/").to_s + "@" + nn.to_s.split("/")[-1].to_s + "@" + f.split("@")[2].to_s
@mounted_disks[@mounted_volumes.index(loc[0].to_s)][0] << f.to_s
else
nf = nloc.to_s + "@" + f.split("@")[1].to_s + "@" + f.split("@")[2].to_s
@mounted_disks[@mounted_volumes.index(loc[0].to_s)][0] << nf
end
if @auto_save ; save_disk(loc[0].to_s) ; end ; "File was copied to: " + nn.to_s
else
"PARENT FOLDER NON-EXISTENT: " + nloc.to_s
end
else
"FILE NON-EXISTENT: " + loc.to_s
end
else
"Disk '" + loc[0].to_s + ":/' is not mounted or is an invalid volume label."
end
end
def append_file(loc,str)
if @mounted_volumes.include(loc[0].to_s)
else
"Disk '" + loc[0].to_s + ":/' is not mounted or is an invalid volume label."
end
end
def insert_file(loc,pos,str)
if @mounted_volumes.include(loc[0].to_s)
else
"Disk '" + loc[0].to_s + ":/' is not mounted or is an invalid volume label."
end
end
def search_file(loc,searchterm)
if @mounted_volumes.include(loc[0].to_s)
else
"Disk '" + loc[0].to_s + ":/' is not mounted or is an invalid volume label."
end
end
def create_folder(loc)
if @mounted_volumes.include?(loc[0].to_s)
if folder_exist?(loc.to_s.split("/")[0..-2].join("/")).is_a? Integer
if folder_exist?(loc).is_a? Integer
"Folder already exists: " + loc.to_s
else
@mounted_disks[@mounted_volumes.index(loc[0].to_s)][1] << loc.to_s ; if @auto_save ; save_disk(loc[0].to_s) ; end ; true
end
else
"PARENT FOLDER DOES NOT EXIST: " + loc.to_s.split("/")[0..-2].join("/").to_s
end
else
"Disk '" + vol.to_s + ":/' is not mounted or is an invalid volume label."
end
end
def delete_folder(loc)
if @mounted_volumes.include?(loc[0].to_s)
c = folder_exist(loc)
if c.is_a? Integer
map = map_folder(loc)
if map != "empty"
ret = false ; if @auto_save ; @auto_save = false ; ret = true ; end
if map[0].length > 0
map[0].each { |f| delete_file(f) }
end
if map[1].length > 0
map[1].each { |f| @mounted_disks[@mounted_volumes.index(loc[0].to_s)][1].delete(f) }
end
if ret ; @auto_save = true ; end
end
@mounted_disks[@mounted_volumes.index(loc[0].to_s)][1].delete(loc)
if @auto_save ; save_disk(loc[0].to_s) ; end ; true
else
"FOLDER NON-EXISTENT"
end
else
"Disk '" + vol.to_s + ":/' is not mounted or is an invalid volume label."
end
end
def rename_folder()
end
def copy_folder()
end
def move_folder()
end
def read_folder(loc) ## returns list of paths of files and folders in given folder
if @mounted_volumes.include?(loc[0].to_s)
vol = loc[0].to_s
i = folder_exist?(loc) ; contents = []
if i.is_a? Integer
@mounted_disks[@mounted_volumes.index(vol)][0].each do |f|
if f.to_s.split("@")[0].to_s == loc.to_s
contents << f.to_s.split("@")[0].to_s + "/" + f.to_s.split("@")[1].to_s
end
end
@mounted_disks[@mounted_volumes.index(loc[0].to_s)][1].each do |f|
if f.to_s.split("/")[0..-2].join("/").to_s == loc.to_s
contents << f.to_s
end
end
if contents.length == 0
return "empty"
else
return contents
end
else
"FOLDER NON-EXISTENT"
end
else
"Disk '" + vol.to_s + ":/' is not mounted or is an invalid volume label."
end
end
def map_folder(loc) ## returns a list of all paths contained in the given folder and all its subfolders
if @mounted_volumes.include?(loc[0].to_s)
vol = loc[0].to_s ; i = folder_exist?(loc) ; files = [] ; folders = []
if i.is_a? Integer
@mounted_disks[@mounted_volumes.index(vol)][1].each do |f|
if loc.to_s == f[0..loc.length - 1]
folders << f.to_s
end
end
@mounted_disks[@mounted_volumes.index(vol)][0].each do |f|
if folders.include?(f.split("@")[0].to_s)
files << f.to_s
end
end
if files.length == 0 and folders.length == 0
"empty"
else
[files,folders]
end
else
"FOLDER NON-EXISTENT"
end
else
"Disk '" + vol.to_s + ":/' is not mounted or is an invalid volume label."
end
end
def import_file(loc,os_file_path)
if ftype?(loc.to_s).to_s == "FOLDER"
if File.exist?(os_file_path.to_s)
begin
f = File.open(os_file_path.to_s,"r") ; cont = f.read.to_s ; f.close
write_file(loc.to_s + "/" + os_file_path.to_s.split("/")[-1].to_s,cont.to_s)
if @auto_save ; save_disk ; end
file_exist?(loc.to_s + "/" + os_file_path.to_s.split("/")[-1].to_s).is_a? Integer
rescue
"Could not access file: " + os_file_path.to_s
end
else
"Invalid File Path: " + os_file_path.to_s
end
else
"INVALID IMPORT LOCATION: " + loc.to_s
end
end
def export_file(loc,os_directory)
if File.directory?(os_directory)
if ftype(loc.to_s).to_s == "FILE"
cont = read_file(loc.to_s).to_s
begin
f = File.open(os_directory.to_s + "/" + loc.to_ss,"w") ; f.write(cont.to_s) ; f.close
File.exist?(os_directory.to_s + "/" + loc.to_s.split("/")[-1].to_s)
rescue
"Could not write to directory: " + os_directory.to_s
end
else
"Invalid Export File: " + loc.to_s
end
else
"Invalid Export Directory: " + os_directory.to_s
end
end
def import_folder(loc,os_file_path)
end
def export_folder(loc,os_directory)
end
def import_disk(vol,diskfilepath,password)
if password == nil or password == '' or password == 0
pv = 0
else
pv = numerize(password.to_s).to_i
end
file = File.open(diskfilepath,"r")
img = file.read.to_s ; file.close
img = img.to_i / pv.to_i + diskfilepath.length.to_i
@mounted_disks[@mounted_volumes.index(loc.to_s.split(":")[0])][0] = denumerize(img.to_s).to_s.split("###")[0].to_s.split(",")
@mounted_disks[@mounted_volumes.index(loc[0].to_s)][1] = denumerize(img.to_s).to_s.split("###")[1].to_s.split(",")
end
def export_disk(vol,diskfilepath,password)
if password == nil or password == "" or password == "0" or password == 0
pv = 0
else
pv = numerize(password.to_s).to_i
end
img = numerize(numerize(@mounted_disks[@mounted_volumes.index(loc.to_s.split(":")[0])][0].join(",") + "###" + @mounted_disks[@mounted_volumes.index(loc[0].to_s)][1].join(","))).to_i * pv - diskfilepath.to_s.length.to_i
file = File.new(diskfilepath,"w")
file.write(img.to_s) ; file.close
diskfilepath.to_s
end
def format_disk(vol)
@mounted_disks[@mounted_volumes.index(vol)][0] = [] ; @mounted_disks[@mounted_volumes.index(vol)][1] = [vol.to_s + ':'] ; save_disk(vol) ; true
end
def disk_size(vol)
@mounted_disks[@mounted_volumes.index(vol)].to_s.length.to_i
end
def mountable?
@volumes
end
def mounted?
return @mounted_volumes
end
def mount(vol)
if @mounted_disks.include?(vol)
"Disk '" + vol.to_s + "' is already mounted."
else
file = File.open(__FILE__,"r") ; fcont = file.read.to_s ; file.close
icont = fcont.to_s.split("###DISK " + "DATA BELOW###")[-1].to_s.split("###DISK " + "DATA ABOVE###")[0]
volumes = eval(icont.to_s.split("\n")[2].to_s.split("@volumes = ")[-1].to_s)
if volumes.include?(vol)
@mounted_volumes << vol.to_s ; @mounted_disks << [[],[]] ; load_disk(vol)
true
else
"Invalid Volume Label: " + vol.to_s + ", No such disk attached."
end
end
end
def unmount(vol)
if @mounted_volumes.include?(vol)
i = @mounted_volumes.index(vol)
@mounted_volumes.delete_at(i) ; @mounted_disks.delete_at(i)
true
else
"Disk '" + vol.to_s + ":/' is not mounted or is an invalid volume label."
end
end
def create_disk(vol)
img = numerize(@mounted_disks[@mounted_volumes.index(vol)][0].join(",") + "###" + @mounted_disks[@mounted_volumes.index(vol)][1].join(","))
file = File.open(__FILE__,"r") ; fcont = file.read.to_s ; file.close
icont = fcont.to_s.split("###DISK " + "DATA BELOW###")[-1].to_s.split("###DISK " + "DATA ABOVE###")[0]
volumes = eval(icont.to_s.split("\n")[2].to_s.split("@volumes = ")[-1].to_s)
disks = eval(icont.to_s.split("\n")[1].to_s.split("@disks = ")[-1].to_s)
if volumes.include?(vol) == false
volumes << vol.to_s ; disks << [[],[]]
first = fcont.to_s.split("###DISK " + "DATA BELOW###")[0].to_s + "###DISK " + "DATA BELOW###"
middle = "@disks = " + disks.to_s + "\n" + "@volumes = " + volumes.to_s + "\n" + icont.to_s.split("\n")[3..-1].join("\n").to_s
last = "###DISK " + "DATA ABOVE###" + fcont.to_s.split("###DISK " + "DATA ABOVE###")[-1].to_s
file = File.open(__FILE__,"w") ; file.write(first.to_s + "\n" + middle.to_s + "\n" + last.to_s + "\n") ; file.close
true
else
"Can not create disk: " + vol.to_s + " because it already exists."
end
end
def load_disk(vol)
if @mounted_volumes.include?(vol)
if @storage_mode == "internal"
file = File.open(__FILE__,"r") ; fcont = file.read.to_s ; file.close
icont = fcont.to_s.split("###DISK " + "DATA BELOW###")[-1].to_s.split("###DISK " + "DATA ABOVE###")[0]
volumes = eval(icont.to_s.split("\n")[2].to_s.split("@volumes = ")[-1].to_s)
disks = eval(icont.to_s.split("\n")[1].to_s.split("@disks = ")[-1].to_s)
img = disks[volumes.index(vol)]
files = denumerize(img.to_s).to_s.split("###")[0].to_s.split(",")
folders = denumerize(img.to_s).to_s.split("###")[1].to_s.split(",")
@mounted_disks[@mounted_volumes.index(vol)] = [files,folders]
true
elsif @storage_mode == "external"
# file = File.open(@fsysimg_external_path,"r") ; icont = file.read.to_s ; file.close
# volumes = eval("[" + icont.to_s.split("\n")[2].to_s.split("@volumes = [")[1].to_s[0..-3].to_s + "]")
# disks = eval(icont.to_s.split("\n")[1].split(" = ")[1].to_s)
# img = disks[volumes.index(vol)]
# files = denumerize(img.to_s).to_s.split("###")[0].to_s.split(",")
#folders = denumerize(img.to_s).to_s.split("###")[1].to_s.split(",")
#@mounted_disks[@mounted_volumes.index(vol)][0] = files
#@mounted_disks[@mounted_volumes.index(vol)][1] = folders
#true
end
else
"Disk '" + vol.to_s + ":/' is not mounted or is an invalid volume label."
end
end
def save_disk(vol)
if @mounted_volumes.include?(vol)
if @storage_mode.to_s == "internal"
img = numerize(@mounted_disks[@mounted_volumes.index(vol)][0].join(",") + "###" + @mounted_disks[@mounted_volumes.index(vol)][1].join(","))
file = File.open(__FILE__,"r") ; fcont = file.read.to_s ; file.close
icont = fcont.to_s.split("###DISK " + "DATA BELOW###")[-1].to_s.split("###DISK " + "DATA ABOVE###")[0]
volumes = eval(icont.to_s.split("\n")[2].to_s.split("@volumes = ")[-1].to_s)
disks = eval(icont.to_s.split("\n")[1].to_s.split("@disks = ")[-1].to_s)
disks[volumes.index(vol)] = img.to_s
first = fcont.to_s.split("###DISK " + "DATA BELOW###")[0].to_s + "###DISK " + "DATA BELOW###"
middle = "@disks = " + disks.to_s + "\n" + "@volumes = " + volumes.to_s + "\n" + icont.to_s.split("\n")[3..-1].join("\n").to_s
last = "###DISK " + "DATA ABOVE###" + fcont.to_s.split("###DISK " + "DATA ABOVE###")[-1].to_s
file = File.open(__FILE__,"w") ; file.write(first.to_s + "\n" + middle.to_s + "\n" + last.to_s + "\n") ; file.close
true
elsif @storage_mode.to_s == "external"
end
else
"Disk '" + loc[0].to_s + ":/' is not mounted or is an invalid volume label."
end
end
def delete_disk(vol)
file = File.open(__FILE__,"r") ; fcont = file.read.to_s ; file.close
icont = fcont.to_s.split("###DISK " + "DATA BELOW###")[-1].to_s.split("###DISK " + "DATA ABOVE###")[0]
volumes = eval(icont.to_s.split("\n")[2].to_s.split("@volumes = ")[-1].to_s)
disks = eval(icont.to_s.split("\n")[1].to_s.split("@disks = ")[-1].to_s)
if volumes.include?(vol)
if @mounted_volumes.include?(vol) ; unmount(vol) ; end
disks.delete_at(volumes.index(vol)) ; volumes.delete(vol)
first = fcont.to_s.split("###DISK " + "DATA BELOW###")[0].to_s + "###DISK " + "DATA BELOW###"
middle = "@disks = " + disks.to_s + "\n" + "@volumes = " + volumes.to_s + "\n" + icont.to_s.split("\n")[3..-1].join("\n").to_s
last = "###DISK " + "DATA ABOVE###" + fcont.to_s.split("###DISK " + "DATA ABOVE###")[-1].to_s
file = File.open(__FILE__,"w") ; file.write(first.to_s + "\n" + middle.to_s + "\n" + last.to_s + "\n") ; file.close
true
else
"Cannot delete disk: " + vol.to_s + " because it is non-existent."
end
end
def set_storage_mode(mode,pw) # internal / external
end
def set_access_mode(mode,pw) # normal / encrypted
end
def change_password(pw,npw)
file = File.open(__FILE__,"r") ; fcont = file.read.to_s ; file.close
icont = fcont.to_s.split("###DISK " + "DATA BELOW###")[-1].to_s.split("###DISK " + "DATA ABOVE###")[0]
pw = numerize(pw).to_i * 12345 ; npw = numerize(npw).to_i * 12345
if pw.to_s == icont.to_s.split("\n")[4].to_s.split("@password = ")[-1].to_s[1..-2].to_s
middle = icont.split("\n") ; middle[4] = "@password = \"" + npw.to_s + "\""
first = fcont.to_s.split("###DISK " + "DATA BELOW###")[0].to_s + "###DISK " + "DATA BELOW###"
middle = middle.join("\n").to_s
last = "###DISK " + "DATA ABOVE###" + fcont.to_s.split("###DISK " + "DATA ABOVE###")[-1].to_s
file = File.open(__FILE__,"w") ; file.write(first.to_s + "\n" + middle.to_s + "\n" + last.to_s + "\n") ; file.close
true
else
"Invalid Password"
end
end
end
class Ossy #V1.3
def initialize
@@vdr = VDrive.new()
@@log = ["Log created by source @ " + Time.now.to_s]
@@ques = []
@@todo = []
@@doing = [] ; @@start_times = []
@@done = [] ; @@finish_times = []
@@cycle_clock = 0.0 ; @@cycle_counter = 0
@@run_main = false
@@main_thread = nil
@@thread_pool = []
end
def start
if @@run_main == false
@@main_thread = Thread.new { run_main } ; true
else
false
end
end
def stop
if @@run_main
@@run_main = false ; @@main_thread.kill ; @@main_thread = nil ; true
else
false
end
end
def que_task(timestr,taskcode) #time str format: 2017.12.13.23.59.59
ct = timestr.to_s.split(".") ; ct = Time.now(ct[0].to_i,ct[1].to_i,ct[2].to_i,ct[3].to_i,ct[4].to_i,ct[5].to_i)
if Time.now < ct
@@ques << timestr.to_s + "@" + numerize(taskcode.to_s) ; true
else
"Invalid Que Time: " + ct.to_s + " Current Host Time: " + Time.now.to_s
end
end
def unque_task(timestr,taskcode)
cstr = timestr.to_s + "@" + numerize(taskcode.to_s).to_s
i = 0 ; f = false
@@ques.each do |q|
if q.to_s == cstr.to_s
f = i ; break
end
i += 1
end
if f != false
@@ques.delete_at(i) ; true
else
false
end
end
def state # state inquires as to run_main and weather the bot is processing new instructions, or running only existing instructions in the
if @@run_main and @@thread_pool.length > 0
"Active"
elsif @@run_main and @@thread_pool.length == 0
"Idle"
elsif @@run_main == false and @@thread_pool.length > 0
"Background"
elsif @@run_main == false and @@thread_pool.length == 0
"Dormant"
end
end
def info
str = "Bot State: " + state.to_s + "\n"
str << "Thread Pool: " + fnum(@@thread_pool.length.to_s).to_s + "\n"
str << "Active Tasks: " + fnum(@@doing.length.to_s).to_s + "\n"
str << "Remaining Tasks: " + fnum(@@todo.length.to_s).to_s + "\n"
str << "FInished Tasks: " + fnum(@@done.length.to_s).to_s + "\n"
str << "Qued Tasks: " + fnum(@@ques.length.to_s).to_s + "\n"
str << "Cycle Clock Delay: " + @@cycle_clock.to_s + "\n"
str << "Cycle Counter: " + fnum(@@cycle_counter.to_s).to_s
return str.to_s
end
def ques
@@ques
end
def todo
@@todo
end
def doing
@@doing
end
def done
@@done
end
def thread_pool
@@thread_pool
end
def run_main ## this is the brain of the bot, tasks you program have their own threads but this thread manages the runtime of all other threads, this run_main loop is the main thread, which is loop that cleans the thread pool, then processes ques for expired or due tasks and finnally does tasks on the todo list and moves them to doing list, the thread cleaning handles moving from doing to done list
if @@run_main == false
@@run_main = true
while @@run_main
d = [] ; i = 0
if @@thread_pool.length > 0
@@thread_pool.each do |t|
unless t.alive? ; d << i.to_i ; end ; i += 1
end
if d.length > 0
d.each { |i| @@thread_pool[i].kill ; @@done << [@@thread_pool[i].value,@@doing[i]] ; @@thread_pool.delete_at(i); @@finish_times << Time.now.to_s ; @@doing.delete_at(i) }
end
end
i = 0
if @@ques.length > 0
@@ques.each do |q|
qt = q.split("@")[0].split(".") ; qt = Time.now(qt[0].to_i,qt[1].to_i,qt[2].to_s,qt[3].to_i,qt[4].to_i,qt[5].to_i)
if Time.now >= qt
@@todo << denumerize(q.split("@")[-1].to_s).to_s ; @@ques.delete_at(i)
end
i += 1
end
end
i = 0
if @@todo.length > 0
todo = @@todo ; @@todo = []
todo.each do |t|
@@doing << t.to_s ; tt = t.to_s
begin
cc = general_request(denumerize(@@vdr.read_file("v:/password.txt").to_i / 12345).to_s,t) ## check for bot command, note that this line is kinda bad cause it mentions the syntax for password decoding, we never include that usually and always check passwords encoded but never decode so you have to figure that out on your own
if cc.to_s == "Invalid Command"
@@thread_pool << eval("Thread.new { " + t.to_s + " }") ## if task is ruby code it goes to the thread pool
else ## otherwise if the task is a command it gets done and logged
@@log << "Bot Command : " + t.to_s ; @@doing.delete(t) ; @@done << tt.to_s
end
@@start_times << Time.now.to_s
rescue
@@log << "Task Execution Failed: " + t.to_s + " @ " + Time.now.to_s
end
i += 1
end
end
@@cycle_counter += 1 ; sleep @@cycle_clock.to_f
end
else
"main is already running"
end
end
def general_request(pw,inp)
pw = numerize(pw).to_i * 12345 ; if pw.to_s == @@vdr.read_file("v:/password.txt").to_s ; pw = nil # set pw to nil so its not left over for commands being executed to use
if inp.to_s[0] == "<" ## heres where the bot gives out control, this could be a back door to the bot and its host, but for now the shell uses this symbol to get control of the bot, this could be password protected
begin ; res = eval(inp[1..-1].to_s) ; rescue ; res = "Ruby Failed" ; end
elsif inp.to_s == "host time"
return Time.now.to_s
elsif inp.to_s[0..15] == "change password:" ## "pw,npw"
pw = numerize(inp[16..-1].to_s.split(",")[0].to_s).to_i * 12345 ; npw = numerize(inp[16..-1].to_s.split(",")[1..-1].join(",").to_s).to_i * 12345
if @@vdr.read_file("v:/password.txt").to_s == pw.to_s
@@vdr.write_file("v:/password.txt",npw.to_s) ; true
else
"Permission Denied"
end
elsif inp.to_s == "finalize"
@@thread_pool.each { |t| t.kill } ; @@thread_pool = [] ; if @@doing.length > 0 ; @@doing.each { |t| @@done << t.to_s } ; @@doing = [] ; end ; true
elsif inp.to_s == "shutdown"
stop ; @@thread_pool.each { |t| t.kill } ; finish
else
## see if cmd is program or file, then check if its ruby that can be executed
"Invalid Command"
end
else
pw = nil ; "Permission Denied"
end
end
end
$ossy = Ossy.new()
require 'ruboto/activity'
require 'ruboto/widget'
require 'ruboto/util/toast'
ruboto_import_widgets :TextView, :LinearLayout, :Button, :ListView, :EditText
class Shell_Activity
def on_create(b)
super
build_ui("login")
end
def build_ui(ui)
if ui == "shell"
setTitle "Welcome to your Shell"
set_content_view(linear_layout(:orientation => :vertical) do
@fid = 0 ; scl = 10 ; $scl = scl - scl - scl ; @sc1 = $scl ; @sc2 = -1 ; $display_lines = [] ; scl.times { $display_lines << "" }
@display = text_view :text => $display_lines.join("\n") ; @wrap_length = 80
set_content_view(linear_layout(:orientation => :horizontal) do
@fid_label = text_view :text => "<" + @fid.to_s + ": "
@input_bar = edit_text
button :text => "E", :on_click_listener => (proc{forward_input}) , :height => 60 , :width => 40
end)
end)
elsif ui == "login"
setTitle "Log into your bot...."
set_content_view(linear_layout(:orientation => :horizontal) do
@inpb = edit_text :width => :wrap_content, :hint => 'password', :transformation_method => android.text.method.PasswordTransformationMethod.new
button :text => "Log In", :on_click_listener => (proc{ @password = @inpb.get_text.to_s ; build_ui("shell") ; append_consol("Welcome to the shell.") }) , :height => 47 , :width => 87
end)
end
end
def append_consol(inp)
scroll_bottom ; ll = [] ; inp.to_s.split("\n").each { |l| ll << l.to_s }
nl = []
ll.each do |l|
if l.to_s.length > @wrap_length.to_i
c = 0 ; line = ""
l.split('').each do |ch|
if c < @wrap_length.to_i - 1
line << ch.to_s ; c += 1
else
nl << line.to_s + ch.to_s ; line = "" ; c = 0
end
end
else
nl << l.to_s
end
end
nl.each { |l| $display_lines << l.to_s }
update_display
end
def update_display
@display.set_text($display_lines[@sc1..@sc2].join("\n").to_s)
end
def set_display_lines(lines) # INTEGER
$scl = lines.to_i - lines.to_i - lines.to_i ; @sc1 = $scl ; update_display
end
def set_display_width(length)
@wrap_length = length.to_i
end
def scroll_top
l = $display_lines.length.to_i ; l = l - l - l
@sc1 = l.to_i ; @sc2 = @sc1 - $scl - 2
update_display
end
def scroll_up
cpv = $display_lines.length - $display_lines.length - $display_lines.length
if @sc1.to_i > cpv.to_i
@sc1 -= 1 ; @sc2 -= 1
update_display
else
## cant scroll any more
end
end
def scroll_bottom
@sc1 = $scl ; @sc2 = -1 ; update_display
end
def scroll_down
if @sc2.to_i < -1
@sc1 += 1 ; @sc2 += 1
update_display
else
## cant scroll any more
end
end
def forward_input ## this takes the command from the textbox widget and sends it where it needs to go, the it prints the feedback below, like a normal shell
cmd = @input_bar.get_text.to_s ; @input_bar.set_text("") ; res = ""
if cmd[0].to_s == "*" ## some of these commands never reach the general_request method, instead they might be a scroll commands, or a command specifically for the shell app, the shell app is combined with ossy in since v2 however it is designed so they can be seperated with ease
cmd = cmd[1..-1].to_s ; @input_bar.set_text("*" + cmd.to_s)
end
if cmd[0..2].to_s == "sh<"
append_consol("<" + @fid.to_s + ": " + cmd.to_s)
begin
res = eval(cmd[3..-1].to_s).to_s
rescue
res = "Execution Failed (Shell: " + self.to_s + ")"
end
elsif cmd.to_s == "[u"
res = nil ; scroll_up
elsif cmd.to_s == "[d"
res = nil ; scroll_down
elsif cmd.to_s == "[h"
res = nil ; scroll_top
elsif cmd.to_s == "[e"
res = nil ; scroll_bottom
else ## not shell cmd so must be for ossy or irb
append_consol("<" + @fid.to_s + ":" + cmd.to_s.split("\n").join(" ; ").to_s)
res = $ossy.general_request(@password.to_s,"<" + cmd).to_s
end
unless res.to_s == ""
append_consol(":" + @fid.to_s + ">" + res.to_s)
end
@fid += 1
@fid_label.set_text(@fid.to_s)
end
end
$irb.start_ruboto_activity :class_name => "Shell_Activity"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment