Skip to content

Instantly share code, notes, and snippets.

@Syrup-tan
Last active August 29, 2015 14:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Syrup-tan/81c88961d3e0059eb3d7 to your computer and use it in GitHub Desktop.
Save Syrup-tan/81c88961d3e0059eb3d7 to your computer and use it in GitHub Desktop.
A simple ZNC search script.
#!/bin/bash
## search.{bash,zsh} v0.0.3
## Configuration
LOG_DIR=~znc/'.znc/moddata/log/syrup-tan';
DEFAULT_SERVER='rizon';
## Usage
## Source the file with $ . ./search.bash
## All queries must begin with a from_* function
## e.g. from_buffer '#r/a/dio' | grep_nick 'DigitalHoarder' | grep_pattern '\.(jpg|png|gif)' | count;
## e.g. from_server 'rizon' | grep_nick 'Indago' | count;
## Queries
## from_buffer $BUFFER [$SERVER] – Outputs all logs from $BUFFER
## from_server [$SERVER] – Outputs all logs from $SERVER
## | grep_nick $NICK – Filters messages that weren't sent by $NICK
## | grep_pattern $PATTERN – Filters messages that don't match $PATTERN
## | count – Outputs the number of messages found
## Get all of the logs from a server
from_server() {
## TODO: Find out if this needs to be lowercase
SERVER="$1";
if [ -z "$SERVER" ]; then
SERVER="$DEFAULT_SERVER";
fi;
## Cat all of the log files
## Requires multiglob support
cat "$LOG_DIR"/"$SERVER"/**/*'.log';
}
## Get all of the logs from a buffer
from_buffer() {
BUFFER="$1";
## Strip bad characters & convert to lowercase
BUFFER="$(
echo -n "$BUFFER" |
tr -c '[[:alnum:]]#._' '-' |
tr '[[:upper:]]' '[[:lower:]]';
)";
SERVER="$2";
if [ -z "$SERVER" ]; then
SERVER="$DEFAULT_SERVER";
fi;
## Cat all of the buffer logs
cat "$LOG_DIR"/"$SERVER"/"$BUFFER"/*'.log';
}
## Grep all of the logs from a nick
grep_nick() {
## Get the nick
NICK="$1";
## Grep the nick from stdin
grep '^\[.\{8\}\] <'"$NICK"'>' /dev/stdin;
}
## Pretty grep
grep_pattern() { grep -E "$1" /dev/stdin; }
## Pretty wc -l
count() { wc -l /dev/stdin | cut -d ' ' -f 1; }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment