Skip to content

Commit d23611b

Browse files
committed
Merge branch 'feature/example-import-from-thunderbird'
2 parents 5db1faa + bd2dbd6 commit d23611b

File tree

3 files changed

+105
-2
lines changed

3 files changed

+105
-2
lines changed

contrib/README.md

+13-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
This directory contains contributed scripts that relate to
44
imap-backup
55

6-
# import-from-csv
6+
# import-accounts-from-csv
77

88
This script reads a CSV file and merges the supplied
99
information into an imap-backup configuration file.
@@ -19,5 +19,16 @@ An example CSV file `contrib/example_users.csv` is provided.
1919
You can try out the script as follows:
2020

2121
```sh
22-
contrib/import-from-csv --csv contrib/example_users.csv --config example-config.json --verbose
22+
contrib/import-accounts-from-csv --csv contrib/example_users.csv --config example-config.json --verbose
23+
```
24+
25+
# import-messages-from-thunderbird
26+
27+
This script imports all messages from a Thunderbird folder.
28+
29+
Obviously, Thunderbird must be installed and the folder in question must
30+
have the Thunderbird setting "Select this folder for offline use".
31+
32+
```sh
33+
contrib/import-thunderbird-folder --config example-config.json --verbose
2334
```
File renamed without changes.

contrib/import-thunderbird-folder

+92
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
#!/usr/bin/env ruby
2+
3+
# This script is an example of how to import messages from a Thunderbird
4+
# folder into imap-backup. It is not meant to be a general-purpose
5+
# Thunderbird importer, but rather a starting point for writing your own.
6+
# Please adapt it to your specific needs.
7+
8+
require "bundler/inline"
9+
10+
gemfile do
11+
source "https://rubygems.org"
12+
13+
gem "imap-backup"
14+
gem "optparse"
15+
gem "thunderbird", "~> 0.5.0"
16+
end
17+
18+
require "imap/backup/logger"
19+
require "imap/backup/configuration"
20+
require "imap/backup/serializer"
21+
require "thunderbird/mbox"
22+
23+
class Options
24+
attr_accessor :email
25+
attr_accessor :config_path
26+
attr_accessor :folder
27+
attr_accessor :mbox_path
28+
attr_accessor :verbose
29+
attr_accessor :quiet
30+
31+
def parse!
32+
OptionParser.new do |opts|
33+
opts.banner = <<~BANNER
34+
Usage: #{$PROGRAM_NAME} [options]"
35+
36+
Import email messages from a Thunderbird folder into imap-backup.
37+
38+
BANNER
39+
40+
opts.on("--config=CONFIG", "The path to an existing (or new) imap-backup config file") do |v|
41+
self.config_path = v
42+
end
43+
opts.on("--email=EMAIL", "The email address configured in imap-backup") do |v|
44+
self.email = v
45+
end
46+
opts.on("--folder=FOLDER", "The folder name to import into") do |v|
47+
self.folder = v
48+
end
49+
opts.on("--mbox=MBOX_PATH", "The path to a Thunderbird folder") do |v|
50+
self.mbox_path = v
51+
end
52+
opts.on("-q", "--quiet", "Do not print any output") do
53+
self.quiet = true
54+
end
55+
opts.on("-v", "--[no-]verbose", "Run verbosely") do |v|
56+
self.verbose = v
57+
end
58+
end.parse!
59+
60+
raise "Please supply a --config PATH option" if !config_path
61+
raise "Please supply a --email EMAIL option" if !email
62+
raise "Please supply a --folder FOLDER option" if !folder
63+
raise "Please supply a --mbox PATH option" if !mbox_path
64+
end
65+
66+
def for_logging
67+
{verbose: [verbose], quiet: quiet}
68+
end
69+
end
70+
71+
options = Options.new.tap(&:parse!)
72+
73+
Imap::Backup::Logger.setup_logging(options.for_logging)
74+
75+
config = Imap::Backup::Configuration.new(path: options.config_path)
76+
77+
account = config.accounts.find { |a| a.username == options.email }
78+
raise "No account found for email address '#{options.email}'" if account.nil?
79+
80+
mbox = Thunderbird::Mbox.new(path: options.mbox_path)
81+
82+
serializer = Imap::Backup::Serializer.new(account.local_path, options.folder)
83+
serializer.force_uid_validity(mbox.uid_validity)
84+
85+
mbox.each do |id, message|
86+
uid = id.to_i
87+
next if serializer.uids.include?(uid)
88+
89+
# Remove Thunderbird mbox "From" line
90+
message.sub!(/^From[\s\r\n]*/m, "")
91+
serializer.append(id, message, [])
92+
end

0 commit comments

Comments
 (0)