Advertisement
Alakazard12

JereChat

Mar 29th, 2014
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.06 KB | None | 0 0
  1. os.loadAPI("gui")
  2. local json = loadfile("json")()
  3.  
  4. local w, h = term.getSize()
  5.  
  6. local sessionkey
  7. local sendDat
  8. local chid
  9. local input
  10.  
  11. local function jncmd(c)
  12.     local h = http.get("https://thejeremail.net/?".. c)
  13.     local ret = h.readAll()
  14.     h.close()
  15.     return ret
  16. end
  17.  
  18. function cmd(c)
  19.     local h = http.get("https://thejeremail.net/apps/JereChat?" .. sessionkey .. "&" .. c)
  20.     local ret = h.readAll()
  21.     h.close()
  22.     return ret
  23. end
  24.  
  25. local function createSession()
  26.     return jncmd("prot=tosess")
  27. end
  28.  
  29. local function login(user, pass)
  30.     local ret = jncmd(sessionkey .. "&prot=tologin&username=" .. user .. "&password=" .. pass)
  31.     if ret == "E:0" then
  32.         return true
  33.     else
  34.         return false
  35.     end
  36. end
  37.  
  38. local function changeChannel(chan)
  39.     sendDat("Resolving " .. chan)
  40.     chid = cmd("chan=" .. chan)
  41.     sendDat("Entering " .. chan)
  42.     chid = cmd("chid=" .. chid)
  43.  sendDat("Entered " .. chan)
  44. end
  45.  
  46. local function listenMsgs()
  47.     while true do
  48.         local ret = cmd("getmsg=true&jpush=true&msc=10")
  49.         if ret and ret ~= '""' and ret ~= '' then
  50.             local dec = json:decode(ret)
  51.             for id, msg in pairs(dec) do
  52.     if msg["senderID"] ~= "E2" then
  53.                 sendDat(tostring(msg["senderID"]) .. ": " .. tostring(msg["content"]))
  54.         end
  55.     end
  56.    input:Draw()
  57.    term.setCursorPos(input:GetPosition())
  58.    term.setCursorBlink(true)
  59.         end
  60.     end
  61. end
  62.  
  63. local function sendMsg(txt)
  64.     local ret = cmd("putmsg=true&prt=cct2&msg=" .. textutils.urlEncode(txt))
  65.  -- sendDat(ret)
  66. end
  67.  
  68. local loadingS = gui.NewUI()
  69. loadingS:BackgroundColor(colors.white)
  70. local img = loadingS:New("ImageLabel")
  71. img:Size(w, h)
  72. img:ShowBackground(false)
  73.  
  74. local loginS = gui.NewUI()
  75. loginS:BackgroundColor(colors.white)
  76.  
  77. local title = loginS:New("TextLabel")
  78. title:Size(w, 1)
  79. title:BackgroundColor(colors.gray)
  80. title:TextColor(colors.white)
  81. title:Text("Login to JereChat")
  82.  
  83. local userL = loginS:New("TextLabel")
  84. userL:ShowBackground(false)
  85. userL:Text("Username:")
  86. userL:Size(9, 1)
  87. userL:TextColor(colors.black)
  88. userL:Position(13, 6)
  89.  
  90. local userI = loginS:New("TextBox")
  91. userI:BackgroundColor(colors.gray)
  92. userI:ActiveTextColor(colors.lightGray)
  93. userI:TextColor(colors.black)
  94. userI:Text("")
  95. userI:TextXAlignment("left")
  96. userI:Size(15, 1)
  97. userI:Position(23, 6)
  98.  
  99. local passL = loginS:New("TextLabel")
  100. passL:ShowBackground(false)
  101. passL:Text("Password:")
  102. passL:Size(9, 1)
  103. passL:TextColor(colors.black)
  104. passL:Position(13, 8)
  105.  
  106. local passI = loginS:New("TextBox")
  107. passI:BackgroundColor(colors.gray)
  108. passI:ActiveTextColor(colors.lightGray)
  109. passI:TextColor(colors.black)
  110. passI:Text("")
  111. passI:TextXAlignment("left")
  112. passI:Size(15, 1)
  113. passI:Position(23, 8)
  114. passI:TextChar("*")
  115.  
  116. local stat = loginS:New("TextLabel")
  117. stat:ShowBackground(false)
  118. stat:Text("")
  119. stat:Size(w, 1)
  120. stat:Position(1, 12)
  121.  
  122. local logB = loginS:New("TextButton")
  123. logB:Text("Login")
  124. logB:Size(7, 1)
  125. logB:BackgroundColor(colors.lightGray)
  126. logB:TextColor(colors.black)
  127. logB:Position(22, 10)
  128.  
  129. local mainS = gui:NewUI()
  130. mainS:BackgroundColor(colors.white)
  131.  
  132. local tBar = mainS:New("TextLabel")
  133. tBar:Size(w, 1)
  134. tBar:BackgroundColor(colors.gray)
  135. tBar:Text("")
  136.  
  137. local tFrame = mainS:New("TextLabel")
  138. tFrame:Text("")
  139. tFrame:Size(w, h - 2)
  140. tFrame:ShowBackground(false)
  141. tFrame:Position(1, 2)
  142.  
  143. input = mainS:New("TextBox")
  144. input:Text("")
  145. input:TextXAlignment("left")
  146. input:Size(w, 1)
  147. input:BackgroundColor(colors.lightGray)
  148. input:ActiveTextColor(colors.lightGray)
  149. input:Position(1, h)
  150. input:Overlap(true)
  151.  
  152. local bods = {}
  153. local don = 0
  154.  
  155. for i = 1, h - 2 do
  156.     local bo = tFrame:New("TextLabel")
  157.     bo:TextColor(colors.black)
  158.     bo:Text("")
  159.     bo:Size(w, 1)
  160.     bo:Position(1, i)
  161.     bo:ShowBackground(false)
  162.     bo:TextXAlignment("left")
  163.  
  164.     table.insert(bods, bo)
  165. end
  166.  
  167. local function splitS(str)
  168.     local new = {}
  169.  
  170.     local lastB = 1
  171.     local lastS = 1
  172.     for i = 1, #str do
  173.         if str:sub(i, i) == " " then
  174.             lastS = i
  175.         end
  176.  
  177.         if i - lastB + 1 == w then
  178.             if lastS + 1 == lastB then
  179.                 table.insert(new, str:sub(lastB, i))
  180.                 lastS = i
  181.                 lastB = i + 1
  182.             else
  183.                 table.insert(new, str:sub(lastB, lastS - 1))
  184.                 lastB = lastS + 1
  185.             end
  186.         end
  187.     end
  188.     if lastB ~= #str then
  189.         table.insert(new, str:sub(lastB, #str))
  190.     end
  191.  
  192.     return new
  193. end
  194.  
  195. function sendDat(str)
  196.     for i,v in pairs(splitS(str)) do
  197.         -- if don >= #bods then
  198.             for m = 1, #bods - 1 do
  199.                 bods[m]:Text(bods[m + 1]:GetText())
  200.             end
  201.         -- end
  202.         bods[#bods]:Text(v)
  203.         don = don + 1
  204.     end
  205.     tFrame:Draw()
  206. end
  207.  
  208. logB:Button1Click(function()
  209.     stat:TextColor(colors.black)
  210.     stat:Text("Logging in...")
  211.     stat:Draw()
  212.     local re = login(userI:GetText(), passI:GetText())
  213.     if re == false then
  214.         stat:TextColor(colors.red)
  215.         stat:Text("Login failed")
  216.         stat:Draw()
  217.     else
  218.         mainS:Draw()
  219.         loginS:StopListen()
  220.         changeChannel("talk")
  221.   input:Focus()
  222.         parallel.waitForAny(function() mainS:Listen() end, listenMsgs)
  223.     end
  224. end)
  225.  
  226. input:EnterPress(function()
  227.     local msg = input:GetText()
  228.     if msg ~= "" then
  229.   input:Text("")
  230.         input:Focus()
  231.         sendMsg(msg)
  232.   input:Draw()
  233.     end
  234. end)
  235.  
  236. sessionkey = createSession()
  237. loginS:Draw()
  238. loginS:Listen()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement