imap_mogura 0.1.1.pre.dev → 0.2.0

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: 63948c26de8c10fedd38588189779fd6eeee167f3ab8fe54be42ab3ae37eac76
4
- data.tar.gz: 1b86c8501895ea103f4627a34cb3057eda23f42d0722aeceb2d6ea1630ed7472
3
+ metadata.gz: ccc5914a45fede7ba28883f41ef95bba9e0f426c7a4a563a926dd7dcbb3c17f1
4
+ data.tar.gz: '069f651ef1fee7cb2f5a5f3547a7c13eca03646c46f47a3d298ba3b21a8db6a8'
5
5
  SHA512:
6
- metadata.gz: 902ce048023623971dd7c26e5512888eea1275e1160895f8fa0d6a36d91386f76e8f5d3f3a3b2cef42709b0d76d77bd08cf8c4381ea03d4dbaa62a9691b25b82
7
- data.tar.gz: 72055ee14145ec3a81ccfe02f50bc903f060368e933fbce27e63f0d45ffdd5f0ec1829ffa054ec4f0179d77bec9f4948c4dc372d408943f1332f7973b81e12ef
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
- Check out this repository first and enter the directory.
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
- $ git clone https://github.com/yskuniv/imap_mogura.git
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
- As following, `start` command will start monitoring RECENT mails on "INBOX". If a mail is coming
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 a mailbox which to be monitored by `-b` option.
42
+ You can specify the mailbox by `-b` option.
49
43
 
50
- 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.
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
@@ -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
- with_all_preparation_ready(config_name, host, port, starttls, use_ssl,
49
- auth_info: { auth_type: auth_type, user: user, password: password },
50
- create_directory: create_directory,
51
- dry_run: dry_run) do |imap_handler, rules|
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.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,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
- with_all_preparation_ready(config_name, host, port, starttls, use_ssl,
103
- auth_info: { auth_type: auth_type, user: user, password: password },
104
- excluded_mailboxes: exclude_mailboxes,
105
- create_directory: create_directory,
106
- dry_run: dry_run) do |imap_handler, rules, options|
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 with_all_preparation_ready(config_name,
156
- host, port,
157
- starttls, use_ssl, certs: nil, verify: true,
158
- auth_info: nil,
159
- excluded_mailboxes: [],
160
- create_directory: true,
161
- dry_run: false, &block)
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
- 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)
216
226
 
217
- # if retry_count is over the threshold, terminate processing
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
- return
222
- 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
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)
@@ -35,7 +35,7 @@ module ImapMogura
35
35
 
36
36
  class FieldMatcher < RuleElement
37
37
  def initialize(regexp)
38
- @regexp = /#{regexp}/ # FIXME: need to care about malicious regular expression injection
38
+ @regexp = Regexp.new(regexp)
39
39
 
40
40
  super()
41
41
  end
@@ -64,6 +64,8 @@ module ImapMogura
64
64
  else
65
65
  GeneralFieldMatcher.new(k, rule[k])
66
66
  end
67
+ rescue RegexpError
68
+ raise ParseError, "illegal regular expression: #{rule[k].inspect}"
67
69
  end
68
70
 
69
71
  def parse_rule_list(rule_list)
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ImapMogura
4
- VERSION = "0.1.1.pre.dev"
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.pre.dev
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-18 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