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
Less Unixy, more cross platform ‘cal’
>>> import datetime
>>> import calendar
>>> now = datetime.datetime.now()
>>> calendar.TextCalendar().prmonth(now.year, now.month)
April 2011
Mo Tu We Th Fr Sa Su
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
>>>
Mark : Done and commited !
https://github.com/JulienPalard/yauib/commit/03a315ff2699d4f56835dcdfe614b92cf766c921
I am trying to add a my own command but I am having issues.
Here is the code for the command
========================
#!/bin/sh -f
usage()
{
echo “I just…”
echo “PONIES!!!!!!!!!!”
}
#[ "$1" = -h -o -z "$1" ] && usage || printf “%s\n” “$*”
echo “Enough ponies have not been herded yet…..”
========================
And here is the message I get in terminal when I try to “pony” or “help pony”
========================
expr: syntax error
expr: syntax error
Running command ‘help’ with ‘pony’
commands-enabled/help: line 11: commands-enabled/pony: Permission denied
========================
I’m having no luck… =(
Seems you just forgot to give execution right on ‘pony’, try :
$ chmod a+x commands-available/pony