Advertisement
Guest User

opencomputers chat client

a guest
Aug 10th, 2018
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.52 KB | None | 0 0
  1. -- thread.lua code is written by Zer0Galaxy
  2. -- Code: http://pastebin.com/E0SzJcCx
  3.  
  4. local thread = require("thread")
  5. local computer = require("computer")
  6. local component = require("component")
  7. local event = require("event")
  8. local serialization = require("serialization")
  9. local text = require("text")
  10. local term = require("term")
  11. local unicode = require("unicode")
  12. local gpu = component.gpu
  13.  
  14. local modem, serverAddress, primaryPort
  15. local name, myMessage
  16. local A, B
  17. local sHandler, rHandler
  18.  
  19. local function Registration()
  20. local nickname, password, repetition, _, _, address, _, _, message
  21. term.clear()
  22. term.write("User: ") nickname = text.trim(term.read())
  23. term.write("Password: ") password = text.trim(term.read(nil, true, nil, "*"))
  24. term.write("Repeat password: ") repetition = text.trim(term.read(nil, true, nil, "*"))
  25. if password ~= repetition then
  26. term.write("\nThe passwords do not match")
  27. os.sleep(0.5)
  28. else
  29. local user = {[1] = nickname, [2] = password}
  30. local packet = serialization.serialize(user)
  31. modem.open(255)
  32. modem.send(serverAddress, 255, packet)
  33. while address ~= serverAddress do
  34. _, _, address, _, _, message = event.pull("modem_message")
  35. end
  36. modem.close(255)
  37. if type(message) == "number" then
  38. term.clear()
  39. print("Registration complete")
  40. else print(message) end
  41. end
  42. event.pull("key_up")
  43. term.clear()
  44. end
  45.  
  46. local function Authentication()
  47. local nickname, password, _, _, address, _, _, message
  48. term.write("User: ") nickname = text.trim(term.read())
  49. term.write("Password: ") password = text.trim(term.read(nil, true, nil, "*"))
  50. local user = {[1] = nickname, [2] = password}
  51. local packet = serialization.serialize(user)
  52. modem.open(256)
  53. modem.send(serverAddress, 256, packet)
  54. while address ~= serverAddress do
  55. _, _, address, _, _, message = event.pull("modem_message")
  56. end
  57. modem.close(256)
  58. if type(message) == "number" then
  59. name = nickname
  60. return message
  61. else
  62. print(message)
  63. os.sleep(0.5)
  64. event.pull("key_up")
  65. term.clear()
  66. return 0
  67. end
  68. end
  69.  
  70. local function Choice()
  71. while true do
  72. print("1. Chat\n2. Registration\n3. Exit\n")
  73. local _, _, _, choice = event.pull("key_up")
  74. term.clear()
  75. if choice == 2 then
  76. local authResult = Authentication()
  77. if authResult ~= 0 then return authResult end
  78. end
  79. if choice == 3 then Registration() end
  80. if choice == 4 then break end
  81. end
  82. return 0
  83. end
  84.  
  85. local function Receiver()
  86. local x, y
  87. local _, _, address, port, _, message, mesHeight
  88. local chatWidth = math.floor(A * 0.75)
  89. local onlineLeftBorder = math.floor(A * 0.8) - 1
  90. local onlineWidth = math.floor(A * 0.2) - 1
  91. local onlineCenter = math.floor(A * 0.9) - 1
  92. local online = {}
  93. while true do
  94. _, _, address, port, _, message = event.pull("modem_message")
  95. if address == serverAddress then
  96. address = nil
  97. if port == 253 then
  98. if message == 'P' then modem.send(serverAddress, 253, name)
  99. else online = serialization.unserialize(message) end
  100. else
  101. if message == 'R' or message == 'C' then
  102. term.setCursor(1, B - 3)
  103. if message == 'R' then print(" [Server] Restarting...")
  104. else print(" [Server] Shutting down...") end
  105. thread.kill(sHandler)
  106. term.setCursorBlink(false)
  107. os.sleep(3)
  108. term.clear()
  109. break
  110. else
  111. x, y = term.getCursor()
  112. local unsent = ''
  113. for i=2, x, 1 do unsent = unsent .. gpu.get(i, y) end
  114. gpu.fill(1, B - 2, A, 3, " ") -- clear input textbox
  115. -- move chat up
  116. mesHeight = math.floor(unicode.len(message) / chatWidth) + 1
  117. for i=1, mesHeight, 1 do
  118. term.setCursor(A, B)
  119. term.write(' ', true)
  120. end
  121. term.setCursor(1, B - 3 - mesHeight)
  122. -- print message
  123. message = text.trim(message)
  124. while true do
  125. if unicode.len(message) < chatWidth then print(' ' .. message) break end
  126. print(' ' .. unicode.sub(message, 1, chatWidth))
  127. message = unicode.sub(message, chatWidth + 1)
  128. end
  129. -- online list
  130. gpu.setForeground(0xffffff)
  131. gpu.setBackground(0x008000)
  132. gpu.fill(onlineLeftBorder, 1, onlineWidth, 3, " ")
  133. term.setCursor(onlineCenter - 3, 2) term.write("ONLINE")
  134. gpu.setForeground(0x000000)
  135. gpu.setBackground(0xffffff)
  136. gpu.fill(onlineLeftBorder, 4, onlineWidth, B - 7, " ")
  137. for i=1,#online,1 do
  138. term.setCursor(onlineLeftBorder, i+4)
  139. term.write(' ' .. online[i])
  140. end
  141. gpu.setForeground(0xffffff)
  142. gpu.setBackground(0x000000)
  143. -- input field
  144. gpu.fill(1, B, A, 1, "—")
  145. gpu.fill(1, B - 2, A, 1, "—")
  146. -- setting cursor to start position
  147. term.setCursor(2, B - 1) print(unsent)
  148. term.setCursor(x, B - 1)
  149. if message ~= myMessage then computer.beep(1000, 0.1) end
  150. end
  151. end
  152. end
  153. end
  154. end
  155.  
  156. local function Sender()
  157. local result
  158. local history = {}
  159. while true do
  160. term.setCursor(2, B - 1)
  161. myMessage = term.read(history, false)
  162. result = text.trim(myMessage)
  163. result = text.detab(result, 1)
  164. if result == "exit" then
  165. term.clear()
  166. thread.kill(rHandler)
  167. break
  168. end
  169. if unicode.len(result) > 0 and unicode.len(result) < 256 then
  170. myMessage = name .. ": " .. myMessage
  171. modem.send(serverAddress, primaryPort, myMessage)
  172. end
  173. term.clearLine()
  174. if #history > 5 then table.remove(history, 1) end
  175. end
  176. end
  177.  
  178. local function CheckModem()
  179. if component.isAvailable("modem") == false then return 0 end
  180. modem = component.modem
  181. modem.setStrength(5000)
  182. return 1
  183. end
  184.  
  185. local function CheckConnection()
  186. if CheckModem() == 0 then
  187. print("Wireless card not found") return 0 end
  188. local serverOn = false
  189. term.clear()
  190. term.write("Connecting")
  191. modem.open(254)
  192. for try = 1, 3 do
  193. modem.broadcast(254, 1)
  194. local _, _, address, _, _, _ = event.pull(3, "modem_message")
  195. if address ~= nil then
  196. serverAddress = address
  197. serverOn = true
  198. break
  199. end
  200. term.write(".")
  201. end
  202. modem.close(254)
  203. term.clear()
  204. if serverOn == true then return 1 end
  205. print("The server is not available")
  206. return 0
  207. end
  208.  
  209. if CheckConnection() == 1 then
  210. local choice = Choice()
  211. if choice > 0 then
  212. primaryPort = choice
  213. modem.open(primaryPort)
  214. modem.open(253)
  215. A, B = gpu.getResolution()
  216. term.clear()
  217. term.setCursor(1, B - 1)
  218. thread.init()
  219. rHandler = thread.create(Receiver)
  220. sHandler = thread.create(Sender)
  221. thread.waitForAll()
  222. modem.close()
  223. elseif choice == -1 then
  224. print("You're banned on this server") end
  225. else
  226. os.sleep(0.5)
  227. event.pull("key_up")
  228. term.clear()
  229. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement