Advertisement
Guest User

pschat

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