revent 0.3.1 → 0.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -295,14 +295,13 @@ module RubyAMF
295
295
  else
296
296
  length = type >> 1
297
297
  begin # first assume its gzipped and rescue an exception if its not
298
- inflated_stream = Zlib::Inflate.inflate( self.stream[self.stream_position,length] )
299
- arr = inflated_stream.unpack('c'*inflated_stream.length)
298
+ bytes = Zlib::Inflate.inflate(self.stream[self.stream_position, length])
300
299
  rescue Exception => e
301
- arr = self.stream[self.stream_position,length].unpack('c'*length)
302
- end
300
+ bytes = self.stream[self.stream_position, length]
301
+ end
303
302
  self.stream_position += length
304
- @stored_objects << arr
305
- arr
303
+ @stored_objects << bytes
304
+ bytes
306
305
  end
307
306
  end
308
307
 
@@ -1,5 +1,8 @@
1
1
  module RubyAMF
2
2
  module IO
3
+ # Used for sending binary data from Ruby to Flash, because the serializer
4
+ # cannot distinguish between a normal Ruby string and a string containing
5
+ # binary data.
3
6
  class ByteArray
4
7
  attr_reader :bytes
5
8
 
@@ -0,0 +1,63 @@
1
+ require 'rubygems'
2
+ require 'crypt/blowfish'
3
+ require 'RMagick'
4
+
5
+ module Revent
6
+ class Captcha
7
+ include Magick
8
+
9
+ JIGGLE = 15
10
+ WOBBLE = 20
11
+
12
+ def initialize(key, code_length, valid_period_in_sec)
13
+ @b = Crypt::Blowfish.new(key)
14
+ @code_length = code_length
15
+ @valid_period_in_sec = valid_period_in_sec
16
+ end
17
+
18
+ # Returns [encrypted_code_with_timestamp, img].
19
+ def new
20
+ code, img = private_new
21
+ code_with_timestamp = code + Time.now.to_i.to_s
22
+ encrypted_code_with_timestamp = @b.encrypt_string(code_with_timestamp)
23
+ [encrypted_code_with_timestamp, img]
24
+ end
25
+
26
+ # Returns true if the code matches and within the valid period.
27
+ def correct?(code, encrypted_code_with_timestamp)
28
+ code_with_timestamp = @b.decrypt_string(encrypted_code_with_timestamp)
29
+ code0 = code_with_timestamp.slice!(0, @code_length)
30
+ timestamp0 = code_with_timestamp.to_i
31
+ code == code0 and Time.now.to_i - timestamp0 < @valid_period_in_sec
32
+ end
33
+
34
+ private
35
+
36
+ # Returns [code (a string of A-Z), img]
37
+ VOCA = ('A'..'Z').to_a.freeze
38
+ def private_new
39
+ code_array = []
40
+ voca_length = VOCA.length
41
+ 1.upto(@code_length) { code_array << VOCA[rand(voca_length)] }
42
+ granite = Magick::ImageList.new('granite:')
43
+ canvas = Magick::ImageList.new
44
+ canvas.new_image(33*@code_length, 40, Magick::TextureFill.new(granite))
45
+ text = Magick::Draw.new
46
+ #text.font_family = 'times'
47
+ text.pointsize = 40
48
+ cur = 10
49
+
50
+ code_array.each do |c|
51
+ rot = (rand(10) > 5)? rand(WOBBLE): -rand(WOBBLE)
52
+ text.annotate(canvas, 0, 0, cur, 25 + rand(JIGGLE), c) do
53
+ self.rotation = rot
54
+ self.fill = 'darkred'
55
+ end
56
+ cur += 30
57
+ end
58
+ code = code_array.to_s
59
+ img = canvas.to_blob { self.format = 'JPG'; self.quality = 60 }
60
+ [code, img]
61
+ end
62
+ end
63
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: revent
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
4
+ version: "0.4"
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ngoc DAO Thanh
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2008-02-20 00:00:00 +09:00
12
+ date: 2008-02-23 00:00:00 +09:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
@@ -37,6 +37,7 @@ files:
37
37
  - lib/revent/as_r.rb
38
38
  - lib/revent/Client.as
39
39
  - lib/revent/synchronize.rb
40
+ - lib/revent/captcha.rb
40
41
  - lib/revent/amf3
41
42
  - lib/revent/amf3/app
42
43
  - lib/revent/amf3/app/fault_object.rb