author | Mahlon E. Smith <mahlon@laika.com> |
Tue, 16 May 2017 15:57:53 -0700 | |
changeset 21 | 1ebc33d48c85 |
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 |
|
17 | 7 |
|
15 | 8 |
# A collection of messages for a specific archive thread. |
9 |
# |
|
10 |
# thread = Ezmlm::List::Thread.new( list, 'acgcbmbmeapgpfckcdol' ) |
|
11 |
# thread.subject #=> "Help - navigate on interface?" |
|
12 |
# thread.first.date.to_s #=> "2017-05-07T14:55:05-07:00" |
|
13 |
# |
|
14 |
#--- |
|
15 |
class Ezmlm::List::Thread |
|
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
|
16 |
# $Id$ |
15 | 17 |
include Enumerable |
18 |
||
19 |
### Instantiate a new thread of messages given |
|
20 |
### a +list+ and a +thread_id+. |
|
21 |
### |
|
22 |
def initialize( list, thread_id ) |
|
23 |
raise ArgumentError, "Unknown list object." unless list.respond_to?( :listdir ) |
|
24 |
raise ArgumentError, "Malformed Thread ID." unless thread_id =~ /^\w{20}$/ |
|
17 | 25 |
raise "Archiving is not enabled." unless list.archived? |
15 | 26 |
|
27 |
@list = list |
|
28 |
@id = thread_id |
|
29 |
@subject = nil |
|
30 |
@messages = nil |
|
31 |
||
32 |
self.load_thread |
|
33 |
end |
|
34 |
||
35 |
||
36 |
# The list object this message is stored in. |
|
37 |
attr_reader :list |
|
38 |
||
39 |
# The thread's identifier. |
|
40 |
attr_reader :id |
|
41 |
||
42 |
# The subject line of the thread. |
|
43 |
attr_reader :subject |
|
44 |
||
45 |
# An array of member messages. |
|
46 |
attr_reader :messages |
|
47 |
||
48 |
# An array of member authors. |
|
49 |
attr_reader :authors |
|
50 |
||
51 |
||
52 |
### Enumerable API: Lazy load each message ID as a |
|
53 |
### Ezmlm::List::Message, yielding it to the block. |
|
54 |
### |
|
55 |
def each |
|
56 |
self.load_thread # refresh for any thread updates since object was created |
|
57 |
self.messages.each do |id| |
|
58 |
yield Ezmlm::List::Message.new( self.list, id ) |
|
59 |
end |
|
60 |
end |
|
17 | 61 |
alias_method :each_message, :each |
62 |
||
63 |
||
64 |
### Lazy load each author ID as a Ezmlm::List::Author, yielding it |
|
65 |
### to the block. |
|
66 |
### |
|
67 |
def each_author |
|
68 |
self.load_thread # refresh for any thread updates since object was created |
|
69 |
self.authors.each do |id| |
|
70 |
yield Ezmlm::List::Author.new( self.list, id ) |
|
71 |
end |
|
72 |
end |
|
15 | 73 |
|
74 |
||
75 |
######### |
|
76 |
protected |
|
77 |
######### |
|
78 |
||
79 |
### Parse the subject index into an array of Messages. |
|
80 |
### |
|
81 |
def load_thread |
|
82 |
@messages = [] |
|
83 |
@authors = [] |
|
84 |
path = self.thread_path |
|
85 |
raise "Unknown thread: %p" % [ self.id ] unless path.exist? |
|
86 |
||
17 | 87 |
path.open( 'r', encoding: Encoding::ISO8859_1 ) do |fh| |
88 |
fh.each_line.with_index do |line, i| |
|
89 |
if i.zero? |
|
90 |
@subject = line.match( /^\w+ (.+)/ )[1] |
|
91 |
else |
|
92 |
match = line.match( /^(\d+):\d+:(\w+) / ) or next |
|
93 |
self.messages << match[1].to_i |
|
94 |
self.authors << match[2] |
|
95 |
end |
|
15 | 96 |
end |
97 |
end |
|
98 |
end |
|
99 |
||
100 |
||
101 |
### Return the path on disk for the thread index. |
|
102 |
### |
|
103 |
def thread_path |
|
104 |
prefix = self.id[ 0 .. 1 ] |
|
105 |
hash = self.id[ 2 .. -1 ] |
|
106 |
return self.list.listdir + 'archive' + 'subjects' + prefix + hash |
|
107 |
end |
|
108 |
||
109 |
end # class Ezmlm::List::Thread |