Ruby4Skype 0.3.1 → 0.4.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (52) hide show
  1. data/README +95 -7
  2. data/Rakefile +3 -3
  3. data/lib/{skypeapi.rb → skype.rb} +102 -89
  4. data/lib/{skypeapi → skype}/application.rb +5 -5
  5. data/lib/{skypeapi → skype}/call.rb +9 -9
  6. data/lib/{skypeapi → skype}/chat.rb +176 -176
  7. data/lib/{skypeapi → skype}/chatmember.rb +31 -31
  8. data/lib/{skypeapi → skype}/chatmessage.rb +9 -9
  9. data/lib/{skypeapi → skype}/error.rb +1 -1
  10. data/lib/{skypeapi → skype}/event.rb +2 -2
  11. data/lib/{skypeapi → skype}/filetransfer.rb +2 -2
  12. data/lib/{skypeapi → skype}/group.rb +8 -8
  13. data/lib/{skypeapi → skype}/menuitem.rb +4 -4
  14. data/lib/{skypeapi → skype}/message.rb +1 -1
  15. data/lib/{skypeapi → skype}/object.rb +16 -15
  16. data/lib/{skypeapi → skype}/os/etc.rb +35 -9
  17. data/lib/skype/os/linux.rb +115 -0
  18. data/lib/skype/os/linux.rb~ +117 -0
  19. data/lib/skype/os/mac.rb +217 -0
  20. data/lib/{skypeapi → skype}/os/notifier.rb +1 -1
  21. data/lib/skype/os/timer.rb +108 -0
  22. data/lib/{skypeapi → skype}/os/window_event_queue.rb +16 -16
  23. data/lib/{skypeapi → skype}/os/window_messagehandler.rb +12 -12
  24. data/lib/{skypeapi → skype}/os/windows.rb +10 -10
  25. data/lib/{skypeapi → skype}/profile.rb +3 -3
  26. data/lib/{skypeapi → skype}/sharefunctions.rb +6 -6
  27. data/lib/{skypeapi → skype}/sms.rb +3 -3
  28. data/lib/{skypeapi → skype}/user.rb +1 -1
  29. data/lib/{skypeapi → skype}/version.rb +2 -2
  30. data/lib/{skypeapi → skype}/voicemail.rb +4 -4
  31. data/spec/{skypeapi → skype}/application_spec.rb +13 -15
  32. data/spec/{skypeapi → skype}/chat_spec.rb +75 -77
  33. data/spec/{skypeapi → skype}/chatmember_spec.rb +13 -13
  34. data/spec/{skypeapi → skype}/chatmessage_spec.rb +12 -13
  35. data/spec/skype/event_spec.rb +33 -0
  36. data/spec/{skypeapi → skype}/filetransfer_spec.rb +10 -11
  37. data/spec/skype/group_spec.rb +15 -0
  38. data/spec/{skypeapi → skype}/menuitem_spec.rb +11 -9
  39. data/spec/skype/os/linux_spec.rb +59 -0
  40. data/spec/skype/os/linux_spec.rb~ +58 -0
  41. data/spec/{skypeapi → skype}/os/windows_spec.rb +8 -8
  42. data/spec/skype/profile_spec.rb +24 -0
  43. data/spec/skype/user_spec.rb +25 -0
  44. data/spec/skype_spec.rb +530 -0
  45. metadata +44 -40
  46. data/lib/skypeapi/os/linux.rb +0 -110
  47. data/lib/skypeapi/os/mac.rb +0 -98
  48. data/spec/skypeapi/event_spec.rb +0 -31
  49. data/spec/skypeapi/group_spec.rb +0 -16
  50. data/spec/skypeapi/profile_spec.rb +0 -22
  51. data/spec/skypeapi/user_spec.rb +0 -25
  52. data/spec/skypeapi_spec.rb +0 -528
@@ -0,0 +1,217 @@
1
+ require 'osx/cocoa'
2
+ require 'thread'
3
+ Dir.chdir '../../' if __FILE__ == $0
4
+ require 'skype/os/etc.rb'
5
+
6
+ OSX.require_framework 'Skype'
7
+
8
+ module Skype
9
+ module OS
10
+ class Mac < Skype::OS::Abstruct
11
+
12
+ class MessageHandler < OSX::NSObject
13
+
14
+ def _init os, name
15
+ @os = os
16
+ @clientApplicationName = name
17
+ end
18
+
19
+ def clientApplicationName
20
+ @clientApplicationName
21
+ end
22
+
23
+ addRubyMethod_withType 'skypeAttachResponse:', 'v@:i'
24
+ def skypeAttachResponse status
25
+ if status == 1
26
+ @os.attached = true
27
+ OSX::SkypeAPI.sendSkypeCommand('PROTOCOL 9999')
28
+ else
29
+ @os.attached = false
30
+ end
31
+ end
32
+
33
+ def skypeBecameAvilable
34
+ end
35
+
36
+ def skypeBecameUnavilable
37
+ end
38
+
39
+ #addRubyMethod_withType 'skypeNotificationReceived:', 'v@:i'
40
+ def skypeNotificationReceived notification_string
41
+ p "[#{notification_string.to_s}]" if @os.debug
42
+
43
+ if notification_string.to_s =~ /^#(\d+) (.+)$/m
44
+ send_count = $1.to_i; res = $2
45
+ @os.response[send_count] = notification_string.to_s
46
+ else
47
+ @os.push_queue notification_string.to_s
48
+ end
49
+ end
50
+ end
51
+
52
+ def initialize client_application_name='ruby4skype'
53
+ @queue = Queue.new
54
+ @attached = false
55
+ @debug = false
56
+
57
+ @send_count = 0
58
+ @send_count_mutex = Mutex.new
59
+ @response = Hash.new
60
+
61
+ @msg = MessageHandler.alloc.init
62
+ @msg._init self, client_application_name
63
+ OSX::SkypeAPI.setSkypeDelegate @msg
64
+
65
+ app = OSX::NSApplication.sharedApplication
66
+ unless app.isRunning
67
+ @t =Thread.new do
68
+ sleep 1
69
+ app.run
70
+ end
71
+ end
72
+ #OSX::NSRunLoop.currentRunLoop.run
73
+ end
74
+ attr_reader :queue, :name, :attached, :response, :debug
75
+ attr_writer :attached
76
+ private :queue
77
+
78
+ def set_notify_selector block=Proc.new
79
+ @notify_selector = block
80
+ end
81
+ attr_reader :notify_selector
82
+
83
+ def attach
84
+ unless attached?
85
+ OSX::SkypeAPI.connect
86
+ end
87
+ end
88
+
89
+ def attach_wait
90
+ attach
91
+ sleep 0.123 until attached?
92
+ OSX::SkypeAPI.sendSkypeCommand('PROTOCOL 9999')
93
+ end
94
+
95
+ def attached?
96
+ @attached
97
+ end
98
+
99
+ def dettach
100
+ self.attached = false
101
+ OSX::SkypeAPI.disconnect
102
+ end
103
+
104
+ def skype_runnging?
105
+ OSX::SkypeAPI.isSkypeRunnging
106
+ end
107
+
108
+ def invoke_prototype cmd
109
+ send_count = send_count_increment
110
+ cmd = "##{send_count} #{cmd}"
111
+
112
+ #@queue.push(proc{do_event(:sent, cmd)}) if exist_event? :sent
113
+ p ">#{cmd}" if @debug
114
+
115
+ res = OSX::SkypeAPI.sendSkypeCommand(cmd).to_s
116
+ if res.empty?
117
+ begin
118
+ timeout(5){sleep 0.123 until response[send_count]}
119
+ rescue TimeoutError
120
+ raise Skype::Error::API.new("#{cmd} => no response. it may be skype bug")
121
+ end
122
+ res = response[send_count]
123
+ response.delete send_count
124
+ end
125
+
126
+ p "<#{res}" if @debug
127
+ #@queue.push(proc{do_event(:received, res)}) if exist_event? :received
128
+
129
+ res = $1 if res =~ /^#\d+ (.+)$/m
130
+
131
+ return res
132
+ end
133
+
134
+ def send_count_increment
135
+ send_count = nil
136
+ @send_count_mutex.synchronize do
137
+ send_count = @send_count
138
+ @send_count+=1
139
+ end
140
+ send_count
141
+ end
142
+ private :send_count_increment
143
+
144
+ def invoke_callback cmd,cb=Proc.new
145
+ res = invoke_block cmd
146
+ cb.call res
147
+ end
148
+
149
+ alias invoke_block invoke_prototype
150
+
151
+ def start_messageloop
152
+ Thread.new{messageloop}
153
+ end
154
+
155
+ def messageloop
156
+ while callback = queue.shift
157
+ break unless message_process callback
158
+ end
159
+ end
160
+
161
+ def messagepolling
162
+ return message_process(queue.shift) unless queue.empty?
163
+ return true
164
+ end
165
+
166
+ def message_process callback
167
+ #sleep 0.001 while paused?
168
+ return false if callback == :exit
169
+ callback.call
170
+ return true
171
+ end
172
+ private :message_process
173
+
174
+ def push_queue res
175
+ queue.push(proc{notify_selector.call res}) if notify_selector
176
+ end
177
+
178
+ def close
179
+ dettach if attached?
180
+ OSX::SkypeAPI.removeSkypeDelegate
181
+ #OSX::NSApplication.sharedApplication.stop(@msg)
182
+ #OSX::NSApplication.sharedApplication.terminate(@msg)
183
+ #@msg.dealloc
184
+ #@t.kill
185
+ queue.push :exit
186
+ end
187
+ #def close()
188
+ # dettach if attached?
189
+ #end
190
+
191
+ end
192
+ end
193
+ end
194
+
195
+
196
+ if __FILE__ == $0
197
+ Thread.abort_on_exception = true
198
+
199
+ os = Skype::OS::Mac.new 'hogehogehogea'
200
+ os.set_notify_selector{|res| p res}
201
+ os.start_messageloop
202
+ p :hoge
203
+ os.attach_wait
204
+ p :hoge
205
+ p tmp = os.invoke('CHAT CREATE echo123')
206
+ tmp =~ /CHAT (.+) /
207
+ p cid = $1
208
+ os.invoke("CHATMESSAGE #{cid} hoge")
209
+
210
+ begin
211
+ p os.invoke("GET AGC")
212
+ rescue
213
+ p $!
214
+ end
215
+ sleep 10
216
+ os.close
217
+ end
@@ -1,4 +1,4 @@
1
- module SkypeAPI
1
+ module Skype
2
2
  module OS
