#
# a program to read the XMail-Format or the mbox-Format (many XMail-behind)
#
# Copyright (c) 1999 by Michael Neumann
#


#
# changes the String str, to make it HTML conform
#

def to_html(str)
	str.gsub!("<","&lt;")
	str.gsub!(">","&gt;")
	str.gsub!("\"","&quot;")
	str
end


$counter = 1

class MailInfo
	attr :number,TRUE	# nur für die HTML-page creation
	attr :firstline,TRUE
	attr :info_dict,TRUE
	attr :mail_body,TRUE

        def get_MessageID
                if info_dict["Message-ID"] == nil then retun nil end
                info_dict["Message-ID"][0] =~ /^<([^\>]*)>/
                return $~[1]
	end
        def get_From; info_dict["From"][0] end
        def get_Date; info_dict["Date"][0] end
        def get_Subject; info_dict["Subject"][0] end
        def get_To; info_dict["To"][0] end
	def get_InReplyTo
                a = info_dict["In-Reply-To"]
		if a == nil 
			return nil
		else
                        if a[0] =~ /^<([^\>]*)>/ then
                                return $~[1]
                        else
                                return nil
                        end
		end
	end
end

# returns the next mail from a mbox-format file
# returns a list [lastmail?, mail-info-class]
$line = 0
def next_mail(f)
	actual = ""
	d = {}
	
        # skip's empty (\n) lines
	loop {
                if f.eof then
                        return [false]
                else
                        $a = f.readline
                        $line += 1
		end
		if $a != "\n" then break end
	}


	mi = MailInfo.new
	mi.number = $counter
	$counter += 1
        mi.firstline = $a
        while true
		a = f.readline
                $line += 1

                if a =~ /^\s+(.*)$/ then
			d[actual][d[actual].size-1] += $/ + $~[1]
		elsif a =~ /^$/ then
			break
		else
                	
			if (a =~ /^([\-\w]+): (.*)$/) then
				if d[$~[1]] == nil then
					d[$~[1]] = [$~[2]]
				else
					d[$~[1]] << $~[2]
				end
				actual = $~[1]
			end
                end
	end

	mi.info_dict = d
	mi.mail_body = []

        #p $line
        #if d["Lines"] == nil then
        #        readline
        #end

	if d["Lines"][0].to_i > 0 then
		for i in 1..(d["Lines"][0].to_i) do mi.mail_body << f.readline end
	end
	return [true,mi]
end


def get_all_mails(file)
	f = open(file,"r")
	mails = []
	loop {
		a = next_mail(f)
		if ! a[0] then break end
                mails << a[1]
	}
	return mails
	f.close
end



# returns from the email-Date e.g. "Wed, 23 Jun 1999 12:51:32 +0200" only "23 Jun"
def extract_subdate(str)
	str =~ /,\s*([^\s]*\s*[^\s]*)/
	return $~[1]
end



# returns a list of mails with replies to mail "mail"
# and deltetes this message from the $m-array
def get_sub_threads(mail)
	x = []
        m_bak = $m.clone
	$m.each {|i|
                a = i.get_InReplyTo
		
                if mail == nil then
                        if a == nil then
                                x << i
                                m_bak.delete(i)
                        end
                else
                        if a == mail.get_MessageID then
                                x << i
                                m_bak.delete(i)
                        end
		end
        }
        $m = m_bak
        return x
end


#
# creates from email "email" an html pendant
#
def create_email_in_html(email,thread_list)	
	i = email
	f = open("#{i.number}.html","w+")
	f << "<html><body>"
	line = "<a href=\"index.html\">Index</a>"
	if i.number == $counter-1 then
		line += " Next"
	else
		line += " <a href=\"#{i.number+1}.html\">Next</a>"
	end	
	if i.number == 1 then
		line += " Prev"
	else
		line += " <a href=\"#{i.number-1}.html\">Prev</a>"
	end

	#if $proceed_subs_level <= 1 or thread_list.size == 1
	#	line += " Next-Thread Prev-Thread"
	#else
	#	inx = thread_list.index(email)
	#	if inx == 0 			# first item
	#		line += " <a href=\"#{
	#	
	#	if inx == thread_list.size-1 then #last item
	#		
	#		
	#end
	

	f << line << "<hr>"
	f << "<p><b>Date: </b>#{to_html i.get_Date}<br>"
	f << "<b>To: </b>#{to_html i.get_To}<br>"
	f << "<b>Subject: </b>#{to_html i.get_Subject}<br></p><br>"
	a = "" #"<br>"		
	f << "<pre>#{i.mail_body.join(a)}</pre>"
	f << "<hr>" << line
	f << "</body></html>"
	f.close
end



def proceed_subs(arr,f)
	arr.each {|i|
		create_email_in_html(i,arr)	#wenn $proceed_subs_level > 1 ist, dann ist arr die Threads-liste

		f << "<li><a href=\"#{i.number}.html\">#{to_html i.get_Subject}]</a> (#{extract_subdate(to_html i.get_Date)}) [#{to_html i.get_From}]</li>"
		s =  get_sub_threads(i)

                if s != nil and s.empty? == false then
			f << "<ul>"
			proceed_subs(s,f)
			f << "</ul>"
		end
	}
end





$m = get_all_mails("mod_perl.mbox")
f = open("index.html","w+")


f << "<HTML><BODY><ol>"

proceed_subs(get_sub_threads(nil),f)

if not $m.empty? then
	f << "<br><br><p>ERROR: there are also some messages, where the repliant is missing!</p>"
end
f << "</ol></BODY></HTML>"
f.close
