summaryrefslogtreecommitdiff
path: root/process_event
blob: 3e38294c81b8f6f6aa2c07a39965880d0b2ac4d5 (plain)
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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
#! /bin/bash

readonly PROCESS_EVENT_SOURCED=1


# This function reads tags of xml in the same way read normally reads lines.
function rdom
{
    local IFS=\>
    read -r -d \< element content
}

function format_cgit_title
{
    if [[ "${url_to_get}" =~ /git.parabola.nu/([^/]+\.git)/tree/ ]]
    then
        repo="${BASH_REMATCH[1]}"
        title=$(sed 's|\\.* - '${repo}'| - '${repo}'|' <<< ${title})
    fi
}

function tell_fact # (thing , channel)
{
    thing="${1}"
    declare -l lower_case_thing="${thing}"
    channel="${2}"

    if [[ -s "info/${lower_case_thing}" ]]
    then
        while read line
        do
            send_msg "${channel}" "${thing} is: ${line}"
        done < <(cat "info/${lower_case_thing}")

        return 0
    else
        return 1
    fi
}

source common_codez

function remember_fact
{
    if grep "^${is}$" "info/${thing}" &>/dev/null
    then
        send_msg "${channel_it_came_from}" "${personoslash}: I know."
    else
        echo "${is}" >> "info/${thing}"
        send_msg "${channel_it_came_from}" "${personoslash}: Remembered."
    fi
}

function forget_fact
{
    number_of_matching_lines=$(grep -c "^${isnt}" "info/${thing}")

    case "${number_of_matching_lines}" in
        0 )
            send_msg "${channel_it_came_from}" "${personoslash}: I know."
            ;;
        1 )
            grep -v "^${isnt}" "info/${thing}" | sponge "info/${thing}"
            send_msg "${channel_it_came_from}" "${personoslash}: OK, entry removed."
            ;;
        * )
            send_msg "${channel_it_came_from}" "${personoslash}: Ambiguos."
            ;;
    esac
}

