LDDestroier

Encmail Login Server

Apr 20th, 2017
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.44 KB | None | 0 0
  1. --Loading AES encryption functions. AES API ported to CC by SquidDev. Thanks heaps!
  2. local apipath
  3. if shell then apipath = fs.combine(shell.dir(),"aes") else apipath = "" end
  4. if (not aes) and (not fs.exists(apipath)) then
  5.     print("AES API not found! Downloading...")
  6.     local prog = http.get("http://pastebin.com/raw/9E5UHiqv")
  7.     if not prog then error("FAIL!") end
  8.     local file = fs.open(apipath,"w")
  9.     file.write(prog.readAll())
  10.     file.close()
  11. end
  12. if not aes then
  13.     local res = os.loadAPI(apipath)
  14.     if not res then error("Didn't load AES API!") end
  15. end
  16.  
  17. --In case I use an API that uses a different syntax than (msg, key)
  18. local encrite = function(msg, key)
  19.     return aes.encrypt(key, msg)
  20. end
  21.  
  22. local decrite = function(msg, key)
  23.     return aes.decrypt(key, msg)
  24. end
  25.  
  26. local encode = function(txt) --converts string into a table of each character's byte code
  27.     if type(txt) ~= "string" then return false, "requires string" end
  28.     return {txt:byte(1,-1)}
  29. end
  30.  
  31. local decode = function(tbl) --converts an encoded string into something useful.
  32.     if type(tbl) ~= "table" then return false, "requires table" end
  33.     return string.char(tableunpack(tbl))
  34. end
  35.  
  36. local strcapsule = function(txt)
  37.     return "\""..tostring(txt).."\""
  38. end
  39.  
  40. local cwrite = function(txt,setY,doClearLine)
  41.     local scr_x, scr_y = termgetSize()
  42.     local x,y = termgetCursorPos()
  43.     termsetCursorPos((scr_x/2)-(#txt/2),setY or y)
  44.     if doClearLine then termclearLine() end
  45.     write(txt)
  46. end
  47.  
  48. local waitForModem = function()
  49.     local mod
  50.     while true do
  51.         sleep(0.2)
  52.         mod = peripheral.find("modem")
  53.         if mod then
  54.             return mod
  55.         end
  56.     end
  57. end
  58.  
  59. if not peripheral.find("modem") then
  60.     termsetBackgroundColor(colors.gray)
  61.     termsetTextColor(colors.white)
  62.     termclear()
  63.     cwrite("Encmail requires a modem.",3)
  64.     cwrite("Add one, or press a key.",4)
  65.     sleep(0.1)
  66.     local outcome = parallelwaitForAny(function() os.pullEvent("key") end, waitForModem)
  67.     modems = {peripheral.find("modem")}
  68.     if #modems == 0 then
  69.         termsetBackgroundColor(colors.black)
  70.         termsetCursorPos(1,scr_y)
  71.         termclearLine()
  72.         sleep(0)
  73.         return false
  74.     end
  75. end
  76.  
  77. local modemOpen = function(chan)
  78.     for a = 1, #modems do
  79.         modems[a].open(chan)
  80.     end
  81. end
  82.  
  83. local modemClose = function(chan)
  84.     for a = 1, #modems do
  85.         modems[a].close(chan)
  86.     end
  87. end
  88.  
  89. local modemTransmit = function(chan,repchan,msg)
  90.     for a = 1, #modems do
  91.         modems[a].transmit(chan,repchan,msg)
  92.     end
  93. end
  94.  
  95. local tsv = function(visible)
  96.     if termcurrent then if termcurrent().setVisible then termcurrent().setVisible(visible) return true else return false end else return false end
  97. end
  98.  
  99.  
  100. local maindir = ".encmail"
  101. local userdir = fs.combine(maindir,"users")
  102. local configpath = fs.combine(maindir,"config")
  103.  
  104. local readConfig = function()
  105.     dofile(configpath)
  106. end
  107.  
  108. local writeConfig = function() --writes current values to config, no arguments needed!
  109.     local file = fs.open(configpath,"w")
  110.     file.writeLine("--This config file will store any variables I want persistant.")
  111.     file.writeLine("--Do not modify while running! It won't do a thing!")
  112.     file.close()
  113. end
  114.  
  115. local fenv = getfenv()
  116. readConfig()
  117. local newfenv = getfenv()
  118. local newvars = {}
  119. for k,v in pairs(newfenv) do
  120.     if not fenv[k] then
  121.         newvars[k] = v
  122.     end
  123. end
  124.  
  125. listUsers = function()
  126.     return fs.list(userdir)
  127. end
  128.  
  129. listMail = function(user)
  130.     if not fs.isDir(fs.combine(userdir,user)) then
  131.         return false, "No such user"
  132.     end
  133.     local output = {}
  134.     local path = fs.list(fs.combine(userdir,user))
  135.        
  136. end
Add Comment
Please, Sign In to add comment