Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --It is currently possible to transfer messages over about 128 blocks between 2 computers by setting up a mail-server in the middle
- --Each Node (Server and Client need any type of network modem
- --The code may not be the prettiest of codes and also not one of the most efficient ones but it currently works really smoothly
- --THIS PROGRAM IS STILL UNDER DEVELOPMENT AND MAY GET SOME UPDATES UNTIL THE FINAL VERSION
- -- local variables --------- local variables --------- local variables --------- local variables --
- local modem = nil
- local tArgs = { ... }
- local cl_modemside, cl_type
- local key = nil
- local username, password
- local prt_transmit = 1912
- local prt_receive = 1997
- -- functions ---- functions ---- functions ---- functions ---- functions ---- functions ---- functions --
- function getModemSides()
- local mc = 0
- local modemlist = {}
- local sides = rs.getSides()
- for i=1,6 do
- if peripheral.getType(sides[i]) == "modem" then
- mc = mc + 1
- modemlist[mc] = sides[i]
- end
- end
- return(modemlist)
- end
- function clear()
- shell.run("clear");
- end
- function getModemToRun(cm)
- if table.getn(cm) == 0 then
- clear()
- print("There is no modem connected to this device!")
- error()
- elseif table.getn(cm) > 1 then
- repeat
- clear()
- print("===================================================")
- print("= SETUP =")
- print("===================================================")
- print()
- print("You have multipe modems connected")
- print("Connected modems:")
- for i=1,table.getn(cm) do
- print(i .. ": " .. cm[i])
- end
- local answer=tonumber(read())
- until answer ~= nil and answer > 0 and answer <= table.getn(cm)
- return answer
- end
- return 1
- end
- function genKey(length)
- length = tonumber(length)
- if length == nil or length < 1 then
- length = 1
- end
- local value = ""
- local hex_values = {"0","1","2","3","4","5","6","7","8","9","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"}
- for i=1,length do
- value = value .. hex_values[math.floor(math.random()*36)+1]
- end
- return value
- end
- function writeConfig()
- local h = fs.open("config/mail.cfg", "w")
- h.writeLine(cl_modemside)
- h.writeLine(cl_type)
- h.close()
- end
- function transmit(data)
- modem.transmit(prt_transmit, prt_receive, textutils.serialize(data))
- end
- function receive()
- modem.open(prt_transmit)
- local timeout = os.startTimer(2)
- while true do
- event = {os.pullEvent()}
- if event[1] == "modem_message" then
- modem.close(prt_transmit)
- return textutils.unserialize(event[5])
- elseif event[1] == "timer" and event[2] == timeout then
- return {""}
- end
- end
- end
- function login()
- print("Username: ")
- user=read()
- print("Password: ")
- pass=read("*")
- return user, pass
- end
- -- code --------- code --------- code --------- code --------- code --------- code --------- code --------- code --
- if tostring(tArgs[1]) == "?" then
- print("This program is a mailing-software. Running this program the first time will start the installation-wizzard.")
- print()
- print(" -r will reinstall the program")
- error()
- elseif tostring(tArgs[1]) == "-r" then
- if fs.exists("config/mail.cfg") then
- shell.run("rm config/mail.cfg")
- shell.run("rm mailserver")
- shell.run("rm startup")
- end
- end
- if fs.exists("config/mail.cfg") then
- local h = fs.open("config/mail.cfg", "r")
- cl_modemside = h.readLine();
- cl_type = h.readLine();
- h.close()
- if peripheral.getType(cl_modemside) ~= "modem" then
- cl_modemside = getModemSides()[getModemToRun(getModemSides())]
- writeConfig()
- end
- else
- cl_modemside = getModemSides()[getModemToRun(getModemSides())]
- repeat
- clear()
- -------=========================-=========================
- print("===================================================")
- print("= SETUP =")
- print("===================================================")
- print()
- print("What role does tis machine have?")
- print("It is a:")
- print("1 => Client")
- print("2 => Server")
- print("3 => Repeater")
- answer=tonumber(read())
- until answer ~= nil and ( answer == 1 or answer == 2 or answer == 3 )
- if answer == 1 then
- cl_type = "client"
- elseif answer == 2 then
- cl_type = "server"
- shell.run("mkdir mailserver")
- local h = fs.open("startup", "w")
- h.writeLine('shell.run("'.. shell.getRunningProgram() .. '")')
- h.close()
- elseif answer == 3 then
- cl_type = "repeater"
- local h = fs.open("startup", "w")
- h.writeLine('shell.run("'.. shell.getRunningProgram() .. '")')
- h.close()
- end
- writeConfig()
- end
- modem = peripheral.wrap(cl_modemside)
- if cl_type == "server" then
- clear()
- -------=========================-=========================
- print("===================================================")
- print("= Computercraft =")
- print("= Mail-Server =")
- print("= Version 1.0 =")
- print("= by BeminAbe =")
- print("===================================================")
- print("")
- print("There are multiple ways of adding People.")
- print("You can add yourself as a user from a client or")
- print("you start this server with -u <username> <password>")
- print("")
- print("Wireless Modems have a range which is 64 blocks")
- print("It can go up to 384 blocks at max. buildheight")
- print("You can use the repeater function built into this")
- print("system to extend the transmission range.")
- while true do
- data = receive()
- if data[2] == "login" then
- user = data[3]
- pass = data[4]
- transmit({data[1].."-", "auth",fs.exists("mailserver/"..user.."/"..pass)})
- elseif data[2] == "register" then
- user = data[3]
- pass = data[4]
- transmit({data[1].."-", "auth",not(fs.exists("mailserver/"..user))})
- if fs.exists("mailserver/"..user) == false then
- print("mailserver/"..user.."/"..pass)
- shell.run("mkdir mailserver/"..user.."/"..pass)
- end
- elseif data[2] == "finduser" then
- user = data[3]
- transmit({data[1].."-", "usercheck",fs.exists("mailserver/"..user)})
- elseif data[2] == "sendmail" then
- from = data[3]
- to = data[4]
- title = data[5]
- message = data[6]
- transmit({data[1].."-", "sentmail",true})
- title = title:gsub(" ", "_")
- local h = fs.open("mailserver/" .. to .. "/inbox/new/"..title, "w")
- h.writeLine("Title: " .. title)
- h.writeLine("From: " .. from)
- h.writeLine("To: you(" .. to .. ")")
- h.writeLine("\n"..message)
- h.close()
- local h = fs.open("mailserver/" .. from .. "/inbox/sent/"..title, "w")
- h.writeLine("Title: " .. title)
- h.writeLine("From: " .. from)
- h.writeLine("To: you(" .. to .. ")")
- h.writeLine("\n"..message)
- h.close()
- elseif data[2] == "dir" then
- user = data[3]
- subdir = textutils.unserialize(data[4])
- sd =""
- for k, v in pairs(subdir) do
- if v ~= nil then
- sd = sd .. v .. "/"
- end
- end
- if fs.isDir("mailserver/"..user.."/inbox/"..sd) then
- transmit({data[1].."-", "ls_dir", textutils.serialize(fs.list("mailserver/"..user.."/inbox/"..sd))})
- else
- local h = fs.open("mailserver/"..user.."/inbox/"..sd, "r")
- message = h.readAll()
- h.close()
- if message == "" then
- message = "\n\n [Unknown Message]";
- end
- transmit({data[1].."-", "message", message})
- end
- elseif data[2] == "rm" then
- user = data[3]
- subdir = textutils.unserialize(data[4])
- sd =""
- for k, v in pairs(subdir) do
- if v ~= nil then
- sd = sd .. v .. "/"
- end
- end
- shell.run("rm mailserver/"..user.."/inbox/"..sd)
- elseif data[2] == "archive" then
- user = data[3]
- subdir = textutils.unserialize(data[4])
- sd =""
- for k, v in pairs(subdir) do
- if v ~= nil then
- sd = sd .. v .. "/"
- end
- end
- shell.run("mv mailserver/"..user.."/inbox/"..sd .. " mailserver/" .. user .. "/inbox/archive/" .. subdir[table.getn(subdir)])
- else
- end
- end
- elseif cl_type == "client" then
- while true do
- repeat
- clear()
- -------=========================-=========================
- print("===================================================")
- print("= Login or Register =")
- print("===================================================")
- print("")
- print("1: Login")
- print("2: Register")
- action=tonumber(read())
- tries = 0
- username = nil
- repeat
- if action == 1 then
- clear()
- -------=========================-=========================
- print("===================================================")
- print("= Login =")
- print("===================================================")
- print()
- if username ~= nil then
- print("Username or Password invalid!")
- end
- username,password = login()
- key = genKey(16)
- transmit({key, "login", username, password})
- elseif action == 2 then
- clear()
- -------=========================-=========================
- print("===================================================")
- print("= Registration =")
- print("===================================================")
- print()
- if username ~= nil then
- print("Username already taken!")
- end
- username,password = login()
- key = genKey(16)
- transmit({key, "register", username, password})
- end
- repeat
- data = receive()
- until string.sub(data[1],0,16) == key and data[2] == "auth"
- tries = tries + 1
- if tries >= 3 then
- action = ""
- end
- until data[3] == true or not(action == 1 or action == 2)
- until action == 1 or action == 2
- repeat
- clear()
- print("===================================================")
- print("= What do you want to do? =")
- print("===================================================")
- print("")
- print("1: Write a message to someone")
- print("2: Check if I have a new message")
- print("0: Exit")
- action=tonumber(read())
- if action == 1 then
- data = {}
- repeat
- clear()
- print("===================================================")
- print("= Mails =")
- print("===================================================")
- print()
- print("Please keep in mind titles are used in the inbox")
- print("That means you can overwrite mails")
- print("Also don't write too long mails. They cant be read")
- print()
- if data[3] == false then
- print("Target not found.")
- end
- print("Sending to: ")
- user=read()
- key = genKey(16)
- transmit({key, "finduser", user})
- repeat
- data = receive()
- until string.sub(data[1],0,16) == key and data[2] == "usercheck"
- until data[2] == "usercheck" and data[3] == true
- print("Title: ")
- title=read()
- print("Message: ")
- message=read()
- repeat
- key = genKey(16)
- transmit({key,"sendmail", username, user, title, message})
- repeat
- data = receive()
- until string.sub(data[1],0,16) == key and data[2] == "sentmail"
- until data[2] == "sentmail" and data[3] == true
- elseif action == 2 then
- local subdir = {}
- local page = 1
- repeat
- key = genKey(16)
- transmit({key, "dir", username, textutils.serialize(subdir)})
- repeat
- data = receive()
- until string.sub(data[1],0,16) == key and ( data[2] == "ls_dir" or data[2] == "message" )
- txt = ""
- dircontent = textutils.unserialize(data[3])
- if data[2] == "ls_dir" then
- for k, v in pairs(textutils.unserialize(data[3])) do
- if v ~= nil then
- txt = txt .. k .. ": " .. v .. "\n"
- end
- end
- clear()
- print("===================================================")
- print("= Mails =")
- print("===================================================")
- print("0: <back 1 folder>")
- print(txt)
- lines = select(2, txt:gsub('\n', '\n'))
- for i=1, 10 - lines do
- print()
- end
- print("===================================================")
- print("<- back 10 (B) EXIT e (F) 10 forward ->")
- print("===================================================")
- action=string.lower(read())
- if action == "b" then
- if page > 1 then
- page = page - 1
- end
- elseif action == "f" then
- if lines == 10 then
- page = page + 1
- end
- elseif action ~= "0" and tonumber(action) ~= nil then
- page = 1
- subdir[table.getn(subdir) +1] = dircontent[tonumber(action)]
- elseif action == "0" and tonumber(action) ~= nil then
- page = 1
- subdir[table.getn(subdir)] = nil
- end
- elseif data[2] == "message" then
- repeat
- clear()
- print("===================================================")
- print("= Message =")
- print("===================================================")
- print(data[3])
- lines = select(2, data[3]:gsub('\n', '\n'))
- for i=1, 11 - lines do
- print()
- end
- print("===================================================")
- print("= delete (D) EXIT e (A) archive=")
- print("===================================================")
- action=string.lower(read())
- if action == "d" then
- key = genKey(16)
- transmit({key, "rm", username, textutils.serialize(subdir)})
- action = "e"
- elseif action == "a" then
- key = genKey(16)
- transmit({key, "archive", username, textutils.serialize(subdir)})
- action = "e"
- end
- until action == "e"
- action = ""
- page = 1
- subdir[table.getn(subdir)] = nil
- end
- until action == "e"
- action = 2
- end
- until action == 0
- end
- elseif cl_type=="repeater" then
- modem.open(prt_transmit)
- modem.open(prt_receive)
- keys = {{"key",5},{"keyyyyy",1}}
- while true do
- local timeout = os.startTimer(1)
- while true do
- event = {os.pullEvent()}
- if event[1] == "modem_message" then
- -- local h = fs.open("config/repeater.log", "a")
- -- for k, v in pairs(event) do
- -- if v ~= nil then
- -- h.writeLine(k .. ": " .. v)
- -- end
- -- end
- -- h.writeLine("---------------------------------------------")
- -- h.close()
- donotsend = false
- for k, v in pairs( keys ) do
- val1 = textutils.unserialize(event[5])
- if v[1] == val1[1] then
- v[2] = 0
- donotsend = true
- end
- end
- print("transmiting: " .. tostring(donotsend))
- if not(donotsend) then
- modem.transmit(event[3], event[4], event[5])
- val1 = textutils.unserialize(event[5])
- keys[table.getn(keys) +1] = {val1[1], 0}
- end
- elseif event[1] == "timer" and event[2] == timeout then
- old_keys = keys
- keys = {}
- for k, v in pairs( old_keys ) do
- print(k, " ", v[1], " ", v[2])
- v[2] = v[2] + 1
- if v[2] < 4 then
- keys[table.getn(keys)+1] = old_keys[k]
- end
- end
- print("---------------")
- break
- end
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement