This is an automated email from the ASF dual-hosted git repository.

sebb pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/whimsy.git


The following commit(s) were added to refs/heads/master by this push:
     new a627a30f Tidy up
a627a30f is described below

commit a627a30f32bb3fd842b2fec5b389eb6d416822f4
Author: Sebb <[email protected]>
AuthorDate: Fri Feb 10 00:06:36 2023 +0000

    Tidy up
---
 tools/parsemail.rb | 90 ++++++++++++++++++++++++++++--------------------------
 1 file changed, 46 insertions(+), 44 deletions(-)

diff --git a/tools/parsemail.rb b/tools/parsemail.rb
index 07552fe6..5f1c29b7 100755
--- a/tools/parsemail.rb
+++ b/tools/parsemail.rb
@@ -17,12 +17,8 @@
 
 #
 # Parse mail files and update summary YAML file
-#
-
-# DRAFT DRAFT DRAFT DRAFT
-#
-# Could perhaps be incorporated into the deliver script, once proven
 
+# Currently used by a cron job to process board and member emails
 
 $LOAD_PATH.unshift '/srv/whimsy/lib'
 require 'whimsy/asf/yaml'
@@ -31,47 +27,53 @@ require 'mail'
 
 MAIL_ROOT = '/srv/mail'
 
-list = ARGV.shift || 'board' # provide the list on the command line (e.g. 
board)
-yyyymm = ARGV.shift || Time.now.strftime('%Y%m')
-yamlfile = ARGV.shift || File.join(MAIL_ROOT, list, "#{yyyymm}.yaml") # where 
to find the YAML summary
+def parse_dir(maildir, yamlfile)
+  data = Hash.new
+  
+  begin
+    current = YamlFile.read(yamlfile)
+  rescue Errno::ENOENT
+    current = {}
+  end
+  
+  Dir.glob("#{maildir}/[0-9a-f][0-9a-f]*").each do |p|
+      name = File.basename(p)
+      unless current[name]
+          mail=Mail.read(p)
+          entry = {
+              Subject:   mail.subject,
+              Date:      (mail['Date'].decoded rescue ''), # textual
+              DateParsed: (mail.date.to_s rescue ''), # parsed
+              From:      (mail['From'].decoded rescue ''),
+              To:        (mail['To'].decoded rescue ''),
+              Cc:        (mail['Cc'].decoded rescue ''),
+              # list of destination emails
+              Emails:     [(mail[:to].addresses.map(&:to_str) rescue 
[]),(mail[:cc].addresses.map(&:to_str) rescue [])].flatten,
+              MessageId: mail.message_id, # could be nil
+              EnvelopeFrom: mail.envelope_from,
+              EnvelopeDate: mail.envelope_date.to_s, # effectively the 
delivery date to the mailing list
+          }
+          data[name] = entry
+      end
+  end
 
-maildir = File.join(MAIL_ROOT, list, yyyymm) # where to find the mail files
-
-data = Hash.new
-
-begin
-  current = YamlFile.read(yamlfile)
-rescue Errno::ENOENT
-  current = {}
+  # update the file with any new entries
+  YamlFile.update(yamlfile) do |yaml|
+      data.each do |k,v|
+          unless yaml[k] # don't update existing entries
+              yaml[k] = v
+          end
+      end
+      yaml
+  end
 end
 
-Dir.glob("#{maildir}/[0-9a-f][0-9a-f]*").each do |p|
-    name = File.basename(p)
-    unless current[name]
-        mail=Mail.read(p)
-        entry = {
-            Subject:   mail.subject,
-            Date:      (mail['Date'].decoded rescue ''), # textual
-            DateParsed: (mail.date.to_s rescue ''), # parsed
-            From:      (mail['From'].decoded rescue ''),
-            To:        (mail['To'].decoded rescue ''),
-            Cc:        (mail['Cc'].decoded rescue ''),
-            # list of destination emails
-            Emails:     [(mail[:to].addresses.map(&:to_str) rescue 
[]),(mail[:cc].addresses.map(&:to_str) rescue [])].flatten,
-            MessageId: mail.message_id, # could be nil
-            EnvelopeFrom: mail.envelope_from,
-            EnvelopeDate: mail.envelope_date.to_s, # effectively the delivery 
date to the mailing list
-        }
-        data[name] = entry
-    end
-end
+if __FILE__ == $0
+  list = ARGV.shift || 'board' # provide the list on the command line (e.g. 
board)
+  yyyymm = ARGV.shift || Time.now.strftime('%Y%m')
+  yamlfile = ARGV.shift || File.join(MAIL_ROOT, list, "#{yyyymm}.yaml") # 
where to find the YAML summary
+  
+  maildir = File.join(MAIL_ROOT, list, yyyymm) # where to find the mail files
 
-# update the file with any new entries
-YamlFile.update(yamlfile) do |yaml|
-    data.each do |k,v|
-        unless yaml[k] # don't update existing entries
-            yaml[k] = v
-        end
-    end
-    yaml
+  parse_dir(maildir, yamlfile)
 end

Reply via email to