tazirc view tazirc @ rev 12

Tiny edits
author Paul Issott <paul@slitaz.org>
date Wed Jan 15 16:04:21 2014 +0000 (2014-01-15)
parents 1b813a8d6daf
children
line source
1 #!/bin/sh
2 #
3 # TazIRC - SliTaz IRC client: Keep it small! Maybe use a plugins system.
4 # We use a single temporary text file to send commands to the IRC server
5 # and handle custom user commands such /q to quit.
6 #
7 # Copyright 2014 (C) SliTaz GNU/Linux - BSD License
8 # Author: Christophe Lincoln <pankso@slitaz.org>
9 #
10 . /lib/libtaz.sh
12 # Internationalization
13 TEXTDOMAIN='tazirc'
14 export TEXTDOMAIN
16 # Early function used by --ask
17 logo() {
18 clear
19 colorize 30 "TazIRC - SliTaz IRC Client"
20 }
22 # Handle ask mode now. So we set $host $nick and optionally $chan
23 # Ask only for a nick: tazirc --ask --host=irc.freenode.net --chan=slitaz
24 if [ "$ask" ]; then
25 logo
26 gettext "Auto-join a channel is optional"; echo ""
27 newline
28 if [ "$host" ]; then
29 gettext "IRC server : "; echo "$host"
30 else
31 gettext "IRC server : "; read host
32 fi
33 if [ ! "$nick" ]; then
34 gettext "Nick name : "; read nick
35 fi
36 if [ ! "$chan" ]; then
37 gettext "Channel : "; read chan
38 fi
39 newline
40 if [ ! "$host" ] || [ ! "$nick" ]; then
41 echo "$(colorize 31 'ERROR:') $(gettext 'empty value')"
42 newline && sleep 2 && exit 0
43 fi
44 fi
46 # Cmdline --options= are parsed by libtaz.sh
47 [ "$host" ] || host="$1"
48 [ "$nick" ] || nick="$2"
49 [ "$mode" ] || mode="+i"
50 [ "$port" ] || port="6667"
51 send="/tmp/tazirc/${host}.${nick}.$$.txt"
53 # Help and usage without 2 cmdline args
54 if [ ! "$host" ] || [ ! "$nick" ]; then
55 cat << EOT
57 $(boldify "$(gettext 'Usage:')") $(basename $0) [host] [nick] [--option]
59 $(boldify "$(gettext 'Options:')")
60 --chan= $(gettext "Join specified channel after connection")
61 --mode= $(gettext "Use specified mode. Default: +i")
62 --port= $(gettext "Use specified port. Default: 6667")
63 --ask $(gettext "Will ask for server, nick and channel")
65 EOT
66 exit 0
67 fi
69 # Clean up on exit
70 trap "echo 'Exiting...' && rm -f $send" SIGINT INT TERM
71 trap "kill 0" EXIT
72 mkdir -p $(dirname $send)
74 #
75 # Functions
76 #
78 # Error message: error "Message blabla..."
79 error() {
80 echo "$(colorize 31 'ERROR:') $1"
81 }
83 #
84 # Start: send login commands to connect to server, then handle commands
85 #
87 # Hello World!
88 [ ! "$ask" ] && logo
89 boldify "$(gettext 'Connecting to:') $host $([ $chan ] && echo \#${chan})"
91 # Login commands
92 cat > ${send} << EOT
93 NICK $nick
94 USER $nick $mode * :$0
95 EOT
96 [ "$chan" ] && echo "JOIN #${chan}" >> ${send}
98 # Connect and handle server messages
99 (tail -f ${send} | busybox nc ${host} ${port} | while read MESSAGE
100 do
101 debug "$MESSAGE"
102 case "$MESSAGE" in
103 *" PRIVMSG "*)
104 # Display channel messages
105 user=$(echo "${MESSAGE%!*}" | sed s'/://')
106 text=$(echo "${MESSAGE#* :}")
107 echo "[$(date -u '+%R')] $(colorize 35 "$user"): $text" ;;
108 *" MODE "*)
109 echo "${MESSAGE#* }" ;;
110 PING*)
111 # Responding to ping
112 echo "PONG${MESSAGE#PING}" >> ${send} ;;
113 *)
114 echo "${MESSAGE#* :}" ;;
115 esac
116 done) &
118 # Handle user input commands/messages
119 while read COMMAND
120 do
121 # tazirc> prompt ?
122 # while true: echo -en "$(colorize 33 "tazirc")> "; read COMMAND
123 case "$COMMAND" in
124 "") continue ;;
125 /JOIN*|/join*|/j*)
126 chan="$(echo ${COMMAND} | cut -d '#' -f 2)"
127 boldify "$(gettext 'Joining:') #$chan"
128 echo "JOIN #$chan" >> ${send} ;;
129 /QUIT|/quit|/q)
130 boldify "$(gettext 'Disconnecting from:') $host"
131 echo "QUIT" >> ${send}
132 sleep 1 && rm -f ${send} && break ;;
133 /*)
134 echo "${COMMAND#/}" >> ${send} ;;
135 *)
136 if [ ! "$chan" ]; then
137 error "$(gettext 'No channel to send to')" && continue
138 fi
139 echo "[$(date -u '+%R')] $(colorize 34 "$nick"): ${COMMAND}"
140 echo "PRIVMSG #$chan :${COMMAND}" >> ${send} ;;
141 esac
142 done
144 exit 0