Advertisement
LDDestroier

PSChat - chatbox mod!

Sep 12th, 2015
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- pastebin get cKmEV1HC pschat
  2. chatbox = peripheral.find("chatbox_admin")
  3. if not chatbox then
  4.     chatbox = peripheral.find("chatbox")
  5.     if not chatbox then
  6.         chatbox = peripheral.find("chat_box")
  7.         if not chatbox then
  8.             chatbox = peripheral.find("chatBox")
  9.             if not chatbox then
  10.                 error("Chatbox required, go get one.")
  11.             end
  12.         end
  13.     end
  14. end
  15. name = "chatboxuser"
  16. term.clear()
  17. --[[Changelog: 1.4: Added Notification sound.
  18. 1.5: Added Join/Leave message.
  19. 3.0 now on App Engine!
  20. ]]--
  21. --Credit Jesusthekiller for color formatting
  22.  
  23. local split = function(str, pat) local t = {} local fpat = "(.-)"..pat local last_end = 1 local s, e, cap = str:find(fpat, 1) if not s then return {str} end while s do if s ~= 1 or cap ~= "" then table.insert(t,cap) end last_end = e+1 s, e, cap = str:find(fpat, last_end) end if last_end <= #str then cap = str:sub(last_end) table.insert(t, cap) end return t end
  24.  
  25. if not inputMessage then
  26.     inputMessage = "nil"
  27.     inputName = "nil"
  28.     inputSide = "nil"
  29. end
  30.  
  31. justStarted = true
  32. isRestart=false
  33. local channel = "main_chatbox"
  34.  
  35. local trimColor=function(str)
  36.   str=string.gsub(str,"%#.","")
  37.   str=string.gsub(str,"%$.","")
  38.   return str
  39. end
  40.  
  41. local history={}
  42.  
  43. local update,colorRead,play,clear,receive,send,processCommand,post,updateDisplay
  44. local cprint,cwrite,toCode,cCode,clear,sb,st,sc
  45. function sc(x, y)
  46.     term.setCursorPos(x, y)
  47. end
  48.  
  49. function clear(move)
  50.     sb(colors.black)
  51.     term.clear()
  52.     if move ~= false then sc(1,1) end
  53. end
  54.  
  55. function sb(color)
  56.     term.setBackgroundColor(color)
  57. end
  58.  
  59. function st(color)
  60.     term.setTextColor(color)
  61. end
  62.  
  63. function cCode(h)
  64.         if term.isColor() and term.isColor then
  65.                 return 2 ^ (tonumber(h, 16) or 0)
  66.         else
  67.                 if h == "f" then
  68.                         return colors.black
  69.                 else
  70.                         return colors.white
  71.                 end
  72.         end
  73. end
  74.  
  75. function toCode(n)
  76.         return string.format('%x', n)
  77. end
  78.  
  79. function cwrite(text)
  80.         text = tostring(text)
  81.        
  82.         local i = 0
  83.     while true  do
  84.                 i = i + 1
  85.                 if i > #text then break end
  86.                
  87.         local c = text:sub(i, i)
  88.  
  89.                 if c == "\\" then
  90.             if text:sub(i+1, i+1) == "&" then
  91.                 write("&")
  92.                 i = i + 1
  93.             elseif text:sub(i+1, i+1) == "$" then
  94.                 write("$")
  95.                 i = i + 1
  96.                         else
  97.                                 write(c)
  98.             end
  99.         elseif c == "#" then
  100.             st(cCode(text:sub(i+1, i+1)))
  101.             i = i + 1
  102.         elseif c == "$" then
  103.             sb(cCode(text:sub(i+1, i+1)))
  104.             i = i + 1
  105.         else
  106.             write(c)
  107.         end
  108.     end
  109.        
  110.         return
  111. end
  112.  
  113. function cprint(text)
  114.         return cwrite(tostring(text).."\n")
  115. end
  116.  
  117. function colorRead( _sReplaceChar, _tHistory )
  118.   term.setCursorBlink( true )
  119.  
  120.     local sLine = ""
  121.   local nHistoryPos = nil
  122.   local nPos = 0
  123.     if _sReplaceChar then
  124.     _sReplaceChar = string.sub( _sReplaceChar, 1, 1 )
  125.   end
  126.  
  127.   local w, h = term.getSize()
  128.   local sx, sy = term.getCursorPos()  
  129.  
  130.   local function redraw( _sCustomReplaceChar )
  131.     local nScroll = 0
  132.     if sx + nPos >= w then
  133.       nScroll = (sx + nPos) - w
  134.     end
  135.      
  136.     term.setCursorPos( sx, sy )
  137.     local sReplace = _sCustomReplaceChar or _sReplaceChar
  138.     if sReplace then
  139.       cwrite( string.rep(sReplace, string.len(sLine) - nScroll) )
  140.     else
  141.       cwrite( string.sub( sLine, nScroll + 1 ) )
  142.     end
  143.     term.setCursorPos( sx + nPos - (#sLine-#trimColor(sLine)) - nScroll, sy )
  144.   end
  145.  
  146.   while true do
  147.     local sEvent, param = os.pullEvent()
  148.     if sEvent == "char" then
  149.       sLine = string.sub( sLine, 1, nPos ) .. param .. string.sub( sLine, nPos + 1 )
  150.       nPos = nPos + 1
  151.       redraw()
  152.      
  153.     elseif sEvent == "key" then
  154.         if param == keys.enter then
  155.         -- Enter
  156.         break
  157.        
  158.       elseif param == keys.left then
  159.         -- Left
  160.         if nPos > #sLine-#trimColor(sLine) then
  161.           nPos = nPos - 1
  162.           redraw()
  163.         end
  164.        
  165.       elseif param == keys.right then
  166.         -- Right        
  167.         if nPos < #sLine then
  168.           nPos = nPos + 1
  169.           redraw()
  170.         end
  171.      
  172.       elseif param == keys.up or param == keys.down then
  173.                 -- Up or down
  174.         if _tHistory then
  175.           redraw(" ");
  176.           if param == keys.up then
  177.             -- Up
  178.             if nHistoryPos == nil then
  179.               if #_tHistory > 0 then
  180.                 nHistoryPos = #_tHistory
  181.               end
  182.             elseif nHistoryPos > 1 then
  183.               nHistoryPos = nHistoryPos - 1
  184.             end
  185.           else
  186.             -- Down
  187.             if nHistoryPos == #_tHistory then
  188.               nHistoryPos = nil
  189.             elseif nHistoryPos ~= nil then
  190.               nHistoryPos = nHistoryPos + 1
  191.             end          
  192.           end
  193.          
  194.           if nHistoryPos then
  195.                       sLine = _tHistory[nHistoryPos]
  196.                       nPos = string.len( sLine )
  197.                     else
  198.             sLine = ""
  199.             nPos = 0
  200.           end
  201.           redraw()
  202.                 end
  203.       elseif param == keys.backspace then
  204.         -- Backspace
  205.         if nPos > 0 then
  206.           redraw(" ");
  207.           sLine = string.sub( sLine, 1, nPos - 1 ) .. string.sub( sLine, nPos + 1 )
  208.           nPos = nPos - 1        
  209.           redraw()
  210.         end
  211.       elseif param == keys.home then
  212.         -- Home
  213.         nPos = 0
  214.         redraw()    
  215.       elseif param == keys.delete then
  216.         if nPos < string.len(sLine) then
  217.           redraw(" ");
  218.           sLine = string.sub( sLine, 1, nPos ) .. string.sub( sLine, nPos + 2 )      
  219.           redraw()
  220.         end
  221.       elseif param == keys["end"] then
  222.         -- End
  223.         nPos = string.len(sLine)
  224.         redraw()
  225.       end
  226.     end
  227.   end
  228.  
  229.   term.setCursorBlink( false )
  230.   term.setCursorPos( w + 1, sy )
  231.   print()
  232.  
  233.   return sLine
  234. end
  235.  
  236. local postLink,getLink
  237. postLink="http://http-chat-serv.appspot.com/post"
  238. getLink="http://http-chat-serv.appspot.com/get"
  239. updateLink = "https://raw.github.com/MudkipTheEpic/chatHTTP/master/Client"
  240. currentProgram = shell.getRunningProgram()
  241. --name = ...
  242. screenX, screenY = term.getSize()
  243. screenY = screenY-2
  244. version = "3.0"
  245. data = {}
  246. if not fs.exists("/.chatData") then
  247.     fs.makeDir("/.chatData")
  248. end
  249. if fs.exists("/.chatData/data") then
  250.     file = fs.open("/.chatData/data", "r")
  251.     data.file = file.readAll()
  252.     password = data.file
  253.     file.close()
  254. else
  255.     password = "guest"
  256. end
  257. local function centerWrite(text, ny)
  258.     if type(text) == "table" then for _, v in pairs(text) do centerPrint(v) end
  259.     else
  260.         local x, y = term.getCursorPos()
  261.         local w, h = term.getSize()
  262.         term.setCursorPos(w/2 - text:len()/2 + (#text % 2 == 0 and 1 or 0), ny or y)
  263.         cwrite(text)
  264.     end
  265. end
  266.  
  267. function updateDisplay(txt)
  268.     term.setCursorPos(1, screenY)    
  269.     term.clearLine()
  270.     centerWrite(txt)
  271. end
  272.  
  273. local post=function(txt,txt2) txt=txt2 or txt return http.get(postLink,{usr = trimColor(name), data = txt, chan = channel}) end
  274. local cmdpost=function(txt,txt2) txt=txt2 or txt return http.get(postLink,{usr = trimColor(name), cmd=txt, pass=password}) end
  275.  
  276. local commandTable={
  277.     update=function() return update() end;
  278.     version=function()      
  279.         return updateDisplay("Version: "..version)
  280.     end;
  281.     exit=function(...) local l if #{...}>0 then l=" ["..table.concat({...}," ").."]" else l="" end post(name.."#0$f left the room."..l) return error(nil,3) end;
  282.     me=function(...)
  283.         local str=table.concat({...}," ")
  284.         return post("*"..name.."#0$f "..str)
  285.     end;
  286.     nick=function(newNick)
  287.         if not newNick or newNick=="" then updateDisplay("Please supply a nickname.") return nil end
  288.         post(name.."#0$f has changed nick to "..newNick)
  289.         name=newNick
  290.         return newNick
  291.     end;
  292.   ping=function()
  293.     updateDisplay(split(cmdpost("ping").readAll(), "\n")[2])
  294.   end;
  295.     server=function(...)
  296.         local args = {...}
  297.         sendstr = table.concat(args, " ")
  298.     return updateDisplay(split(cmdpost(sendstr).readAll(), "\n")[2])
  299.     end;
  300.   swapServ=function(...)
  301.   local args = {...}
  302.   local sendstr = "mvserv "..table.concat(args, " ")
  303.   return updateDisplay(split(cmdpost(sendstr).readAll(), "\n")[2])
  304.   end;
  305.   swapChannel=function(channel)
  306.   local sendstr = "chanc "..channel
  307.   return updateDisplay(split(cmdpost(sendstr).readAll(), "\n")[2])
  308.   end;
  309. }
  310.  
  311. ServCmdTbl = {
  312.     mvserv = function(NewServ)
  313.   post(name.."#0$f has left this server")
  314.     postLink="http://"..NewServ[3]
  315.     getLink="http://"..NewServ[4]
  316.     post(name.."#0$f has joined this server")
  317.     end;
  318.   cc = function(ChannelData)
  319.   post(name.."#0$f has left this channel")
  320.   channel = ChannelData[3]
  321.   post(name.."#0$f has joined this channel")
  322.   end;
  323. }
  324.  
  325. function ProcServCmd(str)
  326.     local data2 = split(str, "\n")
  327.     str2 = data2[#data2]
  328.     if str2:sub(1,10) == "servercmd;" then
  329.         local strT = split(str2, ";")
  330.         for k,v in pairs(ServCmdTbl) do
  331.             if "servercmd;"..k == strT[1]..";"..strT[2] then
  332.                 cmd = strT[2]
  333.                 ServCmdTbl[cmd](strT)
  334.                 return false
  335.             end
  336.         end
  337.         return true
  338.     end
  339.     return true
  340. end
  341.  
  342. function processCommand(str)
  343.     if str:sub(1,1)=="/" then
  344.         local strT=split(str," ")
  345.         for k,v in pairs(commandTable) do
  346.             if "/"..k==strT[1] then
  347.                 return commandTable[strT[1]:sub(2)]
  348.             end
  349.         end
  350.         return 4
  351.     end
  352.     return false
  353. end
  354. --cake
  355. function update()
  356.   updateContent = http.get(updateLink)
  357.   updateContent = updateContent.readAll()
  358.   file = fs.open(currentProgram, "r")
  359.   fileContent = file.readAll()
  360.   file.close()
  361.   if fileContent == updateContent then
  362.     term.setCursorPos(1, screenY)
  363.     write(currentProgram.." is up to date!")
  364.   else
  365.     term.setCursorPos(1, screenY)
  366.     write("New update available! Installing now...")
  367.     sleep(1)
  368.     file = fs.open(currentProgram, "w")
  369.     file.write(updateContent)
  370.     file.close()
  371.     term.setCursorPos(1, screenY)
  372.     term.clearLine()
  373.     write("Done! Restarting "..currentProgram.." now!")
  374.   isRestart=true
  375.   commandTable["exit"]("Updating client...")
  376.   end
  377.  
  378. end
  379. --test
  380. function play()
  381.   redstone.setOutput("back", true)
  382.   sleep(0.1)
  383.   redstone.setOutput("back", false)
  384.   sleep(0.3)
  385.   redstone.setOutput("back", true)
  386.   sleep(0.1)
  387.   redstone.setOutput("back",false)
  388.   sleep(0.05)
  389.   redstone.setOutput("back",true)
  390.   sleep(0.1)
  391.   redstone.setOutput("back",false)
  392. end
  393.  
  394. function clearY(fromY, toY)
  395.   repeat
  396.     term.setBackgroundColor(colors.black)
  397.     term.setTextColor(colors.white)
  398.     term.setCursorPos(1, fromY)
  399.     term.clearLine()
  400.     fromY = fromY+1
  401.   until fromY >= toY
  402. end
  403.  
  404. function receive()
  405.  while true do
  406.    get = http.get(getLink, {usr= name, slines=1, chan = channel})
  407.    lastget = http.get(getLink, {usr= name, slines=1, chan = channel})
  408.    if not get then error("Lost connection to server...") end
  409.    get = get.readAll()
  410.    lastget = lastget.readAll()
  411.    tget = get
  412.     get = string.gsub(get,"\\(.)","%1")
  413.     if get ~= getOld then
  414.         dop = ProcServCmd(get)
  415.         if dop == true then
  416.             xOld, yOld = term.getCursorPos()
  417.             clearY(1, screenY)
  418.             term.setCursorPos(1,1)
  419.             cprint(get)
  420. -- if string.find(get, "#4") then
  421. --   lastmsg = string.gsub(string.sub(lastget, (#lastget-string.find(string.reverse(lastget), "4#")+2), #lastget), "#0$f", "", 1)
  422.      lastmsg = string.gsub(lastget,"\\(.)","%1")   
  423.      if (lastmsg ~= "<"..inputName.."> "..inputMessage) and (justStarted == false) then
  424.        chatbox.say(lastmsg)
  425.      end
  426.      justStarted = false
  427.  --end
  428.         end
  429.         getOld = get
  430.         term.setCursorPos(xOld, yOld)
  431.     term.setBackgroundColor(colors.black)
  432.     term.setTextColor(colors.white)
  433.     end
  434.     sleep(1)
  435.   end
  436. end
  437.  
  438. function send()
  439.   while true do
  440.     event, inputSide, inputName, inputMessage = os.pullEvent("chat_message")
  441.     post("<"..inputName.."> "..inputMessage)
  442.   end
  443. end
  444.  
  445. clearY(screenY+1, screenY+3)
  446.  
  447. get = http.get(getLink, {usr = name, slines=1, chan = channel})
  448. get = get.readAll()
  449. get = getOld
  450. local ok,err=pcall(parallel.waitForAny,send, receive)
  451. print(err)
  452. if isRestart then return shell.run(currentProgram,name)
  453. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement