Mayflower Barcamp: Developing a PHP extension for Skype

Avatar von Andreas Uhsemann

At the Mayflower Barcamp a few weeks ago we had a very interesting and promising project: Develop a PHP extension for communication with Skype. Actually, this project was a merge between two projects: One was to „develop a PHP extension“ – five out of six project members never had written any PHP extension or did any PHP core development before. The other project was the Skype extension, with the goal to have a new PHP function that could send a chat message to a given Skype profile.

Fortunately, we have got some PHP extension knowhow inhouse and got a short overview about creating PHP extensions in general, about the ext_skel shell script to create an extension skeleton, the basic knowledge about the config.m4 and so on by one of our PHP core experts.

All that happened on day one. We were able to compile the basic extension after only a few hours and could start analysing the Skype API documentation. We were happy to have the API documentation all in one file (https://developer.skype.com/Docs/ApiDoc/src) b/c the network at the Barcamp was a little unstable (notice for next time: We need a *stable* Internet connection) so we could still read and navigate in the documentation while we were being offline.

Very soon it was clear that we were not only developing a specialised Skype extension but more general a PHP extension that could talk via D-Bus to any software that communicates over D-Bus. The research for D-Bus was also taking most of the time. We were even trying to get the extension running under Windows, but gave up on the messaging system there because of time pressure (although we managed to install and configure the complete compiling environment using only free and open source tools from scratch on a machine that had never compiled any C code before).

To send a message via D-Bus on Linux one has to first create a new message call:

message = dbus_message_new_method_call (
	"com.Skype.API",
	"/com/Skype",
	"com.Skype.API",
	"Invoke");

Then a couple of parameters have to get set:

dbus_message_set_auto_start (message, TRUE);
if ( !dbus_message_append_args( message,
	DBUS_TYPE_STRING,
	&msg,
	DBUS_TYPE_INVALID )) {
		php_error(E_ERROR, "DBusConnection could not be set to auto start %sn", error.message);
	        dbus_error_free (&error);
        	return;
	}

Finally, we just have to „send and block“ to send the message via D-Bus:

reply = dbus_connection_send_with_reply_and_block (connection,
	message,
	reply_timeout,
	&error);

After that, only some additional error checking is necessary and we can close the D-Bus connection again.

The second part was Skype: After reading the Skype API documentation we saw that one has to send only a few commands via D-Bus to Skype to send a short chat message to a contact. For security reasons, Skype shows a popup box to ask the user if a particular application is allowed to access Skype (the webserver and Skype need to run under the same user id). When specifiying the first command as „NAME php“ Skype will ask the user whether „php“ is allowed to access the API. Then „PROTOCOL 2“ is chosen (which is sufficient for chat messages).

After that one just has to create a connection („CHAT CREATE profilename“) and send the message with „CHATMESSAGE chatid message“. The chatid is returned by CHAT CREATE. Finally, the D-Bus connection has to get closed and we are done:

conn = dbus_bus_get (DBUS_BUS_SESSION, &error);
[...]
tmp = sendDBusMessage(conn, "NAME php");
tmp = sendDBusMessage(conn, "PROTOCOL 2");
sprintf(chatmsg, "CHAT CREATE %s", profilename);
tmp = sendDBusMessage(conn, chatmsg);
[...]
chatid = emalloc( strlen(tmp) - 19);
[...]
sprintf(chatmsg, "CHATMESSAGE %s %s", chatid, message);
[...]
tmp = sendDBusMessage(conn, chatmsg);

It’s just a little bit of code, but we had to do a lot of research. If you are interested in having a look at this work, feel free to access the repository at

cvs -d :pserver:anonymous@mayflowersystem.com:/cvsroot/barcamp login
cvs -d :pserver:anonymous@mayflowersystem.com:/cvsroot/barcamp checkout skype

Avatar von Andreas Uhsemann

Kommentare

11 Antworten zu „Mayflower Barcamp: Developing a PHP extension for Skype“

  1. Keep working – skype module is that what I need (but I can’t code in C :-( ).

  2. Cool stuff! You think this can grow into a pecl extension some day?

  3. sehr nett!

    funktioniert einwandfrei. Danke für den Denkanstoß.

    Grüße

  4. How about using a PHP based solution to talk to a D-Bus session?

    http://article.gmane.org/gmane.comp.freedesktop.dbus/9025

    This should be more flexible than a C extension IMO.

  5. i cant access the cvs repo.. whats is the password?

    1. Hi Yoshi, we’ve moved the server with the repository, the PHP extension for Skype should now be reachable here:

      cvs -d :pserver:anonymous@mayflowersystem.com:/cvsroot/barcamp login
      cvs -d :pserver:anonymous@mayflowersystem.com:/cvsroot/barcamp checkout skype

      1. Can’t checkout the SVN repository.

        $ cvs -d :pserver:anonymous@mayflowersystem.com:/cvsroot/barcamp login
        Logging in to :pserver:anonymous@mayflowersystem.com:2401:/cvsroot/barcamp
        CVS Password:
        cvs [login aborted]: cvs pserver: cannot open /cvsroot/barcamp/CVSROOT/config: Not a directory

  6. i got this error while compiling..

    checking if nawk is broken… no
    checking for skype support… yes, shared
    ./configure: line 3883: SEARCH_FOR: command not found
    checking johannfor skype files in default path… found in /usr
    checking for dbus_connection_open in -ldbus-1… no
    configure: error: wrong skype lib version or lib not found

  7. got it working… but im receiving this message

    Warning: Mitteilung: CHATMESSAGE #yoshix86/$josephine.napari;10cc836e02bc4ff1 HELLLO in /home/aqlx86/www-dev/skype2/test.php on line 7
    ALERT – canary mismatch on efree() – heap overflow detected (attacker ‚REMOTE_ADDR not set‘, file ‚/home/aqlx86/www-dev/skype2/test.php‘, line 7)

  8. is there a solution? is it because of the Suhosin-Patch?

    PHP 5.2.4-2ubuntu5.1 with Suhosin-Patch 0.9.6.2 (cli) (built: May 9 2008 16:34:16)
    Copyright (c) 1997-2007 The PHP Group
    Zend Engine v2.2.0, Copyright (c) 1998-2007 Zend Technologies

  9. Leider benötigt man immer noch ein X-Windows um Skype überhaupt fahren zu können.
    Schade, so fallen Serverapplikationen aus, und die wären ja eigentlich am interessantesten.
    Skype auf einem Linux unter der Konsole laufen zum laufen zu kriegen wäre auch mal spannend.

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert


Für das Handling unseres Newsletters nutzen wir den Dienst HubSpot. Mehr Informationen, insbesondere auch zu Deinem Widerrufsrecht, kannst Du jederzeit unserer Datenschutzerklärung entnehmen.