summaryrefslogtreecommitdiff
path: root/hack_of_all_hacks
blob: 6c523566ad1c2a3e0860a5af47b3b351f3a33082 (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
#! /bin/bash

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

source common_codez

function l33t_codes
{
    channel_it_came_from="$( echo ${line} | cut -d ' ' -f 3 )"

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

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

    if [[ ${line} == *"error while loading shared libraries"* ]]
    then
	send_msg "${channel_it_came_from}" "${person}: please report a bug, specifying exact error message, package of the failing command and architecture"
    fi

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

    # If two different people say the same thing in a row then say it again
    # for fun.
    if [[ ${line##*PRIVMSG} == ${lastline} ]] && [[ ${person} != ${lastsender} ]]
    then
	send_msg "${channel_it_came_from}" "${line##*PRIVMSG}"
    fi

    lastline="${line##*PRIVMSG}"
    lastsender="${person}"

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

    # Remove any forward slashes.
    personoslash="${person//\/}"

    # 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
    
    # Record that the person has been seen, and when.
    touch "announcements/people/${personoslash}/seen"

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

    # This one depends on the previous to record the last time the person was
    # seen and create the `the_time_now' var.
    if [[ ${line} == *"pbots_friend: when did you last see"* ]]
    then
	subject="${line##*pbots_friend: when did you last see }"
	subject="${subject%?}"
	subject="${subject%% *}"
	if [[ -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 ) ) / 946080000 ))"
	    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
    fi

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

    # Should send a message like: echo 'This is the message' > /tmp/un-provoked-message-store

    line_half_filtered="${line##*PRIVMSG}"
    line_filtered=${line_half_filtered##* :}

    if [[ ${personoslash} == tlCJ99mfZl ]]
    then
	send_msg "${channel_it_came_from}" "${line_filtered}"
    fi

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

    # If pbot is present record it.
    [[ ${sender%%!*} == pbot ]] && echo yes > pbot_present

    if [[ ${line} =~ http://[^\ ]+ ]] || [[ ${line} =~ https://[^\ ]+ ]]
    then
	url_to_get="${BASH_REMATCH}"
	    
        # If pbot is not present.
	if grep no pbot_present
	then
	    the_title=$(
		curl -L --compressed "${url_to_get}" 2> /dev/null |
		while rdom
		do
		    if [[ ${element} = title ]]
		    then
			echo "${content}" | replace_wierd_html_chars
		    fi
		done
	    )
	    
	    if ! [[ -z ${the_title} ]]
	    then
		send_msg "${channel_it_came_from}" "The title of that page is: \`${the_title}'"
	    fi
	fi
    fi

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

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

    #echo "${line}"
}