Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- os.loadAPI("gui")
- local json = loadfile("json")()
- local w, h = term.getSize()
- local sessionkey
- local sendDat
- local chid
- local input
- local function jncmd(c)
- local h = http.get("https://thejeremail.net/?".. c)
- local ret = h.readAll()
- h.close()
- return ret
- end
- function cmd(c)
- local h = http.get("https://thejeremail.net/apps/JereChat?" .. sessionkey .. "&" .. c)
- local ret = h.readAll()
- h.close()
- return ret
- end
- local function createSession()
- return jncmd("prot=tosess")
- end
- local function login(user, pass)
- local ret = jncmd(sessionkey .. "&prot=tologin&username=" .. user .. "&password=" .. pass)
- if ret == "E:0" then
- return true
- else
- return false
- end
- end
- local function changeChannel(chan)
- sendDat("Resolving " .. chan)
- chid = cmd("chan=" .. chan)
- sendDat("Entering " .. chan)
- chid = cmd("chid=" .. chid)
- sendDat("Entered " .. chan)
- end
- local function listenMsgs()
- while true do
- local ret = cmd("getmsg=true&jpush=true&msc=10")
- if ret and ret ~= '""' and ret ~= '' then
- local dec = json:decode(ret)
- for id, msg in pairs(dec) do
- if msg["senderID"] ~= "E2" then
- sendDat(tostring(msg["senderID"]) .. ": " .. tostring(msg["content"]))
- end
- end
- input:Draw()
- term.setCursorPos(input:GetPosition())
- term.setCursorBlink(true)
- end
- end
- end
- local function sendMsg(txt)
- local ret = cmd("putmsg=true&prt=cct2&msg=" .. textutils.urlEncode(txt))
- -- sendDat(ret)
- end
- local loadingS = gui.NewUI()
- loadingS:BackgroundColor(colors.white)
- local img = loadingS:New("ImageLabel")
- img:Size(w, h)
- img:ShowBackground(false)
- local loginS = gui.NewUI()
- loginS:BackgroundColor(colors.white)
- local title = loginS:New("TextLabel")
- title:Size(w, 1)
- title:BackgroundColor(colors.gray)
- title:TextColor(colors.white)
- title:Text("Login to JereChat")
- local userL = loginS:New("TextLabel")
- userL:ShowBackground(false)
- userL:Text("Username:")
- userL:Size(9, 1)
- userL:TextColor(colors.black)
- userL:Position(13, 6)
- local userI = loginS:New("TextBox")
- userI:BackgroundColor(colors.gray)
- userI:ActiveTextColor(colors.lightGray)
- userI:TextColor(colors.black)
- userI:Text("")
- userI:TextXAlignment("left")
- userI:Size(15, 1)
- userI:Position(23, 6)
- local passL = loginS:New("TextLabel")
- passL:ShowBackground(false)
- passL:Text("Password:")
- passL:Size(9, 1)
- passL:TextColor(colors.black)
- passL:Position(13, 8)
- local passI = loginS:New("TextBox")
- passI:BackgroundColor(colors.gray)
- passI:ActiveTextColor(colors.lightGray)
- passI:TextColor(colors.black)
- passI:Text("")
- passI:TextXAlignment("left")
- passI:Size(15, 1)
- passI:Position(23, 8)
- passI:TextChar("*")
- local stat = loginS:New("TextLabel")
- stat:ShowBackground(false)
- stat:Text("")
- stat:Size(w, 1)
- stat:Position(1, 12)
- local logB = loginS:New("TextButton")
- logB:Text("Login")
- logB:Size(7, 1)
- logB:BackgroundColor(colors.lightGray)
- logB:TextColor(colors.black)
- logB:Position(22, 10)
- local mainS = gui:NewUI()
- mainS:BackgroundColor(colors.white)
- local tBar = mainS:New("TextLabel")
- tBar:Size(w, 1)
- tBar:BackgroundColor(colors.gray)
- tBar:Text("")
- local tFrame = mainS:New("TextLabel")
- tFrame:Text("")
- tFrame:Size(w, h - 2)
- tFrame:ShowBackground(false)
- tFrame:Position(1, 2)
- input = mainS:New("TextBox")
- input:Text("")
- input:TextXAlignment("left")
- input:Size(w, 1)
- input:BackgroundColor(colors.lightGray)
- input:ActiveTextColor(colors.lightGray)
- input:Position(1, h)
- input:Overlap(true)
- local bods = {}
- local don = 0
- for i = 1, h - 2 do
- local bo = tFrame:New("TextLabel")
- bo:TextColor(colors.black)
- bo:Text("")
- bo:Size(w, 1)
- bo:Position(1, i)
- bo:ShowBackground(false)
- bo:TextXAlignment("left")
- table.insert(bods, bo)
- end
- local function splitS(str)
- local new = {}
- local lastB = 1
- local lastS = 1
- for i = 1, #str do
- if str:sub(i, i) == " " then
- lastS = i
- end
- if i - lastB + 1 == w then
- if lastS + 1 == lastB then
- table.insert(new, str:sub(lastB, i))
- lastS = i
- lastB = i + 1
- else
- table.insert(new, str:sub(lastB, lastS - 1))
- lastB = lastS + 1
- end
- end
- end
- if lastB ~= #str then
- table.insert(new, str:sub(lastB, #str))
- end
- return new
- end
- function sendDat(str)
- for i,v in pairs(splitS(str)) do
- -- if don >= #bods then
- for m = 1, #bods - 1 do
- bods[m]:Text(bods[m + 1]:GetText())
- end
- -- end
- bods[#bods]:Text(v)
- don = don + 1
- end
- tFrame:Draw()
- end
- logB:Button1Click(function()
- stat:TextColor(colors.black)
- stat:Text("Logging in...")
- stat:Draw()
- local re = login(userI:GetText(), passI:GetText())
- if re == false then
- stat:TextColor(colors.red)
- stat:Text("Login failed")
- stat:Draw()
- else
- mainS:Draw()
- loginS:StopListen()
- changeChannel("talk")
- input:Focus()
- parallel.waitForAny(function() mainS:Listen() end, listenMsgs)
- end
- end)
- input:EnterPress(function()
- local msg = input:GetText()
- if msg ~= "" then
- input:Text("")
- input:Focus()
- sendMsg(msg)
- input:Draw()
- end
- end)
- sessionkey = createSession()
- loginS:Draw()
- loginS:Listen()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement