Advertisement
Guest User

Untitled

a guest
Jan 15th, 2018
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.01 KB | None | 0 0
  1. h roaming accounts
  2. Discussion in 'ComputerCraft Programming' started by Azimath, Jan 19, 2013.
  3.  
  4. Azimath
  5. Azimath
  6. Master Zepplin Thief
  7. Joined:Jan 22, 2012
  8. Messages:30
  9. Likes Received:6
  10. Ive seen email systems done in the past, but here's the first usable version of mine. I spent a lot of time writing this stuff at 3am, so it probably needs some cleaning up.
  11.  
  12. It involves a few separate systems: a login server, a DNS-like server, an email server, and an email client.
  13.  
  14. The login server is similar to the one from the wiki, but with changes to the way it works to make it more secure, and allow the server owner to add/remove accounts on the fly. The code comes with an account for the email server to use by default, so you need to add user accounts. To do so , just start the program and press the 'a' key. It will ask for a name and a password, then confirm adding the user to the list. Removing is just pressing the 'r' key, entering the username, and confirming. You can press 'm' to view the list of users currently in the system.
  15.  
  16. DANGER: since this is an early version, anything in any of the servers not hardcoded will not persist after a crash/reboot
  17.  
  18. Code:
  19. pullEvent = os.pullEvent
  20. os.pullEvent = os.pullEventRaw
  21. users = {"mailserver"}
  22. passwords = {"legomail"}
  23. term.clear()
  24. term.setCursorPos(1,1)
  25. print("Legoman technologies authentication server running")
  26. id = os.computerID()
  27. term.setCursorPos(1,2)
  28. print("Computer ID = "..id)
  29.  
  30. local firstCycle = true
  31. local modemSide = "right"
  32. local valid = false
  33. local DNSID = 2
  34.  
  35. while true do
  36. if firstCycle then
  37. rednet.open(modemSide)
  38. firstCycle = false
  39. end
  40. event, p1, p2, p3 = os.pullEvent()
  41. term.clear()
  42. term.setCursorPos(1,1)
  43. print("Legoman technologies authentication server running")
  44. id = os.computerID()
  45. term.setCursorPos(1,2)
  46. print("Computer ID = "..id)
  47.  
  48. if event =="char" then
  49. if p1 == "m" then
  50. for i, v in ipairs(users) do
  51. print(v)
  52. end
  53. end
  54. if p1 == "a" then
  55. term.setCursorPos(1,10)
  56. print("Enter name: ")
  57. name = read()
  58. print("Enter password: ")
  59. password = read()
  60. print("Confim add " .. name .. " Y/N?")
  61. if read() == "Y" then
  62. table.insert(users, name)
  63. table.insert(passwords, password)
  64. print("Added")
  65. end
  66. end
  67. if p1 == "r" then
  68. print("Enter name: ")
  69. name = read()
  70. print("Confim remove " .. name .. " Y/N?")
  71. if read() == "Y" then
  72. for i,v in ipairs(users) do
  73. if name == v then
  74. table.remove(users, i)
  75. table.remove(passwords, i)
  76. end
  77. end
  78. end
  79. end
  80. end
  81.  
  82. if event == "terminate" then
  83. term.setCursorPos(1,10)
  84. print("Enter password: ")
  85. if read("*") == "unlock" then
  86. os.pullEvent = pullEvent
  87. shell.exit()
  88. end
  89. end
  90.  
  91. if event == "rednet_message" then
  92. senderId = p1
  93. message = p2
  94. term.setCursorPos(1,4)
  95. term.clearLine()
  96. print(message)
  97. for i,v in ipairs(users) do
  98. if message == v then
  99. valid = true
  100. password = passwords[i]
  101. user = users[i]
  102. break
  103. else
  104. valid = false
  105. end
  106. end
  107.  
  108. if valid then
  109. rednet.send(senderId, "Valid")
  110. print("Valid")
  111. else
  112. rednet.send(senderId, "Not Valid")
  113. print("Not valid")
  114. end
  115. sender, passwordRcv, dist = rednet.receive(3)
  116. print(sender)
  117. if passwordRcv == password then
  118. rednet.send(senderId, "Authenticated")
  119. rednet.send(DNSID, "Add " .. senderId .. " " .. user)
  120. print("Authenticated")
  121. else
  122. rednet.send(senderId, "Failed")
  123. end
  124. end
  125. end
  126. There is a variable near the start named DNSID. you need to change that to the id of the next computer, the DNS server.
  127.  
  128. When someone logs in, the login server tells the DNS server the name and id of the person who logged in, so others can check the names of users and the ids to send things to. It also supports listing the names in the system using the 'm' key. The variable AUTHID needs to be set to the id of the login server for it to accept new entries, and it is a good idea to give the ids table correct ids for the login and DNS server as well. Currently, if it gets multiple commands to add the same name, the one added first will be the one that counts. If you send a remove message, it will delete you from its table as a means of logging out.
  129. Code:
  130. pullEvent = os.pullEvent
  131. os.pullEvent = os.pullEventRaw
  132.  
  133. names = {"authserver","dnsserver"}
  134. ids = {0,1}
  135.  
  136.  
  137. term.clear()
  138. term.setCursorPos(1,1)
  139. print("Legoman technologies DNS server running")
  140. myid = os.computerID()
  141. term.setCursorPos(1,2)
  142. print("Computer ID = " .. myid)
  143.  
  144. local firstCycle = true
  145. local modemSide = "right"
  146. local AUTHID = 1
  147.  
  148. while true do
  149. if firstCycle then
  150. rednet.open(modemSide)
  151. firstCycle = false
  152. end
  153.  
  154. event, p1, p2, p3 = os.pullEvent()
  155.  
  156. term.clear()
  157. term.setCursorPos(1,1)
  158. print("Legoman technologies DNS server running")
  159. myid = os.computerID()
  160. term.setCursorPos(1,2)
  161. print("Computer ID = " .. myid)
  162.  
  163. if event == "terminate" then
  164. term.setCursorPos(1,10)
  165. print("Enter password: ")
  166. if read("*") == "unlock" then
  167. os.pullEvent = pullEvent
  168. shell.exit()
  169. end
  170. end
  171.  
  172. if event =="char" then
  173. if p1 == "m" then
  174. for i, v in ipairs(names) do
  175. print(v)
  176. end
  177. end
  178. end
  179. if event == "rednet_message" then
  180. senderId = p1
  181. message = p2
  182. term.setCursorPos(1,4)
  183. term.clearLine()
  184. if string.match(message, 'Id') == "Id" then
  185. print("Command: " .. string.match(message, 'Id'))
  186. idLookup = string.match(message, '%d+') + 0
  187. term.clearLine()
  188. print("Param: " .. idLookup)
  189. for i, v in ipairs(ids) do
  190. if idLookup == v then
  191. nameLookup = names[i]
  192. term.clearLine()
  193. print("Return: " .. nameLookup)
  194. rednet.send(senderId, nameLookup)
  195. break
  196. end
  197. end
  198. end
  199. if string.match(message, 'Name') == "Name" then
  200. print("Command: " .. string.match(message, 'Name'))
  201. nameLookup = string.match(message, '%a+', 5)
  202. term.clearLine()
  203. print("Param: " .. nameLookup)
  204. for i, v in ipairs(names) do
  205. if nameLookup == v then
  206. idLookup = ids[i]
  207. term.clearLine()
  208. print("Return: " .. idLookup)
  209. rednet.send(senderId, idLookup.."")
  210. break
  211. end
  212. end
  213. end
  214. if string.match(message, 'Add') == "Add" and senderId + 0 == AUTHID then
  215. print("Command: " .. string.match(message, 'Add'))
  216. nameAdd = string.match(message, '%a+', 4)
  217. idAdd = string.match(message, '%d+', 4)
  218. print("Params: " .. nameAdd .. ", " .. idAdd)
  219. table.insert(names, nameAdd)
  220. table.insert(ids, idAdd+0)
  221. end
  222. if string.match(message, 'Remove') == "Remove" then
  223. print("Command: " .. string.match(message, 'Remove'))
  224. term.clearLine()
  225. print("Param: " .. senderId)
  226. for i, v in ipairs(ids) do
  227. if senderId == v then
  228. table.remove(ids, i)
  229. table.remove(names, i)
  230. end
  231. end
  232. end
  233. end
  234. end
  235. The third and final server in this system is the email server itself. It needs the login and DNS server ids coded into the AUTHID and DNSID variables, respectively. Pressing 'm' lists the recipients of the messages in the system, 'a' adds a user to the list of people who can use email, and 'l' tries to update its id in the DNS server. It works by sending and receiving a variety of messages to and from the clients, and checking who each one is using the reverse DNS function of the DNS server.
  236. Code:
  237. pullEvent = os.pullEvent
  238. os.pullEvent = os.pullEventRaw
  239.  
  240. users = {"test"}
  241. messages = {"blank"}
  242. recipients = {"test"}
  243.  
  244.  
  245. term.clear()
  246. term.setCursorPos(1,1)
  247. print("Legoman technologies email server running")
  248. myid = os.computerID()
  249. term.setCursorPos(1,2)
  250. print("Computer ID = " .. myid)
  251.  
  252. local firstCycle = true
  253. local modemSide = "right"
  254. local AUTHID = 0
  255. local DNSID = 1
  256. local senderValid = false
  257. local recipientValid = false
  258.  
  259. function DNSreverse (Id)
  260. rednet.open(modemSide)
  261. rednet.send(DNSID, "Id " .. Id)
  262. local temp, message = rednet.receive(1)
  263. return message
  264. end
  265.  
  266. function login()
  267. rednet.send(AUTHID, "mailserver")
  268. os.sleep(1)
  269. rednet.send(AUTHID, "legomail")
  270. end
  271.  
  272. while true do
  273. if firstCycle then
  274. rednet.open(modemSide)
  275. firstCycle = false
  276. rednet.send(DNSID, "Remove")
  277. login()
  278. end
  279.  
  280. event, p1, p2, p3 = os.pullEvent()
  281.  
  282. term.clear()
  283. term.setCursorPos(1,1)
  284. print("Legoman technologies email server running")
  285. myid = os.computerID()
  286. term.setCursorPos(1,2)
  287. print("Computer ID = " .. myid)
  288.  
  289. if event == "terminate" then
  290. term.setCursorPos(1,10)
  291. print("Enter password: ")
  292. if read("*") == "unlock" then
  293. os.pullEvent = pullEvent
  294. shell.exit()
  295. end
  296. end
  297.  
  298. if event =="char" then
  299. if p1 == "m" then
  300. for i, v in ipairs(recipients) do
  301. print(v)
  302. end
  303. end
  304. if p1 == "a" then
  305. term.setCursorPos(1,10)
  306. print("Enter name: ")
  307. name = read()
  308. print("Confim add " .. name .. " Y/N?")
  309. if read() == "Y" then
  310. table.insert(users, name)
  311. print("Added")
  312. end
  313. end
  314. if p1 == "l" then
  315. rednet.send(DNSID, "Remove")
  316. login()
  317. end
  318. end
  319.  
  320. if event == "rednet_message" then
  321. senderId = p1
  322. message = p2
  323. senderName = DNSreverse(senderId)
  324. for i, v in ipairs(users) do
  325. if senderName == v then
  326. senderValid = true
  327. print("Sender is valid")
  328. break
  329. else
  330. senderValid = false
  331. end
  332. end
  333.  
  334. if string.match(message, 'Send') == "Send" and senderValid then
  335. print("Command: " .. string.match(message, 'Send'))
  336. recipient = string.match(message, '%a+', 5)
  337. mailMessage = string.match(message, '%p.+', 5)
  338. print("Recipient: " .. recipient)
  339. --print("Message: " .. mailMessage)
  340. os.sleep(1)
  341. for i, v in ipairs(users) do
  342. print(v)
  343. if recipient == v then
  344. recipientValid = true
  345. print("Recipient is valid")
  346. break
  347. else
  348. recipientValid = false
  349. end
  350. end
  351. if recipientValid then
  352. table.insert(messages, mailMessage)
  353. table.insert(recipients, recipient)
  354. rednet.send(senderId, "Ok")
  355. end
  356. end
  357.  
  358. if string.match(message, 'Check') == "Check" and senderValid then
  359. print("Command: " .. string.match(message, 'Check'))
  360. messageNumber = 0
  361. for i, v in ipairs(recipients) do
  362. if senderName == v then
  363. messageNumber = messageNumber + 1
  364. end
  365. end
  366. rednet.send(senderId, messageNumber .. "")
  367. end
  368.  
  369. if string.match(message, 'Read') == "Read" and senderValid then
  370. print("Command: " .. string.match(message, 'Read'))
  371. readNumber = string.match(message, '%d+', 5) + 0
  372. print("Read number: " .. readNumber)
  373. messageNumber = 0
  374. for i, v in ipairs(recipients) do
  375. if senderName == v then
  376. messageNumber = messageNumber + 1
  377. end
  378. if messageNumber == readNumber then
  379. print("Message found")
  380. rednet.send(senderId, messages[i].."")
  381. break
  382. end
  383. end
  384. end
  385. if string.match(message, 'Remove') == 'Remove' and senderValid then
  386. print("Command: " .. string.match(message, 'Remove'))
  387. removeNumber = string.match(message, '%d+', 7)
  388. print("Remove number: " .. removeNumber)
  389. messageNumber = 0
  390. for i, v in ipairs(recipients) do
  391. if senderName == v then
  392. messageNumber = messageNumber + 1
  393. end
  394. if messageNumber == removeNumber + 0 then
  395. table.remove(messages, i)
  396. table.remove(recipients, i)
  397. print("Removed")
  398. break
  399. end
  400. end
  401. end
  402. end
  403. end
  404. The email client interfaces to all 3 servers, and it will attempt to find the id of the mail and login servers from DNS, so be sure they are both registered in the DNS server. It supports roaming accounts, so you can log in from any computer in rednet range. It can:
  405. 1- Check the number of messages in your inbox
  406. 2- Read one of the messages (input the number of the message you want read, with the first in the inbox being number one, the second number two, etc)
  407. 3- Send a message to a particular user, which give feedback in the form of "Sent" or "error" (which usually means invalid recipient)
  408. 4- Remove a message from the inbox, using a number system like reading messages
  409. 5-Log out of your account
  410.  
  411. Code:
  412. pullEvent = os.pullEvent
  413. os.pullEvent = os.pullEventRaw
  414.  
  415. term.clear()
  416. term.setCursorPos(1,1)
  417. print("Legoman technologies email client running")
  418. myid = os.computerID()
  419. term.setCursorPos(1,2)
  420. print("Computer ID = " .. myid)
  421.  
  422. local firstCycle = true
  423. local modemSide = "right"
  424. local AUTHID = 0
  425. local DNSID = 1
  426. local loggedin = false
  427.  
  428. function DNSreverse (Id)
  429. rednet.open(modemSide)
  430. rednet.send(DNSID, "Id " .. Id)
  431. local temp, message = rednet.receive(1)
  432. return message
  433. end
  434.  
  435. function DNSforward(servername)
  436. rednet.open(modemSide)
  437. rednet.send(DNSID, "Name " .. servername)
  438. local temp, message = rednet.receive(3)
  439. return message
  440. end
  441.  
  442. while true do
  443. if firstCycle then
  444. rednet.open(modemSide)
  445. firstCycle = false
  446. end
  447.  
  448. AUTHID = DNSforward("authserver") + 0
  449. MAILID = DNSforward("mailserver") + 0
  450. print("Mail ID: " .. MAILID)
  451. print("Auth ID: " .. AUTHID)
  452. print("Welcome! Please log in.")
  453. print("Username: ")
  454. username = read()
  455. print("Password:")
  456. password = read("*")
  457.  
  458. rednet.send(AUTHID + 0, username)
  459. sender, message = rednet.receive(2)
  460.  
  461. if message == "Valid" then
  462. print("Username ok!")
  463. rednet.send(AUTHID + 0, password)
  464. elseif message == "Not Valid" then
  465. print("Wrong username!")
  466. loggedin = false
  467. else
  468. print("Rednet error!")
  469. end
  470.  
  471. sender, message = rednet.receive(2)
  472.  
  473. if message == "Authenticated" then
  474. loggedin = true
  475. print("Logged in!")
  476. else
  477. print("Login failed!")
  478. end
  479. os.sleep(2)
  480.  
  481. if loggedin then
  482. term.clear()
  483. term.setCursorPos(1,1)
  484. end
  485.  
  486. while loggedin do
  487.  
  488. print("Legoman technologies email client! Username: " .. DNSreverse(myid))
  489. print("What would you like to do?")
  490. print(" 1 - Check for messages")
  491. print(" 2 - Read a message")
  492. print(" 3 - Send a message")
  493. print(" 4 - Remove a message")
  494. print(" 5 - Log out")
  495. print(">")
  496. input = read()
  497.  
  498. if input == "1" then
  499. print("Checking for messages")
  500. rednet.send(MAILID, "Check")
  501. temp, message = rednet.receive(3)
  502. print("You have: " .. message .. " message(s)")
  503. end
  504.  
  505. if input == "2" then
  506. print("Enter message number to read: ")
  507. messageNum = read()
  508. rednet.send(MAILID, "Read " .. messageNum)
  509. temp, message = rednet.receive(3)
  510. print(message)
  511. end
  512.  
  513. if input == "3" then
  514. print("Enter recipient name: ")
  515. recipient = read()
  516. print("Enter message: ")
  517. toSend = read()
  518. print("Send message to " .. recipient .. " Y/N?")
  519. yn = read()
  520. if yn == "Y" then
  521. rednet.send(MAILID, "Send " .. recipient .. " !" .. toSend)
  522. temp, message = rednet.receive(3)
  523. if message == "Ok" then
  524. print("Sent!")
  525. else
  526. print("Error!")
  527. end
  528. end
  529. end
  530.  
  531. if input == "4" then
  532. print("Enter message number to remove: ")
  533. messageNum = read()
  534. os.sleep(1)
  535. print("Proceed? Y/N?")
  536. yn = read()
  537. if yn == "Y" then
  538. rednet.send(MAILID, "Remove " .. messageNum)
  539. end
  540. end
  541.  
  542. if input == "5" then
  543. term.clear()
  544. term.setCursorPos(1,1)
  545. print("Goodbye!")
  546. os.sleep(1)
  547. rednet.send(DNSID, "Remove")
  548. loggedin = false
  549. term.clear()
  550. term.setCursorPos(1,1)
  551. end
  552. end
  553. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement