Advertisement
LDDestroier

PSChat v3 Mirror

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