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