blob: a50446682b74d24a1e944bd3bb479ddd41d782f9 (
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 # Note: This function will timeout when reading from the server 100 times
28 #
29 function get_ping {
30 local portfd=${1}
31 local emptymax=50
32 local emptycount=0
33
34 # get ping
35 while [[ ${emptycount} -lt ${emptymax} ]]; do
36 read ping <&${portfd}
37
38 # If first four match PING, break and process
39 [[ ${ping:0:4} == 'PING' ]] && break
40
41 # If response is empty, increment empty counter
42 [[ ${ping} == '' ]] && emptycount=$(($emptycount + 1))
43 done
44
45 ping=${ping:5}
46 echo "${ping}"
47 }
48
49
50 # function connect {
51 # local server=${1}
52 # local port=${2}
53 #
54 # # Open the socket
55 # exec {portfd}<>/dev/tcp/${server}/${port}
56 #
57 # # Exit on failure
58 # [[ $? -gt 0 ]] && echo "Failed to connect to ${server}:${port}." && exit 1
59 #
60 # echo "${portfd}"
61 # }
62
63
64 function close_sock {
65 local portfd=${1}
66
67 # Close read
68 exec {portfd}<&-
69 # Close write
70 exec {portfd}>&-
71 }
72
73
74 function main {
75 argv=(${@})
76
77 [[ -z ${1} ]] && echo "Please specify a server." && exit 1
78 [[ -z ${2} ]] && echo "Please specify a server port." && exit 1
79 [[ -z ${3} ]] && echo "Please specify a channel." && exit 1
80 [[ -z ${4} ]] && echo "Please specify a message." && exit 1
81
82 # Config
83 nick="ircmsg"
84 server="${1}"
85 port="${2}"
86 chan="${3}"
87 msg="${4}"
88
89 # Open the socket
90 exec {portfd}<>/dev/tcp/${server}/${port}
91 # Exit on failure
92 [[ $? -gt 0 ]] && echo "Failed to connect to ${server}:${port}." && exit 1
93
94 # Authenticate
95 echo "NICK ${nick}" >&${portfd}
96 echo "USER ${nick} 8 * : ${nick}" >&${portfd}
97
98 # Respond to the server with its ping value (ping pong)
99 ping=$(get_ping ${portfd})
100 if [[ ${ping} == '' ]]; then
101 echo "ERROR: No PING response received. Cannot continue."
102 exit 1
103 fi
104
105 echo "PONG ${ping}" >&${portfd}
106
107 # Send message to specified channel
108 echo "PRIVMSG ${chan} ${msg}" >&${portfd}
109
110 # Send quit command
111 echo "QUIT :All done!" >&${portfd}
112
113 # Close up shop
114 close_sock ${portfd}
115 }
116
117 main "${@}"
|