ircmad 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +18 -0
- data/Gemfile +8 -0
- data/LICENSE.txt +22 -0
- data/README.md +77 -0
- data/Rakefile +1 -0
- data/examples/simple.rb +10 -0
- data/ircmad.gemspec +23 -0
- data/lib/ircmad.rb +49 -0
- data/lib/ircmad/configurable.rb +9 -0
- data/lib/ircmad/irc_client.rb +36 -0
- data/lib/ircmad/version.rb +3 -0
- data/lib/ircmad/web_socket.rb +59 -0
- metadata +107 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Takatoshi Matsumoto
|
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,77 @@
|
|
1
|
+
# Ircmad
|
2
|
+
|
3
|
+
Bringing IRC into WebSocket.
|
4
|
+
```
|
5
|
+
IRC <-> WebSoket Server(This product) <-> WebSocket Client
|
6
|
+
```
|
7
|
+
If you use this gateway, you can assess IRC from browser easily.
|
8
|
+
|
9
|
+
Data format is JSON.
|
10
|
+
```
|
11
|
+
WebSocket Server - '{"username":"ToQoz","channel":"#channel1","body":"hello world"}' -> WebSocket Client
|
12
|
+
WebSocket Server <- '{"channel":"#channel1","body":"yes!!!"}' -> WebSocket Client
|
13
|
+
```
|
14
|
+
|
15
|
+
## Installation
|
16
|
+
|
17
|
+
Add this line to your application's Gemfile:
|
18
|
+
|
19
|
+
```ruby
|
20
|
+
gem 'ircmad'
|
21
|
+
```
|
22
|
+
|
23
|
+
And then execute:
|
24
|
+
|
25
|
+
```sh
|
26
|
+
$ bundle
|
27
|
+
```
|
28
|
+
|
29
|
+
Or install it yourself as:
|
30
|
+
|
31
|
+
```sh
|
32
|
+
$ gem install ircmad
|
33
|
+
```
|
34
|
+
|
35
|
+
## Usage
|
36
|
+
|
37
|
+
```ruby
|
38
|
+
# proxy.rb
|
39
|
+
require 'ircmad'
|
40
|
+
Ircmad.new do
|
41
|
+
set :host, '127.0.0.1'
|
42
|
+
set :port, 6667
|
43
|
+
set :channel_list, [ '#channel', '#channel2' ]
|
44
|
+
set :username, 'username'
|
45
|
+
set :password, 'password' # if required
|
46
|
+
set :websocket_port, 3333 # [default is unused port selected automatically]
|
47
|
+
end.run!
|
48
|
+
```
|
49
|
+
$
|
50
|
+
```sh
|
51
|
+
$ ruby proxy.rb
|
52
|
+
```
|
53
|
+
|
54
|
+
In browser
|
55
|
+
```javascript
|
56
|
+
var socket = new WebSocket('ws://localhost:3333')
|
57
|
+
|
58
|
+
// Send
|
59
|
+
socket.send(JSON.stringify({ channel: '#channel1', body: 'yeah' }))
|
60
|
+
|
61
|
+
// Get
|
62
|
+
socket.onmessage = function(msg) { console.log(msg.data) };
|
63
|
+
// => '{"username":"ToQoz","channel":"#channel1","body":"hello world"}'
|
64
|
+
```
|
65
|
+
|
66
|
+
|
67
|
+
## Examples
|
68
|
+
|
69
|
+
In examples/
|
70
|
+
|
71
|
+
## Contributing
|
72
|
+
|
73
|
+
1. Fork it
|
74
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
75
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
76
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
77
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/examples/simple.rb
ADDED
@@ -0,0 +1,10 @@
|
|
1
|
+
require 'ircmad'
|
2
|
+
|
3
|
+
Ircmad.new do
|
4
|
+
set :host, '127.0.0.1'
|
5
|
+
set :port, 6667
|
6
|
+
set :channel_list, [ '#channel', '#channel2' ]
|
7
|
+
set :username, 'username'
|
8
|
+
set :password, 'password' # if required
|
9
|
+
set :websocket_port, 3333 # [default is unused port selected automatically]
|
10
|
+
end.run!
|
data/ircmad.gemspec
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'ircmad/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |gem|
|
7
|
+
gem.name = "ircmad"
|
8
|
+
gem.version = Ircmad::VERSION
|
9
|
+
gem.authors = ["Takatoshi Matsumoto"]
|
10
|
+
gem.email = ["toqoz403@gmail.com"]
|
11
|
+
gem.description = %q{Bringing IRC into WebSocket}
|
12
|
+
gem.summary = %q{Bringing IRC into WebSocket. This enable to assess IRC through WebSocket easily.}
|
13
|
+
gem.homepage = "http://github.com/ToQoz/ircmad"
|
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 "eventmachine"
|
21
|
+
gem.add_dependency "em-websocket"
|
22
|
+
gem.add_dependency "zircon"
|
23
|
+
end
|
data/lib/ircmad.rb
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
require 'json'
|
2
|
+
require 'eventmachine'
|
3
|
+
require 'em-websocket'
|
4
|
+
require 'zircon'
|
5
|
+
|
6
|
+
require "ircmad/version"
|
7
|
+
require "ircmad/configurable"
|
8
|
+
require "ircmad/irc_client"
|
9
|
+
require "ircmad/web_socket"
|
10
|
+
|
11
|
+
class Ircmad
|
12
|
+
include Configurable
|
13
|
+
|
14
|
+
class << self
|
15
|
+
def post_channel
|
16
|
+
@post_channel ||= EM::Channel.new
|
17
|
+
end
|
18
|
+
|
19
|
+
def get_channel
|
20
|
+
@get_channel ||= EM::Channel.new
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def initialize(&block)
|
25
|
+
instance_eval(&block) if block_given?
|
26
|
+
end
|
27
|
+
|
28
|
+
def run!
|
29
|
+
Thread.abort_on_exception = true
|
30
|
+
EM.run do
|
31
|
+
c = config.dup
|
32
|
+
|
33
|
+
EM.defer {
|
34
|
+
WebSocket.new do
|
35
|
+
set :port, c[:websocket_port]
|
36
|
+
end.run!
|
37
|
+
}
|
38
|
+
|
39
|
+
EM.defer {
|
40
|
+
IRCClient.new do
|
41
|
+
set :server, c[:host] || '127.0.0.1'
|
42
|
+
set :port, c[:port] || '6667'
|
43
|
+
set :channel_list, c[:channel_list] || []
|
44
|
+
set :username, c[:username]
|
45
|
+
end.run!
|
46
|
+
}
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
class Ircmad
|
2
|
+
class IRCClient
|
3
|
+
include Configurable
|
4
|
+
|
5
|
+
def initialize(&block)
|
6
|
+
instance_eval(&block) if block_given?
|
7
|
+
|
8
|
+
unless @zircon
|
9
|
+
@zircon = Zircon.new config
|
10
|
+
|
11
|
+
@zircon.send(:login) if @zircon.respond_to?(:login, true)
|
12
|
+
config[:channel_list].each do |channel|
|
13
|
+
@zircon.join channel
|
14
|
+
end
|
15
|
+
end
|
16
|
+
@zircon
|
17
|
+
end
|
18
|
+
|
19
|
+
def run!
|
20
|
+
Ircmad.post_channel.subscribe do |msg|
|
21
|
+
data = JSON.parse(msg, :symbolize_names => true) rescue nil
|
22
|
+
privmsg data[:channel], ":#{data[:message]}"
|
23
|
+
end
|
24
|
+
|
25
|
+
on_privmsg do |msg|
|
26
|
+
Ircmad.get_channel << msg
|
27
|
+
end
|
28
|
+
|
29
|
+
@zircon.run!
|
30
|
+
end
|
31
|
+
|
32
|
+
def method_missing(action, *args, &block)
|
33
|
+
@zircon.send(action.to_s, *args, &block)
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,59 @@
|
|
1
|
+
class Zircon::Message
|
2
|
+
def to_json
|
3
|
+
fencoding = -> s { s.respond_to?(:force_encoding) ? s.force_encoding('UTF-8') : s }
|
4
|
+
{
|
5
|
+
username: fencoding.call(from),
|
6
|
+
channel: fencoding.call(to),
|
7
|
+
body: fencoding.call(body)
|
8
|
+
}.to_json
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
class Ircmad
|
13
|
+
class WebSocket
|
14
|
+
include Configurable
|
15
|
+
|
16
|
+
def initialize(&block)
|
17
|
+
instance_eval(&block) if block_given?
|
18
|
+
end
|
19
|
+
|
20
|
+
def subscribers
|
21
|
+
@subscribers ||= {}
|
22
|
+
end
|
23
|
+
|
24
|
+
def host
|
25
|
+
'127.0.0.1'
|
26
|
+
end
|
27
|
+
|
28
|
+
# http://qiita.com/items/bf47e254d662af1294d8#
|
29
|
+
def port
|
30
|
+
unless @port || config[:port]
|
31
|
+
s = TCPServer.open(0)
|
32
|
+
@port = s.addr[1]
|
33
|
+
s.close
|
34
|
+
end
|
35
|
+
@port || config[:port]
|
36
|
+
end
|
37
|
+
|
38
|
+
def run!
|
39
|
+
puts "Stating WebSocket server on #{host}:#{port}"
|
40
|
+
EM::WebSocket.start(:host => host, :port => port) do |socket|
|
41
|
+
socket.onopen do |sock|
|
42
|
+
subscribers[socket.object_id] = Ircmad.get_channel.subscribe { |msg| socket.send msg.to_json }
|
43
|
+
end
|
44
|
+
|
45
|
+
socket.onclose do |sock|
|
46
|
+
Ircmad.get_channel.unsubscribe(subscribers[socket.object_id])
|
47
|
+
end
|
48
|
+
|
49
|
+
socket.onmessage do |msg|
|
50
|
+
Ircmad.post_channel << msg
|
51
|
+
end
|
52
|
+
|
53
|
+
socket.onerror do |error|
|
54
|
+
puts error
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
metadata
ADDED
@@ -0,0 +1,107 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ircmad
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease:
|
5
|
+
version: 0.0.1
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Takatoshi Matsumoto
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-02-09 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
prerelease: false
|
16
|
+
type: :runtime
|
17
|
+
version_requirements: !ruby/object:Gem::Requirement
|
18
|
+
none: false
|
19
|
+
requirements:
|
20
|
+
- - ! '>='
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: '0'
|
23
|
+
name: eventmachine
|
24
|
+
requirement: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
prerelease: false
|
32
|
+
type: :runtime
|
33
|
+
version_requirements: !ruby/object:Gem::Requirement
|
34
|
+
none: false
|
35
|
+
requirements:
|
36
|
+
- - ! '>='
|
37
|
+
- !ruby/object:Gem::Version
|
38
|
+
version: '0'
|
39
|
+
name: em-websocket
|
40
|
+
requirement: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
prerelease: false
|
48
|
+
type: :runtime
|
49
|
+
version_requirements: !ruby/object:Gem::Requirement
|
50
|
+
none: false
|
51
|
+
requirements:
|
52
|
+
- - ! '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
name: zircon
|
56
|
+
requirement: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
description: Bringing IRC into WebSocket
|
63
|
+
email:
|
64
|
+
- toqoz403@gmail.com
|
65
|
+
executables: []
|
66
|
+
extensions: []
|
67
|
+
extra_rdoc_files: []
|
68
|
+
files:
|
69
|
+
- .gitignore
|
70
|
+
- Gemfile
|
71
|
+
- LICENSE.txt
|
72
|
+
- README.md
|
73
|
+
- Rakefile
|
74
|
+
- examples/simple.rb
|
75
|
+
- ircmad.gemspec
|
76
|
+
- lib/ircmad.rb
|
77
|
+
- lib/ircmad/configurable.rb
|
78
|
+
- lib/ircmad/irc_client.rb
|
79
|
+
- lib/ircmad/version.rb
|
80
|
+
- lib/ircmad/web_socket.rb
|
81
|
+
homepage: http://github.com/ToQoz/ircmad
|
82
|
+
licenses: []
|
83
|
+
post_install_message:
|
84
|
+
rdoc_options: []
|
85
|
+
require_paths:
|
86
|
+
- lib
|
87
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
88
|
+
none: false
|
89
|
+
requirements:
|
90
|
+
- - ! '>='
|
91
|
+
- !ruby/object:Gem::Version
|
92
|
+
version: '0'
|
93
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
94
|
+
none: false
|
95
|
+
requirements:
|
96
|
+
- - ! '>='
|
97
|
+
- !ruby/object:Gem::Version
|
98
|
+
version: '0'
|
99
|
+
requirements: []
|
100
|
+
rubyforge_project:
|
101
|
+
rubygems_version: 1.8.24
|
102
|
+
signing_key:
|
103
|
+
specification_version: 3
|
104
|
+
summary: Bringing IRC into WebSocket. This enable to assess IRC through WebSocket
|
105
|
+
easily.
|
106
|
+
test_files: []
|
107
|
+
has_rdoc:
|