#!/bin/sh #$Id$ #Copyright (c) 2016-2021 Pierre Pronchery #This file is part of DeforaOS Devel scripts # #Redistribution and use in source and binary forms, with or without #modification, are permitted provided that the following conditions #are met: #1. Redistributions of source code must retain the above copyright # notice, this list of conditions and the following disclaimer. #2. Redistributions in binary form must reproduce the above copyright # notice, this list of conditions and the following disclaimer in the # documentation and/or other materials provided with the distribution. # #THIS SOFTWARE IS PROVIDED BY THE EDGEBSD PROJECT AND CONTRIBUTORS #``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED #TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR #PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS #BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR #CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF #SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS #INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN #CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) #ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE #POSSIBILITY OF SUCH DAMAGE. #variables #executables CUT="cut" FORTUNE="fortune -s" HEAD="head" II= KILL="kill" RM="rm -f" SLEEP="sleep 1" TOLOWER="_tolower" TR="tr" #settings DEVNULL="/dev/null" II_CMD_JOIN="/j" II_CMD_NOTICE="/II_CMD_NOTICE" II_CMD_PRIVMSG="/j" II_CMD_QUIT="/II_CMD_QUIT" II_PREFIX="/var/tmp/ii" PREFIX="/usr/local" PROGNAME="deforaos-irc" SYSCONFDIR="$PREFIX/etc" VENDOR="DeforaOS" VERBOSE=0 #load local settings [ -f "$SYSCONFDIR/$VENDOR/$PROGNAME.conf" ] && . "$SYSCONFDIR/$VENDOR/$PROGNAME.conf" [ -f "$HOME/.config/$VENDOR/$PROGNAME.conf" ] && . "$HOME/.config/$VENDOR/$PROGNAME.conf" #functions #irc _irc() { ret=0 pid=0 server=$(echo "$1" | $TOLOWER) port="$2" nickname="$3" channel=$(echo "$4" | $TOLOWER) serverin="$II_PREFIX/$server/in" channelin="$II_PREFIX/$server/$channel/in" #connect to the server if [ ! -w "$serverin" ]; then _info "$server: Connecting to server" $II -s "$server" -p "$port" -n "$nickname" > "$DEVNULL" & pid=$! fi #wait until the server is connected to loop=0 while [ ! -w "$serverin" ]; do $SLEEP loop=$((loop + 1)) if [ $loop -ge 30 ]; then ret=2 break fi done #initiate query if [ "${channel###*}" = "${channel}" ]; then #output the text while read line; do if [ $notice -eq 0 ]; then _info "$server: Messaging user $channel" output="$II_CMD_PRIVMSG $channel $line" else _info "$server: Notifying user $channel" output="$II_CMD_NOTICE $channel :$line" fi echo "$output" $SLEEP done > "$serverin" else #join the channel if [ ! -w "$channelin" ]; then _info "$server: Joining channel $channel" echo "$II_CMD_JOIN $channel" > "$serverin" fi #wait until the channel is joined loop=0 while [ ! -w "$channelin" ]; do $SLEEP loop=$((loop + 1)) if [ $loop -ge 30 ]; then ret=3 break fi done #output the text if [ $ret -eq 0 ]; then if [ $notice -eq 0 ]; then _info "$server: Joined channel $channel" while read line; do echo "$line" $SLEEP done > "$channelin" else _info "$server: Notifying channel $channel" while read line; do echo "$II_CMD_NOTICE $channel :$line" $SLEEP done > "$serverin" fi else _error "$channel: Could not join channel" fi fi if [ $pid -gt 0 ]; then #quit the server _info "$server: Disconnecting from server" fortune=$($FORTUNE | $HEAD -n 1 | $CUT -c 1-50) echo "$II_CMD_QUIT :$fortune" > "$serverin" #wait until the server is disconnected #FIXME ii does not automatically clean up when quitting loop=0 while [ ! -w "$channelin" ]; do $SLEEP loop=$((loop + 1)) if [ $loop -ge 30 ]; then ret=4 break fi done #force quit the server if necessary [ $ret -eq 0 ] || $KILL "$pid" $RM -- "$serverin" "$channelin" fi return $ret } #error _error() { echo "$PROGNAME: $@" 1>&2 return 2 } #info _info() { [ $VERBOSE -eq 0 ] || echo "$PROGNAME: $@" } #tolower _tolower() { $TR A-Z a-z } #usage _usage() { echo "Usage: $PROGNAME [-Nqv][-n nickname] server channel" 1>&2 echo " -N Use notice" 1>&2 echo " -q Quiet mode (default)" 1>&2 echo " -v Be more verbose" 1>&2 return 1 } #main #parse options nickname="$USER" notice=0 port=6667 while getopts "Nn:O:qv" name; do case "$name" in N) notice=1 ;; n) nickname="$OPTARG" ;; O) export "${OPTARG%%=*}"="${OPTARG#*=}" ;; p) port="$OPTARG" ;; q) VERBOSE=0 ;; v) VERBOSE=$((VERBOSE + 1)) ;; *) _usage exit $? ;; esac done shift $((OPTIND - 1)) if [ $# -ne 2 ]; then _usage exit $? fi server="$1" channel="$2" if [ -z "$server" -o -z "$channel" ]; then _usage exit $? fi [ -n "$II" ] || II="ii -i $II_PREFIX" _irc "$server" "$port" "$nickname" "$channel"