function process_event
{
    my_own_name='pbot'

    person="${sender%%!*}"

    # Remove any forward slashes.
    personoslash="${person//\/}"
    declare -l personoslashlower="${personoslash}"

    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}"

    # is this am injected message
    [[ ${personoslash} == ${INJECT_NICK} ]] && is_injected_msg=1 || is_injected_msg=0


    ######################
    # Echo injected data #
    ######################

    # Should send a message like: echo 'This is the message' > /tmp/un-provoked-message-store
    line_filtered=${line##*PRIVMSG +([![:space:]]) :}

    [[ "${line_filtered}" != 'Parabola Community Forum' ]] && \
    (( ${is_injected_msg} )) && send_msg "${channel_it_came_from}" "${line_filtered}"
    # NOTE: 'is_injected_msg' is the IPC mechanism
    #         used by ./pbot-say and the ./labs_change_detector script
    #         see ./labs_change_detector, ./pbot-say , ./transport_plus_ipc.sh.inc
    #       the 'send_msg' command above was originally mutually exclusive
    #         with the remaining entirety of this function
    #         and it was the only place where the 'injected_data' var was set
    #         ('injected_data' is now 'is_injected_msg')
    #         yet, that is tested for below in the 'Page title getter' section
    #         so, presumably this 'injected_data' was intended to fall through?
    #         or perhaps not, because the "Shared libraries error" matcher
    #         does not filter out these messages, which it should
    #       supressing furthur handling of such messages for now
    (( ${is_injected_msg} )) && return


    ###############################################################
    # This is a message from a user. Make preperations to process #
    ###############################################################

    the_time_now=$(date +%s)

    # Make this person a folder if they don't already have one.
    if ! [[ -d "announcements/people/${personoslashlower}" ]]
    then
        mkdir -p "announcements/people/${personoslashlower}"
    fi

    # Record that the person has been seen, and when.
    touch "announcements/people/${personoslashlower}/seen"

    shopt -s extglob

    # We want to get only the message part of the line
    sentence="${line#* }"
    sentence="${sentence#* }"
    sentence="${sentence#* :}"


    ##########################
    # Shared libraries error #
    ##########################

    # If someone complains about error while loading shared libraries error
    # then recomend them to the bug tracker.
    if [[ ${sentence} == *"error while loading shared libraries"* ]] && (( ! $is_injected_msg ))
    then
        # Make sure they have not already been recommended to the bug
        # tracker less than one day ago.
        sharlibsrecfile="announcements/people/${personoslashlower}/shared_libs"

        # Have we recommended them to the bug tracker before?
        if [[ -f ${sharlibsrecfile} ]]
        then
            last_time=$( stat -c %Y ${sharlibsrecfile} )

            (( last_time > the_time_now - 86400 )) && rec_recent=1 || rec_recent=0
        else
            rec_recent=0
        fi

        if ! (( rec_recent ))
        then
            send_msg "${channel_it_came_from}" "${person}: please report a bug, specifying the exact error message, the name of the program, and your architecture: https://labs.parabola.nu/projects/issue-tracker/issues?set_filter=1&tracker_id=1"
            touch "${sharlibsrecfile}"
        fi
    fi


    ##########
    # Repeat #
    ##########

    [[ ${sentence} != ${i_repeated} ]] && say_again=yesyes

    # If two different people say the same thing in a row then say it again for fun;
    # unless it was directed at me.
    if [[ ${sentence} == ${lastline} ]] && [[ ${person} != ${lastsender} ]] && \
       [[ ${say_again} != nono ]] && [[ ${sentence} != "${my_own_name}: "* ]]
    then
        send_msg "${channel_it_came_from}" "${sentence}"
        i_repeated="${sentence}"
        say_again=nono
    fi

    lastline="${sentence}"
    lastsender="${person}"


    #################
    # Announcements #
    #################

    # If someone has sent this person a message then echo it to them.
    if [[ -f "announcements/people/${personoslashlower}/messages" ]]
    then
        uniq "announcements/people/${personoslashlower}/messages" |
        while read line
        do
            # The first field is the time, in *nix seconds, that
            # the message was sent. The second is the name of the
            # sender. And the rest is the message.
            intermediate="${line#* }"
            sender_u="${intermediate%% *}"
            message_u="${intermediate#* }"
            time_sent="${line%% *}"

            seconds_ago_seen="$(( the_time_now - time_sent ))"
            minutes_ago_seen="$(( ( the_time_now - time_sent ) / 60 ))"
            hours_ago_seen="$(( ( the_time_now - time_sent ) / 3600 ))"
            days_ago_seen="$(( ( the_time_now - time_sent ) / 86400 ))"
            months_ago_seen="$(( ( the_time_now - time_sent ) / 2592000 ))"
            years_ago_seen="$(( ( the_time_now - time_sent ) / 31104000 ))"
            if (( seconds_ago_seen < 120 ))
            then
                units="${seconds_ago_seen} seconds"
            elif (( minutes_ago_seen < 120 ))
            then
                units="${minutes_ago_seen} minutes"
            elif (( hours_ago_seen < 48 ))
            then
                units="${hours_ago_seen} hours"
            elif (( days_ago_seen < 60 ))
            then
                units="${days_ago_seen} days"
            elif (( months_ago_seen < 24 ))
            then
                units="${months_ago_seen} months"
            else
                units="${years_ago_seen} years"
            fi

                send_msg "${channel_it_came_from}" "${personoslash}: ${sender_u} told me to tell you, (${units} ago): ${message_u}"
        done
        rm "announcements/people/${personoslashlower}/messages"
        rm "announcements/people/${personoslashlower}/you_have_mail" 2> /dev/null || true
    fi


    #####################
    # Page title getter #
    #####################

    # We don't want to get the page title if it's injected data
    # nor if it probably came from a spambot
    if [[ ${line} =~ http://[^\ ]+ ]] || [[ ${line} =~ https://[^\ ]+ ]] && (( ! $is_injected_msg ))
    then
        url_to_get="${BASH_REMATCH}"
        the_title=$(
            curl -L --compressed --max-time 5 "${url_to_get}" 2> /dev/null |
            while rdom
            do
                if [[ ${element} = title ]] || [[ ${element} = TITLE ]]
                then
                    title="${content}"
                    [[ "${url_to_get}" =~ /git.parabola.nu/ ]] && format_cgit_title

                    sed 's/  / /g' <<< "${title}" | replace_wierd_html_chars
                fi
            done
        )

        [[ -n "${the_title}" ]] && send_msg "${channel_it_came_from}" "Page title: '${the_title}'"
    fi


    ######################
    # main chat triggers #
    ######################

    case "${sentence}" in


        #########
        # hello #
        #########

        'Hello' | 'Hello!' | 'Hello?' | 'Hi' | 'Hi!' | \
        'hello' | 'hello!' | 'hello?' | 'hi' | 'hi!' )
            send_msg "${channel_it_came_from}" "Hello ${person}! Is something on your mind? Feel free to comment or ask a question at any time. Be patient if no one responds immediately. That is quite normal on the IRC. Many people will read your message when they can, and perhaps reply, if you are still in the channel."
            ;;


        ######################
        # you got that right #
        ######################

        *" that right ${my_own_name}"* )
            send_msg "${channel_it_came_from}" "You got that right, ${person}!"
            ;;


        ########
        # Seen #
        ########

        "${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%% *}"
            declare -l subjectlower="${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/${subjectlower}/seen" ]]
            then
                seconds_ago_seen="$(( the_time_now - $( stat -c %Y announcements/people/${subjectlower}/seen ) ))"
                minutes_ago_seen="$(( ( the_time_now - $( stat -c %Y announcements/people/${subjectlower}/seen ) ) / 60 ))"
                hours_ago_seen="$(( ( the_time_now - $( stat -c %Y announcements/people/${subjectlower}/seen ) ) / 3600 ))"
                days_ago_seen="$(( ( the_time_now - $( stat -c %Y announcements/people/${subjectlower}/seen ) ) / 86400 ))"
                months_ago_seen="$(( ( the_time_now - $( stat -c %Y announcements/people/${subjectlower}/seen ) ) / 2592000 ))"
                years_ago_seen="$(( ( the_time_now - $( stat -c %Y announcements/people/${subjectlower}/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
            ;;


        ########
        # tell #
        ########

        "${server_nick_current}: tell "+([![:space:]:])?(:)+([[:space:]])+([![:space:]])* )
            # The line will be something such as:
            # pbot: tell fauno: you suck
            # pbot: tell xylon i have heard that lemons are yummy
            local query="${sentence}"
            local bot_nick=${server_nick_current}
            local receiver=$(expr match "$query" ${bot_nick}': tell \([^\ :]*\):\? ')
            local message=$( expr match "$query" ${bot_nick}': tell [^\ ]* \(.*\)'  )

            if [[ "${receiver}" == "${bot_nick}" ]]
            then
                send_msg "${channel_it_came_from}" "${bot_nick}: ${personoslash} told me to tell you, (0 seconds ago): ${message}"
            else
                declare -l receiver_lower=${receiver//\/}
                local receiver_dir="announcements/people/${receiver_lower}"

                [[ -d "${receiver_dir}" ]] || mkdir -p "${receiver_dir}"
                # The time in *nix seconds is saved there so that
                # pbot can say how long ago the massage was sent
                # when he gives it to it's recipient.
                echo "$(date +%s) ${personoslash} ${message}" >> "${receiver_dir}/messages"

                local response="OK! I will do my best to relay your message to ${receiver}."

                send_msg "${channel_it_came_from}" "${personoslash}: ${response}"

                if ! [[ -f "${receiver_dir}/seen" ]]
                then
                    send_msg "${channel_it_came_from}" "${personoslash}: WARNING: I HAVE NEVER SEEN \"${receiver}\" HERE BEFORE. CHECK YOUR SPELLING."
                fi
            fi
            ;;


        ############
        # factoids #
        ############

        # list factoids
        ',' | ',list' | ',factoids' | "${my_own_name}: list factoids" )
            local factoids=( $(ls info/) )
            local n_factoids=${#factoids[*]}
            local begin_msg="ask me about:"
            local end_msg="e.g. ${server_nick_current}: what is ${factoids[0]} (or ,${factoids[0]})"

            send_msg "${personoslash}" "${begin_msg}"
            for (( factoid_n = 0 ; factoid_n < n_factoids ; factoid_n = factoid_n + 10 ))
            do  send_msg "${personoslash}" "    ${factoids[*]:${factoid_n}:10}"
                sleep 1
            done
            send_msg "${personoslash}" "${end_msg}"
            ;;

        # display factoid
        ','+([!/]) )
            thing="${sentence#,}"

            tell_fact "${thing}" "${channel_it_came_from}"
            ;;
        "${my_own_name}: what is "+([![:space:]]) )
            thing="${sentence#${my_own_name}: what is }"

            tell_fact "${thing}" "${channel_it_came_from}"
            ;;

        # set factoid
        "${my_own_name}: "+([!/])" is "+([![:space:]])* )
            declare -l thing="${sentence#${my_own_name}: }"
            thing="${thing%% is *}"
            is="${sentence#* is }"

            remember_fact
            ;;
        "${my_own_name}: "+([!/])" is: "+([![:space:]])* )
            declare -l thing="${sentence#${my_own_name}: }"
            thing="${thing%% is: *}"
            is="${sentence#* is: }"

            remember_fact
            ;;

        # delete factoid
        "${my_own_name}: "+([![:space:]])" isn't "+([![:space:]])* )
            declare -l thing="${sentence#${my_own_name}: }"
            thing="${thing%% isn\'t *}"
            isnt="${sentence#* isn\'t }"

            forget_fact
            ;;
        "${my_own_name}: "+([![:space:]])" isn't: "+([![:space:]])* )
            declare -l thing="${sentence#${my_own_name}: }"
            thing="${thing%% isn\'t: *}"
            isnt="${sentence#* isn\'t: }"

            forget_fact
            ;;


        #############
        # Footnotes #
        #############

        *\[[[:digit:]]\]* )
            declare -a fn

            while read -d $'\0' file
            do
                #if match = grep "${file##*/}[ ]\?\[[[:digit:]]\]" <<< "${sentence}"
                filename="${file##*/}"

                declare -l lowersentence="${sentence}"

                if [[ " ${lowersentence}" =~ [^[:alnum:]]${filename}[\ ]?\[[[:digit:]]\] ]]
                then
                    index="${BASH_REMATCH: -2:1}"

                    fn[${index}]=$(head -1 "${file}")
                fi
            done < <(find info -print0)

            for (( n=0 ; n<50 ; n++ ))
            do
                str="${fn[${n}]}"

                [[ -z "${str}" ]] && continue

                send_msg "${channel_it_came_from}" "[${n}] ${str}"
            done
            ;;


        ########################
        # unrecognised command #
        ########################

        ${my_own_name}:* | ','* )
            local module
            local modules_help=''
            local help_msg_line

            for module in ${config_modules}
            do  case ${module} in
                     'translate' )
                       [[ "${sentence}" =~ ^"${my_own_name}: translate " ]] && return 0

                       modules_help="${modules_help} modules/m_translate.help.inc"
                     ;;
                esac
            done

            while IFS='' read help_msg_line
            do
                help_msg_line=$(sed "s|_BOT_NICK_|${my_own_name}|" <<< ${help_msg_line})
                send_msg "${personoslash}" "${help_msg_line}" ; sleep 1 ;
            done < <(cat process_event.help.inc ${modules_help})
            ;;
    esac


    # TODO: add a birthday announcement feature, cointoss feature, timer feature.


    #########
    # Tests #
    #########

    #echo "${line}"
}