# File lib/larch/imap/mailbox.rb, line 16
  def initialize(imap, name, delim, subscribed, *attr)
    raise ArgumentError, "must provide a Larch::IMAP instance" unless imap.is_a?(Larch::IMAP)

    @attr       = attr.flatten
    @delim      = delim
    @flags      = []
    @imap       = imap
    @last_scan  = nil
    @name       = name
    @name_utf7  = Net::IMAP.encode_utf7(@name)
    @perm_flags = []
    @subscribed = subscribed

    # Valid mailbox states are :closed (no mailbox open), :examined (mailbox
    # open and read-only), or :selected (mailbox open and read-write).
    @state = :closed

    # Create/update this mailbox in the database.
    mb_data = {
      :name       => @name,
      :delim      => @delim,
      :attr       => @attr.map{|a| a.to_s }.join(','),
      :subscribed => @subscribed ? 1 : 0
    }

    @db_mailbox = imap.db_account.mailboxes_dataset.filter(:name => @name).first

    if @db_mailbox
      @db_mailbox.update(mb_data)
    else
      @db_mailbox = Database::Mailbox.create(mb_data)
      imap.db_account.add_mailbox(@db_mailbox)
    end

    # Create private convenience methods (debug, info, warn, etc.) to make
    # logging easier.
    Logger::LEVELS.each_key do |level|
      next if Mailbox.private_method_defined?(level)

      Mailbox.class_eval do
        define_method(level) do |msg|
          Larch.log.log(level, "#{@imap.options[:log_label]} #{@name}: #{msg}")
        end

        private level
      end
    end
  end