sPhone/src/apps/buddies.lua

129 lines
2.8 KiB
Lua
Raw Normal View History

2015-10-04 21:36:38 +02:00
local w, h = term.getSize()
local users
2015-09-26 11:30:40 +02:00
local function clear()
2015-10-04 21:36:38 +02:00
term.setBackgroundColor(colors.white)
2015-09-26 11:30:40 +02:00
term.clear()
term.setCursorPos(1,1)
2015-10-04 21:36:38 +02:00
term.setTextColor(colors.black)
end
local function header(aR, xChar)
if not xChar then
local xChar = "X"
end
local w, h = term.getSize()
paintutils.drawLine(1,1,w,1,colors.brown)
term.setTextColor(colors.white)
if aR then
term.setCursorPos(2,1)
write("Add")
term.setCursorPos(7,1)
write("Remove")
end
term.setCursorPos(w,1)
write(xChar)
end
users = {}
2015-10-30 19:28:14 +01:00
2015-10-04 21:36:38 +02:00
if fs.exists("/.sPhone/config/buddies") then
local f = fs.open("/.sPhone/config/buddies", "r")
repeat
local line = f.readLine()
table.insert(users, line)
until not line
f.close()
end
local function add()
clear()
header(false, "<")
term.setBackgroundColor(colors.white)
term.setTextColor(colors.black)
term.setCursorPos(2,3)
2015-11-27 22:49:24 +01:00
visum.align("center", " Name",false,3)
2015-10-04 21:36:38 +02:00
term.setCursorPos(2,5)
2015-10-04 21:53:01 +02:00
term.setCursorBlink(true)
2015-10-04 21:36:38 +02:00
local e,_,x,y = os.pullEvent()
if e == "mouse_click" then
if y == 1 and x == w then
return
end
end
2015-10-30 19:28:14 +01:00
addUser = read()
2015-10-04 21:36:38 +02:00
term.setCursorBlink(false)
2015-11-11 17:42:54 +01:00
local check = http.post("http://sertex.x10.bz/exists.php","user="..addUser).readLine()
2015-10-04 21:36:38 +02:00
2015-10-30 19:28:14 +01:00
if check == "true" then
2015-10-04 21:36:38 +02:00
table.insert(users,add)
local f = fs.open("/.sPhone/config/buddies", "a")
2015-10-30 19:28:14 +01:00
f.write(addUser.."\n")
2015-10-04 21:36:38 +02:00
f.close()
sPhone.winOk("Added!")
2015-10-30 19:28:14 +01:00
else
sPhone.winOk("User does", "not exist!")
end
2015-10-04 21:36:38 +02:00
end
local function remove()
while true do
clear()
header(false, "<")
term.setBackgroundColor(colors.white)
term.setTextColor(colors.black)
term.setCursorPos(1,2)
for i = 1, #users do
print(users[i])
end
2015-10-30 19:28:14 +01:00
_,_,x,y = os.pullEvent("mouse_click")
2015-10-04 21:36:38 +02:00
if y == 1 and x == w then
return
elseif y ~= 1 then
2015-10-30 19:28:14 +01:00
userToRemove = users[y-1]
if userToRemove then
if sPhone.yesNo("Remove "..userToRemove.."?",nil) then
2015-10-04 21:36:38 +02:00
local f = fs.open("/.sPhone/config/buddies", "w")
2015-10-30 19:28:14 +01:00
oldUsers = users
users = {}
for i = 1, #oldUsers do
if oldUsers[i] ~= oldUsers[y-1] then
f.writeLine(oldUsers[i])
table.insert(users, oldUsers[i])
end
2015-10-04 21:36:38 +02:00
end
f.close()
end
end
end
end
end
2015-10-30 19:28:14 +01:00
2015-10-04 21:36:38 +02:00
while true do
local function redraw()
clear()
header(true, "X")
term.setBackgroundColor(colors.white)
term.setTextColor(colors.black)
2015-10-30 19:28:14 +01:00
2015-10-04 21:36:38 +02:00
for i = 1, #users do
print(users[i])
end
end
redraw()
local _,_,x,y = os.pullEvent("mouse_click")
if y == 1 then
if x >= 2 and x <= 4 then
add()
elseif x >= 7 and x <= 12 then
remove()
elseif x == w then
return
end
else
if users[y-1] then
shell.run("/.sPhone/apps/sms", users[y-1])
end
end
2015-09-26 11:30:40 +02:00
end