imap_mogura 0.1.1 → 0.2.2.pre.dev

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 3f5a65d48e4e81a37e519995c39cddf533e3336477c358044462f51935ee1d0f
4
- data.tar.gz: 59975c34bdd3ce2d372c71c5d6b8d9689867e431956104df8cd958294a18dc3d
3
+ metadata.gz: e8ab3a83b682da8cfeeb55ea96c1e1bd3b046246da28923196d33fe7cd3d7f26
4
+ data.tar.gz: 3d0fd5fa039f632ed3c63e2467e3eb31703cb4ec9aca2aa8a0a15d2592b04124
5
5
  SHA512:
6
- metadata.gz: c29a3b716cfde67b59876ad149add941ac2db45fa92154fbaa0a3587e438939568ae6eb536b12ddcc28bd14c0ab131a3f4a5725d0e11775cc3f1a95d30400b84
7
- data.tar.gz: fc9c28711eb63e293e09173a3b4eac353e5ca40d4b3d7b01b07ea5f571c80d941d669c1bd93217c21d179a16ff59b116996f0a581b80ad883c5875a590e2f8b2
6
+ metadata.gz: 05230b9da5a38e8abc1b398d0939528fed9dcccf1f01dae327844d63e26f25dfa009083c3ee5b84d070fc164cce22ce18c23a52edb3fcbbef34926f908fd256f
7
+ data.tar.gz: c81ee3bc93144669f170ea3d139aac88bbfc2c660d6fa3e15a2f653b7fde2ff8b2f69bb7dbcef69fed73ff51845aa6ce08f005d1b4326e2ed0798fe397cf3249
data/README.md CHANGED
@@ -4,13 +4,13 @@ A mail filtering tool for IMAP.
4
4
 
5
5
  ## Installation
6
6
 
