Created
September 20, 2009 08:34
-
-
Save ebisawa/189759 to your computer and use it in GitHub Desktop.
Skype API example in Ruby
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'dbus' | |
MYNAME = 'com.example.skype_api_client' | |
class SkypeAPI | |
def initialize(name = MYNAME) | |
bus = DBus.session_bus | |
service = bus.service("com.Skype.API") | |
objs = service.object("/com/Skype") | |
objs.introspect | |
@api = objs["com.Skype.API"] | |
invoke("NAME #{name}") | |
invoke("PROTOCOL 5") | |
end | |
def run | |
loop = DBus::Main.new | |
loop << DBus::SessionBus.instance | |
loop.run | |
end | |
def invoke(cmd) | |
r = @api.Invoke(cmd) | |
r[0] | |
end | |
end | |
class SkypeClient < DBus::Object | |
dbus_interface "com.Skype.API.Client" do | |
dbus_method :Notify, "in msg:s" do |msg| | |
proc_message(msg) | |
end | |
end | |
def initialize(name = MYNAME) | |
super("/com/Skype/Client") | |
bus = DBus.session_bus | |
service = bus.request_service(name) | |
service.export(self) | |
@api = SkypeAPI.new | |
end | |
def run | |
@api.run | |
end | |
private | |
def proc_message(msg) | |
a = msg.split(" ") | |
case a[0] | |
when "CHATMESSAGE" | |
proc_chat_message(msg) | |
end | |
end | |
def proc_chat_message(msg) | |
a = msg.split(" ") | |
if a[2] == "STATUS" && a[3] == "RECEIVED" | |
skypemsg = SkypeMessage.new(@api, a[1]) | |
# XXX dispatch skyemsg to somewhere you want | |
end | |
end | |
end | |
class SkypeMessage | |
def initialize(api, msgid) | |
@api = api | |
@msgid = msgid | |
end | |
def body | |
get_cmsg("BODY") | |
end | |
def from_handle | |
get_cmsg("FROM_HANDLE") | |
end | |
def from_dispname | |
get_cmsg("FROM_DISPNAME") | |
end | |
def chatname | |
get_cmsg("CHATNAME") | |
end | |
private | |
def get_cmsg(prop) | |
cmd = "CHATMESSAGE #{@msgid} #{prop}" | |
r = @api.invoke("GET #{cmd}") | |
r.sub("#{cmd} ", "") | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment