-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweelog
executable file
·96 lines (79 loc) · 2.27 KB
/
weelog
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
#!/usr/bin/env bash
# FIXME: rewrite this garbage in perl
# TODO: allow globs/wildcards in channels
declare from to joinpart_filter nick cmd
declare -a awkcmds
declare -a channels
declare -a nicks
declare -r esc=$'\033'
declare -r c_reset="${esc}[0m"
declare -r c_red="${esc}[31m"
usage() {
LESS=-FEXR less <<-HELP
weelog [options] [string]
-c specify a channel to search, eg: freenode.#freenode
can specify multiple channels with multiple -c flags
-n specify a nick to filter for. can specify multiple nicks with multiple -n flags
-F filter join/part messages and mode changes (ignored when -n is present)
-f define a date to start from. this is passed directly to \`date -d\`
-t define a date to end at. this is passed directly to \`date -d\`
-h show this help
example:
weelog -n DANtheBEASTman -c freenode.#bash -c freenode.#awk -f '3 days ago' -t '1 day ago' 'help'
this would search for all posts by DANtheBEASTman on the channels
#bash and #awk on the freenode network, from 3 days ago til 1 day ago
for all lines matching 'help'
HELP
}
join() {
local delim="$1" output="$2" s
shift 2
for s do
output+="$delim$s"
done
printf '%s' "$output"
}
err() {
printf "${c_red}%s${c_reset}\n" "$*" >&2
}
die() {
[[ -n "$1" ]] && err "$1"
exit 1
}
while getopts "hFt:f:n::c:" opt; do
case "$opt" in
h) usage; exit 0 ;;
F) joinpart_filter=1 ;;
f) from="$OPTARG" ;;
t) to="$OPTARG" ;;
n) nicks+=( "$OPTARG" ) ;;
c) channels+=( "$OPTARG" ) ;;
esac
done
shift "$((OPTIND-1))"
(( ${#channels[@]} > 0 )) || die 'please specify a channel'
if [[ -n "$from" ]]; then
awkcmds+=( "\$1 >= \"$(date -d "$from" +'%F %T')\"" )
fi
if [[ -n "$to" ]]; then
awkcmds+=( "\$1 <= \"$(date -d "$to" +'%F %T')\"" )
fi
if (( ${#nicks[@]} > 0 )); then
for nick in "${nicks[@]}"; do
awkcmds+=( "\$2 ~ /$nick/" )
done
elif [[ -n "$joinpart_filter" ]]; then
awkcmds+=( "\$2 !~ /(--|<--|-->)/" )
fi
if (( $# > 0 )); then
awkcmds+=( "\$0 ~ /$*/" )
fi
cmd=$(join ' && ' "${awkcmds[@]}")
for channel in "${channels[@]}"; do
file="${HOME}/.weechat/logs/irc.${channel}.weechatlog"
if [[ -e "$file" ]]; then
mawk -F '\t' "$cmd { print \"$channel\", \$0 }" "$file"
else
err "can't find a weechatlog file for ${channel}"
fi
done