wuiyang

Login server

Jan 13th, 2013
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.62 KB | None | 0 0
  1. print("this computer ID is "..os.getComputerID())
  2. sleep(0.1)
  3. print("-------------------------------")
  4. print("this server cant do any thing, but it save the username and password")
  5.  
  6. print("changelog")
  7. print("CURRENT:")
  8. print("v0.3.4 - Security question!")
  9. print("--------------------------------------------")
  10. print("v0.3.3 - Updated, you dont need testsave()")
  11. print("v0.3.2 - added Nick checking")
  12. print("v0.3   - NICKNAME!")
  13. print("v0.2   - allow to change username & password")
  14. print("v0.1   - Published")
  15. print("--------------------------------------------")
  16. print("Pre-Pre-Alpla version")
  17. print("v0.05  - fixed some bug")
  18. print("v0.01  - born")
  19.  
  20. function load(name)
  21. if not fs.exists(name) then
  22. local file = fs.open(name,"a")
  23. local teh = {"test", "test"}
  24. file.write(textutils.serialize(teh))
  25. file.close()
  26. return textutils.unserialize(sContents)
  27. else
  28. local file = fs.open(name,"r")
  29. local sContents = file.readAll()
  30. file.close()
  31. return textutils.unserialize(sContents)
  32. end
  33. end
  34.  
  35. function adds(ID, tContents)
  36. rednet.send(ID, "qdone")
  37. fs.delete("security.txt")
  38. local file = fs.open("security.txt","a")
  39. file.write(textutils.serialize(tContents))
  40. file.close()
  41. end
  42.  
  43. function se(username, question, password)
  44. local ID, MSG, DISTANCE = rednet.receive(10)
  45. if MSG == "could" then
  46. local tContents = load("security.txt")
  47. if tContents[1] == "test" and tContents[1] == "test" then
  48. table.remove(tContents, 3)
  49. table.remove(tContents, 2)
  50. table.remove(tContents, 1)
  51. table.insert(tContents, username)
  52. table.insert(tContents, question)
  53. table.insert(tContents, password)
  54. adds(ID, tContents)
  55. else
  56. local i = 1
  57. repeat
  58. if username == tContents[i] then
  59. local o = i + 1
  60. local n = o + 1
  61. table.remove(tContents, n)
  62. table.remove(tContents, o)
  63. table.insert(tContents, o, question)
  64. table.insert(tContents, n, password)
  65. adds(ID, tContents)
  66. does = "no"
  67. elseif tConetents[i] == nil or tConetents[i] == "" then
  68. table.insert(tContents, username)
  69. table.insert(tContents, question)
  70. table.insert(tContents, password)
  71. adds(ID, tContents)
  72. does = "no"
  73. else
  74. i = i + 3
  75. end
  76. until does == "no"
  77. does = "yes"
  78. end
  79. end
  80. end
  81.  
  82. function nickadd(username, nick)
  83. local test = load("nick.txt")
  84. if test[1] == "test" and test[2] == "test" then
  85. table.remove(test, 2)
  86. table.remove(test, 1)
  87. table.insert(test, username)
  88. table.insert(test, nick)
  89. else
  90. table.insert(test, username)
  91. table.insert(test, nick)
  92. end
  93. fs.delete("nick.txt")
  94. local file = fs.open("nick.txt","a")
  95. file.write(textutils.serialize(test))
  96. file.close()
  97. end
  98.  
  99. function nickedit(i, nick)
  100. local test = load("nick.txt")
  101. table.remove(test, i)
  102. table.insert(test, i, nick)
  103. fs.delete("nick.txt")
  104. local file = fs.open("nick.txt","a")
  105. file.write(textutils.serialize(test))
  106. file.close()
  107. end
  108.  
  109. function nickn(username, nick)
  110. local ID, MSG, DISTANCE = rednet.receive(10)
  111. if MSG == "could" then
  112. tContents = load("nick.txt")
  113. local i = 2
  114.  
  115. repeat
  116. if nick == tContents[i] then
  117. rednet.send(ID, "non")
  118. does = "no"
  119.  
  120. elseif tContents[i] == nil or tContents[i] == "" then
  121. rednet.send(ID, "finishn")
  122. local o = 1
  123. repeat
  124. if username == tContents[o] then
  125. n = o+1
  126. nickedit(n, nick)
  127. does = "no"
  128. does2 = "no"
  129. elseif tContents[o] == nil or tContents[o] == "" then
  130. nickadd(username, nick)
  131. does = "no"
  132. does2 = "no"
  133. else
  134. o = o + 2
  135. end
  136.  
  137. until does2 == "no"
  138. does2 = "yes"
  139.  
  140. else
  141. i = i + 2
  142. end
  143.  
  144. until does == "no"
  145. does = "yes"
  146. end
  147. end
  148.  
  149. function nickc(username)
  150. local ID, MSG, DISTANCE = rednet.receive(10)
  151. if MSG == "could" then
  152. tContents = load("nick.txt")
  153. i = 1
  154. repeat
  155. if username == tContents[i] then
  156. local this = load("nick.txt")
  157. n = i + 1
  158. rednet.send(ID, "yes")
  159. rednet.send(ID, this[n])
  160. does = "no"
  161.  
  162. elseif tContents[i] == nil or tContents[i] == "" then
  163. rednet.send(ID, "nope")
  164. does = "no"
  165. else
  166. i = i + 2
  167. end
  168.  
  169. until does == "no"
  170. does = "yes"
  171. end
  172. end
  173.  
  174. function edit(i, txt)
  175. local test = load("user.txt")
  176. table.remove(test, i)
  177. table.insert(test, i, txt)
  178. fs.delete("user.txt")
  179. local file = fs.open("user.txt","a")
  180. file.write(textutils.serialize(test))
  181. file.close()
  182. end
  183.  
  184. function save(username, password)
  185. local test
  186. test = load("user.txt")
  187. if test[i] == "test" and test[2] == "test" then
  188. table.remove(test, 2)
  189. table.remove(test, 1)
  190. table.insert(test, username)
  191. table.insert(test, password)
  192. else
  193. table.insert(test, username)
  194. table.insert(test, password)
  195. end
  196. fs.delete("user.txt")
  197. local file = fs.open("user.txt","a")
  198. file.write(textutils.serialize(test))
  199. file.close()
  200. end
  201.  
  202. function add(username, password)
  203. local ID, MSG, DISTANCE = rednet.receive(10)
  204. if MSG == "could" then
  205. local tContents = load("user.txt")
  206. local i = 1
  207.  
  208. repeat
  209. if username == tContents[i] then
  210. rednet.send(ID, "no")
  211. does = "no"
  212.  
  213. elseif tContents[i] == nil or tContents[i] == "" then
  214. save(username, password)
  215. rednet.send(ID, "complete")
  216. does = "no"
  217.  
  218. else
  219. i = i + 2
  220. end
  221.  
  222. until does == "no"
  223. does = "yes"
  224. end
  225. end
  226.  
  227. function search(username, password)
  228. local ID, MSG, DISTANCE = rednet.receive(10)
  229. if MSG == "could" then
  230. local tContents = load("user.txt")
  231. local i = 1
  232.  
  233. repeat
  234. if username == tContents[i] then
  235. o = i + 1
  236.  
  237. if password == tContents[o] then
  238. if username == "admin" then
  239. rednet.send(ID, "admin")
  240. does = "no"
  241. else
  242. rednet.send(ID, "correct")
  243. does = "no"
  244. end
  245.  
  246. else
  247. rednet.send(ID, "pass")
  248. does = "no"
  249. end
  250.  
  251. elseif tContents[i] == nil or tContents[i] == "" then
  252. rednet.send(ID, "wrong")
  253. does = "no"
  254.  
  255. else
  256. i = i + 2
  257. end
  258.  
  259. until does == "no"
  260. does = "yes"
  261. end
  262. end
  263.  
  264. function change(username, password, wordpass)
  265. local ID, MSG, DISTANCE = rednet.receive(10)
  266. if MSG == "could" then
  267. local tContents = load("user.txt")
  268. local i = 1
  269.  
  270. repeat
  271. if username == tContents[i] then
  272. o = i + 1
  273.  
  274. if password == tContents[o] then
  275. edit(o, wordpass)
  276. rednet.send(ID, "finish")
  277. does = "no"
  278.  
  279. else
  280. rednet.send(ID, "wrongp")
  281. does = "no"
  282. end
  283.  
  284. else
  285. i = i + 2
  286. end
  287.  
  288. until does == "no"
  289. does = "yes"
  290. end
  291. end
  292.  
  293. function change2(username, nameuser)
  294. local ID, MSG, DISTANCE = rednet.receive(10)
  295. if MSG == "could" then
  296. local tContents = load("user.txt")
  297. local i = 1
  298.  
  299. repeat
  300. if username == tContents[i] then
  301. local o = 1
  302. repeat
  303. if nameuser == tContents[o] then
  304. rednet.send(ID, "nou")
  305. does2 = "no"
  306. does = "no"
  307.  
  308. elseif tContents[o] == nil or tContents[o] == "" then
  309. edit(i, nameuser)
  310. rednet.send(ID, "finish")
  311. does2 = "no"
  312. does = "no"
  313.  
  314. else
  315. o = o + 2
  316. end
  317.  
  318. until does2 == "no"
  319. does2 = "yes"
  320.  
  321. else
  322. i = i + 2
  323. end
  324.  
  325. until does == "no"
  326. does = "yes"
  327. end
  328. end
  329.  
  330. rednet.open("back")
  331.  
  332. local username,password,nameuser,wordpass,nick,question,answer
  333.  
  334. while true do
  335. local ID, MSG, DISTANCE = rednet.receive()
  336. if MSG == "user" then
  337. local ID, MSG, DISTANCE = rednet.receive(10)
  338. username = MSG
  339. end
  340.  
  341. if MSG == "user2" then
  342. local ID, MSG, DISTANCE = rednet.receive(10)
  343. nameuser = MSG
  344. end
  345.  
  346. if MSG == "pass" then
  347. local ID, MSG, DISTANCE = rednet.receive(10)
  348. password = MSG
  349. end
  350.  
  351. if MSG == "pass2" then
  352. local ID, MSG, DISTANCE = rednet.receive(10)
  353. wordpass = MSG
  354. end
  355.  
  356. if MSG == "nick" then
  357. local ID, MSG, DISTANCE = rednet.receive(10)
  358. nick = MSG
  359. end
  360.  
  361. if MSG == "ques" then
  362. local ID, MSG, DISTANCE = rednet.receive(10)
  363. question = MSG
  364. end
  365.  
  366. if MSG == "ans" then
  367. local ID, MSG, DISTANCE = rednet.receive(10)
  368. answer = MSG
  369. end
  370.  
  371. if MSG == "r" then
  372. add(username, password)
  373. end
  374.  
  375. if MSG == "l" then
  376. search(username, password)
  377. end
  378.  
  379. if MSG == "cn" then
  380. change2(username, nameuser)
  381. end
  382.  
  383. if MSG == "cp" then
  384. change(username, password, wordpass)
  385. end
  386.  
  387. if MSG == "nc" then
  388. nickn(username, nick)
  389. end
  390.  
  391. if MSG == "check" then
  392. nickc(username)
  393. end
  394.  
  395. if MSG == "secur" then
  396. se(username, question, answer)
  397. end
  398.  
  399. if MSG == "hi" then
  400. rednet.send(ID, "yes2")
  401. end
  402.  
  403. end
Advertisement
Add Comment
Please, Sign In to add comment