[Suspend2-users] hibernate script scriptlet for pidgin (atta…

Top Page
Attachments:
Message as email
+ (text/plain)
+ pidgin (text/plain)
Delete this message
Reply to this message
Author: The place to get help!
Date:  
To: suspend2-users
Subject: [Suspend2-users] hibernate script scriptlet for pidgin (attached)
Hi all,
I have modified the gaim scriptlet in hibernate-1.96 to work with pidgin.
It seems to work for me, but please test it and send feedback ;)

Cheers,
Laurento
# -*- sh -*-
# vim:ft=sh:ts=8:sw=4:noet

# $Id: $

purple_remote=$(command -v purple-remote)

AddConfigHandler PidginOptions
AddConfigHelp "LogoutPidgin <boolean>" "Changes all locally running Pidgin's status to offline before suspending, and (optionally) change it back to the original status after resuming."
AddConfigHelp "PidginRestoreStatus <boolean>" "Changes back Pidgin's status to the original status after resuming."
AddConfigHelp "PidginLogoutMessage <string>" "Status message to set when logging out Pidgin."
AddConfigHelp "PidginLoginMessage <string>" "Status message to set when logging in Pidgin."

# Gets the value of the environment variable with the given name of the process with the given PID.
get_env_var_of_process()
{
local pid="$1" envvar="$2"

tr '\0' '\n' </proc/$pid/environ | sed -ne 's/^'"$envvar"'=\(.*\)$/\1/p'
}

LogoutPidgin()
{
[ x"$LOGOUT_PIDGIN" != "x1" ] && return 0

if [ -z "$purple_remote" ] || [ ! -x "$purple_remote" ]; then
    vecho 0 'Cannot log out Pidgin: `purple-remote` not found.'
    return 0
fi

local pid i=0
for pid in `pidof pidgin`; do
    local user dbus_session_bus_address pidgin_status saved_status_id status_type purple_remote_cmd

    user=$(get_env_var_of_process $pid USER)
    dbus_session_bus_address=$(get_env_var_of_process $pid DBUS_SESSION_BUS_ADDRESS)

    vecho 2 "Saving status of $user's Pidgin using D-Bus session bus address $dbus_session_bus_address"
    pidgin_status=$(DBUS_SESSION_BUS_ADDRESS="$dbus_session_bus_address" su "$user" -c "$purple_remote 'getstatus'")

    # using this eval-crap to be POSIX-compliant (arrays are nonstandard)
    eval "PIDGIN_LOGGED_OUT_SESSIONS_USER_$i='$user'"
    eval "PIDGIN_LOGGED_OUT_SESSIONS_DBUS_$i='$dbus_session_bus_address'"
    eval "PIDGIN_LOGGED_OUT_SESSIONS_STATUS_$i='$pidgin_status'"
    i=`expr $i + 1`

    purple_remote_cmd="setstatus?status=offline"
    if [ -n "$PIDGIN_LOGOUT_MESSAGE" ]; then
        purple_remote_cmd="$purple_remote_cmd&message=$PIDGIN_LOGOUT_MESSAGE"
    fi

    vecho 2 "Logging out $user's Pidgin using D-Bus session bus address $dbus_session_bus_address"
    DBUS_SESSION_BUS_ADDRESS="$dbus_session_bus_address" su "$user" -c "$purple_remote '$purple_remote_cmd'"
done

return 0
}

LoginPidgin()
{
[ x"$LOGOUT_PIDGIN" != "x1" ] && return 0

[ x"$PIDGIN_RESTORE_STATUS" != "x1" ] && return 0

if [ -z "$purple_remote" ] || [ ! -x "$purple_remote" ]; then
    vecho 0 'Cannot log on Pidgin: `purple-remote` not found.'
    return 0
fi

local i=0
while :; do
    local user dbus_session_bus_address pidgin_status purple_remote_cmd

    user=`eval "echo \\\$PIDGIN_LOGGED_OUT_SESSIONS_USER_$i"`
    dbus_session_bus_address=`eval "echo \\\$PIDGIN_LOGGED_OUT_SESSIONS_DBUS_$i"`
    pidgin_status=`eval "echo \\\$PIDGIN_LOGGED_OUT_SESSIONS_STATUS_$i"`
    i=`expr $i + 1`

    [ -z "$user" ] && break

    purple_remote_cmd="setstatus?status=$pidgin_status"
    if [ -n "$PIDGIN_LOGIN_MESSAGE" ]; then
        purple_remote_cmd="$purple_remote_cmd&message=$PIDGIN_LOGIN_MESSAGE"
    fi

    vecho 2 "Logging back (to status $pidgin_status) $user's Pidgin using D-Bus session bus address $dbus_session_bus_address"
    DBUS_SESSION_BUS_ADDRESS="$dbus_session_bus_address" su "$user" -c "$purple_remote '$purple_remote_cmd'"
done

return 0
}

PidginOptions()
{
case "$1" in
    logoutpidgin)
     if BoolIsOn "$1" "$2"; then
        LOGOUT_PIDGIN=1
        if [ -z "$PIDGINLOGOUT_HOOKED" ]; then
         AddSuspendHook 19 LogoutPidgin
         AddResumeHook 19 LoginPidgin
         PIDGINLOGOUT_HOOKED=1
        fi
     else
        LOGOUT_PIDGIN=0
     fi
     ;;
    pidginrestorestatus)
     if BoolIsOn "$1" "$2"; then
        PIDGIN_RESTORE_STATUS=1
     else
        PIDGIN_RESTORE_STATUS=0
     fi
     ;;
    pidginlogoutmessage)
     PIDGIN_LOGOUT_MESSAGE="$2"
     ;;
    pidginloginmessage)
     PIDGIN_LOGIN_MESSAGE="$2"
     ;;
    *)
     return 1
     ;;
esac
}