Skip to content

Instantly share code, notes, and snippets.

@Osse
Created May 10, 2016 18:23
Show Gist options
  • Save Osse/f6ad4501d69a7692cd1255408b56e738 to your computer and use it in GitHub Desktop.
Save Osse/f6ad4501d69a7692cd1255408b56e738 to your computer and use it in GitHub Desktop.
#!/usr/bin/env zsh
setopt extendedglob
die() {
print $1
exit 1
}
hash weechat || die "irssi ain't supported, bitch"
channel=*
network=*
if (( $# == 0 )) || [[ $1 == --help ]]; then
set -- -h
fi
awkargs=()
if [[ -t 1 ]]; then
awkargs=(-vmatchcolor=${(%):-%F{red}%B} -vreset=${(%):-%f%b})
fi
while getopts ':N:n:c:A:B:C:e:djht' opt; do
case $opt in
N)
network=$OPTARG ;;
n)
awkargs+=(-vnick=$OPTARG) ;;
c)
channel=$OPTARG ;;
A)
awkargs+=(-vA=$OPTARG) ;;
B)
awkargs+=(-vB=$OPTARG) ;;
C)
awkargs+=(-vA=$OPTARG -vB=$OPTARG) ;;
t)
awkargs+=(-vcount=1) ;;
e)
awkargs+=(-vpattern=$OPTARG) ;;
d)
awkargs+=(-vstrip_ts=1) ;;
j)
awkargs+=(-vstrip_joins=1) ;;
h)
echo "usage: $0 [ -d ] [ -n nick ] [ -c channel ] [ -N network ] [ -e pattern ] [ -- awk options ]"
echo
echo '-d: strip time stamps'
echo '-j: strip joins/leaves and whatnot'
echo
echo 'All arguments found after -- are passed to awk verbatim'
exit 0
;;
\?)
echo Go fuck yourself >&2
exit 1 ;;
esac
done
shift $((OPTIND - 1))
logs=( ~/.weechat/logs/irc.$~network.(\#|)$~channel.weechatlog(N) )
(( $#logs )) || die 'No IRC logs found'
for log in $logs; do
output=$(awk $awkargs "$@" -f ~/bin/ircgrep.awk $log)
if [[ -n $output ]]; then
printf '%s:\n%s\n' $log:t $output
fi
done
function beginstuff() {
FS = "\t"
OFS = "\t"
match_nicks = length(nick)
match_message = length(pattern)
if (!count) {
store_before = length(B)
store_after = length(A)
}
counter = 0
}
BEGIN {
beginstuff()
}
function shift() {
for (i = 2; i <= NF; i ++)
$(i-1) = $i
NF--
}
function printstuff() {
if (!count) {
if (match_message && matchcolor)
gsub(pattern, matchcolor "&" reset, $3)
if (strip_ts)
shift()
if (store_before) {
afterlines = NR + A
if (seen++ && (last+B+1)<NR)
print "--"
lineno = NR-B
if (lineno<last)
lineno = last+1
for ( ; lineno < NR; lineno++)
if (lineno in lines)
print lines[lineno]
}
print
}
}
function match_line() {
matched = ($2 ~ nick) && ($3 ~ pattern)
if (matched)
counter++
return matched
}
strip_joins && $2 ~ /<?-->?/ {
next
}
FNR == 1 {}
NR <= afterlines {
print
last = NR
next
}
match_line() {
printstuff()
}
store_before {
lines[NR]=$0
}
END {
if (count)
print counter
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment