blockr 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: b7d60e467428a484b551fd64a8e648a1c7685c339f580214cbec1f3bb6ba3ab6
4
+ data.tar.gz: 6445b2573d80c69ade4a9cdb7ae4114c5c3b9634904b399ae452d8e6040dd9ac
5
+ SHA512:
6
+ metadata.gz: e5b456e3e570a9693cbc01ff772c1becbbf723dd3b2aa99e160f9b3c916f07ecfa4561c9504f8e504c09802d2f5958937598717345514958339cd11b9342eb03
7
+ data.tar.gz: f44e5a3bba8218f903416d9c2ca1ceddbc3670b9c42592c1d725d22173c79711c76f0f00155545c2c90acddfb015c3d8f1b7bb5593e988bb9d42e8997db89304
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
@@ -0,0 +1,6 @@
1
+ ---
2
+ language: ruby
3
+ cache: bundler
4
+ rvm:
5
+ - 2.7.0
6
+ before_install: gem install bundler -v 2.1.2
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at abhinav061@gmail.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [https://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: https://contributor-covenant.org
74
+ [version]: https://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,10 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in blockr.gemspec
4
+ gemspec
5
+
6
+ gem "thor"
7
+ gem "rake", "~> 12.0"
8
+ gem "rspec", "~> 3.0"
9
+ gem "tty-file"
10
+ gem "tty-command"
@@ -0,0 +1,61 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ blockr (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ diff-lcs (1.4.4)
10
+ equatable (0.6.1)
11
+ necromancer (0.5.1)
12
+ pastel (0.7.4)
13
+ equatable (~> 0.6)
14
+ tty-color (~> 0.5)
15
+ rake (12.3.3)
16
+ rspec (3.9.0)
17
+ rspec-core (~> 3.9.0)
18
+ rspec-expectations (~> 3.9.0)
19
+ rspec-mocks (~> 3.9.0)
20
+ rspec-core (3.9.2)
21
+ rspec-support (~> 3.9.3)
22
+ rspec-expectations (3.9.2)
23
+ diff-lcs (>= 1.2.0, < 2.0)
24
+ rspec-support (~> 3.9.0)
25
+ rspec-mocks (3.9.1)
26
+ diff-lcs (>= 1.2.0, < 2.0)
27
+ rspec-support (~> 3.9.0)
28
+ rspec-support (3.9.3)
29
+ thor (1.0.1)
30
+ tty-color (0.5.1)
31
+ tty-command (0.9.0)
32
+ pastel (~> 0.7.0)
33
+ tty-cursor (0.7.1)
34
+ tty-file (0.9.0)
35
+ diff-lcs (~> 1.3)
36
+ pastel (~> 0.7.2)
37
+ tty-prompt (~> 0.20)
38
+ tty-prompt (0.21.0)
39
+ necromancer (~> 0.5.0)
40
+ pastel (~> 0.7.0)
41
+ tty-reader (~> 0.7.0)
42
+ tty-reader (0.7.0)
43
+ tty-cursor (~> 0.7)
44
+ tty-screen (~> 0.7)
45
+ wisper (~> 2.0.0)
46
+ tty-screen (0.8.0)
47
+ wisper (2.0.1)
48
+
49
+ PLATFORMS
50
+ ruby
51
+
52
+ DEPENDENCIES
53
+ blockr!
54
+ rake (~> 12.0)
55
+ rspec (~> 3.0)
56
+ thor
57
+ tty-command
58
+ tty-file
59
+
60
+ BUNDLED WITH
61
+ 2.1.2
@@ -0,0 +1,20 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2020 Abhinav Saxena
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
6
+ this software and associated documentation files (the "Software"), to deal in
7
+ the Software without restriction, including without limitation the rights to
8
+ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9
+ the Software, and to permit persons to whom the Software is furnished to do so,
10
+ subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ 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, FITNESS
17
+ FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18
+ COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19
+ IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20
+ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,110 @@
1
+ # blockr
2
+
3
+ Blockr is a command line tool to help you easily block websites, and unblock them when you need them. It modifies /etc/hosts file and add appropriate configuration.
4
+
5
+ In today’s world though it is difficult to continue blocking a few websites because they are quite useful too. And hence, blockr lets you activate and deactivate focus mode, so that you are able to access blocked websites when you are relaxing.
6
+
7
+ ## Installation
8
+ You can install blockr using following command
9
+ `$ sudo gem install blockr`
10
+
11
+
12
+ ## Usage/Commands
13
+ ### block (shortcut b or -b)
14
+ Use this command to block any website you want. It can take one or more websites as input, just separate them by space.
15
+ Please take care to add the exact website URL you want to block - a few websites add www as a subdomain and to block them you will have to add that to in the name of the website.
16
+ **Examples**
17
+ ```
18
+ $ # block youtube
19
+ $ sudo blockr block www.youtube.com
20
+
21
+ $ # use shortcut -b to block youtube
22
+ $ sudo blockr -b www.youtube.com
23
+
24
+ $ # block multiple websites in one shot
25
+ $ sudo blockr -b www.facebook.com www.youtube.com
26
+
27
+ $ # take care to provide exact URLs you want to block
28
+ $ sudo blockr -b www.facebook.com m.facebook.com
29
+ ```
30
+
31
+ ### unblock (shortcut u or -u)
32
+ Use this command to unblock any website you have previously blocked using blockr. It can take one or more space separated websites as input.
33
+
34
+ It is a safe command, and does not return error if website was not blocked.
35
+ ```
36
+ $ # unblock youtube
37
+ $ sudo blockr unblock www.youtube.com
38
+
39
+ $ # use shortcut -u to unblock youtube
40
+ $ sudo blockr -u www.youtube.com
41
+
42
+ $ # unblock multiple websites in one shot
43
+ $ sudo blockr -u www.facebook.com www.youtube.com
44
+
45
+ $ # take care to provide exact URLs you want to unblock
46
+ $ sudo blockr -u www.facebook.com m.facebook.com
47
+ ```
48
+
49
+ ### activate (shortcut a or -a)
50
+ Use this command to activate focus mode. This is a sort of shortcut to block all the websites you have added to blockr and remove all the distracting websites.
51
+
52
+ ```
53
+ $ # activate focus mode
54
+ $ sudo blockr -activate
55
+
56
+ $ # use shortcut -a to activate focus mode
57
+ $ sudo blockr -a
58
+ ```
59
+
60
+ ### deactivate (shortcut d or -d)
61
+ Use this command to deactivate focus mode. This unblocks all the websites you have added to blockr.
62
+
63
+ ```
64
+ $ # deactivate focus mode
65
+ $ sudo blockr deactivate
66
+
67
+ $ # use shortcut -d to deactivate focus mode
68
+ $ sudo blockr -d
69
+ ```
70
+
71
+ ### help (or -h)
72
+ Use this command to know more about blockr commands. You can also use this command with other commands to get command specific help.
73
+
74
+ ```
75
+ $ # see all blockr commands
76
+ $ sudo blockr help
77
+
78
+ $ # get help with respect to a particular command
79
+ $ sudo blockr activate -h
80
+ ```
81
+
82
+ ###### Please Note
83
+ * Given we are modifying /etc/hosts file, you will have to run blockr as an administrator or as a root user.
84
+ * blockr also uses special markers to identify changes done by it, so please don’t manually update entries added by blockr, use blockr commands instead
85
+
86
+ ### TODO
87
+ * Write tests
88
+ * Persist blocked & unlocked settings with activate & deactivate commands
89
+ * Test it on Linux
90
+ * Port it on Windows
91
+
92
+
93
+ ## Development
94
+
95
+ 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.
96
+
97
+ 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).
98
+
99
+ ## Contributing
100
+
101
+ Bug reports and pull requests are welcome on GitHub at https://github.com/abhinavs/blockr. 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/[USERNAME]/blockr/blob/master/CODE_OF_CONDUCT.md).
102
+
103
+
104
+ ## Code of Conduct
105
+
106
+ Everyone interacting in the Blockr project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/blockr/blob/master/CODE_OF_CONDUCT.md).
107
+
108
+ ## Copyright
109
+
110
+ Copyright (c) 2020 Abhinav Saxena. See [MIT License](LICENSE.txt) for further details.
@@ -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
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "blockr"
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__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,36 @@
1
+ require_relative 'lib/blockr/version'
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = "blockr"
5
+ spec.license = "MIT"
6
+ spec.version = Blockr::VERSION
7
+ spec.authors = ["Abhinav Saxena"]
8
+ spec.email = ["abhinav061@gmail.com"]
9
+
10
+ spec.summary = %q{blockr is a commandline line tool to help you easily block websites, and unblock them when you need them}
11
+ #spec.description = %q{TODO: Write a longer description or delete this line.}
12
+ spec.homepage = "http://www.abhinav.co/blockr.html"
13
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
14
+
15
+ #spec.metadata["allowed_push_host"] = "TODO: Set to 'http://mygemserver.com'"
16
+
17
+ spec.metadata["homepage_uri"] = spec.homepage
18
+ spec.metadata["source_code_uri"] = "https://github.com/abhinavs/blockr"
19
+ spec.metadata["changelog_uri"] = "https://github.com/abhinavs/blockr"
20
+
21
+ # Specify which files should be added to the gem when it is released.
22
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
23
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
24
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
25
+ end
26
+
27
+
28
+ spec.add_runtime_dependency "thor", "~> 1.0.1"
29
+ spec.add_runtime_dependency "tty-file", "~> 0.9.0"
30
+ spec.add_runtime_dependency "tty-command", "~> 0.9.0"
31
+
32
+
33
+ spec.bindir = "exe"
34
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
35
+ spec.require_paths = ["lib"]
36
+ end
@@ -0,0 +1,21 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ # NOTE: to suppress "Using the last argument as keyword parameters is deprecated" warning in tty-command
5
+ # Warning[:deprecated] = false
6
+
7
+ lib_path = File.expand_path('../lib', __dir__)
8
+ $:.unshift(lib_path) if !$:.include?(lib_path)
9
+ require 'blockr/cli'
10
+
11
+ Signal.trap('INT') do
12
+ warn("\n#{caller.join("\n")}: interrupted")
13
+ exit(1)
14
+ end
15
+
16
+ begin
17
+ Blockr::CLI.start
18
+ rescue Blockr::CLI::Error => err
19
+ puts "ERROR: #{err.message}"
20
+ exit 1
21
+ end
@@ -0,0 +1,5 @@
1
+ require "blockr/version"
2
+
3
+ module Blockr
4
+ class Error < StandardError; end
5
+ end
@@ -0,0 +1,73 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'thor'
4
+
5
+ module Blockr
6
+ # Handle the application command line parsing
7
+ # and the dispatch to various command objects
8
+ #
9
+ # @api public
10
+ class CLI < Thor
11
+ # Error raised by this runner
12
+ Error = Class.new(StandardError)
13
+
14
+ desc 'version', 'blockr version'
15
+ def version
16
+ require_relative 'version'
17
+ puts "v#{Blockr::VERSION}"
18
+ end
19
+ map %w(--version -v) => :version
20
+
21
+ desc 'deactivate', 'deactivate focus mode, make all blocked websites accessible; shortcut -d'
22
+ method_option :help, aliases: '-h', type: :boolean,
23
+ desc: 'Display usage information'
24
+ def deactivate(*)
25
+ if options[:help]
26
+ invoke :help, ['deactivate']
27
+ else
28
+ require_relative 'commands/deactivate'
29
+ Blockr::Commands::Deactivate.new(options).execute
30
+ end
31
+ end
32
+ map %w(--deactivate -d) => :deactivate
33
+
34
+ desc 'activate', 'activate blockr, make all websites blocked by blockr inaccessible; shortcut -a'
35
+ method_option :help, aliases: '-h', type: :boolean,
36
+ desc: 'Display usage information'
37
+ def activate(*)
38
+ if options[:help]
39
+ invoke :help, ['activate']
40
+ else
41
+ require_relative 'commands/activate'
42
+ Blockr::Commands::Activate.new(options).execute
43
+ end
44
+ end
45
+ map %w(--activate -a) => :activate
46
+
47
+ desc 'unblock [WEBSITES]', 'unblock websites; shortcut -u'
48
+ method_option :help, aliases: '-h', type: :boolean,
49
+ desc: 'Display usage information'
50
+ def unblock(*websites)
51
+ if options[:help]
52
+ invoke :help, ['unblock']
53
+ else
54
+ require_relative 'commands/unblock'
55
+ Blockr::Commands::Unblock.new(websites, options).execute
56
+ end
57
+ end
58
+ map %w(--unblock -u) => :unblock
59
+
60
+ desc 'block [WEBSITES]', 'block websites; shortcut -d'
61
+ method_option :help, aliases: '-h', type: :boolean,
62
+ desc: 'Display usage information'
63
+ def block(*websites)
64
+ if options[:help]
65
+ invoke :help, ['block']
66
+ else
67
+ require_relative 'commands/block'
68
+ Blockr::Commands::Block.new(websites, options).execute
69
+ end
70
+ end
71
+ map %w(--block -b) => :block
72
+ end
73
+ end
@@ -0,0 +1,121 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'forwardable'
4
+
5
+ module Blockr
6
+ class Command
7
+ extend Forwardable
8
+
9
+ def_delegators :command, :run
10
+
11
+ # Execute this command
12
+ #
13
+ # @api public
14
+ def execute(*)
15
+ raise(
16
+ NotImplementedError,
17
+ "#{self.class}##{__method__} must be implemented"
18
+ )
19
+ end
20
+
21
+ # The external commands runner
22
+ #
23
+ # @see http://www.rubydoc.info/gems/tty-command
24
+ #
25
+ # @api public
26
+ def command(**options)
27
+ require 'tty-command'
28
+ TTY::Command.new(options)
29
+ end
30
+
31
+ # The cursor movement
32
+ #
33
+ # @see http://www.rubydoc.info/gems/tty-cursor
34
+ #
35
+ # @api public
36
+ def cursor
37
+ require 'tty-cursor'
38
+ TTY::Cursor
39
+ end
40
+
41
+ # Open a file or text in the user's preferred editor
42
+ #
43
+ # @see http://www.rubydoc.info/gems/tty-editor
44
+ #
45
+ # @api public
46
+ def editor
47
+ require 'tty-editor'
48
+ TTY::Editor
49
+ end
50
+
51
+ # File manipulation utility methods
52
+ #
53
+ # @see http://www.rubydoc.info/gems/tty-file
54
+ #
55
+ # @api public
56
+ def generator
57
+ require 'tty-file'
58
+ TTY::File
59
+ end
60
+
61
+ # Terminal output paging
62
+ #
63
+ # @see http://www.rubydoc.info/gems/tty-pager
64
+ #
65
+ # @api public
66
+ def pager(**options)
67
+ require 'tty-pager'
68
+ TTY::Pager.new(options)
69
+ end
70
+
71
+ # Terminal platform and OS properties
72
+ #
73
+ # @see http://www.rubydoc.info/gems/tty-pager
74
+ #
75
+ # @api public
76
+ def platform
77
+ require 'tty-platform'
78
+ TTY::Platform.new
79
+ end
80
+
81
+ # The interactive prompt
82
+ #
83
+ # @see http://www.rubydoc.info/gems/tty-prompt
84
+ #
85
+ # @api public
86
+ def prompt(**options)
87
+ require 'tty-prompt'
88
+ TTY::Prompt.new(options)
89
+ end
90
+
91
+ # Get terminal screen properties
92
+ #
93
+ # @see http://www.rubydoc.info/gems/tty-screen
94
+ #
95
+ # @api public
96
+ def screen
97
+ require 'tty-screen'
98
+ TTY::Screen
99
+ end
100
+
101
+ # The unix which utility
102
+ #
103
+ # @see http://www.rubydoc.info/gems/tty-which
104
+ #
105
+ # @api public
106
+ def which(*args)
107
+ require 'tty-which'
108
+ TTY::Which.which(*args)
109
+ end
110
+
111
+ # Check if executable exists
112
+ #
113
+ # @see http://www.rubydoc.info/gems/tty-which
114
+ #
115
+ # @api public
116
+ def exec_exist?(*args)
117
+ require 'tty-which'
118
+ TTY::Which.exist?(*args)
119
+ end
120
+ end
121
+ end
@@ -0,0 +1 @@
1
+ #
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative '../command'
4
+ require_relative '../manager'
5
+
6
+ module Blockr
7
+ module Commands
8
+ class Activate < Blockr::Command
9
+ def initialize(options)
10
+ @options = options
11
+ end
12
+
13
+ def execute(input: $stdin, output: $stdout)
14
+ output.puts "Activating focus mode..."
15
+ manager = Blockr::Manager.new()
16
+ manager.activate()
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative '../command'
4
+ require_relative '../manager'
5
+
6
+ module Blockr
7
+ module Commands
8
+ class Block < Blockr::Command
9
+ def initialize(websites, options)
10
+ @websites = websites
11
+ @options = options
12
+ end
13
+
14
+ def execute(input: $stdin, output: $stdout)
15
+ output.puts "Blocking websites..."
16
+ manager = Blockr::Manager.new()
17
+ manager.block(@websites)
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative '../command'
4
+ require_relative '../manager'
5
+
6
+ module Blockr
7
+ module Commands
8
+ class Deactivate < Blockr::Command
9
+ def initialize(options)
10
+ @options = options
11
+ end
12
+
13
+ def execute(input: $stdin, output: $stdout)
14
+ output.puts "Deactivating focus mode..."
15
+ manager = Blockr::Manager.new()
16
+ manager.deactivate()
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative '../command'
4
+ require_relative '../manager'
5
+
6
+ module Blockr
7
+ module Commands
8
+ class Unblock < Blockr::Command
9
+ def initialize(websites, options)
10
+ @websites = websites
11
+ @options = options
12
+ end
13
+
14
+ def execute(input: $stdin, output: $stdout)
15
+ output.puts "Unblocking websites..."
16
+ manager = Blockr::Manager.new()
17
+ manager.unblock(@websites)
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,167 @@
1
+ require 'tty-file'
2
+ require 'tty-command'
3
+ require 'tempfile'
4
+
5
+ module Blockr
6
+
7
+ class Manager
8
+ HOSTS_FILE = "/etc/hosts"
9
+
10
+ KEY_STATUS = "status"
11
+ KEY_NON_BLOCKR_LINES = "non_blockr_lines"
12
+ KEY_BLOCKR_HOSTNAMES = "blocked_hostnames"
13
+
14
+ FILE_EDITED_BY_BLOCKR_HEADER = "# NOTE: following entries were added by blockr, please DON'T manually edit this or any of the following lines ; "
15
+ ACTIVATED = "activated"
16
+ DEACTIVATED = "deactivated"
17
+
18
+ LINE_EDITED_BY_BLOCKR = "# XXX: added by blockr ;"
19
+ DISABLED_BLOCKR_LINE_PREFIX = "#  "
20
+ DUMMY_IP = "127.0.0.1"
21
+
22
+ # for testing
23
+ attr_reader :parsed_data
24
+
25
+ def initialize
26
+ @parsed_data = parse_file(HOSTS_FILE)
27
+ end
28
+
29
+ def block(hostnames)
30
+ return if blank?(hostnames)
31
+
32
+ hostnames.each do |h|
33
+ @parsed_data[KEY_BLOCKR_HOSTNAMES].push(h) if !blank?(h)
34
+ end
35
+ serialize()
36
+ end
37
+
38
+ def unblock(hostnames)
39
+ return if blank?(hostnames)
40
+
41
+ @parsed_data[KEY_BLOCKR_HOSTNAMES].reject! { |h| hostnames.include?(h)}
42
+ serialize()
43
+ end
44
+
45
+ def activate()
46
+ enable()
47
+ serialize()
48
+ end
49
+
50
+ def deactivate()
51
+ disable()
52
+ serialize()
53
+ end
54
+
55
+ private
56
+
57
+ def read_file(filename)
58
+ File.readlines(filename).map(&:chomp)
59
+ end
60
+
61
+ def header_line?(line)
62
+ line.include?(FILE_EDITED_BY_BLOCKR_HEADER)
63
+ end
64
+
65
+ def get_status(header_line)
66
+ matched = header_line.match(/#{FILE_EDITED_BY_BLOCKR_HEADER}#{KEY_STATUS}:(?<status>\w+)/)
67
+ matched['status'] == ACTIVATED ? ACTIVATED : DEACTIVATED
68
+ end
69
+
70
+ def line_edited_by_blockr?(line)
71
+ line.include?(LINE_EDITED_BY_BLOCKR)
72
+ end
73
+
74
+ def blank?(str)
75
+ str.nil? || str.empty?
76
+ end
77
+
78
+ def parse_file(filename)
79
+ lines = read_file(filename)
80
+ parse(lines)
81
+ end
82
+
83
+ def parse(lines)
84
+ parsed_data = {
85
+ KEY_STATUS => ACTIVATED,
86
+ KEY_NON_BLOCKR_LINES => [],
87
+ KEY_BLOCKR_HOSTNAMES => []
88
+ }
89
+
90
+ parsed_data[KEY_NON_BLOCKR_LINES] = lines.select do |line|
91
+ !header_line?(line) && !line_edited_by_blockr?(line)
92
+ end
93
+
94
+ header_line, _ = lines.select { |line| header_line?(line) }
95
+ if !blank?(header_line)
96
+ parsed_data[KEY_STATUS] = get_status(header_line)
97
+ end
98
+
99
+ blockr_lines = lines.select do |line|
100
+ !blank?(line) && !header_line?(line) && line_edited_by_blockr?(line)
101
+ end
102
+
103
+ parsed_data[KEY_BLOCKR_HOSTNAMES] = blockr_lines.map do |line|
104
+ line.sub!(DISABLED_BLOCKR_LINE_PREFIX, '')
105
+ line.sub!(LINE_EDITED_BY_BLOCKR, '')
106
+ words = line.split(' ')
107
+ _ip_address, hostname = words.select { |word| !blank?(word) }
108
+ hostname
109
+ end
110
+
111
+ parsed_data
112
+ end
113
+
114
+ def disable()
115
+ @parsed_data[KEY_STATUS] = DEACTIVATED
116
+ end
117
+
118
+ def enable()
119
+ @parsed_data[KEY_STATUS] = ACTIVATED
120
+ end
121
+
122
+ def serialize()
123
+
124
+ serialized_output = @parsed_data[KEY_NON_BLOCKR_LINES].map { |line| line }
125
+
126
+ blockr_line_prefix = @parsed_data[KEY_STATUS] == DEACTIVATED ? DISABLED_BLOCKR_LINE_PREFIX : ""
127
+
128
+ serialized_output.push("#{FILE_EDITED_BY_BLOCKR_HEADER}#{KEY_STATUS}:#{@parsed_data[KEY_STATUS]}")
129
+
130
+ # uniq
131
+ @parsed_data[KEY_BLOCKR_HOSTNAMES].uniq!
132
+
133
+ serialized_output = serialized_output + @parsed_data[KEY_BLOCKR_HOSTNAMES].map do |hostname|
134
+ "#{blockr_line_prefix}#{DUMMY_IP} #{hostname} #{LINE_EDITED_BY_BLOCKR}"
135
+ end
136
+
137
+ if(write_file(serialized_output))
138
+ clear_cache()
139
+ end
140
+ end
141
+
142
+ def write_file(serialized_output)
143
+ begin
144
+ TTY::File.create_file HOSTS_FILE, serialized_output.join("\n")
145
+ rescue
146
+ $stderr.puts "Error in writing file, please try using sudo"
147
+ return false
148
+ end
149
+ return true
150
+ end
151
+
152
+ def clear_cache
153
+ begin
154
+ cmd = TTY::Command.new
155
+ $stdout.puts "Clearing DNS cache"
156
+
157
+ # XXX mac specific
158
+ out, _ = cmd.run("sudo dscacheutil -flushcache", only_output_on_error: true)
159
+ $stdout.puts out
160
+ rescue
161
+ $stderr.puts "Couldn't clear the cache, please clear cache manually."
162
+ $stderr.puts "See https://www.abhinav.co/clear-dns-cache.html to findout command for your operating system"
163
+ end
164
+ end
165
+
166
+ end
167
+ end
@@ -0,0 +1 @@
1
+ #
@@ -0,0 +1,3 @@
1
+ module Blockr
2
+ VERSION = "0.1.3"
3
+ end
metadata ADDED
@@ -0,0 +1,118 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: blockr
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.3
5
+ platform: ruby
6
+ authors:
7
+ - Abhinav Saxena
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2020-07-27 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: thor
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 1.0.1
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 1.0.1
27
+ - !ruby/object:Gem::Dependency
28
+ name: tty-file
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 0.9.0
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 0.9.0
41
+ - !ruby/object:Gem::Dependency
42
+ name: tty-command
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 0.9.0
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 0.9.0
55
+ description:
56
+ email:
57
+ - abhinav061@gmail.com
58
+ executables:
59
+ - blockr
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ files:
63
+ - ".gitignore"
64
+ - ".rspec"
65
+ - ".travis.yml"
66
+ - CODE_OF_CONDUCT.md
67
+ - Gemfile
68
+ - Gemfile.lock
69
+ - LICENSE.txt
70
+ - README.md
71
+ - Rakefile
72
+ - bin/console
73
+ - bin/setup
74
+ - blockr.gemspec
75
+ - exe/blockr
76
+ - lib/blockr.rb
77
+ - lib/blockr/cli.rb
78
+ - lib/blockr/command.rb
79
+ - lib/blockr/commands/.gitkeep
80
+ - lib/blockr/commands/activate.rb
81
+ - lib/blockr/commands/block.rb
82
+ - lib/blockr/commands/deactivate.rb
83
+ - lib/blockr/commands/unblock.rb
84
+ - lib/blockr/manager.rb
85
+ - lib/blockr/templates/.gitkeep
86
+ - lib/blockr/templates/block/.gitkeep
87
+ - lib/blockr/templates/disable/.gitkeep
88
+ - lib/blockr/templates/enable/.gitkeep
89
+ - lib/blockr/templates/unblock/.gitkeep
90
+ - lib/blockr/version.rb
91
+ homepage: http://www.abhinav.co/blockr.html
92
+ licenses:
93
+ - MIT
94
+ metadata:
95
+ homepage_uri: http://www.abhinav.co/blockr.html
96
+ source_code_uri: https://github.com/abhinavs/blockr
97
+ changelog_uri: https://github.com/abhinavs/blockr
98
+ post_install_message:
99
+ rdoc_options: []
100
+ require_paths:
101
+ - lib
102
+ required_ruby_version: !ruby/object:Gem::Requirement
103
+ requirements:
104
+ - - ">="
105
+ - !ruby/object:Gem::Version
106
+ version: 2.3.0
107
+ required_rubygems_version: !ruby/object:Gem::Requirement
108
+ requirements:
109
+ - - ">="
110
+ - !ruby/object:Gem::Version
111
+ version: '0'
112
+ requirements: []
113
+ rubygems_version: 3.1.4
114
+ signing_key:
115
+ specification_version: 4
116
+ summary: blockr is a commandline line tool to help you easily block websites, and
117
+ unblock them when you need them
118
+ test_files: []