IRC:chat-grep [2020-01-03]

About

The support-page is targeting the usage of hexchat with linux mint IRC support channels.

The inbuilt grep command is usefull to query the scrollback buffer of the current channel at hexchat, but is limited to the last recent conversations.

Turning on logging at hexchat does create complete logfiles per channel, which will not be cut off, or shortened, but can reach back for years.

The created logiles can in turn be grep-ped by a user-command and will return more complete results than the regular grep command.

Remarks

grep logfiles

As the logfiles tend to contain 'binary' data after some time (which would interfere with grep) , you should prefer to use strings to read the logile and pipe it to grep, instead of using the logfile as direct input.

file paths

Logfile pathnames - especially for "Linux Mint" - may contain spaces, which have to be escaped or you have to use quotes with file paths.

Hexchat does allow to use the $HOME environment variable with file paths for user commands, which is used here to keep user-commands portable for others.

user-commands

User commands are defined via Menu: Settings -> User Commands. To execute external applications one make use of the supplied exec-command.

Be aware, HexChat does show an unexpected behaviour: You have to press Enter to finish your edits of the command-text, before pressing the OK-button. Otherwise the user-command would be saved with and empty command-text.

The output should be displayed at some external window, to avoid flooding the channel, thus exec must not be used with the -o option.

As the output is supposed to stay for some longer time (at least beyond any timeout hexchat is willing to accept), the output should be wrapped with a terminal-application and executed as background-job.

Otherwise you might face issues, if you are trying to run the user-command for a second time (at the current channel). As hexchat does use a timeout for calls to external programs - the first run will exeed this limit, leaving an orphaned reference behind (missing cleanup at hexchat). Following calls to any external program (using exec) performed from the same channel do fail afterwards and you have to close and rejoin the channel to get rid of it.

using grep directly

In the sample the user-command will be named ogrep - to be symetric with the inbuilt grep command; but you can choose any (valid) name you prefer.

To grep #linuxmint-help -channel logfile: add a new user-command ogrep and define the external command (minimal version) :

exec gnome-terminal -- bash -c "strings '$HOME/.config/hexchat/logs/linux mint/#linuxmint-help.log' |grep '&2' ; read -p '[ENTER] to close'" &

Will open a new terminal-window, grep the logfile, and waits for the user to press [ENTER].

If you prefer to use egrep and a bit more color (doing almost the same):

exec gnome-terminal -- bash -c "strings $HOME/.config/hexchat/logs/linux\ mint/#linuxmint-help.log|egrep '&2';echo -e '\e[1;32m[shift+PgUp] \e[1;33mto scroll back\e[0m\n\e[1;32m[ENTER] \e[1;33mto close\e[0m'; read" &

When using the ogrep from irc, given additional arguments will be inserted at the &2 place-holder, defining the actual search-expression this way.

Be aware: HexChat will delimite arguments with blanks inserted in between, which affects the search-results. To bypass this automatism you might need to quote additional arguments using " or ' to ensure a seach-term would not be splitted into single arguments.

Using egrep, instead of grep, does allow to use regular expressions as arguments, but requires more care when calling the user-command.

using a shell-script

For more complex commands and to have better control over argument handling it's easier to place the actual functionality at a shell-script and call this from the user-command passing over current parameters to it.

exec "$HOME/bin/ot-grep-irc.sh" &2 &

Create the shell-script - placing it at ~/bin. Besides easier editing, this has the advantage, that the shell-script can be tested outside of hexchat from a terminal. You can get the last recent version using:

# create bin - just ignore if it would already exist
mkdir $HOME/bin
cd $HOME/bin
# get the script and make it executable
wget https://www.orcus.de/Entities/linux/ot-grep-irc.sh
chmod +x ot-grep-irc.sh		

The script uses egrep with an -i -option (ignore case) as default, which you could replace with grep in case you do not want to make use of extended regular expressions for searching...

The #linuxmint-help logfile will be used as default input (read on below).

