• Subscribe to the RSS feed!
  • Subscribe by Email
  • home
  • blog
  • dev
  • Recent Posts

    • Xdebug is full of awesome
    • Creating a chat bot with PHP and Dbus
    • A year in review: 2011
    • Notes on shell scripting
    • Listening to Dbus signals with PHP
    • Configuring 2 monitors with xrandr
    • A quick note on Dojo’s data grids and dojox.data.HtmlStore
    • Communicating with Pidgin from PHP via D-Bus
    • Upgrading to Fedora 16
    • Contributing to Zend Framework 2
  • Recent Comments

    • Creating a chat bot with PHP and Dbus ~ Robert Basic on Communicating with Pidgin from PHP via D-Bus
    • A year in review: 2011 ~ Robert Basic on Announcing Hex
    • Anon on A quick note on Dojo’s data grids and dojox.data.HtmlStore
    • James on Communicating with Pidgin from PHP via D-Bus
    • A Zend Framework 2 EventManager use case ~ Robert Basic « Bookmarks on A Zend Framework 2 EventManager use case
    • Zend_Auth | Kerek egy ég alatt on Login example with Zend_Auth
    • Jowee on A Zend Framework 2 EventManager use case
    • Jurian Sluiman on A Zend Framework 2 EventManager use case
    • Robert on A Zend Framework 2 EventManager use case
    • Jurian Sluiman on A Zend Framework 2 EventManager use case
  • Tags

    about apache ape blog book comic community conference contributing dbus dojo events example facebook framework hack introduction lamp linux me mysql netbeans open source php pidgin plugin pyqt python random registration review script security setup shell signals site svn talk ubuntu web wordpress xdebug zend zend framework
  • Categories

    • Blablabla
    • Development
    • Free time
    • Places on the web
    • Programming
    • Software
  • Archives

    • January 2012
    • December 2011
    • November 2011
    • October 2011
    • September 2011
    • August 2011
    • July 2011
    • May 2011
    • April 2011
    • March 2011
    • January 2011
    • December 2010
    • November 2010
    • October 2010
    • July 2010
    • June 2010
    • April 2010
    • February 2010
    • January 2010
    • December 2009
    • November 2009
    • October 2009
    • August 2009
    • May 2009
    • March 2009
    • February 2009
    • January 2009
    • December 2008
    • November 2008
    • October 2008
    • September 2008
  • Find me on

    • DZone
    • Google Code
    • Google Reader
    • Last.fm
    • StumbleUpon
    • Twitter
    • Vimeo
  • Friends and Blogs

    • Andrew Taylor
    • Andy Sowards
    • Bojan Pejić
    • Eran Galperin
    • Graham Smith
    • Jani Hartikainen
    • Jasper Tandy
    • Matthew Turland
    • Matthew Weier O’Phinney
    • Miff
    • Miloš Ćuković
    • Nebojša Radović
    • Nemanja Avramović
    • Nemanja Tobić
    • Nikola Krajačić
    • Nikola Plejić
    • Pádraic Brady
    • Rob Allen
    • Swizec Teller
    • Vladimir Stanković
    • WeAreJustCreative
    • Željko Stevanović
  • I use

    • 960 Grid System
    • jQuery
    • Notepad++
    • Subversion
    • Trac
    • Vim
    • Zend Framework

Posts Tagged ‘php’

Creating a chat bot with PHP and Dbus

by Robert Basic on January 8th, 2012

Now that we know how to use DBus to communicate with Pidgin from PHP and how to listen to DBus signals, it’s time to put it all together by creating a simple chat bot! Nothing fancy, just a simple script that runs somewhere on some server and, by using a Pidgin account, can respond to some basic queries we send it.

What did we get?

As we want our script to receive messages from an other account, first we need to listen to the ReceivedImMsg event on the im.pidgin.purple.PurpleInterface interface. The data we get with that event is the ID of receiver’s account, the sender of the message, the actual message and the conversation’s ID (and some flags which we’re not interested in):

