#-- # rbmh/shower.rb : rbmhshow 0.4.2 # # Copyright (C) 2004--2005 Merlin Hughes # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA # #++ # Show a message require 'rbmh/ansi' require 'rbmh/header' require 'merlin/each' # TODO: suppress the toc and scissors when there's one body? # TODO: Maybe a view template, per mhl.format module RBMH class Shower def initialize(context) @context = context profile_init # @headers, @color* end def show(message, sink) headers(message, sink) xheaders(message, sink) toc(message, sink) message.show(sink) end def rshow(message, sink) # show for reply... xheaders(message, sink) message.show(sink) end def toc(part, sink) section = part.section section = section ? "#{section}\t" : "" if part.multipart? sink.puts @color_part + section + @uncolor + @color_type + part.type + @uncolor part.each_part { |subpart| toc(subpart, sink) } else name = part.filename sink.puts @color_part + section + @uncolor + @color_name + name + @uncolor + " (" + @color_type + part.type + @uncolor + ")" end end private def profile_init @headers = profile_split(ProfileMailHeaders) @color_date = RBMH::ANSI.color(*profile_split(ProfileColorDate)) @color_to = RBMH::ANSI.color(*profile_split(ProfileColorTo)) @color_cc = RBMH::ANSI.color(*profile_split(ProfileColorCc)) @color_from = RBMH::ANSI.color(*profile_split(ProfileColorFrom)) @color_subject = RBMH::ANSI.color(*profile_split(ProfileColorSubject)) @color_header = RBMH::ANSI.color(*profile_split(ProfileColorHeader)) @color_name = RBMH::ANSI.color(*profile_split(ProfileColorName)) @color_type = RBMH::ANSI.color(*profile_split(ProfileColorType)) @color_part = RBMH::ANSI.color(*profile_split(ProfileColorPart)) @uncolor = RBMH::ANSI.uncolor end def profile_split(key) value = @context.profile[key] value ? value.split(' ') : [] end def headers(part, sink) header("Date: ".ljust(9), @color_date, part.header.date.to_s, sink) if part.header.date header("To: ".ljust(9), @color_to, part.header.to.collect { |address| RBMH::Header.nationalize(address.to_str) }, sink) header("Cc: ".ljust(9), @color_cc, part.header.cc.collect { |address| RBMH::Header.nationalize(address.to_str) }, sink) header("From: ".ljust(9), @color_from, part.header.from.collect { |address| RBMH::Header.nationalize(address.to_str) }, sink) header("Subject: ".ljust(9), @color_subject, RBMH::Header.nationalize(part.header.subject), sink) if part.header.subject sink.puts end def xheaders(part, sink) extra = false if 'full' == ENV['RBMH_HEADERS'] part.header.each { |name, value| name = name + ": " unless name.empty? extra |= header(name, @color_header, value, sink) } else @headers.each { |header| part.header.select(header).each { |name, value| extra |= header(name + ": ", @color_header, value, sink) } } end sink.puts if extra end def header(name, color, values, sink) (values.is_a?(Array) ? values : [values]).each_with_status { |value, status| prefix = status.first? ? name : ' ' * name.length suffix = status.last? ? '' : ',' sink.puts prefix + color + value + @uncolor + suffix }.length > 0 end ProfileMailHeaders = 'rbmh-mail-headers' ProfileColorDate = 'rbmh-color-date' ProfileColorTo = 'rbmh-color-to' ProfileColorCc = 'rbmh-color-cc' ProfileColorFrom = 'rbmh-color-from' ProfileColorSubject = 'rbmh-color-subject' ProfileColorHeader = 'rbmh-color-header' ProfileColorName = 'rbmh-color-name' ProfileColorType = 'rbmh-color-type' ProfileColorPart = 'rbmh-color-part' end # class Show end # module RBMH