|
| 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