lib/ezmlm/list.rb
changeset 4 8c4ae0797d5f
parent 2 7b5a0131d5cd
child 5 804e1c2b9a40
--- a/lib/ezmlm/list.rb	Thu May 08 21:32:18 2008 +0000
+++ b/lib/ezmlm/list.rb	Fri May 09 17:51:36 2008 +0000
@@ -20,6 +20,7 @@
 
 require 'pathname'
 require 'ezmlm'
+require 'tmail'
 
 
 ### A Ruby interface to an ezmlm-idx mailing list directory
@@ -41,6 +42,28 @@
 	attr_reader :listdir
 
 
+	### Return the number of messages in the list archive
+	def message_count
+		numfile = self.listdir + 'num'
+		return 0 unless numfile.exist?
+		return Integer( numfile.read[/^(\d+):/, 1] )
+	end
+
+
+	### Return the Date parsed from the last post to the list.
+	def last_message_date
+		mail = self.last_post or return nil
+		return mail.date
+	end
+
+
+	### Return the author of the last post to the list.
+	def last_message_author
+		mail = self.last_post or return nil
+		return mail.from
+	end
+	
+
 	### Return the email address of the list's owner.
 	def owner
 		config = self.listdir + 'config'
@@ -110,6 +133,30 @@
 	end
 	
 	
+	### Return a TMail::Mail object loaded from the last post to the list. Returns
+	### +nil+ if there are no archived posts.
+	def last_post
+		archivedir = self.listdir + 'archive'
+		return nil unless archivedir.exist?
+
+		# Find the last numbered directory under the archive dir
+		last_archdir = Pathname.glob( archivedir + '[0-9]*' ).
+			sort_by {|pn| Integer(pn.basename.to_s) }.last
+
+		return nil unless last_archdir
+
+		# Find the last numbered file under the last numbered directory we found
+		# above.
+		last_post_path = Pathname.glob( last_archdir + '[0-9]*' ).
+			sort_by {|pn| Integer(pn.basename.to_s) }.last
+
+		raise RuntimeError, "unexpectedly empty archive directory '%s'" % [ last_archdir ] \
+			unless last_post_path
+
+		last_post = TMail::Mail.load( last_post_path.to_s )
+	end
+	
+	
 
 	#########
 	protected