ircmad 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +8 -3
- data/ircmad.gemspec +1 -1
- data/lib/ircmad.rb +15 -4
- data/lib/ircmad/irc_client.rb +66 -19
- data/lib/ircmad/version.rb +1 -1
- data/lib/ircmad/web_socket.rb +14 -0
- data/lib/ircmad/web_socket/buffer.rb +31 -0
- data/lib/ircmad/zircon_ext.rb +26 -3
- metadata +7 -6
data/README.md
CHANGED
@@ -8,8 +8,8 @@ If you use this gateway, you can assess IRC from browser easily.
|
|
8
8
|
|
9
9
|
Data format is JSON.
|
10
10
|
```
|
11
|
-
WebSocket Server - '{"
|
12
|
-
WebSocket Server <- '{"
|
11
|
+
WebSocket Server - '{"from":"ToQoz","to":"#channel1","body":"hello world"}' -> WebSocket Client
|
12
|
+
WebSocket Server <- '{"to":"#channel1","body":"yes!!!"}' -> WebSocket Client
|
13
13
|
```
|
14
14
|
|
15
15
|
## Installation
|
@@ -57,10 +57,15 @@ var socket = new WebSocket('ws://localhost:3333')
|
|
57
57
|
|
58
58
|
// Send
|
59
59
|
socket.send(JSON.stringify({ channel: '#channel1', body: 'yeah' }))
|
60
|
+
ws.send(JSON.stringify({ type: 'join', channel: '#ruby'}))
|
60
61
|
|
61
62
|
// Get
|
62
63
|
socket.onmessage = function(msg) { console.log(msg.data) };
|
63
|
-
// => '{"
|
64
|
+
// => '{"from":"ToQoz","to":"#channel1","body":"hello world","type":"privmsg"}'
|
65
|
+
|
66
|
+
// => {"from":"zlfu","to":"#ruby","body":null,"type":"join"}
|
67
|
+
// => {"from":"hybrid7.debian.local","to":"zlfu","body":"@","type":"353"}
|
68
|
+
// => {"from":"hybrid7.debian.local","to":"zlfu","body":"#ruby","type":"366"}
|
64
69
|
```
|
65
70
|
|
66
71
|
|
data/ircmad.gemspec
CHANGED
data/lib/ircmad.rb
CHANGED
@@ -29,7 +29,7 @@ class Ircmad
|
|
29
29
|
def run!
|
30
30
|
Thread.abort_on_exception = true
|
31
31
|
EM.run do
|
32
|
-
c = config.dup
|
32
|
+
c = default_config.merge(config).dup
|
33
33
|
|
34
34
|
EM.defer {
|
35
35
|
WebSocket.new do
|
@@ -39,13 +39,24 @@ class Ircmad
|
|
39
39
|
|
40
40
|
EM.defer {
|
41
41
|
IRCClient.new do
|
42
|
-
set :server, c[:host]
|
43
|
-
set :port, c[:port]
|
44
|
-
set :
|
42
|
+
set :server, c[:host]
|
43
|
+
set :port, c[:port]
|
44
|
+
set :channel, c[:channel_list].first
|
45
|
+
set :channel_list, c[:channel_list]
|
45
46
|
set :username, c[:username]
|
46
47
|
set :password, c[:password]
|
47
48
|
end.run!
|
48
49
|
}
|
49
50
|
end
|
50
51
|
end
|
52
|
+
|
53
|
+
def default_config
|
54
|
+
{
|
55
|
+
:server => '127.0.0.1',
|
56
|
+
:port => '6667',
|
57
|
+
:channel => '',
|
58
|
+
:channel_list => [],
|
59
|
+
:username => ''
|
60
|
+
}
|
61
|
+
end
|
51
62
|
end
|
data/lib/ircmad/irc_client.rb
CHANGED
@@ -1,42 +1,89 @@
|
|
1
1
|
class Ircmad
|
2
2
|
class IRCClient
|
3
|
+
attr_accessor :client
|
3
4
|
include Configurable
|
4
5
|
|
5
6
|
def initialize(&block)
|
6
7
|
instance_eval(&block) if block_given?
|
7
8
|
|
8
|
-
|
9
|
-
|
10
|
-
@client.send(:login) if @client.respond_to?(:login, true)
|
11
|
-
config[:channel_list].each { |channel| @client.join channel }
|
9
|
+
Ircmad.post_channel.subscribe(&on_post)
|
12
10
|
end
|
13
11
|
|
14
12
|
def run!
|
15
|
-
|
16
|
-
|
17
|
-
|
13
|
+
self.client ||= Zircon.new config
|
14
|
+
|
15
|
+
first_join = true
|
16
|
+
client.on_join do |message|
|
17
|
+
if first_join
|
18
|
+
first_join = false
|
19
|
+
config[:channel_list].each { |channel| client.join channel }
|
20
|
+
else
|
21
|
+
Ircmad.get_channel << message
|
22
|
+
end
|
23
|
+
end
|
24
|
+
client.on_privmsg { |msg| Ircmad.get_channel << msg }
|
25
|
+
client.on_numericreply { |msg| Ircmad.get_channel << msg }
|
26
|
+
client.run!
|
27
|
+
rescue Errno::ECONNREFUSED, Errno::ECONNRESET, Errno::EPIPE => e
|
28
|
+
puts "#{e}\nRetry Now!"
|
29
|
+
close_client
|
30
|
+
sleep 1
|
31
|
+
retry
|
32
|
+
rescue ArgumentError
|
33
|
+
msg = $!.message
|
34
|
+
if /Invalid message:.*/ =~ msg
|
35
|
+
retry
|
36
|
+
else
|
37
|
+
puts 'Unexpected Error!'
|
38
|
+
puts msg
|
39
|
+
exit!
|
40
|
+
end
|
41
|
+
rescue => e
|
42
|
+
puts 'Unexpected Error!'
|
43
|
+
puts e
|
44
|
+
exit!
|
45
|
+
end
|
46
|
+
|
47
|
+
def on_post
|
48
|
+
proc { |msg|
|
49
|
+
m = begin
|
50
|
+
JSON.parse(msg, :symbolize_names => true)
|
18
51
|
rescue JSON::ParserError
|
19
52
|
puts "#{msg} is invalid json"
|
20
|
-
rescue => e
|
21
|
-
puts "Unexpected error"
|
22
|
-
puts e.message
|
23
|
-
puts e.backtrace.join("\n")
|
24
53
|
end
|
25
54
|
|
26
|
-
if
|
27
|
-
|
55
|
+
if m && client
|
56
|
+
m[:type] ||= 'privmsg'
|
57
|
+
|
58
|
+
case m[:type].downcase
|
59
|
+
when 'privmsg'
|
60
|
+
client.privmsg m[:to], ":#{m[:body]}"
|
61
|
+
when 'list'
|
62
|
+
client.list m[:to]
|
63
|
+
when 'names'
|
64
|
+
client.names m[:to]
|
65
|
+
when 'join'
|
66
|
+
client.join m[:to] if m[:to]
|
67
|
+
end
|
28
68
|
end
|
29
|
-
|
69
|
+
}
|
70
|
+
end
|
30
71
|
|
31
|
-
on_privmsg do |msg|
|
32
|
-
Ircmad.get_channel << msg
|
33
|
-
end
|
34
72
|
|
35
|
-
|
73
|
+
|
74
|
+
def close_client
|
75
|
+
# oh...
|
76
|
+
if @client
|
77
|
+
socket = @client.instance_variable_get(:@socket)
|
78
|
+
if socket.respond_to?(:closed?) && !socket.closed?
|
79
|
+
socket.close
|
80
|
+
end
|
81
|
+
@client = nil
|
82
|
+
end
|
36
83
|
end
|
37
84
|
|
38
85
|
def method_missing(action, *args, &block)
|
39
|
-
|
86
|
+
client.send(action.to_s, *args, &block)
|
40
87
|
end
|
41
88
|
end
|
42
89
|
end
|
data/lib/ircmad/version.rb
CHANGED
data/lib/ircmad/web_socket.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
require "ircmad/web_socket/buffer"
|
2
|
+
|
1
3
|
class Ircmad
|
2
4
|
class WebSocket
|
3
5
|
include Configurable
|
@@ -26,8 +28,16 @@ class Ircmad
|
|
26
28
|
|
27
29
|
def run!
|
28
30
|
puts "Stating WebSocket server on #{host}:#{port}"
|
31
|
+
|
32
|
+
Ircmad.get_channel.subscribe do |msg|
|
33
|
+
buffer << msg
|
34
|
+
end
|
35
|
+
|
29
36
|
EM::WebSocket.start(:host => host, :port => port) do |socket|
|
30
37
|
socket.onopen do |sock|
|
38
|
+
buffer.each do |msg|
|
39
|
+
socket.send msg.to_json
|
40
|
+
end
|
31
41
|
subscribers[socket.object_id] = Ircmad.get_channel.subscribe { |msg| socket.send msg.to_json }
|
32
42
|
end
|
33
43
|
|
@@ -44,5 +54,9 @@ class Ircmad
|
|
44
54
|
end
|
45
55
|
end
|
46
56
|
end
|
57
|
+
|
58
|
+
def buffer
|
59
|
+
@buffer ||= Buffer.new
|
60
|
+
end
|
47
61
|
end
|
48
62
|
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
class Ircmad
|
2
|
+
class WebSocket
|
3
|
+
class Buffer
|
4
|
+
attr_reader :ary, :max
|
5
|
+
|
6
|
+
def initialize(_max = 100)
|
7
|
+
@max = _max
|
8
|
+
end
|
9
|
+
|
10
|
+
def push(element)
|
11
|
+
if size > max
|
12
|
+
shift
|
13
|
+
end
|
14
|
+
ary << element
|
15
|
+
end
|
16
|
+
alias_method :<<, :push
|
17
|
+
|
18
|
+
def ary
|
19
|
+
@ary ||= []
|
20
|
+
end
|
21
|
+
|
22
|
+
def method_missing(action, *args, &block)
|
23
|
+
if ary.respond_to?(action.to_s)
|
24
|
+
ary.send(action.to_s, *args, &block)
|
25
|
+
else
|
26
|
+
super
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
data/lib/ircmad/zircon_ext.rb
CHANGED
@@ -3,10 +3,33 @@ class Ircmad
|
|
3
3
|
def to_json
|
4
4
|
fencoding = -> s { s.respond_to?(:force_encoding) ? s.force_encoding('UTF-8') : s }
|
5
5
|
{
|
6
|
-
|
7
|
-
|
8
|
-
body: fencoding.call(body)
|
6
|
+
from: fencoding.call(from),
|
7
|
+
to: fencoding.call(to),
|
8
|
+
body: fencoding.call(body),
|
9
|
+
type: fencoding.call(type),
|
10
|
+
raw: fencoding.call(raw)
|
9
11
|
}.to_json
|
10
12
|
end
|
13
|
+
|
14
|
+
# temporary monkey patch
|
15
|
+
def params
|
16
|
+
@params ||= begin
|
17
|
+
params = []
|
18
|
+
case
|
19
|
+
when !@rest[0].empty?
|
20
|
+
middle, trailer, = *@rest
|
21
|
+
params = middle.split(" ")
|
22
|
+
when !@rest[2].nil? && !@rest[2].empty?
|
23
|
+
middle, trailer, = *@rest[2, 2]
|
24
|
+
params = middle.split(" ")
|
25
|
+
when @rest[1]
|
26
|
+
trailer = @rest[1]
|
27
|
+
when @rest[3]
|
28
|
+
trailer = @rest[3]
|
29
|
+
end
|
30
|
+
params << trailer if trailer
|
31
|
+
params
|
32
|
+
end
|
33
|
+
end
|
11
34
|
end
|
12
35
|
end
|
metadata
CHANGED
@@ -2,14 +2,14 @@
|
|
2
2
|
name: ircmad
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 0.0.
|
5
|
+
version: 0.0.3
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Takatoshi Matsumoto
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-02-
|
12
|
+
date: 2013-02-11 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
prerelease: false
|
@@ -49,16 +49,16 @@ dependencies:
|
|
49
49
|
version_requirements: !ruby/object:Gem::Requirement
|
50
50
|
none: false
|
51
51
|
requirements:
|
52
|
-
- - '
|
52
|
+
- - ! '>='
|
53
53
|
- !ruby/object:Gem::Version
|
54
|
-
version: 0.0.
|
54
|
+
version: 0.0.5
|
55
55
|
name: zircon
|
56
56
|
requirement: !ruby/object:Gem::Requirement
|
57
57
|
none: false
|
58
58
|
requirements:
|
59
|
-
- - '
|
59
|
+
- - ! '>='
|
60
60
|
- !ruby/object:Gem::Version
|
61
|
-
version: 0.0.
|
61
|
+
version: 0.0.5
|
62
62
|
description: Bringing IRC into WebSocket
|
63
63
|
email:
|
64
64
|
- toqoz403@gmail.com
|
@@ -78,6 +78,7 @@ files:
|
|
78
78
|
- lib/ircmad/irc_client.rb
|
79
79
|
- lib/ircmad/version.rb
|
80
80
|
- lib/ircmad/web_socket.rb
|
81
|
+
- lib/ircmad/web_socket/buffer.rb
|
81
82
|
- lib/ircmad/zircon_ext.rb
|
82
83
|
homepage: http://github.com/ToQoz/ircmad
|
83
84
|
licenses: []
|