You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@whimsical.apache.org by Sam Ruby <ru...@apache.org> on 2015/12/13 16:14:52 UTC
[whimsy.git] [8/37] Commit ce8a3b6: refactor
Commit ce8a3b66bee93b05f5c9a8dd4c1d17c4e763af40:
refactor
Branch: refs/heads/secmail
Author: Sam Ruby <ru...@intertwingly.net>
Committer: Sam Ruby <ru...@intertwingly.net>
Pusher: rubys <ru...@apache.org>
------------------------------------------------------------
mailbox.rb | ++++++++++ ---
parsemail.rb | + --------
------------------------------------------------------------
79 changes: 39 additions, 40 deletions.
------------------------------------------------------------
diff --git a/mailbox.rb b/mailbox.rb
index 9f123a9..9487449 100644
--- a/mailbox.rb
+++ b/mailbox.rb
@@ -8,13 +8,6 @@
class Mailbox
#
- # What to use as a hash for mail
- #
- def self.hash(message)
- Digest::SHA1.hexdigest(message[/^Message-ID:.*/i] || message)[0..9]
- end
-
- #
# Read a mailbox and split it into messages
#
def initialize(filename)
@@ -37,7 +30,6 @@ def initialize(filename)
@mails = mails
end
-
#
# Find a message
#
@@ -52,6 +44,42 @@ def find(hash)
def each(&block)
@mails.each(&block)
end
-end
+ #
+ # What to use as a hash for mail
+ #
+ def self.hash(message)
+ Digest::SHA1.hexdigest(message[/^Message-ID:.*/i] || message)[0..9]
+ end
+
+ #
+ # common header logic for messages and attachments
+ #
+ def self.headers(part)
+ # extract all fields from the mail (recovering from bad encoding issues)
+ fields = part.header_fields.map do |field|
+ begin
+ next [field.name, field.to_s] if field.to_s.valid_encoding?
+ rescue
+ end
+
+ if field.value and field.value.valid_encoding?
+ [field.name, field.value]
+ else
+ [field.name, field.value.inspect]
+ end
+ end
+
+ # group fields by name
+ fields = fields.group_by(&:first).map do |name, values|
+ if values.length == 1
+ [name, values.first.last]
+ else
+ [name, values.map(&:last)]
+ end
+ end
+ # return fields as a Hash
+ Hash[fields]
+ end
+end
diff --git a/parsemail.rb b/parsemail.rb
index d4e8422..1167a77 100644
--- a/parsemail.rb
+++ b/parsemail.rb
@@ -23,35 +23,6 @@
system "rsync -av --no-motd --delete --exclude='*.yml' #{SOURCE}/ #{ARCHIVE}/"
end
-# common header logic for messages and attachments
-def headers(part)
- # extract all fields from the mail (recovering from bad encoding issues)
- fields = part.header_fields.map do |field|
- begin
- next [field.name, field.to_s] if field.to_s.valid_encoding?
- rescue
- end
-
- if field.value and field.value.valid_encoding?
- [field.name, field.value]
- else
- [field.name, field.value.inspect]
- end
- end
-
- # group fields by name
- fields = fields.group_by(&:first).map do |name, values|
- if values.length == 1
- [name, values.first.last]
- else
- [name, values.map(&:last)]
- end
- end
-
- # return fields as a Hash
- Hash[fields]
-end
-
# scan each mailbox for updates
width = 0
Dir[File.join(database, '2*')].sort.each do |name|
@@ -113,7 +84,7 @@ def headers(part)
}
# add in header fields
- mbox[id].merge! headers(mail)
+ mbox[id].merge! Mailbox.headers(mail)
# add in attachments
if mail.attachments.length > 0
@@ -124,7 +95,7 @@ def headers(part)
mime: attach.mime_type
}
- description.merge(headers(attach))
+ description.merge(Mailbox.headers(attach))
end
mbox[id][:attachments] = attachments