python, dbus to change multiple accounts status

Claudio claudio.fontana at gliwa.com
Mon Oct 8 06:04:12 EDT 2018


Hello Eion,

thanks a lot!!

On 10/07/2018 08:19 PM, Eion Robb wrote:
> Hi Claudio,
> 
> Your question is a bit more of a devel question than a support one, so cc'ing in the devel list.
> 
> You might find it easier to use the 'savedstatus' API, so that you're not having to fluff around with presences and status. https://developer.pidgin.im/doxygen/2.7.11/html/savedstatuses_8h.html
> 
> I don't know the Python/dbus API well enough, but the C code for what you're trying to do is:
> 
>     PurpleSavedStatus *saved_status;
>     PurpleStatusPrimitive primitive = PURPLE_STATUS_UNAVAILABLE;
>     const gchar *message = "claudio using resource5";
>     saved_status = purple_savedstatus_find_transient_by_type_and_message(primitive, message);
>     if (saved_status == NULL) {
>     saved_status = purple_savedstatus_new(NULL, primitive);
>     purple_savedstatus_set_message(saved_status, message);
>     }
>     purple_savedstatus_activate(saved_status);
> 
> 
> Let us know how it goes :)
> 
> Cheers,
> Eion

It went well, I had to use the "substatus" since on that machine I have to set the status for multiple accounts.

The end result follows.

Thanks a lot! Cheers,

Claudio

#! /usr/bin/python3

import sys
import dbus
from pydbus import SessionBus

def usage():
    text = '''
    ./presence.py busy FIRSTNAME ACCOUNT
    ./presence.py avail FIRSTNAME ACCOUNT

    FIRSTNAME: first name.
    ACCOUNT: the name of the account presence to change

    Example:

    ./presence.py busy claudio resource5
       --> sets resource5 to busy with msg: "claudio using resource5"
    ./presence.py avail claudio resource5
       --> claudio frees up resource5, status set to avail.
    '''
    print(text)
    sys.exit(1)

def process_account(account, arg_status, arg_firstname, arg_account):
    prim = p.PurplePrimitiveGetTypeFromId(arg_status)
    msg = ""
    if (arg_status == "available"):
        msg = arg_firstname + " was last on " + arg_account
    else:
        msg = arg_firstname + " using " + arg_account

    saved_status = p.PurpleSavedstatusGetCurrent()
    status_type = p.PurpleAccountGetStatusTypeWithPrimitive(account, prim)
    p.PurpleSavedstatusSetSubstatus(saved_status, account, status_type, msg)
    p.PurpleSavedstatusActivate(saved_status)

if len(sys.argv) != 4:
    usage()

if (sys.argv[1]) == "busy":
    arg_status = "unavailable"
elif (sys.argv[1]) == "avail":
    arg_status = "available"
else:
    usage()

arg_firstname = sys.argv[2]
arg_account = sys.argv[3]

print("presence: connecting to PurpleService")

bus = SessionBus()
try:
    p = bus.get("im.pidgin.purple.PurpleService",
                "/im/pidgin/purple/PurpleObject")
except:
    sys.exit("presence: failed to connect to PurpleService")

accounts = p.PurpleAccountsGetAll()
name = ""

if len(accounts) == 0:
    sys.exit("presence: no account found.")

for account in accounts:
    name = p.PurpleAccountGetAlias(account)
    #print(name + " is accountid " + str(account))
    if (name == arg_account):
        print("presence: found account " + name + ", setting presence")
        process_account(account, arg_status, arg_firstname, arg_account)
        break

if (name != arg_account):
    print("presence: could not find account " + arg_account)





More information about the Support mailing list