Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Rednet activity logger, written by PaymentOption --
- -- Freeware as long as proper credits are given --
- -- HTTP related methods --
- function GetTimePage()
- local sRespone = nil
- local sLine = ""
- sResponse = http.get( "http://www.timeanddate.com/library/abbreviations/timezones/na/pst.html" )
- if sResponse then
- for nLine = 1, 31 do
- sLine = sResponse.readLine()
- end
- sResponse.close()
- return true, sLine
- else
- return false, nil
- end
- end
- function GetTimeFromLine()
- local bSuccess, sLine = GetTimePage()
- if bSuccess then
- _, nTimePos = string.find( sLine, '<span id="ij0">' )
- nEndTimePos = string.find( sLine, '</span>', nTimePos )
- local sTime = string.sub( sLine, nTimePos + 1, nEndTimePos - 1 )
- return sTime
- else
- return nil
- end
- end
- --------------------------
- -- Variables --
- nScreenWidth, nScreenHeight = term.getSize()
- nLines = 0
- sTime = GetTimeFromLine()
- sDirectory = "Rednet_Logs"
- file = nil
- sStatus = "Logging"
- -- Check if the log file already exists --
- if not fs.exists( sDirectory ) then
- fs.makeDir( sDirectory )
- end
- local tLogFiles = fs.list( sDirectory )
- file = fs.open( sDirectory .. "/" .. #tLogFiles+1 .. "_Log" , "w" )
- tLogFile = nil -- Remove the variable from memeory.
- ------------------------------------------
- ---------------
- -- GUI Methods --
- function printCentered( nHeight, sString )
- term.setCursorPos( nScreenWidth/2 - string.len( sString )/2, nHeight )
- term.write( sString )
- end
- function printRight( nHeight, sString )
- term.setCursorPos( nScreenWidth - string.len( sString ), nHeight )
- term.write( sString )
- end
- function printBorder()
- term.setCursorPos( 1, 1 )
- term.write( "+" .. string.rep( "-", nScreenWidth - 2 ) .. "+" )
- for nHeight = 2, nScreenHeight - 1 do
- term.setCursorPos( 1, nHeight )
- term.write( "|" )
- term.setCursorPos( nScreenWidth, nHeight )
- term.write( "|" )
- end
- term.setCursorPos( 1, nScreenHeight )
- term.write( "+" .. string.rep( "-", nScreenWidth - 2 ) .. "+" )
- printRight( nScreenHeight - 1, "[S]top" )
- end
- function printStatus( nHeight )
- printCentered( nHeight, sStatus .. "..." )
- printCentered( nHeight + 2, "Messages logged: " .. nLines )
- term.setCursorPos( 2, nScreenHeight - 1 )
- term.write( "Rednet Logger by PaymentOption")
- end
- function clearScreen()
- term.clear()
- term.setCursorPos( 1, 1 )
- end
- -----------------
- -- Interception Methods --
- function interceptMessages()
- while true do
- local nSender, sMessage = rednet.receive()
- sTime = GetTimeFromLine()
- file.writeLine( sTime .. ":" .. nSender .. ":" .. sMessage )
- nLines = nLines + 1
- printStatus( 2 )
- end
- end
- function openExistantModem()
- local tSides = rs.getSides()
- for index, side in ipairs( tSides ) do
- if peripheral.isPresent( side ) and peripheral.getType( side ) == "modem" then
- rednet.open( side )
- return true
- end
- end
- return false
- end
- function waitForKeyPress()
- while true do
- local t = os.startTimer( 0.2 )
- local event, key = os.pullEvent()
- if event == "timer" and key == t then
- return
- elseif event == "key" and key == 31 then
- file.close()
- clearScreen()
- printBorder()
- printCentered( 9, "System rebooting..." )
- os.reboot()
- end
- end
- end
- --------------------------
- -- Main loop --
- -- Check if a modem exists.
- if not openExistantModem() then
- error( "No modem found." )
- end
- while true do
- clearScreen()
- printBorder()
- printStatus( 2 )
- parallel.waitForAny( waitForKeyPress, interceptMessages )
- end
- ---------------
Advertisement
Add Comment
Please, Sign In to add comment