anoirc.sh 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. #!/bin/bash
  2. PATH=''
  3. term_height=0
  4. term_width=0
  5. term_scroll_height=0
  6. status_line_row=0
  7. irc_host=''
  8. irc_channel=''
  9. irc_nick=''
  10. irc_gecos=''
  11. function scroll_bottom() {
  12. printf '\e[999B'
  13. }
  14. #figure out terminal height, NOTE: moves cursor to bottom of terminal
  15. function term_height() {
  16. printf '\e[999B\e[6n'
  17. read -s -r -d'['
  18. read -s -r -d';' term_height
  19. read -s -r -d'R' term_width
  20. printf '\e[999D'
  21. }
  22. # Set the area the terminal is allowed to scroll in
  23. function scroll_helper() {
  24. term_scroll_height=$((term_height-2))
  25. status_line_row=$((term_height-1))
  26. printf '\e[0;%sr' "$term_scroll_height"
  27. }
  28. function bottom_line() {
  29. printf '\e[%s;0f' "$term_height"
  30. }
  31. function paste_data() {
  32. printf '\e7\e[%s;0f\n' "$term_scroll_height"
  33. printf ' %s' "$1"
  34. printf '\e8'
  35. }
  36. function status_line() {
  37. printf '\e7\e[%s;0f\e[2K' "$status_line_row"
  38. printf '\e[4;44mSTATUS: %s in %s @ %s\e[0m' "$irc_nick" "$irc_channel" "$irc_host"
  39. printf '\e8'
  40. }
  41. function init_screen() {
  42. printf '\e[r'
  43. term_height
  44. scroll_helper
  45. bottom_line
  46. }
  47. function net_fd_helper() {
  48. local buff=''
  49. #close 44 if open, then open read/write
  50. exec 44>&-
  51. exec 44<>"$1"
  52. #delay slightly to improve chance of success
  53. #read -s -r -t1 -u 44
  54. printf 'NICK %s\r\n' "$irc_nick" >&44
  55. printf 'USER %s %s %s :%s\r\n' "$irc_nick" "$HOSTNAME" "$USER" "$irc_gecos" >&44
  56. while true
  57. do
  58. while IFS='' read -s -r -t1 -u 44
  59. do
  60. case "$REPLY" in
  61. ( :irc* )
  62. paste_data "$REPLY"
  63. ;;
  64. ( PING* )
  65. paste_data "$REPLY"
  66. buff="${REPLY##*:}"
  67. paste_data "PONG :$buff"
  68. printf 'PONG :%s\r\n' "$buff" >&44
  69. ;;
  70. ( * )
  71. buff="${REPLY%%\!*}"
  72. REPLY="${REPLY#*:*:}"
  73. buff="<${buff/:/}> $REPLY"
  74. paste_data "$buff"
  75. ;;
  76. esac
  77. done
  78. while IFS='' read -r -t1
  79. do
  80. status_line
  81. echo -en '\e[2K\r> '
  82. case "$REPLY" in
  83. ( :shell* )
  84. buff="${REPLY#*\ }"
  85. paste_data "$buff"
  86. eval "$buff"
  87. ;;
  88. ( :reset )
  89. init_screen
  90. break
  91. ;;
  92. ( /join* )
  93. irc_channel="${REPLY##*\ }"
  94. printf '%s\r\n' "${REPLY/\//}" >&44
  95. ;;
  96. ( /quit* )
  97. printf 'QUIT :%s\r\n' "${REPLY#*\ }" >&44
  98. exec 44>&-
  99. exit 0
  100. ;;
  101. ( /* )
  102. printf '%s\r\n' "${REPLY/\//}" >&44
  103. ;;
  104. ( "" )
  105. break
  106. ;;
  107. ( * )
  108. printf 'PRIVMSG %s :%s\r\n' "$irc_channel" "$REPLY" >&44
  109. ;;
  110. esac
  111. buff="<$irc_nick> ${REPLY}"
  112. paste_data "$buff"
  113. done
  114. done
  115. }
  116. function main() {
  117. local irc_fd=''
  118. scroll_bottom
  119. read -p 'IRC Server: ' -e -r irc_host
  120. read -p 'IRC Nickname: ' -e -r irc_nick
  121. read -p 'IRC Realname: ' -e -r irc_gecos
  122. init_screen
  123. net_fd_helper "/dev/tcp/$irc_host/6667"
  124. printf '\x1bc'
  125. }
  126. main