Guest User

Untitled

a guest
Aug 1st, 2018
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.38 KB | None | 0 0
  1. -- Luanub's Login Script
  2.  
  3. local prog = ("launcher.lua") -- program to launch after sucessful password entry
  4. local defPass= ("password") -- default password
  5.  
  6. local uname = {}
  7. local passwd = {}
  8. local user = ""
  9.  
  10. local currentX = 1
  11. local currentY = 5
  12. local selection = 1
  13.  
  14. --[[
  15. function os.pullEvent()
  16. local event, p1, p2, p3, p4, p5 = os.pullEventRaw()
  17. if event == "terminate" then
  18. sleep(0)
  19. end
  20. return event, p1, p2, p3, p4, p5
  21. end
  22. ]]--
  23.  
  24. if not fs.exists(".users") then
  25. local file = io.open(".users", "w")
  26. file:write("admin,")
  27. file:close()
  28. end
  29.  
  30. if not fs.exists(".pass") then
  31. local file = io.open(".pass", "w")
  32. file:write(defPass..",")
  33. file:close()
  34. end
  35.  
  36. function pattern(text,pattern,start)
  37. return string.sub(text,string.find(text,pattern,start)) end
  38.  
  39. function split(string)
  40. local sep = {}
  41. local done = false
  42. local count,prevVars,tmp = 0,0,0
  43. string = string..",|"
  44. while not done do
  45. count = count + 1
  46. tmp = pattern(string,"[^,]+",count+prevVars)
  47. if tmp == "|" then done = true return sep end
  48. prevVars = prevVars + tmp:len()
  49. table.insert(sep,count,tmp)
  50. end
  51. return sep
  52. end
  53.  
  54.  
  55. function getUsers()
  56. local file = io.open(".users", "r")
  57. local usernames = file:read()
  58. file:close()
  59. uname = split(usernames)
  60. end
  61.  
  62. function getPass()
  63. local file = io.open(".pass", "r")
  64. local passwords = file:read()
  65. file:close()
  66. passwd = split(passwords)
  67. end
  68.  
  69. function changePass()
  70. while true do
  71. getPass()
  72. local a = table.maxn(uname)
  73. term.clear()
  74. term.setCursorPos(1,3)
  75. print ("Welcome "..user.."\n")
  76. write (" Enter Current Password: ")
  77. local pass = read("*")
  78. c = 0
  79. for x=1, a do
  80. c = c + 1
  81. if user == uname[c] and pass == passwd[c] then
  82. local sUser = uname[c]
  83. write ("\nChange your password? ")
  84. local input = read()
  85. print ()
  86. if input == "y" or input == "Y" then
  87. local test = "false"
  88. repeat
  89. write (" Enter new password: ")
  90. local sPass = read("*")
  91. write (" Verify new password: ")
  92. local sPass2 = read("*")
  93. if sPass == passwd[c] then
  94. term.setCursorPos(1,7)
  95. print ("Can not use your current password\n")
  96. elseif sPass == sPass2 then
  97. table.remove(passwd, c )
  98. table.insert(passwd, c, sPass)
  99. local newNum = table.maxn(passwd)
  100. local sPass = passwd[1]
  101. local g = newNum - 1
  102. local r = 2
  103. for x=1, g do
  104. sPass = sPass..","..passwd[r]
  105. r = r + 1
  106. end
  107. local file = io.open(".pass", "w")
  108. file:write(sPass..",")
  109. file:close()
  110. print ("\nPassword Updated")
  111. sleep(2)
  112. test = "true"
  113. else
  114. term.setCursorPos(1,7)
  115. print ("Passwords entered do not match, try again\n")
  116. end
  117. until test == "true"
  118. end
  119. if sUser == "admin" then
  120. adminMenu()
  121. return
  122. end
  123. loginMenu()
  124. return
  125. end
  126. end
  127. print ("Invalid User/Password Entered")
  128. sleep(2)
  129. end
  130. end
  131.  
  132. function drawCursor()
  133. term.setCursorPos(currentX, currentY)
  134. write("->")
  135. end
  136.  
  137. function addUser()
  138. term.clear()
  139. term.setCursorPos(1,6)
  140. write ("Username: ")
  141. local sName = read()
  142. local file = io.open(".users", "a")
  143. file:write(sName..",")
  144. file:close()
  145. term.clear()
  146. term.setCursorPos(1,6)
  147. local file = io.open(".pass", "a")
  148. file:write(defPass..",")
  149. file:close()
  150. print ("User "..sName.." has been added")
  151. sleep(2)
  152. end
  153.  
  154. function adminUser()
  155. currentX = 1
  156. currentY = 5
  157. selection = 1
  158. while true do
  159. getUsers()
  160. getPass()
  161. local num = table.maxn(uname)
  162. term.clear()
  163. term.setCursorPos(1, 3)
  164. print ("User Administration Menu")
  165. print ("------------------------")
  166. local n = 1
  167. for x=1, num do
  168. print (" "..uname[n])
  169. n = n + 1
  170. end
  171. print ("\n Add User")
  172. print (" Menu")
  173. local c = num + 1
  174. local d = num + 2
  175. if num == 0 then
  176. selection = c
  177. currentY = 6
  178. end
  179. drawCursor()
  180. local e,key, msg = os.pullEvent()
  181. if e == "key" then
  182. if key == 17 or key == 200 then
  183. if selection - 1 == num then
  184. selection = selection - 1
  185. currentY = currentY - 2
  186. elseif selection - 1 >= 1 then
  187. selection = selection - 1
  188. currentY = currentY - 1
  189. end
  190. elseif key == 31 or key == 208 then
  191. if selection + 1 <= num then
  192. selection = selection + 1
  193. currentY = currentY + 1
  194. elseif selection + 1 == c then
  195. selection = selection + 1
  196. currentY = currentY + 2
  197. elseif selection + 1 == d then
  198. selection = selection + 1
  199. currentY = currentY + 1
  200. end
  201. elseif key == 28 then
  202. if selection == c then
  203. addUser()
  204. currentX = 1
  205. currentY = 5
  206. selection = 1
  207. elseif selection == d then
  208. adminMenu()
  209. break
  210. elseif selection == 1 then
  211. term.clear()
  212. term.setCursorPos(1,6)
  213. print ("Unable to delete user")
  214. sleep (2)
  215. elseif selection >=2 or selection <= num then
  216. term.clear()
  217. term.setCursorPos(1,6)
  218. local sPlayer = uname[selection]
  219. write ("Delete User "..sPlayer.." y/n? ")
  220. local input = read()
  221. if input == "y" or input == "Y" then
  222. table.remove(uname, selection )
  223. table.remove(passwd, selection )
  224. local newNum = table.maxn(uname)
  225. local sUsers = uname[1]
  226. local f = newNum - 1
  227. local q = 2
  228. for x=1, f do
  229. sUsers = sUsers..","..uname[q]
  230. q = q + 1
  231. end
  232. local sPass = passwd[1]
  233. local g = newNum - 1
  234. local r = 2
  235. for x=1, g do
  236. sPass = sPass..","..passwd[r]
  237. r = r + 1
  238. end
  239. local file1 = io.open(".users", "w")
  240. file1:write(sUsers..",")
  241. file1:close()
  242. local file2 = io.open(".pass", "w")
  243. file2:write(sPass..",")
  244. file2:close()
  245. print ("User "..sPlayer.." has been deleted.")
  246. sleep(2)
  247. end
  248. currentX = 1
  249. currentY = 5
  250. selection = 1
  251. else
  252. print("Error: Selection out of bounds: ", selection)
  253. read()
  254. end
  255. end
  256. end
  257. end
  258. end
  259.  
  260. function adminPass()
  261. currentX = 1
  262. currentY = 5
  263. selection = 1
  264. while true do
  265. getUsers()
  266. getPass()
  267. local num = table.maxn(passwd)
  268. term.clear()
  269. term.setCursorPos(1, 3)
  270. print ("Password Administration Menu")
  271. print ("----------------------------")
  272. local n = 1
  273. for x=1, num do
  274. print (" "..uname[n])
  275. n = n + 1
  276. end
  277. print ("\n Menu")
  278. print("\nSelect User to reset password")
  279. local c = num + 1
  280. drawCursor()
  281. if num == 0 then
  282. selection = c
  283. currentY = 6
  284. end
  285. drawCursor()
  286. local e,key, msg = os.pullEvent()
  287. if e == "key" then
  288. if key == 17 or key == 200 then
  289. if selection - 1 == num then
  290. selection = selection - 1
  291. currentY = currentY - 2
  292. elseif selection - 1 >= 1 then
  293. selection = selection - 1
  294. currentY = currentY - 1
  295. end
  296. elseif key == 31 or key == 208 then
  297. if selection + 1 <= num then
  298. selection = selection + 1
  299. currentY = currentY + 1
  300. elseif selection + 1 == c then
  301. selection = selection + 1
  302. currentY = currentY + 2
  303. end
  304. elseif key == 28 then
  305. if selection == c then
  306. adminMenu()
  307. break
  308. elseif selection >=1 or selection <= num then
  309. term.clear()
  310. term.setCursorPos(1,6)
  311. local sPlayer = uname[selection]
  312. write ("Reset "..sPlayer.."'s password y/n? ")
  313. local input = read()
  314. if input == "y" or input == "Y" then
  315. table.remove(passwd, selection)
  316. table.insert(passwd, selection, defPass)
  317. local newNum = table.maxn(passwd)
  318. local sPass = passwd[1]
  319. local g = newNum - 1
  320. local r = 2
  321. for x=1, g do
  322. sPass = sPass..","..passwd[r]
  323. r = r + 1
  324. end
  325. local file = io.open(".pass", "w")
  326. file:write(sPass..",")
  327. file:close()
  328. print ("User "..sPlayer.." password has been reset.")
  329. sleep(2)
  330. end
  331. currentX = 1
  332. currentY = 5
  333. selection = 1
  334. else
  335. print("Error: Selection out of bounds: ", selection)
  336. read()
  337. end
  338. end
  339. end
  340. end
  341. end
  342.  
  343. function adminMenu()
  344. currentX = 1
  345. currentY = 5
  346. selection = 1
  347. while true do
  348. term.clear()
  349. term.setCursorPos(1, 3)
  350. print (" Select Action")
  351. print ("---------------------")
  352. print ("| User Maintenance |")
  353. print ("| Reset Passwords |")
  354. print ("| Change Password |")
  355. print ("| Exit |")
  356. print ("---------------------")
  357. drawCursor()
  358. local e, key = os.pullEvent("key")
  359. if key == 17 or key == 200 then
  360. if selection - 1 >= 1 then
  361. selection = selection - 1
  362. currentY = currentY -1
  363. end
  364. elseif key == 31 or key == 208 then
  365. if selection + 1 <= 4 then
  366. selection = selection + 1
  367. currentY = currentY +1
  368. end
  369. elseif key == 28 then
  370. if selection == 1 then
  371. adminUser()
  372. break
  373. elseif selection == 2 then
  374. adminPass()
  375. break
  376. elseif selection == 3 then
  377. changePass()
  378. break
  379. elseif selection == 4 then
  380. loginMenu()
  381. break
  382. else
  383. print("Error: Selection out of bounds: ", selection)
  384. read()
  385. end
  386. end
  387. end
  388. end
  389.  
  390. function loginMenu()
  391. while true do
  392. getUsers()
  393. getPass()
  394. local a = table.maxn(uname)
  395. term.clear()
  396. term.setCursorPos(1, 3)
  397. write (" Username: ")
  398. user = read()
  399. write (" Password: ")
  400. local pass = read("*")
  401. c = 0
  402. for x=1, a do
  403. c = c + 1
  404. if user == uname[c] and pass == passwd[c] then
  405. print ("\nLogin Sucessful")
  406. sleep(1)
  407. if user == "admin" then
  408. adminMenu()
  409. return
  410. end
  411. write ("\nChange Password y/n? ")
  412. local input = read()
  413. if input == "y" or input == "Y" then
  414. changePass()
  415. return
  416. end
  417. shell.run(prog)
  418. return
  419. end
  420. end
  421. print ("Invalid Username or Password Entered")
  422. sleep(2)
  423. end
  424. end
  425.  
  426. loginMenu()
Add Comment
Please, Sign In to add comment