Robert Basic's blog

Archives for January, 2012

Xdebug is full of awesome

by Robert Basic on January 30, 2012.
Heads-up! You're reading an old post and the information in it is quite probably outdated.

I’m currently trying to fix a Mockery bug and, while deep in the code, I came across a piece of code which gets eval’d. Mainly to understand better what’s going on, I wanted to step debug it. I first set a breakpoint before the eval call and then tried to step into the eval’d code, but that didn’t work out, Netbeans just moves along to the next line.

What did work, is setting a xdebug_break() call inside of the code that will be eval’d - and BAM! it works! Netbeans picks up the signal and everything works just as with regular code - you can view the values of the variables, step in, step out and step over code.

Xdebug is full of awesome.

Happy hackin’!

Tags: debugging, xdebug.
Categories: Development, Programming.

Need help on your PHP projects? Let's talk!

Creating a chat bot with PHP and Dbus

by Robert Basic on January 08, 2012.
Heads-up! You're reading an old post and the information in it is quite probably outdated.

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):

<?php
$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”:

<?php
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:

<?php
$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.

Need help on your PHP projects? Let's talk!

A year in review: 2011

by Robert Basic on January 01, 2012.
Heads-up! You're reading an old post and the information in it is quite probably outdated.

2011 has been and gone, all in all an excellent year for me; much better than the few clusterfuck years before. Going into 2012 with tons of positive energy and going to make it even better!

Sadly, not everything was perfect. In October my grandmother and one of my uncles passed away. Around the same time my Dad’s shop was robbed, twice. That was a tough period for my parents and the entire family, still not over it completely, but not giving up either! We love and support each other and will get out stronger than ever.

Now that the bad part is out, let’s jump to the awesome part ;)

Life changing decisions

In 2011 I actually had three life changing decisions.

First one in March, when I decided to finally quit smoking. And I did it! After 11 years I finally threw that shit away. Gave me a nice self-confidence boost, gained much needed weight - around 10kgs (now just to prevent that turning into fat and into muscles instead) - and I feel much, much better overall.

For the second one I owe a lot to my parents and to Srdjan Vranac, for their continuos support and making it possible to happen. In May I handed over my resignation at my previous job and in June I started my own company, Hex, and joined up with Vranac. Just this one thing changed my life to better in so many ways that I can’t thank Vranac enough for all he did, and is still doing! Thank you :)

The third decision happened in August when I cut my hair. And nope, I didn’t regret it :)

Friends and conferences

This year I spoke at two conferences, DORS/CLUC in May in Zagreb, and Webkonf in October in Budapest. My talks didn’t turn out that good (heck, the second one was pure horror), but I did make new friends which trumps bad talks any day!

In Zagreb I had the honor to meet fellow hacker Nikola Plejic, who kindly offered me a place to stay during the conference and showed me around the city. Now waiting for him to come to Novi Sad so I can return the favor. :)

In Budapest I met fellow hackers Ferenc Kovacs and Dubravszky Jozsef. Great people with whom I’d like to grab a beer or two this year :)

Open source and hacking

Contributed to web2project and Zend Framework throughout the year, but somewhere in September got really close to a burn out so I decided to back up a little and have a break. I’m trying to ease my way back in to contributing and general hacking now. Need to be careful though, don’t want to get this close to a burn out again, next time might not see the line.

Started to learn C just for fun. Don’t know what to do with it yet, can’t hurt learning/knowing it.

All in all, awesome year which set the bar high for 2012, but with all this positive energy it will be a piece of cake to fly over it!

Happy hackin’ and a happy new year!

Tags: about, hex, life, random.
Categories: Blablabla, Development.

Need help on your PHP projects? Let's talk!