#!/usr/bin/env ruby
# Copyright (C) 2003-2005 Kouichirou Eto, All rights reserved.

base = File.dirname(__FILE__)+"/.." # /usr/local/qwik/bin -> /usr/local/qwik
$LOAD_PATH.unshift(File.expand_path(base+"/lib"))
$LOAD_PATH.unshift(File.expand_path(base+"/compat"))

require "qwik/qp"

def get_group(line)
  if /\A.+\[(.+)\]: .+$/ =~ line
    return $1
  end
  return nil
end

def fatal_error(msg)
  p "Fatal Error: "+msg
  exit
end

def parse(line)
  line.chomp!
  date, group, cmd, user = line.split(/: /)

  cmd = "MLClosed" if cmd == "ML Closed"
  cmd = "NewML" if /\ANew ML by / =~ cmd

  if /\A\[([^:\]]+)/ =~ group
    group = $1
  else
    fatal_error "what?"+line
  end

  return date, group, cmd, user
end

def main
  open("quickml1a.log", "wb") {|out1|
  open("quickml1.log", "wb") {|out|
    open("quickml0.log") {|f|

      bad_log_group = nil

      while line = f.gets

	if bad_log_group
	  group = get_group(line)
	  if /: Inactive$/ =~ line ||
	      /: ML Closed$/ =~ line
	    next # ignore it
	  end
	  bad_log_gruop = nil
	end

	if /New ML by $/ =~ line # error line
	  bad_log_group = get_group(line)
	  fatal_error(line) if bad_log_group.nil?
	  next
	end
	bad_log_group = nil

	# remove hacker
	next if /\[youpy/ =~ line

	#if /^\[(.*?)\]: (Add|Remove|New ML|ML Closed|Send)(?:: (.*))?/ =~ msg
	# This is the condition for the lines will be used.

	if /\: Send\: 127\.0\.0\.1 / =~ line
	  line = line.sub(/\: Send\: 127\.0\.0\.1 \d+.\d+ sec.$/, ": Send: ")
	end

	if /: Add: / =~ line ||
	    /: Remove: / =~ line ||
	    /: New ML by / =~ line ||
	    /: ML Closed$/ =~ line ||
	    /: Send: / =~ line

	  out1.print line

	  date, group, cmd, user = parse(line)
	  out.print "#{date} #{group} #{cmd} #{user}\n"
	  next
	end

	#next

=begin
	 # remove detail
	 next if /: Command: / =~ line
	 next if /: MAIL FROM:/ =~ line
	 next if /: RCPT TO:/ =~ line
	 next if /: From:/ =~ line
	 next if /: Cc: / =~ line
	 next if /: bare From: / =~ line
	 next if /: bare Cc: / =~ line

	 # remove information
	 next if /: Too Long Line: / =~ line
	 next if /: Invalid ML Address: / =~ line
	 next if /: QwikPost: / =~ line
	 next if /: ResetError: / =~ line
	 next if /: Too Large Mail: / =~ line
	 next if /: Accept confirmation:  / =~ line
	 next if /: Alert: ML will be closed soon$/ =~ line
	 next if /: Reject: / =~ line

	 next if /^$/ =~ line

	 # remove server log
	 next if /: Sweeper started$/ =~ line
	 next if /: Sweeper runs$/ =~ line
	 next if /: Sweeper finished$/ =~ line
	 next if /: Sweeper shutdown$/ =~ line

	 next if /: Server started at / =~ line
	 next if /: Server exited / =~ line

	 next if /: Closed: localhost$/ =~ line
	 next if /: Connect: localhost$/ =~ line
	 next if /: Session finished: / =~ line
	 next if /: Connect: / =~ line
	 next if /: Closed: / =~ line
	 next if /: Unacceptable RCPT TO:/ =~ line
	 next if /: Excluded: / =~ line

	 # remove error message
	 next if %r+: /qwik/lib/qwik/+ =~ line
	 next if %r+: /usr/local+ =~ line

	 next if /: Invalid From Address: / =~ line
	 next if /: Unknown Sweep Error: / =~ line
	 next if /: Unknown Session Error: / =~ line

	 next if /: ErrorMail: / =~ line
	 next if /: AddError: / =~ line
	 next if /: Send confirmation: / =~ line
	 next if /: Forward Address: / =~ line
	 next if /\A	/ =~ line
	 next if /: rejected$/ =~ line

	 next if /: Notify: Remove / =~ line
	 next if /: Unsubscribe: / =~ line
	 next if /: Inactive$/ =~ line

	 #qp "what?", line
=end

    end
    }
  }
  }
end
main
