bogofilter 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
+ SHA256:
3
+ metadata.gz: a0db0bcb3a8885135b3e92d8dc78c4deb04d0fde4f79309d083ec6f6730c1d66
4
+ data.tar.gz: db5bd10ed0ae159cf7d38ec78b1a245d341327aee809b9bc300c5c6f10134d81
5
+ SHA512:
6
+ metadata.gz: 7fd055760c4a068b45169bc8baa7ba06a93b4c147d974551872fe8d857f012bb02865644c91315c71b19c650acff24e9afa3b92669ee3d5a34cd41d0a6485a34
7
+ data.tar.gz: 916eb039e7edd12be88f11e669eb7b843b649ac3b236674ab81ff7bc00f950ceb72449037404043e2683be83b96286927962e3445aac0e2d4ea8028c470f0504
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/CHANGELOG.md ADDED
@@ -0,0 +1,10 @@
1
+ ## [0.2.0] - Unreleased
2
+
3
+ ## [0.1.0] - 2022-08-03
4
+
5
+ - #add_spam, #remove_spam
6
+ - #add_ham, #remove_ham
7
+ - #is_spam?, #is_ham?, #classify
8
+ - 0-4 verbose levels -> nothing, -v, -vv, -vvv, -vvvv in bogofilter and prints the output to stdout
9
+ - .version, .configuration
10
+ - Initial release
@@ -0,0 +1,84 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
6
+
7
+ We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
8
+
9
+ ## Our Standards
10
+
11
+ Examples of behavior that contributes to a positive environment for our community include:
12
+
13
+ * Demonstrating empathy and kindness toward other people
14
+ * Being respectful of differing opinions, viewpoints, and experiences
15
+ * Giving and gracefully accepting constructive feedback
16
+ * Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
17
+ * Focusing on what is best not just for us as individuals, but for the overall community
18
+
19
+ Examples of unacceptable behavior include:
20
+
21
+ * The use of sexualized language or imagery, and sexual attention or
22
+ advances of any kind
23
+ * Trolling, insulting or derogatory comments, and personal or political attacks
24
+ * Public or private harassment
25
+ * Publishing others' private information, such as a physical or email
26
+ address, without their explicit permission
27
+ * Other conduct which could reasonably be considered inappropriate in a
28
+ professional setting
29
+
30
+ ## Enforcement Responsibilities
31
+
32
+ Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
33
+
34
+ Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
35
+
36
+ ## Scope
37
+
38
+ This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
39
+
40
+ ## Enforcement
41
+
42
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at ^_^@myrtana.sk. All complaints will be reviewed and investigated promptly and fairly.
43
+
44
+ All community leaders are obligated to respect the privacy and security of the reporter of any incident.
45
+
46
+ ## Enforcement Guidelines
47
+
48
+ Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
49
+
50
+ ### 1. Correction
51
+
52
+ **Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
53
+
54
+ **Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
55
+
56
+ ### 2. Warning
57
+
58
+ **Community Impact**: A violation through a single incident or series of actions.
59
+
60
+ **Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
61
+
62
+ ### 3. Temporary Ban
63
+
64
+ **Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
65
+
66
+ **Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
67
+
68
+ ### 4. Permanent Ban
69
+
70
+ **Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
71
+
72
+ **Consequence**: A permanent ban from any sort of public interaction within the community.
73
+
74
+ ## Attribution
75
+
76
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
77
+ available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
78
+
79
+ Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
80
+
81
+ [homepage]: https://www.contributor-covenant.org
82
+
83
+ For answers to common questions about this code of conduct, see the FAQ at
84
+ https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in bogofilter.gemspec
6
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,40 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ bogofilter (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ coderay (1.1.3)
10
+ diff-lcs (1.5.0)
11
+ method_source (1.0.0)
12
+ pry (0.14.1)
13
+ coderay (~> 1.1)
14
+ method_source (~> 1.0)
15
+ rake (13.0.6)
16
+ rspec (3.11.0)
17
+ rspec-core (~> 3.11.0)
18
+ rspec-expectations (~> 3.11.0)
19
+ rspec-mocks (~> 3.11.0)
20
+ rspec-core (3.11.0)
21
+ rspec-support (~> 3.11.0)
22
+ rspec-expectations (3.11.0)
23
+ diff-lcs (>= 1.2.0, < 2.0)
24
+ rspec-support (~> 3.11.0)
25
+ rspec-mocks (3.11.1)
26
+ diff-lcs (>= 1.2.0, < 2.0)
27
+ rspec-support (~> 3.11.0)
28
+ rspec-support (3.11.0)
29
+
30
+ PLATFORMS
31
+ x86_64-linux
32
+
33
+ DEPENDENCIES
34
+ bogofilter!
35
+ pry
36
+ rake (~> 13.0)
37
+ rspec (~> 3.0)
38
+
39
+ BUNDLED WITH
40
+ 2.3.7
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2022 Ivan Stana
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,59 @@
1
+ # Bogofilter
2
+
3
+ A simple library written in Ruby language to detect spam built around `bogofilter` executable (wrapper).
4
+ The supported input format is the same as in bogofilter - text, EML, mbox.
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ ```ruby
11
+ gem 'bogofilter'
12
+
13
+ or
14
+
15
+ gem 'bogofilter', github: 'istana/bogofilter-ruby'
16
+ ```
17
+
18
+ And then execute:
19
+
20
+ $ bundle install
21
+
22
+ Or install it yourself as:
23
+
24
+ $ gem install bogofilter
25
+
26
+ Also `bogofilter` executable is required on the system, run `apt-get install bogofilter` or similar.
27
+
28
+ ## Usage
29
+
30
+ ```ruby
31
+ filter = Bogofilter.new(dbpath: "exampledb")
32
+ #=> #<Bogofilter:0x00007fa9e2999a60 @dbpath="exampledb", @verbosity=0>
33
+ filter.add_spam("test")
34
+ #=> true # adds and also creates the database
35
+ filter.classify("an example of tested text")
36
+ #=> {:result=>:unsure, :score=>0.52}
37
+ filter.is_spam?("an example of tested text")
38
+ #=> nil (nil means unsure result)
39
+ ```
40
+
41
+ For more information, see the source code. The same behavior applies to the gem as with pure bogofilter command, i.e. a database needs to be trained to be sure if input is spam or ham.
42
+
43
+ ## Development
44
+
45
+ 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.
46
+
47
+ 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 the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
48
+
49
+ ## Contributing
50
+
51
+ Bug reports and pull requests are welcome on GitHub at https://github.com/istana/bogofilter-ruby. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/istana/bogofilter-ruby/blob/master/CODE_OF_CONDUCT.md).
52
+
53
+ ## License
54
+
55
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
56
+
57
+ ## Code of Conduct
58
+
59
+ Everyone interacting in the Bogofilter project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/istana/bogofilter-ruby/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ task default: :spec
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Bogofilter
4
+ VERSION = "0.1.0"
5
+ end
data/lib/bogofilter.rb ADDED
@@ -0,0 +1,100 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "open3"
4
+ require "shellwords"
5
+ require_relative "bogofilter/version"
6
+
7
+ class Bogofilter
8
+ class Error < StandardError; end
9
+
10
+ def self.configuration
11
+ run(arguments: ["-Q"], verbosity: 0)[0]
12
+ end
13
+
14
+ def self.version
15
+ result = run(arguments: ["-V"], verbosity: 0)[0]
16
+ version_match = result.match(%r{bogofilter version (\d+\.\d+\.\d+)})
17
+
18
+ if version_match
19
+ version_match[1]
20
+ else
21
+ raise Error.new('cannot detect bogofilter version')
22
+ end
23
+ end
24
+
25
+ def self.run(arguments: [], stdin: nil, verbosity:)
26
+ verbosity_args = verbosity > 0 ? ["-D", "-#{'v' * verbosity}"] : []
27
+ command = Shellwords.join(["bogofilter", "-C", "-e", *verbosity_args, *arguments])
28
+
29
+ stdout, status = Open3.capture2(command, stdin_data: stdin)
30
+
31
+ if verbosity > 0
32
+ $stdout.puts stdout
33
+ end
34
+
35
+ raise Error.new("command failed with #{status}") if status != 0
36
+
37
+ [stdout, status]
38
+ end
39
+
40
+ def initialize(dbpath:, verbosity: 0)
41
+ @dbpath = dbpath
42
+ @verbosity = verbosity
43
+
44
+ raise Error.new('verbosity can be between 0-4') if !(0..4).cover?(verbosity)
45
+ end
46
+
47
+ def run(arguments: [], stdin: nil)
48
+ self.class.run(arguments: ["-d", @dbpath, *arguments],
49
+ stdin: stdin,
50
+ verbosity: @verbosity,
51
+ )
52
+ end
53
+
54
+ def add_spam(text)
55
+ _stdout, status = run(arguments: ["-s"], stdin: text)
56
+ status == 0
57
+ end
58
+
59
+ def remove_spam(text)
60
+ _stdout, status = run(arguments: ["-S"], stdin: text)
61
+ status == 0
62
+ end
63
+
64
+ def add_ham(text)
65
+ _stdout, status = run(arguments: ["-n"], stdin: text)
66
+ status == 0
67
+ end
68
+
69
+ def remove_ham(text)
70
+ _stdout, status = run(arguments: ["-N"], stdin: text)
71
+ status == 0
72
+ end
73
+
74
+ def classify(text)
75
+ stdout, _status = run(arguments: ["-T"], stdin: text)
76
+ classification, score = stdout.split(" ", 2)
77
+ mapping = {
78
+ "H" => :ham,
79
+ "S" => :spam,
80
+ "U" => :unsure,
81
+ }
82
+
83
+ {
84
+ result: mapping[classification],
85
+ score: score.to_f,
86
+ }
87
+ end
88
+
89
+ def is_spam?(text)
90
+ classification = classify(text)
91
+ return nil if classification[:result] == :unsure
92
+ classification[:result] == :spam
93
+ end
94
+
95
+ def is_ham?(text)
96
+ classification = classify(text)
97
+ return nil if classification[:result] == :unsure
98
+ classification[:result] == :ham
99
+ end
100
+ end
@@ -0,0 +1,4 @@
1
+ module Bogofilter
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
metadata ADDED
@@ -0,0 +1,102 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: bogofilter
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Ivan Stana
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2022-08-03 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: pry
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '13.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '13.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
+ description: |-
56
+ A simple library written in Ruby language to detect spam built around bogofilter executable.
57
+ The supported input format is the same as in bogofilter - text, EML, mbox.
58
+ email:
59
+ - "^_^@myrtana.sk"
60
+ executables: []
61
+ extensions: []
62
+ extra_rdoc_files: []
63
+ files:
64
+ - ".rspec"
65
+ - CHANGELOG.md
66
+ - CODE_OF_CONDUCT.md
67
+ - Gemfile
68
+ - Gemfile.lock
69
+ - LICENSE.txt
70
+ - README.md
71
+ - Rakefile
72
+ - lib/bogofilter.rb
73
+ - lib/bogofilter/version.rb
74
+ - sig/bogofilter.rbs
75
+ homepage: https://github.com/istana/bogofilter-ruby
76
+ licenses:
77
+ - MIT
78
+ metadata:
79
+ allowed_push_host: https://rubygems.org
80
+ homepage_uri: https://github.com/istana/bogofilter-ruby
81
+ source_code_uri: https://github.com/istana/bogofilter-ruby
82
+ changelog_uri: https://github.com/istana/bogofilter-ruby/blob/master/CHANGELOG.md
83
+ post_install_message:
84
+ rdoc_options: []
85
+ require_paths:
86
+ - lib
87
+ required_ruby_version: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - ">="
90
+ - !ruby/object:Gem::Version
91
+ version: 2.1.0
92
+ required_rubygems_version: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ requirements: []
98
+ rubygems_version: 3.3.7
99
+ signing_key:
100
+ specification_version: 4
101
+ summary: Ruby library for spam detection using bogofilter executable
102
+ test_files: []