mbox_merge 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/mbox_merge.rb +86 -0
  3. metadata +73 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: e39d77dd7c066dd650439c52f9ca3e6cd122749fa76c9822b8b2b10f9a37c606
4
+ data.tar.gz: a7e4809e4675e7fc01222f96d81c5a979b2db0896cbd7fad6ee9107b55b14637
5
+ SHA512:
6
+ metadata.gz: 7aaa43cb6e3d643fb3b2850c04cbc3f9a00b20f68cc19bd855c196b6def23d1e81e10d9b80cafc34b9cf94efaeaa8b30b25cb6eb30f0260526724a79d59ba025
7
+ data.tar.gz: 0a8551999f6e85f39c1165153905cc6e2a5f4c12589b82c5f700d52504b814875d15376383c21181f2adc6684adf74f77510942f6c07c09ca6570bdd534732b8
data/lib/mbox_merge.rb ADDED
@@ -0,0 +1,86 @@
1
+ require 'mail'
2
+ require 'ruby-progressbar'
3
+
4
+ module MboxMerge
5
+ class Merger
6
+ # Merges multiple mbox files into a single mbox file, sorting emails by date
7
+ # and presenting statistics of merged emails.
8
+ #
9
+ # @param output_mbox [String] Path to the output mbox file
10
+ # @param mbox_files [Array<String>] List of mbox files to be merged
11
+ # @return [Statistics] Statistics object containing information about the merging
12
+ def self.merge_mboxes(output_mbox, *mbox_files)
13
+ emails = []
14
+
15
+ # Load emails from each mbox file
16
+ mbox_files.each do |mbox_file|
17
+ File.open(mbox_file, 'r') do |f|
18
+ # Split the file into chunks using the standard mbox 'From ' separator
19
+ f.read.split(/^From /).each do |raw_message|
20
+ begin
21
+ # Only try to read valid messages (non-empty chunks)
22
+ unless raw_message.strip.empty?
23
+ # Prepend the 'From ' as it's removed by the split
24
+ email = Mail.read_from_string("From #{raw_message}")
25
+ emails << email
26
+ end
27
+ rescue => e
28
+ puts "Failed to parse email: #{e.message}"
29
+ end
30
+ end
31
+ end
32
+ end
33
+
34
+ # Calculate the total emails
35
+ total_emails = emails.size
36
+
37
+ # Sort emails by date (convert dates to Time objects for consistent comparison)
38
+ sorted_emails = emails.sort_by do |email|
39
+ begin
40
+ email.date.to_time
41
+ rescue
42
+ Time.now # emails without a valid date (TODO)
43
+ end
44
+ end
45
+
46
+ # Progress bar for merging
47
+ progress_bar = ProgressBar.create(title: "Merging", total: sorted_emails.size, format: '%t: |%B| %p%%')
48
+
49
+ # Write the sorted emails to the output mbox file
50
+ File.open(output_mbox, 'w') do |out|
51
+ sorted_emails.each do |email|
52
+ # Ensure email date is present; fallback to Time.now if nil
53
+ email_date = email.date ? email.date.to_time.strftime("%a, %d %b %Y %H:%M:%S %z") : Time.now.strftime("%a, %d %b %Y %H:%M:%S %z")
54
+ out.puts "From #{email.envelope_from} #{email_date}"
55
+ out.puts email.to_s
56
+ progress_bar.increment
57
+ end
58
+ end
59
+
60
+ # Return statistics
61
+ Statistics.new(mbox_files.size, total_emails)
62
+ end
63
+ end
64
+
65
+ class Statistics
66
+ attr_reader :file_count, :email_count
67
+
68
+ # Initializes the statistics for the merge process
69
+ #
70
+ # @param file_count [Integer] The number of mbox files merged
71
+ # @param email_count [Integer] The total number of emails merged
72
+ def initialize(file_count, email_count)
73
+ @file_count = file_count
74
+ @email_count = email_count
75
+ end
76
+
77
+ # Prints the statistics to the console
78
+ #
79
+ # @return [void]
80
+ def print_statistics
81
+ puts "Merging mbox files completed!"
82
+ puts "Total Mbox Files: #{@file_count}"
83
+ puts "Total Emails Merged: #{@email_count}"
84
+ end
85
+ end
86
+ end
metadata ADDED
@@ -0,0 +1,73 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mbox_merge
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - firefly-cpp
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2024-11-12 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: mail
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.7'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.7'
27
+ - !ruby/object:Gem::Dependency
28
+ name: ruby-progressbar
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.11'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.11'
41
+ description: Merges multiple mbox files, orders emails by date, and provides statistics
42
+ on the merged emails.
43
+ email:
44
+ - iztok@iztok-jr-fister.eu
45
+ executables: []
46
+ extensions: []
47
+ extra_rdoc_files: []
48
+ files:
49
+ - lib/mbox_merge.rb
50
+ homepage: https://codeberg.org/firefly-cpp/mbox_merge
51
+ licenses:
52
+ - MIT
53
+ metadata: {}
54
+ post_install_message:
55
+ rdoc_options: []
56
+ require_paths:
57
+ - lib
58
+ required_ruby_version: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: '0'
63
+ required_rubygems_version: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: '0'
68
+ requirements: []
69
+ rubygems_version: 3.5.16
70
+ signing_key:
71
+ specification_version: 4
72
+ summary: A tool to merge multiple mbox files and order emails by date.
73
+ test_files: []