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