7
- To install this gem, you can get it from RubyGems.org. Just run as following.
7
+ To install this gem, just run as following. You can get the gem from [RubyGems.org](https://rubygems.org/).
8
8
 
9
9
  ```console
10
10
  $ gem install imap_mogura --pre
11
11
  ```
12
12
 
13
- (This gem has only released pre-release versions now, so you must specify `--pre` option.)
13
+ (This branch is `develop`, that means this is a pre-release version. You can install this version by specifying `--pre` option.)
14
14
 
15
15
  ## Usage
16
16
 
@@ -35,22 +35,21 @@ rules:
35
35
  subject: "i'm trash-like email!!"
36
36
  ```
37
37
 
38
- As following, `start` command will start monitoring RECENT mails on "INBOX". If a mail is coming
39
- and it's RECENT, it will be filtered.
38
+ Running `mogura start` command will start monitoring RECENT mails coming to "INBOX". If a mail with RECENT flag is coming to the mailbox, it will be filtered.
40
39
 
41
40
  ```console
42
41
  $ mogura start <host> -u <user> --password-base64=<password-base64-encoded> -c rules.yml -b INBOX
43
42
  ```
44
43
 
45
- You can specify a mailbox which to be monitored by `-b` option.
44
+ You can specify the mailbox by `-b` option.
46
45
 
47
- If you want to just filter mails on a specific mailbox, run the `filter` command as following.
46
+ If you want to just filter mails on the specific mailbox, use `mogura filter` command.
48
47
 
49
48
  ```console
50
49
  $ mogura filter <host> -u <user> --password-base64=<password-base64-encoded> -c rules.yml -b <mailbox>
51
50
  ```
52
51
 
53
- You can check your config by `check-config` command. It returns just OK if no errors in the config.
52
+ You can check your config by `mogura check-config` command. It returns just OK if no errors in the config.
54
53
 
55
54
  ```console
56
55
  $ mogura check-config -c rules.yml
@@ -42,7 +42,6 @@ module ImapMogura
42
42
  create_directory = options[:create_directory]
43
43
  dry_run = options[:dry_run]
44
44
 
45
- monitored_events = ["RECENT"]
46
45
  search_keys = ["RECENT", *(["UNSEEN"] if filter_unseen)]
47
46
 
48
47
  with_preparation_ready(config_name, host, port, starttls, use_ssl,
@@ -51,7 +50,8 @@ module ImapMogura
51
50
  dry_run: dry_run) do |imap_handler, rules|
52
51
  warn "* start monitoring recent mails in \"#{target_mailbox}\""
53
52
 
54
- imap_handler.monitor_events(target_mailbox, monitored_events) do
53
+ monitor_recents_on_mailbox(imap_handler, target_mailbox) do
54
+ # find mails with search keys on the target mailbox and handle them
55
55
  imap_handler.find_and_handle_mails(target_mailbox, search_keys) do |message_id|
56
56
  warn "mail (id = #{message_id} on \"#{target_mailbox}\") is recent"
57
57
 
@@ -86,13 +86,12 @@ module ImapMogura
86
86
  all_mailbox = options[:all_mailbox]
87
87
  exclude_mailboxes = options[:exclude_mailboxes]
88
88
  target_mailbox = options[:target_mailbox] unless all_mailbox
89
-
90
- raise CustomOptionError, "--all-mailbox (-a) or --target-mailbox (-b) is required" if !all_mailbox && target_mailbox.nil?
91
-
92
89
  filter_only_unseen = options[:filter_only_unseen]
93
90
  create_directory = options[:create_directory]
94
91
  dry_run = options[:dry_run]
95
92
 
93
+ raise CustomOptionError, "--all-mailbox (-a) or --target-mailbox (-b) is required" if !all_mailbox && target_mailbox.nil?
94
+
96
95
  search_keys = if filter_only_unseen
97
96
  ["UNSEEN"]
98
97
  else
@@ -107,7 +106,10 @@ module ImapMogura
107
106
  if all_mailbox
108
107
  excluded_mailboxes = options[:excluded_mailboxes]
109
108
 
110
- imap_handler.all_mailbox_list.reject { |mailbox| excluded_mailboxes.include?(mailbox) }.each do |mailbox|
109
+ # figure out target mailboxes
110
+ target_mailboxes = imap_handler.all_mailbox_list.reject { |mailbox| excluded_mailboxes.include?(mailbox) }
111
+
112
+ target_mailboxes.each do |mailbox|
111
113
  filter_mails(imap_handler, rules, mailbox, search_keys, dry_run: dry_run)
112
114
  end
113
115
  else
@@ -135,6 +137,7 @@ module ImapMogura
135
137
  starttls: starttls, usessl: use_ssl, certs: nil, verify: true,
136
138
  auth_info: { auth_type: auth_type, user: user, password: password })
137
139
 
140
+ # output all mailbox list
138
141
  puts imap_handler.all_mailbox_list
139
142
 
140
143
  imap_handler.close
@@ -174,10 +177,12 @@ module ImapMogura
174
177
  # exit
175
178
  # end
176
179
 
180
+ # create directories specified as destination
177
181
  touch_all_mailboxes_in_rules(imap_handler, rules, dry_run: dry_run) if create_directory
178
182
 
179
183
  options = { excluded_mailboxes: excluded_mailboxes }
180
184
 
185
+ # call the given block
181
186
  block[imap_handler, rules, options]
182
187
 
183
188
  imap_handler.close
@@ -208,29 +213,39 @@ module ImapMogura
208
213
  end
209
214
  end
210
215
 
216
+ def monitor_recents_on_mailbox(imap_handler, mailbox, retry_count = 0, &block)
217
+ imap_handler.monitor_events(mailbox, ["RECENT"], &block)
218
+ rescue IMAPHandler::MailFetchError => e
219
+ handle_mail_fetch_error_and_preprocess_retrying(e, retry_count)
220
+
221
+ # retry monitor recents on mailbox itself with retry count to be incremented
222
+ warn "retry monitoring mails on #{e.mailbox}..."
223
+
224
+ monitor_recents_on_mailbox(imap_handler, mailbox, retry_count + 1)
225
+ end
226
+
211
227
  def filter_mails(imap_handler, rules, mailbox, search_keys = ["ALL"], retry_count = 0, dry_run: false)
212
228
  imap_handler.find_and_handle_mails(mailbox, search_keys) do |message_id|
