All pastes #359303 Raw Edit

test

public text v1 · immutable
·published 2007-02-16 18:39 UTC
rendered paste body
#!/bin/bash

owner="CUT"

hostname=${1:-irc.freenet.de}
port=${2:-6667}
nick=${3:-0}
ident=${4:-bot}

chan=${5:-#specjal}
pass=${6:-}
# pass=${6:-password}

cmdchar="."
realname="CUT"

fifo=/tmp/bot.$$

# make the fifos
mkfifo ${fifo}.in
mkfifo ${fifo}.out

printf " [*] CUT \n" >&2

(nc -q0 $hostname $port < $fifo.in > $fifo.out; rm -f $fifo.in $fifo.out) &

exec 3>${fifo}.in 4<${fifo}.out

    read irc_info <&4
    printf "%s. \n" "${irc_info}" >&2
    printf "NICK %s \n" "${nick}" >&3
    printf " [*] Nick: %s sent \n" "${nick}" >&2
    printf "USER %s %s %s :%s \n" "${ident}" "${ident}" "${ident}" "${realname}" >&3
    read irc_reply <&4

    printf " [*] debug: %s \n" "${irc_reply}" >&2
    printf "JOIN %s %s \n" "${chan} ${pass}" >&3

while `test -n "${irc_reply}"`; do

# :CUT PRIVMSG !AIP17[[[CUT]]] :jest maj lof

    read irc_reply <&4
    time=`date | cut -f 4 -d " "`
    printf " [*] [%s] debug: %s \n" "${time}" "${irc_reply}" >&2
    response=(`echo $irc_reply`)

    if [ "${response[0]}" = "PING" ]; then

        printf "PONG %s \n" "${response[1]}" >&3
        printf " [*] [%s] debug->OUT: PONG %s \n" "${time}" "${response[1]}" >&2

    fi

    if [ `echo ${response[0]} | grep ${owner}` ]&&[ "${response[1]}" == "PRIVMSG" ]; then

        # Full text recived from bot owner
        owner_response=`echo ${irc_reply} | cut -f 3 -d ":"`

        # Command - part of text specified after a special character
        # Default is message after . (dot)
        owner_cmd=`echo ${owner_response} | awk -F "${cmdchar}" '{ print $2 }'`

        # Test it here is a command
        if [ -n "${owner_cmd}" ]; then
            # If yes - react

            # split command into pieces
            owner_cmd_id=`echo ${owner_cmd} | awk -F " " '{ print $1 }' | tr '[:upper:]' '[:lower:]'`
            owner_cmd_body=`echo ${owner_cmd} | sed -e 's/^'${owner_cmd_id}' //'`

            case "${owner_cmd_id}" in
                "quit") #Part IRC
                    printf "QUIT :%s \n" "${owner_cmd_body}" >&3
                ;;

                "join") # Join channels
                    chan_table=(`echo ${owner_cmd_body}`)
                    printf "JOIN %s %s \n" "${chan_table[0]}" "${chan_table[1]}" >&3
                ;;

                "part") # Part channels
                    chan_name=`echo ${owner_cmd_body} | awk -F " " '{ print $1 }'`
                    part_reason=`echo ${owner_cmd_body} | sed -e 's/^'${chan_name}' //'| tr '/n' ' '`

                    if [ "${chan}" == "${chan_name}" ]; then
                        printf "PRIVMSG %s :Cannot part controlling channel \n" "${chan}" >&3
                    else
                        printf "PART %s :%s \n" "${chan_name}" "${part_reason}" >&3
                    fi
                ;;

                "say") #say smoething
                    printf "PRIVMSG %s :%s \n" "${chan}" "${owner_cmd_body}" >&3
                ;;

                "raw") #send a raw IRC command
                    printf "%s \n" "${owner_cmd_body}" >&3
                ;;

                "nick") #Change bot's nick
                    printf "NICK %s \n" "${owner_cmd_body}" >&3
                ;;


                *) #default action
                    printf "NOTICE %s :command undefined or too few parameters - %s \n" "${chan}" "${owner_cmd_id}" >&3
                ;;
            esac
            # Command done - back to the loop start

        fi

        #printf "NOTICE %s :owner says - %s \n" "${chan}" "${owner_response}" >&3

    fi

done

exec 3>&- 4<&-