#!/bin/sh# SERVERS should be a file with a list of servers, each followed by the ii# command line for that server (except the -s because we start with the server)# A -i prefix argument will override the one provided here, useful if you want# a specific server somewhere other than the given hierarchy, but it should be# noted that the fifo path will still be relative to $PREFIX/$serv/. There are# then a list of "fifo command" pairs for each server. If a line ends with a "w"# we wait a few seconds before executing it (default 5). Each server block# should be separated by a new line. The commands for a given server are# performed in serial, and each block runs in parallel. I do not do any error# checking for you, so don't screw up the format. I never claimed this was# pretty, just that it works for me :-)## Adding 'torify' to the beginning of the line will torify the ii instance.## For example, my $SERVERS file (nick and pass changed of course)## <start of $SERVERS># torify irc.oftc.net -n mynick# nickserv/in identify mypass w# in /j #ii# in /j #suckless# in /j #heresy# nickserv/in /l w## localhost -n mynick -p 13245# &bitlbee/in identify mypass# &bitlbee/in blist w# <EOF>## TIP: note the use of 'w' both when identifying to nickserv and leaving# nickserv. The first one is to give ii some time to connect, the second# should wait until after nickserv replies to your identify, so you don't# reopen a window when it does so. the "nickserv/in /l w" is at the end# so I don't have to wait for that command before executing the othersTITLE="pcw"PREFIX="$HOME/irc"SERVERS="$PREFIX/servers"LOG="$PREFIX/log"DELAY=5# seconds to wait when we get a "w"trap"" HUP
if ! tmux -L "$TITLE" list-sessions | grep -q "^$TITLE";then tmux -L "$TITLE" new-session -d -s "$TITLE" sleep .1
fi{echodate
if pgrep -lf "pcw $PREFIX";thenecho"killing previous pcw" pkill -f "pcw $PREFIX"fi} >> "$LOG" 2>&1
# not inside ^that block because it needs to be run in the backgroundtype pcw > /dev/null 2>&1&& pcw "$PREFIX" >> "$LOG" 2>&1&whileread line;doif["${line%% *}"="torify"];thenTORIFY="torify "line="${line##torify }"elseunset TORIFY
fiargs="$line"serv="${line%% *}"unset commands
whileread line;do[ -n "$line"]||break#stop on empty linecommands="$commands;$line"doneecho"starting $serv"echo"find log in $PREFIX/$serv/log"echo"commands $commands"[ -d "$PREFIX/$serv"]|| mkdir "$PREFIX/$serv"{echodate
echo$TORIFY"ii -i $PREFIX -s $args"} >> "$PREFIX/$serv/log" 2>&1
whiletrue;do find "$PREFIX/$serv" -type p -name "in" -exec rm {} + # clean stale fifos# compress old stuff, means fewer lines for cw to rewrite for reflows,# and we still get to keep the old stuff, and it's smallerfor f in $(find "$PREFIX/$serv" -type f -name "out" -print);do gzip -c "$f" >> "${f}_old.gz" rm "$f"doneoIFS="$IFS"IFS=";"for line in $commands;do[ -n "$line"]||continuefifo="${line%% *}"cmd="${line#* }"cmd="${cmd% w}"printf"trying $cmd > $PREFIX/$serv/$fifo."ifecho"$line"| grep -q " w$"; then printf " delay $DELAY seconds." sleep $DELAY fi # wait for the fifo to exist, don't want to make a normal file i=0 while [ $i -lt 10 -a ! -p "$PREFIX/$serv/$fifo" ]; do i=$((i +1)) printf "." sleep 1 done if ! [ -p "$PREFIX/$serv/$fifo" ]; then echo "FAILED: timeout" continue fi echo "$cmd" > "$PREFIX/$serv/$fifo" echo "OK" done >> "$PREFIX/$serv/log" 2>&1 & # background so we can start ii IFS="$oIFS" eval $TORIFY ii -i "$PREFIX" -s $args [ $? -eq 2 ] || break # only restart ii on timeout (not default for ii) done >> "$PREFIX/$serv/log" 2>&1 & # instead of locking the main one...done < "$SERVERS" >> "$LOG" 2>&1tmux -L "$TITLE" attach-session -t "$TITLE"# vim: set ts=4 sw=4 noexpandtab: