Advertisement
natedrake

autho

Jun 6th, 2016
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.97 KB | None | 0 0
  1. --[[
  2.   @author natedrake1234567
  3.   @version 0.1a
  4.   @note simple mail server
  5. --]]
  6.  
  7. os.loadAPI("/conf");
  8. os.loadAPI("/src/core/timeMgmt");
  9. os.loadAPI("/src/core/netMgmt");
  10. os.loadAPI("/src/core/printMgmt");
  11.  
  12. --[[
  13.     @note version info
  14. --]]
  15. local appName="autho";
  16. local version="0.1a";
  17.  
  18. --[[ GloBal Variables/Config ]]--
  19.  
  20. local tArgs={...};
  21.  
  22. local termWidth, termHeight=term.getSize();
  23. local selectedIndex=1;
  24. local inMenu=true;
  25.  
  26. local menuOptions = {
  27.     [1] = {
  28.         text="Login",
  29.         handler=function()
  30.             local loggedIn = false;
  31.             while loggedIn == false do
  32.                 username, password = printLoginScreen();
  33.                 if login(username, password) then
  34.                     loggedIn = true;
  35.                     shell.run("/testMailClient0.1", tostring(username));
  36.                 else
  37.                     loggedIn = false;
  38.                 end
  39.             end
  40.         end
  41.     },
  42.     [2] = {
  43.         text="Create",
  44.         handler=function()
  45.             local accountCreated = false;
  46.             while accountCreated == false do
  47.                 printMgmt.printCentered("Name:", 9);
  48.                 name=read();
  49.                 printMgmt.printCentered("Username:", 10);
  50.                 username=read();
  51.                 printMgmt.printCentered("Password:", 11);
  52.                 password=read();
  53.                 printMgmt.printCentered("Confirm:", 11);
  54.                 confPassword=read();
  55.  
  56.                 if password == confPassword then
  57.                     createUser(name, username, password);
  58.                 else
  59.                     printMgmt.clearScreen();
  60.                     printMgmt.printCentered("Passwords do not match", 7);
  61.                 end
  62.             end
  63.         end
  64.     }
  65. }
  66.  
  67. function printHeader()
  68.     printMgmt.printCentered("Minceraft PC", 2);
  69.     term.setTextColor(colors.blue);
  70.     printMgmt.printLeft("Powered by "..appName.." "..version, termHeight);
  71.     term.setTextColor(colors.white);
  72. end
  73.  
  74. function printMenu(menu)
  75.     --printMgmt.clearScreen();
  76.     printHeader();
  77.     term.setCursorPos(16, termHeight/2);
  78.     for i=1, #menu do
  79.         if i==selectedIndex then
  80.             term.setTextColor(colors.green);
  81.             term.write("["..menu[i].text.."]".."\t\t\t");
  82.         else
  83.             term.setTextColor(colors.white);
  84.             term.write(" "..menu[i].text.." \t\t\t");
  85.         end
  86.     end
  87.     term.setTextColor(colors.white);
  88. end
  89.  
  90. function printLoginScreen()
  91.     printMgmt.clearScreen();
  92.     printHeader();
  93.  
  94.     printMgmt.printPos("Username:", 17, 10);
  95.     username = read();
  96.     printMgmt.printPos("Password:", 17, 11);
  97.     password = read();
  98.     return username, password;
  99. end
  100.  
  101. function onKeyPressed(key, menu)
  102.     if key == keys.enter then
  103.         onItemSelected(menu)
  104.     elseif key == keys.left then
  105.         if selectedIndex > 1 then
  106.             selectedIndex = selectedIndex-1;
  107.         end
  108.     elseif key == keys.right then
  109.         if selectedIndex < #menu then
  110.             selectedIndex=selectedIndex+1;
  111.         end
  112.     end
  113. end
  114.  
  115. function onItemSelected(menu)
  116.     menu[selectedIndex].handler();
  117. end
  118.  
  119.  
  120. -- @note init function
  121. function init()
  122.     if not netMgmt.isOpen() then
  123.         netMgmt.open();
  124.     end
  125. end
  126.  
  127. function createUser(name, username, password)
  128.     local messageObject = {
  129.         type="auth0Create",
  130.         userAgent=conf.name..";"..conf.version,
  131.         timestamp=timeMgmt.getCurrentRealTime(),
  132.         data={
  133.             name=name,
  134.             username=username,
  135.             password=password
  136.         }
  137.     }
  138.     rednet.send(conf.serverID, messageObject);
  139.  
  140.     senderID, message, distance, protocol = rednet.receive();
  141.  
  142.     if message.type == "auth0CreateSuccess" then
  143.         shell.run("/testMailClient0.1", tostring(username));
  144.     else
  145.         printMgmt.printCentered(message.message, 7);
  146.     end
  147. end
  148.  
  149. function login(username, password)
  150.     local messageObject = {
  151.         type="auth0Login",
  152.         userAgent=conf.name..";"..conf.version,
  153.         timestamp=timeMgmt.getCurrentRealTime(),
  154.         data={
  155.             username=username,
  156.             password=password
  157.         }
  158.     }
  159.     rednet.send(conf.serverID, messageObject);
  160.  
  161.     senderID, message, distance, protocol = rednet.receive();
  162.  
  163.     if message.type == "auth0UserFound" then
  164.         return true;
  165.     else
  166.         printMgmt.clearScreen()
  167.         term.setTextColor(colors.red);
  168.         printMgmt.printCentered("Incorrect Login Please Try Again!", 5);
  169.         term.setTextColor(colors.white);
  170.         return false;
  171.     end
  172.  
  173. end
  174.  
  175.  
  176. function main()
  177.     init();
  178.     while inMenu do
  179.         printMenu(menuOptions);
  180.         event, key=os.pullEvent("key");
  181.         onKeyPressed(key, menuOptions);
  182.     end    
  183. end
  184.  
  185. main();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement