imap_guard 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/.ruby-version +1 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +65 -0
- data/Rakefile +1 -0
- data/imap_guard.gemspec +25 -0
- data/lib/imap_guard.rb +6 -0
- data/lib/imap_guard/guard.rb +71 -0
- data/lib/imap_guard/query.rb +50 -0
- data/lib/imap_guard/version.rb +3 -0
- metadata +111 -0
data/.gitignore
ADDED
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
1.9.3
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Cédric Félizard
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,65 @@
|
|
1
|
+
# ImapGuard
|
2
|
+
|
3
|
+
TODO: Write a gem description
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'imap_guard'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install imap_guard
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
TODO: Write usage instructions here
|
22
|
+
|
23
|
+
|
24
|
+
```ruby
|
25
|
+
require 'imap_guard'
|
26
|
+
load File.expand_path('../settings.rb', __FILE__)
|
27
|
+
settings = SETTINGS.merge({ read_only: true })
|
28
|
+
|
29
|
+
s = IMAPGuard::Guard.new settings
|
30
|
+
s.select 'INBOX.ops'
|
31
|
+
|
32
|
+
query = IMAPGuard::Query.new.before(7).subject("abc").from("root")
|
33
|
+
s.delete query
|
34
|
+
|
35
|
+
pattern = "monit alert -- Resource limit "
|
36
|
+
query = IMAPGuard::Query.new.subject(pattern).before(7)
|
37
|
+
s.delete query do |mail|
|
38
|
+
# the pattern given to subject() is a mere string
|
39
|
+
# pass an optional block to perform advanced filtering such as regexp matching
|
40
|
+
# the yielded mail object is an instance of the current mail providing many methods
|
41
|
+
mail.subject.start_with? pattern
|
42
|
+
end
|
43
|
+
|
44
|
+
query = IMAPGuard::Query.new.before(7).subject("Logwatch for ")
|
45
|
+
s.delete query do |mail|
|
46
|
+
mail.subject =~ /\ALogwatch for \w \(Linux\)\Z/
|
47
|
+
end
|
48
|
+
|
49
|
+
# You can also forge your own raw IMAP search queries like this
|
50
|
+
query = 'SEEN SUBJECT "ALERT" FROM "root"'
|
51
|
+
s.delete query do |mail|
|
52
|
+
mail.subject == "ALERT" and \
|
53
|
+
mail.body == "ALERT"
|
54
|
+
end
|
55
|
+
|
56
|
+
s.close
|
57
|
+
```
|
58
|
+
|
59
|
+
## Contributing
|
60
|
+
|
61
|
+
1. Fork it
|
62
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
63
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
64
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
65
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/imap_guard.gemspec
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'imap_guard/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "imap_guard"
|
8
|
+
spec.version = ImapGuard::VERSION
|
9
|
+
spec.authors = ["Cédric Félizard"]
|
10
|
+
spec.email = ["cedric@felizard.fr"]
|
11
|
+
spec.description = %q{A guard for your IMAP server}
|
12
|
+
spec.summary = spec.description
|
13
|
+
spec.homepage = ""
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_dependency 'mail', '~> 2.5.3'
|
22
|
+
|
23
|
+
spec.add_development_dependency 'bundler', '~> 1.3'
|
24
|
+
spec.add_development_dependency 'rake'
|
25
|
+
end
|
data/lib/imap_guard.rb
ADDED
@@ -0,0 +1,71 @@
|
|
1
|
+
require 'net/imap'
|
2
|
+
require 'mail'
|
3
|
+
require 'ostruct'
|
4
|
+
|
5
|
+
module IMAPGuard
|
6
|
+
class Guard
|
7
|
+
def initialize settings
|
8
|
+
@settings = OpenStruct.new(settings).freeze
|
9
|
+
|
10
|
+
# http://www.ruby-doc.org/stdlib-1.9.3/libdoc/net/imap/rdoc/Net/IMAP.html#method-c-new
|
11
|
+
@imap = Net::IMAP.new(@settings.host, @settings.port, true, nil, false)
|
12
|
+
@imap.login(@settings.username, @settings.password)
|
13
|
+
end
|
14
|
+
|
15
|
+
def select mailbox
|
16
|
+
if @settings.read_only
|
17
|
+
@imap.examine(mailbox) # open in read-only
|
18
|
+
else
|
19
|
+
@imap.select(mailbox) # open in read-write
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def delete query
|
24
|
+
unless query.is_a? String
|
25
|
+
if query.respond_to? :to_s
|
26
|
+
query = query.to_s
|
27
|
+
else
|
28
|
+
raise ArgumentError, "query must provide #to_s"
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
messages = @imap.search query
|
33
|
+
count = messages.size
|
34
|
+
puts "Query: #{query.inspect}: #{count} results"
|
35
|
+
|
36
|
+
counter = 0
|
37
|
+
messages.each do |message_id|
|
38
|
+
counter += 1
|
39
|
+
# puts message_id
|
40
|
+
mail = nil
|
41
|
+
if block_given?
|
42
|
+
msg = @imap.fetch(message_id, 'RFC822')[0].attr['RFC822']
|
43
|
+
mail = Mail.read_from_string msg
|
44
|
+
result = yield mail
|
45
|
+
if result
|
46
|
+
# puts "Given filter matched"
|
47
|
+
else
|
48
|
+
# puts "Given filter returned falsy"
|
49
|
+
next
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
print "Deleting UID #{message_id} (#{counter}/#{count})"
|
54
|
+
print " (DRY-RUN)" if @settings.read_only
|
55
|
+
if mail
|
56
|
+
puts ": #{mail.subject} (#{mail.body.to_s.length})"
|
57
|
+
else
|
58
|
+
puts
|
59
|
+
end
|
60
|
+
@imap.store(message_id, "+FLAGS", [:Deleted])
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
def close
|
65
|
+
puts "Expunging deleted messages and closing mailbox..."
|
66
|
+
# p imap.expunge
|
67
|
+
@imap.close
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
@@ -0,0 +1,50 @@
|
|
1
|
+
module IMAPGuard
|
2
|
+
class Query
|
3
|
+
def initialize
|
4
|
+
@criteria = []
|
5
|
+
seen.unanswered.unflagged
|
6
|
+
end
|
7
|
+
|
8
|
+
def to_s
|
9
|
+
@criteria.join ' '
|
10
|
+
end
|
11
|
+
|
12
|
+
def seen
|
13
|
+
@criteria << 'SEEN'
|
14
|
+
self
|
15
|
+
end
|
16
|
+
|
17
|
+
def unanswered
|
18
|
+
@criteria << 'UNANSWERED'
|
19
|
+
self
|
20
|
+
end
|
21
|
+
|
22
|
+
def unflagged
|
23
|
+
@criteria << 'UNFLAGGED'
|
24
|
+
self
|
25
|
+
end
|
26
|
+
|
27
|
+
def subject string
|
28
|
+
@criteria << "SUBJECT \"#{string}\""
|
29
|
+
self
|
30
|
+
end
|
31
|
+
|
32
|
+
def from string
|
33
|
+
@criteria << "FROM \"#{string}\""
|
34
|
+
self
|
35
|
+
end
|
36
|
+
|
37
|
+
def before date
|
38
|
+
case date
|
39
|
+
when Fixnum
|
40
|
+
date = (Date.today - date).strftime '%e-%b-%Y'
|
41
|
+
when Date
|
42
|
+
date = Date.strptime date, '%e-%b-%Y'
|
43
|
+
end
|
44
|
+
|
45
|
+
@criteria << "BEFORE #{date}"
|
46
|
+
self
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
metadata
ADDED
@@ -0,0 +1,111 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: imap_guard
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Cédric Félizard
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-03-19 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: mail
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 2.5.3
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ~>
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: 2.5.3
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: bundler
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ~>
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '1.3'
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ~>
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '1.3'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: rake
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
description: A guard for your IMAP server
|
63
|
+
email:
|
64
|
+
- cedric@felizard.fr
|
65
|
+
executables: []
|
66
|
+
extensions: []
|
67
|
+
extra_rdoc_files: []
|
68
|
+
files:
|
69
|
+
- .gitignore
|
70
|
+
- .ruby-version
|
71
|
+
- Gemfile
|
72
|
+
- LICENSE.txt
|
73
|
+
- README.md
|
74
|
+
- Rakefile
|
75
|
+
- imap_guard.gemspec
|
76
|
+
- lib/imap_guard.rb
|
77
|
+
- lib/imap_guard/guard.rb
|
78
|
+
- lib/imap_guard/query.rb
|
79
|
+
- lib/imap_guard/version.rb
|
80
|
+
homepage: ''
|
81
|
+
licenses:
|
82
|
+
- MIT
|
83
|
+
post_install_message:
|
84
|
+
rdoc_options: []
|
85
|
+
require_paths:
|
86
|
+
- lib
|
87
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
88
|
+
none: false
|
89
|
+
requirements:
|
90
|
+
- - ! '>='
|
91
|
+
- !ruby/object:Gem::Version
|
92
|
+
version: '0'
|
93
|
+
segments:
|
94
|
+
- 0
|
95
|
+
hash: -502947235
|
96
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
97
|
+
none: false
|
98
|
+
requirements:
|
99
|
+
- - ! '>='
|
100
|
+
- !ruby/object:Gem::Version
|
101
|
+
version: '0'
|
102
|
+
segments:
|
103
|
+
- 0
|
104
|
+
hash: -502947235
|
105
|
+
requirements: []
|
106
|
+
rubyforge_project:
|
107
|
+
rubygems_version: 1.8.25
|
108
|
+
signing_key:
|
109
|
+
specification_version: 3
|
110
|
+
summary: A guard for your IMAP server
|
111
|
+
test_files: []
|