213
229
  filter_mail(imap_handler, rules, mailbox, message_id, dry_run: dry_run)
214
230
  end
215
231
  rescue IMAPHandler::MailFetchError => e
216
- warn "failed to fetch mail (id = #{e.message_id} on mailbox #{e.mailbox}): #{e.bad_response_error_message}"
232
+ handle_mail_fetch_error_and_preprocess_retrying(e, retry_count)
217
233
 
218
- # if retry_count is over the threshold, terminate processing
219
- unless retry_count < 3
220
- warn "retry count is over the threshold, stop processing"
234
+ # retry filter all mails itself with retry count to be incremented
235
+ warn "retry filtering all mails on #{e.mailbox}"
221
236
 
222
- return
223
- end
237
+ filter_mails(imap_handler, rules, mailbox, search_keys, retry_count + 1, dry_run: dry_run)
238
+ end
224
239
 
225
- warn "wait a moment..."
240
+ def handle_mail_fetch_error_and_preprocess_retrying(error, retry_count)
241
+ warn "failed to fetch mail (id = #{error.message_id} on mailbox #{error.mailbox}): #{error.bad_response_error_message}"
226
242
 
227
- # wait a moment...
228
- sleep 10
243
+ # if retry_count is over the threshold, abort processing
244
+ raise Thor::Error, "retry count is over the threshold, stop processing" unless retry_count < 3
229
245
 
230
- warn "retry filter all mails on #{e.mailbox}"
246
+ warn "wait a moment..."
231
247
 
232
- # retry filter all mails itself
233
- filter_mails(imap_handler, rules, mailbox, search_keys, retry_count + 1, dry_run: dry_run)
248
+ sleep 10
234
249
  end
235
250
 
236
251
  def filter_mail(imap_handler, rules, mailbox, message_id, dry_run: false)
@@ -239,27 +254,31 @@ module ImapMogura
239
254
  warn "# filtering mail on \"#{mailbox}\" of subject \"#{mail.subject}\"..."
240
255
 
241
256
  rules.each do |rule_set|
242
- dst_mailbox = rule_set.destination
243
- rule = rule_set.rule
257
+ try_to_filter_mail_for_rule_set(imap_handler, rule_set, mailbox, message_id, mail, dry_run: dry_run)
258
+ end
259
+
260
+ imap_handler.close_operation_for_mailbox(mailbox)
261
+ end
244
262
 
245
- next unless rule.match?(mail)
263
+ def try_to_filter_mail_for_rule_set(imap_handler, rule_set, mailbox, message_id, mail, dry_run: false)
264
+ dst_mailbox = rule_set.destination
265
+ rule = rule_set.rule
246
266
 
247
- warn "the mail matches for the rule of the destination \"#{dst_mailbox}\""
248
- warn "moving the mail..."
267
+ return unless rule.match?(mail)
249
268
 
250
- if dry_run
251
- warn "moving skipped because this is dry run"
269
+ warn "the mail matches for the rule of the destination \"#{dst_mailbox}\""
270
+ warn "moving the mail..."
271
+
272
+ if dry_run
273
+ warn "moving skipped because this is dry run"
274
+ else
275
+ result = imap_handler.move(mailbox, message_id, dst_mailbox)
276
+ if result
277
+ warn "moving done"
252
278
  else
253
- result = imap_handler.move(mailbox, message_id, dst_mailbox)
254
- if result
255
- warn "moving done"
256
- else
257
- warn "moving skipped because the destination is the same with the current mailbox \"#{mailbox}\""
258
- end
279
+ warn "moving skipped because the destination is the same with the current mailbox \"#{mailbox}\""
259
280
  end
260
281
  end
261
-
262
- imap_handler.close_operation_for_mailbox(mailbox)
263
282
  end
264
283
  end
265
284
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ImapMogura
4
- VERSION = "0.1.1"
4
+ VERSION = "0.2.2.pre.dev"
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: imap_mogura
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.2.pre.dev
5
5
  platform: ruby
6
6
  authors:
7
7
  - ysk
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-11-19 00:00:00.000000000 Z
11
+ date: 2024-11-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: base64