Home » BASH » Making a simple IRC chat bot logger using bash script

Making a simple IRC chat bot logger using bash script

Last updated May 18, 2021

In this article, we will make a simple bot logger. This script will log a few channels as well as handle the pings.

Prerequisites

Besides having a Terminal open, you need to have basic knowledge of IRC.

Write script:

Now, we will write a script for an IRC logging bot. Create the logging_bot.sh script and write the following code in it:

logging_bot.sh

#!/bin/bash 
nick="blb$$" 
channel=testchannel 
server=irc.freenode.net 
config=/tmp/irclog 
[ -n "$1" ] && channel=$1 
[ -n "$2" ] && server=$2 
config="${config}_${channel}" 
echo "NICK $nick" > $config 
echo "USER $nick +i * :$0" >> $config 
echo "JOIN #$channel" >> $config 
trap "rm -f $config;exit 0" INT TERM EXIT 
tail -f $config | nc $server 6667 | while read MESSAGE 
do 
	case "$MESSAGE" in 
		PING*) echo "PONG${MESSAGE#PING}" >> $config;; *QUIT*) ;; 
		*PART*) ;; 
		*JOIN*) ;; 
		*NICK*) ;; 
		*PRIVMSG*) echo "${MESSAGE}" | sed -nr "s/^:([^!]+).*PRIVMSG[^:]+:(.*)/[$(date '+%R')] \1> \2/p";; 
		*) echo "${MESSAGE}";; 
	esac 
done

How script works

In this script, we handled the pings; also, we are logging some channels.

Related Posts

Creating a config file and using it in tandem with your scripts

In this article, we are going to create a config file and use it in our shell script.PrerequisitesBesides having a terminal open, you need basic knowledge of creating scripts and config files.Write scriptNow, we are going to create a script and config file. The...

Calculating and reducing the runtime of a script

In this article, we are going to learn how to calculate and reduce the script’s runtime. A simple time command will help in calculating the execution time.PrerequisitesBesides having a terminal open, make sure you have the necessary scripts present in your...

Using Bash to monitor battery life and optimize it

In this article, we will learn about the TLP Linux tool. TLP is a command-line tool; it is used for power management and will optimize the battery life.PrerequisitesBesides having a Terminal open, you need to ensure that you have TLP installed on your system.How to do...

Creating a simple NAT and DMZ firewall using bash script

In this article, we will create a simple NAT firewall with DMZ using iptables.PrerequisitesBesides having a Terminal open, you need to ensure that iptables is installed in your machine.Write scriptWe will write a script to set up a DMZ using iptables. Create...

0 Comments

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

thirteen − 3 =

Follow Us

Our Communities

More on BASH

The Ultimate Managed Hosting Platform
Load WordPress Sites in as fast as 37ms!