60aa30636ef2fc2361f3a840e158c90bbc43c549
[skype-conference-recorder.git] / SkypeAPI.pm
1 #!/usr/bin/perl -w
2
3 # This is a very basic example on how to use the Skype API via DBus using perl.
4 # Written by jlh at gmx dot ch.  I declare this to be public domain, use it for
5 # whatever you like.
6
7 # This requires the module Net::DBus to be installed.
8
9 use strict;
10 use warnings;
11
12 package SkypeAPI; # -----------------------------------------------------------
13
14 # We need two objects to communicate with Skype, one for the client -> Skype
15 # direction, and one for Skype -> client direction.  This class (SkypeAPI)
16 # inherits from Net::DBus::Object and represents the DBus /com/Skype/Client
17 # object that Skype uses to notify us about events (by calling its Notify
18 # method).  The other object, which is Skype's /com/Skype object, is held in
19 # $self->{invoker}; we call its Invoke method to tell Skype our commands.
20
21 # This class by itself only does the handshake with Skype and then merely
22 # prints all notifications it receives to the terminal.  If you want to do
23 # something useful, then you have to subclass this and override the Notify
24 # method to do whatever you want.  We do this as an example with the Example
25 # class further down.
26
27 use base 'Net::DBus::Object';
28 use Net::DBus;
29
30 sub new {
31         my ($class, $name) = @_;
32
33         my $bus = Net::DBus->session;
34
35         # export a service and the object /com/Skype/Client, so Skype can
36         # invoke the 'Notify' method on it in order to communicate with us.
37         my $exp_service = $bus->export_service("com.Skype.API") or die;
38         my $self = $class->SUPER::new($exp_service, '/com/Skype/Client') or die;
39         bless $self, $class;
40
41         # get a handle to Skype's /com/Skype object, so we can invoke the
42         # 'Invoke' method on it to communicate with Skype.
43         my $service = $bus->get_service("com.Skype.API") or die;
44         $self->{invoker} = $service->get_object("/com/Skype") or die;
45
46         # setup is done, let's shake hands
47         my $r = $self->Invoke("NAME $name");
48         die "Handshake failed: $r" unless $r eq 'OK';
49         $r = $self->Invoke("PROTOCOL 5");
50         die "Handshake failed: $r" unless $r eq 'PROTOCOL 5';
51
52         return $self;
53 }
54
55 sub Notify {
56         # only print to terminal.  override this in a subclass for something
57         # more useful.
58         my ($self, $string) = @_;
59         print "-> $string\n";
60         # be careful with what you return here!  DBus will try to serialize it,
61         # returning it to skype.  you should explicitely return something
62         # simple to avoid to leak something unserializable, causing odd errors.
63         return 0;
64 }
65
66 sub Invoke {
67         # this doesn't print $string, so that subclasses can call it without
68         # that side effect.  subclass it yourself if you want it to do that.
69         my ($self, $string) = @_;
70         return $self->{invoker}->Invoke($string);
71 }
72
73 package Example; # ------------------------------------------------------------
74
75 # This Example class inherits from SkypeAPI and does something very simple:
76 # Whenever you go into away-mode, it will put you in not-available-mode
77 # instead.  That's not very useful, but it's just an example.  This is where
78 # you would implement your interesting stuff and/or write a nicer wrapper
79 # around the whole Skype API.
80
81 use base 'SkypeAPI';
82
83 sub Notify {
84         my ($self, $string) = @_;
85
86         # call the parent's Notify method, so it still prints to the terminal.
87         $self->SUPER::Notify($string);
88
89         if ($string eq 'USERSTATUS AWAY') {
90                 # away mode is no good!  let's be not available
91                 $self->Invoke('SET USERSTATUS NA');
92         }
93         return 0;
94 }
95
96 package main; # ---------------------------------------------------------------
97
98 use Net::DBus::Reactor;
99
100 my $skype = Example->new('Example');
101
102 # Run main event loop, the $skype instance has automatically been attached to
103 # it (it's a singleton).  See the documentation for Net::DBus::Reactor to learn
104 # how to attach more stuff to this loop, like file handles or timer events.
105 my $reactor = Net::DBus::Reactor->main;
106 $reactor->run;
107
108 # end of file