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