psyho-skype 0.1.0

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.
@@ -0,0 +1,9 @@
1
+ 2010/08/12
2
+ - Added some very basic instructions.
3
+
4
+ 2010/04/12
5
+ - Simple class structure for sending chat messages
6
+
7
+ 2010/04/11
8
+ - Started development, with a simple file that uses various chat-related aspects of the raw dbus-transported API.
9
+ - Added dependency: dbus => http://github.com/mvidner/ruby-dbus/downloads
@@ -0,0 +1,21 @@
1
+ require 'forwardable'
2
+
3
+ require 'skype/api'
4
+ require 'skype/chat'
5
+ require 'skype/chatmessage'
6
+ require 'skype/events'
7
+
8
+ module Skype
9
+ class << self
10
+ extend Forwardable
11
+
12
+ def_delegator Skype::Api, :instance, :instance
13
+ def_delegator Skype::Api, :attach
14
+ def_delegator Skype::Chat, :new, :chat
15
+ def_delegator Skype::Chat, :all, :chats
16
+ def_delegator Skype::Events, :on
17
+
18
+ Skype::Events.initialize_listeners
19
+ end
20
+ end
21
+
@@ -0,0 +1,111 @@
1
+ require 'dbus'
2
+ require 'forwardable'
3
+
4
+ module Skype
5
+ class Notify < DBus::Object
6
+ dbus_interface "com.Skype.API.Client" do
7
+ dbus_method :Notify, "in data:s" do |message|
8
+ Api.notify(message)
9
+ end
10
+ end
11
+ end
12
+
13
+ class Api
14
+ class << self
15
+ extend Forwardable
16
+ def_delegators :instance, :attach, :invoke, :on_notification, :notify
17
+
18
+ def instance
19
+ @instance ||= new
20
+ end
21
+ end
22
+
23
+ def attach(application_name="ruby-skype")
24
+ raise "Already attached." if @attached
25
+
26
+ # Say hi to Skype.
27
+ api.Invoke "NAME #{application_name}"
28
+ api.Invoke "PROTOCOL 7"
29
+
30
+ run_notification_thread
31
+
32
+ @attached = true
33
+ end
34
+
35
+ def invoke(message, &block)
36
+ raise "Not attached to skype. Call Skype::Api.attach first." unless @attached
37
+
38
+ log_outgoing message
39
+ if block_given?
40
+ api.Invoke(message) do |headers, answer|
41
+ log_incoming answer
42
+ block.call(answer)
43
+ end
44
+ else
45
+ answer = api.Invoke(message) do |_, _|
46
+ # Huh? Without passing in a block, sometimes it hangs...??
47
+ end
48
+ log_incoming answer
49
+ answer
50
+ end
51
+ end
52
+
53
+ def on_notification(scope, proc=nil, &block)
54
+ raise "Need to register callbacks before attaching to Skype." if @attached
55
+
56
+ callbacks[scope] ||= []
57
+ callbacks[scope] << (proc ? proc : block)
58
+ end
59
+
60
+ def notify(message)
61
+ log_incoming message
62
+
63
+ callbacks.keys.each do |key|
64
+ next unless match = Regexp.new("^#{key}").match(message)
65
+ callbacks[key].each{ |callback| callback.call(*match.captures) }
66
+ end
67
+ end
68
+
69
+ private
70
+
71
+ def initialize
72
+ # 'pu'a says no.
73
+ end
74
+
75
+ def api
76
+ @api ||= begin
77
+ skype_service = bus.service("com.Skype.API")
78
+ skype_object = skype_service.object('/com/Skype')
79
+ skype_object.introspect
80
+ skype_object["com.Skype.API"]
81
+ end
82
+ end
83
+
84
+ def run_notification_thread
85
+ thread = Thread.new do
86
+ receiving_service = bus.request_service("com.nikofelger.ruby-skype")
87
+ receiving_service.export(Notify.new("/com/Skype/Client"))
88
+ dbus_event_loop = DBus::Main.new
89
+ dbus_event_loop << bus
90
+ dbus_event_loop.run
91
+ end
92
+ thread.run
93
+ end
94
+
95
+ def bus
96
+ DBus::SessionBus.instance
97
+ end
98
+
99
+ def callbacks
100
+ @callback ||= {}
101
+ end
102
+
103
+ def log_incoming(message)
104
+ STDERR.puts "<- #{message}"
105
+ end
106
+
107
+ def log_outgoing(message)
108
+ STDERR.puts "-> #{message}"
109
+ end
110
+ end
111
+ end
@@ -0,0 +1,28 @@
1
+ require 'skype/events'
2
+
3
+ module Skype
4
+ class Chat
5
+ class << self
6
+ attr_accessor :chats
7
+
8
+ def all
9
+ chats if chats
10
+
11
+ Api.invoke("SEARCH CHATS")
12
+ sleep 0.01 while chats.nil?
13
+
14
+ yield chats
15
+ end
16
+ end
17
+
18
+ attr_reader :chatname
19
+
20
+ def initialize(chatname)
21
+ @chatname = chatname
22
+ end
23
+
24
+ def send_message(message)
25
+ Api.invoke("CHATMESSAGE #{@chatname} #{message}")
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,27 @@
1
+ module Skype
2
+ class Chatmessage
3
+ def initialize(id)
4
+ @chatmessage_id = id
5
+ end
6
+
7
+ def chat(&block)
8
+ return unless block_given?
9
+ get_property("CHATNAME") do |chatname|
10
+ block.call(Chat.new(chatname))
11
+ end
12
+ end
13
+
14
+ def body(&block)
15
+ return unless block_given?
16
+ get_property("BODY", &block)
17
+ end
18
+
19
+ private
20
+ def get_property(property, &block)
21
+ return unless block_given?
22
+ Api.invoke("GET CHATMESSAGE #{@chatmessage_id} #{property}") do |message|
23
+ yield message.split[3..-1].join(' ')
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,29 @@
1
+ require 'thread'
2
+
3
+ module Skype
4
+ class Events
5
+ class << self
6
+ def on(event)
7
+ case event
8
+
9
+ when :chatmessage_received
10
+ Skype::Api.instance.on_notification("CHATMESSAGE (.*) STATUS RECEIVED") do |chatmessage_id|
11
+ yield Chatmessage.new(chatmessage_id)
12
+ end
13
+
14
+ when :chats_received
15
+ Skype::Api.instance.on_notification("CHATS (.*)") do |chatlist|
16
+ yield chatlist.split(', ').map { |chatname| Chat.new(chatname) }
17
+ end
18
+ end
19
+ end
20
+
21
+ def initialize_listeners
22
+ mutex = Mutex.new
23
+ Skype::Events.on(:chats_received) do |chats|
24
+ mutex.synchronize { Skype::Chat.chats = chats }
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
metadata ADDED
@@ -0,0 +1,80 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: psyho-skype
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.1.0
6
+ platform: ruby
7
+ authors:
8
+ - Niko Felger
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2011-05-17 00:00:00 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: rspec
17
+ prerelease: false
18
+ requirement: &id001 !ruby/object:Gem::Requirement
19
+ none: false
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "0"
24
+ type: :development
25
+ version_requirements: *id001
26
+ - !ruby/object:Gem::Dependency
27
+ name: cucumber
28
+ prerelease: false
29
+ requirement: &id002 !ruby/object:Gem::Requirement
30
+ none: false
31
+ requirements:
32
+ - - ">="
33
+ - !ruby/object:Gem::Version
34
+ version: "0"
35
+ type: :development
36
+ version_requirements: *id002
37
+ description:
38
+ email: niko.felger@gmail.com
39
+ executables: []
40
+
41
+ extensions: []
42
+
43
+ extra_rdoc_files: []
44
+
45
+ files:
46
+ - History.txt
47
+ - lib/skype.rb
48
+ - lib/skype/chat.rb
49
+ - lib/skype/chatmessage.rb
50
+ - lib/skype/events.rb
51
+ - lib/skype/api.rb
52
+ homepage: http://github.com/nfelger/skype
53
+ licenses: []
54
+
55
+ post_install_message:
56
+ rdoc_options: []
57
+
58
+ require_paths:
59
+ - lib
60
+ required_ruby_version: !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ">="
64
+ - !ruby/object:Gem::Version
65
+ version: "0"
66
+ required_rubygems_version: !ruby/object:Gem::Requirement
67
+ none: false
68
+ requirements:
69
+ - - ">="
70
+ - !ruby/object:Gem::Version
71
+ version: "0"
72
+ requirements: []
73
+
74
+ rubyforge_project:
75
+ rubygems_version: 1.7.2
76
+ signing_key:
77
+ specification_version: 3
78
+ summary: Skype Api wrapper
79
+ test_files: []
80
+