imap_mogura 0.1.1.pre.dev → 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 +4 -4
- data/README.md +6 -12
- data/lib/imap_mogura/cli.rb +42 -32
- data/lib/imap_mogura/rules_parser/rule_elements.rb +1 -1
- data/lib/imap_mogura/rules_parser.rb +2 -0
- data/lib/imap_mogura/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ccc5914a45fede7ba28883f41ef95bba9e0f426c7a4a563a926dd7dcbb3c17f1
|
4
|
+
data.tar.gz: '069f651ef1fee7cb2f5a5f3547a7c13eca03646c46f47a3d298ba3b21a8db6a8'
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0cfac9d2da8aca0e2d8ec59775699ebea6038d70e3308522a067eccb5b80150116200796071ac23713af8f4a3b4224b8fdc940e67601f1f00ee4c1599fe211ec
|
7
|
+
data.tar.gz: 7b89c3160fd326abdac3ed32d019adbfe18870a7aba4159411e974cac4e408f9aead8f52f28f187d245c9bb60ce0f6c166a273ac5ac4f1e5c292f891dbaaf2f7
|
data/README.md
CHANGED
@@ -4,17 +4,12 @@ A mail filtering tool for IMAP.
|
|
4
4
|
|
5
5
|
## Installation
|
6
6
|
|
7
|
-
|
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
|
-
$
|
11
|
-
$ cd imap_mogura/
|
10
|
+
$ gem install imap_mogura
|
12
11
|
```
|
13
12
|
|
14
|
-
Install this gem and add to the application's Gemfile by executing:
|
15
|
-
|
16
|
-
$ bundle install
|
17
|
-
|
18
13
|
## Usage
|
19
14
|
|
20
15
|
Create `rules.yml` and write rules like following.
|
@@ -38,22 +33,21 @@ rules:
|
|
38
33
|
subject: "i'm trash-like email!!"
|
39
34
|
```
|
40
35
|
|
41
|
-
|
42
|
-
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.
|
43
37
|
|
44
38
|
```console
|
45
39
|
$ mogura start <host> -u <user> --password-base64=<password-base64-encoded> -c rules.yml -b INBOX
|
46
40
|
```
|
47
41
|
|
48
|
-
You can specify
|
42
|
+
You can specify the mailbox by `-b` option.
|
49
43
|
|
50
|
-
If you want to just filter mails on
|
44
|
+
If you want to just filter mails on the specific mailbox, use `mogura filter` command.
|
51
45
|
|
52
46
|
```console
|
53
47
|
$ mogura filter <host> -u <user> --password-base64=<password-base64-encoded> -c rules.yml -b <mailbox>
|
54
48
|
```
|
55
49
|
|
56
|
-
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.
|
57
51
|
|
58
52
|
```console
|
59
53
|
$ mogura check-config -c rules.yml
|
data/lib/imap_mogura/cli.rb
CHANGED
@@ -42,16 +42,15 @@ 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
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
47
|
+
with_preparation_ready(config_name, host, port, starttls, use_ssl,
|
48
|
+
auth_info: { auth_type: auth_type, user: user, password: password },
|
49
|
+
create_directory: create_directory,
|
50
|
+
dry_run: dry_run) do |imap_handler, rules|
|
52
51
|
warn "* start monitoring recent mails in \"#{target_mailbox}\""
|
53
52
|
|
54
|
-
imap_handler
|
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,24 +85,23 @@ 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
|
99
97
|
["ALL"]
|
100
98
|
end
|
101
99
|
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
100
|
+
with_preparation_ready(config_name, host, port, starttls, use_ssl,
|
101
|
+
auth_info: { auth_type: auth_type, user: user, password: password },
|
102
|
+
excluded_mailboxes: exclude_mailboxes,
|
103
|
+
create_directory: create_directory,
|
104
|
+
dry_run: dry_run) do |imap_handler, rules, options|
|
107
105
|
if all_mailbox
|
108
106
|
excluded_mailboxes = options[:excluded_mailboxes]
|
109
107
|
|
@@ -152,13 +150,14 @@ module ImapMogura
|
|
152
150
|
|
153
151
|
private
|
154
152
|
|
155
|
-
def
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
153
|
+
def with_preparation_ready(config_name,
|
154
|
+
host, port,
|
155
|
+
starttls, use_ssl, certs: nil, verify: true,
|
156
|
+
auth_info: nil,
|
157
|
+
excluded_mailboxes: [],
|
158
|
+
create_directory: true,
|
159
|
+
dry_run: false,
|
160
|
+
&block)
|
162
161
|
_, rules = load_and_handle_config(config_name)
|
163
162
|
|
164
163
|
warn "* connecting the server #{host}:#{port}..."
|
@@ -207,29 +206,40 @@ module ImapMogura
|
|
207
206
|
end
|
208
207
|
end
|
209
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
|
+
|
210
220
|
def filter_mails(imap_handler, rules, mailbox, search_keys = ["ALL"], retry_count = 0, dry_run: false)
|
211
221
|
imap_handler.find_and_handle_mails(mailbox, search_keys) do |message_id|
|
212
222
|
filter_mail(imap_handler, rules, mailbox, message_id, dry_run: dry_run)
|
213
223
|
end
|
214
224
|
rescue IMAPHandler::MailFetchError => e
|
215
|
-
|
225
|
+
handle_mail_fetch_error_and_preprocess_retrying(e, retry_count)
|
216
226
|
|
217
|
-
|
218
|
-
unless retry_count < 3
|
219
|
-
warn "retry count is over the threshold, stop processing"
|
227
|
+
warn "retry filtering all mails on #{e.mailbox}"
|
220
228
|
|
221
|
-
|
222
|
-
|
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
|
223
238
|
|
224
239
|
warn "wait a moment..."
|
225
240
|
|
226
241
|
# wait a moment...
|
227
242
|
sleep 10
|
228
|
-
|
229
|
-
warn "retry filter all mails on #{e.mailbox}"
|
230
|
-
|
231
|
-
# retry filter all mails itself
|
232
|
-
filter_mails(imap_handler, rules, mailbox, search_keys, retry_count + 1, dry_run: dry_run)
|
233
243
|
end
|
234
244
|
|
235
245
|
def filter_mail(imap_handler, rules, mailbox, message_id, dry_run: false)
|
data/lib/imap_mogura/version.rb
CHANGED
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.
|
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-
|
11
|
+
date: 2024-11-22 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: base64
|