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