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