turtle5204

Ringchat Server

Jun 19th, 2018
855
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.51 KB | None | 0 0
  1. --@version 1.2
  2.  
  3. local version = 1.2
  4. local ht_up = http.get("https://pastebin.com/raw/nQ7rSYbN")
  5. local up_A = ht_up.readLine()
  6. local up_B = ht_up.readAll()
  7. local up = up_A .. up_B
  8. ht_up.close()
  9. local their_version = tonumber(up_A:sub(12))
  10. if (their_version > version) then
  11.     local fi_up = fs.open(shell.getRunningProgram(), "w")
  12.     fi_up.write(up)
  13.     fi_up.close()
  14.     shell.run(shell.getRunningProgram())
  15.     return
  16. end
  17.  
  18. local name = "NONAME"
  19. local output = nil
  20. local password = nil
  21. if not fs.exists("/.rc_name") then
  22.     repeat
  23.         term.setBackgroundColor(colors.black)
  24.         term.setTextColor(colors.white)
  25.         term.clear()
  26.         term.setCursorPos(1, 1)
  27.         write("Server name (min 3 length): ")
  28.         name = tostring(read())
  29.         sleep(0.1)
  30.     until name ~= "nil" and #name > 2
  31.     local rc = fs.open("/.rc_name", "w")
  32.     rc.write(name)
  33.     rc.close()
  34. else
  35.     local rc = fs.open("/.rc_name", "r")
  36.     name = tostring(rc.readAll())
  37.     rc.close()
  38. end
  39.  
  40. if not fs.exists("/.rc_key") then
  41.     term.setBackgroundColor(colors.black)
  42.     term.setTextColor(colors.white)
  43.     term.clear()
  44.     term.setCursorPos(1, 1)
  45.     write("Password (blank for none): ")
  46.     password = tostring(read())
  47.     if password == "" or password == nil or password == "blank" then --yeah i know you smartasses will enter blank
  48.         local f = fs.open("/.rc_key", "w")
  49.         f.write("blank")
  50.         f.close()
  51.     else
  52.         local f = fs.open("/.rc_key", "w")
  53.         f.write(password)
  54.         f.close()
  55.     end
  56. else
  57.     local f = fs.open("/.rc_key", "r")
  58.     password = f.readAll()
  59.     f.close()
  60.     if password == "blank" then
  61.         password = nil
  62.     end
  63. end
  64.  
  65. local pingdata_channel = 895
  66. local connect_channel = 885
  67.  
  68. local our_connections = {}
  69. local chatLog = {}
  70. for i = 1, 18 do
  71.     table.insert(chatLog, "")
  72. end
  73. local d = false
  74. local mod = nil
  75. for k, v in pairs(peripheral.getNames()) do
  76.     if peripheral.getType(v) == "modem" then
  77.         rednet.open(v)
  78.         mod = peripheral.wrap(v)
  79.         d = true
  80.     end
  81. end
  82. if not d or not mod then
  83.     error("A modem is required!")
  84. end
  85.  
  86. local function download_file(url, target)
  87.     local ht = http.get(url)
  88.     local data = ht.readAll()
  89.     ht.close()
  90.     local file = fs.open(target, "w")
  91.     file.write(data)
  92.     file.close()
  93. end
  94.  
  95. local function read_file(target)
  96.     local file = fs.open(target, "r")
  97.     local data = file.readAll()
  98.     file.close()
  99.     return data
  100. end
  101.  
  102. -- API Loading
  103. local function getAPI(name, url)
  104.     local apiloc = "/" .. name
  105.     if (not fs.exists(apiloc)) then
  106.         local newapiloc = shell and shell.resolveProgram(name) or nil
  107.         if type(newapiloc) ~= "string" then
  108.             download_file(url, apiloc)
  109.         else
  110.             apiloc = newapiloc
  111.         end
  112.     end
  113.     return apiloc
  114. end
  115.  
  116. local function tFind(a, b)
  117.     for k, v in pairs(a) do
  118.         if b == v then
  119.             return k
  120.         end
  121.     end
  122.     return nil
  123. end
  124.  
  125. local function add_chat(mes)
  126.     table.remove(chatLog, 1)
  127.     table.insert(chatLog, mes)
  128. end
  129.  
  130. local names = {}
  131. local ringnet = require(getAPI("ringnet", "https://pastebin.com/raw/tUe94t9J"))
  132. getAPI("aeslua", "https://git.io/aeslua")
  133. os.loadAPI("aeslua")
  134. --ringnet.setDebug(true)
  135. ringnet.openChannel(pingdata_channel)
  136. ringnet.openChannel(connect_channel)
  137.  
  138. local challenge_string = nil
  139.  
  140. if password ~= nil and password ~= "" and password ~= "blank" then
  141.     challenge_string = aeslua.encrypt(password, "_A_COMMON_ENCRYPTION_STRING", aeslua.AES128, aeslua.CFBMODE)
  142. end
  143.  
  144. local function name_exists(n)
  145.     for k, v in pairs(names) do
  146.         if v == n then
  147.             return true
  148.         end
  149.     end
  150.     return fase
  151. end
  152.  
  153. local function sendData(a, b)
  154.     if password then
  155.         b = aeslua.encrypt(password, b, aeslua.AES128, aeslua.CFBMODE)
  156.     end
  157.     ringnet.sendData(a, b)
  158. end
  159.  
  160. parallel.waitForAll(
  161.     ringnet.connectionHandler,
  162.     function()
  163.         while true do
  164.             local _, side, sfrq, rfrq, mes, dist = os.pullEvent()
  165.             if (_ == "modem_message") then
  166.                 if (type(mes) == "string") then
  167.                     mes = textutils.unserialize(mes)
  168.                 end
  169.                 if (type(mes) == "table") then
  170.                     if (mes.type == "SERVER_DATA" and type(mes.rid) == "number") then
  171.                         mod.transmit(
  172.                             sfrq,
  173.                             sfrq,
  174.                             textutils.serialize(
  175.                                 {
  176.                                     type = "SERVER_RETURNDATA",
  177.                                     to = mes.rid,
  178.                                     name = name,
  179.                                     fingerprint = ringnet.get_fingerprint(),
  180.                                     hid = ringnet.get_handshakeID(),
  181.                                     locked = challenge_string
  182.                                 }
  183.                             )
  184.                         )
  185.                     end
  186.                 end
  187.             elseif (_ == "tunnel_finish") then
  188.                 table.insert(our_connections, side)
  189.             elseif (_ == "secure_receive") then
  190.                 local data = sfrq
  191.                 local cid = side
  192.                 if (type(data) == "string") then
  193.                     data = textutils.unserialize(data)
  194.                 end
  195.                 if (type(data) == "table") then
  196.                     if (data.type == "CHAT_LOGIN" and type(data.name) == "string") then
  197.                         local current_num = 1
  198.                         local actual_name = data.name
  199.                         if (name_exists(data.name)) then
  200.                             repeat
  201.                                 actual_name = tostring(data.name) .. tostring(current_num)
  202.                                 current_num = current_num + 1
  203.                             until (not name_exists(actual_name))
  204.                         end
  205.                         names[cid] = actual_name
  206.                         add_chat(
  207.                             {
  208.                                 mes = actual_name .. " has joined.",
  209.                                 raw = true
  210.                             }
  211.                         )
  212.                         sendData(
  213.                             cid,
  214.                             textutils.serialize(
  215.                                 {
  216.                                     type = "CHAT_LOG",
  217.                                     log = chatLog
  218.                                 }
  219.                             )
  220.                         )
  221.                     elseif (data.type == "CHAT_LOGOUT" and type(names[cid]) == "string") then
  222.                         add_chat(
  223.                             {
  224.                                 mes = tostring(names[cid]) .. " has left.",
  225.                                 raw = true
  226.                             }
  227.                         )
  228.                         for k, v in pairs(our_connections) do
  229.                             sendData(
  230.                                 v,
  231.                                 textutils.serialize(
  232.                                     {
  233.                                         type = "CHAT_LOG",
  234.                                         log = chatLog
  235.                                     }
  236.                                 )
  237.                             )
  238.                         end
  239.                         names[cid] = nil
  240.                         local ccid = tFind(our_connections, cid)
  241.                         if ccid then
  242.                             table.remove(our_connections, ccid)
  243.                         end
  244.                     elseif (data.type == "CHAT_CHAT" and type(names[cid]) == "string" and type(data.message) == "string") then
  245.                         local name = names[cid]
  246.                         add_chat(
  247.                             {
  248.                                 name = name,
  249.                                 mes = tostring(data.message)
  250.                             }
  251.                         )
  252.                         local prefix = (name .. ": ")
  253.                         local dje = (tostring(data.message) .. "\n")
  254.                         local combined = (prefix .. dje)
  255.                         term.blit(combined, (string.rep("0", #prefix) .. string.rep("8", #dje)), string.rep("f", #combined))
  256.                         --print("ALLCONNECTIONS!")
  257.                         --print(textutils.serialize(our_connections))
  258.                         for k, v in pairs(our_connections) do
  259.                             sendData(
  260.                                 v,
  261.                                 textutils.serialize(
  262.                                     {
  263.                                         type = "CHAT_LOG",
  264.                                         log = chatLog
  265.                                     }
  266.                                 )
  267.                             )
  268.                         end
  269.                     end
  270.                 end
  271.             end
  272.         end
  273.     end
  274. )
Advertisement
Add Comment
Please, Sign In to add comment