imapget 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
data/README CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  == VERSION
4
4
 
5
- This documentation refers to imapget version 0.0.4
5
+ This documentation refers to imapget version 0.0.5
6
6
 
7
7
 
8
8
  == DESCRIPTION
@@ -27,7 +27,7 @@ Rubyforge project:: <http://rubyforge.org/projects/imapget>
27
27
 
28
28
  == LICENSE AND COPYRIGHT
29
29
 
30
- Copyright (C) 2009 Jens Wille,
30
+ Copyright (C) 2009 Jens Wille
31
31
 
32
32
  imapget is free software: you can redistribute it and/or modify it under the
33
33
  terms of the GNU General Public License as published by the Free Software
data/bin/imapget CHANGED
@@ -93,6 +93,8 @@ profiles.each { |profile|
93
93
 
94
94
  config.update(default_config) { |key, old, new| old }
95
95
 
96
+ next if config[:skip]
97
+
96
98
  unless host = config[:host]
97
99
  warn "No host for profile #{profile}"
98
100
  next
@@ -4,7 +4,7 @@ class IMAPGet
4
4
 
5
5
  MAJOR = 0
6
6
  MINOR = 0
7
- TINY = 4
7
+ TINY = 5
8
8
 
9
9
  class << self
10
10
 
data/lib/imapget.rb CHANGED
@@ -27,6 +27,7 @@
27
27
  require 'time'
28
28
  require 'net/imap'
29
29
  require 'fileutils'
30
+ require 'enumerator' unless [].respond_to?(:each_slice)
30
31
 
31
32
  require 'imapget/version'
32
33
 
@@ -37,28 +38,21 @@ class IMAPGet
37
38
  STRATEGIES = [:include, :exclude]
38
39
  DEFAULT_STRATEGY = :exclude
39
40
 
41
+ DEFAULT_LEVEL = 2
40
42
  DEFAULT_DELIM = '/'
41
43
 
44
+ FETCH_ATTR = %w[FLAGS INTERNALDATE RFC822]
45
+ FETCH_SIZE = 100
46
+
42
47
  attr_reader :imap, :strategy, :incl, :excl
43
48
  attr_accessor :log_level
44
49
 
45
50
  def initialize(options)
46
- @imap = Net::IMAP.new(
47
- options[:host],
48
- options[:port] || Net::IMAP::PORT,
49
- options[:use_ssl],
50
- options[:certs],
51
- options[:verify]
52
- )
51
+ @log_level = options[:log_level] || DEFAULT_LEVEL
53
52
 
54
- @imap.authenticate(
55
- @imap.capability.include?('AUTH=CRAM-MD5') ? 'CRAM-MD5' : 'LOGIN',
56
- options[:user], options[:password]
57
- )
58
-
59
- inclexcl(options)
60
-
61
- @log_level = 2
53
+ init_imap options
54
+ authenticate options
55
+ inclexcl options
62
56
  end
63
57
 
64
58
  def inclexcl(options)
@@ -109,7 +103,7 @@ class IMAPGet
109
103
  when Net::IMAP::MailboxList then mailbox_or_name.name
110
104
  when String then mailbox_or_name
111
105
  else
112
- raise TypeError, "String or MailboxList expected, got #{mailbox_or_name.class}"
106
+ raise TypeError, "MailboxList or String expected, got #{mailbox_or_name.class}"
113
107
  end
114
108
 
115
109
  case strategy
@@ -122,30 +116,20 @@ class IMAPGet
122
116
 
123
117
  def get(path)
124
118
  each { |mailbox|
125
- name = mailbox.name
126
- dir = File.join(path, Net::IMAP.decode_utf7(name))
127
-
128
- if File.directory?(dir)
129
- last_update = File.mtime(dir)
130
- else
131
- FileUtils.mkdir_p(dir)
132
- last_update = Time.at(0)
133
- end
119
+ name = mailbox.name
120
+ dir = File.join(path, Net::IMAP.decode_utf7(name))
121
+ update = check_dir(dir)
134
122
 
135
123
  log "#{'=' * 10} #{name} #{'=' * 10}"
136
124
  imap.examine(name)
137
125
 
138
- uids = imap.uid_search("SINCE #{last_update.utc.strftime('%d-%b-%Y')}")
126
+ uids = imap.uid_search("SINCE #{update.strftime('%d-%b-%Y')}")
139
127
  next if uids.empty?
140
128
 
141
- imap.uid_fetch(uids, %w[RFC822 INTERNALDATE FLAGS]).each { |mail|
142
- uid = mail.attr['UID']
143
- flags = mail.attr['FLAGS']
144
- file = File.join(dir, uid.to_s)
145
- date = Time.parse(mail.attr['INTERNALDATE'])
146
-
147
- deleted = flags.include?(Net::IMAP::DELETED)
129
+ fetch(uids) { |mail, uid, flags, date, body|
130
+ file = File.join(dir, uid.to_s)
148
131
  exists = File.exists?(file)
132
+ deleted = flags.include?(Net::IMAP::DELETED)
149
133
 
150
134
  if deleted
151
135
  if exists
@@ -155,7 +139,7 @@ class IMAPGet
155
139
  else
156
140
  unless exists && File.mtime(file) >= date
157
141
  info "#{dir}: #{name} -> #{uid}"
158
- File.open(file, 'w') { |f| f.puts mail.attr['RFC822'] }
142
+ File.open(file, 'w') { |f| f.puts body }
159
143
  end
160
144
  end
161
145
  }
@@ -166,6 +150,44 @@ class IMAPGet
166
150
 
167
151
  private
168
152
 
153
+ def init_imap(options)
154
+ @imap = Net::IMAP.new(
155
+ options[:host],
156
+ options[:port] || Net::IMAP::PORT,
157
+ options[:use_ssl],
158
+ options[:certs],
159
+ options[:verify]
160
+ )
161
+ end
162
+
163
+ def authenticate(options)
164
+ imap.authenticate(
165
+ imap.capability.include?('AUTH=CRAM-MD5') ? 'CRAM-MD5' : 'LOGIN',
166
+ options[:user], options[:password]
167
+ )
168
+ end
169
+
170
+ def fetch(uids, fetch_size = FETCH_SIZE, fetch_attr = FETCH_ATTR)
171
+ uids.each_slice(fetch_size) { |slice|
172
+ imap.uid_fetch(slice, fetch_attr).each { |mail|
173
+ yield mail, *['UID', *fetch_attr].map { |a|
174
+ v = mail.attr[a]
175
+ v = Time.parse(v) if a == 'INTERNALDATE'
176
+ v
177
+ }
178
+ }
179
+ }
180
+ end
181
+
182
+ def check_dir(dir)
183
+ if File.directory?(dir)
184
+ File.mtime(dir)
185
+ else
186
+ FileUtils.mkdir_p(dir)
187
+ Time.at(0)
188
+ end.utc
189
+ end
190
+
169
191
  def log(msg, level = 1)
170
192
  warn msg if log_level >= level
171
193
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: imapget
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jens Wille
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-05-25 00:00:00 +02:00
12
+ date: 2009-05-26 00:00:00 +02:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency