imap_archiver 0.0.4 → 0.0.5
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +1 -1
- data/Gemfile.lock +9 -11
- data/VERSION +1 -1
- data/lib/imap_archiver/archiver.rb +29 -10
- data/lib/imap_archiver/cli.rb +5 -3
- data/spec/cli_spec.rb +4 -3
- data/spec/imap_archiver_spec.rb +1 -1
- data/spec/integration_spec.rb +4 -4
- metadata +4 -4
data/Gemfile
CHANGED
data/Gemfile.lock
CHANGED
@@ -30,16 +30,14 @@ GEM
|
|
30
30
|
mocha (0.9.8)
|
31
31
|
rake
|
32
32
|
rake (0.8.7)
|
33
|
-
rspec (2.0
|
34
|
-
rspec-core (~> 2.0
|
35
|
-
rspec-expectations (~> 2.0
|
36
|
-
rspec-mocks (~> 2.0
|
37
|
-
rspec-core (2.
|
38
|
-
rspec-expectations (2.0
|
39
|
-
diff-lcs (
|
40
|
-
rspec-mocks (2.0
|
41
|
-
rspec-core (~> 2.0.1)
|
42
|
-
rspec-expectations (~> 2.0.1)
|
33
|
+
rspec (2.3.0)
|
34
|
+
rspec-core (~> 2.3.0)
|
35
|
+
rspec-expectations (~> 2.3.0)
|
36
|
+
rspec-mocks (~> 2.3.0)
|
37
|
+
rspec-core (2.3.1)
|
38
|
+
rspec-expectations (2.3.0)
|
39
|
+
diff-lcs (~> 1.1.2)
|
40
|
+
rspec-mocks (2.3.0)
|
43
41
|
term-ansicolor (1.0.5)
|
44
42
|
trollop (1.16.2)
|
45
43
|
|
@@ -56,4 +54,4 @@ DEPENDENCIES
|
|
56
54
|
jeweler (~> 1.5.1)
|
57
55
|
mocha
|
58
56
|
rake (~> 0.8.7)
|
59
|
-
rspec (~> 2.
|
57
|
+
rspec (~> 2.3.0)
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.0.
|
1
|
+
0.0.5
|
@@ -1,11 +1,21 @@
|
|
1
|
+
require "logger"
|
1
2
|
require "config"
|
2
3
|
module ImapArchiver
|
3
4
|
class Archiver
|
4
5
|
include ::ImapArchiver::Config
|
5
6
|
attr_accessor :connection
|
6
|
-
def initialize(config_file)
|
7
|
+
def initialize(config_file,debug=false)
|
8
|
+
@logger = Logger.new(STDOUT)
|
9
|
+
@logger.level = Logger::WARN
|
10
|
+
if debug
|
11
|
+
@logger.level = Logger::DEBUG
|
12
|
+
@Logger.debug "Debugging enabled."
|
13
|
+
end
|
14
|
+
@logger.debug "Loading config"
|
7
15
|
self.load_config(config_file)
|
16
|
+
@logger.debug "config loaded"
|
8
17
|
config_valid?
|
18
|
+
@logger.debug "config valid"
|
9
19
|
end
|
10
20
|
|
11
21
|
def connect
|
@@ -14,18 +24,22 @@ module ImapArchiver
|
|
14
24
|
end
|
15
25
|
|
16
26
|
def reconnect
|
27
|
+
@logger.debug "reconnect"
|
17
28
|
self.connection = Net::IMAP.new(imap_server) rescue nil
|
29
|
+
@logger.debug "connected"
|
18
30
|
capability = self.connection.capability rescue nil
|
31
|
+
@logger.debug "capability: #{capability.inspect}"
|
19
32
|
if capability.detect {|c| c =~ /AUTH=(CRAM|DIGEST)-MD5/}
|
20
|
-
|
33
|
+
@logger.debug "loging in with #{auth_mech}"
|
21
34
|
self.connection.authenticate(auth_mech,username,password)
|
22
35
|
else
|
23
|
-
|
36
|
+
@logger.debug "plain login"
|
24
37
|
self.connection.login(username,password) rescue nil
|
25
38
|
end
|
26
39
|
end
|
27
40
|
|
28
41
|
def folder_list
|
42
|
+
@logger.debug "folder_list"
|
29
43
|
connect if connection.nil?
|
30
44
|
if folders_to_archive.is_a? Array
|
31
45
|
folders = folders_to_archive.map {|f| connection.list('',f)}
|
@@ -39,25 +53,29 @@ module ImapArchiver
|
|
39
53
|
end
|
40
54
|
|
41
55
|
def start
|
56
|
+
@logger.debug "start"
|
42
57
|
folder_list.each do |folder|
|
58
|
+
@logger.debug "starting folder #{folder}"
|
43
59
|
since_date = Date.today.months_ago(3).beginning_of_month
|
44
|
-
before_date= Date.today.months_ago(
|
60
|
+
before_date= Date.today.months_ago(2).beginning_of_month
|
45
61
|
archive_folder_between_dates(folder.name,since_date,before_date) #if folder.name =~ /^Public Folders\/Team\//
|
46
62
|
end
|
47
63
|
end
|
48
64
|
|
49
65
|
def archive_folder_between_dates(folder, since_date, before_date)
|
66
|
+
@logger.debug "archive_folder_between_dates #{folder}, #{since_date}, #{before_date}"
|
50
67
|
tmp_folder = Pathname.new(folder).relative_path_from(Pathname.new(base_folder)).to_s
|
51
68
|
current_archive_folder = "#{archive_folder}/#{tmp_folder}/#{since_date.strftime("%b %Y")}"
|
52
|
-
|
69
|
+
@logger.debug "archiving to #{archive_folder}"
|
53
70
|
conditions = ["SINCE", since_date.strftime("%d-%b-%Y"), "BEFORE", before_date.strftime("%d-%b-%Y"), "SEEN", "NOT", "FLAGGED"]
|
71
|
+
@logger.debug conditions.join(" ")
|
54
72
|
retry_count = 0
|
55
73
|
begin
|
56
74
|
connection.select(folder)
|
57
75
|
# puts "will search 1"
|
58
76
|
msgs_to_archive = connection.search(conditions)
|
59
77
|
if msgs_to_archive.size > 0
|
60
|
-
|
78
|
+
@logger.debug "will archive #{msgs_to_archive.size} messages"
|
61
79
|
if connection.list("",current_archive_folder).nil?
|
62
80
|
connection.create(current_archive_folder)
|
63
81
|
if connection.capability.include?("ACL")
|
@@ -72,11 +90,12 @@ module ImapArchiver
|
|
72
90
|
connection.expunge
|
73
91
|
end
|
74
92
|
if connection.search(["BEFORE", since_date.strftime("%d-%b-%Y"), "SEEN", "NOT", "FLAGGED"]).size > 0
|
75
|
-
archive_folder_between_dates(folder,since_date.
|
93
|
+
archive_folder_between_dates(folder,since_date.prev_month, since_date)
|
76
94
|
end
|
77
95
|
rescue IOError => e
|
78
96
|
retry_count += 1
|
79
|
-
|
97
|
+
@logger.debug e.backtrace
|
98
|
+
@logger.debug "retrying"
|
80
99
|
if retry_count < 3
|
81
100
|
reconnect
|
82
101
|
retry
|
@@ -98,8 +117,8 @@ module ImapArchiver
|
|
98
117
|
end
|
99
118
|
end
|
100
119
|
|
101
|
-
def self.run(config_file)
|
120
|
+
def self.run(config_file,debug=false)
|
102
121
|
self.new(config_file).start
|
103
122
|
end
|
104
123
|
end
|
105
|
-
end
|
124
|
+
end
|
data/lib/imap_archiver/cli.rb
CHANGED
@@ -6,7 +6,8 @@ module ImapArchiver
|
|
6
6
|
|
7
7
|
# NOTE: the option -p/--path= is given as an example, and should be replaced in your application.
|
8
8
|
options = {
|
9
|
-
:config_file => "~/.imap_archiver.rb"
|
9
|
+
:config_file => "~/.imap_archiver.rb",
|
10
|
+
:debug => false
|
10
11
|
}
|
11
12
|
mandatory_options = %w( )
|
12
13
|
|
@@ -19,6 +20,7 @@ module ImapArchiver
|
|
19
20
|
opts.separator ""
|
20
21
|
opts.on("-h", "--help",
|
21
22
|
"Show this help message.") { stdout.puts opts; exit }
|
23
|
+
opts.on("-d", "--debug", "Enable debugging.") { options[:debug] = true}
|
22
24
|
opts.on("-F PATH", "", String, "Configuration file", "Default: ~/.imap_archiver.yml"){|arg| options[:config_file] = arg}
|
23
25
|
opts.parse!(arguments)
|
24
26
|
|
@@ -28,9 +30,9 @@ module ImapArchiver
|
|
28
30
|
end
|
29
31
|
|
30
32
|
# Config.load_config(options[:config_file])
|
31
|
-
Archiver.run(options[:config_file])
|
33
|
+
Archiver.run(options[:config_file],options[:debug])
|
32
34
|
|
33
35
|
return 0
|
34
36
|
end
|
35
37
|
end
|
36
|
-
end
|
38
|
+
end
|
data/spec/cli_spec.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
2
|
describe ImapArchiver::CLI do
|
3
3
|
it "should start the archiver with the default config file if no options are given" do
|
4
|
-
ImapArchiver::Archiver.expects(:run).with('~/.imap_archiver.rb')
|
4
|
+
ImapArchiver::Archiver.expects(:run).with('~/.imap_archiver.rb',false)
|
5
5
|
ImapArchiver::CLI.execute(STDOUT,STDIN,[])
|
6
6
|
end
|
7
7
|
|
@@ -11,6 +11,7 @@ describe ImapArchiver::CLI do
|
|
11
11
|
Options are:
|
12
12
|
|
13
13
|
-h, --help Show this help message.
|
14
|
+
-d, --debug Enable debugging.
|
14
15
|
-F PATH
|
15
16
|
Configuration file
|
16
17
|
Default: ~/.imap_archiver.yml
|
@@ -23,7 +24,7 @@ Options are:
|
|
23
24
|
end
|
24
25
|
|
25
26
|
it "should load the given config file" do
|
26
|
-
ImapArchiver::Archiver.expects(:run).with('/tmp/imap_archiver.rb')
|
27
|
+
ImapArchiver::Archiver.expects(:run).with('/tmp/imap_archiver.rb',false)
|
27
28
|
ImapArchiver::CLI.execute(STDOUT,STDIN,%w(-F /tmp/imap_archiver.rb))
|
28
29
|
end
|
29
|
-
end
|
30
|
+
end
|
data/spec/imap_archiver_spec.rb
CHANGED
data/spec/integration_spec.rb
CHANGED
@@ -65,7 +65,7 @@ describe "imap_archiver" do
|
|
65
65
|
@connection.expects(:list).with("","*").returns([mock1=stub(:name=>'testfolder'),mock2=stub(:name=>'foldertest')])
|
66
66
|
@connection.expects(:select).with('testfolder')
|
67
67
|
# @connection.expects(:search).twice.returns([1,2],[])
|
68
|
-
@connection.expects(:search).with(["SINCE", @archive_date.strftime("%d-%b-%Y"), "BEFORE", @archive_date.
|
68
|
+
@connection.expects(:search).with(["SINCE", @archive_date.strftime("%d-%b-%Y"), "BEFORE", @archive_date.next_month.beginning_of_month.strftime("%d-%b-%Y"), "SEEN", "NOT", "FLAGGED"]).returns([1,2])
|
69
69
|
@connection.expects(:search).with(["BEFORE", @archive_date.strftime("%d-%b-%Y"), "SEEN", "NOT", "FLAGGED"]).returns([])
|
70
70
|
|
71
71
|
@connection.expects(:copy).with([1,2],"/Archive/test/testfolder/#{@archive_date.strftime("%b %Y")}")
|
@@ -87,10 +87,10 @@ describe "imap_archiver" do
|
|
87
87
|
it "should search for messages older then the archiving period" do
|
88
88
|
@connection.expects(:list).with("","*").returns([mock1=stub(:name=>'testfolder'),mock2=stub(:name=>'foldertest')])
|
89
89
|
@connection.expects(:select).with('testfolder').twice
|
90
|
-
@connection.expects(:search).with(["SINCE", @archive_date.strftime("%d-%b-%Y"), "BEFORE", @archive_date.
|
90
|
+
@connection.expects(:search).with(["SINCE", @archive_date.strftime("%d-%b-%Y"), "BEFORE", @archive_date.next_month.beginning_of_month.strftime("%d-%b-%Y"), "SEEN", "NOT", "FLAGGED"]).returns([1,2])
|
91
91
|
@connection.expects(:search).with(["BEFORE", @archive_date.strftime("%d-%b-%Y"), "SEEN", "NOT", "FLAGGED"]).returns([3,4])
|
92
92
|
@archive_date = @archive_date.prev_month
|
93
|
-
@connection.expects(:search).with(["SINCE", @archive_date.strftime("%d-%b-%Y"), "BEFORE", @archive_date.
|
93
|
+
@connection.expects(:search).with(["SINCE", @archive_date.strftime("%d-%b-%Y"), "BEFORE", @archive_date.next_month.beginning_of_month.strftime("%d-%b-%Y"), "SEEN", "NOT", "FLAGGED"]).returns([1,2])
|
94
94
|
@connection.expects(:search).with(["BEFORE", @archive_date.strftime("%d-%b-%Y"), "SEEN", "NOT", "FLAGGED"]).returns([])
|
95
95
|
|
96
96
|
ImapArchiver::CLI.execute(STDOUT,STDIN,["-F",@config_file.path])
|
@@ -137,4 +137,4 @@ describe "imap_archiver" do
|
|
137
137
|
ImapArchiver::CLI.execute(STDOUT,STDIN,["-F",@config_file.path])
|
138
138
|
end
|
139
139
|
end
|
140
|
-
end
|
140
|
+
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: imap_archiver
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 21
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 0.0.
|
9
|
+
- 5
|
10
|
+
version: 0.0.5
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Jelle Helsen
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date:
|
18
|
+
date: 2011-02-05 00:00:00 +01:00
|
19
19
|
default_executable: imap_archiver
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|