imap-backup 16.4.1 → 16.4.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,123 +0,0 @@
1
- require "json"
2
-
3
- require "imap/backup/serializer/imap"
4
-
5
- module Imap; end
6
-
7
- module Imap::Backup
8
- class Serializer; end
9
-
10
- # Migrates serialized folder metadata from the version 2 format to the version 3 format
11
- class Serializer::Version2Migrator
12
- # @param folder_path [String] the base pathv(without extension) of the folder backup
13
- def initialize(folder_path)
14
- @folder_path = folder_path
15
- end
16
-
17
- def required?
18
- return false if !mbox_exists?
19
- return false if !imap_exists?
20
- return false if !imap_data
21
- return false if imap_data[:version] != 2
22
- return false if !imap_data[:uid_validity]
23
- return false if !uids.is_a?(Array)
24
-
25
- true
26
- end
27
-
28
- # Runs the migration
29
- # @return [Boolean] whether the migration was run
30
- def run
31
- return false if !required?
32
-
33
- messages = message_uids_and_lengths
34
-
35
- return false if !messages
36
-
37
- imap.delete
38
- imap.uid_validity = imap_data[:uid_validity]
39
- messages.map { |m| imap.append(m[:uid], m[:length]) }
40
-
41
- true
42
- end
43
-
44
- private
45
-
46
- attr_reader :folder_path
47
-
48
- def imap_pathname
49
- "#{folder_path}.imap"
50
- end
51
-
52
- def imap_exists?
53
- File.exist?(imap_pathname)
54
- end
55
-
56
- def mbox_pathname
57
- "#{folder_path}.mbox"
58
- end
59
-
60
- def mbox_exists?
61
- File.exist?(mbox_pathname)
62
- end
63
-
64
- def imap_data
65
- @imap_data ||=
66
- begin
67
- content = File.read(imap_pathname)
68
- JSON.parse(content, symbolize_names: true)
69
- rescue JSON::ParserError
70
- nil
71
- end
72
- end
73
-
74
- def uids
75
- imap_data[:uids]
76
- end
77
-
78
- def message_uids_and_lengths
79
- messages = []
80
-
81
- File.open(mbox_pathname, "rb") do |f|
82
- lines = []
83
-
84
- loop do
85
- line = f.gets
86
- break if !line
87
-
88
- if line.start_with?("From ")
89
- if lines.any?
90
- message = {
91
- uid: uids[messages.count],
92
- length: lines.join.length
93
- }
94
-
95
- messages << message
96
- end
97
-
98
- lines = [line]
99
- else
100
- lines << line
101
- end
102
- end
103
-
104
- next if lines.none?
105
-
106
- message = {
107
- uid: uids[messages.count],
108
- length: lines.join.length
109
- }
110
-
111
- messages << message
112
- end
113
-
114
- return nil if messages.count != uids.count
115
-
116
- messages
117
- end
118
-
119
- def imap
120
- @imap ||= Serializer::Imap.new(folder_path)
121
- end
122
- end
123
- end