jmcarbo-imapstore 0.4.2 → 0.4.5

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -8,7 +8,7 @@ ruby gem to use an imap server as a file storage device
8
8
 
9
9
  == USAGE
10
10
 
11
- * recursive storign files
11
+ * recursive storing files
12
12
 
13
13
  imapstore -c put -r -d <folder> -f <files>
14
14
 
data/bin/imapstore CHANGED
@@ -12,7 +12,7 @@ opt = GetOptions.new(%w( help subject=s command=s dir=s files=@s recursive! vers
12
12
 
13
13
  if opt['help']
14
14
  puts "Usage: imapstore -c <#{COMMAND_LIST.join(', ')}> -s <subject> -d <folder> -f <file list or glob> "
15
- puts " -i <imapstore> -x --recursive --versioned --all"
15
+ puts " -i <imapstore> -x --recursive --versioned --all -t <text of message>"
16
16
  puts " imapstore -h"
17
17
  exit 0
18
18
  end
@@ -28,18 +28,25 @@ versioned = opt['versioned']
28
28
  verbose = opt['x'] || false
29
29
  text = opt['text'] || "no text"
30
30
 
31
+
32
+
33
+
31
34
  begin
35
+
32
36
  if COMMAND_LIST.include? command
33
37
  if command == 'setup'
34
38
  IMAPSTORE::Imapstore.create_config_file
35
39
  exit
36
40
  end
37
41
 
38
-
42
+ puts "Opening mailstore #{imapstore} configured in #{File.expand_path('~/.imapstore/config.yml')}" if verbose
39
43
  i = IMAPSTORE::Imapstore.new(File.expand_path('~/.imapstore/config.yml'), imapstore)
44
+ puts "Mailstore #{imapstore} opened" if verbose
45
+
40
46
  i.versioned=versioned if versioned!=nil
41
47
  i.verbose = verbose
42
-
48
+
49
+ puts "Executing command #{command}" if verbose
43
50
  case command
44
51
  when 'ls':
45
52
  files.each do |f|
@@ -50,8 +57,9 @@ if COMMAND_LIST.include? command
50
57
  end
51
58
  end
52
59
  when 'put':
53
-
60
+ puts "Storing files" if verbose
54
61
  files.each do |f|
62
+ f = File.expand_path(f)
55
63
  local_dir = File.dirname(f)
56
64
  f = local_dir + "/**/" + File.basename(f) if recursive
57
65
 
@@ -60,8 +68,12 @@ if COMMAND_LIST.include? command
60
68
  remote_dir = dir
61
69
  append_dir = File.dirname(f2).sub(/^#{local_dir}\/*/,"")
62
70
  remote_dir = remote_dir + "/" + append_dir if append_dir != ""
63
- puts "Storing file #{f2} in remote dir \"#{remote_dir}\""
64
- i.store_file(f2,remote_dir,subject)
71
+ if File.exists?(f2)
72
+ puts "Storing file #{f2} in remote dir \"#{remote_dir}\""
73
+ i.store_file(f2,remote_dir,subject)
74
+ else
75
+ puts "File #{f2} does not exist."
76
+ end
65
77
  end
66
78
  end
67
79
  end
@@ -92,13 +104,16 @@ if COMMAND_LIST.include? command
92
104
 
93
105
  i.snipplet(subject,text,dir, nil)
94
106
  else
95
- puts "unimplemented command #{command}"
107
+ raise "unimplemented command #{command}"
96
108
  end
97
109
 
98
110
 
99
111
 
100
112
  i.disconnect
113
+ else
114
+ puts "command #{command} is unimplemented"
101
115
  end
116
+
102
117
  rescue
103
118
  puts "An error has occured #{$!}"
104
119
  end
@@ -18,13 +18,15 @@ default:
18
18
  store_tag: IMAPSTORE
19
19
  imap_server: imap.gmail.com
20
20
  imap_port: 993
21
-
21
+ ssl: true
22
+
22
23
  other:
23
24
  email: other_account@gmail.com
24
25
  password: password
25
26
  store_tag: IMAPSTORE
26
27
  imap_server: imap.gmail.com
27
28
  imap_port: 993
29
+ ssl: true
28
30
  END
29
31
 
30
32
  def self.create_config_file(file = File.expand_path('~/.imapstore/config.yml'))
@@ -36,11 +38,13 @@ END
36
38
  end
37
39
 
38
40
  def initialize(file = File.expand_path('~/.imapstore/config.yml'), imapstore="default")
41
+ puts "Initializing IMAP" if @verbose
39
42
  @versioned = true
40
43
  @verbose = false
41
-
42
- get_config(file, imapstore)
43
- connect
44
+
45
+ puts "Getting configuration file #{file} for account #{imapstore}" if @verbose
46
+ get_config(file, imapstore)
47
+ connect
44
48
  end
45
49
 
46
50
  def versioned
@@ -75,13 +79,17 @@ END
75
79
  @verbose
76
80
  end
77
81
 
78
- def verbose=value
82
+ def verbose=(value)
79
83
  @verbose = value
80
84
  end
81
85
 
82
86
  def connect()
83
87
  if !@imap && @email && @password && @imap_server && @imap_port
84
- @imap = Net::IMAP.new(@imap_server, @imap_port, true)
88
+ if(@ssl == true)
89
+ @imap = Net::IMAP.new(@imap_server, @imap_port, true)
90
+ else
91
+ @imap = Net::IMAP.new(@imap_server, @imap_port, false)
92
+ end
85
93
  @imap.login(@email, @password)
86
94
  end
87
95
  end
@@ -109,26 +117,29 @@ END
109
117
  @store_tag = aConfig[imapstore]['store_tag'] || "IMAPSTORE"
110
118
  @imap_server = aConfig[imapstore]['imap_server']
111
119
  @imap_port = aConfig[imapstore]['imap_port']
120
+ @ssl = aConfig[imapstore]['ssl'] || false
112
121
  #END CONFIGURATION SECTION
122
+
123
+ aConfig
113
124
  end
114
125
 
115
126
  def snipplet(subject = "snipplet", body = "", folder="snipplets", file = nil)
116
127
  puts "Storing snipplet" if @verbose
117
128
 
118
129
  mail = TMail::Mail.new
119
- mail.to = @email
120
- mail.from = @email
121
- mail.subject = subject
122
-
123
- mail.date = Time.now
124
- mail.mime_version = '1.0'
130
+ mail.to = @email
131
+ mail.from = @email
132
+ mail.subject = subject
133
+
134
+ mail.date = Time.now
135
+ mail.mime_version = '1.0'
125
136
 
126
- mailpart1=TMail::Mail.new
127
- mailpart1.set_content_type 'text', 'plain'
128
- mailpart1.body = body
129
- mail.parts << mailpart1
130
- mail.set_content_type 'multipart', 'mixed'
131
-
137
+ mailpart1=TMail::Mail.new
138
+ mailpart1.set_content_type 'text', 'plain'
139
+ mailpart1.body = body
140
+ mail.parts << mailpart1
141
+ mail.set_content_type 'multipart', 'mixed'
142
+
132
143
  # if file && FileTest.exists?(file)
133
144
  # mailpart=TMail::Mail.new
134
145
  # mailpart.body = Base64.encode64(chunk.to_s)
@@ -140,14 +151,14 @@ END
140
151
  @imap.append(folder, mail.to_s, [:Seen], Time.now)
141
152
 
142
153
  end
143
-
154
+
144
155
  def store_file_chunk( file, folder = "INBOX", subject = "", chunk=nil, chunk_no=0, max_chunk_no=0 )
145
156
  if(max_chunk_no>0)
146
157
  puts "Storing part #{chunk_no} of #{max_chunk_no}"
147
158
  end
148
- basename = File.basename(file)
159
+ basename = File.basename(file)
149
160
  version = 0
150
-
161
+
151
162
  if max_chunk_no == 0
152
163
  tag = "#{@store_tag}[#{basename}]"
153
164
  else
@@ -164,12 +175,12 @@ END
164
175
  @imap.expunge
165
176
 
166
177
  mail = TMail::Mail.new
167
- mail.to = @email
168
- mail.from = @email
178
+ mail.to = @email
179
+ mail.from = @email
169
180
  if max_chunk_no == 0
170
- mail.subject = "#{@store_tag}[#{basename}]"
181
+ mail.subject = "#{@store_tag}[#{basename}]"
171
182
  else
172
- mail.subject = "#{@store_tag}[#{basename}](#{chunk_no}-#{max_chunk_no})"
183
+ mail.subject = "#{@store_tag}[#{basename}](#{chunk_no}-#{max_chunk_no})"
173
184
  end
174
185
 
175
186
  if version > 0
@@ -180,36 +191,36 @@ END
180
191
  mail.subject = mail.subject + ": #{subject}"
181
192
  end
182
193
 
183
- mail.date = Time.now
184
- mail.mime_version = '1.0'
185
-
186
- mailpart1=TMail::Mail.new
187
- mailpart1.set_content_type 'text', 'plain'
188
- mailpart1.body = "This is a mail storage message of file #{file}"
189
- mail.parts << mailpart1
190
- mail.set_content_type 'multipart', 'mixed'
191
-
192
- if FileTest.exists?(file)
193
- mailpart=TMail::Mail.new
194
- mailpart.body = Base64.encode64(chunk.to_s)
195
- mailpart.transfer_encoding="Base64"
194
+ mail.date = Time.now
195
+ mail.mime_version = '1.0'
196
+
197
+ mailpart1=TMail::Mail.new
198
+ mailpart1.set_content_type 'text', 'plain'
199
+ mailpart1.body = "This is a mail storage message of file #{file}"
200
+ mail.parts << mailpart1
201
+ mail.set_content_type 'multipart', 'mixed'
202
+
203
+ if FileTest.exists?(file)
204
+ mailpart=TMail::Mail.new
205
+ mailpart.body = Base64.encode64(chunk.to_s)
206
+ mailpart.transfer_encoding="Base64"
196
207
  if max_chunk_no == 0
197
- mailpart['Content-Disposition'] = "attachment; filename=#{CGI::escape(basename)}"
208
+ mailpart['Content-Disposition'] = "attachment; filename=#{CGI::escape(basename)}"
198
209
  else
199
- mailpart['Content-Disposition'] = "attachment; filename=#{CGI::escape(basename)}_#{chunk_no}-#{max_chunk_no}"
210
+ mailpart['Content-Disposition'] = "attachment; filename=#{CGI::escape(basename)}_#{chunk_no}-#{max_chunk_no}"
200
211
  end
201
- mail.parts.push(mailpart)
202
- end
203
-
204
-
205
- @imap.append(folder, mail.to_s, [:Seen], File.new(file).atime)
206
-
212
+ mail.parts.push(mailpart)
213
+ end
214
+
215
+
216
+ @imap.append(folder, mail.to_s, [:Seen], File.new(file).atime)
217
+
207
218
  end
208
219
 
209
- def store_file( file, folder = "INBOX", subject = "")
210
-
211
- @imap.create(folder) if !@imap.list("", folder)
212
-
220
+ def store_file( file, folder = "INBOX", subject = "")
221
+
222
+ @imap.create(folder) if !@imap.list("", folder)
223
+
213
224
  size = File.size(file)
214
225
  if(size > MAX_FILE_SIZE)
215
226
  remaining = size
@@ -227,12 +238,12 @@ END
227
238
  content=File.open(file).read
228
239
  store_file_chunk( file, folder, subject, content)
229
240
  end
230
- end
231
-
241
+ end
242
+
232
243
  def transverse(target_folder = "INBOX", glob = /.+/, recursive = false, dot_files = false, folder_only=false)
233
244
  file_list=[]
234
-
235
-
245
+
246
+
236
247
 
237
248
 
238
249
  begin
@@ -241,7 +252,7 @@ END
241
252
  puts "..... fetching #{target_folder} " if @verbose
242
253
 
243
254
  @imap.list(target_folder, "*").each do |folder|
244
-
255
+
245
256
  puts "..... transversing #{folder.name} " if @verbose
246
257
 
247
258
  if ((recursive == true) && (target_folder == "" ? true : folder.name.match(/^#{target_folder}(\/.+)*$/))) || (folder.name == target_folder)
@@ -250,9 +261,9 @@ END
250
261
  yield(folder.name, nil, nil, nil, nil) if block_given?
251
262
 
252
263
  if((!folder.attr.include? :Noselect) && (folder_only==false))
253
-
264
+
254
265
  @imap.select(folder.name)
255
-
266
+
256
267
  @imap.search(["NOT", "DELETED"]).each do |message_id|
257
268
  a = @imap.fetch(message_id, "RFC822")
258
269
  mail = TMail::Mail.parse(a[0].attr["RFC822"])
@@ -266,14 +277,14 @@ END
266
277
  rescue
267
278
  end
268
279
 
269
- file_list.sort
280
+ file_list.sort
270
281
  end
271
-
272
- def ls(target_folder = "INBOX", glob = /.+/, recursive = false, dot_files = false)
282
+
283
+ def ls(target_folder = "INBOX", glob = /.+/, recursive = false, dot_files = false)
273
284
  transverse(target_folder, glob, recursive, dot_files, false)
274
- end
275
-
276
- def get_file(target_folder = "INBOX", glob = /.+/, recursive = false, dot_files = false)
285
+ end
286
+
287
+ def get_file(target_folder = "INBOX", glob = /.+/, recursive = false, dot_files = false)
277
288
  file_list = transverse(target_folder, glob, recursive, dot_files, false) do |folder, file, message_id, mail, file_name|
278
289
 
279
290
  if mail && mail.multipart? then
@@ -282,7 +293,7 @@ END
282
293
  append_dir = folder.sub(/^#{target_folder}\/*/,"")
283
294
  if append_dir != ""
284
295
  Dir.mkdir(append_dir) if !File.exists? append_dir
285
-
296
+
286
297
  file_name = append_dir + "/" + file_name
287
298
  end
288
299
  File.open(file_name,"w").write(m.body)
@@ -290,21 +301,21 @@ END
290
301
  end
291
302
  end
292
303
  end
293
- end
294
-
295
-
296
-
297
- def rm_file(target_folder = "INBOX", glob = /.+/, recursive = false, dot_files = false)
304
+ end
305
+
306
+
307
+
308
+ def rm_file(target_folder = "INBOX", glob = /.+/, recursive = false, dot_files = false)
298
309
  file_list = transverse(target_folder, glob, false, dot_files, false) do |folder, file, message_id, mail, file_name|
299
310
  if file && message_id
300
- @imap.store(message_id, "+FLAGS", [:Deleted])
311
+ @imap.store(message_id, "+FLAGS", [:Deleted])
301
312
  @imap.expunge
302
313
  end
303
314
  end
304
315
 
305
- file_list
306
- end
307
-
316
+ file_list
317
+ end
318
+
308
319
  def mkdir(folder)
309
320
  @imap.create(folder)
310
321
  end
@@ -319,11 +330,11 @@ END
319
330
 
320
331
  if file && message_id
321
332
  puts "..... actually removing dir #{folder} file #{file_name}" if @verbose
322
- @imap.store(message_id, "+FLAGS", [:Deleted])
333
+ @imap.store(message_id, "+FLAGS", [:Deleted])
323
334
  @imap.expunge
324
335
  end
325
336
  end
326
-
337
+
327
338
 
328
339
  folders.each do |folder|
329
340
  puts "..... actually removing dir #{folder}" if @verbose
data/lib/imapstore.rb CHANGED
@@ -4,6 +4,6 @@ $:.unshift(File.dirname(__FILE__)) unless
4
4
  require 'imapstore/imapstore.rb'
5
5
 
6
6
  module IMAPSTORE
7
- VERSION = '0.4.2'
7
+ VERSION = '0.4.4'
8
8
 
9
9
  end
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.4.2
4
+ version: 0.4.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Joan Marc Carbo Arnau