This simple shell script defines two usable functions: pidgin_set_status() and pidgin_restore_old_status(). Other functions are for internal use only and be careful when using them - there is no input or data validation for them. _dbus-send-recv() is universal function that is used also in other scripts. The simplest way to use this code is to put it into .bashrc file.
pidgin_set_status() needs one parameter which is name of one saved status in Pidgin. It will change Pidgin status to the given saved status only if it exists. There is no way to define a new status with this function. Before changing status it also saves previous status id to variable PIDGIN_OLD_STATUS_ID. This variable can be used to restore old status later.
pidgin_restore_old_status() looks for saved status id in PIDGIN_OLD_STATUS_ID and restores previous status if possible. If PIDGIN_OLD_STATUS_ID is missing then it just does not change anything.
Here is an example how to use these two functions:
pidgin_set_status Away xlock pidgin_restore_old_status
Sorry, there are no comments in these functions. Functions are short and simple enough to understand them without any additional documentation.
_dbus-send-recv()
{
local dbusdest dbuspath dbusmsg dbusmember replytype
dbusdest="$1"; shift
dbuspath="$1"; shift
dbusmsg="$1"; shift
dbusmember="$1"; shift
replytype="$1"; shift
if [ "$1" ]; then
dbus-send \
--dest=${dbusdest} \
--print-reply \
--type=method_call \
${dbuspath} \
${dbusmsg}.${dbusmember} \
"$@" \
| grep $replytype \
| sed 's/^ *[^ ]* *//' \
| sed 's/^"//' \
| sed 's/"$//'
else
dbus-send \
--dest=${dbusdest} \
--print-reply \
--type=method_call \
${dbuspath} \
${dbusmsg}.${dbusmember} \
| grep $replytype \
| sed 's/^ *[^ ]* *//' \
| sed 's/^"//' \
| sed 's/"$//'
fi
}
_dbus_pidgin()
{
_dbus-send-recv im.pidgin.purple.PurpleService /im/pidgin/purple/PurpleObject im.pidgin.purple.PurpleInterface "$@"
}
_pidgin_find_saved_status()
{
local findstatus i
findstatus="$1"
for i in $(_dbus_pidgin PurpleSavedstatusesGetAll "int32")
do
if [ "${findstatus}" = $(_dbus_pidgin PurpleSavedstatusGetTitle "string" "int32:${i}") ]; then
echo $i
return
fi
done
}
_pidgin_get_status()
{
_dbus_pidgin PurpleSavedstatusGetCurrent "int32"
}
_pidgin_set_status()
{
_dbus_pidgin PurpleSavedstatusActivate none "int32:$1"
}
pidgin_set_status()
{
if [ -z "$1" ]; then
echo "usage: ${FUNCNAME[0]} <saved status name>" >&2
return
fi
local newstatusid
newstatusid=$(_pidgin_find_saved_status $1)
if [ "$newstatusid" ]; then
PIDGIN_OLD_STATUS_ID=$(_pidgin_get_status)
_pidgin_set_status $newstatusid
else
echo "no such saved status" >&2
fi
}
pidgin_restore_old_status()
{
[ "${PIDGIN_OLD_STATUS_ID}" ] && _pidgin_set_status ${PIDGIN_OLD_STATUS_ID}
unset PIDGIN_OLD_STATUS_ID
}