3
3
  class Notifier
4
4
  def notify
@@ -0,0 +1,108 @@
1
+ #module Skype
2
+ # module OS
3
+ # class Windows
4
+ class Timer
5
+ class << self
6
+ def interval term, block=Proc.new
7
+ Interval.new term, block
8
+ end
9
+
10
+ def timeout term, block=Proc.new
11
+ Timeout.new term, block
12
+ end
13
+
14
+ def delete instance
15
+ case instance.class
16
+ when Timeout
17
+ instance.delete
18
+ when Interval
19
+ instance.delete
20
+ else
21
+ raise ArgumentError
22
+ end
23
+ end
24
+
25
+ def polling
26
+ now = Time.now.to_i
27
+ Timeout.polling now
28
+ Interval.polling now
29
+ end
30
+ end
31
+
32
+ class Abstruct
33
+
34
+ class << self
35
+
36
+ def new term, block
37
+ instance = super
38
+ @stack << instance
39
+ return instance
40
+ end
41
+
42
+ def delete instance
43
+ @stack.delete instance
44
+ end
45
+
46
+ end
47
+
48
+
49
+ def delete
50
+ self.class.delete self
51
+ end
52
+
53
+ attr_reader :term, :block
54
+
55
+ end
56
+
57
+ class Timeout < Abstruct
58
+ @stack = Array.new
59
+ class << self
60
+
61
+ def polling now
62
+ @stack.delete_if do |timeout|
63
+ if now >= timeout.term
64
+ timeout.block.call
65
+ true
66
+ else
67
+ false
68
+ end
69
+ end
70
+ end
71
+
72
+ end
73
+
74
+ def initialize term, block
75
+ @term = term + Time.now.to_i
76
+ @block = block
77
+ end
78
+
79
+ end
80
+
81
+ class Interval < Abstruct
82
+ @stack = Array.new
83
+
84
+ class << self
85
+
86
+ def polling now
87
+ @stack.each do |interval|
88
+ if now >= interval.term + interval.old
89
+ interval.old = now
90
+ interval.block.call
91
+ end
92
+ end
93
+ end
94
+
95
+ end
96
+
97
+ def initialize term, block
98
+ @term = term
99
+ @old = Time.now.to_i
100
+ @block = block
101
+ end
102
+
103
+ attr_accessor :old
104
+ end
105
+ end
106
+ # end
107
+ # end
108
+ #end
@@ -6,7 +6,7 @@ class BlockQueue < Queue #:nodoc: all
6
6
  end
