Skip to content

Commit 875ce6b

Browse files
committed
Better topic handling and program interuption
1 parent 7ea8817 commit 875ce6b

File tree

7 files changed

+134
-24
lines changed

7 files changed

+134
-24
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ _yardoc/
1818
doc/
1919
rdoc/
2020
.yardoc
21+
log/*
2122

2223
## Environment normalisation:
2324
/.bundle/

bin/rwire

+44-8
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,41 @@ class Window
3939
content = getstr
4040

4141
if content.length > 0
42-
message = Wired::Message.new(:name => "wired.chat.send_say", :spec => @client.spec)
43-
message.add_parameter("wired.chat.id", 1)
44-
message.add_parameter("wired.chat.say", content)
45-
46-
@client.send_message(message)
42+
message = parse_input(content)
43+
@client.send_message(message) if message
44+
redraw
4745
end
4846
end
4947

48+
def parse_input(string)
49+
message = nil
50+
51+
if string[0] == "/"
52+
reststring = string.split(' ')[1..-1].join(' ')
53+
54+
if string.start_with? "/help"
55+
56+
57+
elsif string.start_with? "/me"
58+
message = Wired::Message.new(:name => "wired.chat.send_me", :spec => @client.spec)
59+
message.add_parameter("wired.chat.id", 1)
60+
message.add_parameter("wired.chat.me", reststring)
61+
62+
elsif string.start_with? "/topic"
63+
message = Wired::Message.new(:name => "wired.chat.set_topic", :spec => @client.spec)
64+
message.add_parameter("wired.chat.id", 1)
65+
message.add_parameter("wired.chat.topic.topic", reststring)
66+
67+
end
68+
else
69+
message = Wired::Message.new(:name => "wired.chat.send_say", :spec => @client.spec)
70+
message.add_parameter("wired.chat.id", 1)
71+
message.add_parameter("wired.chat.say", string)
72+
end
73+
74+
return message
75+
end
76+
5077
def redraw
5178
draw_text_field
5279
draw_messages
@@ -112,8 +139,10 @@ class RWire < Thor
112139

113140

114141
def connect(hostname, login="guest", password="")
115-
# Set debug level
116-
Wired::Log.level = :info
142+
# Logging setup (remove stdout and add rwire.log file)
143+
Wired::Log.remove_appenders Logging.appenders.stdout
144+
Wired::Log.add_appenders Logging.appenders.file('log/rwire.log')
145+
Wired::Log.level = :debug
117146

118147
# Create a Wired Spec object based on "wired.xml" file
119148
spec = Wired::Spec.new
@@ -133,7 +162,14 @@ class RWire < Thor
133162
# Connect the client to the url
134163
exit 0 unless client.connect url, window
135164

136-
window.start
165+
begin
166+
window.start
167+
rescue Interrupt
168+
client.disconnect
169+
warn "\nYou have been disconnected from #{client.server.name}"
170+
exit 1
171+
end
172+
137173
end
138174
end
139175

lib/wired.rb

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
require_relative 'wired/server'
66
require_relative 'wired/socket'
77
require_relative 'wired/spec'
8+
require_relative 'wired/topic'
89
require_relative 'wired/user'
910
require_relative 'wired/url'
1011
require_relative 'wired/wired.xml'

lib/wired/client.rb

+27
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ class Client
1111
attr_accessor :icon
1212
attr_accessor :socket
1313
attr_accessor :server
14+
attr_accessor :topic
1415

1516
attr_accessor :connected_block
1617
attr_accessor :receive_block
@@ -120,6 +121,8 @@ def handle_message(message)
120121
wired_chat_say_or_me message
121122
when "wired.chat.me"
122123
wired_chat_say_or_me message
124+
when "wired.chat.topic"
125+
wired_chat_topic message
123126
end
124127
end
125128

@@ -198,6 +201,19 @@ def wired_chat_say_or_me(message)
198201

199202

200203

204+
def wired_chat_topic(message)
205+
has_topic = (@topic == nil) ? false : true
206+
207+
@topic = Wired::Topic.new(message) if message
208+
now = DateTime.now.strftime("%Y/%m/%d %H:%M")
209+
210+
if has_topic
211+
listeners_print "#{now} << #{@topic.nick} changed topic to #{@topic.topic} >>"
212+
else
213+
listeners_print "Server Topic : #{@topic.topic} by #{@topic.nick}"
214+
end
215+
end
216+
201217

202218

203219

@@ -213,6 +229,17 @@ def listen
213229

214230

215231

232+
def ping
233+
Thread.new do
234+
loop do
235+
send_message Wired::Message.new(:spec => @spec, :name => "wired.send_ping")
236+
sleep 30
237+
end
238+
end
239+
end
240+
241+
242+
216243
def listening?
217244
@listening == true
218245
end

lib/wired/logger.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@ module Wired
22
require 'logging'
33

44
# @return [Logging] The logger used internally by the library
5-
Log = Logging.logger(STDOUT)
5+
Log = Logging.logger['wired']
66
Log.level = :debug
77
end

lib/wired/socket.rb

+47-15
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ module Cipher
106106

107107
# @return [Boolean] Compression enabled internally
108108
attr_reader :compression_enabled
109+
attr_reader :compression_configured
109110

110111
# @return [Boolean] Encryption enabled internally
111112
attr_reader :encryption_enabled
@@ -181,7 +182,7 @@ def connect(handshake = true)
181182
end
182183

183184
# create a new socket for connection
184-
@socket = Socket::Socket.new(:INET, :STREAM)
185+
@socket = Socket::Socket.new(:INET, :STREAM)
185186
remote_addr = Socket::Socket.pack_sockaddr_in(@port, @hostname)
186187

187188
@socket.set_encoding("ASCII-8BIT")
@@ -207,6 +208,11 @@ def connect(handshake = true)
207208
return false
208209
end
209210

211+
# configure compression
212+
@compression_enabled = true if @compression_configured
213+
214+
# configure checksum
215+
210216
# perform the Wired key exange
211217
if !connect_key_exange(nil, nil)
212218
Wired::Log.error "Handshake failed to #{@hostname}:#{@port}"
@@ -275,16 +281,16 @@ def read
275281

276282
if binary.length > 0
277283
# decompress data
278-
if @compression_enabled
279-
binary = inflate_data(binary)
280-
end
284+
if @compression_enabled
285+
binary = inflate_data(binary)
286+
end
281287

282-
# decrypt data
283-
if @encryption_enabled
284-
binary = @ssl_cipher.decrypt(binary)
285-
end
288+
# decrypt data
289+
if @encryption_enabled
290+
binary = @ssl_cipher.decrypt(binary)
291+
end
286292

287-
# create message with binary data
293+
# create message with binary data
288294
message = Wired::Message.new(:spec => @spec, :binary => binary)
289295

290296
# internally handle the message (logging, errors, etc.)
@@ -428,15 +434,15 @@ def connect_handshake?
428434
# get socket options
429435
if(@serialization == Wired::Socket::Serialization::BINARY)
430436
if(message.parameter("p7.handshake.compression") == 0)
431-
@compression_enabled = true
437+
@compression_configured = true
432438
end
433439

434440
if(message.parameter("p7.handshake.encryption") != nil)
435441
@cipher = message.parameter("p7.handshake.encryption").to_i
436442
end
437443
end
438444

439-
puts "@compression_enabled : #{@compression_enabled}"
445+
puts "@compression_configured : #{@compression_configured}"
440446

441447
if(message.parameter("p7.handshake.compatibility_check") == false)
442448
@remote_compatibility_check = false
@@ -579,8 +585,6 @@ def send_compatibilit_check
579585

580586

581587
def handle_message(message)
582-
return if !message
583-
584588
Wired::Log.debug "Received Message: " + message.name
585589
handle_error message if message.name == "wired.error"
586590
end
@@ -590,7 +594,7 @@ def handle_message(message)
590594

591595
def handle_error(message)
592596
Wired::Log.error "Wired Error: " + message.to_s
593-
@errors.push message
597+
@errors.push message
594598
end
595599

596600

@@ -630,7 +634,35 @@ def deflate_data(data)
630634

631635

632636
def inflate_data(data)
633-
#return Zlib::Inflate.inflate(data, Zlib::DEFAULT_COMPRESSION)
637+
puts "inflate_data"
638+
puts "compressed_data : "+ data.unpack("H*").to_s
639+
640+
bytes = nil
641+
c_stream = Z_stream.new
642+
c_stream.data_type = Z_UNKNOWN
643+
644+
data = Bytef.new(data)
645+
data_length = data.length
646+
647+
(2..16).step(1) do |multiple|
648+
compressed_length = data_length * (1 << multiple);
649+
compressed_data = Bytef.new(0.chr * compressed_length)
650+
651+
c_stream.next_in = data
652+
c_stream.avail_in = data.length
653+
c_stream.next_out = compressed_data
654+
c_stream.avail_out = compressed_length
655+
656+
err = inflate(c_stream, Z_FINISH)
657+
bytes = compressed_data.buffer
658+
errend = deflateEnd(c_stream)
659+
660+
puts "data : "+ bytes.unpack("H*").to_s
661+
662+
break if err == Z_STREAM_END and enderr != Z_BUF_ERROR
663+
end
664+
665+
return bytes
634666
end
635667

636668

lib/wired/topic.rb

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
module Wired
2+
class Topic
3+
attr_accessor :topic
4+
attr_accessor :nick
5+
attr_accessor :time
6+
7+
def initialize(message)
8+
@nick = message.parameter("wired.user.nick")
9+
@topic = message.parameter("wired.chat.topic.topic")
10+
@time = message.parameter("wired.chat.topic.time")
11+
end
12+
end
13+
end

0 commit comments

Comments
 (0)