lib/ezmlm/list/author.rb
changeset 17 23c7f5c8ee39
parent 15 a38e6916504c
child 20 9d59d30685cb
equal deleted inserted replaced
16:e135ccae6783 17:23c7f5c8ee39
     1 #!/usr/bin/ruby
     1 #!/usr/bin/ruby
     2 # vim: set nosta noet ts=4 sw=4:
     2 # vim: set nosta noet ts=4 sw=4:
     3 #
     3 
       
     4 
     4 # A collection of messages authored from a unique user.
     5 # A collection of messages authored from a unique user.
     5 #
     6 #
     6 # Note that Ezmlm uses the "real name" part of an address
     7 # Note that Ezmlm uses the "real name" part of an address
     7 # to identify an author.
     8 # to identify an author.
     8 #
     9 #
    25 ###
    26 ###
    26 class Ezmlm::List::Author
    27 class Ezmlm::List::Author
    27 	include Enumerable
    28 	include Enumerable
    28 
    29 
    29 	### Instantiate a new list of messages given
    30 	### Instantiate a new list of messages given
    30 	### a +list+ and a +author_id+.
    31 	### a +list+ and an +author_id+.
    31 	###
    32 	###
    32 	def initialize( list, author_id )
    33 	def initialize( list, author_id )
    33 		raise ArgumentError, "Unknown list object." unless list.respond_to?( :listdir )
    34 		raise ArgumentError, "Unknown list object." unless list.respond_to?( :listdir )
    34 		raise ArgumentError, "Malformed Author ID." unless author_id =~ /^\w{20}$/
    35 		raise ArgumentError, "Malformed Author ID." unless author_id =~ /^\w{20}$/
    35 		raise "Thread indexing is not enabled." unless list.threaded?
    36 		raise "Archiving is not enabled." unless list.archived?
    36 
    37 
    37 		@list     = list
    38 		@list     = list
    38 		@id       = author_id
    39 		@id       = author_id
    39 		@messages = nil
    40 		@messages = nil
    40 
    41 
    65 		self.load_index # refresh for any updates since object was created
    66 		self.load_index # refresh for any updates since object was created
    66 		self.messages.each do |id|
    67 		self.messages.each do |id|
    67 			yield Ezmlm::List::Message.new( self.list, id )
    68 			yield Ezmlm::List::Message.new( self.list, id )
    68 		end
    69 		end
    69 	end
    70 	end
       
    71 	alias_method :each_message, :each
    70 
    72 
    71 
    73 
    72 	### Lazy load each thread ID as a Ezmlm::List::Thread, yielding it to the block.
    74 	### Lazy load each thread ID as a Ezmlm::List::Thread, yielding it to the block.
    73 	###
    75 	###
    74 	def each_thread
    76 	def each_thread
    90 		@threads  = []
    92 		@threads  = []
    91 
    93 
    92 		path = self.author_path
    94 		path = self.author_path
    93 		raise "Unknown author: %p" % [ self.id ] unless path.exist?
    95 		raise "Unknown author: %p" % [ self.id ] unless path.exist?
    94 
    96 
    95 		path.each_line.with_index do |line, i|
    97 		path.open( 'r', encoding: Encoding::ISO8859_1 ) do |fh|
    96 			if i.zero?
    98 			fh.each_line.with_index do |line, i|
    97 				@name = line.match( /^\w+ (.+)/ )[1]
    99 				if i.zero?
    98 			else
   100 					@name = line.match( /^\w+ (.+)/ )[1]
    99 				match = line.match( /^(\d+):\d+:(\w+) / ) or next
   101 				else
   100 				self.messages << match[1].to_i
   102 					match = line.match( /^(\d+):\d+:(\w+) / ) or next
   101 				self.threads  << match[2]
   103 					self.messages << match[1].to_i
       
   104 					self.threads  << match[2]
       
   105 				end
   102 			end
   106 			end
   103 		end
   107 		end
   104 	end
   108 	end
   105 
   109 
   106 
   110