CAZine: issue 6, December 2009

CAZine: issue 6, December 2009

Coders Corner – Intro To Perl Bots v2

By: Gen gothicfreak

This article is part II of the series. To view the first check out Novembers Issue!

Ok if you followed the first release of this you have the basic setup of the bot, it connects but doesn’t do anything else.

So first you need to get the info sent from the server into something easy to read/parse, i put it into an array so i can easily select by location in the string.

  1. $line = $_;
  2. chomp($line);
  3. @lines = split(/ /, $line);

Ok so basically i take the line and put it into a variable so i can easily remove the return character on the end

then put it into an array splitting by spaces so i can call it by $lines[0], $lines[1], etc

While your testing and coding your bot to check syntax and such you might wanna have it do a

  1. print "$line\n";

that way you can see what each incoming msg is and it helps and is usually easier then reading through the rfc for every little command.

One issue that you might run into on certain servers is before you connect you have to reply to a funky ping request usually something like :2158A, so your bot might not completely connect

at this time. That and while your connected you get a ping request from the server every so often to make sure your still connected (that’s why you will notice occasional users quitting with the reason

(Ping timeout)

To get it to look for this and properly respond to keep you connected use this code:

  1. if ($lines[0] eq ‘PING’) {
  2.      print $irc "PONG $lines[1]\n";
  3. }

That will respond to the server saying ‘yeah I’m here don’t disconnect me’

So now the bot will connect to the server and respond to pings. Not very exciting but a needed aspect.

So lets start with having it join a channel. There are a few aspects you can look for to know your fully connected before having it join the channels.

The end of the MOTD if your bots nick is registered with services wait until after it responds etc.

I generally wait for the bot to register with nickserv that way any vhost is set before having it join the chans.

To do this i usually use:

  1. elsif ($lines[1] eq ‘NOTICE’) {
  2.      if ($lines[0] =~ /:NickServ/i) {
  3.           if ($lines[6]) {
  4.                if ($lines[6] =~ /registered/i) {
  5.                     print $irc "PRIVMSG NickServ :identify $botpass\n";
  6.                     print $irc "MODE $botnick $botmodes\n";
  7.                     foreach $line (@default_chans) {
  8.                          print $irc "JOIN $line\n";
  9.                     }
  10.                }
  11.           }
  12.      }
  13. }

Basically that looks for any NOTICE then makes sure its from nickserv if it is it checks to make sure that the 7th ‘word’ in the line is registered (part of the: this nickname is registered and protected)

if it is then it will respond by logging into nickserv. If there are certain modes the server requires or you want to have (like +B) you can send that cmd then to set those modes.

Now if you setup an array with your channels (like i show in the example) you need to cycle through each and join. IE

  1. @default_chans = (#cyberarmy, #cauniversity, #class);

Otherwise if you want to just join 1 channel and use a single variable you can do

  1. $default_chan = ‘#cyberarmy’;

then replace the foreach line and the 2 lines after it with just:

  1. print $irc "JOIN $default_chan\n";

Quick simple and easy huh?

Next we will work on messages. First we need to be able to tell the difference between private messages and channel messages. Its actually quite simple:

  1. elsif ($lines[1] eq ‘PRIVMSG’) {
  2.      if ($lines[2] eq "$botnick") {
  3.           #privmsg cmds
  4.      }
  5.      else {
  6.           #channel cmds
  7.      }
  8. }

Ok basically $lines[2] is the 3rd line and it shows the target for the command or msg. So by checking that to see if its the bots nickname or not we know if its just for the bot or if its for the channel

that the bot is in.

So by using that we can make certain commands (or syntax for commands) vary by public and private.

IE You don’t want to make a login feature something that’s done in a public channel otherwise everyone knows your password.

Now if you have ever looked at raw msgs sent to an irc server it looks like this: PRIVMSG #cauniversity :whats up guys?

First you send the command (PRIVMSG), 2nd the target (#cauniversity), and 3rd the messages itself.

One thing to remember though you need the colons where they are if you send a private msg to the server as PRIVMSG #cauniversity whats up guys?

its going to only show “whats” to the channel

Using this aspect you can reply to certain messages that you receive from different users. And have it perform tasks or respond. There are numerous things that you can have your bot do.

Next Lets say you have your bot you have it going and ready to do the commands you wanna do. So lets turn it into a background process so you don’t have to leave a terminal window open after you run it. Its actually quite a simple process.

  1. $mypid = fork();
  2. open(PF, ">is.pid"); print PF $mypid; close(PF);

See quick simple easy you just fork() it. That will turn it into a background process. The 2nd line opens a .pid file (process id) that way you can view that file to kill the bot if needed (like if you haven’t added a quit cmd or if it gets stuck in an endless loop). You can also use this file to have a cron job check if it is still running and restart if needed.

Coming in the next issue I will show you some of the scripts I have added to bots for entertainment and such.

VN:F [1.7.9_1023]
Rating: 10.0/10 (7 votes cast)
CAZine: issue 6, December 200910.0107

Pages: 1 2 3 4 5 6 7 8 9 10 11 12 13 14

About the Author