cinch-cooldown 0.0.1

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.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in cinch-cooldown.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Brian Haberer
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,30 @@
1
+ # Cinch::Cooldown
2
+
3
+ This library is used to add a global cooldown so that users are prevented from spamming the
4
+ channel.
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ gem 'cinch-cooldown'
11
+
12
+ And then execute:
13
+
14
+ $ bundle
15
+
16
+ Or install it yourself as:
17
+
18
+ $ gem install cinch-cooldown
19
+
20
+ ## Usage
21
+
22
+ Coming Soon. :)
23
+
24
+ ## Contributing
25
+
26
+ 1. Fork it
27
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
28
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
29
+ 4. Push to the branch (`git push origin my-new-feature`)
30
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,21 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'cinch-cooldown/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "cinch-cooldown"
8
+ gem.version = Cinch::Cooldown::VERSION
9
+ gem.authors = ["Brian Haberer"]
10
+ gem.email = ["bhaberer@gmail.com"]
11
+ gem.description = %q{This gem allows you to set a shared timer across plugins that are configured to respect it.}
12
+ gem.summary = %q{Global Cooldown tracker for Cinch Plugins}
13
+ gem.homepage = "https://github.com/bhaberer/cinch-cooldown"
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
19
+
20
+ gem.add_dependency 'cinch-toolbox'
21
+ end
@@ -0,0 +1,83 @@
1
+ require 'cinch-cooldown/version'
2
+ require 'cinch-toolbox'
3
+
4
+ module Cinch
5
+ module Plugin
6
+ module ClassMethods
7
+ def enforce_cooldown
8
+ hook(:pre, :for => [:match], :method => lambda {|m| cooldown_finished?(m)})
9
+ end
10
+
11
+ def reset_cooldown(m)
12
+ # TODO
13
+ end
14
+ end
15
+
16
+ def cooldown_finished?(m)
17
+ return true unless shared[:cooldown] && shared[:cooldown][:config]
18
+ synchronize(:cooldown) do
19
+ return true if m.channel.nil?
20
+
21
+ channel = m.channel.name
22
+ nick = m.user.nick
23
+
24
+ return true unless shared[:cooldown][:config][channel]
25
+
26
+ unless shared[:cooldown].key?(channel)
27
+ shared[:cooldown][channel] = { 'global' => Time.now, nick => Time.now }
28
+ return true
29
+ end
30
+
31
+ if channel_cooldown_finished?(channel)
32
+ # Global cd is up, check per user
33
+ if shared[:cooldown][channel].key?(nick)
34
+ # User's in the config, check time
35
+ if user_cooldown_finished?(channel, nick)
36
+ shared[:cooldown][channel]['global'] = Time.now
37
+ shared[:cooldown][channel][nick] = Time.now
38
+ return true
39
+ end
40
+ else
41
+ # User's not used bot before
42
+ shared[:cooldown][channel][nick] = Time.now
43
+ shared[:cooldown][channel]['global'] = Time.now
44
+ return true
45
+ end
46
+ end
47
+
48
+ message = "Sorry, you'll have to wait "
49
+ unless channel_cooldown_finished?(channel)
50
+ message << "#{Cinch::Toolbox.time_format(channel_time_remaining(channel))} before I can talk in the channel again, and "
51
+ end
52
+ message << "#{Cinch::Toolbox.time_format(user_time_remaining(channel, nick))} before your nick can use any commands."
53
+
54
+ m.user.notice message
55
+ return false
56
+ end
57
+ end
58
+
59
+ def user_cooldown_finished?(chan, user)
60
+ shared[:cooldown][:config][chan]['user'] < user_time_elapsed(chan, user)
61
+ end
62
+
63
+ def channel_cooldown_finished?(chan)
64
+ shared[:cooldown][:config][chan]['global'] < channel_time_elapsed(chan)
65
+ end
66
+
67
+ def channel_time_remaining(chan)
68
+ shared[:cooldown][:config][chan]['global'] - channel_time_elapsed(chan)
69
+ end
70
+
71
+ def user_time_remaining(chan, user)
72
+ shared[:cooldown][:config][chan]['user'] - user_time_elapsed(chan, user)
73
+ end
74
+
75
+ def channel_time_elapsed(chan)
76
+ (Time.now - shared[:cooldown][chan]['global']).floor
77
+ end
78
+
79
+ def user_time_elapsed(chan, user)
80
+ (Time.now - shared[:cooldown][chan][user]).floor
81
+ end
82
+ end
83
+ end
@@ -0,0 +1,5 @@
1
+ module Cinch
2
+ module Cooldown
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
metadata ADDED
@@ -0,0 +1,70 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cinch-cooldown
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Brian Haberer
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-05-02 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: cinch-toolbox
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
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: '0'
30
+ description: This gem allows you to set a shared timer across plugins that are configured
31
+ to respect it.
32
+ email:
33
+ - bhaberer@gmail.com
34
+ executables: []
35
+ extensions: []
36
+ extra_rdoc_files: []
37
+ files:
38
+ - .gitignore
39
+ - Gemfile
40
+ - LICENSE.txt
41
+ - README.md
42
+ - Rakefile
43
+ - cinch-cooldown.gemspec
44
+ - lib/cinch-cooldown.rb
45
+ - lib/cinch-cooldown/version.rb
46
+ homepage: https://github.com/bhaberer/cinch-cooldown
47
+ licenses: []
48
+ post_install_message:
49
+ rdoc_options: []
50
+ require_paths:
51
+ - lib
52
+ required_ruby_version: !ruby/object:Gem::Requirement
53
+ none: false
54
+ requirements:
55
+ - - ! '>='
56
+ - !ruby/object:Gem::Version
57
+ version: '0'
58
+ required_rubygems_version: !ruby/object:Gem::Requirement
59
+ none: false
60
+ requirements:
61
+ - - ! '>='
62
+ - !ruby/object:Gem::Version
63
+ version: '0'
64
+ requirements: []
65
+ rubyforge_project:
66
+ rubygems_version: 1.8.24
67
+ signing_key:
68
+ specification_version: 3
69
+ summary: Global Cooldown tracker for Cinch Plugins
70
+ test_files: []