7
7
  end
8
8
 
9
- module SkypeAPI
9
+ module Skype
10
10
  module OS
11
11
  class WindowsEventQueue
12
12
  def initialize windows
@@ -97,27 +97,27 @@ module SkypeAPI
97
97
  end
98
98
 
99
99
  def start_messageloop
100
- @messageloop_thread = Thread.new{messageloop}
100
+ Thread.new{messageloop}
101
101
  end
102
102
 
103
103
  def messageloop
104
- @messageloop_flag = true
105
104
  while callback = queue.shift
106
- sleep 0.001 while paused?
107
- break if callback == :exit
108
- callback.call
105
+ break unless message_process callback
109
106
  end
110
- @messageloop_flag = false
111
107
  end
112
108
 
113
109
  def messagepolling
114
- until queue.empty?
115
- sleep 0.001 while paused?
116
- callback = queue.shift
117
- break if callback == :exit
118
- callback.call
119
- end
110
+ return message_process(queue.shift) unless queue.empty?
111
+ return true
112
+ end
113
+
114
+ def message_process callback
115
+ sleep 0.001 while paused?
116
+ return false if callback == :exit
117
+ callback.call
118
+ return true
120
119
  end
120
+ private :message_process
121
121
 
122
122
  def close
123
123
  queue.clear
@@ -142,14 +142,14 @@ module SkypeAPI
142
142
  @pause_mutex.synchronized{@pause_count += 1}
143
143
  end
144
144
 
145
- def paly
145
+ def play
146
146
  @pause_mutex.synchronized{@pause_count -=1}
147
147
  end
148
148
 
149
149
  def push_detached_hook
150
150
  #windows.attached = false
151
151
  queue.push_block{ call_hook(:detached) }
152
- #SkypeAPI.attach
152
+ #Skype.attach
153
153
  end
154
154
 
155
155
  private
@@ -166,7 +166,7 @@ module SkypeAPI
166
166
  def when_detached
167
167
  windows.attached = false
168
168
  queue.push_block{ call_hook(:detached) }
169
- SkypeAPI.attach
169
+ Skype.attach
170
170
  end
171
171
 
172
172
  def set_cmd_response(cmd_num, res)
@@ -1,4 +1,4 @@
1
- module SkypeAPI
1
+ module Skype
2
2
  module OS
3
3
  class MessageHandler < SWin::Window
4
4
 
@@ -7,10 +7,10 @@ module SkypeAPI
7
7
  @queue = queue
8
8
 
9
9
  @dwDiscoverMsg = RegisterWindowMessage.call("SkypeControlAPIDiscover");
