Advertisement
Guest User

Untitled

a guest
Dec 26th, 2012
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 1.57 KB | None | 0 0
  1. $lines = [] #Global array to store lines in
  2. $htmlhead = '<div class="block">\n    <h1>Chat</h1>\n'
  3. $htmlfoot = ' </div>'
  4. def parse_file
  5.     $lines = [] #Clear array in case this is looped instead of run in cron
  6.     f = File.new("server.log") #Load server log
  7.     f.each do |line|
  8.         match = /^((\d+-\d+-\d+)\s(\d+:\d+:\d+) \[INFO\] (<(.+?)>\s+(?!P\s)(.+)))$/.match(line) #match regex against line
  9.         # Example for: 2011-03-30 10:48:47 [INFO] <pname023> test message thing
  10.         # [1] = 2011-03-30 10:48:47 [INFO] <pname023> test message thing
  11.         # [2] = 2011-03-30
  12.         # [3] = 10:40:47
  13.         # [4] = <pname023> test message thing
  14.         # [5] = pname023
  15.         # [6] = test message thing
  16.         unless match.nil?
  17.             str = "<div class=\"chatwrap\"><div class=\"chattop\"><div class=\"chattime\">\n<img src=\"images/icons/#{match[5]}.png\" title=\"#{match[5]}\"><em>#{match[2]} #{match[3]}</em>\n</div></div><div class=\"chatmain\"><p>#{match[6]}</p></div><div class=\"chatbot\"></div>\n</div><br clear=\"all\">"
  18.             $lines << str
  19.         end
  20.     end
  21.     f.close
  22. end
  23.  
  24. def write_file
  25.     fstr = "#{$htmlhead}#{$lines.last(50).join}#{$htmlfoot}"
  26.     $lines.clear #in case of loop
  27.     f = File.new("chatbox.html", "w")
  28.     f.print(fstr)
  29.     f.close
  30.  
  31.     #If you need to cp or scp this file to your public directory, do this here.
  32. end
  33.  
  34. def loop
  35.     parse_file
  36.     write_file
  37. end
  38.  
  39. loop #see below
  40.  
  41. #This file is written to be run with cron
  42. #If you want to just run this file once and have it work without cron, replace line 39 (loop) with the following
  43. #code and remove the comments
  44.  
  45. #while true
  46. #   loop
  47. #   sleep(60) #sleep for one minute
  48. #end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement