-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathhttptitle.bmod
More file actions
87 lines (71 loc) · 2.4 KB
/
httptitle.bmod
File metadata and controls
87 lines (71 loc) · 2.4 KB
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
#!/usr/bin/env bash
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2, June 1991.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/gpl-2.0.html>,
# or in pdf format at <http://www.dhampir.no/stuff/private/gpl-2.0.pdf>
# Copyright 2014 - Øyvind 'bolt' Hvidsten <bolt@dhampir.no>
! ${GHOST:-false} || return 0
if ! isset "httptitle_data"; then
httptitle_data="$(sf_tmpdir)/httptitle_$$.dat"
sf_mkfile -qf "$httptitle_data" >/dev/null
fi
function httptitle_canprint
{
local host="$1" add="$2"
craplimiter "$httptitle_data" "$host" "${HTTPTITLE_DELAY:-10}" "$add"
}
function httptitle_grab { curl -f -m10 "$@"; }
function httptitle_do
{
local host=$1
shift
httptitle_canprint "$host" "false" || return 0
local words
read -r -a words <<<"$@"
(
tmp=""
count=0
trap '[[ -z "$tmp" ]] || rm "$tmp" 2>/dev/null || :' EXIT
sf_mkfile -o tmp
for url in "${words[@]}"; do
if ! [[ "$url" =~ https?://.+ ]]; then
[[ "$url" =~ .+\..+ ]] || continue
local domain="${url%%/*}"
nslookup "$domain" >/dev/null 2>&1 || continue
fi
for ((i=0; i<5; i++)); do
loc=$(httptitle_grab -I --url "$url" | sed -n 's/^Location: //p' | tr -d '\r\n')
[[ -n "$loc" ]] || break
url=$loc
done
httptitle_grab --url "$url" >"$tmp" 2>/dev/null || continue
title="$(perl -l -0777 -ne 'print $1 if /<title.*?>\s*(.*?)\s*<\/title/si' 2>/dev/null <"$tmp")"
[[ -n "$title" ]] || title="$(file -bN "$tmp")"
if ! type recode >/dev/null 2>&1; then
sf_error "recode is not installed! Can not print http title safely."
return
fi
title=$(sf_web2sane -p <<<"$title" | sf_oneline)
domain=$(cut -d '/' -f 3 <<<"$url") # cut http(s)://
[[ "$domain" != *"."*"."* ]] || domain=${domain#www.*} # cut any www. part
httptitle_canprint "$host" "true" || break
echo "PRIVMSG $target :${title:0:128} ($domain)" >"$FIFO"
break
done
) &
}
if
[[ "$cmd" = "PRIVMSG" ]] &&
[[ "$target" = "#"* ]] &&
[[ -n "$data" ]] &&
[[ -z "$reply" ]]
then
httptitle_do "$(src_host)" "$data"
fi