summaryrefslogtreecommitdiff
path: root/ircmsg.sh
blob: 754d73699379ee9559681d01e02337b2ec280e13 (plain)
    1 #!/bin/bash
    2 #
    3 # Ircmsg sends messages to irc servers using bash sockets.
    4 # Copyright (C)  2016  Aaron Ball <nullspoon@oper.io>
    5 #
    6 # This program is free software: you can redistribute it and/or modify
    7 # it under the terms of the GNU General Public License as published by
    8 # the Free Software Foundation, either version 3 of the License, or
    9 # (at your option) any later version.
   10 #
   11 # This program is distributed in the hope that it will be useful,
   12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
   13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   14 # GNU General Public License for more details.
   15 #
   16 # You should have received a copy of the GNU General Public License
   17 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
   18 #
   19 
   20 portfd=''
   21 
   22 #
   23 # Takes active file descriptor to irc server and reads until finding a line
   24 # matching "PING". Retrieves value of that line (without the PING prefix), and
   25 # returns so the corresponding PONG can be sent.
   26 #
   27 function get_ping {
   28   local portfd=${1}
   29 
   30   # get ping
   31   while [[ ${ping:0:4} != 'PING' ]]; do
   32     read ping <&${portfd}
   33   done
   34   
   35   ping=${ping:5}
   36   echo "${ping}"
   37 }
   38 
   39 
   40 # function connect {
   41 #   local server=${1}
   42 #   local port=${2}
   43 # 
   44 #   # Open the socket
   45 #   exec {portfd}<>/dev/tcp/${server}/${port}
   46 # 
   47 #   # Exit on failure
   48 #   [[ $? -gt 0 ]] && echo "Failed to connect to ${server}:${port}." && exit 1
   49 # 
   50 #   echo "${portfd}"
   51 # }
   52 
   53 
   54 function close_sock {
   55   local portfd=${1}
   56 
   57   # Close read
   58   exec {portfd}<&-
   59   # Close write
   60   exec {portfd}>&-
   61 }
   62 
   63 
   64 function main {
   65   argv=(${@})
   66 
   67   [[ -z ${1} ]] && echo "Please specify a server." && exit 1
   68   [[ -z ${2} ]] && echo "Please specify a server port." && exit 1
   69   [[ -z ${3} ]] && echo "Please specify a channel." && exit 1
   70   [[ -z ${4} ]] && echo "Please specify a message." && exit 1
   71 
   72   # Config
   73   nick="ircmsg"
   74   server="${1}"
   75   port="${2}"
   76   chan="${3}"
   77   msg="${4}"
   78 
   79   # Open the socket
   80   exec {portfd}<>/dev/tcp/${server}/${port}
   81   # Exit on failure
   82   [[ $? -gt 0 ]] && echo "Failed to connect to ${server}:${port}." && exit 1
   83 
   84   # Authenticate
   85   echo "NICK ${nick}" >&${portfd}
   86   echo "USER ${nick} 8 * : ${nick}" >&${portfd}
   87 
   88   # Respond to the server with its ping value (ping pong)
   89   ping=$(get_ping ${portfd})
   90   echo "PONG ${ping}" >&${portfd}
   91 
   92   # Send message to specified channel
   93   echo "PRIVMSG ${chan} ${msg}" >&${portfd}
   94 
   95   # Send quit command
   96   echo "QUIT :All done!" >&${portfd}
   97 
   98   # Close up shop
   99   close_sock ${portfd}
  100 }
  101 
  102 main "${@}"

Generated by cgit