PaymentOption

Intercept

Sep 17th, 2012
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.54 KB | None | 0 0
  1. -- Rednet activity logger, written by PaymentOption --
  2. -- Freeware as long as proper credits are given --
  3.  
  4. -- HTTP related methods --
  5. function GetTimePage()
  6.     local sRespone = nil
  7.     local sLine = ""
  8.    
  9.     sResponse = http.get( "http://www.timeanddate.com/library/abbreviations/timezones/na/pst.html" )
  10.    
  11.     if sResponse then
  12.         for nLine = 1, 31 do
  13.             sLine = sResponse.readLine()
  14.         end
  15.         sResponse.close()
  16.        
  17.         return true, sLine
  18.     else
  19.         return false, nil
  20.     end
  21. end
  22.  
  23. function GetTimeFromLine()
  24.     local bSuccess, sLine = GetTimePage()
  25.    
  26.     if bSuccess then
  27.         _, nTimePos = string.find( sLine, '<span id="ij0">' )
  28.         nEndTimePos = string.find( sLine, '</span>', nTimePos )
  29.        
  30.         local sTime = string.sub( sLine, nTimePos + 1, nEndTimePos - 1 )
  31.         return sTime
  32.     else
  33.         return nil
  34.     end
  35. end
  36. --------------------------
  37.  
  38. -- Variables --
  39. nScreenWidth, nScreenHeight = term.getSize()
  40.  
  41. nLines = 0
  42. sTime = GetTimeFromLine()
  43. sDirectory = "Rednet_Logs"
  44. file = nil
  45.  
  46. sStatus = "Logging"
  47.  
  48. -- Check if the log file already exists --
  49. if not fs.exists( sDirectory ) then
  50.     fs.makeDir( sDirectory )
  51. end
  52.  
  53. local tLogFiles = fs.list( sDirectory )
  54. file = fs.open( sDirectory .. "/" .. #tLogFiles+1 .. "_Log" , "w" )
  55. tLogFile = nil -- Remove the variable from memeory.
  56. ------------------------------------------
  57. ---------------
  58.  
  59. -- GUI Methods --
  60. function printCentered( nHeight, sString )
  61.     term.setCursorPos( nScreenWidth/2 - string.len( sString )/2, nHeight )
  62.     term.write( sString )
  63. end
  64.  
  65. function printRight( nHeight, sString )
  66.     term.setCursorPos( nScreenWidth - string.len( sString ), nHeight )
  67.     term.write( sString )
  68. end
  69.  
  70. function printBorder()
  71.     term.setCursorPos( 1, 1 )
  72.     term.write( "+" .. string.rep( "-", nScreenWidth - 2 ) .. "+" )
  73.    
  74.     for nHeight = 2, nScreenHeight - 1 do
  75.         term.setCursorPos( 1, nHeight )
  76.         term.write( "|" )
  77.        
  78.         term.setCursorPos( nScreenWidth, nHeight )
  79.         term.write( "|" )
  80.     end
  81.    
  82.     term.setCursorPos( 1, nScreenHeight )
  83.     term.write( "+" .. string.rep( "-", nScreenWidth - 2 ) .. "+" )
  84.    
  85.     printRight( nScreenHeight - 1, "[S]top" )
  86. end
  87.  
  88. function printStatus( nHeight )
  89.     printCentered( nHeight, sStatus .. "..." )
  90.     printCentered( nHeight + 2, "Messages logged: " .. nLines )
  91.        
  92.     term.setCursorPos( 2, nScreenHeight - 1 )
  93.     term.write( "Rednet Logger by PaymentOption")
  94. end
  95.  
  96. function clearScreen()
  97.     term.clear()
  98.     term.setCursorPos( 1, 1 )
  99. end
  100. -----------------
  101.  
  102. -- Interception Methods --
  103. function interceptMessages()
  104.     while true do
  105.         local nSender, sMessage = rednet.receive()
  106.         sTime = GetTimeFromLine()
  107.        
  108.         file.writeLine( sTime .. ":" .. nSender .. ":" .. sMessage )
  109.         nLines = nLines + 1
  110.         printStatus( 2 )
  111.     end
  112. end
  113.  
  114. function openExistantModem()
  115.     local tSides = rs.getSides()
  116.    
  117.     for index, side in ipairs( tSides ) do
  118.         if peripheral.isPresent( side ) and peripheral.getType( side ) == "modem" then
  119.             rednet.open( side )
  120.             return true
  121.         end
  122.     end
  123.    
  124.     return false
  125. end
  126.  
  127. function waitForKeyPress()
  128.     while true do
  129.         local t = os.startTimer( 0.2 )
  130.         local event, key = os.pullEvent()
  131.        
  132.         if event == "timer" and key == t then
  133.             return
  134.         elseif event == "key" and key == 31 then
  135.             file.close()
  136.             clearScreen()
  137.             printBorder()
  138.             printCentered( 9, "System rebooting..." )
  139.             os.reboot()
  140.         end
  141.     end
  142. end
  143. --------------------------
  144.  
  145. -- Main loop --
  146. -- Check if a modem exists.
  147. if not openExistantModem() then
  148.     error( "No modem found." )
  149. end
  150.  
  151. while true do
  152.     clearScreen()
  153.     printBorder()
  154.     printStatus( 2 )
  155.    
  156.     parallel.waitForAny( waitForKeyPress, interceptMessages )
  157. end
  158. ---------------
Advertisement
Add Comment
Please, Sign In to add comment