Advertisement
Guest User

TinyircSetup.sh

a guest
Jun 2nd, 2014
390
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/bin/sh
  2.  
  3. # The ramdisk /bin/sh uses "\c" at end of line to suppress newlines.
  4. # Oher shells use -ne and -e.  So define c or the other two.
  5. c=\\c
  6. #ne=-ne; e=-e
  7.  
  8. E=$(printf "\033")
  9. ESC=$(printf "\033")
  10. C_D=$(printf "\004")
  11. C_H=$(printf "\010")
  12. TAB=$(printf "\011")
  13. BS=$(printf "\177")
  14. NL='
  15. '
  16.  
  17. # ANSI colors (darK,Red,Green,Yellow,Blue,Magenta,Cyan,White,Normal)
  18. K="${E}[1;30m"
  19. R="${E}[1;31m"
  20. G="${E}[1;32m"
  21. Y="${E}[1;33m"
  22. B="${E}[1;34m"
  23. M="${E}[1;35m"
  24. C="${E}[1;36m"
  25. W="${E}[1;37m"
  26. N="${E}[0m"
  27. BOLD="${E}[1m"
  28. REV="${E}[7m"
  29. ITAL="${E}[3m"
  30. UNDR="${E}[4m"
  31. BLNK="${E}[5m"
  32.  
  33. WB="${E}[1;44;37m"
  34. BW="${E}[0;47;34m"
  35.  
  36. DRAW="${E}(0"
  37. TEXT="${E}(B"
  38.  
  39. #NICK="zippy"
  40. DSRV="irc.freenode.net"
  41. SERV=$DSRV
  42. PORT=6667
  43.  
  44. printat()
  45. {
  46.   printf "${ESC}[%d;%dH" ${1:-1} ${2:-1}
  47.   [ $# -gt 2 ] && {
  48.     shift 2
  49.     printf "%s" "$*"
  50.   }
  51. }
  52.  
  53. sreplace()
  54. {
  55.   S1=$(expr substr "$STR" 1 $1)
  56.   [ $# -lt 2 ] && S1=$(expr substr $STR 1 $(($1 - 1)))
  57.   S2=$(expr substr "$STR" $(($1 + 1)) ${#STR})
  58.   STR="${S1}${2}${S2}"
  59. }
  60.  
  61. cursor()
  62. {
  63.   case $1 in
  64.     0) printf "${ESC}[?25l" ;;
  65.     *) printf "${ESC}[?12l${ESC}[?25h" ;;
  66.   esac
  67. }
  68.  
  69. bold()
  70. {
  71.   case $1 in
  72.     0) printf "${ESC}[22m" ;;
  73.     *) printf "${ESC}[1m" ;;
  74.   esac
  75. }
  76.  
  77. get() ## get a single keystroke from stdin without waiting
  78. {
  79.   if [ -t 0 ]
  80.   then
  81.     if [ -z "$_STTY" ]
  82.     then
  83.       _STTY=$(stty -g)
  84.       stty -echo -icanon time ${GET_TMOUT:-0} min ${GET_MIN:-0}
  85.     elif [ -n "$GET_TMOUT$GET_MIN" ]
  86.     then
  87.       stty -echo -icanon time ${GET_TMOUT:-0} min ${GET_MIN:-0}
  88.     fi
  89.   fi
  90. #  _KEY=$(dd bs=1 count=1  2>/dev/null; printf '.')
  91.   _KEY=$(dd bs=1 count=1  2>/dev/null; echo '.')
  92.   _KEY=${_KEY%.}
  93.   [ -n "$_KEY" ]
  94. }
  95.  
  96. get_key()
  97. {
  98.   while :
  99.   do
  100.     GET_TMOUT=5 get && break ## Half second timeout on each loop
  101.   done
  102.  
  103.   case $_KEY in
  104.   "${ESC}")
  105.     stty -echo -icanon time 0 min 0
  106.     read _KEY
  107.     if [ -z "$_KEY" ]
  108.     then
  109. #      _KEY='?'
  110.       _KEY="${ESC}"
  111.     fi
  112.   ;;
  113.   esac
  114. }
  115.  
  116.  
  117. # Define TUI Widgets (Type, StrName, FieldLength, AtLoc, EmptyStr)
  118. WT1=s  WS1="NICK";  WL1=39;  WA1="${E}[2;13H";   WE1="${WA1}$(printf "%-39s")${WA1}"
  119. WT2=b  WS2="ID";    WL2=39;  WA2="${E}[3;13H";   WE2="${WA2}$(printf "%-39s")${WA2}"
  120. WT1=s  WS3="IPAS";  WL3=39;  WA3="${E}[4;13H";   WE3="${WA3}$(printf "%-39s")${WA3}"
  121. WT4=s  WS4="SERV";  WL4=39;  WA4="${E}[6;13H";   WE4="${WA4}$(printf "%-39s")${WA4}"
  122. WT5=i  WS5="PORT";  WL5=39;  WA5="${E}[7;13H";   WE5="${WA5}$(printf "%-39s")${WA5}"
  123. WT6=b  WS6="NPAS";  WL6=39;  WA6="${E}[8;13H";   WE6="${WA6}$(printf "%-39s")${WA6}"
  124. WT7=s  WS7="PASS";  WL7=39;  WA7="${E}[9;13H";   WE7="${WA7}$(printf "%-39s")${WA7}"
  125. WT8=s  WS8="CH1";   WL8=24;  WA8="${E}[11;3H";   WE8="${WA8}$(printf "%-24s")${WA8}"
  126. WT9=s  WS9="CH2";   WL9=24;  WA9="${E}[12;3H";   WE9="${WA9}$(printf "%-24s")${WA9}"
  127. WT10=s WS10="CH3";  WL10=24; WA10="${E}[13;3H";  WE10="${WA10}$(printf "%-24s")${WA10}"
  128. WT11=s WS11="CH4";  WL11=24; WA11="${E}[14;3H";  WE11="${WA11}$(printf "%-24s")${WA11}"
  129. WT12=b WS12="NK1";  WL12=3;  WA12="${E}[11;32H"; WE12="${WA12}$(printf "%-3s")${WA12}"
  130. WT13=b WS13="NK2";  WL13=3;  WA13="${E}[12;32H"; WE13="${WA13}$(printf "%-3s")${WA13}"
  131. WT14=b WS14="NK3";  WL14=3;  WA14="${E}[13;32H"; WE14="${WA14}$(printf "%-3s")${WA14}"
  132. WT15=b WS15="NK4";  WL15=3;  WA15="${E}[14;32H"; WE15="${WA15}$(printf "%-3s")${WA15}"
  133. WT16=s WS16="KEY1"; WL16=15; WA16="${E}[11;37H"; WE16="${WA16}$(printf "%-15s")${WA16}"
  134. WT17=s WS17="KEY2"; WL17=15; WA17="${E}[12;37H"; WE17="${WA17}$(printf "%-15s")${WA17}"
  135. WT18=s WS18="KEY3"; WL18=15; WA18="${E}[13;37H"; WE18="${WA18}$(printf "%-15s")${WA18}"
  136. WT19=s WS19="KEY4"; WL19=15; WA19="${E}[14;37H"; WE19="${WA19}$(printf "%-15s")${WA19}"
  137.  
  138. WT20=b WS20="SAVE"; WL20=4;   WA20="${E}[17;8H"; WE20="${WA20}$(printf "%-4s")${WA20}"
  139. WT21=b WS21="SAVEAS"; WL21=6; WA21="${E}[17;25H"; WE21="${WA21}$(printf "%-6s")${WA21}"
  140. WT22=b WS22="EXIT"; WL22=4;   WA22="${E}[17;43H"; WE22="${WA22}$(printf "%-4s")${WA22}"
  141.  
  142. SAVE="Save"; SAVEAS="SaveAs"; EXIT="Exit"
  143. ###################
  144. ShowTUI()
  145. {
  146. printf "${ESC}[2J"
  147.  
  148. # Must use UPPERCASE for words in DRAW mode.  Or add labels later.
  149. # NOTE: see how the cfaj code makes variable sized boxes.
  150. printat 1 1  "$WB$DRAW"
  151. echo "lqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqk"
  152. echo "x NICK                                              x"
  153. echo "x IDENTIFY                                          x"
  154. echo "x PASSWORD                                          x"
  155. echo "tqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqu"
  156. echo "x SERVER                                            x"
  157. echo "x PORT                                              x"
  158. echo "x NEEDPASS                                          x"
  159. echo "x PASSWORD                                          x"
  160. echo "t CHANNELS  qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqu"
  161. echo "x                          KEY                      x"
  162. echo "x                          KEY                      x"
  163. echo "x                          KEY                      x"
  164. echo "x                          KEY                      x"
  165. echo "tqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqu"
  166. echo "x    lqqqqqqk         lqqqqqqqqk        lqqqqqqk    x"
  167. echo "x    x      x         x        x        x      x    x"
  168. echo "x    mqqqqqqj         mqqqqqqqqj        mqqqqqqj    x"
  169. echo "mqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqj"
  170. printf "$TEXT$BW"
  171. #
  172. echo "${WE1}${NICK}"
  173. echo "${WE2}${ID}"
  174. echo "${WE3}${IPAS}"
  175. echo "${WE4}${SERV}"
  176. echo "${WE5}${PORT}"
  177. echo "${WE6}${NPAS}"
  178. echo "${WE7}${PASS}"
  179. echo "${WE8}${CH1}${WE12}${NK1}${WE16}${KEY1}"
  180. echo "${WE9}${CH2}${WE13}${NK2}${WE17}${KEY2}"
  181. echo "${WE10}${CH3}${WE14}${NK3}${WE18}${KEY3}"
  182. echo "${WE11}${CH4}${WE15}${NK4}${WE19}${KEY4}"
  183.  
  184. echo "${WE20}${SAVE}${WE21}${SAVEAS}${WE22}${EXIT}"
  185. #echo $N
  186. }
  187.  
  188. # Boolean/PushButton handler
  189. button()
  190. {
  191.   [ "$WT" != "b" ] && return
  192.   case $J in 20) echo "break" ; return ;;
  193.     21) echo "break" ; return ;;
  194.     22) echo "break" ; return ;;
  195.   esac
  196.   #case $_KEY in ${NL}) return ;; esac
  197.   #case "$STR" in y*|Y*) STR="no " ;; *) STR="yes" ;; esac
  198.   #eval SP=\$WS$J; eval $SP="$STR"
  199.   #echo "printat $y $xo '${STR}'"
  200. }
  201.  
  202. # Open the tinyirc wrapper script and read lines until one starts with tinyirc.
  203. IRC=/usr/local/bin/irc
  204. if [ -r $IRC ] ; then
  205. exec 3<$IRC
  206. # Find SERV NICK PORT
  207. while read line <&3
  208. do
  209.   if [ "${line%% *}" = "tinyirc" ] ; then
  210.     # Tries to remove the first word
  211.     CFG="${line#tinyirc }"
  212.     # Check if we removed anything
  213.     if [ "$CFG" != "$line" ] ; then
  214.       for ARG in $CFG
  215.       do
  216.     case $ARG in
  217.       */*|*:*|*.*) SERV="$ARG" ;;
  218.       -i) ID="yes" ;;
  219.       -*) ARGS="$ARGS $ARG" ;;
  220.       ''|*[!0-9]*) NICK="$ARG" ;;
  221.       *) PORT="$ARG" ;;
  222.     esac
  223.       done
  224.     fi
  225.   fi
  226. done
  227. exec 3</dev/null
  228. fi
  229.  
  230. # Remove irc:// from SERV
  231. case $SERV in
  232.   *://*) SERV=${SERV#*://} ;;
  233. esac
  234. # Split SERV at first /
  235. case $SERV in
  236.   */*) CHAN=${SERV#*/} ;;
  237.   *) CHAN="" ;;  
  238. esac
  239. SERV=${SERV%%/*}
  240. # Split SERV at first :
  241. case $SERV in
  242.   *:*) PORT=${SERV#*:} ; SERV=${SERV%%:*} ;;
  243. esac
  244.  
  245. # Get the channels
  246. J=1; while [ $J -lt 5 ]
  247. do
  248.   CH=${CHAN%%/*}
  249.   case $CHAN in
  250.     */*) CHAN=${CHAN#*/} ;;
  251.     *) CHAN="" ;;
  252.   esac
  253.   case $CH in
  254.     *[\?\&,]*) OPTS=${CH#*[[\?\&,]} ; CH=${CH%%[\?\&,]*} ;;
  255.     *) OPTS="" ;;
  256.   esac
  257.   echo "$J CH=<$CH> OPTS=<$OPTS>"
  258.   if [ -n "$CH" ] ; then
  259.     eval CH$J=$CH ; eval OPTS$J=$OPTS ;
  260.     case $OPTS in
  261.       *key=*) CK=${OPTS#key=} ; CK=${CK#*[[\?\&,]} ; eval KEY$J=$CK ;;
  262.       *needkey*) eval NK$J="yes" ;;
  263.     esac
  264.     J=`expr $J + 1` ;
  265.   elif [ -z "$OPTS" ] ; then
  266.     J=`expr $J + 1` ;
  267.   else # We got some server opts (no channel)
  268.     SOPT="$SOPT,$OPTS"
  269.   fi
  270.   case $OPTS in
  271.     *identify=*) IPAS=${OPTS#identify=} ; IPAS=${IPAS#*[[\?\&,]} ;;
  272.   esac
  273.   case $OPTS in
  274.     *pass=*) PASS=${OPTS#pass=} ; PASS=${PASS#*[[\?\&,]} ;;
  275.     *needpass*) NPAS="yes" ;;
  276.   esac
  277. done
  278.  
  279. # Check if various passwords are needed
  280. if [ -n "$ID" ] ; then ID="yes" ;
  281. elif [ -n "$IPAS" ] ; then ID="yes" ;
  282. else ID="no" ;
  283. fi
  284. if [ -n "$NPAS" ] ; then NPAS="yes" ;
  285. elif [ -n "$PASS" ] ; then NPAS="yes" ;
  286. else NPAS="no" ;
  287. fi
  288.  
  289. if [ -n "$NK1" ] ; then NK1="yes" ;
  290. elif [ -n "$KEY1" ] ; then NK1="yes" ;
  291. else NK1="no" ;
  292. fi
  293. if [ -n "$NK2" ] ; then NK2="yes" ;
  294. elif [ -n "$KEY2" ] ; then NK2="yes" ;
  295. else NK2="no" ;
  296. fi
  297. if [ -n "$NK3" ] ; then NK3="yes" ;
  298. elif [ -n "$KEY4" ] ; then NK3="yes" ;
  299. else NK3="no" ;
  300. fi
  301. if [ -n "$NK4" ] ; then NK4="yes" ;
  302. elif [ -n "$KEY4" ] ; then NK4="yes" ;
  303. else NK4="no" ;
  304. fi
  305.  
  306.  
  307. ################## Now draw and run the TUI.
  308. ShowTUI
  309.  
  310. #cursor 0
  311. J=1
  312. y=2
  313. x=13
  314. xo=13
  315. STR=$NICK
  316. WT=s
  317.  
  318. # Run main dialog box until ESC
  319. while :
  320. do
  321. printat $y $x
  322. get_key
  323. MV=0 # Handle widget traversal (UP,DN,TAB).
  324. case $_KEY in
  325.    " ")printat 1 24 "key=[SP]" ; eval $(button)
  326. ;; ${NL})printat 1 24 "key=[NL]" ; eval $(button)
  327.         eval SP=\$WS$J; eval $SP="$STR"
  328.     x=$xo
  329. ;; "${ESC}") MV=$J #break
  330. ;; '[A')printat 1 24 "key=[UP]" ; MV=$J
  331.         [ "$J" -eq 12 ] && J=8
  332.         [ "$J" -eq 16 ] && J=8
  333.         [ "$J" -gt 19 ] && J=12
  334.         J=$(($J - 1))
  335. ;; '[B')printat 1 24 "key=[DN]" ; MV=$J
  336.         [ "$J" -eq 11 ] && J=20
  337.         [ "$J" -eq 15 ] && J=20
  338.         [ "$J" -le 19 ] && J=$(($J + 1))
  339. ;; "${TAB}")printat 1 24 "key=[TB]" ; MV=$J
  340.     case $J in
  341.           22) J=1 ;;
  342.           [89]|1[0-5]) J=$(($J + 4)) ;;
  343.           1[6-8]) J=$(($J - 7)) ;;
  344.           *) J=$(($J + 1)) ;;
  345.     esac
  346. ;; '[5~')printat 1 24 "key=[V+]"
  347. ;; '[6~')printat 1 24 "key=[V-]"
  348. ;; '[1~')printat 1 24 "key=[HM]"
  349. ;; '[4~')printat 1 24 "key=[EN]"
  350. ;; '[C')printat 1 24 "key=[>>]"
  351.         [ "$WT" != "b" ] && [ $x -lt $((${#STR} + $xo)) ] && x=$(($x + 1))
  352.         [ "$WT" = "b" ] && [ $J -ge 12 ] && [ $J -le 15 ] && {
  353.           MV=$J ; J=$(($J + 4))
  354.     }
  355.         [ "$WT" = "b" ] && [ $J -ge 20 ] && [ $J -le 22 ] && {
  356.           MV=$J ; J=$(($J + 1))
  357.         }
  358. ;; '[D')printat 1 24 "key=[<<]"
  359.         [ "$WT" != "b" ] && [ $x -gt $xo ] && x=$(($x - 1))
  360.         [ "$WT" = "b" ] && [ $J -ge 12 ] && [ $J -le 15 ] && {
  361.           MV=$J ; J=$(($J - 4))
  362.     }
  363.         [ "$WT" = "b" ] && [ $J -gt 20 ] && {
  364.           MV=$J ; J=$(($J - 1))
  365.         }
  366. ;; "${C_D}")printat 1 24 "key=[^D]"
  367. ;; "${C_H}")printat 1 24 "key=[^H]"
  368. ;; "${BS}")printat 1 24 "key=[BS]"
  369.     [ "$WT" != "b" ] && [ $x -gt $xo ] && {
  370.           sreplace $(($x - $xo))
  371.       x=$(($x - 1))
  372.           printat $y $xo "$STR "
  373.         }
  374. ;; [[:cntrl:]]) printat 1 24 "key=[CTL]"
  375. ;; [[:graph:]]) printat 1 24 "key=[${_KEY}]"
  376.         case $_KEY in .|-|_) ;; [[:punct:]]) continue ;; esac
  377.     [ "$WT" = "i" ] && { case $_KEY in [0-9]) ;; *) continue ;; esac }
  378.     [ "$WT" != "b" ] && {
  379.           sreplace $(($x - $xo)) "${_KEY}"
  380.           printat $y $xo $STR
  381.       x=$(($x + 1))
  382.         }
  383.     [ "$WT" = "b" ] && [ $J -le 19 ] && {
  384.           case $_KEY in y|Y) STR="yes" ;; *) STR="no " ;; esac
  385.           printat $y $xo "${STR}"
  386.       eval SP=\$WS$J; eval $SP="$STR"
  387.     }
  388. ;; *)printat 1 24 "key=[HUH]"
  389. ;;
  390. esac
  391.  
  392. # Set x and y if traversing between widgets
  393. if [ $MV -ne 0 ] ; then
  394.   # Clear and restore old widget to STR before moving on.
  395.   eval WE=\$WE$MV
  396.   eval SP=\$WS$MV
  397.   eval STR=\$"$SP"
  398.   echo "${WE}${STR}"
  399.   # Traverse
  400.   if [ "$J" -lt 1 ] ; then J=1
  401.   elif [ "$J" -gt 22 ] ; then J=22
  402.   fi
  403.   if   [ "$J" -lt 4 ] ; then y=$(($J + 1)) ; x=13
  404.   elif [ "$J" -lt 8 ] ; then y=$(($J + 2)) ; x=13
  405.   elif [ "$J" -lt 12 ] ; then y=$(($J + 3)) ; x=3
  406.   elif [ "$J" -lt 16 ] ; then y=$(($J - 1)) ; x=32
  407.   elif [ "$J" -lt 20 ] ; then y=$(($J - 5)) ; x=37
  408.   elif [ "$J" -eq 20 ] ; then y=17; x=8
  409.   elif [ "$J" -eq 21 ] ; then y=17; x=25
  410.   elif [ "$J" -eq 22 ] ; then y=17; x=43
  411.   fi
  412.   xo=$x
  413.   eval SP=\$WS$J
  414.   eval STR=\$"$SP"
  415.   printat $y $xo $STR
  416.   eval WT=\$WT$J
  417. fi
  418. done
  419.  
  420. # Goto top left corner (1,1), set normal text, fix tty, turn on the cursor.
  421. printat 1 1 $N
  422. stty sane
  423. clear
  424. cursor 1
  425.  
  426. ### Construct a tinyirc command line to save.
  427. CMD="tinyirc $NICK $PORT ${SERV:-$DSRV}"
  428.  
  429. OPS=""
  430. OP=\?
  431. case "$ID" in y*|Y*) OPS="${OP}identify=$IPAS"; OP=\& ;; esac
  432. if [ -n "$PASS" ] ; then
  433.   OPS="${OPS}${OP}pass=${PASS}";
  434. else case "$NPAS" in
  435.   y*|Y*) OPS="${OPS}${OP}needpass" ;; esac
  436. fi
  437. [ -n "$OPS" ] && CMD="${CMD}/$OPS"
  438.  
  439. for J in 1 2 3 4
  440. do
  441. eval CH="\$CH$J"; eval NK="\$NK$J"; eval KEY="\$KEY$J"
  442. [ -n "$CH" ] && {
  443.   CMD="${CMD}/$CH"
  444.   if [ -n "$KEY" ] ; then
  445.     CMD="${CMD},key=${KEY}"
  446.   else case "$NK" in
  447.     y*|Y*) CMD="${CMD},needkey" ;; esac
  448.   fi
  449. }
  450. done
  451.  
  452. echo $CMD
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement