Around OHM2013 I started using Xchat again to manage all the IRC networks I have to be on.
Pidgin simply could not do the job.

However I have to constantly authenticate my self against services. Unfortunately I have lost my ns-authenticate.pl plugin so I wrote a new one :)

#!/usr/bin/perl
use strict;
use warnings;

Xchat::register('NS Identify', '0.2', 'Identify against NickServ');
Xchat::hook_server( 'Notice', \&ns_identify );
Xchat::hook_print( 'Message Send', \&hide_nickserv ); 

my %pass = (
    'UniBG' => '',
    'FreeNode' => '',
    'OFTC' => ''
);

# Catch the notices from services and send passwords
sub ns_identify {
    return Xchat::EAT_NONE if !defined($_[0]);
    if ($_[0][0] =~ /^:NS!NickServ\@UniBG/ && $_[0][3] =~ /^:This/) {
        Xchat::command("msg NS identify $pass{'UniBG'}");
        return Xchat::EAT_NONE;
    }
    if ($_[0][0] =~ /^:NickServ!NickServ\@services./ && $_[0][3] =~ /^:This/) {
        Xchat::command("msg NickServ identify $pass{'FreeNode'}");
        return Xchat::EAT_NONE;
    }
    return Xchat::EAT_NONE;
}

# Catch the messages to NS and NickServ and do not show them in the windows and logs.
sub hide_nickserv {
    if (defined($_[0])) {
        if ($_[0][0] eq 'NS' || $_[0][0] =~ /NickServ/) {
            return Xchat::EAT_ALL;
        }
    }
    return Xchat::EAT_NONE;
}
Posted by HackMan
Dated: 9th August 2013
Filled Under: Uncategorized
Comments: Post the 1st one!

For a project that I’m working on I had to create MAC generator. Initially I decided that I will do it in BASH.

function gen_mac() {
        mac_vars=(0 1 2 3 4 5 6 7 8 9 a b c d e f)
        mac_base='52:53:54:'
        ret=''
        for i in {1..6}; do
                n=$RANDOM
                let 'n %= 16'
                ret="${ret}${mac_vars[$n]}"
                if [ $i -eq 2 ] || [ $i -eq 4 ]; then
                        ret="${ret}:"
                fi
        done
        echo "${mac_base}${ret}"
}

But unfortunately I had to rewrite it in plpgsql in order to prevent a stupid race condition that can appear.

CREATE OR REPLACE FUNCTION generate_mac() RETURNS text
    LANGUAGE plpgsql
    AS $$DECLARE
	mac TEXT;
	a CHAR;
	count INTEGER;
BEGIN
	mac='52:53:54:';
	FOR count IN 1..6 LOOP
		SELECT substring('0123456789abcdef' FROM (random()*16)::int + 1 FOR 1) INTO a;
		-- This fixes un issue, wehre the above SELECT returns NULL or empty string
		-- If for some reason we concatenate with a NULL string, the result will be NULL string
		WHILE  a IS NULL OR a = '' LOOP
			SELECT substring('0123456789abcdef' FROM (random()*16)::int + 1 FOR 1) INTO a;
		END LOOP;
		mac = mac || a;
		IF count = 2 OR count = 4 THEN
			mac = mac || ':';
		END IF;
	END LOOP;
	RETURN mac;
END;$$;
Posted by HackMan
Dated: 25th March 2013
Filled Under: Uncategorized
Comments: Post the 1st one!

Today I decided to play a bit with my Raspberry Pi and one of my Relay shields that I have for the Arduino.
I was surprised to see how easy it was to control and read the GPIOs on the Raspberry. So I decided to make it even easier by writing a simple bash script which I use to turn on and off relays from the shield.

This is the script:

#!/bin/bash

