#!/bin/sh
#
# TazIRC - SliTaz IRC client: Keep it small! Maybe use a plugins system.
# We use a single temporary text file to send commands to the IRC server
# and handle custom user commands such /q to quit.
#
# Copyright 2014 (C) SliTaz GNU/Linux - BSD License
# Author: Christophe Lincoln <pankso@slitaz.org>
#
. /lib/libtaz.sh

# Internationalization
TEXTDOMAIN='tazirc'
export TEXTDOMAIN

# Early function used by --ask
logo() {
	clear
	colorize 30 "TazIRC - SliTaz IRC Client"
}

# Handle ask mode now. So we set $host $nick and optionally $chan
# Ask only for a nick: tazirc --ask --host=irc.freenode.net --chan=slitaz
if [ "$ask" ]; then
	logo
	gettext "Auto-join a channel is optional"; echo ""
	newline
	if [ "$host" ]; then
		gettext "IRC server : "; echo "$host"
	else
		gettext "IRC server : "; read host
	fi
	if [ ! "$nick" ]; then 
		gettext "Nick name  : "; read nick
	fi
	if [ ! "$chan" ]; then
		gettext "Channel    : "; read chan
	fi
	newline
	if [ ! "$host" ] || [ ! "$nick" ]; then
		echo "$(colorize 31 'ERROR:') $(gettext 'empty value')"
		newline && sleep 2 && exit 0
	fi
fi

# Cmdline --options= are parsed by libtaz.sh
[ "$host" ] || host="$1"
[ "$nick" ] || nick="$2"
[ "$mode" ] || mode="+i"
[ "$port" ] || port="6667"
send="/tmp/tazirc/${host}.${nick}.$$.txt"

# Help and usage without 2 cmdline args
if [ ! "$host" ] || [ ! "$nick" ]; then
	cat << EOT

$(boldify "$(gettext 'Usage:')") $(basename $0) [host] [nick] [--option]

$(boldify "$(gettext 'Options:')")
  --chan=    $(gettext "Join specified channel after connection")
  --mode=    $(gettext "Use specified mode. Default: +i")
  --port=    $(gettext "Use specified port. Default: 6667")
  --ask      $(gettext "Will ask for server, nick and channel")

EOT
	exit 0
fi

# Clean up on exit
trap "echo 'Exiting...' && rm -f $send" SIGINT INT TERM
trap "kill 0" EXIT
mkdir -p $(dirname $send)

#
# Functions
#

# Error message: error "Message blabla..."
error() {
	echo "$(colorize 31 'ERROR:') $1"
}

#
# Start: send login commands to connect to server, then handle commands
#

# Hello World!
[ ! "$ask" ] && logo
boldify "$(gettext 'Connecting to:') $host $([ $chan ] && echo \#${chan})"

# Login commands
cat > ${send} << EOT
NICK $nick
USER $nick $mode * :$0
EOT
[ "$chan" ] && echo "JOIN #${chan}" >> ${send}

# Connect and handle server messages
(tail -f ${send} | busybox nc ${host} ${port} | while read MESSAGE
do
	debug "$MESSAGE"
	case "$MESSAGE" in
		*" PRIVMSG "*)
			# Display channel messages
			user=$(echo "${MESSAGE%!*}" | sed s'/://')
			text=$(echo "${MESSAGE#* :}")
			echo "[$(date -u '+%R')] $(colorize 35 "$user"): $text" ;;
		*" MODE "*)
			echo "${MESSAGE#* }" ;;
		PING*)
			# Responding to ping
			echo "PONG${MESSAGE#PING}" >> ${send} ;;
		*)
			echo "${MESSAGE#* :}" ;;
	esac
done) &

# Handle user input commands/messages
while read COMMAND
do
	# tazirc> prompt ?
	# while true: echo -en "$(colorize 33 "tazirc")> "; read COMMAND
	case "$COMMAND" in
		"") continue ;;
		/JOIN*|/join*|/j*)
			chan="$(echo ${COMMAND} | cut -d '#' -f 2)"
			boldify "$(gettext 'Joining:') #$chan"
			echo "JOIN #$chan" >> ${send} ;;
		/QUIT|/quit|/q)
			boldify "$(gettext 'Disconnecting from:') $host"
			echo "QUIT" >> ${send}
			sleep 1 && rm -f ${send} && break ;;
		/*)
			echo "${COMMAND#/}" >> ${send} ;;
		*)
			if [ ! "$chan" ]; then
				error "$(gettext 'No channel to send to')" && continue
			fi
			echo "[$(date -u '+%R')] $(colorize 34 "$nick"): ${COMMAND}"
			echo "PRIVMSG #$chan :${COMMAND}" >> ${send} ;;
	esac
done

exit 0