$interface = "im.pidgin.purple.PurpleInterface";
$method = "ReceivedImMsg";
if ($signal->matches($interface, $method)) {
    $data = $signal->getData()->getData();
    $receiver = $data[0];
    $sender = $data[1];
    $message = $data[2];
    $conversation = $data[3];
}

Of course, this is only for this one event, for data associated with other events see Pidgin’s manual.

Who’s there?

The event we are listening for will fire for any and all accounts, no matter who is the sender or the receiver of the message. We need to make sure that the receiving and the sending accounts are the correct ones, that the receiver is connected and that the receiver and the sender are contacts, “buddies”:

if ($receiver == 2034 && $proxy->PurpleAccountIsConnected($receiver)
    && $proxy->PurpleFindBuddy($receiver, $sender) == 3681) {

The numbers 2034 and 3681 are the account IDs for my accounts I used in this example; you’ll need to figure out yours.

Sending a response

Now that we know who’s talking to whom, we can act upon the received message, do something with it, create a response message and send it back! The data we got with the event, has the ID of the conversation between the two accounts. We create a new instant message for that conversation and send it on it’s merry way with our clever response message:

$im = $proxy->PurpleConvIm($conversation);
$proxy->PurpleConvImSend($im, $responseMessage);

As for what action the script can take upon a new message, is really up to the developer: it can do simple stuff like sending back the current uptime of the server or the current IP, running other tools like usher and sending that result back, or whatever is necessary.

Daemonizing

As this little bot is supposed to run on some server, the easiest way to run it as a “daemon” is to put the script in a background job via nohup:

$ nohup php chat.php &

If needed, creating daemons in PHP can be done too.

And that’s about all what’s needed to create a chat bot. See a complete example here on Github.

As for, is PHP the right tool for creating this kind of thing, I don’t know, maybe, maybe not. What I do know, is that I had fun writing all this and I learned a lot along the way :)

Happy hackin’!

Tags: bot, dbus, events, php, pidgin, signals.
Categories: Development, Programming.
Comments: None.

Listening to Dbus signals with PHP

by Robert Basic on December 26th, 2011

In my previous post I described (tried, at least) how to communicate with Pidgin from PHP, by using the Dbus PHP extension.

The good part is that not can we only call different methods against Pidgin’s libpurple API, we can also listen to different signals on different events, that are sent via Dbus. Some of the events that are signalled are when a chat message is recieved, a friend comes online, a file is sent, or any other from a list of some 110 different events.

The PHP Dbus extension allows us to watch for one exact signal on an interface, or for all signals on an interface. Of course, we can add watches on multiple interfaces at once.

Watching for signals

Once we know the interface and/or the specific signal we’re interested in, we can add a watch on it. This is done by calling the addWatch method on the Dbus object, were the first parameter is the interface, and the second, optional parameter is the exact signal we want to listen to.

<?php

$dbus = new Dbus(Dbus::BUS_SESSION);

// watching for a specific signal
$dbus->addWatch("im.pidgin.purple.PurpleInterface", "ReceivedImMsg");
// or watching on an entire interface
// $dbus->addWatch("im.pidgin.purple.PurpleInterface");
// also can listen to different interfaces at the same time
$dbus->addWatch("org.freedesktop.Hal.Device");

Next, we need a way to actually get these signals when the events occur. For this we are using the waitLoop method of the Dbus class. That method accepts a number as a parameter, which is the number of miliseconds it should wait between requests. If an event happened on the interface we’re watching, it will return the signal, which is a DbusSignal; otherwise we’ll get a null:


do {
    $signal = $dbus->waitLoop(1000);

    if ($signal instanceof DbusSignal) {
        // even if we watch only for one signal on one interface
        // we still can get rubbish, so making sure this is what we need
        if($signal->matches("im.pidgin.purple.PurpleInterface","ReceivedImMsg")){
            // data is in this weird DbusSet object thingy
            $data = $signal->getData()->getData();
            echo "Got stuff!\n";
        }
    }
} while (true);

Once we got the signal, to make sure that the signal is really the one we’re interested in, we call the matches method on it. The first parameter is the interface and the second is the signal.

Each event has (can have? not sure yet) additional data associated with it. To get to it, for some odd reason, we need to call getData()->getData() on the signal; note that this is in case of listening on libpurple’s interfaces, not sure about others. Experiment. Also, what kind of data is returned, again, depends on the interface and/or the event – some return arrays, some strings.

Have a look at the Github repo for some more examples (the dbus-signals* files).

In the third, and probably last post in this dbus mini-series, I’ll try to build a bot with which I can communicate and issue out commands to.

Happy hackin’!

Tags: dbus, events, listen, php, pidgin, signals, xmpp.
Categories: Development, Programming.
Comments: None.

Communicating with Pidgin from PHP via D-Bus

by Robert Basic on December 18th, 2011

Earlier this week I got an idea of trying to communicate with Pidgin, a chat client, via the terminal. Sounded like a fun thing to hack on, plus, could be made useful (in my head, at least), for things like logging from a web application directly to IM, or, heck, even creating something like Github’s Hubot, commanding a server or an application just via chat. Surely I wasn’t the first one to come up with this idea and after a bit of a googling found out that Pidgin’s libpurple has a nice API for that, exposed via D-Bus.

I first planned to write some scripts for this in Python or C, but when I finally sat down over the weekend to hack on this, realized there is a PHP D-Bus extension, thanks to Derick Rethans! As I rarely have the opportunity/need to play with more “obscure” PHP extensions, decided to give this one a spin… (Note: apart from that D-Bus is used for processes communicating with each other, I have zero knowledge about it, so I might be wrong with some things down below.)

Installing D-Bus for PHP

As the extension requires the dbus-devel package, first make sure it is installed:

$ yum install dbus-devel

The installation of the extension itself is pretty easy:

$ pecl install dbus-beta

Add the extension=dbus.so line to your php.ini, restart Apache if needed and have a look at the phpinfo();, there should be an entry for D-Bus listed.

Note that there is no documentation for this extension at the moment, but, luckily, the sources include an examples directory full of goodies! After fiddling around with those for an hour or so, got the basics of the extension figured out.

One thing I haven’t figured out completely, is how to run scripts which use the Dbus extension via the browser, as in those cases the scripts are dying with a terrible X11 error message. So, run Dbus scripts from the console and all should be fine.

The a-ha! moment

What I learned by looking at the examples, is that the Dbus class is used for creating a proxy, which can be used to call methods on the service/application we’re interested in. But, what methods are available, what arguments do those methods accept (if any), and what will they return as a result?

This can easily be found out by introspection. Create a proxy to an object which implements the Introspectable interface and call the Introspect method on that proxy:

<?php

$dbus = new Dbus;

$proxy = $dbus->createProxy("im.pidgin.purple.PurpleService",
                            "/im/pidgin/purple/PurpleObject",
                            "org.freedesktop.DBus.Introspectable");

$data = $proxy->Introspect();

file_put_contents('introspect.xml', $data);

As the result of the introspection is returned in an XML and can be quite big, putting it in a file for easier viewing.

By looking at the XML file, it’s pretty easy to figure out what’s going on; method names, method arguments, their names and types, and the returned result:


  
  

With all this information at our disposal, it’s easy to write a script which does something useful, like, listing all the connected accounts and the protocols they are using:

<?php

$dbus = new Dbus;

$proxy = $dbus->createProxy("im.pidgin.purple.PurpleService",
                            "/im/pidgin/purple/PurpleObject",
                            "im.pidgin.purple.PurpleInterface");

$accounts = $proxy->PurpleAccountsGetAllActive();

foreach ($accounts->getData() as $account) {
    if ($proxy->PurpleAccountIsConnected($account)) {
        $username = $proxy->PurpleAccountGetUsername($account);
        $protocolId = $proxy->PurpleAccountGetProtocolId($account);
        $protocolName = $proxy->PurpleAccountGetProtocolName($account);
        echo $username . " is connected on the " . $protocolName
                       . " (" . $protocolId . ") protocol.\n";
    }
}

A sample output would be something like: “robertbasic@irc.freenode.net is connected on the IRC (prpl-irc) protocol.”

Next steps