GPIO=11
if [ $# == 1 ]; then
        if ! [[ "$1" =~ ^[0-9]+$ ]]; then
                echo "Error: wrong pin format"
                echo "Usage: $0 [GPIO]"
                echo "GPIOs: [0-24]"
                exit
        fi
        GPIO=$1
fi

# export GPIO 11 to userspace if it isn't exported already
if [ ! -d /sys/class/gpio/gpio${GPIO} ]; then
        echo "Exporting GPIO $GPIO"
        echo $GPIO > /sys/class/gpio/export
        sleep 1
fi

if [ "$(</sys/class/gpio/gpio${GPIO}/direction)" != 'out' ]; then
        echo "Setting GPIO $GPIO to OUT"
        echo out > /sys/class/gpio/gpio${GPIO}/direction
fi

if [ "$(</sys/class/gpio/gpio${GPIO}/value)" == 1 ]; then
        echo "Set GPIO $GPIO OFF"
        echo 0 > /sys/class/gpio/gpio${GPIO}/value
else
        echo "Set GPIO $GPIO ON"
        echo 1 > /sys/class/gpio/gpio${GPIO}/value
fi

I decided to use GPIO 11 as default, because it is the closest to pin 25(ground). So it was easier to have the wires close to each other.

Posted by HackMan
Dated: 1st December 2012
Filled Under: Uncategorized
Comments: Post the 1st one!

For the past 3 times when I was teaching Linux System and Network Administration at the Sofia University I’m using a tool to generate the tests.
Since today I had to again fix some issues in the generator I decided that I will publish it on GitHub.

So here is the repository.

Posted by HackMan
Dated: 29th November 2012
Filled Under: Uncategorized
Comments: Post the 1st one!

For a few years I was thinking about controlling the (un)locking and start/stop of my car via GSM. Two weeks ago, me and a friend of mine finally did it. Using a simple Arduino Uno plus GPRS Shield and Relay Shield we were able to call the Arduino and trigger a relay to open or close.
The proof of concept code of this simple setup can be found on github.

If you use the GPRS Shield of seeedstudio you have to know that the Relay shield can not be stacked together with the GPRS Shield because both use pin 7 and 8. What we have done is to connect pins 7,6,5,4 from the Relay shield to pins 2,3,4,5 on the Arduino/GPRS Shield.

WIth Sparkfun’s GPRS Shield you don’t have this problem because it uses pins 1 and 2 for communication.

Posted by HackMan
Dated: 24th November 2012
Filled Under: Uncategorized
Comments: Post the 1st one!

Here is one pretty easy and useful one liner in Perl:

perl -MDevice::SerialPort -e 'Device::SerialPort->new("/dev/ttyACM0")->pulse_dtr_on(300);'

And here is a fully functional perl script that I wrote for the same purpose:

#!/usr/bin/perl
use Device::SerialPort;

sub find_dev {
        my $test = shift;
        my $dev = '';
        for (my $i=0; $i< =4; $i++) {
                if ( -e $test . $i ) {
                        $dev = $test . $i;
                        last
                }
        }
        return $dev;
}

my $dev = '';
$dev = find_dev('/dev/ttyACM');

if ($dev eq '') {
        $dev = find_dev('/dev/ttyUSB');
}

if ($dev eq '') {
        print "Unable to find a suitable device\n";
        exit;
}
print "Device located at $dev\nSending reset!\n";

Device::SerialPort->new($dev)->pulse_dtr_on(300);
Posted by HackMan
Dated: 24th November 2012
Filled Under: Uncategorized
Comments: Post the 1st one!

In the past few weeks I managed to add changes to a few of my projects in the wrong branches, which led to the merges of unwanted code.
So, I decided that I have to do something about this, something that will prevent me from working on the wrong branch and blindly commit to it :)

I decided that it would be very neat if I have the branch name in my cmd prompt. So I wrote this little script:

#!/bin/bash 

function change_ps {
        PS1='($(git branch 2>/dev/null|awk "/^*/{print \$2}")):\w\$ '
        export PSCHANGED=1
}

function revert_ps {
        PS1="\u@\h:\w\$ "
        export PSCHANGED=0
}

function branch {
        cd $1
        cwd=$(pwd)
        if [[ "$cwd" =~ \/home\/hackman\/Projects ]] && [ -d $cwd/.git ]; then
                if [ "$PSCHANGED" == 1 ]; then
                        if [ "$cwd" != "$OLDPWD" ]; then
                                change_ps
                        fi
                else
                        change_ps
                fi
        else
                revert_ps
        fi
}


After I wrote it, I found another project that does a lot more (git-prompt). But I think that git-prompt is a big overkill for me. The main difference is that my script does a lot less computations for the task of changing the branch.

I have published this code at github

Posted by HackMan
Dated: 22nd April 2012
Filled Under: Technology
Comments: Post the 1st one!

Last week I was in Rust, Germany at the WorldHostingDays conference & expo. The biggest problem we had, was that there was almost no Internet :)

The organizers made a very poor design of their network and so, most of the time there was no connection at all.

We were exhibitor there and as such the expo was a great success for us. We met a lot of clients and I was amazed to talk with so many great technical figures. I wasn’t expecting that. It was nice to see what these people will think about our products. And the biggest thrill for me was, that everyone that I talked with really liked what we offered. Even thou a lot of people call our software ‘just a bunch of scripts’, after a few minutes talk with me they were convinced that the product is much more then that.

The next thing was that I collected a few really good ideas that our future clients wanted. And the strangest and maybe most disturbing one was that there was at least 4 people that requested monitoring for Windows.
No one actually requested monitoring of network devices which was something that I wanted to add to our software.

On the VIP dinner I set next to Roger Nolan(the founder of Symbian), that was a talk I really liked. And on the next day I had a dinner with Mario and Daivid from cPanel. Even thou I don’t really like their software, they were really cool to talk to. I learned a lot for their way of working and it was a really fun dinner. Because of guys like these it is sometimes very nice to visit such business conferences.

Posted by HackMan
Dated: 29th March 2011
Filled Under: Uncategorized
Comments: Post the 1st one!

Today’s event was a real success. We had more then 40 people in the morning and something around 30 after the lunch break.

The talks were really interesting and inspiring. Alexey’s talk about ThinPacker was really inspiring and it captured my attention, so we will work together on that.

We made a really good discussion about the Frameworks and I hope that everyone that were listening and talking learned a lot of it.

I have to say that I’m surprised from my self since I finally made a presentation that I like :) I hope that all visitors also found it useful.

And finally I have to add that my company(1H) sponsored the travel of Alexis which proved to be quite a good idea. So next year we will consider sponsoring again.

Posted by HackMan
Dated: 27th February 2011
Filled Under: Uncategorized
Comments: Post the 1st one!

Next Saturday I’m organizing the annual Bulgarian Perl Workshop. One of the biggest challenges for this event is making people come and talk at the conference. And I’m not talking about international guests. I’m talking about Bulgarian speakers.

We have a lot of people working with Perl every day and it is strange to me that only one or two of them is actually coming to speak to the event.

But enough about this… I’m preparing a talk about embedding Perl in C and the other way around. The ugly true of scattered documentation and half working modules.

Posted by HackMan
Dated: 21st February 2011
Filled Under: Uncategorized
Comments: Post the 1st one!