local version =
1.7
function detectModem()
for _,side in ipairs(rs.getSides()) do
if peripheral.isPresent(side) then
if peripheral.getType(side) == "modem" then
if not rednet.isOpen(side) then
rednet.open(side)
end
return side
end
end
end
return false
end
--[[
Huge thanks goes to Cranium
for showing me a updating
technic!
]]--
local modem = detectModem()
if modem == false then
if term.isColor then
if term.isColor() then
term.setTextColor(colors.red)
term.setBackgroundColor(colors.black)
end
end
print("No modem found!")
return
else
rednet.open(modem)
end
local siteUrl = "http://Ninety7Seven.com/CC/prog/JagChat.lua"
local width, height = term.getSize()
local event,param1,param2,param3
local written = ""
local output = ""
local scrolls = 2
local site, siteFile, writeFile
local newVersion
local x,y
local id, message = 0, ""
local table1 = {}
local table2 = {}
local usersTable = {}
local command = ""
local commands = {
"logout",
"list",
"update",
"help",
"commands",
}
term.setCursorBlink(true)
function setColor(txtColor,bgColor)
if term.isColor then
if term.isColor() then
if txtColor then
term.setTextColor(txtColor)
end
if bgColor then
term.setBackgroundColor(bgColor)
end
end
end
end
function printC(inputString,spaces)
if not spaces then
spaces = " "
else
spaces = tostring(spaces)
end
string_len = string.len(inputString)
num = math.floor(width/2 - string_len/2)
for j = 1,num do
output = output..spaces
end
output = output..tostring(inputString)
for j = 1,num do
output = output..spaces
end
setColor(colors.red,colors.gray)
term.clearLine()
write(string.sub(output,1,width-1))
output = ""
end
function printN(name,inputString)
inputString = tostring(inputString)
setColor(colors.lightGray,colors.black)
term.clearLine()
if name then write(name) end
setColor(colors.yellow)
write(inputString.."\n")
term.clearLine()
if scrolls > height-2 then
term.scroll(1)
else
scrolls = scrolls + 1
end
end
function listUsers()
term.setCursorBlink(false)
usersTable = {}
rednet.broadcast(textutils.serialize({chat={listAll=true}}))
repeat
id, message = 0,""
id, message = rednet.receive(.5)
if message == textutils.serialize({chat={listMe=true}}) then
table.insert(usersTable, id)
end
until id == nil
if #usersTable == 0 then
clearF()
term.setCursorPos(x,y)
printN(_,"No users active!")
else
clearF()
term.setCursorPos(x,y)
printN(_,"Users active: ")
for h = 1,#usersTable do
clearF()
term.setCursorPos(x,y)
printN(h..": ",usersTable[h])
end
end
term.setCursorBlink(true)
end
function check4update()
term.setCursorBlink(false)
term.setCursorPos(x,y)
printN(_,"Searching for updates...")
clearF()
term.setCursorPos(x,y)
site = http.get(siteUrl)
if not site then
printN(_,"Failed! Try again later.")
term.setCursorBlink(true)
return false
end
site.readLine()
newVersion = site.readLine()
if tonumber(newVersion) > version then
printN(_,"Program is out of date, updating...")
clearF()
term.setCursorPos(x,y)
site = http.get(siteUrl)
if not site then
printN(_,"Failed! Try again later.")
term.setCursorBlink(true)
return false
end
siteFile = site.readAll()
writeFile = fs.open(shell.getRunningProgram(),"w")
writeFile.write(siteFile)
writeFile.close()
printN(_,"Update successful! Restarting program...")
clearF()
sleep(2)
rednet.broadcast(textutils.serialize({chat={login=false}}))
rednet.close("top")
shell.run(shell.getRunningProgram())
os.reboot()
else
printN(_,"Program is up to date!")
end
term.setCursorBlink(true)
end
function commandList()
clearF()
term.setCursorPos(x,y)
printN(_,"Commands:")
for o = 1,#commands do
clearF()
term.setCursorPos(x,y)
printN(o..": ","/"..commands[o])
end
end
function clearF()
term.setCursorPos(1,1)
printC(" JagChat v"..version.." ","-")
term.setCursorPos(1,scrolls)
x,y = term.getCursorPos()
term.setCursorPos(1,height-1)
term.clearLine()
printC(" Your id is #"..os.getComputerID().." ","-")
term.setCursorPos(1,height)
setColor(colors.orange,colors.gray)
term.clearLine()
write("> ")
setColor(colors.yellow)
write(written)
end
rednet.broadcast(textutils.serialize({chat={login=true}}))
setColor(_,colors.black)
term.clear()
while true do
clearF()
event,param1,param2,param3 = os.pullEvent()
if event == "char" then
if string.len(written) < width-string.len(tostring(os.getComputerID()))-4 then
written = written .. param1
end
end
if event == "key" then
if param1 == 28 then
if string.len(written) ~= 0 then
if string.sub(written,0,1) == "/" then
--commands
command = string.sub(written,2)
if command == "logout" then
break
elseif command == "list" then
listUsers()
elseif command == "update" then
check4update()
elseif command == "help" or command == "commands" then
commandList()
else
term.setCursorPos(x,y)
printN(_,"Command \""..command.."\" does not exists!")
end
else
term.setCursorPos(x,y)
printN("<"..os.getComputerID().."> ",written)
table1 = {chat={isWritten=written}}
rednet.broadcast(textutils.serialize(table1))
end
written = ""
end
elseif param1 == 14 then
written = string.sub(written,1,-2)
end
elseif event == "rednet_message" then
term.setCursorPos(x,y)
table2 = textutils.unserialize(param2)
if type(table2) == "table" then
if table2.chat then
if table2.chat.isWritten then
if param1 ~= os.getComputerID() then
printN("<"..param1.."> ",table2.chat.isWritten)
end
elseif table2.chat.login == true or table2.chat.login == false then
if table2.chat.login == true then
printN("User #"..param1.." joined the chat!","")
elseif table2.chat.login == false then
printN("User #"..param1.." left the chat!","")
end
elseif table2.chat.listAll then
if table2.chat.listAll == true then
rednet.send(param1,textutils.serialize({chat={listMe=true}}))
end
end
end
end
table2 = {}
end
end
rednet.broadcast(textutils.serialize({chat={login=false}}))
rednet.close(modem)
term.setCursorPos(1,height-1)
printC(" Chat shut down ","-")
term.setCursorPos(1,height)
setColor(colors.white,colors.black)
term.clearLine()
Comments