aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKazuki Yamaguchi <k@rhe.jp>2017-06-08 12:48:39 +0900
committerKazuki Yamaguchi <k@rhe.jp>2017-06-08 12:48:39 +0900
commit29dc34ca3883952fc40d0e599ec16743593fe8e8 (patch)
tree92bc21e57909017af30c81b01f240aa669b3e2cc
parent353fd27a96c576f248fe54e74ce75aeaab711322 (diff)
downloadpuke-29dc34ca3883952fc40d0e599ec16743593fe8e8.tar.gz
Support importing mbox containing multiple messages
-rw-r--r--lib/puke/core.rb72
-rwxr-xr-xpuke-import2
2 files changed, 55 insertions, 19 deletions
diff --git a/lib/puke/core.rb b/lib/puke/core.rb
index 77652aa..4cbb0d5 100644
--- a/lib/puke/core.rb
+++ b/lib/puke/core.rb
@@ -57,20 +57,8 @@ module Puke
end
CreateMutex = Thread::Mutex.new
- def create(io, is_raw = false)
- t = Tempfile.open("unnamed", tmpdir)
- if is_raw
- # Is this really correct??
- t << "From mboxrd Thu Jan 1 00:00:00 1970\n"
- io.each_line do |line|
- t << line.gsub(/\A(>*)From /, "\\1>From ").gsub(/\r\n\z/, "\n")
- end
- t << "\n"
- else
- IO.copy_stream(io, t)
- end
- t.rewind
- mid, tid, subject, date = parse_file(t)
+ def create0(file)
+ mid, tid, subject, date = parse_file(file)
fpath = File.join(datadir, mangle_mid(mid) + ".mbox")
# A mutex is used here in case two threads try to store the same message
@@ -100,11 +88,61 @@ module Puke
@gdbm["t#{tid}"] = l.map { |i| i[:mid] }.join("\0")
end
@gdbm["m#{mid}"] = [tid, subject, date.rfc2822].join("\0")
- File.link(t, fpath)
+ File.link(file, fpath)
}
mid
- ensure
- t and t.close!
+ end
+
+ def create(io, is_raw = false)
+ if is_raw
+ Tempfile.create("unnamed", tmpdir) { |t|
+ t.binmode
+ # Is this really correct??
+ t << "From mboxrd Thu Jan 1 00:00:00 1970\n"
+ x = nil
+ io.each_line do |line|
+ x = line[-1] == "\n"
+ line.gsub!(/\A(>*)From /, "\\1>From ")
+ line.gsub!(/\r\n\z/, "\n")
+ t << line
+ end
+ if x
+ t << "\n\n"
+ else
+ t << "\n"
+ end
+ t.rewind
+ create0(t)
+ }
+ else
+ # io is already in mboxrd format and may contain multiple messages
+ mids = []
+ begin
+ t = nil
+ io.each_line do |line|
+ # Beginning of a new message
+ if line.start_with?("From ")
+ if t
+ t.rewind
+ mids << create0(t)
+ t.close!
+ end
+ t = Tempfile.open("unnamed", tmpdir)
+ t.binmode
+ end
+ raise "invalid mbox header" unless t
+ t << line
+ end
+ if t
+ t.rewind
+ mids << create0(t)
+ t.close!
+ end
+ ensure
+ t and not t.closed? and t.close!
+ end
+ mids
+ end
end
def parse_file(io)
diff --git a/puke-import b/puke-import
index ed362df..1a7c500 100755
--- a/puke-import
+++ b/puke-import
@@ -7,8 +7,6 @@ require "net/http"
#
# URI is the url for importing an message.
# E.g. 'http://localhost:8080/new/secret-string'
-#
-# TODO: mboxrd containing multiple emails
uri = URI.parse(ARGV.shift)
http = Net::HTTP.new(uri.host, uri.port)