vmail 0.3.9 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -70,6 +70,18 @@ module Vmail
70
70
  @imap.select(@mailbox)
71
71
  end
72
72
 
73
+ def prime_connection
74
+ reconnect_if_necessary(4) do
75
+ # this is just to prime the IMAP connection
76
+ # It's necessary for some reason before update and deliver.
77
+ log "priming connection for delivering"
78
+ res = @imap.fetch(@ids[-1], ["ENVELOPE"])
79
+ if res.nil?
80
+ raise IOError, "IMAP connection seems broken"
81
+ end
82
+ end
83
+ end
84
+
73
85
  def list_mailboxes
74
86
  @mailboxes ||= (@imap.list("[Gmail]/", "%") + @imap.list("", "%")).
75
87
  select {|struct| struct.attr.none? {|a| a == :Noselect} }.
@@ -90,6 +102,7 @@ module Vmail
90
102
 
91
103
  # id_set may be a range, array, or string
92
104
  def fetch_envelopes(id_set)
105
+ log "fetch_envelopes: #{id_set.inspect}"
93
106
  if id_set.is_a?(String)
94
107
  id_set = id_set.split(',')
95
108
  end
@@ -206,19 +219,18 @@ module Vmail
206
219
  query.unshift "1:#@num_messages"
207
220
  @all_search = false
208
221
  end
222
+ log "@all_search #{@all_search}"
209
223
  @query = query.join(' ')
210
224
  log "search query: #@query"
211
- ids = reconnect_if_necessary do
225
+ @ids = reconnect_if_necessary do
212
226
  @imap.search(@query)
213
227
  end
214
- fetch_ids = if ids.size > limit
215
- log "truncating returned set to #{limit}"
216
- @start_index = ids.index(ids[-1]) - limit
217
- # save ids in @ids
218
- @ids = ids
219
- ids[@start_index..ids[-1]]
220
- else
221
- ids
228
+ # save ids in @ids, because filtered search relies on it
229
+ fetch_ids = if @all_search
230
+ @ids
231
+ else #filtered search
232
+ @start_index = [@ids.length - limit, 0].max
233
+ @ids[@start_index..-1]
222
234
  end
223
235
  log "search query result: #{fetch_ids.inspect}"
224
236
  res = fetch_envelopes(fetch_ids)
@@ -226,31 +238,24 @@ module Vmail
226
238
  end
227
239
 
228
240
  def update
229
- reconnect_if_necessary(4) do
230
- # this is just to prime the IMAP connection
231
- # It's necessary for some reason.
232
- log "priming connection for update"
233
- res = @imap.fetch(@all_ids[-1], ["ENVELOPE"])
234
- if res.nil?
235
- raise IOError, "IMAP connection seems broken"
236
- end
237
- end
241
+ prime_connection
238
242
  ids = reconnect_if_necessary {
239
243
  log "search #@query"
240
244
  @imap.search(@query)
241
245
  }
242
246
  # TODO change this. will throw error now
243
- new_ids = ids - @all_ids
247
+ new_ids = ids.select {|x| x > @ids.max}
248
+ @ids = @ids + new_ids
244
249
  log "UPDATE: NEW UIDS: #{new_ids.inspect}"
245
250
  if !new_ids.empty?
246
251
  res = fetch_envelopes(new_ids)
247
- @all_ids = ids
248
252
  res
249
253
  end
250
254
  end
251
255
 
252
256
  # gets 100 messages prior to id
253
257
  def more_messages(message_id, limit=100)
258
+ log "more_messages: message_id #{message_id}"
254
259
  message_id = message_id.to_i
255
260
  if @all_search
256
261
  x = [(message_id - limit), 0].max
@@ -259,26 +264,31 @@ module Vmail
259
264
  add_more_message_line(res, x)
260
265
  else
261
266
  # filter search query
267
+ log "@start_index #@start_index"
262
268
  x = [(@start_index - limit), 0].max
263
269
  y = [@start_index - 1, 0].max
264
270
  @start_index = x
271
+ log "fetch_envelopes @ids[#{x}..#{y}]"
265
272
  res = fetch_envelopes(@ids[x..y])
266
273
  add_more_message_line(res, @ids[x])
267
274
  end
268
275
  end
269
276
 
270
277
  def add_more_message_line(res, start_id)
278
+ log "add_more_message_line for start_id #{start_id}"
271
279
  if @all_search
272
280
  return res if start_id.nil?
273
281
  if start_id <= 1
274
282
  return res
275
283
  end
276
- log "remaining = start_id - 1: #{start_id} - 1"
277
284
  remaining = start_id - 1
278
285
  else # filter search
279
- log "remaining = @ids.index(#{start_id}) - 1; @ids.size: #{@ids.size}"
280
286
  remaining = @ids.index(start_id) - 1
281
287
  end
288
+ if remaining < 1
289
+ log "none remaining"
290
+ return res
291
+ end
282
292
  log "remaining messages: #{remaining}"
283
293
  "> Load #{[100, remaining].min} more messages. #{remaining} remaining.\n" + res
284
294
  end
@@ -465,15 +475,7 @@ EOF
465
475
  def deliver(text)
466
476
  # parse the text. The headers are yaml. The rest is text body.
467
477
  require 'net/smtp'
468
- reconnect_if_necessary(4) do
469
- # this is just to prime the IMAP connection
470
- # It's necessary for some reason.
471
- log "priming connection for delivering"
472
- res = @imap.fetch(@all_ids[-1], ["ENVELOPE"])
473
- if res.nil?
474
- raise IOError, "IMAP connection seems broken"
475
- end
476
- end
478
+ prime_connection
477
479
  mail = new_mail_from_input(text)
478
480
  mail.delivery_method(*smtp_settings)
479
481
  log mail.deliver!
@@ -1,3 +1,3 @@
1
1
  module Vmail
2
- VERSION = "0.3.9"
2
+ VERSION = "0.4.0"
3
3
  end
metadata CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 0
7
- - 3
8
- - 9
9
- version: 0.3.9
7
+ - 4
8
+ - 0
9
+ version: 0.4.0
10
10
  platform: ruby
11
11
  authors:
12
12
  - Daniel Choi