python, dbus to change multiple accounts status

Claudio claudio.fontana at gliwa.com
Sat Oct 6 09:02:09 EDT 2018


Hello,

I am trying to set up a presence protocol for access to a particular server's resources using finch and DBUS.

On this I do not have much choice, as this is what happens manually right now.

I would like to automate most of the case though, so I switched from pidgin to finch (so no need for SSH forwarding, which causes issues with pidgin -m),

and plan to write some scripts to make it easier to change the current status for the account used on a particular server.

I am very confused about the differences between

status
status_id
status_type
account vs presence
...

I have a vague idea that I tried to implement, but it does not work.
I don't get errors, but when I look at the finch client, it does not show any change in the status.

The finch client on the server has already multiple accounts configured, each corresponding to a particular contended resource.

On ssh login, I would like to run a script to acquire that resource, and if it works, the status for that account should be
"Do Not Disturb", with the firstname of the user appended to the message.

When freeing that resource, the status for the account corresponding to that resource should be set to "Available".

Thank you for any help. Here is what I have:

#! /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 alias 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)

#sys.argv = ["./presence.py", "busy", "claudio", "resource5"]
    
if len(sys.argv) != 4:
    print(len(sys.argv))
    usage()

if (sys.argv[1]) == "busy":
    arg_status = "Do Not Disturb"
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()

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

for account in accounts:
    alias = p.PurpleAccountGetAlias(account)
    print(alias + " is accountid " + str(account))
    if (alias != arg_account):
        print("presence: skipping account " + alias)
        continue

    print("presence: found account " + alias)

    presence = p.PurpleAccountGetPresence(account)
    status = p.PurplePresenceGetActiveStatus(presence)
    status_type = p.PurpleStatusGetType(status)
    status_type_name = p.PurpleStatusTypeGetName(status_type)

    if (status_type_name == arg_status):
        sys.exit("presence: account is already " + arg_status)

    status_type_list = p.PurpleAccountGetStatusTypes(account)
    status_id = "";

    for status_type in status_type_list:
        status_type_name = p.PurpleStatusTypeGetName(status_type)
        print("status_type_name in status_type_list = " + status_type_name)
        if (status_type_name == arg_status):
            status_id = p.PurpleStatusTypeGetId(status_type)

    if (status_id == ""):
        sys.exit("presence: no status type " + arg_status + " found.")

    status = p.PurpleStatusNew(status_type, presence)
    if (arg_status == "Do Not Disturb"):
        p.PurpleStatusSetAttrString(status, "message", arg_firstname +
                                    " on " + alias)
    pres_statuses = p.PurplePresenceGetStatuses(presence)
    for pres_status in pres_statuses:
        print("Presence status old " + str(pres_status))

    p.PurplePresenceAddStatus(presence, status)

    pres_statuses = p.PurplePresenceGetStatuses(presence)
    for pres_status in pres_statuses:
        print("Presence status new " + str(pres_status))

    status_id = p.PurpleStatusGetId(status)
    p.PurplePresenceSetStatusActive(presence, status_id, True)
    p.PurplePresenceSwitchStatus(presence, status_id)
    p.PurpleAccountSetStatusList(account, status_id, True, 0)



More information about the Support mailing list