osa 0.1.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 +7 -0
- data/.dockerignore +195 -0
- data/.gitignore +196 -0
- data/.rubocop.yml +197 -0
- data/Dockerfile +5 -0
- data/Gemfile +6 -0
- data/Gemfile.lock +86 -0
- data/LICENSE.txt +21 -0
- data/README.md +79 -0
- data/Rakefile +10 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/exe/osa +19 -0
- data/lib/osa.rb +1 -0
- data/lib/osa/clients/http_client.rb +41 -0
- data/lib/osa/clients/ms_auth_client.rb +36 -0
- data/lib/osa/clients/ms_graph_client.rb +86 -0
- data/lib/osa/migrations/00001_create_blacklists.rb +10 -0
- data/lib/osa/migrations/00002_create_email_providers.rb +21 -0
- data/lib/osa/migrations/00003_create_config.rb +13 -0
- data/lib/osa/migrations/free-email-providers.txt +3782 -0
- data/lib/osa/scripts/scan_junk_folder.rb +38 -0
- data/lib/osa/scripts/scan_report_folder.rb +33 -0
- data/lib/osa/services/auth_service.rb +56 -0
- data/lib/osa/services/setup_service.rb +27 -0
- data/lib/osa/util/constants.rb +7 -0
- data/lib/osa/util/context.rb +28 -0
- data/lib/osa/util/db.rb +22 -0
- data/lib/osa/util/paginated.rb +31 -0
- data/lib/osa/version.rb +3 -0
- data/osa.gemspec +28 -0
- data/release.sh +23 -0
- data/web/login.html +14 -0
- metadata +146 -0
data/Dockerfile
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,86 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
osa (0.1.0)
|
5
|
+
activerecord (~> 6.0)
|
6
|
+
faraday (~> 1.1)
|
7
|
+
public_suffix (~> 4.0)
|
8
|
+
sqlite3 (~> 1.4)
|
9
|
+
tty-prompt (~> 0.22)
|
10
|
+
|
11
|
+
GEM
|
12
|
+
remote: https://rubygems.org/
|
13
|
+
specs:
|
14
|
+
activemodel (6.0.3.4)
|
15
|
+
activesupport (= 6.0.3.4)
|
16
|
+
activerecord (6.0.3.4)
|
17
|
+
activemodel (= 6.0.3.4)
|
18
|
+
activesupport (= 6.0.3.4)
|
19
|
+
activesupport (6.0.3.4)
|
20
|
+
concurrent-ruby (~> 1.0, >= 1.0.2)
|
21
|
+
i18n (>= 0.7, < 2)
|
22
|
+
minitest (~> 5.1)
|
23
|
+
tzinfo (~> 1.1)
|
24
|
+
zeitwerk (~> 2.2, >= 2.2.2)
|
25
|
+
ast (2.4.1)
|
26
|
+
concurrent-ruby (1.1.7)
|
27
|
+
faraday (1.1.0)
|
28
|
+
multipart-post (>= 1.2, < 3)
|
29
|
+
ruby2_keywords
|
30
|
+
i18n (1.8.5)
|
31
|
+
concurrent-ruby (~> 1.0)
|
32
|
+
minitest (5.14.2)
|
33
|
+
multipart-post (2.1.1)
|
34
|
+
parallel (1.20.0)
|
35
|
+
parser (2.7.2.0)
|
36
|
+
ast (~> 2.4.1)
|
37
|
+
pastel (0.8.0)
|
38
|
+
tty-color (~> 0.5)
|
39
|
+
public_suffix (4.0.6)
|
40
|
+
rainbow (3.0.0)
|
41
|
+
regexp_parser (1.8.2)
|
42
|
+
rexml (3.2.4)
|
43
|
+
rubocop (1.3.1)
|
44
|
+
parallel (~> 1.10)
|
45
|
+
parser (>= 2.7.1.5)
|
46
|
+
rainbow (>= 2.2.2, < 4.0)
|
47
|
+
regexp_parser (>= 1.8)
|
48
|
+
rexml
|
49
|
+
rubocop-ast (>= 1.1.1)
|
50
|
+
ruby-progressbar (~> 1.7)
|
51
|
+
unicode-display_width (>= 1.4.0, < 2.0)
|
52
|
+
rubocop-ast (1.1.1)
|
53
|
+
parser (>= 2.7.1.5)
|
54
|
+
rubocop-performance (1.9.0)
|
55
|
+
rubocop (>= 0.90.0, < 2.0)
|
56
|
+
rubocop-ast (>= 0.4.0)
|
57
|
+
ruby-progressbar (1.10.1)
|
58
|
+
ruby2_keywords (0.0.2)
|
59
|
+
sqlite3 (1.4.2)
|
60
|
+
thread_safe (0.3.6)
|
61
|
+
tty-color (0.6.0)
|
62
|
+
tty-cursor (0.7.1)
|
63
|
+
tty-prompt (0.22.0)
|
64
|
+
pastel (~> 0.8)
|
65
|
+
tty-reader (~> 0.8)
|
66
|
+
tty-reader (0.8.0)
|
67
|
+
tty-cursor (~> 0.7)
|
68
|
+
tty-screen (~> 0.8)
|
69
|
+
wisper (~> 2.0)
|
70
|
+
tty-screen (0.8.1)
|
71
|
+
tzinfo (1.2.8)
|
72
|
+
thread_safe (~> 0.1)
|
73
|
+
unicode-display_width (1.7.0)
|
74
|
+
wisper (2.0.1)
|
75
|
+
zeitwerk (2.4.1)
|
76
|
+
|
77
|
+
PLATFORMS
|
78
|
+
ruby
|
79
|
+
|
80
|
+
DEPENDENCIES
|
81
|
+
osa!
|
82
|
+
rubocop
|
83
|
+
rubocop-performance
|
84
|
+
|
85
|
+
BUNDLED WITH
|
86
|
+
2.1.2
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2020 Moray Baruh
|
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,79 @@
|
|
1
|
+
# Outlook Spam Automator
|
2
|
+
|
3
|
+
## Principle
|
4
|
+
|
5
|
+
### Basics
|
6
|
+
|
7
|
+
OSA uses asks you to choose two folders one "junk" folder and one "report" folder. Even though you can choose any folder
|
8
|
+
for both, it's recommend to choose the default junk folder as the junk folder and create a custom folder for as the report
|
9
|
+
folder.
|
10
|
+
|
11
|
+
These two folders will be used the following way:
|
12
|
+
- When the report folder is scanned, all emails are reported to [Spamcop](https://spamcop.net), deleted and the senders blacklisted.
|
13
|
+
- When the junk folder is scanned, all emails from the blacklist are reported to [Spamcop](https://spamcop.net) and deleted.
|
14
|
+
|
15
|
+
*OSA will not touch any folder beside these two.*
|
16
|
+
|
17
|
+
*It's the user's responsibility to move junk mails to the report folder to build up the blacklist.*
|
18
|
+
|
19
|
+
### The blacklist
|
20
|
+
|
21
|
+
Blacklisting is performed using the main domain name of the sender, excluding subdomains. For example, an email sent from
|
22
|
+
`spammer@spam.example.com`, will blacklist any sender from `example.com`, so `legit-person@not-spam.example.com` will also
|
23
|
+
be blacklisted. However, to prevent millions of users to go blacklisted because of a single user's spam, OSA includes a
|
24
|
+
list of free email providers (which includes domains like gmail.com, outlook.com among others). If the sender uses a free
|
25
|
+
email provider, the full address is blacklisted.
|
26
|
+
|
27
|
+
## Installation
|
28
|
+
|
29
|
+
You can install OSA from RubyGems:
|
30
|
+
|
31
|
+
```sh
|
32
|
+
gem install osa
|
33
|
+
```
|
34
|
+
|
35
|
+
Or use the Docker image:
|
36
|
+
|
37
|
+
```sh
|
38
|
+
docker pull moray95/osa:{version}
|
39
|
+
```
|
40
|
+
|
41
|
+
## Configuring the database
|
42
|
+
|
43
|
+
OSA uses a simple sqlite database to store your configurations and blacklist. The database is configured by default at
|
44
|
+
the current working directory. This means that after a first setup, if you run OSA from a different directory, you will
|
45
|
+
start from a blank configuration and blacklist. If you need to run from different directories, you can specify the database
|
46
|
+
file with the `DATABASE` environment variable.
|
47
|
+
|
48
|
+
## Usage
|
49
|
+
|
50
|
+
Setup your account and settings:
|
51
|
+
|
52
|
+
The setup process will authenticate you with Outlook and ask you to select your folders. If you want to create
|
53
|
+
a new folder, make sure you do it before you run the script.
|
54
|
+
|
55
|
+
```sh
|
56
|
+
osa setup
|
57
|
+
```
|
58
|
+
|
59
|
+
Each time you run this command, your previous configuration will be erased, except for your blacklist.
|
60
|
+
|
61
|
+
Process your report folder:
|
62
|
+
|
63
|
+
```sh
|
64
|
+
osa scan-report
|
65
|
+
```
|
66
|
+
|
67
|
+
Process your junk folder:
|
68
|
+
|
69
|
+
```sh
|
70
|
+
osa scan-junk
|
71
|
+
```
|
72
|
+
|
73
|
+
## Contributing
|
74
|
+
|
75
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/moray95/osa.
|
76
|
+
|
77
|
+
## License
|
78
|
+
|
79
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "osa"
|
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
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
data/exe/osa
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
require 'osa/services/setup_service'
|
3
|
+
require 'osa/services/auth_service'
|
4
|
+
|
5
|
+
cmd = $ARGV.shift
|
6
|
+
|
7
|
+
case cmd
|
8
|
+
when 'setup'
|
9
|
+
OSA::SetupService.new.setup!
|
10
|
+
when 'login'
|
11
|
+
OSA::AuthService.login(Config.first || Config.new)
|
12
|
+
when 'scan-junk'
|
13
|
+
require 'osa/scripts/scan_junk_folder'
|
14
|
+
when 'scan-report'
|
15
|
+
require 'osa/scripts/scan_report_folder'
|
16
|
+
else
|
17
|
+
$stderr.puts "Usage: #{File.basename($0)} [setup|login|scan-junk|scan-report]"
|
18
|
+
exit 1
|
19
|
+
end
|
data/lib/osa.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "osa/version"
|
@@ -0,0 +1,41 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
module OSA
|
3
|
+
class HttpClient
|
4
|
+
def initialize(connection)
|
5
|
+
@connection = connection
|
6
|
+
end
|
7
|
+
|
8
|
+
def get(*args, **kwargs)
|
9
|
+
response = @connection.get(*args, **kwargs)
|
10
|
+
handle_response(response)
|
11
|
+
end
|
12
|
+
|
13
|
+
def post(*args, **kwargs)
|
14
|
+
response = @connection.post(*args, **kwargs)
|
15
|
+
handle_response(response)
|
16
|
+
end
|
17
|
+
|
18
|
+
def delete(*args, **kwargs)
|
19
|
+
response = @connection.delete(*args, **kwargs)
|
20
|
+
handle_response(response)
|
21
|
+
end
|
22
|
+
|
23
|
+
def patch(*args, **kwargs)
|
24
|
+
response = @connection.patch(*args, **kwargs)
|
25
|
+
handle_response(response)
|
26
|
+
end
|
27
|
+
|
28
|
+
private
|
29
|
+
|
30
|
+
def handle_response(response)
|
31
|
+
if response.status > 299
|
32
|
+
raise StandardError, "Request failed with status code: #{response.status}, body: #{response.body}"
|
33
|
+
end
|
34
|
+
if response.headers['content-type'].include?('application/json')
|
35
|
+
JSON.parse(response.body)
|
36
|
+
else
|
37
|
+
response.body
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
require 'osa/clients/http_client'
|
3
|
+
require 'faraday'
|
4
|
+
require 'osa/util/constants'
|
5
|
+
|
6
|
+
module OSA
|
7
|
+
class MSAuthClient < HttpClient
|
8
|
+
def initialize
|
9
|
+
connection = Faraday.new(
|
10
|
+
url: 'https://login.microsoft.com'
|
11
|
+
)
|
12
|
+
super connection
|
13
|
+
end
|
14
|
+
|
15
|
+
def code_token(code)
|
16
|
+
body = {
|
17
|
+
client_id: CLIENT_ID,
|
18
|
+
scope: SCOPE,
|
19
|
+
redirect_uri: REDIRECT_URL,
|
20
|
+
grant_type: :authorization_code,
|
21
|
+
code: code
|
22
|
+
}
|
23
|
+
post('/consumers/oauth2/v2.0/token', body, {})
|
24
|
+
end
|
25
|
+
|
26
|
+
def refresh_token(refresh_token)
|
27
|
+
body = {
|
28
|
+
client_id: CLIENT_ID,
|
29
|
+
scope: SCOPE,
|
30
|
+
refresh_token: refresh_token,
|
31
|
+
grant_type: :refresh_token
|
32
|
+
}
|
33
|
+
post('/consumers/oauth2/v2.0/token', body)
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,86 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
require 'faraday'
|
3
|
+
require 'json'
|
4
|
+
require 'base64'
|
5
|
+
require 'osa/util/paginated'
|
6
|
+
require 'osa/clients/http_client'
|
7
|
+
|
8
|
+
module OSA
|
9
|
+
class MSGraphClient < HttpClient
|
10
|
+
def initialize(token)
|
11
|
+
connection = Faraday.new(
|
12
|
+
url: 'https://graph.microsoft.com',
|
13
|
+
headers: {
|
14
|
+
'authorization' => "Bearer #{token}"
|
15
|
+
}
|
16
|
+
)
|
17
|
+
super connection
|
18
|
+
end
|
19
|
+
|
20
|
+
def rules
|
21
|
+
get('/v1.0/me/mailFolders/inbox/messageRules')
|
22
|
+
end
|
23
|
+
|
24
|
+
def rule(id)
|
25
|
+
get("/v1.0/me/mailFolders/inbox/messageRules/#{id}")
|
26
|
+
end
|
27
|
+
|
28
|
+
def folders
|
29
|
+
Paginated.new(get('/v1.0/me/mailFolders'), self)
|
30
|
+
end
|
31
|
+
|
32
|
+
def mails(folder_id)
|
33
|
+
Paginated.new(get("/v1.0/me/mailFolders/#{folder_id}/messages"), self)
|
34
|
+
end
|
35
|
+
|
36
|
+
def raw_mail(mail_id)
|
37
|
+
get("/v1.0/me/messages/#{mail_id}/$value")
|
38
|
+
end
|
39
|
+
|
40
|
+
def forward_mail_as_attachment(mail_id, to)
|
41
|
+
raw_mail = self.raw_mail(mail_id)
|
42
|
+
forward_message = create_forward_message(mail_id)
|
43
|
+
add_email_attachment(forward_message['id'], raw_mail)
|
44
|
+
update = {
|
45
|
+
toRecipients: [
|
46
|
+
{
|
47
|
+
emailAddress: {
|
48
|
+
address: to
|
49
|
+
}
|
50
|
+
}
|
51
|
+
]
|
52
|
+
}
|
53
|
+
update_message(forward_message['id'], update)
|
54
|
+
send_message(forward_message['id'])
|
55
|
+
end
|
56
|
+
|
57
|
+
def create_forward_message(mail_id)
|
58
|
+
post("/v1.0/me/messages/#{mail_id}/createForward")
|
59
|
+
end
|
60
|
+
|
61
|
+
def add_email_attachment(mail_id, content)
|
62
|
+
body = {
|
63
|
+
"@odata.type": '#microsoft.graph.fileAttachment',
|
64
|
+
"contentBytes": Base64.encode64(content),
|
65
|
+
"name": 'email.eml'
|
66
|
+
}
|
67
|
+
post("/v1.0/me/messages/#{mail_id}/attachments", body.to_json, 'content-type': 'application/json')
|
68
|
+
end
|
69
|
+
|
70
|
+
def delete_mail(mail_id)
|
71
|
+
delete("/v1.0/me/messages/#{mail_id}")
|
72
|
+
end
|
73
|
+
|
74
|
+
def update_rule(id, update)
|
75
|
+
patch("/v1.0/me/mailFolders/inbox/messageRules/#{id}", update.to_json, 'content-type' => 'application/json')
|
76
|
+
end
|
77
|
+
|
78
|
+
def update_message(id, update)
|
79
|
+
patch("/v1.0/me/messages/#{id}", update.to_json, 'content-type': 'application/json')
|
80
|
+
end
|
81
|
+
|
82
|
+
def send_message(id)
|
83
|
+
post("/v1.0/me/messages/#{id}/send")
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
require 'active_record/migration'
|
3
|
+
|
4
|
+
class CreateEmailProviders < ActiveRecord::Migration[5.0]
|
5
|
+
def change
|
6
|
+
create_table :email_providers do |t|
|
7
|
+
t.text :value, unique: true
|
8
|
+
end
|
9
|
+
|
10
|
+
reversible do |dir|
|
11
|
+
file = "#{File.dirname(__FILE__)}/free-email-providers.txt"
|
12
|
+
dir.up do
|
13
|
+
File.open(file).each do |provider|
|
14
|
+
execute "insert into email_providers (value) values (\"#{provider.strip}\")"
|
15
|
+
end
|
16
|
+
end
|
17
|
+
dir.down do
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|