module BotConsole::LogLine
Constants
- DEBUG_LINE_RE
- HANDLER_NOISE_RE
- IRC_LINE_RE
- MAX_BODY
- STOPPING_HANDLER_RE
- TIMESTAMP_RE
Public Instance Methods
Source
# File lib/bot_console/log_line.rb, line 19 def condense(raw) line = raw.to_s.chomp return nil if line.empty? return nil if handler_noise?(line) if (match = line.match(IRC_LINE_RE)) return format_irc( time: extract_time(line), dir: match[1], nick: nick_from_prefix(match[2]), command: match[3], target: match[4], body: match[5] ) end if (match = line.match(DEBUG_LINE_RE)) return format_debug(extract_time(line), match[1]) end format_plain(line) end
Source
# File lib/bot_console/log_line.rb, line 50 def extract_time(line) line[TIMESTAMP_RE, 1] || line[/\A\[(\d{2}:\d{2}:\d{2})\]/, 1] || "--:--:--" end
Source
# File lib/bot_console/log_line.rb, line 100 def format_debug(time, message) text = strip_irc(message) return nil if text.empty? return nil if text.start_with?("#<Cinch::") "#{time} !! #{truncate(text, MAX_BODY)}" end
Source
# File lib/bot_console/log_line.rb, line 76 def format_irc(time:, dir:, nick:, command:, target:, body:) text = strip_irc(body) case command when "PRIVMSG", "NOTICE" label = target summary = text.empty? ? command : truncate(text, MAX_BODY) "#{time} #{dir} #{nick} #{label}: #{summary}" when "JOIN" "#{time} #{dir} #{nick} -> #{target}" when "PART" part_msg = text.empty? ? "" : " (#{truncate(text, 80)})" "#{time} #{dir} #{nick} <- #{target}#{part_msg}" when "QUIT" quit_msg = text.empty? ? "" : " #{truncate(text, 80)}" "#{time} #{dir} #{nick} quit#{quit_msg}" when "NICK" "#{time} #{dir} #{nick} nick -> #{target}" else detail = [command, target, text].reject { |part| part.to_s.empty? }.join(" ") "#{time} #{dir} #{nick} #{truncate(detail, MAX_BODY)}" end end
Source
# File lib/bot_console/log_line.rb, line 109 def format_plain(line) if (match = line.match(TIMESTAMP_RE)) time = match[1] body = line.sub(TIMESTAMP_RE, "").strip body = strip_irc(body) return nil if body.empty? return "#{time} #{truncate(body, MAX_BODY)}" unless body.start_with?("[") end plain = strip_irc(line) return nil if plain.empty? truncate(plain, MAX_BODY) end
Source
# File lib/bot_console/log_line.rb, line 42 def handler_noise?(line) line.match?(HANDLER_NOISE_RE) || line.match?(STOPPING_HANDLER_RE) || line.include?("#<Cinch::Handler") || line.include?("#<Cinch::Pattern") end
Source
# File lib/bot_console/log_line.rb, line 55 def nick_from_prefix(prefix) prefix.to_s.sub(/\A:/, "").split("!", 2).first end
Source
# File lib/bot_console/log_line.rb, line 60 def strip_irc(text) text.to_s .gsub(/\x02|\x1d|\x1f|\x16/, "") .gsub(/\x03(?:\d{1,2}(?:,\d{1,2})?)?/, "") .strip end
Source
# File lib/bot_console/log_line.rb, line 68 def truncate(text, max) str = text.to_s return str if str.length <= max "#{str[0, max - 1]}…" end