dots

git clone git://mattcarlson.org/repos/dots.git
Log | Files | Refs

chat.sh (2365B)


      1 #!/bin/sh
      2 #
      3 # chat
      4 
      5 HOST='127.0.0.1'
      6 # standard IRC port
      7 PORT='6667'
      8 # encrypted connection
      9 SSL='6697'
     10 # the most popular IRC network
     11 NETWORK='irc.libera.chat'
     12 CHANNEL='archlinux'
     13 NICK='RETRACTED'
     14 PASS="${XDG_DATA_HOME:-${HOME}/.local/share/}/kirc/pass.gpg"
     15 LOG="${XDG_DATA_HOME:-${HOME}/.local/share/}/kirc/kirc.log"
     16 
     17 log() {
     18     while :; do
     19         tail -fn 1 "${1}" | awk "/PRIVMSG #.*${NICK}.*/ {
     20             system(\"herbe 'kirc - new message!' &\")
     21             exit
     22         }"
     23         printf '%s\n' "$(date +'%a %b %e %H:%M:%S %Y')::kirc - new message!" >> "${1}"
     24         sleep 1
     25     done
     26 
     27     return 0
     28 }
     29 
     30 # TODO: figure out how to 'daemonize' kirc, maybe implement networks other than libera
     31 main() {
     32     # test for dependencies
     33     { command -v socat >/dev/null && command -v kirc >/dev/null; } || { printf '%s\n' 'socat and/or kirc not installed.'; return 1; }
     34 
     35     # connect to network
     36     while ! socat /dev/null ssl:"${network}":"${SSL}" 2>/dev/null; do
     37         printf '%s' "Enter IRC network [${NETWORK}]: "
     38         read -r network
     39         if [ -z "${network}" ]; then
     40             network="${NETWORK}"
     41         else
     42             printf '%s\n' 'Testing network...'
     43         fi
     44     done
     45     if socat tcp-listen:"${PORT}",reuseaddr,fork,bind="${HOST}" ssl:"${network}":"${SSL}" >/dev/null 2>&1 & pids="${pids-} ${!}"; then
     46         # get socat pid so we can kill it after
     47         socatid="${!}"
     48 
     49         printf '%s\n' "Securely connected to ${network} via TLS/SSL."
     50 
     51         # get channel to join
     52         printf '%s' "Enter channel name without the '#' [archlinux]: "
     53         read -r channel
     54         [ -z "${channel}" ] && channel="${CHANNEL}"
     55 
     56         # gen auth token for SASL PLAIN
     57         pass="$(gpg -dq "${PASS}")"
     58         SASL="$(python -c "import base64; print(base64.standard_b64encode(b'${NICK}\x00${NICK}\x00${pass}'))")"
     59         #shellcheck disable=SC2059
     60         SASL="$(printf '%s\n' "${SASL}" | awk -v FS="(b'|')" '{print $2}')"
     61 
     62         # enable logging
     63         log "${LOG}" & pids="${pids-} ${!}"
     64 
     65         # launch IRC client
     66         kirc -s "${HOST}" -a "${SASL}" -c "${channel}" -n "${NICK}" -o "${LOG}"
     67 
     68         # kill pids
     69         #shellcheck disable=SC2086
     70         kill -9 ${pids}
     71     else
     72         printf '%s\n' 'Unable to connect via TLS/SSL. Is the address or port already in use?'
     73         return 1
     74     fi
     75 
     76     return 0
     77 }
     78 
     79 main "${@}"