User Tools

Site Tools


user:ccaccia:portfolio:project2

Project: Creating an IRC Bot

A project for UNIX/Linux Fundamentals by Christopher M. Caccia during the 2011 Fall Semester.

This project was begun on November 15th and has taken 2 months to complete.

Objectives

The purpose of this project is to create an IRC Bot that when asked a specific question will respond with output to the bot channel on a IRC server. By the end of this project I hope to have a computer program that will run in the background of my terminal and output specific commands when prompted. The bot will be able to respond to it's name, and also display some utilities like “cal” and “who” to output on the IRC channel.

Prerequisites

In order to successfully accomplish/perform this project, the listed resources/experiences need to be consulted/achieved:

  • Bash shell scripting
  • Variable Assigning
  • Using “echo”
  • Vi Text Editing
  • Redirecting text to a file called in (> in)
  • Utilizing the sleep command
  • The who command
  • The cal command

Background

IRC bots are computer programs that run in a IRC chat room, that when asked a question or statement, will independently respond to you. I feel that the bots really give the perception of artificial intelligence. A computer program that can independently recognize that it has been asked a question and will respond with output to the IRC channel. Granted, in the program I have written the bot is limited to seven statements or questions, but there are bots out there that have been programed to access library files of words and randomly put sentences together. These types of bot programs are much more advanced and give a much better perception of AI to an experienced user. However, even though the bot program I have written may not be as complex… to an inexperienced user, the effect is still the same.

Lets get started on creating an IRC bot!

Scope

To get a better understanding of bash shell scripting I will create a IRC bot that when asked a specific question will return an answer to output in the IRC server channel. I will also make the IRC bot to run independently in the background, so that the terminal will be available to the user.

Below I have listed the statements my IRC bot will be programed to answer:

1.) hi

2.) calender

3.) who is the best long cat ever

4.) who is on

5.) what is your name

6.) you are broken

7.) quit

Attributes

State and justify the attributes you'd like to receive upon successful approval and completion of this project.

  • Commands: Used commands like echo, sleep, who, cal, and cat within my script
  • Files & Directories: The program is a file, and I also redirected program output to another file
  • Text Processing: I used text processing commands like tail, grep, and cut
  • The UNIX shell: Assigning variables like BOT, CHAN, SERV, and CMD
  • Job Control & Multitasking: Program was ran in the background (./bigdaddybot.sh & )
  • Scripting: Created the program using bash shell scripting
  • Security: used chmod utility to change permissions and restrict access
  • System Administration: I own the program bigdaddybot.sh and execute the program to run within the irc server.

Procedure

The actual steps taken to accomplish the project. Include images, code snippets, command-line excerpts; whatever is useful for intuitively communicating important information for accomplishing the project.

We will start by opening up VI text editor and creating a file called bigdaddybot. We are going to write a bash shell script so I started my text document with a sh bang! (#!/bin/bash). Anytime you write a bash shell script the first line needs to start with a sh bang. I started by assigning some variables, BOT=“Bigdaddy” - the bots name, CHAN=“#botchan” - the name of the IRC channel, SERV=“irc” - assigning irc server. The next few lines of code in my program are the instructions for the bot to enter the correct irc channel. Once the bot enters the botchan, the program will echo to output “You have been infected by Bigdaddy”. At this point I started to write the statements that Bigdaddy would respond to. The first programed command is “hi”, and the bot will respond by sending to output , whats going on?…. Dude! I think your zipper is down. The second command is “calender”, and when this command is entered, the bot will respond by asking you to hang on a moment, then it will run the cal utility and output the result to the irc screen. The third command is “who is the best long cat ever”; when this command is executed the bigdaddybot will ask the user a question, “who the heck do you think”, sleep 2 seconds and respond with the answer “the best long cat ever would have to be…. Jimmy”. The fourth command the bot is programmed with is “who is on”. When this question is asked to the bigdaddybot, the program will respond stating “Good Question”, then execute the “who” command, direct it to display on the irc server, and finally follow up with a statement saying“ I think thats everyone!” The Fifth command the bot will respond to is “what is your name”; when this command is executed the bot will respond by saying “They call me… Bigdaddy!”. The sixth command I had a little fun with. When you make the statement “you are broken”, the bot will respond by telling the user “No I'm not! You are a moron!. The last command that the bot will respond to is “quit”. Typing this command will cause the irc bot to terminate itself.

Once the script was written and all the responses where redirected to a “in” file, that allows the bot output to be displayed on the irc channel, it was time to run the program. First I wanted to change the security of the program so that the owner was the only one able to execute, or modify the program. Typing the command “chmod 700 bigdaddybot.sh” successfully changed the permissions of the program to the desired level. Then I typed the following command at the prompt, ”./bigdaddybot.sh &“, which executes the program and places the running program in the background. The ”&“ key typed at the end of the execution command will send the program to the background to run. Once the program is running in the background, it was time to login to the irc bot channel. Once in the channel, the way to send the bot a command, would be to type the bot name, followed by a colon, then followed by the command. For example, if I typed, “Bigdaddy: hi” at the irc prompt, Bigdaddy would respond back to me by stating “Whats going on?…. Dude! I think your zipper is down!” Following the same format by typing “Bigdaddy: command” will allow you to communicate with the bot.

Code

#!/bin/bash
 
pkill ii 2>/dev/null
 
BOT="Bigdaddy"
CHAN="#botchan"
SERV="irc"
 
ii -s $SERV -n $BOT &
PID=$$
cd ~/irc/irc
echo "/join $CHAN" > in
cd "$CHAN"
echo "You have been infected by Bigdaddy" > in
CMD="`cat out|grep $BOT|tail -1|cut -d':' -f3`"
while [ ! "$CMD" = " quit" ];do
if [ "$CMD" = " hi" ]; then
        echo "Whats going on?.... Dude! I think your zipper is down!" > in
fi
if [ "$CMD" = " calender" ]; then
        echo "Hang on please......" > in
        sleep 2
        cal > in
        echo " There You Go!" > in
fi
if [ "$CMD" = " who is the best long cat ever" ];then
        echo "Who the heck do you think" > in
        sleep 2
        echo "The best long cat ever, would have to be...." > in
        sleep 2
        echo "J I M M Y.... of course!" > in
fi
if [ "$CMD" = " who is on" ]; then
        echo "Good Question!" > in
        sleep 1
        who > in
        sleep 1
        echo " I think that's everyone!" > in
fi
if [ "$CMD" = " what is your name" ]; then
        sleep 1
        echo "They call me... Bigdaddy!" > in
fi
 
if [ "$CMD" = " you are broken" ]; then
        sleep 2
        echo " No i'm not! You are a moron!" > in
        sleep 2
fi
        sleep 2
        CMD="`cat out|grep $BOT|tail -1|cut -d':' -f3`"
 
done
echo "/quit" > in
exit

Execution

This is the process for running my bot program from a terminal. Please notice that a ”&“ has been added to the end of the command. This is the needed indication to run the program in the background.

lab46:~$ cd bash
lab46:~/bash$ ./bigdaddybot.sh &
[1] 22144
lab46:~/bash$ 

Reflection

This project really helped me with the basics of shell scripting and how it can be implemented within everyday use of a irc chat channel. I also realized just how much work actually goes into each program. My program has fifty-five lines of code for 7 programmed commands. It was a lot of work to write this program and yet it is very small and will only do a small number of things. After this project I certainly have a much higher level of respect for the life of a programmer.

References

In performing this project, the following resources were referenced:

user/ccaccia/portfolio/project2.txt · Last modified: 2011/12/15 20:37 by ccaccia