-
Notifications
You must be signed in to change notification settings - Fork 0
/
iibot-ng
executable file
·178 lines (162 loc) · 5.72 KB
/
iibot-ng
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
#!/bin/sh
# Desc: Wrapper for iibot based on script by c00kiemon5ter
#
# Example of .cfg file:
# ~~~
# net:irc.ssh.cz:#chan1 #chan2
# nickname:testme
# ircdir:$HOME/ii/
# bitly_api_token:api_token
# bitly_group_id:group_id
# ~~~
#
# 2017/Apr/08 @ Zdenek Styblik <[email protected]>
set -e
set -u
monitor()
{
# shellcheck disable=SC3043
local iipid="${1}"
tail -f -n1 --pid="${iipid}" "${ircdir}/${network}/${channel}/out" | \
# NOTE: format of output changed in v1.8
while read -r nixtime nick msg; do
# if msg is by the system ignore it
if [ "$nick" = '-!-' ]; then
continue
fi
# shellcheck disable=SC2034
nixtime_is_unused="${nixtime}"
# strip < and >. if msg is by ourself ignore it
nick=$(printf -- "%s" "${nick}" | sed -e 's@^<@@' | sed -e 's@>$@@')
if [ "${nick}" = "${nickname}" ]; then
continue
fi
# if msg contains a url, transform to url command
if printf -- "%s" "${msg}" | grep -q -E -e 'https?://' ; then
export IICMD_BITLY_GROUP_ID="${bitly_group_id}"
export IICMD_BITLY_API_TOKEN="${bitly_api_token}"
exec "${ircdir}/iicmd.py" \
--nick "${nick}" \
--message "url ${msg#\!}" \
--ircd "${ircdir}" \
--network "${network}" \
--channel "${channel}" \
--self "${nickname}" | fold -w 255 &
continue
fi
# NOTE: commands MUST start with _!_ and that's why nothing might be
# happening.
#
# if msg is a command, invoke iicmd
if printf -- "%s" "${msg}" | grep -q -E -e '^!' ; then
exec "${ircdir}/iicmd.py" \
--nick "${nick}" \
--message "${msg#\!}" \
--ircd "${ircdir}" \
--network "${network}" \
--channel "${channel}" \
--self "${nickname}" | fold -w 255 &
continue
fi
done > "${ircdir}/${network}/${channel}/in"
}
monitor_link()
{
# shellcheck disable=SC3043
local iipid="${1}"
IFS='
'
tail -f -n1 --pid="${iipid}" "${ircdir}/${network}/out" | \
while read -r response; do
if printf -- "%s" "${response}" | grep -q -i -e 'Closing Link' -E -e "${nickname}.*ping timeout"; then
printf "Killing bot.\n" 1>&2
kill "${iipid}"
break
fi
done
}
remove_lock()
{
if [ -n "${pids}" ]; then
printf -- "%s" "${pids}" | xargs kill || true
fi
rmdir "${LOCK_DIR}"
}
IRC_CONFIG=${1:?Supply name of IRC config to use.}
IRC_CONFIG_NAME=$(basename -- "${IRC_CONFIG}")
LOCK_DIR="/tmp/${IRC_CONFIG_NAME}.lock"
if [ ! -r "${IRC_CONFIG}" ]; then
printf "Config file '%s' doesn't exist or is not readable.\n" \
"${IRC_CONFIG}" 1>&2
exit 2
fi
ircdir=$(grep -e '^ircdir:' "${IRC_CONFIG}" | cut -d ':' -f 2- | head -n 1)
ircdir=${ircdir:-${HOME}/tmp/ii/ii/}
nickname=$(grep -e '^nickname:' "${IRC_CONFIG}" | awk -F':' '{ print $2 }' |\
head -n 1)
nickname=${nickname:-"testme"}
net_conf=$(grep -e '^net:' "${IRC_CONFIG}" | sort | uniq | head -n1)
network=$(printf -- "%s" "${net_conf}" | awk -F: '{ print $2 }')
bitly_api_token=$(grep -e '^bitly_api_token:' "${IRC_CONFIG}" | sort | uniq | head -n1)
bitly_group_id=$(grep -e '^bitly_group_id:' "${IRC_CONFIG}" | sort | uniq | head -n1)
if [ -z "${net_conf}" ] || [ -z "${network}" ]; then
printf "No network configuration has been found in '%s'.\n" \
"${IRC_CONFIG}" 1>&2
exit 1
fi
mkdir -p "${LOCK_DIR}" || \
( printf "Failed to create lock for '%s'.\n" "${IRC_CONFIG}" 1>&2; exit 1; )
trap remove_lock INT QUIT TERM EXIT
# some privacy please, thanks
chmod 700 "${ircdir}"
chmod 600 "${ircdir}"/*/ident 1> /dev/null 2>&1 || true
pids=""
# cleanup
rm -f "${ircdir}/${network}/in"
# connect to network - password is set through the env var IIPASS
# NOTE: name of env variable MUST BE given as an CLI arg, eg. -k IIPASS,
# therefore this currently doesn't work.
ii -i "${ircdir}" -n "${nickname}" -s "${network}" \
-f "${nickname}" >> "${ircdir}/${network}.log" 2>&1 &
pid="$!"
# wait for the connection
time_slept=0
while ! test -p "${ircdir}/${network}/in"; do
sleep 1
time_slept=$((time_slept + 1))
if [ ${time_slept} -ge 60 ]; then
# We either didn't get the connection or something went wrong -> crash.
#
# NOTE(zstyblik): we cannot continue without pipe. If we do, then it
# looks like we will end up with files instead of pipes(which is bad).
# This doesn't mean that pipe cannot go away later, but still.
printf "Pipe '%s' still does not exist - giving up.\n" \
"${ircdir}/${network}/in" 1>&2
exit 1
fi
done
monitor_link "$pid" &
pids=$(printf -- "%s %s" "${pids}" $!)
# auth to services
if [ -e "${ircdir}/${network}/ident" ]; then
printf -- "/j nickserv identify %s\n" \
"$(cat "${ircdir}/${network}/ident")" > "${ircdir}/${network}/in"
fi
# clean that up - ident passwd is in there
rm -f "${ircdir}/${network}/nickserv/out"
sleep 3
# join channels
for channel in $(printf -- "%s" "${net_conf}" | awk -F':' '{ print $3 }'); do
rm -f "${ircdir}/${network}/${channel}/in"
if [ ! -e "${ircdir}/${network}/${channel}/out" ]; then
touch "${ircdir}/${network}/${channel}/out"
fi
printf -- "/j %s\n" "${channel}" > "${ircdir}/${network}/in"
monitor "${pid}" &
pids=$(printf -- "%s %s" "${pids}" $!)
done
# if connection is lost, die
wait "${pid}"
remove_lock
trap - INT QUIT TERM EXIT
# EOF