Yauib : Yet another useless IRC Bot !

After 2 years of … non blogging … I’m back !
This time I stopped C# (Oh yeah !) and I’m writing a lot of Python ! (Oh YEAH !)

So to say HELLO I’ll present something useless : My Python IRC Bot. But this article should be usefull, so I’ll speak about the Unix Philosophy :
As Doug McIlroy said :

This is the Unix philosophy: Write programs that do one thing and do it well. Write programs to work together. Write programs to handle text streams, because that is a universal interface.

So my Python Bot is wrote like this, only ~200 lines of Python it has only two interfaces, and they are simple.

The first step is to start the bot :

./ircbot.py connect 'server' '#chan' 'nickname'

Ok the bot is connected to a chan, now you have to write some hooks, in yauib every action raise a hook in the ‘hooks’ directory, you can write a hook in any language you want. As a good start point I wrote a default hook for received messages in hook/pubmsg, simplified like this :

#!/bin/sh -f
s_login="$(echo "$1" | sed 's#/#__SLASH__#g')"
s_host="$2"
t_login="$3"
t_host="$4"
shift 4
arguments="$*"
command=$(echo "${1%% *}" | sed 's#/#__SLASH__#g')
if [ -x "commands/$command" ]
    then
    args=$(echo "$1" | sed 's/^ *[^ ]* *//g' )
    commands/$command $args
fi

You should read : “When a message is received, the command in commands/[1st word of the message] is executed.”

So now you can start to write simple commands in the commands folder, like the command say :

#!/bin/sh
echo "$*"

This command is 17 chars long, and now, without restarting your bot, try to make it say something on the channel :
you> say hello
bot> hello

An now you can start to write dauting ones …. like … display a readeable calendar of the current month like :

     March 2011
Su Mo Tu We Th Fr Sa
       1  2  3  4  5
 6  7  8  9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31      

Ready ?

#!/bin/sh
cal

As the binary cal is installed on you machine, (windows user, you can leave, now), that’s all, your’re done … sorry, it just work :p

Et voilĂ  !

You have to keep the UNIX philosophy in mind :
– Everything is a file
– Write programs that do one thing and do it well

And you’ll be happy developers !

PS: You can download / contribute to Yauib on https://github.com/JulienPalard/yauib

Posted in Python, unix | 4 Comments

Post data lost on 301 Moved Permanently

What the hell with 301 Moved Permanently HTTP header !?

I’ll take an exemple to explain my ugly problem, take, a [very ugly cause it's PHP] index.php, it could be retrieved by using :

http://example.com/directory/index.php

-> Got the page, 200 OK
or http://example.com/directory/
-> Got the page, 200 OK
or http://example.com/directory
-> Got a 301 Moved Permanently Location: http://example.com/directory/

Just like expected … but…

As you can see, on the captured HTTP headers below, when you POST data on a 301 target, you’ll be redirected, but unfortunately you’ll lost your POST data, even worse, your request can be reforged as a GET request !

Continue reading

Posted in Code | 4 Comments

Integrating google bookmarks in google chrome

As every developer, you have 42 computers, 8 browsers, and spent a lot of time asking why google chrome does not integrate google bookmarks ?
Continue reading

Posted in Code | Leave a comment

C# Using alias directives

Just found in section 9.4.1 of the C# language specification :
The using keyword can be used to alias a namespace or a type name :

using-alias-directive:
using identifier = namespace-or-type-name ;

Continue reading

Posted in Code | 2 Comments

Covariance and Contravariance in C#3

A short introduction to Covariance and Contravariance in C# 3 preparing you to an article about that in C# 4.
So what is covariance ?
Covariance is basically using a method which returns something derived from the expected type.

An exemple ? It’s safe to have a method returning a cat when you expect it to return an animal.
Continue reading

Posted in C#, Code | 2 Comments