jmcarbo-imapstore 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -6,15 +6,16 @@ require 'imapstore'
6
6
  require 'getoptions'
7
7
 
8
8
  COMMAND_LIST = ['ls', 'rm', 'put', 'get', 'rmdir', 'mkdir', 'quota', 'setup' ]
9
- opt = GetOptions.new(%w(help subject=s command=s dir=s files=@s recursive! versioned! all! ))
9
+ opt = GetOptions.new(%w( help subject=s command=s dir=s files=@s recursive! versioned! all! imapstore:s x! ))
10
10
 
11
11
  if opt['help']
12
12
  puts "Usage: imapstore -c <#{COMMAND_LIST.join(', ')}> -s <subject> -d <folder> -f <file list or glob> "
13
+ puts " -i <imapstore> -x --recursive --versioned --all"
13
14
  puts " imapstore -h"
14
15
  exit 0
15
16
  end
16
17
 
17
-
18
+ imapstore = opt['imapstore'] || "default"
18
19
  command = opt['command'] || "ls"
19
20
  dir = opt['dir'] || "INBOX"
20
21
  files = opt['files'] || "*"
@@ -22,6 +23,7 @@ subject = opt['subject'] || ""
22
23
  recursive = opt['recursive'] || false
23
24
  all = opt['all'] || false
24
25
  versioned = opt['versioned']
26
+ verbose = opt['x'] || false
25
27
 
26
28
 
27
29
  begin
@@ -30,9 +32,12 @@ if COMMAND_LIST.include? command
30
32
  IMAPSTORE::Imapstore.create_config_file
31
33
  exit
32
34
  end
33
- i = IMAPSTORE::Imapstore.new
34
- i.versioned=versioned if versioned!=nil
35
35
 
36
+
37
+ i = IMAPSTORE::Imapstore.new(File.expand_path('~/.imapstore/config.yml'), imapstore)
38
+ i.versioned=versioned if versioned!=nil
39
+ i.verbose = verbose
40
+
36
41
  case command
37
42
  when 'ls':
38
43
  files.each do |f|
@@ -4,6 +4,6 @@ $:.unshift(File.dirname(__FILE__)) unless
4
4
  require 'imapstore/imapstore.rb'
5
5
 
6
6
  module IMAPSTORE
7
- VERSION = '0.1.0'
7
+ VERSION = '0.2.0'
8
8
 
9
9
  end
@@ -12,11 +12,19 @@ module IMAPSTORE
12
12
  CHUNK_SIZE=10000000
13
13
 
14
14
  CONFIG_TEMPLATE = <<-END
15
- email: account@gmail.com
16
- password: password
17
- store_tag: IMAPSTORE
18
- imap_server: imap.gmail.com
19
- imap_port: 993
15
+ default:
16
+ email: account@gmail.com
17
+ password: password
18
+ store_tag: IMAPSTORE
19
+ imap_server: imap.gmail.com
20
+ imap_port: 993
21
+
22
+ other:
23
+ email: other_account@gmail.com
24
+ password: password
25
+ store_tag: IMAPSTORE
26
+ imap_server: imap.gmail.com
27
+ imap_port: 993
20
28
  END
21
29
 
22
30
  def self.create_config_file(file = File.expand_path('~/.imapstore/config.yml'))
@@ -27,9 +35,11 @@ END
27
35
  end
28
36
  end
29
37
 
30
- def initialize
38
+ def initialize(file = File.expand_path('~/.imapstore/config.yml'), imapstore="default")
31
39
  @versioned = true
32
- get_config
40
+ @verbose = false
41
+
42
+ get_config(file, imapstore)
33
43
  connect
34
44
  end
35
45
 
@@ -61,6 +71,14 @@ END
61
71
  @imap_port
62
72
  end
63
73
 
74
+ def verbose
75
+ @verbose
76
+ end
77
+
78
+ def verbose=value
79
+ @verbose = value
80
+ end
81
+
64
82
  def connect()
65
83
  if !@imap && @email && @password && @imap_server && @imap_port
66
84
  @imap = Net::IMAP.new(@imap_server, @imap_port, true)
@@ -78,18 +96,19 @@ END
78
96
  end
79
97
  end
80
98
 
81
- def get_config(file = File.expand_path('~/.imapstore/config.yml'))
99
+ def get_config(file = File.expand_path('~/.imapstore/config.yml'), imapstore = "default")
82
100
  if !File.exists?(file)
83
101
  File.open(file,"w").write(CONFIG_TEMPLATE)
84
102
  end
103
+
85
104
  aConfig = YAML::load_file(file)
86
105
 
87
106
  #COFIGURATION SECTION
88
- @email = aConfig['email']
89
- @password = aConfig['password']
90
- @store_tag = aConfig['store_tag'] || "IMAPSTORE"
91
- @imap_server = aConfig['imap_server']
92
- @imap_port = aConfig['imap_port']
107
+ @email = aConfig[imapstore]['email']
108
+ @password = aConfig[imapstore]['password']
109
+ @store_tag = aConfig[imapstore]['store_tag'] || "IMAPSTORE"
110
+ @imap_server = aConfig[imapstore]['imap_server']
111
+ @imap_port = aConfig[imapstore]['imap_port']
93
112
  #END CONFIGURATION SECTION
