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