blur 1.8.6 → 2.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,77 +0,0 @@
1
- # encoding: utf-8
2
-
3
- module Blur
4
- class Script
5
- # The +Cache+ class enables data storing inside Blur and it scripts.
6
- #
7
- # What it does is simply store a hash, and act like it is that hash.
8
- #
9
- # When the client closes, it sends a message to all available scripts
10
- # and then those scripts tells the cache to save, in order to remember
11
- # that data and reload it at the next run.
12
- #
13
- # Cache can then save the contents of the hash to a yaml file that persists
14
- # in the ./cache/ directory.
15
- #
16
- # That same file is then loaded once needed again.
17
- class Cache
18
- # Get the path to the cache directory (./cache/)
19
- def self.path
20
- %{#{File.dirname File.expand_path $0}/cache}
21
- end
22
-
23
- # Check if there exists a cache file for the script with name +name+.
24
- def self.exists? name
25
- File.exists? "#{path}/#{name}.yml"
26
- end
27
-
28
- # Get a cache value by key.
29
- def [] key; @hash[key] end
30
-
31
- # Set a cache value by key.
32
- def []= key, value; @hash[key] = value end
33
-
34
- # Instantiate a cache with a script reference.
35
- def initialize script
36
- @hash = {}
37
- @script = script
38
- end
39
-
40
- # Save all internal data to a yaml file in the cache directory.
41
- def save
42
- directory = File.dirname path
43
-
44
- unless File.directory? directory
45
- Dir.mkdir directory
46
- end
47
-
48
- File.open path, ?w do |file|
49
- YAML.dump @hash, file
50
- end
51
- end
52
-
53
- # Load a yaml file as internal data from the cache directory.
54
- #
55
- # @return [Hash] the loaded data.
56
- def load
57
- if yaml = YAML.load_file(path)
58
- @hash = yaml
59
- end
60
- rescue
61
- File.unlink path
62
- end
63
-
64
- # Let Hash#to_s do the job.
65
- def to_s; @hash end
66
-
67
- private
68
-
69
- # The current caches file path.
70
- #
71
- # @return [String] the file path.
72
- def path
73
- %{#{Cache.path}/#{@script.__name}.yml}
74
- end
75
- end
76
- end
77
- end
@@ -1,77 +0,0 @@
1
- # encoding: utf-8
2
-
3
- module Blur
4
- class Script
5
- # The +Commands+ module is a module that gives the ability to turn a
6
- # script into a DSL-like framework.
7
- module Commands
8
- class Command
9
- DefaultOptions = { prefix: '.', hostmask: nil }
10
-
11
- def initialize triggers, options = {}, &block
12
- @triggers = Array === triggers ? triggers : [triggers]
13
- @options = DefaultOptions.merge options
14
- @block = block
15
- end
16
-
17
- # Called by the Commands module.
18
- #
19
- # Calls the command block if the trigger matches the criteria.
20
- def received_message user, channel, message
21
- prefix = @options[:prefix]
22
-
23
- # Return if the prefix don't match.
24
- return unless message.start_with? prefix
25
-
26
- # Return if the hostmask don't match.
27
- # FIXME: Maybe use globbing instead of regular expressions?
28
- unless @options[:hostmask].nil?
29
- hostmask = "#{user.nick}!#{user.name}@#{user.host}"
30
-
31
- return unless hostmask =~ @options[:hostmask]
32
- end
33
-
34
- command, args = split_message message
35
-
36
- # Strip the prefix and compare the trigger name.
37
- if self.matches_trigger? command
38
- @block.call user, channel, args
39
- end
40
- end
41
-
42
- protected
43
-
44
- def split_message message
45
- prefix = @options[:prefix]
46
- command, args = message[prefix.length..-1].split $;, 2
47
-
48
- return command, args
49
- end
50
-
51
- def matches_trigger? command
52
- return @triggers.find{|trigger| trigger.to_s == command }
53
- end
54
- end
55
-
56
- # Extend +base+ with self.
57
- def self.extended base
58
- base.instance_variable_set :@__commands, []
59
- end
60
-
61
- # Add a new command handler and trigger.
62
- def command name, *args, &block
63
- @__commands << Command.new(name, *args, &block)
64
- end
65
-
66
- # Handle all calls to the scripts +message+ method, check to see if
67
- # the message containts a valid command, serialize it and pass it to
68
- # the script as command_name with the parameters +user+, +channel+
69
- # and +message+.
70
- def message user, channel, line
71
- @__commands.each do |command|
72
- command.received_message user, channel, line
73
- end
74
- end
75
- end
76
- end
77
- end
@@ -1,52 +0,0 @@
1
- # encoding: utf-8
2
-
3
- module Blur
4
- class Script
5
- module DSL
6
- def self.included base
7
- base.send :attr_accessor, :__name
8
- base.send :attr_accessor, :__author
9
- base.send :attr_accessor, :__version
10
- base.send :attr_accessor, :__description
11
- end
12
-
13
- # Set the author.
14
- #
15
- # @example
16
- # Author "John Doe <john.doe@gmail.com>"
17
- def Author *authors
18
- @__author = authors.join ', '
19
- end
20
-
21
- # Set the description.
22
- #
23
- # @example
24
- # Description "This is an example script."
25
- def Description description
26
- @__description = description
27
- end
28
-
29
- # Set the version.
30
- #
31
- # @example
32
- # Version "1.0"
33
- def Version version
34
- @__version = version
35
- end
36
-
37
- # @return the name of the script.
38
- def name; @__name end
39
-
40
- # @return the name of the author(s).
41
- def author; @__author end
42
-
43
- # @return the script version.
44
- def version; @__version end
45
-
46
- # @return the description.
47
- def description; @__description end
48
-
49
- alias_method :Authors, :Author
50
- end
51
- end
52
- end