Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save iamjarvo/2889222 to your computer and use it in GitHub Desktop.
Save iamjarvo/2889222 to your computer and use it in GitHub Desktop.
Skype API example in Ruby
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