Posted 30 May 2012 - 06:44 PM
Hi,
So, today, I wrote my first LUA program. It's a news aggregator, which pulls news items from my server's website using the HTTP API.
It appears to work perfecltly. However, when I run the "lua" program on any machine, the output from my program (Which runs at startup) ends up being execurted by the LUA prompt.
EDIT: Added screenshots
This is the output of my program.
And this is what happens when I run "lua".
Here's my code..
Snippet added to serverwide "startup"
News program
So, today, I wrote my first LUA program. It's a news aggregator, which pulls news items from my server's website using the HTTP API.
It appears to work perfecltly. However, when I run the "lua" program on any machine, the output from my program (Which runs at startup) ends up being execurted by the LUA prompt.
EDIT: Added screenshots
This is the output of my program.
And this is what happens when I run "lua".
Here's my code..
Snippet added to serverwide "startup"
if not fs.exists("/NewsCache/NoStartup") then
shell.run("news", "update")
shell.run("news", "read")
end
News program
appver = "0.1"
term.clear() -- Clear the terminal
term.setCursorPos(1, 1) -- Set the cursor position to the top left
local tArgs = { ... } -- Get the shell args
local x, y = term.getSize() -- Get the size of the terminal (For == NEWS == headers)
local headerNum = ( x / 2 ) - 4
local headerString = string.rep("=", headerNum)
function outputHeader()
print ( headerString .. " NEWS " .. headerString)
end
function outputFooter()
print ( string.rep("=", (x - 2) ) )
end
function update(silent)
if silent == nil then
silent = false
end
if not silent then
print ( "Checking for a news update.." )
end
local h = http.get("http://archivesmc.com/TekkitNewsVersion.txt")
if h == nil then
print ( "There was a problem checking for updates." )
return
end
latestVersion = tonumber(h.readAll())
h.close()
if localVersion < latestVersion then
local updateNum = latestVersion - localVersion
print ("Downloading ".. updateNum .." update(s).")
i = 0
curUpdate = (localVersion + 1)
while i < updateNum do
h = http.get("http://archivesmc.com/TekkitNews/"..curUpdate)
if h == nil then
print ("Unable to download update #" .. curUpdate)
else
local filename = "NewsCache/news/"..curUpdate
local data = h.readAll()
h.close()
if not fs.exists(filename) then
h = fs.open(filename, "w")
h.write(data)
h.close()
if not silent then
print ( "Downloaded update #"..curUpdate..".")
end
else
if not silent then
print ( "Update #" .. curUpdate .. " already downloaded, skipping.." )
end
end
end
i = (i + 1)
curUpdate = (curUpdate + 1)
end
h = fs.open("NewsCache/version", "w")
h.write(latestVersion)
h.close()
print ( "Updated to version " .. latestVersion .. "." )
localVersion = latestVersion
else
print ("Nothing to update.")
end
end
function read(num)
if num == nil then
num = localVersion -- Just get the latest news item.
end
local filepath = "NewsCache/news/" .. num
if not fs.exists( filepath ) then
print ( "Update "..num.." cannot be found." )
print ( "The latest stored update is "..localVersion.."." )
return
end
h = fs.open( filepath, "r" )
data = h.readAll()
h.close()
outputHeader()
print ( data )
outputFooter()
end
if not fs.exists( "NewsCache" ) then -- If the News cache folder doesn't exist..
fs.makeDir( "NewsCache" ) -- Create it
end
if not fs.exists( "NewsCache/news" ) then -- If the News cache is missing its news folder..
fs.makeDir( "NewsCache/news" ) -- Create it
end
localVersion = 0
if not fs.exists( "NewsCache" ) then -- If the News cache folder doesn't exist..
fs.makeDir( "NewsCache" ) -- Create it
end
if not fs.exists( "NewsCache/news" ) then -- If the News cache is missing its news folder..
fs.makeDir( "NewsCache/news" ) -- Create it
end
localVersion = 0
if fs.exists( "NewsCache/version" ) then -- If we have a version file, load it
h = fs.open( "NewsCache/version", "r" )
localVersion = tonumber(h.readAll())
h.close()
else -- If not, create it and update
h = fs.open( "NewsCache/version", "w")
h.write("0")
h.close()
update()
term.clear()
term.setCursorPos(1, 1)
read()
return
end
if #tArgs == 0 then -- No args?
shell.run("news", "help")
return
elseif tArgs[1] == "update" then -- Update?
update()
return
elseif tArgs[1] == "read" then -- Read?
read(tArgs[2])
return
elseif tArgs[1] == "startup" then -- Startup check?
if fs.exists("NewsCache/NoStartup") then
fs.delete("NewsCache/NoStartup")
print ( "Startup check enabled.")
else
fs.open("NewsCache/NoStartup", "w")
print ( "Startup check disabled." )
end
elseif tArgs[1] == "help" then -- Help?
print ( "News V " .. appver .. " by g/arbot" )
print ( "" )
print ( "-- Command listing --" )
print ( "" )
print ( "news help: This information" )
print ( "news update: Check for news" )
print ( " updates." )
print ( "news read [entry]: Read the" )
print ( " news (entry optional)" )
print ( "news startup: Enable or disable")
print ( " startup check" )
else -- Not recognized?
print (tArgs[1] .. " is not a recognized command. Try help for more info.")
end