Personally i always have my irc client running (i’m using weechat + tmux) and chat with lots of interesting people who inspire me to try new technologies and learn something different every day. One person, who’s nick i won’t name, was always telling me about how awesome Perl as a programming language is and how great it’s potential is thanks to CPAN that has almost 124k modules for any life situation. I always thought he was exaggerating and literally acting like a Perl fanboy. Perl was the first programming language i’ve learned back in the late 90’s and remembering how frustuating my experience with it was and how cryptic it really was for me do something with it when i was unexperienced and lacked lots of qualities that make up a any decent software engineer i was skeptic about using it again. Well, time passed, time always passes, and i haven’t written anything more than quick 50 line server scripts in Perl for almost 13 years. I’ve almost forgotten everything about Perl. Since lately i was having this crazy idea about writing irc bot that could store and execute shell scripts on server so i could automate my servers through irc, i thought why not write it in Perl. I’ve remembered that person who was always bragging about Perl’s greatness wrote an irc bot in Perl using POE::Component::IRC so i’ve decided to try and use the same framework for my bot. It’s based on really popular POE event loop framework which is very easy to learn and use. Matt Cashner wrote a really good introduction article called Application Design with POE
The whole code for my bot is just 500 lines and is available from this repository shellbot. I’m going to walk through a key concepts that are essential for writing an irc bot in POCO::IRC using my bot’s source code as a reference.
Before we’ll start our Perl IRC bot we need some way to store configuration for it. Since CPAN has lots of modules that deal with configuration the choice wasn’t an easy one but i’ve decided to use YAML which is module for loading YAML data into Perl that can work the other way too. YAML is a simple markup language that is perfect for storing configuration and it’s really quick to learn. For loading YAML configuration i’ve used LoadFile function and to store Perl data back into file i’ve used DumpFile function. Just two simple functions that do all the complex work work for me. Since i wanted to store commands in the same file i just used Perl’s list construct to specify that i’m loading two seperate YAML documents.
1 2 3 4 5
Next step is to create POE Session and start event loop. Note that since my module is named Shellbot i need to specify it otherwise event loop won’t be able to call functions. Each of this functions are called by POE when specified events occur and all the bot logic is handled by those functions.
1 2 3 4 5 6 7 8
First event that is executed after the POE event loop starts is _start event so we need to initialize bot state in that event. Also since POCO::IRC comes with some essential plugins and is modular by itself we can take advantage of this. Instead of manually making bot reconnect when it looses connection with server i’ve used Connector plugin. If we want our bot to automaticly join some channel we can use AutoJoin plugin. And to easily handle when someone addresses bot i’ve used BotAddressed plugin. Since i wanted my bot to handle commands i could have used BotCommand plugin however i wanted my bot to have two modes of commands so i’ve decided to write bot command handling functions manually.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
Since bot can be addressed both using a private message and refering him on a channel i’ve decided to handle both irc_msg and irc_bot_addressed events uniformly in msg_received function
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
Before bot accepts a command we need some way to check if person who is issuing a command is authorized to do so. I’m doing a simple check of full irc name that can be specified in configuration list option authorizations
1 2 3 4
All that is left is to match commands in msg_recieved using Perl’s regex. This part is just a long series of bot command logic and if elsif else statements so i won’t reference them here. Also the key function is run_job which executes prestored shell script. It creates a temporary file that is executed using a shell
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
1 2 3 4 5 6 7 8
That’s all there is to it. To install and trying it out just look through a readme
Offcourse making a temporary shell script and executing it is generally unsafe. Also there is security concern that bot can be somehow hacked and commanded by unathorized nick but i’m not sure how can this be done without changing vhost. That is why i called this bot a potentially unsafe. If anyone can find any security holes please do pull request or just email me patch.
To make this bot little more secure we need to make him connect to irc using SSL. For this i’ll walk through a general steps for configuring CertFP and generating self signed certificate for Freenode as an example. First step is to register your bot’s nick with NickServ. Choose a nickname for your bot and start it up. Ask your bot to register with NickServ by private messaging him on irc.
Shortly you’ll receive an email verification that will include verification code, privately message your bot again to verify your registration
Now your bot’s nick is registered with NickServ so edit your configuration file and uncomment nickserv line to specify your nickservpass that will be used each time your bot will connect to server for authorization of your nick
Now we’ll generate a self signed certificate for SSL, i’ve used this manual for reference.
You’ll be asked some questions and after that openssl will generate two files, your bot’s certificate and key. For CertFP to work we need a fingerprint of your key and certificate
After this you’ll get a fingerprint that you need to add to NickServ
Now all that is left is to specify in configuration that we are connecting using SSL certificate and key. Just uncomment sslcrt and sslkey options and specify full path to your bot’s certificate and key. Also don’t forget to change the port since we need to specify an SSL port instead of usual one, just specify 6697, 7000 or 7070 and restart your bot to enjoy fully secured connection to irc server. Also i’ve used Proc::Daemon for running this bot as daemon so you can uncomment daemon and log options to run it as daemon process.
So what did i learned from writing this small irc bot in Perl? Now the first most common mistake i was always making when coding in Perl was the difference between list and array, so I recommend this article by Mike Friedman Arrays vs. Lists in Perl that helped me a lot. Also dereferncing was the second most common mistake i had, until i’ve read this article Dereferencing in Perl. But the biggest thing i’ve learned was that Perl as a scripting language is really easy to use and powerfull despite some people claiming it’s dead, it’s still alive and it’s doing just fine! Just look how many modules are released on CPAN everyday! And it’s really really fun to code in, so everyone should try learning and using it!!!