This will interest very few of my readers, make sense to even less, and be worth reading to maybe .35 of you (if you happen to find this post fully worth reading, there's a good chance i only think of you as a tenth (or less) of a person). I suggest you wait for the next post.
I have a tendency to keep my IM client logged in 24/7/365 independant of whether i'm near a computer. Currently i only run my IM client on one machine (my work machine) and remote display it onto my home machine when i'm not at work. I also log everything and have scripts that assist me in viewing the most recent messages (necessary if i kill the client to remote display it - sure i could run it in vnc but don't feel like it). Sometimes i get messages i'd prefer to read before i get back to a computer, so yesterday evening i started on a script that prints the last X minutes worth of messages. Here's the script:
#!/bin/sh
#
#prints out last few minutes worth of logged gaim messages.
# Usage: $0 [minutes] defaults to 15 minutes.
diff=$1
if [ X"$diff" = X ]; then
diff=15
fi
#convert time into something manageable
h=`date +'%H'`
m=`date +'%M'`
time=`expr $h \* 60 + $m - $diff`
mtime=`expr $h \* 60 + $m + $diff \* 2`
#took me only 2 hours to forget why i am using this tmpfile, it looks wrong
tmpfile=`mktemp /tmp/XXXXXX`
echo 0 > $tmpfile
#find all files that have been modified in the requested time interval
find $HOME/.gaim/logs -mmin -$diff -a \! -path '*.system*' | while read file; do
found=0
#run through all the lines in that file, looking for the first
#timestamp within the range we want
while read line; do
if [ $found = 0 -a `echo $line | grep '^('|wc -l` = 1 ]; then
ch=`echo $line | sed -e 's/:.*//' -e 's/^(//' -e 's/[^0-9]//g'`
cm=`echo $line | sed -e 's/:..).*//' -e 's/^(..://' -e 's/[^0-9]//g'`
ctime=`expr $ch \* 60 + $cm`
if [ $ctime -ge $time -a $ctime -le $mtime ]; then
echo
echo $file | awk -F/ '{ print $8" on "$6":"}'
echo $line
found=1
echo 1 > $tmpfile
fi
elif [ $found = 1 ]; then
echo $line
fi
done < $file
done
retval=`cat $tmpfile`
rm $tmpfile
exit $retval
With that utility, i only need to answer the following questions:
1) am i running gaim at work?
2) is one of gaim's parents sshd (and thus it's being remote displayed - would be easier to check if i installed ptree, or if this were Solaris (pargs and the other p* utils are great!))?
3) is my screensaver running at work?
4) is my screensaver running at home?
5) is my monitor at home on?
Based on that information, i can determine whether or not i am actively using a computer that i am running gaim on, and hence whether or not to run the aforementioned script that prints the last few messages. If there are any messages i can then mail these to my phone (this step would be easier if OpenBSD's mail command supported the -e option like Solaris and FreeBSD). Luckily this second part fits nicely into a small cron entry:
*/15 * * * * t=`mktemp /tmp/c.XXXXXX`; if [ `pgrep -u $USER xlock | wc -l` -ge 1 -a \( X`echo $(ps -o ucomm= -p $(ps -o ppid= -p $(ps -o ppid= -p $(pgrep gaim))))|awk '{print $1}'` != "Xsshd" -o X$(ssh inti.blackant.net 'if [ `pgrep -U $USER xlock|wc -l` -ge 1 -o `DISPLAY=localhost:0 xset q | grep "Monitor is On"| wc -l` = 0 -o `pgrep -u $USER xlock | wc -l` -ge 1 ]; then echo yes; else echo no; fi' 2>&1 | tail -1) = Xyes \) ]; then $HOME/bin/lastgaim 15 > $t 2>&1 ; if [ `wc -l < $t` -ge 1 ]; then mail -s 'gaim check' $myaddress < $t; fi fi ; rm $tThis could all be more readable and efficient in perl, and even more efficient if it were done as a plugin to the IM client (the next step, though right after upgrading the client, instead of continually patching), but sometimes it's nice to excercise shell skills. I'm sure there's plenty up there that should be done better. I'm also aware there are a million caveats (plus a bug or three) to the above, such as i get the last 15 minutes of messages, even if the screensaver's only been on for 5. Until i get the plugin written (or find the one that has already been written), i can live with that.
Labels: tech