Guest User

Untitled

a guest
Dec 11th, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.70 KB | None | 0 0
  1. -- Config
  2. local nMinNameLen = 3 -- minimum user name lenght
  3. local nMaxNameLen = 8 -- maximum user name lenght
  4. local nMaxHistory = 50 -- maximum chat history size, set to nil to disable
  5.  
  6. -- System vars
  7. local nScreenW, nScreenH = term.getSize()
  8. local tUsers = {}
  9. -- User message vars
  10. local sUsrName = ""
  11. local sUsrMsg = ""
  12. local nPos = 0
  13. -- Received messages vars
  14. local tMessages = {}
  15. local nMsg = 1
  16. -- Menu vars
  17. local bMenu = false
  18. local nSelected = 1
  19. local tMenuFunctions = {}
  20. local tMenuOptions = {}
  21. -- Menu functions vars
  22. -- Add any variable you need here
  23. local bExit = false
  24.  
  25. local function Clear()
  26.         term.clear()
  27.         term.setCursorPos(1, 1)
  28. end
  29.  
  30. local function OpenAll()
  31.         for _,side in ipairs(rs.getSides()) do
  32.                 rednet.open(side)
  33.         end
  34. end
  35.  
  36. local function AddMessage(sMsg, sName)
  37.         if #sMsg > 0 then
  38.                 local msg
  39.                 if sName then
  40.                         msg = sName..": "..sMsg
  41.                 else
  42.                         msg = "<"..sMsg..">"
  43.                 end
  44.                 table.insert(tMessages, msg)
  45.                 if #tMessages - nMsg >= nScreenH - 1 then
  46.                         nMsg = nMsg + 1
  47.                 end
  48.                 if nMaxHistory ~= nil and #tMessages > nMaxHistory then
  49.                         table.remove(tMessages, 1)
  50.                         if nMsg > 1 then
  51.                                 nMsg = nMsg - 1
  52.                         end
  53.                 end
  54.         end
  55. end
  56.  
  57. local function AddUser(nID, sName)
  58.         tUsers[nID] = sName
  59.         AddMessage("User "..sName.." connected from "..tostring(nID))
  60. end
  61.  
  62. local function RemoveUser(nID)
  63.         AddMessage("User "..tUsers[nID].." disconnected")
  64.         tUsers[nID] = nil
  65. end
  66.  
  67. local function WriteMsg(sText)
  68.         local x, y = term.getCursorPos()
  69.         local function newLine()
  70.                 x = 1
  71.                 y = y + 1
  72.                 term.setCursorPos(x, y)
  73.                 if y < nScreenH - 2 then
  74.                         return true
  75.                 end
  76.                 return false
  77.         end
  78.         while #sText > 0 do
  79.                 local whitespace = string.match(sText, "^[ \t]+")
  80.                 if whitespace then
  81.                         term.write(whitespace)
  82.                         x, y = term.getCursorPos()
  83.                         sText = string.sub(sText, #whitespace + 1)
  84.                 end
  85.                 local newline = string.match(sText, "^\n")
  86.                 if newline then
  87.                         if not newLine() then
  88.                                 return true
  89.                         end
  90.                         sText = string.sub(sText, 2)
  91.                 end
  92.                 local text = string.match(sText, "^[^ \t\n]+")
  93.                 if text then
  94.                         sText = string.sub(sText, #text + 1)
  95.                         if #text > nScreenW then
  96.                                 while #text > 0 do
  97.                                         if x > nScreenW then
  98.                                                 if not newLine() then
  99.                                                         return true
  100.                                                 end
  101.                                         end
  102.                                         term.write(text)
  103.                                         text = string.sub(text, (nScreenW - x) + 2)
  104.                                         x, y = term.getCursorPos()
  105.                                 end
  106.                         else
  107.                                 if x + #text > nScreenW then
  108.                                         if not newLine() then
  109.                                                 return true
  110.                                         end
  111.                                 end
  112.                                 term.write(text)
  113.                                 x, y = term.getCursorPos()
  114.                         end
  115.                 end
  116.         end
  117.         return false
  118. end
  119.  
  120. local function WriteMessages()
  121.         local i = 0
  122.         while nMsg + i <= #tMessages and i < nScreenH - 1 do
  123.                 if WriteMsg(tMessages[nMsg + i]) then
  124.                         break
  125.                 end
  126.                 local x, y = term.getCursorPos()
  127.                 term.setCursorPos(1, y + 1)
  128.                 i = i + 1
  129.         end
  130. end
  131.  
  132. local function RedrawUserMsg()
  133.         local nScroll = 0
  134.         if nPos + 3 >= nScreenW then
  135.                 nScroll = nPos + 3 - nScreenW
  136.         end
  137.         term.setCursorPos(1, nScreenH)
  138.         write("> ")
  139.         write(string.sub(sUsrMsg, nScroll + 1))
  140.         term.setCursorPos(nPos + 3 - nScroll, nScreenH)
  141. end
  142.  
  143. local function RedrawMenu()
  144.         term.setCursorPos(1, nScreenH)
  145.         for i, s in ipairs(tMenuOptions) do
  146.                 if i == nSelected then
  147.                         term.write("["..s.."]")
  148.                 else
  149.                         term.write(s)
  150.                 end
  151.                 term.write(" ")
  152.         end
  153. end
  154.  
  155. local function Redraw()
  156.         Clear()
  157.         WriteMessages()
  158.         if bMenu then
  159.                 RedrawMenu()
  160.         else
  161.                 RedrawUserMsg()
  162.         end
  163. end
  164.  
  165. local function ParseMsg(nID, sMsg)
  166.         local sAction, sArgs = string.match(sMsg, "(%a+): (.+)")
  167.         if sAction == "MSG" then
  168.                 AddMessage(sArgs, tUsers[nID])
  169.         elseif sAction == "PONG" then
  170.                 AddUser(nID, sArgs)
  171.         elseif sAction == "PING" then
  172.                 AddUser(nID, sArgs)
  173.                 rednet.send(nID, "PONG: "..sUsrName)
  174.         elseif sAction == "QUIT" then
  175.                 RemoveUser(nID)
  176.         end
  177. end
  178.  
  179. local function Ping()
  180.         rednet.broadcast("PING: "..sUsrName)
  181. end
  182.  
  183. local function Disconnect()
  184.         for id,_ in pairs(tUsers) do
  185.                 rednet.send(id, "QUIT: "..sUsrName)
  186.         end
  187. end
  188.  
  189. local function SendMsg()
  190.         AddMessage(sUsrMsg, sUsrName)
  191.         for id,_ in pairs(tUsers) do
  192.                 rednet.send(id, "MSG: "..sUsrMsg)
  193.         end
  194.         sUsrMsg = ""
  195.         nPos = 0
  196. end
  197.  
  198. local function DoMenuItem()
  199.         tMenuFunctions[nSelected]()
  200.         nSelected = 1
  201. end
  202.  
  203. function KeyPress(key)
  204.         if key == 28 then -- Enter
  205.                 if bMenu then
  206.                         DoMenuItem()
  207.                 else
  208.                         SendMsg()
  209.                 end
  210.         elseif key == 203 then -- Left
  211.                 if bMenu then
  212.                         if nSelected > 1 then
  213.                                 nSelected = nSelected - 1
  214.                         end
  215.                 else
  216.                         if nPos > 0 then
  217.                                 nPos = nPos - 1
  218.                         end
  219.                 end
  220.         elseif key == 205 then -- Right
  221.                 if bMenu then
  222.                         if nSelected < #tMenuOptions then
  223.                                 nSelected = nSelected + 1
  224.                         end
  225.                 else
  226.                         if nPos < #sUsrMsg then
  227.                                 nPos = nPos + 1
  228.                         end
  229.                 end
  230.         elseif key == 14 then -- Backspace
  231.                 if not bMenu then
  232.                         if nPos > 0 then
  233.                                 sUsrMsg = string.sub(sUsrMsg, 1, nPos - 1)..string.sub(sUsrMsg, nPos + 1)
  234.                                 nPos = nPos - 1
  235.                         end
  236.                 end
  237.         elseif key == 211 then -- Delete
  238.                 if not bMenu then
  239.                         sUsrMsg = string.sub(sUsrMsg, 1, nPos)..string.sub(sUsrMsg, nPos + 2)
  240.                 end
  241.         elseif key == 199 then -- Start
  242.                 if bMenu then
  243.                         nSelected = 1
  244.                 else
  245.                         nPos = 0
  246.                 end
  247.         elseif key == 207 then -- End
  248.                 if bMenu then
  249.                         nSelected = #tMenuOptions
  250.                 else
  251.                         nPos = #sUsrMsg
  252.                 end
  253.         elseif key == 200 then -- Up
  254.                 if nMsg > 1 then
  255.                         nMsg = nMsg - 1
  256.                 end
  257.         elseif key == 208 then -- Down
  258.                 if nMsg < #tMessages then
  259.                         nMsg = nMsg + 1
  260.                 end
  261.         elseif key == 29 then -- Ctrl
  262.                 bMenu = not bMenu
  263.                 term.setCursorBlink(not bMenu)
  264.         end
  265. end
  266.  
  267. local function AddChar(sChar)
  268.         sUsrMsg = string.sub(sUsrMsg, 1, nPos)..sChar..string.sub(sUsrMsg, nPos + 1)
  269.         nPos = nPos + 1
  270. end
  271.  
  272. local function CheckUserName()
  273.         if #sUsrName < nMinNameLen then
  274.                 return false, "Name too short."
  275.         elseif #sUsrName > nMaxNameLen then
  276.                 return false, "Name too long."
  277.         end
  278.         return true
  279. end
  280.  
  281. -- Menu
  282.  
  283. -- Add Menu functions here --
  284. --[[
  285. tMenuOptions[n] = "your option name"
  286. tMenuFunctions[n] = function()
  287.         -- Your code here
  288. end
  289. --]]
  290.  
  291. table.insert(tMenuOptions, "Exit")
  292. table.insert(tMenuFunctions, function()
  293.         bExit = true
  294. end )
  295.  
  296. -- Main loop
  297.  
  298. repeat
  299.         clearArea()
  300.         print("- CC IRC -")
  301.         write("Enter your nick: ")
  302.         sUsrName = read()
  303.         local bValid, err = CheckUserName()
  304.         if not bValid then
  305.                 print(err)
  306.                 sleep(2)
  307.         end
  308. until bValid
  309. OpenAll()
  310. Ping()
  311. term.setCursorBlink(true)
  312.  
  313. while not bExit do
  314.         Redraw()
  315.         local evt, arg1, arg2 = os.pullEvent()
  316.         if evt == "key" then
  317.                 KeyPress(arg1)
  318.         elseif evt == "char" then
  319.                 AddChar(arg1)
  320.         elseif evt == "rednet_message" then
  321.                 ParseMsg(arg1, arg2)
  322.         end
  323. end
  324. Disconnect()
  325. Clear()
Add Comment
Please, Sign In to add comment