Advertisement
Guest User

Untitled

a guest
Sep 25th, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 3.06 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. # Defaults
  4. SHIC_HOST="localhost"
  5. SHIC_PORT=4243
  6. SHIC_NICK="dicks"
  7. SHIC_PASS=""
  8. USE_SSL="0"
  9. LOCALPORT="$(( 44001 + $(( $RANDOM % 4000 ))))"
  10.  
  11. # e.g: SHIC_SCRIPT=":j #archlinux; Heya all!; :s;
  12. [[ -z $SHIC_SCRIPT ]] && SHIC_SCRIPT=""
  13. # Red error, green background for private message, cyan for #archlinux,
  14. # white for conversations in and out, and gray for everything else
  15. [[ -z $SHIC_PREFIX ]] && SHIC_PREFIX=(
  16.     "\e[31m::^ERROR"
  17.     "\e[42m\e[30m::(^<[^@]*@[^#])"
  18.     "\e[36m::#archlinux"
  19.     "\e[0m::^<"
  20.     "\e[0m::^->"
  21.     "\e[1;30m::(.*)"
  22. )
  23.  
  24. # Don't exit at Ctrl-C
  25. #trap "echo" SIGINT
  26. #trap "kill 0" EXIT
  27.  
  28. # Send raw message to server
  29. function _send() {
  30.     printf "%s\r\n" "$*" >&3
  31. }
  32.  
  33. # Print for user
  34. function _output() {
  35.     _prefix=""
  36.     for rule in ${SHIC_PREFIX[@]}; do
  37.         [[ "$@" =~ ${rule#*::} ]] && _prefix="${rule%%::*}$_prefix"
  38.     done
  39.  
  40. printf "$_prefix%s\e[0m\n" "$*"
  41. }
  42.  
  43. # Handle user input
  44. function _input() {
  45.     local line="$@"
  46.     if [[ "${line:0:1}" != "/" ]]; then
  47.         [[ -z $channel ]] && _output "ERROR: No channel to send to" && return
  48.  
  49.     _send "PRIVMSG $channel :$line"
  50.         _output "-> $channel> $line"
  51.         return
  52.     else
  53.  
  54. if [[ ${#line} == 2 || ${line:2:1} == " " ]]; then
  55. _txt="${line:3}"
  56.         case ${line:1:1} in
  57.             m ) read -r _to _msg <<< "$_txt" && _send "PRIVMSG $_to :$_msg" && _output "-> $_to> $_msg"; return;;
  58.             l ) read -r _from _msg <<< "$_txt" && _send "PART $_from :$_msg"; return;;
  59.             j ) _send "JOIN $_txt"; [[ -z $channel ]] && channel=$_txt; return;;
  60.             s ) channel="$_txt"; return;;
  61.             q ) _send "QUIT"; exit 0;;
  62.         esac
  63. fi
  64. fi
  65.     # Not recognized command, send to server
  66.     _send "${line:1}"
  67. }
  68.  
  69. # Parse command line
  70. while getopts "h:p:n:k:v" flag; do
  71. case $flag in
  72.         h) SHIC_HOST="$OPTARG";;
  73.         p) SHIC_PORT="$OPTARG";;
  74.         n) SHIC_NICK="$OPTARG";;
  75.         k) SHIC_PASS="$OPTARG";;
  76.         ?) printf "Unknown option. Usage: $0 [-h hostname] [-p port] [-n nick] [-k password]\n"
  77. >&2; exit
  78. 1;;
  79.     esac
  80. done
  81.  
  82.  
  83.  
  84. socat OPENSSL:$SHIC_HOST:$SHIC_PORT,verify=0 TCP4-LISTEN:$LOCALPORT,fork &
  85.  
  86. while [ ! $(exec 3<>/dev/tcp/localhost/$LOCALPORT) ] ; do sleep 10; done
  87.  
  88. {
  89.     while read _line; do
  90.         [[ ${_line:0:1} == "/" ]] && _source="${_line%% *}" && _line="${_line#* }"
  91.         _source="${_source:1}"
  92.         _user=${_source%%\!*}
  93.         _txt="${_line#*:}"
  94.  
  95.         case "${_line%% *}" in
  96.             "PING")
  97.                 _send "PONG" ;;
  98.             "PRIVMSG")
  99.                 _ch="${_line%% :*}"
  100.                 _ch="${_ch#* }"
  101.                 _output "<$_user@$_ch> $_txt" ;;
  102.             *)
  103.                 _output "$_source >< $_line" ;;
  104.         esac
  105. done
  106. } <&3 &
  107.  
  108.  
  109.  
  110. _send "NICK $SHIC_NICK"
  111. _send "USER $SHIC_NICK localhost $SHIC_HOST :$SHIC_NICK"
  112.  
  113. function _trim() { echo $1; }
  114.  
  115. # Execute login script
  116. IFS=";" read -ra C <<< "$SHIC_SCRIPT"
  117. for _cmd in "${C[@]}"; do
  118. _input $(_trim "$_cmd")
  119. done
  120.  
  121. # Handle input
  122. while read -e line; do
  123. _input "$line"
  124. done
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement