dazeus 0.0.1.201306012328

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/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format progress
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in dazeus-ruby.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Ruben Nijveld
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,29 @@
1
+ # Dazeus
2
+
3
+ DaZeus bindings for Ruby
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'dazeus'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install dazeus
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,4 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ task :default => :spec
data/dazeus.gemspec ADDED
@@ -0,0 +1,22 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'dazeus/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "dazeus"
8
+ gem.version = Dazeus::VERSION
9
+ gem.authors = ["Ruben Nijveld"]
10
+ gem.email = ["ruben@gewooniets.nl"]
11
+ gem.description = %q{Ruby bindings for DaZeus}
12
+ gem.summary = %q{Ruby bindings for DaZeus}
13
+ gem.homepage = "https://github.com/dazeus/dazeus-ruby"
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_development_dependency 'rspec'
21
+ gem.add_development_dependency 'rake'
22
+ end
data/examples/echo.rb ADDED
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env ruby
2
+ require "dazeus"
3
+
4
+ client = Dazeus::create('unix:///tmp/dazeus.sock')
5
+ client.subscribe('message') {|msg| msg.reply(msg.message) }
6
+ client.run
@@ -0,0 +1,54 @@
1
+ require 'socket'
2
+ require 'json'
3
+
4
+ module Dazeus
5
+ class Connection
6
+ def initialize(address)
7
+ @socket = create_socket(address)
8
+ @cache = []
9
+ end
10
+
11
+ def send(message)
12
+ @socket.sendmsg(dazeusify message)
13
+ end
14
+
15
+ def receive
16
+ if @cache.length == 0
17
+ message = @socket.recvmsg[0]
18
+ message = message.force_encoding 'UTF-8'
19
+ message = message.strip
20
+
21
+ while message.length > 0
22
+ digits = ""
23
+ while message[0] =~ /\d/
24
+ digits += message.slice! 0
25
+ end
26
+ @cache.push JSON.parse(message.slice!(0, digits.to_i))
27
+ message = message.strip
28
+ end
29
+ end
30
+ @cache.shift
31
+ end
32
+
33
+ def dazeusify(message)
34
+ msg = JSON.dump(message)
35
+ msg.bytesize.to_s + msg
36
+ end
37
+
38
+ private
39
+ def create_socket(address)
40
+ if address.start_with? 'tcp://'
41
+ address = address[6..-1]
42
+ if address.count ':' != 1
43
+ raise 'Invalid TCP socket format, specify both host and port'
44
+ end
45
+ host, port = address.split ':'
46
+ TCPSocket.new host, port
47
+ elsif address.start_with? 'unix://'
48
+ UNIXSocket.new address[7..-1]
49
+ else
50
+ raise 'Invalid socket format'
51
+ end
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,240 @@
1
+ require 'dazeus/event/action'
2
+ require 'dazeus/event/alias'
3
+ require 'dazeus/event/command'
4
+ require 'dazeus/event/event'
5
+ require 'dazeus/event/message'
6
+ require 'dazeus/event/names'
7
+ require 'dazeus/event/whois'
8
+
9
+
10
+ module Dazeus
11
+ class Dazeus
12
+ attr_accessor :conn
13
+ attr_reader :handshake
14
+
15
+ Protocol = 1
16
+
17
+ def initialize(connection)
18
+ @conn = connection
19
+ @subscribers = {}
20
+ @handshake = false
21
+ end
22
+
23
+ def networks
24
+ response = send_receive({:get => 'networks'})
25
+ return [] unless response['success']
26
+ response['networks']
27
+ end
28
+
29
+ def channels(network)
30
+ response = send_receive({:get => 'channels', :params => [network]})
31
+ return [] unless response['success']
32
+ response['channels']
33
+ end
34
+
35
+ def message(network, channel, message)
36
+ response = send_receive({:do => 'message', :params => [network, channel, message]})
37
+ response['success']
38
+ end
39
+
40
+ def action(network, channel, message)
41
+ response = send_receive({:do => 'action', :params => [network, channel, message]})
42
+ response['success']
43
+ end
44
+
45
+ def reply(network, channel, nick, message, highlight=true, action=false)
46
+ bot_nick = self.nick(network)
47
+ if channel == bot_nick
48
+ if action
49
+ self.action(network, nick, message)
50
+ else
51
+ self.message(network, nick, message)
52
+ end
53
+ else
54
+ message = nick + ': ' + message if highlight
55
+ if action
56
+ self.action(network, channel, message)
57
+ else
58
+ self.message(network, channel, message)
59
+ end
60
+ end
61
+ end
62
+
63
+ def send_names(network, channel)
64
+ response = send_receive({:do => 'names', :params => [network, channel]})
65
+ response['success']
66
+ end
67
+
68
+ def names(network, channel, &block)
69
+ fn = lambda do |response|
70
+ block.call(response)
71
+ unsubscribe &fn
72
+ end
73
+ subscribe('NAMES', &fn)
74
+ send_names(network, nick)
75
+ end
76
+
77
+ def send_whois(network, nick)
78
+ response = send_receive({:do => 'whois', :params => [network, nick]})
79
+ response['success']
80
+ end
81
+
82
+ def whois(network, nick, &block)
83
+ fn = lambda do |response|
84
+ block.call(response)
85
+ unsubscribe &fn
86
+ end
87
+ subscribe('WHOIS', &fn)
88
+ send_whois(network, nick)
89
+ end
90
+
91
+ def join(network, channel)
92
+ response = send_receive({:do => 'join', :params => [network, channel]})
93
+ response['success']
94
+ end
95
+
96
+ def part(network, channel)
97
+ response = send_receive({:do => 'part', :params => [network, channel]})
98
+ response['success']
99
+ end
100
+
101
+ def nick(network)
102
+ response = send_receive({:get => 'nick', :params => [network]})
103
+ return nil unless response['success']
104
+ response['nick']
105
+ end
106
+
107
+ def do_handshake(name, version, config=nil)
108
+ config = name if config == nil
109
+ response = send_receive({:do => 'handshake', :params => [name, version, Protocol, config]})
110
+ @handshake = true if response['success']
111
+ response['success']
112
+ end
113
+
114
+ def get_config(name, group=:plugin)
115
+ return nil unless group == :core || @handshake
116
+ response = send_receive({:get => 'config', :params => [group.to_s, name]})
117
+ return nil unless response['success'] && response.key?('value')
118
+ response['value']
119
+ end
120
+
121
+ def highlight_character
122
+ get_config('highlight', :core)
123
+ end
124
+
125
+ def get_property(name, scope=[])
126
+ data = {:do => 'property', :params => ['get', name]}
127
+ data[:scope] = scope if scope.length > 0
128
+ response = send_receive(data)
129
+ return nil unless response['success'] && response.key?('value')
130
+ response['value']
131
+ end
132
+
133
+ def set_property(name, value, scope=[])
134
+ data = {:do => 'property', :params => ['set', name, value]}
135
+ data[:scope] = scope if scope.length > 0
136
+ response = send_receive(data)
137
+ response['success']
138
+ end
139
+
140
+ def unset_property(name, scope=[])
141
+ data = {:do => 'property', :params => ['unset', name]}
142
+ data[:scope] = scope if scope.length > 0
143
+ response = send_receive(data)
144
+ response['success']
145
+ end
146
+
147
+ def get_property_keys(name, scope=[])
148
+ data = {:do => 'property', :params => ['keys', name]}
149
+ data[:scope] = scope if scope.length > 0
150
+ response = send_receive(data)
151
+ return [] unless response['success'] && response.key?('keys')
152
+ response['keys']
153
+ end
154
+
155
+ def subscribe(event, &callback)
156
+ success = true
157
+ event.upcase.split(' ').each do |ev|
158
+ ev = Event::Alias.resolve(ev)
159
+ @subscribers[ev] = [] unless @subscribers.has_key?(ev)
160
+ if @subscribers[ev].length == 0
161
+ response = send_receive({:do => 'subscribe', :params => [ev]})
162
+ success = success && response['success']
163
+ end
164
+ @subscribers[ev].push callback
165
+ end
166
+ success
167
+ end
168
+
169
+ def unsubscribe(event, &callback)
170
+ success = true
171
+ event.upcase.split(' ').each do |ev|
172
+ ev = Event::Alias.resolve(ev)
173
+ @subscribers[ev] = [] unless @subscribers.has_key?(ev)
174
+ precount = @subscribers[ev].length
175
+ @subscribers[ev].delete callback
176
+ if precount > 0 && @subscribers[ev].length == 0
177
+ response = send_receive({:do => 'unsubscribe', :params => [ev]})
178
+ success = success && response['success']
179
+ end
180
+ end
181
+ success
182
+ end
183
+
184
+ def on_command(command, network=nil, &callback)
185
+ @subscribers['COMMAND'] = [] unless @subscribers.has_key?('COMMAND')
186
+ @subscribers['COMMAND'][command] = [] unless @subscribers['COMMAND'].has_key?(command)
187
+ success = true
188
+ if @subscribers['COMMAND'][command].length == 0
189
+ response = send_receive({:do => 'command', :params => [command]})
190
+ success = response['success']
191
+ end
192
+ @subscribers['COMMAND'][command].push [network, callback]
193
+ success
194
+ end
195
+
196
+ def run
197
+ loop do
198
+ handle_event conn.receive
199
+ end
200
+ end
201
+
202
+ private
203
+ def send_receive(message)
204
+ conn.send message
205
+ response = nil
206
+ loop do
207
+ response = conn.receive
208
+ break unless handle_event response
209
+ end
210
+ response
211
+ end
212
+
213
+ def handle_event(response)
214
+ if response.has_key?('event')
215
+ event = response['event'].upcase
216
+ obj = case event
217
+ when 'PRIVMSG', 'PRIVMSG_ME' then Event::Message.new(response, self)
218
+ when 'COMMAND' then Event::Command.new(response, self)
219
+ when 'ACTION', 'ACTION_ME' then Event::Action.new(response, self)
220
+ when 'NAMES' then Event::Names.new(response, self)
221
+ when 'WHOIS' then Event::Whois.new(response, self)
222
+ else Event::Event.new(response, self)
223
+ end
224
+
225
+ if event == 'COMMAND'
226
+ @subscribers[event][obj['params'][3]].each do |callback|
227
+ callback.call(obj)
228
+ end
229
+ else
230
+ @subscribers[event].each do |callback|
231
+ callback.call(obj)
232
+ end
233
+ end
234
+ true
235
+ else
236
+ false
237
+ end
238
+ end
239
+ end
240
+ end
@@ -0,0 +1,9 @@
1
+ require 'dazeus/event/message'
2
+
3
+ module Dazeus
4
+ module Event
5
+ class Action < Message
6
+
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,25 @@
1
+ module Dazeus
2
+ module Event
3
+ module Alias
4
+ @@aliases = {
5
+ 'MESSAGE' => 'PRIVMSG',
6
+ 'MESSAGE_ME' => 'PRIVMSG_ME',
7
+ 'RENAME' => 'NICK',
8
+ 'CTCPREP' => 'CTCP_REP',
9
+ 'MESSAGEME' => 'PRIVMSG_ME',
10
+ 'PRIVMSGME' => 'PRIVMSG_ME',
11
+ 'ACTIONME' => 'ACTION_ME',
12
+ 'CTCPME' => 'CTCP_ME'
13
+ }
14
+
15
+ def self.resolve(a)
16
+ return a unless @@aliases.has_key?(a)
17
+ @@aliases[a]
18
+ end
19
+
20
+ def self.add_alias(from, to)
21
+ @@aliases[from] = to
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,17 @@
1
+ require 'dazeus/event/message'
2
+
3
+ module Dazeus
4
+ module Event
5
+ class Command < Message
6
+ attr_accessor :command, :args, :remainder
7
+ def post_init
8
+ super
9
+ @remainder = if @params.length > 4 then @params[4] else '' end
10
+ @message = @dazeus.highlightCharacter + @message
11
+ @message += ' ' + @remainder if @remainder.length > 0
12
+ @args = @params[5..-1]
13
+ @args = [] if @args == nil
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,20 @@
1
+ module Dazeus
2
+ module Event
3
+ class Event < Hash
4
+ attr_accessor :event, :params, :dazeus
5
+
6
+ def initialize(data, dazeus)
7
+ super()
8
+ self.merge! data
9
+ @dazeus = dazeus
10
+ @event = self['event']
11
+ @params = self['params']
12
+ post_init
13
+ end
14
+
15
+ def post_init
16
+
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,29 @@
1
+ require 'dazeus/event/event'
2
+
3
+ module Dazeus
4
+ module Event
5
+ class Message < Event
6
+ attr_accessor :message, :network, :channel, :nick
7
+
8
+ def post_init
9
+ super
10
+ @network = @params[0]
11
+ @nick = @params[1]
12
+ @channel = @params[2]
13
+ @message = @params[3]
14
+ end
15
+
16
+ def reply(message, highlight=false, action=false)
17
+ @dazeus.reply(@network, @channel, @nick, message, highlight, action)
18
+ end
19
+
20
+ def highlight(message)
21
+ reply(message, true)
22
+ end
23
+
24
+ def action(message)
25
+ reply(message, false, true)
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,9 @@
1
+ require 'dazeus/event/event'
2
+
3
+ module Dazeus
4
+ module Event
5
+ class Names < Event
6
+
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,9 @@
1
+ require 'dazeus/event/event'
2
+
3
+ module Dazeus
4
+ module Event
5
+ class Whois < Event
6
+
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,3 @@
1
+ module Dazeus
2
+ VERSION = "0.0.1.201306012328"
3
+ end
data/lib/dazeus.rb ADDED
@@ -0,0 +1,9 @@
1
+ require 'dazeus/version'
2
+ require 'dazeus/dazeus'
3
+ require 'dazeus/connection'
4
+
5
+ module Dazeus
6
+ def self.create(address)
7
+ Dazeus.new(Connection.new(address))
8
+ end
9
+ end
@@ -0,0 +1,20 @@
1
+ Bundler.setup
2
+ require 'dazeus'
3
+
4
+ # This file was generated by the `rspec --init` command. Conventionally, all
5
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
6
+ # Require this file using `require "spec_helper"` to ensure that it is only
7
+ # loaded once.
8
+ #
9
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
10
+ RSpec.configure do |config|
11
+ config.treat_symbols_as_metadata_keys_with_true_values = true
12
+ config.run_all_when_everything_filtered = true
13
+ config.filter_run :focus
14
+
15
+ # Run specs in random order to surface order dependencies. If you find an
16
+ # order dependency and want to debug it, you can fix the order by providing
17
+ # the seed, which is printed after each run.
18
+ # --seed 1234
19
+ config.order = 'random'
20
+ end
metadata ADDED
@@ -0,0 +1,104 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dazeus
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1.201306012328
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Ruben Nijveld
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-06-16 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
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
+ - !ruby/object:Gem::Dependency
31
+ name: rake
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ description: Ruby bindings for DaZeus
47
+ email:
48
+ - ruben@gewooniets.nl
49
+ executables: []
50
+ extensions: []
51
+ extra_rdoc_files: []
52
+ files:
53
+ - .gitignore
54
+ - .rspec
55
+ - Gemfile
56
+ - LICENSE.txt
57
+ - README.md
58
+ - Rakefile
59
+ - dazeus.gemspec
60
+ - examples/echo.rb
61
+ - lib/dazeus.rb
62
+ - lib/dazeus/connection.rb
63
+ - lib/dazeus/dazeus.rb
64
+ - lib/dazeus/event/action.rb
65
+ - lib/dazeus/event/alias.rb
66
+ - lib/dazeus/event/command.rb
67
+ - lib/dazeus/event/event.rb
68
+ - lib/dazeus/event/message.rb
69
+ - lib/dazeus/event/names.rb
70
+ - lib/dazeus/event/whois.rb
71
+ - lib/dazeus/version.rb
72
+ - spec/spec_helper.rb
73
+ homepage: https://github.com/dazeus/dazeus-ruby
74
+ licenses: []
75
+ post_install_message:
76
+ rdoc_options: []
77
+ require_paths:
78
+ - lib
79
+ required_ruby_version: !ruby/object:Gem::Requirement
80
+ none: false
81
+ requirements:
82
+ - - ! '>='
83
+ - !ruby/object:Gem::Version
84
+ version: '0'
85
+ segments:
86
+ - 0
87
+ hash: -1952192882182759643
88
+ required_rubygems_version: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ segments:
95
+ - 0
96
+ hash: -1952192882182759643
97
+ requirements: []
98
+ rubyforge_project:
99
+ rubygems_version: 1.8.25
100
+ signing_key:
101
+ specification_version: 3
102
+ summary: Ruby bindings for DaZeus
103
+ test_files:
104
+ - spec/spec_helper.rb