-
Notifications
You must be signed in to change notification settings - Fork 10
/
ircmsg.sh
executable file
·261 lines (238 loc) · 7.24 KB
/
ircmsg.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
#!/bin/bash
# A simple irc client based pure bash
# - used to send irc msg to notify some event
# - or your can use -I option to talk with others
# Author: yin-jianhong@163.com
##########################################################
# Config
P=$(readlink -f $0)
PROXY_SERVER="host.that.are.running.ircproxy"
PROXY_PORT=6667
ProxySession=
UserPasswd=
NICK="testbot"
#CHANNEL="#fs-fs"
CHANNEL=
qe_assistantx=/usr/local/bin/qe_assistant
configdir=~/.config/ircmsg
recorddir=$configdir/record/
if [[ -f ~/.ircmsg/ircmsg.rc ]]; then
mkdir -p ~/.config
mv ~/.ircmsg ~/.config/ircmsg
fi
[[ -f ~/.config/ircmsg/ircmsg.rc ]] && source ~/.config/ircmsg/ircmsg.rc
##########################################################
# Main
export LANG=C
Interactive=no
msg=
namelist=
P=${0##*/}
#-------------------------------------------------------------------------------
Usage() {
echo "Usage: $P [-hqdiI] [-s serv -p port -S session] {-n <nick> [-C channel [-c <nick>]]} msg"
echo " Options:"
echo " -n <nick> used \$nick as nick name to JOIN channel"
echo " -C <chan> used to join into \$chan"
echo " -c <nick> used to send private msg to \$nick"
echo " -s <proxyserv> proxy server addr, or set 'PROXY_SERVER=' in ~/.config/ircmsg/ircmsg.rc"
echo " -p <proxyserv> proxy server port, or set 'PROXY_PORT=' in ~/.config/ircmsg/ircmsg.rc"
echo " -S <name:irc_serv_addr> proxy session, or set 'ProxySession=' in ~/.config/ircmsg/ircmsg.rc"
echo " -U <user:passwd> proxy login user and passwd, or set 'UserPasswd=' in ~/.config/ircmsg/ircmsg.rc"
echo " -q quit or quit after send message"
echo " -d open debug mode"
echo " -I work in interactive mode as a simple irc client, not finish"
echo " -i run as a deamon robot"
echo
echo "Example:"
echo " $P [-s serv -p port -S session] -n testbot -C #chan msg #channel msg"
echo " $P [-s serv -p port -S session] -n testbot -C #chan -c mike msg #private msg"
echo " $P [-s serv -p port -S session] -n testbot -C #chan -q #quit from #chan"
}
_at=`getopt -o hdqn:C:c:s:p:S:P:U:L:iI --long create-session \
-n 'ircmsg' -- "$@"`
eval set -- "$_at"
while true; do
case "$1" in
-h) Usage; shift 1; exit 0;;
-d) DEBUG=1; shift 1;;
-n) NICK=$2; shift 2;;
-C) CHANNEL=$2; shift 2;;
-c) Chan=$2; shift 2;;
-s) PROXY_SERVER=$2; shift 2;;
-p) PROXY_PORT=$2; shift 2;;
-S|-P) ProxySession=$2; shift 2;;
-U|-L) UserPasswd=$2; shift 2;;
--create-session) CreateSession=1; shift 1;;
-q) QUIT=1; shift 1;;
-I) Interactive=yes; shift 1;;
-i) Interactive=deamon; shift 1;;
--) shift; break;;
esac
done
test -c /dev/tcp || {
if test $(id -u) = 0; then
mknod /dev/tcp c 30 36
else
echo "[Warn] Can't find /dev/tcp, try 'sudo mknod /dev/tcp c 30 36'" >&2
exit 1
fi
}
msg="$*"
[[ -n "${CHANNEL}" && ${CHANNEL:0:1} != '#' ]] && CHANNEL=${CHANNEL/#/#}
Chan=${Chan:-$CHANNEL}
Chan=${Chan:-NULL}
echo "Connecting to ${PROXY_SERVER}:${PROXY_PORT} ..."
exec 100<>/dev/tcp/${PROXY_SERVER}/${PROXY_PORT}
[[ $? != 0 ]] && { exit 1; }
echo "NICK ${NICK}" >&100
echo "USER ${NICK} 8 * : ${NICK}" >&100
test -n "$ProxySession" && {
read session ircserv <<<"${ProxySession/:/ }"
while read line <&100; do
test -n "$DEBUG" && echo $line
[[ $line =~ NOTICE.AUTH.:To.connect ]] && break
done
echo "PRIVMSG root :${UserPasswd/:/ }" >&100
read line <&100 && echo "proxy: $line"
test -n $CreateSession && echo "PRIVMSG root :create ${session} ${ircserv}" >&100
echo "PRIVMSG root :connect ${session}" >&100
}
test -n "${CHANNEL}" && echo "JOIN ${CHANNEL}" >&100
while read line <&100; do
test -n "$DEBUG" && echo $line
read _serv _time _nick _tag _chan nlist <<< $line
[[ $_tag = '=' ]] && {
namelist=$nlist
test -n "$DEBUG" && echo "[namelist] $namelist"
}
[[ $line =~ [^\ ]*\ JOIN\ $CHANNEL ]] && read Head ignore <<<$line
[[ $line =~ :End\ of\ /NAMES\ list. ]] && break
[[ $line =~ .*No\ such\ channel|JOIN\ :Not\ enough\ parameters ]] && break
done
mkdir -p $recorddir
while read line; do
[ -z "$line" ] && continue
echo "$Head PRIVMSG ${Chan} :$line" | tee -a >&100 $recorddir/${Chan}
done <<<"$msg"
if [[ $Interactive = no ]]; then
if test -n "$QUIT"; then
if test -n "${CHANNEL}"; then
#leaving from chan
echo -e "$Head PART ${Chan} I-am-leaving" >&100
echo -e "$Head /PART ${Chan} leaving" >&100
else
echo "$Head QUIT" >&100
fi
fi
test -z "$ProxySession" && echo "$Head QUIT" >&100
exit $?
elif [[ $Interactive = deamon ]]; then
if [[ -z "$TMUX" ]]; then
echo "have to run $P with -i option in tmux:"
echo " tmux new -s ircmsg0 -d \"$P ${_at[@]}\""
exit 0
fi
bgnick=$NICK
while read line <&100; do
line=${line/$'\r'/}
if [[ "$line" =~ PING ]]; then
echo "$Head ${line/PING/PONG}" >&100
else
chanfile=$Chan
read _head _cmd _chan _msg <<<"$line"
[[ $_cmd = MODE || $_cmd =~ [0-9]+ ]] && {
echo -e "${_head%\!*}! ${_cmd} ${_chan} $_msg" >>$recorddir/$chanfile
continue
}
[[ $_cmd = PRIVMSG ]] && {
peernick=$(awk -F'[:!]' '{print $2}' <<<"${_head}")
chanfile=${_chan}
[[ ${_chan:0:1} = "#" ]] || chanfile=$peernick
}
head=${_head%\!*}!
echo -e "${head}! ${_cmd} ${_chan} $_msg" >>$recorddir/$chanfile
[[ "$_chan" = $bgnick ]] && {
_chan=$peernick
_msg="$bgnick $_msg"
}
if [[ -x "$qe_assistantx" ]]; then
$qe_assistantx "${_msg/#:/} from:${_chan}(${head:1:-1})" color | tee -a $recorddir/$chanfile |
while read line; do [[ -z "$line" ]] && continue; echo "$Head PRIVMSG ${_chan} :$line"; done >&100
fi
fi
done
#fixme not finish
else
if ! grep ^ircmsg0: < <(tmux ls); then
echo "tmux session ircmsg0 is required, please try run bellow cmd line first:"
echo " tmux new -s ircmsg0 -d \"$P ${_at[*]/-I/-i}\""
exit 0
fi
trap "sigproc" SIGINT SIGTERM SIGHUP SIGQUIT
sigproc() {
kill $pid 2>/dev/null
exit
}
help() {
echo "
/quit;
/part;
/nick <new nick name>;
/join <Channel|nick name>;
/names [Channel];
/help"
}
export NCURSES_NO_UTF8_ACS=1
curchan=$configdir/curchan
echo -n $Chan >$curchan
while :; do
chan=$(< $curchan)
touch $recorddir/$chan
dialog --backtitle "irc $NICK($chan); proxy.user(${UserPasswd%:*})" --no-shadow \
--begin 2 0 --title "$NICK($chan)" --tailboxbg $recorddir/$chan 27 120 --and-widget \
--begin 30 0 --title "$NICK($chan)" --inputbox "" 5 120 2>$configdir/msg.txt
retval=$?
msg=$(< $configdir/msg.txt)
[[ $msg =~ ^\ *$ ]] && continue
case "$msg" in
/nick*)
read ignore _nick
[ -n $_nick ] && NICK=$_nick
echo "$Head NICK ${NICK}" >&100
;;
/join\ *)
read ignore _chan <<<"$msg"
Chan=$_chan
[[ ${Chan:0:1} = '#' ]] && echo "JOIN ${Chan}" >&100
echo -n $Chan >$curchan
;;
/names|/names\ *)
read ignore _chan <<<"$msg"
echo "NAMES ${_chan:-$chan}" >&100
;;
/raw\ *)
read ignore rawdata <<<"$msg"
echo "$rawdata" >&100
;;
/msg\ *)
read ignore tonick _msg <<<"$msg"
echo "PRIVMSG $tonick :$_msg" >&100
echo "PRIVMSG $tonick :$_msg" >>$recorddir/$chan
;;
/help)
help >>$recorddir/$chan;;
/exit|/quit)
kill $pid $$; exit 0;;
/part)
echo -e "$Head PART ${chan} I-am-leaving" >&100
echo -e "$Head /PART ${chan} leaving" >&100
kill $pid $$
exit 0
;;
*) echo "${Head} PRIVMSG ${chan} :$msg" >&100
echo "${Head} PRIVMSG ${chan} :$msg" >>$recorddir/$chan
;;
esac
done
fi