summaryrefslogtreecommitdiff
path: root/bin/ii-answer
blob: 0fa032c36eeaefad914ef5c9681a789be5154afe (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
#!/bin/sh

# answer to stuff on irc

# shellcheck disable=SC2119,SC2120

# shellcheck source=../lib/load-configuration
. "${0%/*}/../lib/load-configuration"

# welcome devs (in #archlinux32 only)
welcome_user_regex='^\S\+ -!- \(abaumann\|deep42thought\|tyzoid\|phrik\)(.* has joined \S\+$'
new_users=$(
  sed -i '
    s/'"${welcome_user_regex}"'/\1/
    T
    w /dev/stdout
    d
  ' "${irc_dir}/#archlinux32/out" | \
    sort -u
)
if [ -n "${new_users}" ]; then
  printf '%s\n' "${new_users}" | \
    sed '
      s/^/Hi /
      s/$/!/
    ' | \
    irc_say '' 'copy'
fi

find "${irc_dir}" \
  -type f \
  -name 'out' \
  -printf '%p\n' | \
  while read -r out_file; do
    channel="${out_file%/out}"
    channel="${channel##*/}"
    if [ -z "${channel%%#*}" ]; then
      prefix='buildmaster: '
      sloppy_salutation='\S\+\s\+\S\+\s.*buildmaster'
    else
      prefix=''
      sloppy_salutation=''
    fi
    # answer "why don't you?"
    regex='^\S\+ <\(\S\+\)> '"${prefix}"'!\?why[- ]don'"'"'\?t[- ]you \(build\|stabilize\|unstage\|keep\|stubbornly_keep\) '
    if grep -q "${regex}" "${out_file}"; then
      sed -i '
        s/'"${regex}"'/\1 \2 /
        T
        w /dev/stdout
        d
      ' "${out_file}" | \
        while read -r asker line; do
          reason=$(
            echo "${line}" | \
              xargs "${base_dir}/bin/why-dont-you" 2>&1 | \
              sed '
                1 s/^/'"${asker}"': /
              '
          )
          if [ "$(echo "${reason}" | wc -l)" -le 5 ]; then
            echo "${reason}"
          else
            echo "${reason}" | \
              head -n5
            printf '... (%s lines total)\n' "$(echo "${reason}" | wc -l)"
          fi | \
            irc_say "${channel}" 'copy'
        done
    fi
    # answer "wtf?"
    regex='^\S\+ <\(\S\+\)> '"${prefix}"'!\?wtf '
    if grep -q "${regex}" "${out_file}"; then
      sed -i '
        s/'"${regex}"'/\1 /
        T
        w /dev/stdout
        d
      ' "${out_file}" | \
        while read -r asker line; do
          reason=$("${base_dir}/bin/wtf" "${line}");
          if [ -z "${reason}" ]; then
            reason="Huh, I don't know that one."
          fi
          reason="${asker}: ${reason}"
          if [ "$(echo "${reason}" | wc -l)" -le 5 ]; then
            echo "${reason}"
          else
            echo "${reason}" | \
              head -n5
            printf '... (%s lines total)\n' "$(echo "${reason}" | wc -l)"
          fi | \
            irc_say "${channel}" 'copy'
        done
    fi
    # answer "wtp?"
    regex='^\S\+ <\(\S\+\)> '"${prefix}"'!\?wtp '
    if grep -q "${regex}" "${out_file}"; then
      sed -i '
        s/'"${regex}"'/\1 /
        T
        w /dev/stdout
        d
      ' "${out_file}" | \
        while read -r asker line; do
          reason=$("${base_dir}/bin/wtp" "${line}");
          if [ -z "${reason}" ]; then
            reason="No, we don't have that."
          fi
          reason="${asker}: ${reason}"
          if [ "$(echo "${reason}" | wc -l)" -le 5 ]; then
            echo "${reason}"
          else
            echo "${reason}" | \
              head -n5
            printf '... (%s lines total)\n' "$(echo "${reason}" | wc -l)"
          fi | \
            irc_say "${channel}" 'copy'
        done
    fi
    # answer "What's up?"
    regex='^\S\+ <\S\+> .*[Ww]hat'"'"'\?s *[Uu]p'
    if grep "${regex}" "${out_file}" | \
      grep -q "${sloppy_salutation}"; then
      sed -i "/${regex}/d" "${out_file}"
      {
        printf 'up? I'"'"'m up for %s, %s' \
          "$(uptime -p | sed 's/^up //')" \
          "$(uptime | sed 's/.*,  //')"
        if [ -s "${work_dir}/build-master-sanity" ]; then
          printf ' ... and I'"'"'m insane :-D'
        fi
        printf '\n'
      } | \
        irc_say "${channel}" 'copy'
    fi
    # answer "Thanks!"
    regex='^\S\+ <\(\S\+\)> \(.* \)\?[Tt]h\(anks\|x\)\([ ,.!].*\)\?$'
    if grep "${regex}" "${out_file}" | \
      grep -q "${sloppy_salutation}"; then
      sed -i '
        /'"${sloppy_salutation}"'/ s/'"${regex}"'/np, \1/
        T
        w /dev/stdout
        d
      ' "${out_file}" | \
        sort -u | \
        irc_say "${channel}" 'copy'
    fi
    if [ -z "${channel%%#*}" ]; then
      # answer "Shut up!"
      regex='^\S\+ <\S\+> \(.* \)\?[Ss][Hh][Uu][Tt] \?[Uu][Pp]'
      if grep "${regex}" "${out_file}" | \
        grep -q "${sloppy_salutation}"; then
        sed -i "/${regex}/d" "${out_file}"
        printf 'Sorry, I will do.\n' | \
          irc_say "${channel}" 'copy'
        printf '%s\n' "$((
          $(date '+%s') + 3600
        ))" > "${work_dir}/irc-shut-up"
      fi
    fi
  done