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