94
113
  end
95
114
 
@@ -184,27 +203,41 @@ END
184
203
  def transverse(target_folder = "INBOX", glob = /.+/, recursive = false, dot_files = false, folder_only=false)
185
204
  file_list=[]
186
205
 
187
- @imap.list(target_folder, "*").each do |folder|
188
- if ((recursive == true) && (folder.name.match(/^#{target_folder}(\/.+)*$/))) || (folder.name == target_folder)
189
- file_list << folder.name
190
- yield(folder.name, nil, nil, nil, nil) if block_given?
206
+
207
+
208
+
209
+ begin
210
+ target_folder = "" if target_folder == "/"
211
+
212
+ puts "..... fetching #{target_folder} " if @verbose
191
213
 
192
- if((!folder.attr.include? :Noselect) && (folder_only==false))
214
+ @imap.list(target_folder, "*").each do |folder|
193
215
 
194
- @imap.select(folder.name)
216
+ puts "..... transversing #{folder.name} " if @verbose
217
+
218
+ if ((recursive == true) && (target_folder == "" ? true : folder.name.match(/^#{target_folder}(\/.+)*$/))) || (folder.name == target_folder)
219
+ puts "..... got hit transversing #{folder.name} " if @verbose
220
+ file_list << folder.name
221
+ yield(folder.name, nil, nil, nil, nil) if block_given?
222
+
223
+ if((!folder.attr.include? :Noselect) && (folder_only==false))
224
+
225
+ @imap.select(folder.name)
195
226
 
196
- @imap.search(["NOT", "DELETED"]).each do |message_id|
197
- a = @imap.fetch(message_id, "RFC822")
198
- mail = TMail::Mail.parse(a[0].attr["RFC822"])
199
- file_name = mail.subject.match(/^[^\[]+\[([^\]]+)\]/)[1]
200
- file_list << folder.name + "/" + file_name if file_name.match(glob)
201
- yield(folder.name, file_name, message_id, mail, file_name) if block_given? && file_name.match(glob)
227
+ @imap.search(["NOT", "DELETED"]).each do |message_id|
228
+ a = @imap.fetch(message_id, "RFC822")
229
+ mail = TMail::Mail.parse(a[0].attr["RFC822"])
230
+ file_name = mail.subject.match(/^[^\[]+\[([^\]]+)\]/)[1]
231
+ file_list << folder.name + "/" + file_name if file_name.match(glob)
232
+ yield(folder.name, file_name, message_id, mail, file_name) if block_given? && file_name.match(glob)
233
+ end
202
234
  end
203
235
  end
204
236
  end
237
+ rescue
205
238
  end
206
-
207
- file_list.sort
239
+
240
+ file_list.sort
208
241
  end
209
242
 
210
243
  def ls(target_folder = "INBOX", glob = /.+/, recursive = false, dot_files = false)
@@ -232,13 +265,14 @@ END
232
265
 
233
266
 
234
267
 
235
- def rm_file(folder = "INBOX", glob = /.+/, recursive = false, dot_files = false)
236
- file_list = transverse(target_folder, glob, recursive, dot_files, false) do |folder, file, message_id, mail, file_name|
237
- @imap.store(message_id, "+FLAGS", [:Deleted]) if file && message_id
268
+ def rm_file(target_folder = "INBOX", glob = /.+/, recursive = false, dot_files = false)
269
+ file_list = transverse(target_folder, glob, false, dot_files, false) do |folder, file, message_id, mail, file_name|
270
+ if file && message_id
271
+ @imap.store(message_id, "+FLAGS", [:Deleted])
272
+ @imap.expunge
273
+ end
238
274
  end
239
-
240
- @imap.expunge
241
-
275
+
242
276
  file_list
243
277
  end
244
278
 
@@ -246,11 +280,28 @@ END
246
280
  @imap.create(folder)
247
281
  end
248
282
 
249
- def rmdir(folder, recursive = false, dot_files = false)
250
- transverse(target_folder, glob, false, dot_files, true) do |folder, file, message_id, mail, file_name|
251
- rm_file(folder)
283
+ def rmdir(target_folder, recursive = false, dot_files = false)
284
+ folders = []
285
+ puts "..... Deleting #{target_folder}" if @verbose
286
+
287
+ transverse(target_folder, /.+/, recursive, dot_files, false) do |folder, file, message_id, mail, file_name|
288
+ puts "..... assigning dir #{folder} for removal" if @verbose
289
+ folders << folder if !(folders.include? folder)
290
+
291
+ if file && message_id
292
+ puts "..... actually removing dir #{folder} file #{file_name}" if @verbose
293
+ @imap.store(message_id, "+FLAGS", [:Deleted])
294
+ @imap.expunge
295
+ end
296
+ end
297
+
298
+
299
+ folders.each do |folder|
300
+ puts "..... actually removing dir #{folder}" if @verbose
252
301
  @imap.delete(folder)
253
302
  end
303
+
304
+ folders
254
305
  end
255
306
 
256
307
  def quota(folder)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jmcarbo-imapstore
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Joan Marc Carbo Arnau