BASH 119
Irc_bash Guest on 2nd January 2021 06:11:26 PM
  1. #!/bin/bash
  2.  
  3. #no PATH, no way to accidently run any programs
  4. PATH=''
  5.  
  6. #useful variables
  7. term_height=0
  8. term_width=0
  9. term_scroll_height=0
  10. status_line_row=0
  11. irc_host=''
  12. irc_channel=''
  13. irc_nick=''
  14.  
  15.  
  16. function scroll_bottom() {
  17.         echo -en '\e[999B'
  18. }
  19.  
  20. #figure out terminal height, NOTE: moves cursor to bottom of terminal
  21. function term_height() {
  22.         echo -en '\e[999B\e[6n'
  23.         read -s -r -d'['
  24.         read -s -r -d';' term_height
  25.         read -s -r -d'R' term_width
  26.         echo -en '\e[999D'
  27. }
  28.  
  29. #set the area the terminal is allowed to scroll in
  30. function scroll_helper() {
  31.         term_scroll_height=$((term_height-2))
  32.         status_line_row=$((term_height-1))
  33.         echo -en "\e[0;${term_scroll_height}r"
  34. }
  35.  
  36. function bottom_line() {
  37.         echo -en "\e[${term_height};0f"
  38. }
  39.  
  40. function paste_data() {
  41.         echo -en '\e7' "\e[${term_scroll_height};0f" '\n'
  42.         echo -n " $1"
  43.         echo -en '\e8'
  44. }
  45.  
  46. function status_line() {
  47.         echo -en '\e7' "\e[${status_line_row};0f" '\e[2K'
  48.         echo -en "\e[4;44mSTATUS: $irc_nick in $irc_channel @ $irc_host\e[0m"
  49.         echo -en '\e8'
  50. }
  51.  
  52. function init_screen() {
  53.         echo -en '\e[r' #reset screen
  54.         term_height
  55.         scroll_helper
  56.         bottom_line
  57. }
  58.  
  59. function net_fd_helper() {
  60.         local buff=''
  61.        
  62.         #close 44 if open, then open read/write
  63.         exec 44>&-
  64.         exec 44<>"$1"
  65.        
  66.         #delay slightly to improve chance of success
  67.         #read -s -r -t1 -u 44
  68.  
  69.         #try connecting, sometimes JOIN won't work here and will have to be done manually
  70.         echo "NICK $irc_nick" >&44
  71.         echo "USER $irc_nick 8 * :${HOSTNAME} ${USER}" >&44
  72.         echo "JOIN $irc_channel" >&44
  73.        
  74.         while true
  75.         do
  76.                 while IFS='' read -s -r -t1 -u 44
  77.                 do
  78.                         case "$REPLY" in
  79.                         ( :irc* )
  80.                                 paste_data "$REPLY"
  81.                                 ;;
  82.                         ( PING* )
  83.                                 paste_data "$REPLY"
  84.                                 buff="${REPLY##*:}"
  85.                                 paste_data "PONG :$buff"
  86.                                 echo "PONG :$buff" >&44
  87.                                 ;;
  88.                         ( * )
  89.                                 buff="${REPLY%%\!*}"
  90.                                 REPLY="${REPLY#*:*:}"
  91.                                 buff="<${buff/:/}> $REPLY"
  92.                                 paste_data "$buff"
  93.                                 ;;
  94.                         esac
  95.                 done
  96.        
  97.  
  98.                 while IFS='' read -r -t1
  99.                 do
  100.                         status_line
  101.                         echo -en '\e[2K\r> '
  102.                         case "$REPLY" in
  103.                         ( :shell* )
  104.                                 buff="${REPLY#*\ }"
  105.                                 paste_data "$buff"
  106.                                 eval "$buff"
  107.                                 ;;
  108.                         ( :reset )
  109.                                 init_screen
  110.                                 break
  111.                                 ;;
  112.                         ( /JOIN* )
  113.                                 irc_channel="${REPLY##*\ }"
  114.                                 echo "${REPLY/\//}" >&44
  115.                                 ;;
  116.                         ( /QUIT* )
  117.                                 echo "QUIT :${REPLY#*\ }" >&44
  118.                                 exec 44>&-
  119.                                 exit 0
  120.                                 ;;
  121.                         ( /* )
  122.                                 echo "${REPLY/\//}" >&44
  123.                                 ;;
  124.                         ( "" )
  125.                                 break
  126.                                 ;;
  127.                         ( * )
  128.                                 echo "PRIVMSG $irc_channel :$REPLY" >&44
  129.                                 ;;
  130.                         esac
  131.                        
  132.                         buff="<$irc_nick> ${REPLY}"
  133.                         paste_data "$buff"
  134.                 done
  135.         done
  136. }
  137.  
  138. function main() {
  139.         local irc_fd=''
  140.         scroll_bottom
  141.         read -p 'IRC Server (eg irc.rizon.net): ' -e -r irc_host
  142.         read -p 'IRC Channel (eg #foobar): ' -e -r irc_channel
  143.         read -p 'IRC Nickname (eg egg_lover): ' -e -r irc_nick
  144.         init_screen
  145.         net_fd_helper "/dev/tcp/$irc_host/6667"
  146. }
  147.  
  148. main

Paste Reisub es para el código fuente y el texto de depuración general.

Iniciar sesión o Registrarse para editar, eliminar y mantener un seguimiento de sus pegados y mucho más.

Pasta cruda

Iniciar sesión o Registrarse para editar o bifurcar este pegado. Es gratis.