Of course, this is far from a chat bot which can execute commands on a remote server, but at least we have some foundation to build on. In the coming days I’ll try to figure out how to create a loop which can be used to listen to different purple events – when a contact comes online, a chat is sent, or received, etc.

Also, it is quite fun trying to figure out a PHP extension just by looking at examples and the C source itself. One can learn a lot this way.

Happy hackin’!

P.S.: Code samples are up on Github!

Update 2011-12-26: the 2nd post, listening to dbus signals with php, is published!

Tags: communication, dbus, pecl, php, pidgin, xmpp.
Categories: Development, Programming.
Comments: 2.

Oktober(blog)fest!

by Robert Basic on October 3rd, 2011

Just as we had a blog movement back in March, thanks to Stuart Herbert we now have a new one for October – Oktober(blog)fest! :)

The rules are easy:

  • Write a post called Oktober(blog)Fest.
  • List some of the reasons you like blogs.
  • Pledge to blog more the rest of the month.
  • Share your thoughts on Twitter with the #oktoberblogfest hashtag.
  • If we all blog a little more than we normally would this month, maybe we can be reminded of all of the reasons blogs are great.

Well, here I am, pledging that I’ll be blogging a bit more in the coming days/weeks/months. I really do like blogging, don’t know why am I so lazy about it.

Don’t expect anything revolutionary from me tho, heh. An update or two from this weeks Webkonf and maybe some news from the Zend Framework 2 world :)

Happy hackin’!

Tags: community, oktoberblogfest, php.
Categories: Blablabla, Development.
Comments: None.

Book review – Guide to Web Scraping with PHP

by Robert Basic on May 25th, 2011

It took me a while to grab myself a copy of Matthew Turland’s “Guide to Web Scraping with PHP“, but a few weeks ago a copy finally arrived and I had the pleasure of reading it. I planned to buy it right as the print copy was announced, but then realised that php|arch accepts only PayPal as the payment method, which doesn’t work from Serbia, so I had to postpone the shopping for some better times. Fast forward 5-6 months and I found a copy on the Book Depository, which has no shipping costs! Yey!

Guide to Web Scraping with PHP

Guide to Web Scraping with PHP

My overall impression of the book is that it was worth the time and I’m really glad that I bought it. Matthew did a great job explaining all the tools we have at our disposal for writing web scrapers and how to use them. The chapter on HTTP at the beginning and a chapter with some tips and tricks at the end of the book, fit in great with the rest of the chapters, which are full of code examples. For the first reading, I’d recommend reading the book cover to cover, to get an overall view of all the tools presented, but later the chapters can be read independently.

As I said, the first chapter (actually, the second one, the first one is the introductory chapter :p), deals with the HTTP, especially with the parts of it which are needed for understanding, using and creating web scrapers.

The book then continues on different client libraries we can use to send HTTP requests and receive responses. Libraries like cURL or Zend_Http_Client are explained, but it is also explained how one can create his own using streams (the author does note that you’d be better of with an existing one!). For each of the tools it is described, how to handle things like authentication, redirects and timeouts, amongst others…

The second part of the book deals with preparing the documents for, and with the actual parsing of the data from these documents. Again, different tools are presented and explained, which one to use when and why. If none of the parsing tools can help, a most essential overview of the PCRE extension is given, too.

The book is finished with a nice “Tips and Tricks” chapter, which discusses real-time vs batch job scrapers, how to work with forms, the importance of unit testing… IMHO, without this last chapter, the book would not be finished.

I’m thinking hard right now, what bad things could I say about this book, but I can’t think of any. It is a guide, clear and straight-to-the-point, explaining what tools are there, which one to use and how for writing scrapers and that’s exactly what I wanted to know.

Yep, I’d recommend this book to anyone interested in web scraping with PHP :)

Tags: about, book, php, review, web scraping.
Categories: Development, Programming.
Comments: 2.
12345 » Last
Robert Basic © 2008 — 2012
Design & graphics by: Livia Radvanski — Lady L.
Coded by: Robert Basic
Home page last updated on November 30th, 2009.
Frameworks used: Zend Framework, jQuery, 960 Grid System
Blog is powered by Wordpress
Subscribe: Entries — RSS & Comments — RSS