warchat 0.0.7 → 0.0.8

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.
@@ -29,9 +29,7 @@ This is a simple chat client that will let you talk in guild chat and receive me
29
29
  client = Warchat::Chat::Client.new
30
30
 
31
31
  client.on_establish = Proc.new do |response|
32
- client.character_name = CHARACTER_NAME
33
- client.character_realm = CHARACTER_REALM
34
- client.login
32
+ client.login CHARACTER_NAME,CHARACTER_REALM
35
33
  end
36
34
 
37
35
  client.on_message = Proc.new do |message|
@@ -1,3 +1,4 @@
1
+ # encoding: ASCII-8BIT
1
2
  require 'active_support/inflector'
2
3
 
3
4
  module Warchat
@@ -1,3 +1,4 @@
1
+ # encoding: ASCII-8BIT
1
2
  module Warchat
2
3
  class ByteString < String
3
4
 
@@ -1,3 +1,4 @@
1
+ # encoding: ASCII-8BIT
1
2
  module Warchat
2
3
  module Chat
3
4
  module ChatResponse
@@ -1,9 +1,9 @@
1
+ # encoding: ASCII-8BIT
1
2
  module Warchat
2
3
  module Chat
3
4
  class Warchat::Chat::Client
4
5
  attr_accessor :on_message,:on_presence,:on_logout,:on_fail,:on_establish
5
6
  attr_accessor :on_message_afk,:on_message_dnd,:on_message_guild_chat,:on_message_motd,:on_message_officer_chat,:on_message_whisper,:on_chat_logout
6
- attr_accessor :character_name,:character_realm
7
7
 
8
8
  attr_reader :session
9
9
 
@@ -16,6 +16,7 @@ module Warchat
16
16
  end
17
17
 
18
18
  def start username, password
19
+ [username,password].each do |s| s.respond_to? :force_encoding and s.force_encoding(__ENCODING__) end
19
20
  self.session.start(username,password)
20
21
  end
21
22
 
@@ -32,7 +33,8 @@ module Warchat
32
33
  send(m,response) if respond_to? m
33
34
  end
34
35
 
35
- def login
36
+ def login character_name,character_realm
37
+ [character_name,character_realm].each do |s| s.respond_to? :force_encoding and s.force_encoding(__ENCODING__) end
36
38
  request = Warchat::Network::Request.new("/chat-login",:options=>{:mature_filter=>'false'},:n=>character_name,:r=>character_realm)
37
39
  session.send_request(request)
38
40
  @timer = Warchat::Timer.new(30,&method(:keep_alive))
@@ -55,11 +57,11 @@ module Warchat
55
57
  end
56
58
 
57
59
  def chat response
58
- response.extend(ChatResponse)
60
+ response.extend(Warchat::Chat::ChatResponse)
59
61
  if response.ack?
60
62
 
61
63
  elsif response.message?
62
- message = Message.new(response)
64
+ message = Warchat::Chat::Message.new(response)
63
65
  [on_message,send("on_message_#{message.type}".to_sym)].compact.each do |m| m.call(message) end
64
66
  elsif response.presence?
65
67
  on_presence and on_presence.call(Presence.new(response))
@@ -1,3 +1,4 @@
1
+ # encoding: ASCII-8BIT
1
2
  module Warchat
2
3
  module Chat
3
4
  class Message
@@ -1,3 +1,4 @@
1
+ # encoding: ASCII-8BIT
1
2
  module Warchat
2
3
  module Chat
3
4
  class Presence
@@ -1,3 +1,4 @@
1
+ # encoding: ASCII-8BIT
1
2
  module Warchat
2
3
  module Network
3
4
  class BinaryReader
@@ -11,11 +12,13 @@ module Warchat
11
12
  end
12
13
 
13
14
  def substream l
14
- @socket.read l
15
+ sub = @socket.read(l)
16
+ sub << @socket.read(l-sub.length) until sub.length >= l
17
+ sub
15
18
  end
16
19
 
17
20
  def byte
18
- substream 1
21
+ substream(1)
19
22
  end
