#-- # rbmh/profile.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 # #++ # Parses your mh-profile module RBMH MHProfile = "#{ENV['HOME']}/.mh_profile" ProfilePathEntry = 'Path' ProfilePathDefault = 'Mail' ProfileUnseenSequenceEntry = 'Unseen-Sequence' class Profile # < Hash def initialize(context) @profile = {} mhprofile = MHProfile raise RBMH::Exception, "mh-profile not found" unless FileTest.file?(mhprofile) and FileTest.readable?(mhprofile) File.open(mhprofile) { | profile | @mtime = profile.stat.mtime value = "" profile.each { | line | if line =~ EntryRE key, value = $1, $2 STDERR.puts "rbmh: Duplicate mh-profile entry `#{key}'" if @profile.has_key?(key) && key =~ /^rbmh/ @profile[key] = value elsif line =~ ContinuationRE continuation = $1 value << continuation end } } @path = @profile[ProfilePathEntry] or ProfilePathDefault if @path[0] != Slash @path = "#{ENV['HOME']}/#{@path}" end if @path[-1] == Slash @path = @path[0,@path.length - 1] end @unseen_sequence = @profile[ProfileUnseenSequenceEntry] self.each(ProfileEnvRE) { |variable, value| ENV[variable] = value unless ENV.has_key?(variable) } end attr_reader :mtime, :path, :unseen_sequence def [](key) @profile[key] end def each(matcher = NullRE) @profile.each { | key, value | if key =~ matcher parameters = ($~.length == 1) ? [ key ] : $~.to_a[1..-1] yield(parameters << value) end } end def mhpath(folder, message) # support absolute paths if folder =~ /^[.\/]/ "#{folder}/#{message}" else "#{@path}/#{folder}/#{message}" end end private EntryRE = /^(\S+):\s+(.+)$/ # $1: key, $2: value ContinuationRE = /^(\s.*)$/ # $1: continuation ProfileEnvRE = /^rbmh-env-(\w+)$/ # $1: variable NullRE = // Slash = 47 end # class Profile end # module RBMH