immibis

IIM test 3

Jan 21st, 2012
656
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.73 KB | None | 0 0
  1. local session, nick
  2.  
  3. local term_w, term_h = term.getSize()
  4.  
  5. local function urlencode(msg)
  6.     local ok = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_.~"
  7.     local enc = ""
  8.     local fmt = "%%%02X"
  9.     for k = 1, msg:len() do
  10.         local ch = msg:sub(k, k)
  11.         if ok:find(ch, 1, true) then
  12.             enc = enc .. ch
  13.         else
  14.             enc = enc .. fmt:format(string.byte(ch))
  15.         end
  16.     end
  17.     return enc
  18. end
  19.  
  20. local function getLastIndex()
  21.     local f = http.get("http://immibis.awardspace.biz/cc/iim/get.php")
  22.     local last = tonumber(f.readLine())
  23.     f.close()
  24.     return last
  25. end
  26.  
  27. local function startSession()
  28.     local f = http.get("http://immibis.awardspace.biz/cc/iim/start.php?nick="..urlencode(nick))
  29.     local response = f.readLine()
  30.     f.close()
  31.     if response:sub(1,2) == "OK" then
  32.         session = response:sub(3)
  33.     else
  34.         print(response)
  35.     end
  36. end
  37.  
  38. local function sendMessage(msg)
  39.     http.request("http://immibis.awardspace.biz/cc/iim/send.php?s="..urlencode(session).."&msg="..urlencode(msg))
  40. end
  41.  
  42. local buffer = {}
  43. local typing = ""
  44. local typing_pos = 0
  45. local typing_scroll = 0
  46. local history_pos = 0
  47. local history = {}
  48. local lastChatIndex = getLastIndex()
  49.  
  50. local function addLine(line)
  51.     if #buffer >= term_h - 1 then
  52.         table.remove(buffer, 1)
  53.     end
  54.     table.insert(buffer, line)
  55. end
  56.  
  57. local function displayChat(msg)
  58.     while msg:len() > term_w do
  59.         addLine(msg:sub(1,term_w))
  60.         msg = msg:sub(term_w + 1)
  61.     end
  62.     addLine(msg)
  63. end
  64.  
  65. local function updateCursor()
  66.     term.setCursorPos(typing_pos - typing_scroll + 3, term_h)
  67. end
  68.  
  69. local function updateInputLine()
  70.     term.setCursorPos(1, term_h)
  71.     term.clearLine()
  72.     term.write("> " .. typing:sub(typing_scroll + 1))
  73.     updateCursor()
  74. end
  75.  
  76. local function updateScreen()
  77.     for k = 1, term_h - 1 do
  78.         term.setCursorPos(1, k)
  79.         term.clearLine()
  80.         term.write(buffer[k] or "")
  81.     end
  82.     updateCursor()
  83. end
  84.  
  85. local pollURL
  86.  
  87. local function startPoll()
  88.     pollURL = "http://immibis.awardspace.biz/cc/iim/get.php?last=" .. lastChatIndex .. "&ping=" .. session
  89.     http.request(pollURL)
  90. end
  91.  
  92. local function handlePollResponse(f)
  93.     local any = false
  94.     while true do
  95.         local line = f.readLine()
  96.         if line == nil then break end
  97.         if line == "EMPTY" then break end
  98.         if line == "END" then break end
  99.         local index = line:find(" ")
  100.         if index ~= nil then
  101.             local id = tonumber(line:sub(1, index-1))
  102.             local msg = line:sub(index+1)
  103.             lastChatIndex = id
  104.             displayChat(msg)
  105.             any = true
  106.         end
  107.     end
  108.     f.close()
  109.     if any then updateScreen() end
  110. end
  111.  
  112. repeat
  113.     print("Enter your nickname: ")
  114.     nick = read()
  115.     startSession()
  116. until session ~= nil
  117.  
  118. local function updateHScroll()
  119.     if typing_pos < typing_scroll + 4 and typing_pos > 4 then
  120.         typing_scroll = math.max(0, typing_pos - 4)
  121.         return true
  122.     elseif typing_pos > typing_scroll + term_w - 7 then
  123.         typing_scroll = typing_pos - term_w + 7
  124.         return true
  125.     else
  126.         return false
  127.     end
  128. end
  129.  
  130. local function onInputLine(msg)
  131.     sendMessage(msg)
  132. end
  133.  
  134. lastChatIndex = 0
  135. startPoll()
  136. term.setCursorBlink(true)
  137. updateScreen()
  138. updateInputLine()
  139. while true do
  140.     local evt, p1, p2 = os.pullEvent()
  141.     if evt == "http_failure" and p1 == pollURL then
  142.         displayChat("Network error")
  143.     elseif evt == "http_success" and p1 == pollURL then
  144.         handlePollResponse(p2)
  145.         startPoll()
  146.     elseif evt == "http_success" then
  147.         p2.close()
  148.     elseif evt == "char" then
  149.         typing = typing:sub(1, typing_pos)..p1..typing:sub(typing_pos + 1)
  150.         typing_pos = typing_pos + 1
  151.         updateHScroll()
  152.         updateInputLine()
  153.     elseif evt == "key" then
  154.         if p1 == 28 then
  155.             -- Enter
  156.             onInputLine(typing)
  157.             table.insert(history, typing)
  158.             typing = ""
  159.             typing_pos = 0
  160.             typing_scroll = 0
  161.             history_pos = #history
  162.             updateInputLine()
  163.         elseif p1 == 203 then
  164.             -- Left
  165.             if typing_pos > 0 then
  166.                 typing_pos = typing_pos - 1
  167.                 if updateHScroll() then
  168.                     updateInputLine()
  169.                 else
  170.                     updateCursor()
  171.                 end
  172.             end
  173.         elseif p1 == 205 then
  174.             -- Right
  175.             if typing_pos < typing:len() then
  176.                 typing_pos = typing_pos + 1
  177.                 if updateHScroll() then
  178.                     updateInputLine()
  179.                 else
  180.                     updateCursor()
  181.                 end
  182.             end
  183.         elseif p1 == 14 then
  184.             -- Backspace
  185.             if typing_pos > 0 then
  186.                 typing = typing:sub(1, typing_pos-1) .. typing:sub(typing_pos+1)
  187.                 typing_pos = typing_pos - 1
  188.                 updateHScroll()
  189.                 updateInputLine()
  190.             end
  191.         elseif p1 == 200 then
  192.             -- Up
  193.             if history_pos > 1 then
  194.                 history_pos = history_pos - 1
  195.                 typing = history[history_pos]
  196.                 typing_pos = typing:len()
  197.                 updateHScroll()
  198.                 updateInputLine()
  199.             end
  200.         elseif p1 == 208 then
  201.             -- Down
  202.             if history_pos < #history then
  203.                 history_pos = history_pos + 1
  204.                 typing = history[history_pos]
  205.                 typing_pos = typing:len()
  206.                 updateHScroll()
  207.                 updateInputLine()
  208.             end
  209.         end
  210.     end
  211. end
Advertisement
Add Comment
Please, Sign In to add comment