20
23
 
21
24
  def string
@@ -23,7 +26,7 @@ module Warchat
23
26
  end
24
27
 
25
28
  def array
26
- (1..(int_32)).map &method(:parse_next)
29
+ (1..(int_32)).map(&method(:parse_next))
27
30
  end
28
31
 
29
32
  def hash
@@ -1,3 +1,4 @@
1
+ # encoding: ASCII-8BIT
1
2
  module Warchat
2
3
  module Network
3
4
  class BinaryWriter
@@ -79,7 +80,7 @@ module Warchat
79
80
  end
80
81
 
81
82
  def bytes obj
82
- @stream.print obj.to_s
83
+ @stream.print(obj.to_s)
83
84
  end
84
85
  end
85
86
  end
@@ -1,3 +1,4 @@
1
+ # encoding: ASCII-8BIT
1
2
  require 'socket'
2
3
  require 'thread'
3
4
 
@@ -66,7 +67,6 @@ module Warchat
66
67
  until is_closed?
67
68
  @mutex.synchronize do
68
69
  until @queue.empty?
69
-
70
70
  request = @queue.shift
71
71
  unless is_closed?
72
72
  request.stream @socket
@@ -1,3 +1,4 @@
1
+ # encoding: ASCII-8BIT
1
2
  module Warchat
2
3
  module Network
3
4
  class Request < Hash
@@ -30,7 +31,8 @@ module Warchat
30
31
  end
31
32
 
32
33
  def stream socket
33
- writer = BinaryWriter.new socket
34
+ str_socket = StringSocket.new
35
+ writer = BinaryWriter.new str_socket
34
36
  writer.string(target)
35
37
  writer.int_32(@id)
36
38
  each do |k,v|
@@ -50,6 +52,7 @@ module Warchat
50
52
  writer.byte 0xFF
51
53
  end
52
54
  writer.byte 0xFF
55
+ socket.print(str_socket.value)
53
56
  end
54
57
 
55
58
  def inspect
@@ -1,3 +1,4 @@
1
+ # encoding: ASCII-8BIT
1
2
  module Warchat
2
3
  module Network
3
4
  class Response < Hash
@@ -1,3 +1,4 @@
1
+ # encoding: ASCII-8BIT
1
2
  module Warchat
2
3
  module Network
3
4
  class Session
@@ -1,3 +1,4 @@
1
+ # encoding: ASCII-8BIT
1
2
  require 'digest'
2
3
  require 'openssl'
3
4
 
@@ -13,7 +14,7 @@ module Warchat
13
14
  HASH_SIZE = 32
14
15
  SESSION_KEY_SIZE = HASH_SIZE * 2
15
16
 
16
- attr_reader :b,:b_bytes
17
+ attr_reader :b,:b_bytes,:salt,:user,:password
17
18
 
18
19
  def modpow(a, n, m)
19
20
  r = 1
@@ -21,12 +22,12 @@ module Warchat
21
22
  r = r * a % m if n[0] == 1
22
23
  n >>= 1
23
24
  return r if n == 0
24
- a = a * a % m
25
+ a = (a ** 2) % m
25
26
  end
26
27
  end
27
28
 
28
29
  def pack_int int
29
- Warchat::ByteString.new [int.to_s(16).reverse].pack('h*')
30
+ Warchat::ByteString.new([int.to_s(16).reverse].pack('h*'))
30
31
  end
31
32
 
32
33
  def unpack_int str
@@ -34,11 +35,15 @@ module Warchat
34
35
  end
35
36
 
36
37
  def digest_int s
37
- unpack_int(Digest::SHA2.digest(s))
38
+ unpack_int(digest_str(s))
39
+ end
40
+
41
+ def digest_str s
42
+ Digest::SHA2.digest(s).tap do |s| s.respond_to? :force_encoding and s.force_encoding(__ENCODING__) end
38
43
  end
39
44
 
40
45
  def adjust_size str,length