10
- raise SkypeAPI::Error::Attach.new("SkypeControlAPIDiscover nothing") unless @dwDiscoverMsg
10
+ raise Skype::Error::Attach.new("SkypeControlAPIDiscover nothing") unless @dwDiscoverMsg
11
11
 
12
12
  @dwAttachMsg = RegisterWindowMessage.call("SkypeControlAPIAttach")
13
- raise SkypeAPI::Error::Attach.new("SkypeControlAPIAttach nothing") unless @dwAttachMsg
13
+ raise Skype::Error::Attach.new("SkypeControlAPIAttach nothing") unless @dwAttachMsg
14
14
 
15
15
  addEvent(WM_COPYDATA)
16
16
  addEvent(WM_USER_MSG)
@@ -21,7 +21,7 @@ module SkypeAPI
21
21
 
22
22
  def attach
23
23
  unless PostMessage.call(HWND_BROADCAST, @dwDiscoverMsg, hWnd, 0)
24
- raise SkypeAPI::Error::Attach.new("SkypeControlAPIDiscover broadcast failure")
24
+ raise Skype::Error::Attach.new("SkypeControlAPIDiscover broadcast failure")
25
25
  end
26
26
  return true
27
27
  end
@@ -35,17 +35,17 @@ module SkypeAPI
35
35
  end
36
36
 
37
37
  def invoke num, cmd
38
- unless @hSkypeAPIWindowHandle
39
- raise SkypeAPI::Error::Attach.new("NullPointerException SendSkype!")
38
+ unless @hSkypeWindowHandle
39
+ raise Skype::Error::Attach.new("NullPointerException SendSkype!")
40
40
  return false
41
41
  end
42
42
 
43
43
  cmd = '#' + num.to_s + ' ' + cmd + "\0"
44
44
  pCopyData = application.arg2cstructStr("LLS",0,cmd.length+1,cmd)
45
45
  send_buffer[num] = cmd
46
- unless PostMessage.call(hWnd, WM_USER_MSG, @hSkypeAPIWindowHandle, pCopyData)
47
- @hSkypeAPIWindowHandle = nil
48
- raise SkypeAPI::Error::Attach.new("Skype not ready")
46
+ unless PostMessage.call(hWnd, WM_USER_MSG, @hSkypeWindowHandle, pCopyData)
47
+ @hSkypeWindowHandle = nil
48
+ raise Skype::Error::Attach.new("Skype not ready")
49
49
  end
50
50
  @queue.push_hook(:sent, cmd.chop) if @queue.exist_hook? :sent
51
51
  return true
@@ -56,7 +56,7 @@ module SkypeAPI
56
56
  when @dwAttachMsg
57
57
  case sMsg.lParam
58
58
  when SKYPECONTROLAPI_ATTACH_SUCCESS
59
- @hSkypeAPIWindowHandle = sMsg.wParam
59
+ @hSkypeWindowHandle = sMsg.wParam
60
60
  invoke_protocol
61
61
  @queue.push_hook(:attach,:success)
62
62
 
@@ -90,7 +90,7 @@ module SkypeAPI
90
90
  end
91
91
  sMsg.retval = 1
92
92
  when WM_COPYDATA
93
- if sMsg.wParam == @hSkypeAPIWindowHandle
93
+ if sMsg.wParam == @hSkypeWindowHandle
94
94
  retval = application.cstruct2array(sMsg.lParam,"LLL")
95
95
  cmd = application.pointer2string(retval[2],retval[1])
96
96
  @queue.push cmd
@@ -98,7 +98,7 @@ module SkypeAPI
98
98
  end
99
99
  when WM_USER_MSG
100
100
  unless SendMessage.call(sMsg.wParam, WM_COPYDATA, sMsg.hWnd, sMsg.lParam)
101
- raise SkypeAPI::Error::Attach.new("Skype not ready")
101
+ raise Skype::Error::Attach.new("Skype not ready")
102
102
  end
103
103
  sMsg.retval = 1
104
104
  else