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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
|
#! /bin/bash
# This function reads tags of xml in the same way read normally reads lines.
function rdom
{
local IFS=\>
read -d \< element content
}
function tell_fact # thing channel
{
thing="${1}"
channel="${2}"
if [[ -f "info/${thing}" ]]
then
# For the first entry we will say like: `thing is: '
first="${thing} is: "
uniq "info/${thing}" |
while read line
do
send_msg "${channel}" "${first}${line}"
first=''
done
return
else
return 1
fi
}
source common_codez
function l33t_codes
{
my_own_name='pbot'
person="${sender%%!*}"
# Remove any forward slashes.
personoslash="${person//\/}"
channel_it_came_from="$( echo ${line} | cut -d ' ' -f 3 )"
# If it's a private message
[[ "${channel_it_came_from}" == "${my_own_name}" ]] && channel_it_came_from="personoslash"
######################
# Echo injected data #
######################
# Should send a message like: echo 'This is the message' > /tmp/un-provoked-message-store
injected_data=0
line_half_filtered="${line##*PRIVMSG}"
line_filtered=${line_half_filtered##* :}
if [[ ${personoslash} == tlCJ99mfZl ]]
then
send_msg "${channel_it_came_from}" "${line_filtered}"
injected_data=1
else
##########################
# Shared libraries error #
##########################
the_time_now=$(date +%s)
# If someone complains about error while loading shared libraries error
# then recomend them to the bug tracker.
if [[ ${line} == *"error while loading shared libraries"* ]]
then
yepyep=1
# Make sure they have not already been recommended to the bug
# tracker less than one day ago.
if [[ -f announcements/people/${personoslash}/seen ]] && (( ( $( stat -c %Y announcements/people/${personoslash}/shared_libs ) + 86400 ) > the_time_now ))
then
yepyep=0
fi
if (( yepyep ))
then
send_msg "${channel_it_came_from}" "${person}: please report a bug, specifying the exact error message, package of the failing command and architecture: http://labs.parabola.nu"
touch "announcements/people/${personoslash}/shared_libs"
fi
fi
##########
# Repeat #
##########
stuff_said="$( echo ${line} | cut --complement -d ' ' -f 1,2,3 )"
stuff_said="${stuff_said#:}"
[[ ${stuff_said} != ${i_repeated} ]] && say_again=yesyes
# If two different people say the same thing in a row then say it again
# for fun.
if [[ ${stuff_said} == ${lastline} ]] && [[ ${person} != ${lastsender} ]] && [[ ${say_again} != nono ]] && [[ ${stuff_said} != "${my_own_name}: "* ]] # If two different people say the same thing to me in quick sucession I shouldn't repeat them.
then
send_msg "${channel_it_came_from}" "${stuff_said}"
i_repeated="${stuff_said}"
say_again=nono
fi
lastline="${stuff_said}"
lastsender="${person}"
#################
# Announcements #
#################
# Make this person a folder if they don't already have one.
if ! [[ -d "announcements/people/${personoslash}" ]]
then
mkdir -p "announcements/people/${personoslash}"
touch "announcements/people/${personoslash}/phrases"
cat << EOF > "announcements/people/${personoslash}/settings"
enabled=yes
locked=no
EOF
fi
the_time_now=$(date +%s)
# If this person has announcements enabled and there is at least one
# phrase in their file and their seen log exists.
if grep 'enabled=yes' "announcements/people/${personoslash}/settings" > \
/dev/null && (( $( wc -l "announcements/people/${personoslash}/phrases" | cut -d ' ' -f 1 ) )) && [[ -f announcements/people/${personoslash}/seen ]]
then
# Check if they were last present more than three hours ago.
if (( ( $( stat -c %Y announcements/people/${personoslash}/seen ) +
10800 ) < the_time_now ))
then
send_msg "${channel_it_came_from}" \
"$( shuf "announcements/people/${personoslash}/phrases" | head -1 )"
fi
fi
# If someone has sent this person a message then echo it to them.
if [[ -f "announcements/people/${personoslash}/messages" ]]
then
uniq "announcements/people/${personoslash}/messages" |
while read line
do
send_msg "${channel_it_came_from}" "${personoslash}: ${line}"
done
rm "announcements/people/${personoslash}/messages"
fi
# Record that the person has been seen, and when.
touch "announcements/people/${personoslash}/seen"
#####################
# Page title getter #
#####################
# If pbot is present record it.
#[[ ${sender%%!*} == pbot ]] && echo yes > pbot_present # commented out because pbot-ng now uses the pbot nickname
# We don't want to get the page title if it's injected data.
if [[ ${line} =~ http://[^\ ]+ ]] || [[ ${line} =~ https://[^\ ]+ ]] &&
(( ! injected_data ))
then
url_to_get="${BASH_REMATCH}"
# If pbot is not present.
if true #grep no pbot_present > /dev/null
then
the_title=$(
curl -L --compressed "${url_to_get}" 2> /dev/null |
while rdom
do
if [[ ${element} = title ]]
then
sed 's/ / /g' <<< "${content}" | replace_wierd_html_chars
fi
done
)
if ! [[ -z ${the_title} ]]
then
send_msg "${channel_it_came_from}" "Page title: \`${the_title}'"
fi
fi
fi
########
# Seen #
########
shopt -s extglob
# We want to get only the message part of the line
sentence="${line#* }"
sentence="${sentence#* }"
sentence="${sentence#* :}"
# This one depends on the previous to record the last time the person
# was seen and create the `the_time_now' var.
case "${sentence}" in
"${my_own_name}: when did you last see"* )
subject="${sentence##${my_own_name}: when did you last see }"
subject="${subject##${my_own_name}: when did you last see: }" # If there's an `:', we can still handle it.
subject="${subject%?}"
subject="${subject%% *}"
if [[ "${subject}" == ${my_own_name} ]]
then
send_msg "${channel_it_came_from}" "I last saw ${subject} speak 0 seconds ago."
elif [[ -f announcements/people/${subject}/seen ]]
then
seconds_ago_seen="$(( the_time_now - $( stat -c %Y announcements/people/${subject}/seen ) ))"
minutes_ago_seen="$(( ( the_time_now - $( stat -c %Y announcements/people/${subject}/seen ) ) / 60 ))"
hours_ago_seen="$(( ( the_time_now - $( stat -c %Y announcements/people/${subject}/seen ) ) / 3600 ))"
days_ago_seen="$(( ( the_time_now - $( stat -c %Y announcements/people/${subject}/seen ) ) / 86400 ))"
months_ago_seen="$(( ( the_time_now - $( stat -c %Y announcements/people/${subject}/seen ) ) / 2592000 ))"
years_ago_seen="$(( ( the_time_now - $( stat -c %Y announcements/people/${subject}/seen ) ) / 31104000 ))"
if (( seconds_ago_seen < 120 ))
then
send_msg "${channel_it_came_from}" "I last saw ${subject} speak ${seconds_ago_seen} seconds ago."
elif (( minutes_ago_seen < 120 ))
then
send_msg "${channel_it_came_from}" "I last saw ${subject} speak ${minutes_ago_seen} minutes ago."
elif (( hours_ago_seen < 48 ))
then
send_msg "${channel_it_came_from}" "I last saw ${subject} speak ${hours_ago_seen} hours ago."
elif (( days_ago_seen < 60 ))
then
send_msg "${channel_it_came_from}" "I last saw ${subject} speak ${days_ago_seen} days ago."
elif (( months_ago_seen < 24 ))
then
send_msg "${channel_it_came_from}" "I last saw ${subject} speak ${months_ago_seen} months ago."
else
send_msg "${channel_it_came_from}" "I last saw ${subject} speak ${years_ago_seen} years ago."
fi
else
send_msg "${channel_it_came_from}" "I never saw ${subject} speak."
fi
;;
"${my_own_name}: tell "+([![:space:]])": "+([![:space:]])* )
# The line will be something such as:
# pbot: tell fauno: you suck
process="${sentence##${my_own_name}: tell }"
subject="${process%%:*}"
message="${process#*:}"
message="${message# }"
[[ -d "announcements/people/${subject}" ]] || mkdir -p "announcements/people/${subject}"
echo "${personoslash} told me to tell you: ${message}" >> "announcements/people/${subject}/messages"
response=$(shuf -e 'certainly' 'I will do' 'OK' | head -1)
send_msg "${channel_it_came_from}" "${personoslash}: ${response}"
;;
"${my_own_name}: "+([![:space:]])" is "+([![:space:]])* )
thing="${sentence#${my_own_name}: }"
thing="${thing%% is *}"
is="${sentence#* is }"
echo "${is}" >> "info/${thing}"
send_msg "${channel_it_came_from}" "${personoslash}: Remembered."
;;
"${my_own_name}: "+([![:space:]])" is: "+([![:space:]])* )
thing="${sentence#${my_own_name}: }"
thing="${thing%% is: *}"
is="${sentence#* is: }"
echo "${is}" >> "info/${thing}"
send_msg "${channel_it_came_from}" "${personoslash}: Remembered."
;;
"${my_own_name}: "+([![:space:]])" isn't "+([![:space:]])* )
thing="${sentence#${my_own_name}: }"
thing="${thing%% isn\'t *}"
isnt="${sentence#* isn\'t }"
echo "thing: ${thing}" >> pstd.txt
echo "inst: ${thing}" >> pstd.txt
if grep "${isnt}" "info/${thing}" >/dev/null
then
grep -v "${isnt}" "info/${thing}" | sponge "info/${thing}"
send_msg "${channel_it_came_from}" "${personoslash}: OK, entry removed."
else
send_msg "${channel_it_came_from}" "${personoslash}: I know."
fi
;;
"${my_own_name}: "+([![:space:]])" isn't: "+([![:space:]])* )
thing="${sentence#${my_own_name}: }"
thing="${thing%% isn\'t: *}"
isnt="${sentence#* isn\'t: }"
if grep "${isnt}" "info/${thing}" >/dev/null
then
grep -v "${isnt}" "info/${thing}" | sponge "info/${thing}"
send_msg "${channel_it_came_from}" "${personoslash}: OK, entry removed."
else
send_msg "${channel_it_came_from}" "${personoslash}: I know."
fi
;;
','+([![:space:]]) )
tell_fact "${sentence#,}" "${channel_it_came_from}"
;;
${my_own_name}:* )
while read line
do
send_msg "${channel_it_came_from}" "${line}"
done <<< cat << EOF
Command not recognised. Example commands:
${my_own_name}: tell Jack: hi Jack
${my_own_name}: when did you last see Jill?
${my_own_name}: lemon is yummy
${my_own_name}: lemon isn't yummy
,lemon
,tell jack about foo
EOF
;;
esac
# ' I put this comment here to fix a bug in emacs shell-script-mode that causes the display
# to get messed up
###########################
# answer nicks over query #
###########################
if [[ "${line}" =~ ,tell\ [^\ ]+\ about\ [^\ ]+ ]]
then
gotit="${BASH_REMATCH}" # will be like: `,tell jack about blah'
dudep1="${gotit#,tell }"
dude="${dudep1%% *}"
thing="${gotit##* }"
if [[ -n "${dude}" ]] && [[ -n "${thing}" ]]
then
tell_fact "${thing}" "${dude}" || send_msg "${channel_it_came_from}" "${personoslash}: Error, failed to tell ${dude} about ${thing}"
fi
fi
# TODO: add a birthday announcement feature, cointoss feature, timer
# feature.
#########
# Tests #
#########
#echo "${line}"
fi
}
|