Advertisement
Guest User

Untitled

a guest
Feb 10th, 2016
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.90 KB | None | 0 0
  1. #!/bin/bash
  2. # dirc - DracOS IRc Client
  3. # Copyright (c) 2016 - randalltux
  4. ###################################
  5. ##### HOW TO #####
  6. ##### -h: hostname #####
  7. ##### -p: port #####
  8. ##### -n: nick #####
  9. ##### -k: password #####
  10. ##### -c: config #####
  11. ##### -v: print version #####
  12. ###################################
  13.  
  14. # Defaults
  15. [[ -z $DIRC_HOST ]] && DIRC_HOST="irc.dracos-linux.org"
  16. [[ -z $DIRC_PORT ]] && DIRC_PORT=6667
  17. [[ -z $DIRC_NICK ]] && DIRC_NICK="$USER"
  18. [[ -z $DIRC_PASS ]] && DIRC_PASS=""
  19. # Automatically execute these inputs at startup, separated by ;
  20. # e.g: DIRC_SCRIPT=":j #dracos-linux
  21. [[ -z $DIRC_SCRIPT ]] && DIRC_SCRIPT=""
  22. # Red error, green background for private message, cyan for #archlinux,
  23. # white for conversations in and out, and gray for everything else
  24. [[ -z $DIRC_PREFIX ]] && DIRC_PREFIX=(
  25. "\e[31m::^ERROR"
  26. "\e[42m\e[30m::(^<[^@]*@[^#])"
  27. "\e[36m::#dracos-team"
  28. "\e[0m::^<"
  29. "\e[0m::^->"
  30. "\e[1;30m::(.*)"
  31. )
  32.  
  33. # Read config files
  34. [[ -r "$HOME/.DIRCrc" ]] && source "$HOME/.DIRCrc"
  35. _xdgconf="${XDG_CONFIG_HOME:-$HOME/.config}/DIRC/DIRCrc"
  36. [[ -r "$_xdgconf" ]] && source "$_xdgconf"
  37.  
  38. # Don't exit at Ctrl-C
  39. trap "echo" SIGINT
  40.  
  41. # Clean up children at exit
  42. trap "kill 0" EXIT
  43.  
  44. # Send raw message to server
  45. function _send() {
  46. printf "%s\r\n" "$*" >&3
  47. }
  48.  
  49. # Print for user
  50. function _output() {
  51. _prefix=""
  52. for rule in ${DIRC_PREFIX[@]}; do
  53. [[ "$@" =~ ${rule#*::} ]] && _prefix="${rule%%::*}$_prefix"
  54. done
  55.  
  56. printf "$_prefix%s\e[0m\n" "$*"
  57. }
  58.  
  59. # Handle user input
  60. function _input() {
  61. local line="$@"
  62. if [[ "${line:0:1}" != ":" ]]; then
  63. [[ -z $channel ]] && _output "ERROR: No channel to send to" && return
  64.  
  65. _send "PRIVMSG $channel :$line"
  66. _output "-> $channel> $line"
  67. return
  68. fi
  69.  
  70. if [[ ${#line} == 2 || ${line:2:1} == " " ]]; then
  71. _txt="${line:3}"
  72. case ${line:1:1} in
  73. m ) read -r _to _msg <<< "$_txt" && _send "PRIVMSG $_to :$_msg" && _output "-> $_to> $_msg"; return;;
  74. l ) read -r _from _msg <<< "$_txt" && _send "PART $_from :$_msg"; return;;
  75. j ) _send "JOIN $_txt"; [[ -z $channel ]] && channel=$_txt; return;;
  76. s ) channel="$_txt"; return;;
  77. q ) _send "QUIT"; exit 0;;
  78. esac
  79. fi
  80.  
  81. # Not recognized command, send to server
  82. _send "${line:1}"
  83. }
  84.  
  85. # Parse command line
  86. while getopts "h:p:n:k:c:v" flag; do
  87. case $flag in
  88. v) printf "DIRC v. 0.1, by randalltux.\n"; exit;;
  89. h) DIRC_HOST="$OPTARG";;
  90. p) DIRC_PORT="$OPTARG";;
  91. n) DIRC_NICK="$OPTARG";;
  92. k) DIRC_PASS="$OPTARG";;
  93. c) source "$OPTARG";;
  94. ?) printf "Unknown option. Usage: $0 [-h hostname] [-p port] [-n nick] [-k password] [-c configfile] [-v]\n" >&2; exit 1;;
  95. esac
  96. done
  97.  
  98. # Open connection to server
  99. exec 3<>/dev/tcp/$DIRC_HOST/$DIRC_PORT || exit 1
  100.  
  101. # Handle messages from server
  102. # This runs as a separate process, which means that no variables are shared with
  103. # the input process. For better or for worse. Mostly for worse.
  104. {
  105. while read _line; do
  106. [[ ${_line:0:1} == ":" ]] && _source="${_line%% *}" && _line="${_line#* }"
  107. _source="${_source:1}"
  108. _user=${_source%%\!*}
  109. _txt="${_line#*:}"
  110.  
  111. case "${_line%% *}" in
  112. "PING")
  113. _send "PONG" ;;
  114. "PRIVMSG")
  115. _ch="${_line%% :*}"
  116. _ch="${_ch#* }"
  117. _output "<$_user@$_ch> $_txt" ;;
  118. *)
  119. _output "$_source >< $_line" ;;
  120. esac
  121. done
  122. } <&3 &
  123.  
  124. # Introduce myself
  125. [[ $DIRC_PASS ]] && _send "PASS $DIRC_PASS"
  126. _send "NICK $DIRC_NICK"
  127. _send "USER $DIRC_NICK localhost $DIRC_HOST :$DIRC_NICK"
  128.  
  129. function _trim() { echo $1; }
  130.  
  131. # Execute login script
  132. IFS=";" read -ra C <<< "$DIRC_SCRIPT"
  133. for _cmd in "${C[@]}"; do
  134. _input $(_trim "$_cmd")
  135. done
  136.  
  137. # Handle input
  138. while read -e line; do
  139. _input "$line"
  140. done
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement