anoidc.sh 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. #!/bin/bash
  2. PATH=''
  3. term_height=0
  4. term_width=0
  5. term_scroll_height=0
  6. status_line_row=0
  7. idc_host=''
  8. idc_channel=''
  9. idc_nick=''
  10. idc_gecos=''
  11. scroll_bottom() {
  12. printf '\e[999B'
  13. }
  14. term_height() {
  15. printf '\e[999B\e[6n'
  16. read -s -r -d'['
  17. read -s -r -d';' term_height
  18. read -s -r -d'R' term_width
  19. printf '\e[999D'
  20. }
  21. scroll_helper() {
  22. term_scroll_height=$((term_height-2))
  23. status_line_row=$((term_height-1))
  24. printf '\e[0;%sr' "$term_scroll_height"
  25. }
  26. bottom_line() {
  27. printf '\e[%s;0f' "$term_height"
  28. }
  29. paste_data() {
  30. printf '\e7\e[%s;0f\n' "$term_scroll_height"
  31. printf ' %s' "$1"
  32. printf '\e8'
  33. }
  34. status_line() {
  35. printf '\e7\e[%s;0f\e[2K' "$status_line_row"
  36. printf '\e[4;44mSTATUS: %s in %s @ %s\e[0m' "$idc_nick" "$idc_channel" "$idc_host"
  37. printf '\e8'
  38. }
  39. init_screen() {
  40. printf '\e[r'
  41. term_height
  42. scroll_helper
  43. bottom_line
  44. }
  45. escape() {
  46. local str="${1//\\/\\\\}"
  47. str=${str//$'\t'/\\t}
  48. str=${str//$'\r'/\\r}
  49. str=${str//$'\n'/\\n}
  50. printf "%s" "$str"
  51. }
  52. net_fd_helper() {
  53. local buff=''
  54. #close 44 if open, then open read/write
  55. exec 44>&-
  56. exec 44<>"$1"
  57. printf 'LOGIN\tUSERNAME=%s\tPASSWORD=%s\r\n' "$idc_user" "$idc_pass" >&44
  58. while true
  59. do
  60. while IFS='' read -s -r -t1 -u 44
  61. do
  62. case "$REPLY" in
  63. ( :idc* )
  64. paste_data "$REPLY"
  65. ;;
  66. ( PING* )
  67. paste_data "$REPLY"
  68. buff="${REPLY##*:}"
  69. paste_data "PONG :$buff"
  70. printf 'PONG :%s\r\n' "$buff" >&44
  71. ;;
  72. ( * )
  73. buff="${REPLY%%\!*}"
  74. REPLY="${REPLY#*:*:}"
  75. buff="<${buff/:/}> $REPLY"
  76. paste_data "$buff"
  77. ;;
  78. esac
  79. done
  80. while IFS='' read -r -t1
  81. do
  82. status_line
  83. echo -en '\e[2K\r> '
  84. case "$REPLY" in
  85. ( :shell* )
  86. buff="${REPLY#*\ }"
  87. paste_data "$buff"
  88. eval "$buff"
  89. ;;
  90. ( :reset )
  91. init_screen
  92. break
  93. ;;
  94. ( /join* )
  95. idc_channel="${REPLY##*\ }"
  96. printf '%s\r\n' "${REPLY/\//}" >&44
  97. ;;
  98. ( /quit* )
  99. printf 'QUIT :%s\r\n' "${REPLY#*\ }" >&44
  100. exec 44>&-
  101. exit 0
  102. ;;
  103. ( /* )
  104. printf '%s\r\n' "${REPLY/\//}" >&44
  105. ;;
  106. ( "" )
  107. break
  108. ;;
  109. ( * )
  110. printf 'PRIVMSG %s :%s\r\n' "$idc_channel" "$REPLY" >&44
  111. ;;
  112. esac
  113. buff="<$idc_nick> ${REPLY}"
  114. paste_data "$buff"
  115. done
  116. done
  117. }
  118. main() {
  119. local idc_fd=''
  120. scroll_bottom
  121. read -p 'IDC Server: ' -e -r idc_host
  122. read -p 'IDC Port: ' -e -r idc_port
  123. read -p 'IDC Username: ' -e -r idc_user
  124. # printf 'IDC Password: '
  125. read -s -p 'IDC Password: ' -e -r idc_pass
  126. printf '\n'
  127. init_screen
  128. net_fd_helper "/dev/tcp/$idc_host/$idc_port"
  129. printf '\x1bc'
  130. }
  131. main