net-irc 0.0.7 → 0.0.8
Sign up to get free protection for your applications and to get access to all the features.
- data/ChangeLog +6 -0
- data/README +14 -15
- data/Rakefile +1 -1
- data/examples/client.rb +1 -1
- data/examples/echo_bot.rb +1 -1
- data/examples/gmail.rb +2 -1
- data/examples/hatena-star-stream.rb +56 -11
- data/examples/hig.rb +54 -22
- data/examples/iig.rb +63 -86
- data/examples/ircd.rb +358 -0
- data/examples/mixi.rb +2 -1
- data/examples/sig.rb +2 -1
- data/examples/tig.rb +1671 -524
- data/examples/wig.rb +27 -27
- data/lib/net/irc.rb +36 -21
- data/lib/net/irc/client.rb +8 -2
- data/lib/net/irc/client/channel_manager.rb +13 -13
- data/lib/net/irc/constants.rb +2 -2
- data/lib/net/irc/message.rb +17 -10
- data/lib/net/irc/message/modeparser.rb +5 -5
- data/lib/net/irc/message/serverconfig.rb +3 -3
- data/lib/net/irc/pattern.rb +2 -2
- data/lib/net/irc/server.rb +8 -5
- data/spec/net-irc_spec.rb +33 -8
- metadata +16 -18
- data/examples/lig.rb +0 -551
- data/examples/lingr.rb +0 -327
- data/examples/nig.rb +0 -154
data/lib/net/irc/server.rb
CHANGED
@@ -68,12 +68,12 @@ class Net::IRC::Server
|
|
68
68
|
end
|
69
69
|
|
70
70
|
# Override subclass.
|
71
|
-
def
|
71
|
+
def available_user_modes
|
72
72
|
"eixwy"
|
73
73
|
end
|
74
74
|
|
75
75
|
# Override subclass.
|
76
|
-
def
|
76
|
+
def available_channel_modes
|
77
77
|
"spknm"
|
78
78
|
end
|
79
79
|
|
@@ -126,12 +126,14 @@ class Net::IRC::Server
|
|
126
126
|
# Set @nick.
|
127
127
|
def on_nick(m)
|
128
128
|
@nick = m.params[0]
|
129
|
+
@prefix = Prefix.new("#{@nick}!#{@user}@#{@host}") if defined? @prefix
|
129
130
|
end
|
130
131
|
|
131
132
|
# Default USER callback.
|
132
133
|
# Set @user, @real, @host and call initial_message.
|
133
134
|
def on_user(m)
|
134
135
|
@user, @real = m.params[0], m.params[3]
|
136
|
+
@nick ||= @user
|
135
137
|
@host = @socket.peeraddr[2]
|
136
138
|
@prefix = Prefix.new("#{@nick}!#{@user}@#{@host}")
|
137
139
|
initial_message
|
@@ -164,11 +166,12 @@ class Net::IRC::Server
|
|
164
166
|
private
|
165
167
|
# Post message to server.
|
166
168
|
#
|
167
|
-
# include Net::IRC::
|
169
|
+
# include Net::IRC::Constants
|
168
170
|
# post prefix, PRIVMSG, "#channel", "foobar"
|
169
171
|
def post(prefix, command, *params)
|
170
172
|
m = Message.new(prefix, command, params.map {|s|
|
171
|
-
s.gsub(
|
173
|
+
#s.gsub(/\r\n|[\r\n]/, " ")
|
174
|
+
s.tr("\r\n", " ")
|
172
175
|
})
|
173
176
|
@log.debug "SEND: #{m.to_s.chomp}"
|
174
177
|
@socket << m
|
@@ -182,7 +185,7 @@ class Net::IRC::Server
|
|
182
185
|
post server_name, RPL_WELCOME, @nick, "Welcome to the Internet Relay Network #{@prefix}"
|
183
186
|
post server_name, RPL_YOURHOST, @nick, "Your host is #{server_name}, running version #{server_version}"
|
184
187
|
post server_name, RPL_CREATED, @nick, "This server was created #{Time.now}"
|
185
|
-
post server_name, RPL_MYINFO, @nick, "#{server_name} #{server_version} #{
|
188
|
+
post server_name, RPL_MYINFO, @nick, "#{server_name} #{server_version} #{available_user_modes} #{available_channel_modes}"
|
186
189
|
end
|
187
190
|
end
|
188
191
|
end # Server
|
data/spec/net-irc_spec.rb
CHANGED
@@ -168,25 +168,25 @@ describe Net::IRC::Prefix, "" do
|
|
168
168
|
end
|
169
169
|
|
170
170
|
describe Net::IRC, "utilities" do
|
171
|
-
it "has
|
172
|
-
message =
|
171
|
+
it "has ctcp_encode method" do
|
172
|
+
message = ctcp_encode "ACTION hehe"
|
173
173
|
message.should == "\x01ACTION hehe\x01"
|
174
174
|
|
175
|
-
message =
|
175
|
+
message = ctcp_encode "ACTION \x01 \x5c "
|
176
176
|
message.should == "\x01ACTION \x5c\x61 \x5c\x5c \x01"
|
177
177
|
|
178
|
-
message =
|
178
|
+
message = ctcp_encode "ACTION \x00 \x0a \x0d \x10 "
|
179
179
|
message.should == "\x01ACTION \x100 \x10n \x10r \x10\x10 \x01"
|
180
180
|
end
|
181
181
|
|
182
|
-
it "has
|
183
|
-
message =
|
182
|
+
it "has ctcp_decode method" do
|
183
|
+
message = ctcp_decode "\x01ACTION hehe\x01"
|
184
184
|
message.should == "ACTION hehe"
|
185
185
|
|
186
|
-
message =
|
186
|
+
message = ctcp_decode "\x01ACTION \x5c\x61 \x5c\x5c \x01"
|
187
187
|
message.should == "ACTION \x01 \x5c "
|
188
188
|
|
189
|
-
message =
|
189
|
+
message = ctcp_decode "\x01ACTION \x100 \x10n \x10r \x10\x10 \x01"
|
190
190
|
message.should == "ACTION \x00 \x0a \x0d \x10 "
|
191
191
|
end
|
192
192
|
end
|
@@ -283,6 +283,31 @@ describe Net::IRC, "server and client" do
|
|
283
283
|
message.to_s.should == "PRIVMSG #channel :message a b c\r\n"
|
284
284
|
end
|
285
285
|
|
286
|
+
if defined? Encoding
|
287
|
+
it "dummy encoding: client posts PRIVMSG and server receives it." do
|
288
|
+
@client.instance_eval do
|
289
|
+
s = "てすと".force_encoding("UTF-8")
|
290
|
+
post PRIVMSG, "#channel", s
|
291
|
+
end
|
292
|
+
|
293
|
+
message = server_q.pop
|
294
|
+
message.should be_a_kind_of(Net::IRC::Message)
|
295
|
+
message.to_s.should == "PRIVMSG #channel てすと\r\n"
|
296
|
+
end
|
297
|
+
|
298
|
+
it "dummy encoding: client posts PRIVMSG and server receives it." do
|
299
|
+
@client.instance_eval do
|
300
|
+
s = "てすと".force_encoding("UTF-8")
|
301
|
+
s.encode!("ISO-2022-JP")
|
302
|
+
post PRIVMSG, "#channel", s
|
303
|
+
end
|
304
|
+
|
305
|
+
message = server_q.pop
|
306
|
+
message.should be_a_kind_of(Net::IRC::Message)
|
307
|
+
message.to_s.should == "PRIVMSG #channel \e$B$F$9$H\e(B\r\n"
|
308
|
+
end
|
309
|
+
end
|
310
|
+
|
286
311
|
it "should allow lame RPL_WELCOME (not prefix but nick)" do
|
287
312
|
client = @client
|
288
313
|
TestServerSession.instance.instance_eval do
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: net-irc
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.8
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- cho45
|
@@ -9,7 +9,7 @@ autorequire: ""
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-
|
12
|
+
date: 2009-08-08 00:00:00 +09:00
|
13
13
|
default_executable:
|
14
14
|
dependencies: []
|
15
15
|
|
@@ -26,36 +26,34 @@ files:
|
|
26
26
|
- README
|
27
27
|
- ChangeLog
|
28
28
|
- Rakefile
|
29
|
+
- spec/channel_manager_spec.rb
|
29
30
|
- spec/modeparser_spec.rb
|
30
31
|
- spec/net-irc_spec.rb
|
31
32
|
- spec/spec.opts
|
32
|
-
- spec/channel_manager_spec.rb
|
33
33
|
- lib/net
|
34
|
-
- lib/net/irc.rb
|
35
34
|
- lib/net/irc
|
36
35
|
- lib/net/irc/client
|
37
36
|
- lib/net/irc/client/channel_manager.rb
|
38
|
-
- lib/net/irc/constants.rb
|
39
|
-
- lib/net/irc/pattern.rb
|
40
37
|
- lib/net/irc/client.rb
|
41
|
-
- lib/net/irc/
|
42
|
-
- lib/net/irc/message.rb
|
38
|
+
- lib/net/irc/constants.rb
|
43
39
|
- lib/net/irc/message
|
44
|
-
- lib/net/irc/message/serverconfig.rb
|
45
40
|
- lib/net/irc/message/modeparser
|
46
41
|
- lib/net/irc/message/modeparser.rb
|
47
|
-
-
|
48
|
-
-
|
49
|
-
-
|
50
|
-
-
|
51
|
-
-
|
52
|
-
- examples/lingr.rb
|
42
|
+
- lib/net/irc/message/serverconfig.rb
|
43
|
+
- lib/net/irc/message.rb
|
44
|
+
- lib/net/irc/pattern.rb
|
45
|
+
- lib/net/irc/server.rb
|
46
|
+
- lib/net/irc.rb
|
53
47
|
- examples/client.rb
|
54
48
|
- examples/echo_bot.rb
|
49
|
+
- examples/gmail.rb
|
50
|
+
- examples/hatena-star-stream.rb
|
55
51
|
- examples/hig.rb
|
56
|
-
- examples/
|
52
|
+
- examples/iig.rb
|
53
|
+
- examples/ircd.rb
|
57
54
|
- examples/mixi.rb
|
58
|
-
- examples/
|
55
|
+
- examples/sig.rb
|
56
|
+
- examples/tig.rb
|
59
57
|
- examples/wig.rb
|
60
58
|
has_rdoc: true
|
61
59
|
homepage: http://lowreal.rubyforge.org
|
@@ -90,7 +88,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
90
88
|
requirements: []
|
91
89
|
|
92
90
|
rubyforge_project: lowreal
|
93
|
-
rubygems_version: 1.
|
91
|
+
rubygems_version: 1.3.1
|
94
92
|
signing_key:
|
95
93
|
specification_version: 2
|
96
94
|
summary: library for implementing IRC server and client
|
data/examples/lig.rb
DELETED
@@ -1,551 +0,0 @@
|
|
1
|
-
#!/usr/bin/env ruby
|
2
|
-
=begin
|
3
|
-
|
4
|
-
# lig.rb
|
5
|
-
|
6
|
-
Lingr IRC Gateway - IRC Gateway to Lingr ( http://www.lingr.com/ )
|
7
|
-
|
8
|
-
## Launch
|
9
|
-
|
10
|
-
$ ruby lig.rb # daemonized
|
11
|
-
|
12
|
-
If you want to help:
|
13
|
-
|
14
|
-
$ ruby lig.rb --help
|
15
|
-
Usage: examples/lig.rb [opts]
|
16
|
-
|
17
|
-
|
18
|
-
Options:
|
19
|
-
-p, --port [PORT=16669] port number to listen
|
20
|
-
-h, --host [HOST=localhost] host name or IP address to listen
|
21
|
-
-l, --log LOG log file
|
22
|
-
-a, --api_key API_KEY Your api key on Lingr
|
23
|
-
--debug Enable debug mode
|
24
|
-
|
25
|
-
## Configuration
|
26
|
-
|
27
|
-
Configuration example for Tiarra ( http://coderepos.org/share/wiki/Tiarra ).
|
28
|
-
|
29
|
-
lingr {
|
30
|
-
host: localhost
|
31
|
-
port: 16669
|
32
|
-
name: username@example.com (Email on Lingr)
|
33
|
-
password: password on Lingr
|
34
|
-
in-encoding: utf8
|
35
|
-
out-encoding: utf8
|
36
|
-
}
|
37
|
-
|
38
|
-
Set your email as IRC 'real name' field, and password as server password.
|
39
|
-
This does not allow anonymous connection to Lingr.
|
40
|
-
You must create a account on Lingr and get API key (ask it first time).
|
41
|
-
|
42
|
-
## Client
|
43
|
-
|
44
|
-
This gateway sends multibyte nicknames at Lingr rooms as-is.
|
45
|
-
So you should use a client which treats it correctly.
|
46
|
-
|
47
|
-
Recommended:
|
48
|
-
|
49
|
-
* LimeChat for OSX ( http://limechat.sourceforge.net/ )
|
50
|
-
* Irssi ( http://irssi.org/ )
|
51
|
-
* (gateway) Tiarra ( http://coderepos.org/share/wiki/Tiarra )
|
52
|
-
|
53
|
-
## Nickname/Mask
|
54
|
-
|
55
|
-
nick -> nickname in a room.
|
56
|
-
o_id -> occupant_id (unique id in a room)
|
57
|
-
u_id -> user_id (unique user id in Lingr)
|
58
|
-
|
59
|
-
* Anonymous User: <nick>|<o_id>!anon@lingr.com
|
60
|
-
* Logged-in User: <nick>|<o_id>!<u_id>@lingr.com
|
61
|
-
* Your: <nick>|<u_id>!<u_id>@lingr.com
|
62
|
-
|
63
|
-
So you can see some nicknames in same user, but it is needed for
|
64
|
-
nickname management on client.
|
65
|
-
|
66
|
-
(Lingr allows different nicknames between rooms in a same user, but IRC not)
|
67
|
-
|
68
|
-
## Licence
|
69
|
-
|
70
|
-
Ruby's by cho45
|
71
|
-
|
72
|
-
## 備考
|
73
|
-
|
74
|
-
このクライアントで 1000speakers への応募はできません。lingr.com から行ってください。
|
75
|
-
|
76
|
-
=end
|
77
|
-
|
78
|
-
$LOAD_PATH << File.dirname(__FILE__)
|
79
|
-
$LOAD_PATH << "lib"
|
80
|
-
$LOAD_PATH << "../lib"
|
81
|
-
|
82
|
-
require "rubygems"
|
83
|
-
require "lingr"
|
84
|
-
require "net/irc"
|
85
|
-
require "pit"
|
86
|
-
require "mutex_m"
|
87
|
-
|
88
|
-
|
89
|
-
class LingrIrcGateway < Net::IRC::Server::Session
|
90
|
-
def server_name
|
91
|
-
"lingrgw"
|
92
|
-
end
|
93
|
-
|
94
|
-
def server_version
|
95
|
-
"0.0.0"
|
96
|
-
end
|
97
|
-
|
98
|
-
def initialize(*args)
|
99
|
-
super
|
100
|
-
@channels = {}
|
101
|
-
@channels.extend(Mutex_m)
|
102
|
-
end
|
103
|
-
|
104
|
-
def on_user(m)
|
105
|
-
super
|
106
|
-
@real, *@copts = @real.split(/\s+/)
|
107
|
-
@copts ||= []
|
108
|
-
|
109
|
-
# Tiarra sends prev nick when reconnects.
|
110
|
-
@nick.sub!(/\|.+$/, "")
|
111
|
-
|
112
|
-
log "Hello #{@nick}, this is Lingr IRC Gateway."
|
113
|
-
log "Client Option: #{@copts.join(", ")}"
|
114
|
-
@log.info "Client Option: #{@copts.join(", ")}"
|
115
|
-
@log.info "Client initialization is completed."
|
116
|
-
|
117
|
-
@lingr = Lingr::Client.new(@opts.api_key)
|
118
|
-
@lingr.create_session('human')
|
119
|
-
@lingr.login(@real, @pass)
|
120
|
-
@session_observer = Thread.start do
|
121
|
-
loop do
|
122
|
-
begin
|
123
|
-
@log.info "Verifying session..."
|
124
|
-
@log.info "Verifed session => #{@lingr.verify_session.inspect}"
|
125
|
-
rescue Lingr::Client::APIError => e
|
126
|
-
@log.info "Verify session raised APIError<#{e.code}:#{e.message}>. Try to re-create session."
|
127
|
-
@lingr.create_session('human')
|
128
|
-
@lingr.login(@real, @pass)
|
129
|
-
rescue Exception => e
|
130
|
-
@log.info "Error on verify_session: #{e.inspect}"
|
131
|
-
end
|
132
|
-
sleep 9 * 60
|
133
|
-
end
|
134
|
-
end
|
135
|
-
@user_info = @lingr.get_user_info
|
136
|
-
|
137
|
-
prefix = make_ids(@user_info)
|
138
|
-
@user_info["prefix"] = prefix
|
139
|
-
post @prefix, NICK, prefix.nick
|
140
|
-
|
141
|
-
rescue Lingr::Client::APIError => e
|
142
|
-
case e.code
|
143
|
-
when 105
|
144
|
-
post nil, ERR_PASSWDMISMATCH, @nick, "Password incorrect"
|
145
|
-
else
|
146
|
-
log "Error: #{e.code}: #{e.message}"
|
147
|
-
end
|
148
|
-
finish
|
149
|
-
end
|
150
|
-
|
151
|
-
def on_privmsg(m)
|
152
|
-
target, message = *m.params
|
153
|
-
if @channels.key?(target.downcase)
|
154
|
-
@lingr.say(@channels[target.downcase][:ticket], message)
|
155
|
-
else
|
156
|
-
post nil, ERR_NOSUCHNICK, @user_info["prefix"].nick, target, "No such nick/channel"
|
157
|
-
end
|
158
|
-
rescue Lingr::Client::APIError => e
|
159
|
-
log "Error: #{e.code}: #{e.message}"
|
160
|
-
log "Coundn't say to #{target}."
|
161
|
-
on_join(Message.new(nil, "JOIN", [target])) if e.code == 102 # invalid session
|
162
|
-
end
|
163
|
-
|
164
|
-
def on_notice(m)
|
165
|
-
on_privmsg(m)
|
166
|
-
end
|
167
|
-
|
168
|
-
def on_whois(m)
|
169
|
-
nick = m.params[0]
|
170
|
-
chan = nil
|
171
|
-
info = nil
|
172
|
-
|
173
|
-
@channels.each do |k, v|
|
174
|
-
if v[:users].key?(nick)
|
175
|
-
chan = k
|
176
|
-
info = v[:users][nick]
|
177
|
-
break
|
178
|
-
end
|
179
|
-
end
|
180
|
-
|
181
|
-
if chan
|
182
|
-
prefix = info["prefix"]
|
183
|
-
real_name = info["description"].to_s
|
184
|
-
server_info = "Lingr: type:#{info["client_type"]} source:#{info["source"]}"
|
185
|
-
channels = [info["client_type"] == "human" ? "@#{chan}" : chan]
|
186
|
-
me = @user_info["prefix"]
|
187
|
-
|
188
|
-
post nil, RPL_WHOISUSER, me.nick, prefix.nick, prefix.user, prefix.host, "*", real_name
|
189
|
-
post nil, RPL_WHOISSERVER, me.nick, prefix.nick, prefix.host, server_info
|
190
|
-
# post nil, RPL_WHOISOPERATOR, me.nick, prefix.nick, "is an IRC operator"
|
191
|
-
# post nil, RPL_WHOISIDLE, me.nick, prefix.nick, idle, "seconds idle"
|
192
|
-
post nil, RPL_WHOISCHANNELS, me.nick, prefix.nick, channels.join(" ")
|
193
|
-
post nil, RPL_ENDOFWHOIS, me.nick, prefix.nick, "End of WHOIS list"
|
194
|
-
else
|
195
|
-
post nil, ERR_NOSUCHNICK, me.nick, nick, "No such nick/channel"
|
196
|
-
end
|
197
|
-
rescue Exception => e
|
198
|
-
@log.error e.inspect
|
199
|
-
e.backtrace.each do |l|
|
200
|
-
@log.error "\t#{l}"
|
201
|
-
end
|
202
|
-
end
|
203
|
-
|
204
|
-
def on_who(m)
|
205
|
-
channel = m.params[0]
|
206
|
-
return unless channel
|
207
|
-
|
208
|
-
info = @channels.synchronize { @channels[channel.downcase] }
|
209
|
-
me = @user_info["prefix"]
|
210
|
-
res = @lingr.get_room_info(info[:chan_id], nil, info[:password])
|
211
|
-
res["occupants"].each do |o|
|
212
|
-
next unless o["nickname"]
|
213
|
-
u_id, o_id, prefix = *make_ids(o, true)
|
214
|
-
op = (o["client_type"] == "human") ? "@" : ""
|
215
|
-
post nil, RPL_WHOREPLY, me.nick, channel, o_id, "lingr.com", "lingr.com", prefix.nick, "H*#{op}", "0 #{o["description"].to_s.gsub(/\s+/, " ")}"
|
216
|
-
end
|
217
|
-
post nil, RPL_ENDOFWHO, me.nick, channel
|
218
|
-
rescue Lingr::Client::APIError => e
|
219
|
-
log "Maybe gateway don't know password for channel #{channel}. Please part and join."
|
220
|
-
end
|
221
|
-
|
222
|
-
def on_join(m)
|
223
|
-
channels = m.params[0].split(/\s*,\s*/)
|
224
|
-
password = m.params[1]
|
225
|
-
channels.each do |channel|
|
226
|
-
next if @channels.key? channel.downcase
|
227
|
-
begin
|
228
|
-
@log.debug "Enter room -> #{channel}"
|
229
|
-
res = @lingr.enter_room(channel.sub(/^#/, ""), @nick, password)
|
230
|
-
res["password"] = password
|
231
|
-
|
232
|
-
@channels.synchronize do
|
233
|
-
create_observer(channel, res)
|
234
|
-
end
|
235
|
-
rescue Lingr::Client::APIError => e
|
236
|
-
log "Error: #{e.code}: #{e.message}"
|
237
|
-
log "Coundn't join to #{channel}."
|
238
|
-
if e.code == 102
|
239
|
-
log "Invalid session... prompt the client to reconnect"
|
240
|
-
finish
|
241
|
-
end
|
242
|
-
rescue Exception => e
|
243
|
-
@log.error e.inspect
|
244
|
-
e.backtrace.each do |l|
|
245
|
-
@log.error "\t#{l}"
|
246
|
-
end
|
247
|
-
end
|
248
|
-
end
|
249
|
-
end
|
250
|
-
|
251
|
-
def on_part(m)
|
252
|
-
channel = m.params[0]
|
253
|
-
info = @channels[channel.downcase]
|
254
|
-
prefix = @user_info["prefix"]
|
255
|
-
|
256
|
-
if info
|
257
|
-
info[:observer].kill
|
258
|
-
@lingr.exit_room(info[:ticket])
|
259
|
-
@channels.delete(channel.downcase)
|
260
|
-
|
261
|
-
post prefix, PART, channel, "Parted"
|
262
|
-
else
|
263
|
-
post nil, ERR_NOSUCHCHANNEL, prefix.nick, channel, "No such channel"
|
264
|
-
end
|
265
|
-
rescue Lingr::Client::APIError => e
|
266
|
-
unless e.code == 102
|
267
|
-
log "Error: #{e.code}: #{e.message}"
|
268
|
-
log "Coundn't say to #{target}."
|
269
|
-
|
270
|
-
@channels.delete(channel.downcase)
|
271
|
-
post prefix, PART, channel, "Parted"
|
272
|
-
end
|
273
|
-
end
|
274
|
-
|
275
|
-
def on_disconnected
|
276
|
-
@channels.each do |k, info|
|
277
|
-
info[:observer].kill
|
278
|
-
end
|
279
|
-
@session_observer.kill rescue nil
|
280
|
-
begin
|
281
|
-
@lingr.destroy_session
|
282
|
-
rescue
|
283
|
-
end
|
284
|
-
end
|
285
|
-
|
286
|
-
private
|
287
|
-
|
288
|
-
def create_observer(channel, response)
|
289
|
-
Thread.start(channel, response) do |chan, res|
|
290
|
-
myprefix = @user_info["prefix"]
|
291
|
-
if @channels[chan.downcase]
|
292
|
-
@channels[chan.downcase][:observer].kill rescue nil
|
293
|
-
end
|
294
|
-
@channels[chan.downcase] = {
|
295
|
-
:ticket => res["ticket"],
|
296
|
-
:counter => res["room"]["counter"],
|
297
|
-
:o_id => res["occupant_id"],
|
298
|
-
:chan_id => res["room"]["id"],
|
299
|
-
:password => res["password"],
|
300
|
-
:users => res["occupants"].reject {|i| i["nickname"].nil? }.inject({}) {|r,i|
|
301
|
-
i["prefix"] = make_ids(i)
|
302
|
-
r.update(i["prefix"].nick => i)
|
303
|
-
},
|
304
|
-
:hcounter => 0,
|
305
|
-
:observer => Thread.current,
|
306
|
-
}
|
307
|
-
|
308
|
-
post server_name, TOPIC, chan, "#{res["room"]["url"]} #{res["room"]["description"]}"
|
309
|
-
post myprefix, JOIN, channel
|
310
|
-
post server_name, MODE, channel, "+o", myprefix.nick
|
311
|
-
post nil, RPL_NAMREPLY, myprefix.nick, "=", chan, @channels[chan.downcase][:users].map{|k,v|
|
312
|
-
v["client_type"] == "human" ? "@#{k}" : k
|
313
|
-
}.join(" ")
|
314
|
-
post nil, RPL_ENDOFNAMES, myprefix.nick, chan, "End of NAMES list"
|
315
|
-
|
316
|
-
info = @channels[chan.downcase]
|
317
|
-
while true
|
318
|
-
begin
|
319
|
-
@log.debug "observe_room<#{info[:counter]}><#{chan}> start <- #{myprefix}"
|
320
|
-
res = @lingr.observe_room info[:ticket], info[:counter]
|
321
|
-
|
322
|
-
info[:counter] = res["counter"] if res["counter"]
|
323
|
-
|
324
|
-
(res["messages"] || []).each do |m|
|
325
|
-
next if m["id"].to_i <= info[:hcounter]
|
326
|
-
|
327
|
-
u_id, o_id, prefix = *make_ids(m, true)
|
328
|
-
|
329
|
-
case m["type"]
|
330
|
-
when "user"
|
331
|
-
# Don't send my messages.
|
332
|
-
unless info[:o_id] == o_id
|
333
|
-
post prefix, PRIVMSG, chan, m["text"]
|
334
|
-
end
|
335
|
-
when "private"
|
336
|
-
# TODO not sent from lingr?
|
337
|
-
post prefix, PRIVMSG, chan, ctcp_encoding("ACTION Sent private: #{m["text"]}")
|
338
|
-
|
339
|
-
# system:{enter,leave,nickname_changed} should not be used for nick management.
|
340
|
-
# when "system:enter"
|
341
|
-
# post prefix, PRIVMSG, chan, ctcp_encoding("ACTION #{m["text"]}")
|
342
|
-
# when "system:leave"
|
343
|
-
# post prefix, PRIVMSG, chan, ctcp_encoding("ACTION #{m["text"]}")
|
344
|
-
# when "system:nickname_change"
|
345
|
-
# post prefix, PRIVMSG, chan, ctcp_encoding("ACTION #{m["text"]}")
|
346
|
-
when "system:broadcast"
|
347
|
-
post "system.broadcast", NOTICE, chan, m["text"]
|
348
|
-
end
|
349
|
-
|
350
|
-
info[:hcounter] = m["id"].to_i if m["id"]
|
351
|
-
end
|
352
|
-
|
353
|
-
if res["occupants"]
|
354
|
-
enter = [], leave = []
|
355
|
-
newusers = res["occupants"].reject {|i| i["nickname"].nil? }.inject({}) {|r,i|
|
356
|
-
i["prefix"] = make_ids(i)
|
357
|
-
r.update(i["prefix"].nick => i)
|
358
|
-
}
|
359
|
-
|
360
|
-
|
361
|
-
nickchange = newusers.inject({:new => [], :old => []}) {|r,(k,new)|
|
362
|
-
old = info[:users].find {|l,old|
|
363
|
-
# same occupant_id and different nickname
|
364
|
-
# when nickname was changed and when un-authed user promoted to authed user.
|
365
|
-
new["prefix"] != old["prefix"] && new["id"] == old["id"]
|
366
|
-
}
|
367
|
-
if old
|
368
|
-
old = old[1]
|
369
|
-
post old["prefix"], NICK, new["prefix"].nick
|
370
|
-
r[:old] << old["prefix"].nick
|
371
|
-
r[:new] << new["prefix"].nick
|
372
|
-
end
|
373
|
-
r
|
374
|
-
}
|
375
|
-
|
376
|
-
entered = newusers.keys - info[:users].keys - nickchange[:new]
|
377
|
-
leaved = info[:users].keys - newusers.keys - entered - nickchange[:old]
|
378
|
-
|
379
|
-
leaved.each do |leave|
|
380
|
-
leave = info[:users][leave]
|
381
|
-
post leave["prefix"], PART, chan, ""
|
382
|
-
end
|
383
|
-
|
384
|
-
entered.each do |enter|
|
385
|
-
enter = newusers[enter]
|
386
|
-
prefix = enter["prefix"]
|
387
|
-
post prefix, JOIN, chan
|
388
|
-
if enter["client_type"] == "human"
|
389
|
-
post server_name, MODE, chan, "+o", prefix.nick
|
390
|
-
end
|
391
|
-
end
|
392
|
-
|
393
|
-
info[:users] = newusers
|
394
|
-
end
|
395
|
-
|
396
|
-
|
397
|
-
rescue Lingr::Client::APIError => e
|
398
|
-
case e.code
|
399
|
-
when 100
|
400
|
-
@log.fatal "BUG: API returns invalid HTTP method"
|
401
|
-
exit 1
|
402
|
-
when 102
|
403
|
-
@log.error "BUG: API returns invalid session. Prompt the client to reconnect."
|
404
|
-
finish
|
405
|
-
when 104
|
406
|
-
@log.fatal "BUG: API returns invalid response format. JSON is unsupported?"
|
407
|
-
exit 1
|
408
|
-
when 109
|
409
|
-
@log.error "Error: API returns invalid ticket. Rejoin this channel..."
|
410
|
-
on_part(Message.new(nil, PART, [chan, res["error"]["message"]]))
|
411
|
-
on_join(Message.new(nil, JOIN, [chan, info["password"]]))
|
412
|
-
when 114
|
413
|
-
@log.fatal "BUG: API returns no counter parameter."
|
414
|
-
exit 1
|
415
|
-
when 120
|
416
|
-
@log.error "Error: API returns invalid encoding. But continues."
|
417
|
-
when 122
|
418
|
-
@log.error "Error: API returns repeated counter. But continues."
|
419
|
-
info[:counter] += 10
|
420
|
-
log "Error: repeated counter. Some message may be ignored..."
|
421
|
-
else
|
422
|
-
# may be socket error?
|
423
|
-
@log.debug "observe failed : #{res.inspect}"
|
424
|
-
log "Error: #{e.code}: #{e.message}"
|
425
|
-
end
|
426
|
-
rescue Timeout::Error
|
427
|
-
# pass
|
428
|
-
rescue JSON::ParserError => e
|
429
|
-
@log.error e
|
430
|
-
info[:counter] += 10
|
431
|
-
log "Error: JSON::ParserError Some message may be ignored..."
|
432
|
-
rescue Exception => e
|
433
|
-
@log.error e.inspect
|
434
|
-
e.backtrace.each do |l|
|
435
|
-
@log.error "\t#{l}"
|
436
|
-
end
|
437
|
-
end
|
438
|
-
sleep 1
|
439
|
-
end
|
440
|
-
end
|
441
|
-
end
|
442
|
-
|
443
|
-
def log(str)
|
444
|
-
str.gsub!(/\s/, " ")
|
445
|
-
begin
|
446
|
-
post nil, NOTICE, @user_info["prefix"].nick, str
|
447
|
-
rescue
|
448
|
-
post nil, NOTICE, @nick, str
|
449
|
-
end
|
450
|
-
end
|
451
|
-
|
452
|
-
def make_ids(o, ext=false)
|
453
|
-
u_id = o["user_id"] || "anon"
|
454
|
-
o_id = o["occupant_id"] || o["id"]
|
455
|
-
nick = (o["default_nickname"] || o["nickname"]).gsub(/\s+/, "")
|
456
|
-
if o["user_id"] == @user_info["user_id"]
|
457
|
-
nick << "|#{o["user_id"]}"
|
458
|
-
else
|
459
|
-
nick << "|#{o["user_id"] ? o_id : "_"+o_id}"
|
460
|
-
end
|
461
|
-
pref = Prefix.new("#{nick}!#{u_id}@lingr.com")
|
462
|
-
ext ? [u_id, o_id, pref] : pref
|
463
|
-
end
|
464
|
-
end
|
465
|
-
|
466
|
-
|
467
|
-
if __FILE__ == $0
|
468
|
-
require "rubygems"
|
469
|
-
require "optparse"
|
470
|
-
require "pit"
|
471
|
-
|
472
|
-
opts = {
|
473
|
-
:port => 16669,
|
474
|
-
:host => "localhost",
|
475
|
-
:log => nil,
|
476
|
-
:debug => false,
|
477
|
-
:foreground => false,
|
478
|
-
}
|
479
|
-
|
480
|
-
OptionParser.new do |parser|
|
481
|
-
parser.instance_eval do
|
482
|
-
self.banner = <<-EOB.gsub(/^\t+/, "")
|
483
|
-
Usage: #{$0} [opts]
|
484
|
-
|
485
|
-
EOB
|
486
|
-
|
487
|
-
separator ""
|
488
|
-
|
489
|
-
separator "Options:"
|
490
|
-
on("-p", "--port [PORT=#{opts[:port]}]", "port number to listen") do |port|
|
491
|
-
opts[:port] = port
|
492
|
-
end
|
493
|
-
|
494
|
-
on("-h", "--host [HOST=#{opts[:host]}]", "host name or IP address to listen") do |host|
|
495
|
-
opts[:host] = host
|
496
|
-
end
|
497
|
-
|
498
|
-
on("-l", "--log LOG", "log file") do |log|
|
499
|
-
opts[:log] = log
|
500
|
-
end
|
501
|
-
|
502
|
-
on("-a", "--api_key API_KEY", "Your api key on Lingr") do |key|
|
503
|
-
opts[:api_key] = key
|
504
|
-
end
|
505
|
-
|
506
|
-
on("--debug", "Enable debug mode") do |debug|
|
507
|
-
opts[:log] = $stdout
|
508
|
-
opts[:debug] = true
|
509
|
-
end
|
510
|
-
|
511
|
-
on("-f", "--foreground", "run foreground") do |foreground|
|
512
|
-
opts[:log] = $stdout
|
513
|
-
opts[:foreground] = true
|
514
|
-
end
|
515
|
-
|
516
|
-
parse!(ARGV)
|
517
|
-
end
|
518
|
-
end
|
519
|
-
|
520
|
-
opts[:logger] = Logger.new(opts[:log], "daily")
|
521
|
-
opts[:logger].level = opts[:debug] ? Logger::DEBUG : Logger::INFO
|
522
|
-
|
523
|
-
def daemonize(foreground=false)
|
524
|
-
[:INT, :TERM, :HUP].each do |sig|
|
525
|
-
Signal.trap sig, "EXIT"
|
526
|
-
end
|
527
|
-
return yield if $DEBUG || foreground
|
528
|
-
Process.fork do
|
529
|
-
Process.setsid
|
530
|
-
Dir.chdir "/"
|
531
|
-
File.open("/dev/null") {|f|
|
532
|
-
STDIN.reopen f
|
533
|
-
STDOUT.reopen f, "w"
|
534
|
-
STDERR.reopen f, "w"
|
535
|
-
}
|
536
|
-
yield
|
537
|
-
end
|
538
|
-
exit! 0
|
539
|
-
end
|
540
|
-
|
541
|
-
opts[:api_key] = Pit.get("lig.rb", :require => {
|
542
|
-
"api_key" => "API key of Lingr"
|
543
|
-
})["api_key"] unless opts[:api_key]
|
544
|
-
|
545
|
-
daemonize(opts[:debug] || opts[:foreground]) do
|
546
|
-
Net::IRC::Server.new(opts[:host], opts[:port], LingrIrcGateway, opts).start
|
547
|
-
end
|
548
|
-
|
549
|
-
end
|
550
|
-
|
551
|
-
|