summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--lib/feed2imap/maildir.rb24
-rw-r--r--test/maildir/cur/1376317520.15784_1.debian:2,S11
-rw-r--r--test/maildir/cur/1376319137.17850_1.debian:2,11
-rw-r--r--test/maildir/cur/1376320022.18396_5.debian:2,FS11
-rw-r--r--test/maildir/new/1376320099.18396_7.debian11
-rw-r--r--test/tc_maildir.rb93
6 files changed, 156 insertions, 5 deletions
diff --git a/lib/feed2imap/maildir.rb b/lib/feed2imap/maildir.rb
index eb7c8a2..8a5ade1 100644
--- a/lib/feed2imap/maildir.rb
+++ b/lib/feed2imap/maildir.rb
@@ -19,6 +19,8 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
require 'uri'
require 'fileutils'
require 'fcntl'
+require 'rmail'
+require 'socket'
class MaildirAccount
MYHOSTNAME = Socket.gethostname
@@ -67,9 +69,10 @@ class MaildirAccount
next if (not flags.index('S') or
flags.index('F') or
mtime > recent_time)
- File.open(fn) do |f|
- mail = RMail::Parser.read(f)
+ mail = File.open(fn) do |f|
+ RMail::Parser.read(f)
end
+ subject = mail.header['Subject']
if dryrun
puts "To remove: #{subject} #{mtime}"
else
@@ -141,9 +144,10 @@ class MaildirAccount
Dir[File.join(subdir, '*')].each do |fn|
File.open(fn) do |f|
mail = RMail::Parser.read(f)
- cache_index = mail.header['Message-Id']
- next if not (cache_index and cache_index == idx)
- dir_paths.push(File.join(d, File.basename(fn)))
+ cache_index = mail.header['Message-ID']
+ if cache_index && (cache_index == idx || cache_index == "<#{idx}>")
+ dir_paths.push(File.join(d, File.basename(fn)))
+ end
end
end
end
@@ -171,5 +175,15 @@ class MaildirAccount
@@seq_num += 1
fn
end
+
+ def maildir_file_info_flags(fn)
+ parts = fn.split(',')
+ if parts.size == 1
+ ''
+ else
+ parts.last
+ end
+ end
+
end
diff --git a/test/maildir/cur/1376317520.15784_1.debian:2,S b/test/maildir/cur/1376317520.15784_1.debian:2,S
new file mode 100644
index 0000000..fc6aaab
--- /dev/null
+++ b/test/maildir/cur/1376317520.15784_1.debian:2,S
@@ -0,0 +1,11 @@
+Date: Mon, 12 Aug 2013 16:25:20 +0200
+From: Antonio Terceiro <terceiro@debian.org>
+To: terceiro@debian.org
+Subject: UTF-8 data: =?iso-8859-1?B?4ent8/o=?=
+Message-ID: <regular-message-id@debian.org>
+MIME-Version: 1.0
+Content-Type: text/plain; charset=us-ascii
+Content-Disposition: inline
+User-Agent: Mutt/1.5.21 (2010-09-15)
+
+This is a sample email
diff --git a/test/maildir/cur/1376319137.17850_1.debian:2, b/test/maildir/cur/1376319137.17850_1.debian:2,
new file mode 100644
index 0000000..0bff46a
--- /dev/null
+++ b/test/maildir/cur/1376319137.17850_1.debian:2,
@@ -0,0 +1,11 @@
+Date: Mon, 12 Aug 2013 16:52:17 +0200
+From: Antonio Terceiro <terceiro@debian.org>
+To: terceiro@debian.org
+Subject: an unread message
+Message-ID: <unread-message-id@debian.org>
+MIME-Version: 1.0
+Content-Type: text/plain; charset=us-ascii
+Content-Disposition: inline
+User-Agent: Mutt/1.5.21 (2010-09-15)
+
+This message was not read yet
diff --git a/test/maildir/cur/1376320022.18396_5.debian:2,FS b/test/maildir/cur/1376320022.18396_5.debian:2,FS
new file mode 100644
index 0000000..2547416
--- /dev/null
+++ b/test/maildir/cur/1376320022.18396_5.debian:2,FS
@@ -0,0 +1,11 @@
+Date: Mon, 12 Aug 2013 17:07:02 +0200
+From: Antonio Terceiro <terceiro@debian.org>
+To: terceiro@debian.org
+Subject: a flagged message
+Message-ID: <flagged-message-id@debian.org>
+MIME-Version: 1.0
+Content-Type: text/plain; charset=us-ascii
+Content-Disposition: inline
+User-Agent: Mutt/1.5.21 (2010-09-15)
+
+This message is flagged.
diff --git a/test/maildir/new/1376320099.18396_7.debian b/test/maildir/new/1376320099.18396_7.debian
new file mode 100644
index 0000000..ba54ddd
--- /dev/null
+++ b/test/maildir/new/1376320099.18396_7.debian
@@ -0,0 +1,11 @@
+Date: Mon, 12 Aug 2013 17:08:19 +0200
+From: Antonio Terceiro <terceiro@debian.org>
+To: terceiro@debian.org
+Subject: a new message
+Message-ID: <new-message-id@debian.org>
+MIME-Version: 1.0
+Content-Type: text/plain; charset=us-ascii
+Content-Disposition: inline
+User-Agent: Mutt/1.5.21 (2010-09-15)
+
+This message is new
diff --git a/test/tc_maildir.rb b/test/tc_maildir.rb
new file mode 100644
index 0000000..343e5f7
--- /dev/null
+++ b/test/tc_maildir.rb
@@ -0,0 +1,93 @@
+require 'test/unit'
+require 'fileutils'
+require 'tmpdir'
+require 'mocha/setup'
+
+require 'feed2imap/maildir'
+
+class TestMaildir < Test::Unit::TestCase
+
+ def setup
+ @tmpdirs = []
+ end
+
+ def tear_down
+ @tmpdirs.each do |dir|
+ FileUtils.rm_rf(dir)
+ end
+ end
+
+ def test_cleanup
+ folder = create_maildir
+ msgs = message_count(folder)
+
+ maildir_account.cleanup(folder)
+
+ assert_equal msgs - 1, message_count(folder)
+ end
+
+ def test_putmail
+ folder = create_maildir
+ msgs = message_count(folder)
+
+ mail = RMail::Message.new
+ mail.header['Subject'] = 'a message I just created'
+ mail.body = 'to test maildir'
+ maildir_account.putmail(folder, mail)
+
+ assert_equal msgs + 1, message_count(folder)
+ end
+
+ def test_updatemail
+ folder = create_maildir
+ path = maildir_account.send(
+ :find_mails,
+ folder,
+ 'regular-message-id@debian.org'
+ ).first
+ assert_not_nil path
+ mail = RMail::Message.new
+ mail.header['Subject'] = 'a different subject'
+ mail.header['Message-ID'] = 'regular-message-id@debian.org'
+ mail.body = 'This is the body of the message'
+ maildir_account.updatemail(folder, mail, 'regular-message-id@debian.org')
+
+ updated_path = maildir_account.send(
+ :find_mails,
+ folder,
+ 'regular-message-id@debian.org'
+ ).first
+ updated_mail = RMail::Parser.read(File.open(File.join(folder, updated_path)))
+
+ assert_equal 'a different subject', updated_mail.header['Subject']
+ end
+
+ def test_find_mails
+ folder = create_maildir
+ assert_equal 0, maildir_account.send(:find_mails, folder, 'SomeRandomMessageID').size
+ end
+
+ private
+
+ def create_maildir
+ parent = Dir.mktmpdir
+ @tmpdirs << parent
+ FileUtils.cp_r('test/maildir', parent)
+ return File.join(parent, 'maildir')
+ end
+
+ def message_count(folder)
+ Dir.glob(File.join(folder, '**', '*')).reject { |f| File.directory?(f) }.size
+ end
+
+ def maildir_account
+ @maildir_account ||=
+ begin
+ MaildirAccount.new.tap do |account|
+ account.stubs(:puts)
+ end
+ end
+ end
+
+end
+