-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathiii.sh
executable file
·164 lines (140 loc) · 4.56 KB
/
iii.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#!/usr/bin/env sh
# Ivan c00kiemon5ter V Kanakarakis (http://c00kiemon5ter.github.com)
# for noncopyright information see UNLICENSE file http://unlicense.org/ .
#
# frontend to ii/iim for a single channel
# follows the tail of the out file
# and redirects input to the in file
: "${n:=$USER}" # the user's nickname
: "${i:=$HOME/irc}" # root irc dir
: "${s:=irc.freenode.net}" # server
: "${c:=""}" # channel
: "${m:=12}" # max nick lenght
: "${w:=120}" # max characters per mesg - fold after limit
: "${h:=20}" # lines from history
: "${p:=1}" # prefity - colors and special patterns
[ "$1" != '-r' ] && exec rlwrap -a -r -S "${c:-$s}> " -pGREEN "$0" -r
infile="$i/$s/$c/in"
outfile="$i/$s/$c/out"
sepr='|'
# colors
reset="$(tput sgr0)"
dark="$(tput setaf 8)"
highlight="$(tput setaf 3)"
# markup
sb="$(tput bold; tput setaf 9)"
eb="$(tput sgr0; tput sgr0)"
su="$(tput smul; tput setaf 11)"
eu="$(tput rmul; tput sgr0)"
si="$(tput sitm; tput setaf 13)"
ei="$(tput ritm; tput sgr0)"
[ -p "$infile" ] || exit 1
[ -e "$outfile" ] || { touch "$outfile" || exit 1; }
tail -f -n "$h" "$outfile" | while IFS= read -r line
do
unset date time nick mesg ctcp
date="${line%% *}" line="${line#* }"
time="${line%% *}" line="${line#* }"
nick="${line%% *}" line="${line#* }"
# strip '<nick>' to 'nick'
nick="${nick#<}" nick="${nick%>}"
# do not notify of server messages
case "$nick" in -!-) ;; *) tput bel ;; esac
# prettify
if [ "$p" -ne 0 ]
then
unset clrdate clrnick clrsepr clrmesg tmpnick
clrdate="$dark"
case "$line" in *"$n"*) clrdate="$highlight" ;; esac
tmpnick="${nick%[_12]}"
clrnick="$(printf '(%d ^ %d + %d + %d)' "${#tmpnick}" "'${tmpnick}" "'${tmpnick#?}" "'${tmpnick#??}")"
# avoid black(1,8), dark blue(4), bright yellow(11) and white(7, 15)
clrnick="$(($clrnick % 10 + 1))"
case "$clrnick" in 4) clrnick=12 ;; 7) clrnick=13 ;; 8) clrnick=14 ;; esac
clrnick="$(tput setaf "$clrnick")"
clrsepr="$dark"
clrmesg="$reset"
# dark color for special nicks
case "$nick" in -!-) clrnick="$dark" clrmesg="$dark";; esac
fi
# handle CTCP messages
if [ "${line#}" != "${line}" ]
then
line="${line#}"
line="${line%}"
ctcp="${line%% *}"
line="${line#* }"
if [ "$ctcp" != 'ACTION' ]
then line="${clrnick}[CTCP:${ctcp}]${clrmesg} ${line}"
else line="${clrnick}${nick}${clrmesg} ${line}" nick="*"
fi
fi
# fold lines breaking on spaces if message is greater than 'w' chars or does not fit
mw="$(($(tput cols) - ${#date} - ${#time} - $m - 5))"
[ "$mw" -gt "$w" ] && mw="$w"
printf '%s\n' "$line" | fold -s -w "$mw" | while IFS= read -r mesg; \
do
[ "$p" -gt 1 ] && mesg="$(echo "$mesg" | sed \
-e "s,\(^\|[[:space:]][[:punct:]]*\)\([_][[:alnum:][:punct:]]\+[_]\)\([[:punct:][:space:]]\|$\),\1${su}\2${eu}\3,g" \
-e "s,\(^\|[[:space:]][[:punct:]]*\)\([_][[:alnum:][:punct:]]\+[_]\)\([[:punct:][:space:]]\|$\),\1${su}\2${eu}\3,g" \
-e "s,\(^\|[[:space:]][[:punct:]]*\)\([/][[:alnum:][:punct:]]\+[/]\)\([[:punct:][:space:]]\|$\),\1${si}\2${ei}\3,g" \
-e "s,\(^\|[[:space:]][[:punct:]]*\)\([/][[:alnum:][:punct:]]\+[/]\)\([[:punct:][:space:]]\|$\),\1${si}\2${ei}\3,g" \
-e "s,\(^\|[[:space:]][[:punct:]]*\)\([*][[:alnum:][:punct:]]\+[*]\)\([[:punct:][:space:]]\|$\),\1${sb}\2${eb}\3,g" \
-e "s,\(^\|[[:space:]][[:punct:]]*\)\([*][[:alnum:][:punct:]]\+[*]\)\([[:punct:][:space:]]\|$\),\1${sb}\2${eb}\3,g")"
printf '\r%s%s %s %s%*.*s %s%s %s%s%s\n' \
"${clrdate}" "${date}" "${time}" \
"${clrnick}" "${m}" "${m}" "${nick}" \
"${clrsepr}" "${sepr}" \
"${clrmesg}" "${mesg}" "${reset}"
done
done &
trap "stty '$(stty -g)'; kill -TERM 0" EXIT
stty -echonl -echo
bar="--------------------------------------------------------------------------------"
mark() { printf '%s -!- %.*s\n' "$(date +"%F %R")" "$w" "${bar}${bar}" >>"$outfile"; }
while IFS= read -r input; do
case "$input" in
'')
continue
;;
:m)
mark
continue
;;
:x)
mark
break
;;
:q)
break
;;
/wi" "*)
input="/j nickserv info ${input#/wi}"
;;
/me" "*)
input="ACTION${input#/me}"
;;
/names)
input="/names $c"
;;
/op" "*)
input="/j chanserv op $c ${input##* }"
;;
/deop" "*)
input="/j chanserv deop $c ${input##* }"
;;
/bans)
input="/j chanserv akick $c LIST"
;;
/ban" "*)
input="/j chanserv akick $c ADD ${input##* } -- goodbye"
;;
/unban" "*)
input="/j chanserv akick $c DEL ${input##* }"
;;
/t)
input="/topic $c"
;;
esac
[ -n "$input" ] && printf '%s\n' "$input"
done >"$infile"