Advertisement
cyber_Ahn

netChat.0.5

Apr 3rd, 2015
708
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.67 KB | None | 0 0
  1. local user_use = "x"
  2. local userList = {}
  3. local msgList = {}
  4. local ix = false
  5. shell.run("delete caAPI")
  6. shell.run("pastebin get EDLdR1nF caAPI")
  7. os.loadAPI("caAPI")
  8. local monitor_number = caAPI.get_hardware("monitor")
  9. local found = fs.exists("config/monitor.cfg")
  10. if found == true then
  11. file = fs.open("config/monitor.cfg","r")
  12. local fileData = {}
  13. local line = file.readLine()
  14. repeat
  15. table.insert(fileData,line)
  16. line = file.readLine()
  17. until line == nil
  18. file.close()
  19. monitor_number = fileData[1]
  20. end
  21. local mon = peripheral.wrap(monitor_number)
  22. os.loadAPI("caAPI")
  23. local ModemSide = caAPI.detectDivice("modem")
  24. rednet.open(ModemSide)
  25. --draw screen
  26. function draw_screen(userw)
  27. mon.setBackgroundColor(colors.white)
  28. mon.clear()
  29. mon.setBackgroundColor(colors.white)
  30. mon.setCursorPos(1,1)
  31. mon.setBackgroundColor(colors.blue)
  32. mon.write("NetChat 1.0                                       ")
  33. if userw ~= "new" then
  34. mon.setCursorPos(20,1)
  35. mon.write("Login as "..userw)
  36. end
  37. mon.setCursorPos(16,26)
  38. mon.write("Chat:                              ")
  39. mon.setCursorPos(50,1)
  40. mon.setBackgroundColor(colors.red)
  41. mon.setTextColor(colors.black)
  42. mon.write("X")
  43. local startline = 2
  44. mon.setBackgroundColor(colors.gray)
  45. for x=1,25 do
  46. mon.setCursorPos(1,startline)
  47. mon.write("               ")
  48. startline = startline + 1
  49. end
  50. end
  51. --set user name--
  52. function user_set()
  53. mon.setCursorPos(20,15)
  54. mon.setBackgroundColor(colors.red)
  55. mon.write("UserName:                 ")
  56. mon.setCursorPos(29,15)
  57. local user_name = read()
  58. mon.setCursorPos(29,15)
  59. mon.write(user_name)
  60. sleep(3)
  61. send("us:"..user_name)
  62. draw_screen(user_name)
  63. user_use = user_name
  64. touch()
  65. end
  66. --inputs touch--
  67. function touch()
  68. event,side,x,y = os.pullEvent()
  69. if event == "monitor_touch" then
  70. if x == 50 and y == 1 then
  71. local sdaa = "logoff:"..user_use
  72. send(sdaa)
  73. sleep(0.5)
  74. ix = true
  75. end
  76. if x > 16 and x < 50 and y == 26 then
  77. mon.setCursorPos(21,26)
  78. mon.setBackgroundColor(colors.blue)
  79. mon.write("                              ")
  80. mon.setCursorPos(21,26)
  81. local chat = read()
  82. local send_chat = "msg:"..user_use..":"..chat
  83. write_text(user_use..":"..chat)
  84. send(send_chat)
  85. draw_screen(user_use)
  86. refresh_text()
  87. refresh_user()
  88. end
  89. end
  90. if ix == true then
  91. rednet.close(ModemSide)
  92. shell.run("clear")
  93. else
  94. red_in()
  95. end
  96. end
  97. --rednet input--
  98. function red_in()
  99. local id,msg = rednet.receive()
  100. local inpT = {}
  101. inpT = caAPI.split(msg,":")
  102. if inpT[1] == "us" then
  103. write_user(inpT[2])
  104. end
  105. if inpT[1] == "msg" then
  106. write_text(inpT[2]..":"..inpT[3])
  107. local found = caAPI.in_table(userList,inpT[2])
  108. if found == false then
  109. write_user(inpT[2])
  110. end
  111. end
  112. touch()
  113. end
  114. -- write text--
  115. function write_text(msgU)
  116. if #msgList > 24 then
  117. msgList = {}
  118. end
  119. table.insert(msgList,msgU)
  120. local startMline = 2
  121. for x=1,#msgList do
  122. mon.setCursorPos(16,startMline)
  123. mon.setBackgroundColor(colors.white)
  124. mon.write(msgList[x])
  125. startMline = startMline + 1
  126. end
  127. end
  128. --write user--
  129. function write_user(nameU)
  130. table.insert(userList,nameU)
  131. local startUline = 2
  132. for x=1,#userList do
  133. mon.setCursorPos(1,startUline)
  134. mon.setBackgroundColor(colors.gray)
  135. mon.write(userList[x])
  136. startUline = startUline + 1
  137. end
  138. end
  139. --refresh text--
  140. function refresh_text()
  141. local startMline = 2
  142. for x=1,#msgList do
  143. mon.setCursorPos(16,startMline)
  144. mon.setBackgroundColor(colors.white)
  145. mon.write(msgList[x])
  146. startMline = startMline + 1
  147. end
  148. end
  149. --refresh user--
  150. function refresh_user()
  151. local startUline = 2
  152. for x=1,#userList do
  153. mon.setCursorPos(1,startUline)
  154. mon.setBackgroundColor(colors.gray)
  155. mon.write(userList[x])
  156. startUline = startUline + 1
  157. end
  158. end
  159. -- send --
  160. function send(sda)
  161. rednet.broadcast(sda)
  162. end
  163. --program
  164. draw_screen("new")
  165. user_set()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement