imap_mogura 0.1.1 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 3f5a65d48e4e81a37e519995c39cddf533e3336477c358044462f51935ee1d0f
4
- data.tar.gz: 59975c34bdd3ce2d372c71c5d6b8d9689867e431956104df8cd958294a18dc3d
3
+ metadata.gz: ccc5914a45fede7ba28883f41ef95bba9e0f426c7a4a563a926dd7dcbb3c17f1
4
+ data.tar.gz: '069f651ef1fee7cb2f5a5f3547a7c13eca03646c46f47a3d298ba3b21a8db6a8'
5
5
  SHA512:
6
- metadata.gz: c29a3b716cfde67b59876ad149add941ac2db45fa92154fbaa0a3587e438939568ae6eb536b12ddcc28bd14c0ab131a3f4a5725d0e11775cc3f1a95d30400b84
7
- data.tar.gz: fc9c28711eb63e293e09173a3b4eac353e5ca40d4b3d7b01b07ea5f571c80d941d669c1bd93217c21d179a16ff59b116996f0a581b80ad883c5875a590e2f8b2
6
+ metadata.gz: 0cfac9d2da8aca0e2d8ec59775699ebea6038d70e3308522a067eccb5b80150116200796071ac23713af8f4a3b4224b8fdc940e67601f1f00ee4c1599fe211ec
7
+ data.tar.gz: 7b89c3160fd326abdac3ed32d019adbfe18870a7aba4159411e974cac4e408f9aead8f52f28f187d245c9bb60ce0f6c166a273ac5ac4f1e5c292f891dbaaf2f7
data/README.md CHANGED
@@ -4,14 +4,12 @@ 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
- $ gem install imap_mogura --pre
10
+ $ gem install imap_mogura
11
11
  ```
12
12
 
13
- (This gem has only released pre-release versions now, so you must specify `--pre` option.)
14
-
15
13
  ## Usage
16
14
 
17
15
  Create `rules.yml` and write rules like following.
@@ -35,22 +33,21 @@ rules:
35
33
  subject: "i'm trash-like email!!"
36
34
  ```
37
35
 
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.
36
+ 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
37
 
41
38
  ```console
42
39
  $ mogura start <host> -u <user> --password-base64=<password-base64-encoded> -c rules.yml -b INBOX
43
40
  ```
44
41
 
45
- You can specify a mailbox which to be monitored by `-b` option.
42
+ You can specify the mailbox by `-b` option.
46
43
 
47
- If you want to just filter mails on a specific mailbox, run the `filter` command as following.
44
+ If you want to just filter mails on the specific mailbox, use `mogura filter` command.
48
45
 
49
46
  ```console
50
47
  $ mogura filter <host> -u <user> --password-base64=<password-base64-encoded> -c rules.yml -b <mailbox>
51
48
  ```
52
49
 
53
- You can check your config by `check-config` command. It returns just OK if no errors in the config.
50
+ You can check your config by `mogura check-config` command. It returns just OK if no errors in the config.
54
51
 
55
52
  ```console
56
53
  $ 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,7 @@ 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
55
54
  imap_handler.find_and_handle_mails(target_mailbox, search_keys) do |message_id|
56
55
  warn "mail (id = #{message_id} on \"#{target_mailbox}\") is recent"
57
56
 
@@ -86,13 +85,12 @@ module ImapMogura
86
85
  all_mailbox = options[:all_mailbox]
87
86
  exclude_mailboxes = options[:exclude_mailboxes]
88
87
  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
88
  filter_only_unseen = options[:filter_only_unseen]
93
89
  create_directory = options[:create_directory]
94
90
  dry_run = options[:dry_run]
95
91
 
92
+ raise CustomOptionError, "--all-mailbox (-a) or --target-mailbox (-b) is required" if !all_mailbox && target_mailbox.nil?
93
+
96
94
  search_keys = if filter_only_unseen
97
95
  ["UNSEEN"]
98
96
  else
@@ -208,29 +206,40 @@ module ImapMogura
208
206
  end
209
207
  end
210
208
 
209
+ def monitor_recents_on_mailbox(imap_handler, mailbox, retry_count = 0, &block)
210
+ imap_handler.monitor_events(mailbox, ["RECENT"], &block)
211
+ rescue IMAPHandler::MailFetchError => e
212
+ handle_mail_fetch_error_and_preprocess_retrying(e, retry_count)
213
+
214
+ warn "retry monitoring mails on #{e.mailbox}..."
215
+
216
+ # retry monitor recents on mailbox itself with retry count to be incremented
217
+ monitor_recents_on_mailbox(imap_handler, mailbox, retry_count + 1)
218
+ end
219
+
211
220
  def filter_mails(imap_handler, rules, mailbox, search_keys = ["ALL"], retry_count = 0, dry_run: false)
212
221
  imap_handler.find_and_handle_mails(mailbox, search_keys) do |message_id|
213
222
  filter_mail(imap_handler, rules, mailbox, message_id, dry_run: dry_run)
214
223
  end
215
224
  rescue IMAPHandler::MailFetchError => e
216
- warn "failed to fetch mail (id = #{e.message_id} on mailbox #{e.mailbox}): #{e.bad_response_error_message}"
225
+ handle_mail_fetch_error_and_preprocess_retrying(e, retry_count)
217
226
 
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"
227
+ warn "retry filtering all mails on #{e.mailbox}"
221
228
 
222
- return
223
- end
229
+ # retry filter all mails itself with retry count to be incremented
230
+ filter_mails(imap_handler, rules, mailbox, search_keys, retry_count + 1, dry_run: dry_run)
231
+ end
232
+
233
+ def handle_mail_fetch_error_and_preprocess_retrying(error, retry_count)
234
+ warn "failed to fetch mail (id = #{error.message_id} on mailbox #{error.mailbox}): #{error.bad_response_error_message}"
235
+
236
+ # if retry_count is over the threshold, abort processing
237
+ raise Thor::Error, "retry count is over the threshold, stop processing" unless retry_count < 3
224
238
 
225
239
  warn "wait a moment..."
226
240
 
227
241
  # wait a moment...
228
242
  sleep 10
229
-
230
- warn "retry filter all mails on #{e.mailbox}"
231
-
232
- # retry filter all mails itself
233
- filter_mails(imap_handler, rules, mailbox, search_keys, retry_count + 1, dry_run: dry_run)
234
243
  end
235
244
 
236
245
  def filter_mail(imap_handler, rules, mailbox, message_id, dry_run: false)
@@ -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.0"
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.0
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-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: base64