The script does create a temporary file based on the channel-name at /tmp containing the grep-result. Using an editor to view results, does allow use the inbuilt search to explore the details.

#!/bin/bash
# ot-grep-irc.sh - filter irc-logs for keywords (or regular expressions)
# ----------------------------------------------------------------------
# BE AWARE: this will create files at /tmp which might leak personal data
# (files get chmod'ed to 600 and will only contain matching content from
# last search and will be gone on next reboot)
ot_tool="ot-grep-irc.h"
ot_ver="V. 003 - 2020-01-06"

# using egrep and ignoring case as default - switch/modify as needed
ot_grep="egrep -i"
#ot_grep="grep -i"

# the default channel-name to use
ot_channel="#linuxmint-help"
# override with --channel "<channel-name>" from the user-command
# the quotes are important to allow the required usage of # with the
# channel-name


ot_irclogs="$HOME/.config/hexchat/logs/linux mint"

# usefull for testing from the shell: use 0 (off) and 1 (on) - be sure
# to turn if off again if using from irc - or at least switch to some
# temporary channel to test it there (avoid flooding official channels
# with unexpected output)
ot_trace=0

# check argcount
if [ 0 -eq $# ]; then
	if [ 1 -eq $ot_trace ]; then
		echo "$ot_tool $ot_ver"
		echo "usage: ot-grep-irc.sh [--channel "<channel-name>"] keywordlist"
		echo "expected at least 1 argument - but got $#"
	fi
	exit 1
fi

if [ "--channel" == "$1" ]; then
	# requires at least one keyword besides the channel-name
	if [ 2 -ge $# ]; then
		if [ 1 -eq $ot_trace ]; then
			echo "$ot_tool $ot_ver"
			echo "usage: ot-grep-irc.sh [--channel "<channel-name>"] keywordlist"
			echo "expected at least a channel-name + one extra keyword to search for, got $# argument(s):"
			echo "$@"
		fi
		exit 2
	fi
	ot_channel="$2"
	# get rid of the channel args
	shift 2
fi

# input read from
ot_log=${ot_irclogs}/${ot_channel}.log
# extracted data get here-to (removing the leading # from the channel-name)
ot_out=/tmp/${ot_channel//#/}.log
# errors get redirected here-to
ot_err=/tmp/${ot_channel//#/}.err

if [ 1 -eq $ot_trace ]; then echo -e "log: ${ot_log}\nout: ${ot_out}\nerr: ${ot_err}\nkeylist: '$@'"; fi

# using string does help to read log-files containing "binary" data (which will
# be needed sooner or later)
strings "${ot_log}" | ${ot_grep} "$@" >${ot_out} 2>>${ot_err}

# only display any results if the output isn't a empty file (or missing)
if [ -s ${ot_out} ]; then
	# restrict access .. might not be needed at single-user install
	chmod 600 ${ot_out}
	if [ 1 -eq $ot_trace ]; then echo "display result using xed ${ot_out}"; fi
	# running as background job helps to unblock hexchat for another tool
	# (workaround if caller didn't care about at the user-command :-) )
	xed ${ot_out} &
else
	# :) - at hexchat we likely don't want to see any error-messages
	# but still expect some hint about the empty seach-result
	spd-say "nope"
	if [ 1 -eq $ot_trace ]; then echo "empty result"; fi
fi		

With the defaults ot-grep-irc.sh will use the #linuxmint-help logfile.

To make use of other logfiles: define another user-command and use --channel "<channel-name>" as first arguments, where you quote the channel-name including the leading #.

To define a user-command ogrepc for the #linuxmint-chat -channel, use:

exec "$HOME/bin/ot-grep-irc.sh" --channel "#linuxmint-chat" &2 &


© 1998-2023 by ORCUS® GmbH - www.orcus.de (imprint) - we strictly do not track (period) - details: privacy - overview IRC: IRC support

note-1: not tracking does mean too - we are strictly NOT using direct external links within the content = we bother you having to copy&paste offered URLs