imap_clear 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 3e32bbcd47c4d4f522bf4ca569795ebac1c14ed8
4
+ data.tar.gz: e3c066de9dfacc644e93b701bde00a70ac794f8c
5
+ SHA512:
6
+ metadata.gz: c15c803121eb6af9bd54176b2fa548688c6fb90706eb984c3253eed6cfa7f1f345efef8c3a5cd87529a2642e723d7d597fb878153c61b81e3c1b13864f189555
7
+ data.tar.gz: 4a5bfac24fff65adb54c437a867810789312c2f0650ff934f0eb62a2d7d4af2f2698a536c760ce730c1e3b373a799f7eacebbb54aff40893cec4cfed7fc7f013
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+
11
+ .envsetup
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.1
5
+ before_install: gem install bundler -v 1.13.6
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in imap_clear.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Jeff McAffee
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,55 @@
1
+ # ImapClear
2
+
3
+ `imap_clear` is a command line application to delete emails on an IMAP server.
4
+ It is designed to be called from a cron job on a periodic basis.
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ ```ruby
11
+ gem 'imap_clear'
12
+ ```
13
+
14
+ And then execute:
15
+
16
+ $ bundle
17
+
18
+ Or install it yourself as:
19
+
20
+ $ gem install imap_clear
21
+
22
+ ## Usage
23
+
24
+ From the command line:
25
+
26
+ imap_clear server-host username password
27
+
28
+ Example:
29
+
30
+ imap_clear mail.example.com user@example.com userpassword
31
+
32
+
33
+ ## Development
34
+
35
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
36
+
37
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
38
+
39
+ ## Contributing
40
+
41
+ Bug reports and pull requests are welcome on GitHub at https://github.com/jmcaffee/imap_clear.
42
+
43
+ 1. Fork it ( https://github.com/jmcaffee/imap_clear/fork )
44
+ 1. Clone it (`git clone git@github.com:[my-github-user-name]/imap_clear.git`)
45
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
46
+ 3. Create tests for your feature branch
47
+ 4. Commit your changes (`git commit -am 'Add some feature'`)
48
+ 5. Push to the branch (`git push origin my-new-feature`)
49
+ 6. Create a new Pull Request
50
+
51
+
52
+ ## License
53
+
54
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
55
+
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,110 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "imap_clear"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ def get_app the_host, the_user, the_pass
10
+ app = ImapClear::App.new
11
+ app.host = the_host
12
+ app.port = 993
13
+ app.user = the_user
14
+ app.pass = the_pass
15
+
16
+ app
17
+ end
18
+
19
+ def connect_to_server
20
+ raise ArgumentError, "IMAPCLEAN_SERVER env var not set" if ENV["IMAPCLEAN_SERVER"].nil?
21
+ raise ArgumentError, "IMAPCLEAN_USER env var not set" if ENV["IMAPCLEAN_USER"].nil?
22
+ raise ArgumentError, "IMAPCLEAN_PASS env var not set" if ENV["IMAPCLEAN_PASS"].nil?
23
+
24
+ app = get_app(ENV["IMAPCLEAN_SERVER"], ENV["IMAPCLEAN_USER"], ENV["IMAPCLEAN_PASS"])
25
+
26
+ app.connect
27
+ app.login
28
+
29
+ app
30
+ end
31
+
32
+ def print text
33
+ $stdout.print text; $stdout.flush
34
+ end
35
+
36
+ def daemon_email
37
+ ENV["IMAPCLEAN_DAEMON_EMAIL"]
38
+ end
39
+
40
+ def uids_from_daemon app
41
+ app.get_uids_where_from(daemon_email)
42
+ end
43
+
44
+ def uids_of_bodies_with_invalid_email app
45
+ app.get_uids_where_body_contains "#{ENV["IMAPCLEAN_USER"]}@"
46
+ end
47
+
48
+ def emails_to_clean_count
49
+ app = connect_to_server
50
+
51
+ print "#{uids_from_daemon(app).count} emails to clean\n"
52
+
53
+ app.disconnect
54
+ end
55
+
56
+ def invalid_emails_to_clean_count
57
+ app = connect_to_server
58
+
59
+ print "#{uids_of_bodies_with_invalid_email(app).count} emails to clean\n"
60
+
61
+ app.disconnect
62
+ end
63
+
64
+ def clean_server
65
+ clean_emails_from_daemon
66
+
67
+ clean_emails_to_user
68
+ end
69
+
70
+ def clean_emails_from_daemon
71
+ app = connect_to_server
72
+ uids = uids_from_daemon(app)
73
+ start_count = uids.count
74
+
75
+ print "\nCleaning emails from #{daemon_email}.\n"
76
+ app.delete_uids uids
77
+
78
+ app.expunge
79
+
80
+ uids = uids_from_daemon(app)
81
+ final_count = uids.count
82
+
83
+ print "\nDeleted #{start_count - final_count} of #{start_count} emails."
84
+ app.disconnect
85
+ end
86
+
87
+ def clean_emails_to_user
88
+ app = connect_to_server
89
+ uids = uids_of_bodies_with_invalid_email(app)
90
+ start_count = uids.count
91
+
92
+ print "\nCleaning emails containing #{ENV["IMAPCLEAN_USER"]}@nnn.nnn.nnn.nnn.\n"
93
+ app.delete_uids uids
94
+
95
+ app.expunge
96
+
97
+ uids = uids_of_bodies_with_invalid_email(app)
98
+ final_count = uids.count
99
+
100
+ print "\nDeleted #{start_count - final_count} of #{start_count} emails."
101
+ app.disconnect
102
+ end
103
+
104
+ # (If you use this, don't forget to add pry to your Gemfile!)
105
+ require "pry"
106
+ Pry.start
107
+
108
+ #require "irb"
109
+ #IRB.start
110
+
data/bin/setup ADDED
@@ -0,0 +1,61 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ #set -vx
5
+
6
+ bundle install
7
+
8
+ OUTFILE=.envsetup # Name of the env value file
9
+
10
+ write_envsetup() {
11
+
12
+ # Write an example .envsetup file
13
+
14
+
15
+ # -----------------------------------------------------------
16
+ (
17
+ cat <<'EOF'
18
+ #!/bin/bash
19
+ # vi: ft=shell
20
+
21
+ export IMAPCLEAN_SERVER=mail.example.com
22
+ export IMAPCLEAN_USER=john.doe@example.com
23
+ export IMAPCLEAN_PASS=ENTER_SECRET_HERE
24
+
25
+ export IMAPCLEAN_DAEMON_EMAIL=MAILER-DAEMON@envious.nexcess.net
26
+
27
+ # Uncomment these and modify if needed (defaults are shown)
28
+ #export IMAPCLEAN_INBOX=INBOX
29
+ #export IMAPCLEAN_TRASHBOX=INBOX.Trash
30
+
31
+ # Use this file by sourcing it before running app:
32
+ # ex:
33
+ # source .envsetup && ./exe/imap_clean
34
+ #
35
+ # or:
36
+ # . .envsetup && ./exe/imap_clean
37
+ #
38
+ EOF
39
+ ) > $OUTFILE
40
+
41
+ echo
42
+ echo
43
+ echo "$OUTFILE has been generated."
44
+ echo "Edit $OUTFILE and replace the SERVER, USER and PASS values with credentials from your mail server."
45
+ echo
46
+ echo "Use $OUTFILE as:"
47
+ echo
48
+ echo "source $OUTFILE && ./exe/imap_clean"
49
+ echo
50
+ }
51
+
52
+ # Create an .envsetup file if it doesn't exist.
53
+ if [ ! -f $OUTFILE ]; then
54
+ write_envsetup
55
+ fi
56
+
57
+ echo
58
+ echo
59
+ echo
60
+ echo "ImapClean setup complete. Confirm there are no errors."
61
+ echo
data/exe/imap_clear ADDED
@@ -0,0 +1,138 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "imap_clear"
5
+
6
+ $host = nil
7
+ $user = nil
8
+ $pass = nil
9
+
10
+ def process_cmd_line_args
11
+ arg_errors = false
12
+ errs = []
13
+
14
+ $host = ARGV.shift
15
+ $user = ARGV.shift
16
+ $pass = ARGV.shift
17
+
18
+ if $host.nil?
19
+ arg_errors = true
20
+ errs << "Missing server host address (mail.example.com)"
21
+ end
22
+
23
+ if $user.nil?
24
+ arg_errors = true
25
+ errs << "Missing user name (user@example.com)"
26
+ end
27
+
28
+ if $pass.nil?
29
+ arg_errors = true
30
+ errs << "Missing user password"
31
+ end
32
+
33
+ if ARGV.count > 0
34
+ arg_errors = true
35
+ errs << "Unexpected arguments: #{ARGV.join("|")}"
36
+ end
37
+
38
+ if arg_errors
39
+ raise ArgumentError, "\n" + errs.join("\n")
40
+ end
41
+ end
42
+
43
+ def get_app the_host, the_user, the_pass
44
+ app = ImapClear::App.new
45
+ app.host = the_host
46
+ app.port = 993
47
+ app.user = the_user
48
+ app.pass = the_pass
49
+
50
+ app
51
+ end
52
+
53
+ def connect_to_server
54
+ app = get_app($host, $user, $pass)
55
+
56
+ app.connect
57
+ app.login
58
+
59
+ app
60
+ end
61
+
62
+ def print text
63
+ $stdout.print text; $stdout.flush
64
+ end
65
+
66
+ def daemon_email
67
+ ENV["IMAPCLEAN_DAEMON_EMAIL"]
68
+ end
69
+
70
+ def username
71
+ $user
72
+ end
73
+
74
+ def uids_from_daemon app
75
+ app.get_uids_where_from(daemon_email)
76
+ end
77
+
78
+ def uids_of_bodies_with_invalid_email app
79
+ app.get_uids_where_body_contains "#{username}@"
80
+ end
81
+
82
+ def emails_to_clean_count
83
+ app = connect_to_server
84
+
85
+ print "#{uids_from_daemon(app).count} emails to clean\n"
86
+
87
+ app.disconnect
88
+ end
89
+
90
+ def invalid_emails_to_clean_count
91
+ app = connect_to_server
92
+
93
+ print "#{uids_of_bodies_with_invalid_email(app).count} emails to clean\n"
94
+
95
+ app.disconnect
96
+ end
97
+
98
+ def clean_server
99
+ clean_emails_from_daemon
100
+
101
+ clean_emails_to_user
102
+ end
103
+
104
+ def clean_emails_from_daemon
105
+ app = connect_to_server
106
+ uids = uids_from_daemon(app)
107
+ start_count = uids.count
108
+
109
+ print "\nCleaning emails from #{daemon_email}.\n"
110
+ app.delete_uids uids
111
+
112
+ app.expunge
113
+
114
+ final_count = uids_from_daemon(app).count
115
+
116
+ print "\nDeleted #{start_count - final_count} of #{start_count} emails.\n"
117
+ app.disconnect
118
+ end
119
+
120
+ def clean_emails_to_user
121
+ app = connect_to_server
122
+ uids = uids_of_bodies_with_invalid_email(app)
123
+ start_count = uids.count
124
+
125
+ print "\nCleaning emails containing #{username}@nnn.nnn.nnn.nnn.\n"
126
+ app.delete_uids uids
127
+
128
+ app.expunge
129
+
130
+ final_count = uids_of_bodies_with_invalid_email(app).count
131
+
132
+ print "\nDeleted #{start_count - final_count} of #{start_count} emails.\n"
133
+ app.disconnect
134
+ end
135
+
136
+
137
+ process_cmd_line_args
138
+ clean_server
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'imap_clear/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "imap_clear"
8
+ spec.version = ImapClear::VERSION
9
+ spec.authors = ["Jeff McAffee"]
10
+ spec.email = ["jeff@ktechsystems.com"]
11
+
12
+ spec.summary = %q{Clear out IMAP emailbox}
13
+ spec.description = spec.summary
14
+ spec.homepage = "https://github.com/jmcaffee/imap_clear"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
18
+ f.match(%r{^(test|spec|features)/})
19
+ end
20
+ spec.bindir = "exe"
21
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
+ spec.require_paths = ["lib"]
23
+
24
+ spec.add_development_dependency "bundler", "~> 1.13"
25
+ spec.add_development_dependency "rake", "~> 10.0"
26
+ spec.add_development_dependency "rspec", "~> 3.0"
27
+ spec.add_development_dependency "pry"
28
+ end
data/lib/imap_clear.rb ADDED
@@ -0,0 +1,8 @@
1
+ require "imap_clear/version"
2
+
3
+ module ImapClear
4
+ # Your code goes here...
5
+ end
6
+
7
+ require 'imap_clear/app.rb'
8
+
@@ -0,0 +1,96 @@
1
+ ##############################################################################
2
+ # File:: app.rb
3
+ # Purpose:: filedescription
4
+ #
5
+ # Author:: Jeff McAffee 11/13/2016
6
+ # Copyright:: Copyright (c) 2016, kTech Systems LLC. All rights reserved.
7
+ # Website:: http://ktechsystems.com
8
+ ##############################################################################
9
+
10
+ require 'net/imap'
11
+
12
+ module ImapClear
13
+ class App
14
+
15
+ attr_accessor :host
16
+ attr_accessor :port
17
+ attr_accessor :user
18
+ attr_accessor :pass
19
+ attr_reader :imap
20
+
21
+ attr_accessor :trashbox
22
+ attr_accessor :inbox
23
+
24
+
25
+ def initialize
26
+ @inbox = "INBOX"
27
+ @trashbox = "INBOX.Trash"
28
+ end
29
+
30
+ def connect
31
+ @imap = Net::IMAP.new(host, port, true, nil, false)
32
+ end
33
+
34
+ def login
35
+ @imap.login(user, pass)
36
+ end
37
+
38
+ def list_mailboxes
39
+ imap.list("", "*")
40
+ end
41
+
42
+ def select_mailbox mailbox
43
+ imap.select(mailbox)
44
+ end
45
+
46
+ def get_uids_where_from email_address
47
+ select_mailbox(inbox)
48
+ uids = imap.uid_search(["FROM", email_address])
49
+ end
50
+
51
+ def get_uids_where_body_contains value
52
+ select_mailbox(inbox)
53
+ uids = imap.uid_search(["BODY", value])
54
+ end
55
+
56
+ def fetch seqno
57
+ imap.fetch(seqno, ["UID","ENVELOPE"])
58
+ end
59
+
60
+ def uid_fetch uid
61
+ imap.uid_fetch(uid, "ENVELOPE")
62
+ end
63
+
64
+ def subject_line seqno
65
+ imap.fetch(seqno, "BODY[HEADER.FIELDS (SUBJECT)]")
66
+ end
67
+
68
+ def uid_subject_line uid
69
+ imap.uid_fetch(uid, "BODY[HEADER.FIELDS (SUBJECT)]")
70
+ end
71
+
72
+ def delete_uids uids
73
+ $stdout.print "Deleting #{uids.count} emails\n"
74
+ $stdout.flush
75
+ uids.each do |uid|
76
+ imap.uid_copy(uid, trashbox)
77
+ imap.uid_store(uid, "+FLAGS", [:Deleted])
78
+ $stdout.print "."
79
+ $stdout.flush
80
+ end
81
+ end
82
+
83
+ def show_envelope_for_uid uid
84
+ imap.uid_fetch(uid, "ENVELOPE")
85
+ end
86
+
87
+ def expunge
88
+ imap.expunge
89
+ end
90
+
91
+ def disconnect
92
+ imap.disconnect unless imap.disconnected?
93
+ end
94
+ end
95
+ end # module
96
+
@@ -0,0 +1,3 @@
1
+ module ImapClear
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,116 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: imap_clear
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Jeff McAffee
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-11-15 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.13'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.13'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: pry
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: Clear out IMAP emailbox
70
+ email:
71
+ - jeff@ktechsystems.com
72
+ executables:
73
+ - imap_clear
74
+ extensions: []
75
+ extra_rdoc_files: []
76
+ files:
77
+ - ".gitignore"
78
+ - ".rspec"
79
+ - ".travis.yml"
80
+ - Gemfile
81
+ - LICENSE.txt
82
+ - README.md
83
+ - Rakefile
84
+ - bin/console
85
+ - bin/setup
86
+ - exe/imap_clear
87
+ - imap_clear.gemspec
88
+ - lib/imap_clear.rb
89
+ - lib/imap_clear/app.rb
90
+ - lib/imap_clear/version.rb
91
+ homepage: https://github.com/jmcaffee/imap_clear
92
+ licenses:
93
+ - MIT
94
+ metadata: {}
95
+ post_install_message:
96
+ rdoc_options: []
97
+ require_paths:
98
+ - lib
99
+ required_ruby_version: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ required_rubygems_version: !ruby/object:Gem::Requirement
105
+ requirements:
106
+ - - ">="
107
+ - !ruby/object:Gem::Version
108
+ version: '0'
109
+ requirements: []
110
+ rubyforge_project:
111
+ rubygems_version: 2.5.1
112
+ signing_key:
113
+ specification_version: 4
114
+ summary: Clear out IMAP emailbox
115
+ test_files: []
116
+ has_rdoc: