Advertisement
Alakazard12

IRC

Dec 14th, 2012
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.52 KB | None | 0 0
  1. local sides = {"top", "bottom", "left", "right", "front", "back"}
  2. local cons = {}
  3. local serv = 0
  4. local hosting = false
  5. local tArgs = {...}
  6. local w, h = term.getSize()
  7. local input = ""
  8. local nick = ""
  9. local pinging = false
  10. pinged = {}
  11.  
  12. local config = textutils.unserialize(tArgs[1])
  13.  
  14. local function cprint(txt)
  15.     term.setTextColor(config.colors.main)
  16.     print(txt)
  17.     term.setTextColor(config.colors.second)
  18. end
  19.  
  20. local function cwrite(txt)
  21.     term.setTextColor(config.colors.main)
  22.     write(txt)
  23.     term.setTextColor(config.colors.second)
  24. end
  25.  
  26. local function loadInput()
  27.     local cinp = input
  28.     local limit = w-4
  29.     if #cinp > limit then
  30.         cinp = string.sub(cinp, #cinp-limit, #cinp)
  31.     end
  32.     term.setCursorPos(1, h)
  33.     term.clearLine()
  34.     cwrite("> ")
  35.     write(cinp)
  36. end
  37.  
  38. local function ports()
  39.     for i,v in pairs(sides) do
  40.         rednet.open(v)
  41.     end
  42. end
  43.  
  44. local function dot(name, txt)
  45.     term.clearLine()
  46.     term.scroll(1)
  47.     term.setCursorPos(1, h-2)
  48.     cwrite("["..name.."] ")
  49.     write(txt)
  50.     local x, y = term.getCursorPos()
  51.     if y > h-2 then
  52.         term.scroll(y-(h-2))
  53.     end
  54.     loadInput()
  55. end
  56.  
  57. local function senda(name, txt)
  58.     for i,v in pairs(cons) do
  59.         rednet.send(i, textutils.serialize({name, txt}))
  60.     end
  61.     if txt ~= "PINGONONONPINGONONPINGONONPING2424523523" then
  62.         dot(name, txt)
  63.     end
  64. end
  65.  
  66. local function sendp(vic, txt)
  67.     if vic == os.getComputerID() then
  68.         dot("Server", txt)
  69.     else
  70.         rednet.send(vic, textutils.serialize({"Server", txt}))
  71.     end
  72. end
  73.  
  74. local cmds = {
  75.     ["nick"] = function(id, msg)
  76.         cons[id] = msg
  77.         sendp(id, "Changed nickname to '"..msg.."'")
  78.     end;
  79. }
  80.  
  81. local function checkc(id)
  82.     local isf = nil
  83.     for i,v in pairs(cons) do
  84.         if v[1] == id then
  85.             isf = v[2]
  86.         end
  87.     end
  88.     return isf
  89. end
  90.  
  91. local function doh(p1, p2)
  92.     if string.sub(p2, 1, 4) == "IRC:" then
  93.         p2 = string.sub(p2, 5)
  94.         if not checkc(p1) then
  95.             if string.sub(p2, 1, 5) == "JOIN:" then
  96.                 table.insert(cons, {p1, string.sub(p2, 6)})
  97.                 senda("Server", string.sub(p2, 6).." has joined.")
  98.             end
  99.         elseif string.sub(p2, 1, 1) == "/" then
  100.             local dead = #p2+1
  101.             for i = 1, #p2 do
  102.                 if string.sub(p2, i, i) == " " then
  103.                     dead = i
  104.                 end
  105.             end
  106.             for i,v in pairs(cmds) do
  107.                 if string.sub(p2, 2, dead-1) == i then
  108.                     v(p1, string.sub(p2, dead+1))
  109.                 end
  110.             end
  111.         elseif p2 == "PINGRESPONSE11234234BIDSFBsofnskifn(S*9e-r94thtrebgd" then
  112.             table.insert(pinged, p1)
  113.         else
  114.             senda(checkc(p1), p2)
  115.         end
  116.     end
  117. end
  118.  
  119. local function sendt(txt)
  120.     if hosting == true then
  121.         doh(os.getComputerID(), "IRC:"..txt)
  122.     end
  123.     rednet.send(serv, "IRC:"..txt)
  124. end
  125.  
  126. local function pinga()
  127.     senda("Server", "PINGONONONPINGONONPINGONONPING2424523523")
  128.     pinging = true
  129.     pinged = {os.getComputerID()}
  130.     os.startTimer(1)
  131. end
  132.  
  133. local rtbl = function(tbl, ind)
  134.     for i = ind+1, #tbl do
  135.         tbl[i-1] = tbl[i]
  136.     end
  137.     tbl[#tbl] = nil
  138. end
  139.  
  140. local function getPings()
  141.     pinging = false
  142.     local i = 0
  143.     for dsfsf = 1, #cons do
  144.         i = i + 1
  145.         local v = cons[i]
  146.         local isp = false
  147.         local lot = 0
  148.         for tdgdfhdfh = 1, #pinged do
  149.             lot = lot + 1
  150.             if v[1] == pinged[lot] then
  151.                 isp = true
  152.                 rtbl(pinged, lot)
  153.                 lot = lot - 1
  154.             end
  155.         end
  156.         if isp == false then
  157.             senda("Server", v[2].." has left the server")
  158.             rtbl(cons, i)
  159.             i = i - 1
  160.         end
  161.     end
  162.     pinged = {}
  163. end
  164.  
  165. term.setBackgroundColor(config.colors.bg)
  166. term.clear()
  167. term.setCursorPos(1, 1)
  168. ports()
  169.  
  170. cwrite("Server: ")
  171. serv = tonumber(read())
  172. if not serv then
  173.     cprint("Invalid")
  174.     return
  175. end
  176. if serv == os.getComputerID() then
  177.     hosting = true
  178.     cprint("Hosting")
  179. end
  180.  
  181. cwrite("Nickame: ")
  182. nick = read()
  183. if hosting == true then
  184.     table.insert(cons, {os.getComputerID(), nick})
  185. else
  186.     sendt("JOIN:"..nick)
  187. end
  188.  
  189. term.clear()
  190. term.setCursorPos(1, 1)
  191.  
  192. loadInput()
  193.  
  194. if hosting == true then
  195.     pinga()
  196. end
  197.  
  198. while true do
  199.     term.setCursorBlink(true)
  200.     local e, p1, p2 = os.pullEvent()
  201.     if e == "char" then
  202.         input = input..p1
  203.         loadInput()
  204.     elseif e == "key" then
  205.         if p1 == keys.backspace then
  206.             input = string.sub(input, 1, #input-1)
  207.             loadInput()
  208.         elseif p1 == keys.enter then
  209.             sendt(input)
  210.             input = ""
  211.             loadInput()
  212.         end
  213.     elseif e == "rednet_message" then
  214.         if p1 == serv then
  215.             local tp = textutils.unserialize(p2)
  216.             if tp[2] == "PINGONONONPINGONONPINGONONPING2424523523" then
  217.                 sendt("PINGRESPONSE11234234BIDSFBsofnskifn(S*9e-r94thtrebgd")
  218.             else
  219.                 dot(tp[1], tp[2])
  220.             end
  221.         end
  222.         if hosting == true then
  223.             doh(p1, p2)
  224.         end
  225.     elseif e == "timer" then
  226.         -- getPings()
  227.         -- pinga()
  228.     end
  229. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement