123 lines
4.1 KiB
Bash
123 lines
4.1 KiB
Bash
#!/bin/bash
|
|
# -*- coding: utf-8 -*-
|
|
###########################################################################
|
|
# #
|
|
# envbot - an IRC bot in bash #
|
|
# Copyright (C) 2007-2008 Arvid Norlander #
|
|
# #
|
|
# 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 <http://www.gnu.org/licenses/>. #
|
|
# #
|
|
###########################################################################
|
|
#---------------------------------------------------------------------
|
|
## A transport module using /dev/tcp
|
|
#---------------------------------------------------------------------
|
|
|
|
# A list of features supported
|
|
# These are used: ipv4, ipv6, ssl, nossl, bind
|
|
transport_supports="ipv4 ipv6 nossl"
|
|
|
|
# Check if all the stuff needed to use this transport is available
|
|
# Return status
|
|
# 0 yes
|
|
# 1 no
|
|
transport_check_support() {
|
|
# If anyone can tell me how to check if /dev/tcp is supported
|
|
# without trying to make a connection (that could fail for so
|
|
# many other reasons), please contact me.
|
|
echo "NOTE: It is possible that this transport is not supported on your system"
|
|
echo " However, there is no way it can be checked except trying to connect."
|
|
echo " If you see an error below try netcat or socat transport instead."
|
|
return 0
|
|
}
|
|
|
|
# Try to connect
|
|
# Parameters
|
|
# $1 hostname/IP
|
|
# $2 port
|
|
# $3 If 1 use SSL. If the module does not support it, just ignore it.
|
|
# $4 IP to bind to if any and if supported
|
|
# If the module does not support it, just ignore it.
|
|
# Return status
|
|
# 0 if Ok
|
|
# 1 if connection failed
|
|
transport_connect() {
|
|
exec 3<&-
|
|
exec 3<> "/dev/tcp/${1}/${2}"
|
|
time_get_current 'transport_lastvalidtime'
|
|
}
|
|
|
|
# Called to close connection
|
|
# No parameters, no return code check
|
|
transport_disconnect() {
|
|
exec 3<&-
|
|
# To force code to consider this disconnected.
|
|
transport_lastvalidtime=0
|
|
}
|
|
|
|
# Return status
|
|
# 0 If connection is still alive
|
|
# 1 If it isn't.
|
|
transport_alive() {
|
|
local newtime=
|
|
time_get_current 'newtime'
|
|
(( newtime - transport_lastvalidtime > 300 )) && return 1
|
|
return 0
|
|
}
|
|
|
|
# Return a line in the variable line.
|
|
# Return status
|
|
# 0 If Ok
|
|
# 1 If connection failed
|
|
transport_read_line() {
|
|
while true
|
|
do
|
|
ze_length="$( wc -l '/tmp/un-provoked-message-store' 2> /dev/null )"
|
|
|
|
if [[ -r /tmp/un-provoked-message-store ]] &&
|
|
[[ -w /tmp/un-provoked-message-store ]] && (( ${ze_length%% *} ))
|
|
then
|
|
read -r line < /tmp/un-provoked-message-store
|
|
line=':tlCJ99mfZl!~user@::1 PRIVMSG #hyperbola :'"${line}"
|
|
|
|
# remove the top line of the file
|
|
if (( ${ze_length%% *} < 2 ))
|
|
then
|
|
echo -n > /tmp/un-provoked-message-store
|
|
else
|
|
tail -n +2 /tmp/un-provoked-message-store |
|
|
sponge /tmp/un-provoked-message-store
|
|
fi
|
|
|
|
break
|
|
else
|
|
read -t 5 -ru 3 line
|
|
the_return_code="${?}"
|
|
(( the_return_code == 0 )) && break
|
|
(( the_return_code > 128 )) && continue
|
|
[[ "${the_return_code}" -ne 0 ]] && return
|
|
fi
|
|
done
|
|
|
|
time_get_current 'transport_lastvalidtime'
|
|
line=${line//$'\r'/}
|
|
}
|
|
|
|
# Send a line
|
|
# Parameters
|
|
# $* send this
|
|
# Return code not checked.
|
|
transport_write_line() {
|
|
echo "$*" >&3
|
|
}
|