#!/bin/bash
# bipIRCnickmailnotify.sh
# Sends bip IRC proxy log lines by mail
# when a specific string is mentioned by other users.
#
# Uses since - a unix utility similar to tail
# that unlike tail only shows the lines appended since the last execution. 
# http://welz.org.za/projects/since
#
# Bip IRC proxy : http://bip.milkypond.org
#
# Author :
# Jean-Marc Liotier 
#
# Changelog :
# 20110301 - 0.1 - Initial release.
#
# This program is free software: you can redistribute it and/or modify it under
# the terms of the GNU Affero General Public License as published by the Free
# Software Foundation, either version 3 of the License, or (at your option) any
# later version.

# User-configurable variables :
bip_log_path="/var/log/bip/jim/freenode"
bip_log_IRC_channel="#b^2"
watched_string="JiML"
recipient="jm@liotier.org"
sender="jim@ruwenzori.net"
# TODO let those variables be set by command line arguments.

# Bip logs are in the following format :
# /var/log/bip/jim/freenode/2011-02/#b^2.28.log
bip_log_daily_file=$bip_log_path"/"`date +%Y`"-"`date +%m`"/"$bip_log_IRC_channel"."`date +%d`".log"

# The action
# Use 'since -n' for debugging
since $bip_log_daily_file | grep "$nick" | egrep '^[0-9][0-9]-[0-9][0-9]-[0-9][0-9][0-9][0-9] [0-9][0-9]:[0-9][0-9]:[0-9][0-9] < ' | grep "$watched_string" | cut -b 22-| mail -e -s "IRC logs for $watched_string at $bip_log_IRC_channel" $sender