lib/ezmlm/list/message.rb
changeset 15 a38e6916504c
child 17 23c7f5c8ee39
equal deleted inserted replaced
14:cba9fb39bcdb 15:a38e6916504c
       
     1 #!/usr/bin/ruby
       
     2 # vim: set nosta noet ts=4 sw=4:
       
     3 #
       
     4 # An individual list message.
       
     5 #
       
     6 #    message = Ezmlm::List::Message.new( list, 24 )
       
     7 #    message.thread    #=> (a thread object this message is part of)
       
     8 #    message.from      #=> ["jalon.hermann@example.com"]
       
     9 #    puts message.to_s #=> (raw email)
       
    10 #
       
    11 # This class passes all heavy lifting to the Mail::Message library.
       
    12 # Please see it for specifics on usage.
       
    13 #
       
    14 # == Version
       
    15 #
       
    16 #  $Id$
       
    17 #
       
    18 #---
       
    19 
       
    20 require 'pathname'
       
    21 require 'ezmlm' unless defined?( Ezmlm )
       
    22 require 'mail'
       
    23 
       
    24 
       
    25 ### A Ruby interface to an individual list message.
       
    26 ###
       
    27 class Ezmlm::List::Message
       
    28 
       
    29 	### Instantiate a new messag from a +list+ and a +message_number+.
       
    30 	###
       
    31 	def initialize( list, message_number=0 )
       
    32 		raise ArgumentError, "Unknown list object." unless list.respond_to?( :listdir )
       
    33 		raise ArgumentError, "Invalid message number (impossible)" if message_number < 1
       
    34 		raise ArgumentError, "Invalid message number (out of list bounds)" if message_number > list.message_count
       
    35 
       
    36 		@list = list
       
    37 		@id   = message_number
       
    38 		@post = self.load_message
       
    39 	end
       
    40 
       
    41 
       
    42 	# The list object this message is stored in.
       
    43 	attr_reader :list
       
    44 
       
    45 	# The list message delivery identifier.
       
    46 	attr_reader :id
       
    47 
       
    48 	# The Mail::Message object for this post.
       
    49 	attr_reader :post
       
    50 
       
    51 
       
    52 	### Return the thread object this message is
       
    53 	### a member of.
       
    54 	###
       
    55 	def thread
       
    56 		unless @thread_id
       
    57 			idx = self.list.index
       
    58 			@thread_id = idx[ self.id - 1 ][ :thread ]
       
    59 		end
       
    60 
       
    61 		return Ezmlm::List::Thread.new( self.list, @thread_id )
       
    62 	end
       
    63 
       
    64 
       
    65 	### Return the author object this message is
       
    66 	### a member of.
       
    67 	###
       
    68 	def author
       
    69 		unless @author_id
       
    70 			idx = self.list.index
       
    71 			@author_id = idx[ self.id - 1 ][ :author ]
       
    72 		end
       
    73 
       
    74 		return Ezmlm::List::Author.new( self.list, @author_id )
       
    75 	end
       
    76 
       
    77 
       
    78 	### Render the message as a string.
       
    79 	###
       
    80 	def to_s
       
    81 		return self.post.to_s
       
    82 	end
       
    83 
       
    84 	### Provide implicit arrays (Mail::Message does not.)
       
    85 	###
       
    86 	def to_ary
       
    87 		return [ self.post ]
       
    88 	end
       
    89 
       
    90 
       
    91 	### Pass all unknown methods to the underlying Mail::Message object.
       
    92 	###
       
    93 	def method_missing( meth, *args )
       
    94 		return self.post.method( meth ).call( *args )
       
    95 	end
       
    96 
       
    97 
       
    98 	#########
       
    99 	protected
       
   100 	#########
       
   101 
       
   102 	### Parse the message into a Mail::Message.
       
   103 	###
       
   104 	def load_message
       
   105 		path = self.message_path
       
   106 		raise "Unable to determine message path: %p" % [ path ] unless path.exist?
       
   107 		return Mail.read( path.to_s )
       
   108 	end
       
   109 
       
   110 
       
   111 	### Return the path on disk for the message.
       
   112 	###
       
   113 	def message_path
       
   114 		hashdir = self.id / 100
       
   115 		message = "%02d" % [ self.id % 100 ]
       
   116 		return self.list.listdir + 'archive' + hashdir.to_s + message.to_s
       
   117 	end
       
   118 
       
   119 end # class Ezmlm::List::Message