#!/bin/bash # # Ircmsg sends messages to irc servers using bash sockets. # Copyright (C) 2016 Aaron Ball # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see . # portfd='' # # Takes active file descriptor to irc server and reads until finding a line # matching "PING". Retrieves value of that line (without the PING prefix), and # returns so the corresponding PONG can be sent. # # Note: This function will timeout when reading from the server 100 times # function get_ping { local portfd=${1} local emptymax=50 local emptycount=0 # get ping while [[ ${emptycount} -lt ${emptymax} ]]; do read ping <&${portfd} # If first four match PING, break and process [[ ${ping:0:4} == 'PING' ]] && break # If response is empty, increment empty counter [[ ${ping} == '' ]] && emptycount=$(($emptycount + 1)) done ping=${ping:5} echo "${ping}" } # function connect { # local server=${1} # local port=${2} # # # Open the socket # exec {portfd}<>/dev/tcp/${server}/${port} # # # Exit on failure # [[ $? -gt 0 ]] && echo "Failed to connect to ${server}:${port}." && exit 1 # # echo "${portfd}" # } function close_sock { local portfd=${1} # Close read exec {portfd}<&- # Close write exec {portfd}>&- } function main { argv=(${@}) [[ -z ${1} ]] && echo "Please specify a server." && exit 1 [[ -z ${2} ]] && echo "Please specify a server port." && exit 1 [[ -z ${3} ]] && echo "Please specify a channel." && exit 1 [[ -z ${4} ]] && echo "Please specify a message." && exit 1 # Config nick="ircmsg" server="${1}" port="${2}" chan="${3}" msg="${4}" # Open the socket exec {portfd}<>/dev/tcp/${server}/${port} # Exit on failure [[ $? -gt 0 ]] && echo "Failed to connect to ${server}:${port}." && exit 1 # Authenticate echo "NICK ${nick}" >&${portfd} echo "USER ${nick} 8 * : ${nick}" >&${portfd} # Respond to the server with its ping value (ping pong) ping=$(get_ping ${portfd}) if [[ ${ping} == '' ]]; then echo "ERROR: No PING response received. Cannot continue." exit 1 fi echo "PONG ${ping}" >&${portfd} # Send message to specified channel echo "PRIVMSG ${chan} ${msg}" >&${portfd} # Send quit command echo "QUIT :All done!" >&${portfd} # Close up shop close_sock ${portfd} } main "${@}"