Class: Net::IMAP::CopyUIDData

Inherits:
Object
  • Object
show all
Defined in:
lib/net/imap/uidplus_data.rb

Overview

NOTE: CopyUIDData replaced UIDPlusData for COPYUID in the 0.6.0 release. To use CopyUIDData before 0.6.0, set Config#parser_use_deprecated_uidplus_data to false.

CopyUIDData represents the ResponseCode#data that accompanies the COPYUID response code.

A server that supports UIDPLUS (or IMAP4rev2) should send CopyUIDData in response to copy, uid_copy, move, and uid_move commands—unless the destination mailbox reports UIDNOTSTICKY.

Note that copy and uid_copy return CopyUIDData in their TaggedResponse. But move and uid_move should send CopyUIDData in an UntaggedResponse response before sending their TaggedResponse. However some servers do send CopyUIDData in the TaggedResponse for MOVE commands—this complies with the older UIDPLUS specification but is discouraged by the MOVE extension and disallowed by IMAP4rev2.

Required capability

Requires either UIDPLUS [RFC4315] or IMAP4rev2 capability.

Instance Method Summary collapse

Constructor Details

#initialize(uidvalidity:, source_uids:, assigned_uids:) ⇒ CopyUIDData



77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/net/imap/uidplus_data.rb', line 77

def initialize(uidvalidity:, source_uids:, assigned_uids:)
  uidvalidity   = Integer(uidvalidity)
  source_uids   = SequenceSet[source_uids]
  assigned_uids = SequenceSet[assigned_uids]
  NumValidator.ensure_nz_number(uidvalidity)
  if source_uids.include_star? || assigned_uids.include_star?
    raise DataFormatError, "uid-set cannot contain '*'"
  elsif source_uids.count_with_duplicates != assigned_uids.count_with_duplicates
    raise DataFormatError, "mismatched uid-set sizes for %s and %s" % [
      source_uids, assigned_uids
    ]
  end
  super
end

Instance Method Details

#assigned_uid_for(source_uid) ⇒ Object Also known as: []

:call-seq:

assigned_uid_for(source_uid) -> uid
self[source_uid] -> uid

Returns the UID in the destination mailbox for the message that was copied from source_uid in the source mailbox.

This is the reverse of #source_uid_for.

Related: source_uid_for, each_uid_pair, uid_mapping



125
126
127
128
# File 'lib/net/imap/uidplus_data.rb', line 125

def assigned_uid_for(source_uid)
  idx = source_uids.find_ordered_index(source_uid) and
    assigned_uids.ordered_at(idx)
end

#each_uid_pairObject Also known as: each_pair, each

Yields a pair of UIDs for each copied message. The first is the message’s UID in the source mailbox and the second is the UID in the destination mailbox.

Returns an enumerator when no block is given.

Please note the warning on uid_mapping before calling methods like to_h or to_a on the returned enumerator.

Related: uid_mapping, assigned_uid_for, source_uid_for



155
156
157
158
159
160
161
162
# File 'lib/net/imap/uidplus_data.rb', line 155

def each_uid_pair
  return enum_for(__method__) unless block_given?
  source_uids.each_ordered_number.lazy
    .zip(assigned_uids.each_ordered_number.lazy) do
      |source_uid, assigned_uid|
      yield source_uid, assigned_uid
    end
end

#sizeObject

Returns the number of messages that have been copied or moved. source_uids and the assigned_uids will both the same number of UIDs.



111
112
113
# File 'lib/net/imap/uidplus_data.rb', line 111

def size
  assigned_uids.count_with_duplicates
end

#source_uid_for(assigned_uid) ⇒ Object

:call-seq:

source_uid_for(assigned_uid) -> uid

Returns the UID in the source mailbox for the message that was copied to assigned_uid in the source mailbox.

This is the reverse of #assigned_uid_for.

Related: assigned_uid_for, each_uid_pair, uid_mapping



140
141
142
143
# File 'lib/net/imap/uidplus_data.rb', line 140

def source_uid_for(assigned_uid)
  idx = assigned_uids.find_ordered_index(assigned_uid) and
    source_uids.ordered_at(idx)
end

#uid_mappingObject

:call-seq: uid_mapping -> hash

Returns a hash mapping each source UID to the newly assigned destination UID.

Warning: The hash that is created may consume much more memory than the data used to create it. When handling responses from an untrusted server, check #size before calling this method.

Related: each_uid_pair, assigned_uid_for, source_uid_for



176
177
178
# File 'lib/net/imap/uidplus_data.rb', line 176

def uid_mapping
  each_uid_pair.to_h
end