41
- str[0..(length-1)].ljust(length,"\000")
46
+ Warchat::ByteString.new str[0..(length-1)].ljust(length,"\000")
42
47
  end
43
48
 
44
49
  def random_crypt
@@ -48,14 +53,14 @@ module Warchat
48
53
  def hN_xor_hG
49
54
  # xor H(N) and H(G)
50
55
  return @hN_xor_hG if @hN_xor_hG
51
- hN = Digest::SHA2.digest(pack_int(MODULUS))
52
- hG = Digest::SHA2.digest(pack_int(G))
56
+ hN = digest_str(pack_int(MODULUS)).unpack('C*')
57
+ hG = digest_str(pack_int(G)).unpack('C*')
53
58
 
54
- @hN_xor_hG = "\000" * HASH_SIZE
59
+ tmp = []
55
60
 
56
- HASH_SIZE.times do |i| @hN_xor_hG[i] = (hN[i] ^ hG[i]) end
61
+ HASH_SIZE.times do |i| tmp[i] = (hN[i] ^ hG[i]) end
57
62
 
58
- @hN_xor_hG
63
+ @hN_xor_hG = tmp.pack('C*')
59
64
  end
60
65
 
61
66
  def a
@@ -77,7 +82,7 @@ module Warchat
77
82
 
78
83
  def x
79
84
  # H(salt | H(userHash | : | sessionPassword))
80
- @x ||= digest_int(@salt+Digest::SHA2.digest(@user+":"+@password))
85
+ @x ||= digest_int(@salt+digest_str(@user+":"+@password))
81
86
  end
82
87
 
83
88
  def s
@@ -92,13 +97,18 @@ module Warchat
92
97
 
93
98
  def auth1_proof user, password, salt, b_bytes
94
99
  @b = unpack_int b_bytes
100
+
95
101
  @b_bytes = adjust_size(b_bytes,MODULUS_SIZE)
96
102
  @salt = adjust_size(salt,SALT_SIZE)
97
103
  @user = user
98
104
  @password = password
99
105
 
100
- # hash this to generate client proof, H(H(N) xor H(G) | H(userHash) | salt | A | B | K)
101
- Warchat::ByteString.new Digest::SHA2.digest(hN_xor_hG+Digest::SHA2.digest(@user)+salt+a_bytes+@b_bytes+session_key)
106
+ proof
107
+ end
108
+
109
+ def proof
110
+ # hash this to generate client proof, H(H(N) xor H(G) | H(userHash) | salt | A | B | K)
111
+ Warchat::ByteString.new digest_str(hN_xor_hG+digest_str(user)+salt+a_bytes+b_bytes+session_key)
102
112
  end
103
113
 
104
114
  def session_key
@@ -116,7 +126,7 @@ module Warchat
116
126
  temp << s_bytes[i*2+offset]
117
127
  end
118
128
 
119
- hash = Digest::SHA2.digest(temp)
129
+ hash = digest_str(temp)
120
130
  HASH_SIZE.times do |i|
121
131
  @session_key[i*2] = hash[i]
122
132
  end
@@ -126,7 +136,7 @@ module Warchat
126
136
  temp << s_bytes[i*2+offset+1]
127
137
  end
128
138
 
129
- hash = Digest::SHA2.digest(temp)
139
+ hash = digest_str(temp)
130
140
  HASH_SIZE.times do |i|
131
141
  @session_key[i*2+1] = hash[i]
132
142
  end
@@ -1,3 +1,4 @@
1
+ # encoding: ASCII-8BIT
1
2
  require 'monitor'
2
3
 
3
4
  module Warchat
@@ -1,3 +1,4 @@
1
+ # encoding: ASCII-8BIT
1
2
  module Warchat
2
- VERSION = "0.0.7"
3
+ VERSION = "0.0.8"
3
4
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: warchat
3
3
  version: !ruby/object:Gem::Version
4
- hash: 17
4
+ hash: 15
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 7
10
- version: 0.0.7
9
+ - 8
10
+ version: 0.0.8
11
11
  platform: ruby
12
12
  authors:
13
13
  - Zachary Gavin