Reply
Thread Tools
Posts: 5 | Thanked: 15 times | Joined on Aug 2010
#1
Hello! Here is a python program to set IM presence and status from the command line. MissionControl (Telepathy) GUI allows set online/offline/busy presence and doesn't remember text statuses so one have to write statuses from scratch every time. This cli script simplifies the process; it could be called from shell scripts where a user can store his/her text statuses. It also allows to set any presence - away/extended away (not available)/invisible. Works on all active accounts.
Code:
#!/usr/bin/python

import sys
import dbus

presence_const = int(sys.argv[1])

try:
    presence_text = sys.argv[2]
except IndexError:
    presence_text = ''

bus = dbus.SessionBus()
account_manager = bus.get_object('org.freedesktop.Telepathy.AccountManager',
                         '/org/freedesktop/Telepathy/AccountManager')
accounts = account_manager.Get(
        'org.freedesktop.Telepathy.AccountManager', 'ValidAccounts')

for account in accounts:
    account = bus.get_object('org.freedesktop.Telepathy.AccountManager',
            account)
    enabled = account.Get('org.freedesktop.Telepathy.Account', 'Enabled')
    if not enabled:
        continue
    if str(account) == '/org/freedesktop/Telepathy/Account/ring/tel/ring':
        continue
    account.Set('org.freedesktop.Telepathy.Account', 'RequestedPresence', \
           dbus.Struct((dbus.UInt32(presence_const), '', presence_text),
                signature='uss'),
           dbus_interface='org.freedesktop.DBus.Properties')
Telepathy GUI reflects the presence but doesn't reflects the text status.

Usage:
Code:
# Offline:
SetPresence.py 1
# Online:
SetPresence.py 2 "I am available"
# Away:
SetPresence.py 3 Away
# Extended away:
SetPresence.py 4 "Not available"
# Invisible:
SetPresence.py 5
# Busy (do not disturb)
SetPresence.py 6 "Busy / Do not disturb"

Last edited by phd; 2010-08-06 at 16:09.
 

The Following 7 Users Say Thank You to phd For This Useful Post:
Posts: 5 | Thanked: 15 times | Joined on Aug 2010
#2
A slightly better version - now you can use text statuses instead of numbers:
Code:
#!/usr/bin/python

import sys
import dbus

status_dict = {
    1: "Offline",
    2: "Online",
    3: "Away",
    4: "Extended_Away",
    5: "Invisible",
    6: "Busy"
}

status_dict_rev = {
    "off": 1,
    "offline": 1,
    "on": 2,
    "online": 2,
    "away": 3,
    "extended away": 4,
    "na": 4,
    "n/a": 4,
    "invisible": 5,
    "invis": 5,
    "busy": 6,
    "dnd": 6,
}

try:
    presence = sys.argv[1]
except IndexError:
    sys.exit('Usage: %s off|on|away|na|invis|dnd')

try:
    presence_const = int(presence)
except ValueError:
    try:
        presence_const = status_dict_rev[presence]
    except KeyError:
        sys.exit('Usage: %s off|on|away|na|invis|dnd')

presence = status_dict[presence_const]

try:
    presence_text = sys.argv[2]
except IndexError:
    presence_text = ''

bus = dbus.SessionBus()
account_manager = bus.get_object('org.freedesktop.Telepathy.AccountManager',
                         '/org/freedesktop/Telepathy/AccountManager')
accounts = account_manager.Get(
        'org.freedesktop.Telepathy.AccountManager', 'ValidAccounts')

for account_path in accounts:
    if str(account_path) == '/org/freedesktop/Telepathy/Account/ring/tel/ring':
        continue
    account = bus.get_object('org.freedesktop.Telepathy.AccountManager',
            account_path)
    enabled = account.Get('org.freedesktop.Telepathy.Account', 'Enabled')
    if not enabled:
        continue
    account.Set('org.freedesktop.Telepathy.Account', 'RequestedPresence', \
           dbus.Struct((dbus.UInt32(presence_const), presence, presence_text),
                signature='uss'),
           dbus_interface='org.freedesktop.DBus.Properties')
Usage:
Code:
SetPesence.py online "I'm available"

Last edited by phd; 2010-08-06 at 23:37. Reason: Fixed a bug. Optimization: skip telephone account without testing if it's enabled.
 

The Following 6 Users Say Thank You to phd For This Useful Post:
alienhead's Avatar
Posts: 139 | Thanked: 28 times | Joined on Jun 2010 @ Connecticut, United States
#3
I always wanted this. was looking for it but never found it. thank you thank you thank you.
 
Posts: 47 | Thanked: 3 times | Joined on May 2010 @ Johannesburg, South Africa
#4
Originally Posted by phd View Post
A slightly better version - now you can use text statuses instead of numbers:
Code:
#!/usr/bin/python

import sys
import dbus

status_dict = {
    1: "Offline",
    2: "Online",
    3: "Away",
    4: "Extended_Away",
    5: "Invisible",
    6: "Busy"
}

status_dict_rev = {
    "off": 1,
    "offline": 1,
    "on": 2,
    "online": 2,
    "away": 3,
    "extended away": 4,
    "na": 4,
    "n/a": 4,
    "invisible": 5,
    "invis": 5,
    "busy": 6,
    "dnd": 6,
}

try:
    presence = sys.argv[1]
except IndexError:
    sys.exit('Usage: %s off|on|away|na|invis|dnd')

try:
    presence_const = int(presence)
except ValueError:
    try:
        presence_const = status_dict_rev[presence]
    except KeyError:
        sys.exit('Usage: %s off|on|away|na|invis|dnd')

presence = status_dict[presence_const]

try:
    presence_text = sys.argv[2]
except IndexError:
    presence_text = ''

bus = dbus.SessionBus()
account_manager = bus.get_object('org.freedesktop.Telepathy.AccountManager',
                         '/org/freedesktop/Telepathy/AccountManager')
accounts = account_manager.Get(
        'org.freedesktop.Telepathy.AccountManager', 'ValidAccounts')

for account_path in accounts:
    if str(account_path) == '/org/freedesktop/Telepathy/Account/ring/tel/ring':
        continue
    account = bus.get_object('org.freedesktop.Telepathy.AccountManager',
            account_path)
    enabled = account.Get('org.freedesktop.Telepathy.Account', 'Enabled')
    if not enabled:
        continue
    account.Set('org.freedesktop.Telepathy.Account', 'RequestedPresence', \
           dbus.Struct((dbus.UInt32(presence_const), presence, presence_text),
                signature='uss'),
           dbus_interface='org.freedesktop.DBus.Properties')
Usage:
Code:
SetPesence.py online "I'm available"
I created a new file named SetPresence.py and copied the code into it and saved it.

I can see the file when using the ls -a command from the terminal, but when i try to execute it, I get the response SetPresence.py: not found.

Do I need to install something else to get python scripts to execute?
 
Posts: 5 | Thanked: 15 times | Joined on Aug 2010
#5
Originally Posted by sting04 View Post
when i try to execute it, I get the response SetPresence.py: not found.
Either put SetPresence.py to a directory in PATH or call it with a relative or full path; .e.g. ./SetPresence.py if it's in the current directory or /home/user/bin/SetPresence.py if it's in /home/user/bin/.
 
Posts: 47 | Thanked: 3 times | Joined on May 2010 @ Johannesburg, South Africa
#6
Originally Posted by phd View Post
Either put SetPresence.py to a directory in PATH or call it with a relative or full path; .e.g. ./SetPresence.py if it's in the current directory or /home/user/bin/SetPresence.py if it's in /home/user/bin/.
Hi phd, I've tried it and this is what happens..

Code:
~ $ cd /home/user/MyDocs
~/MyDocs $ ls Set
ls: Set: No such file or directory
~/MyDocs $ ls -a 
.                   .qf                 Signature.jpg
..                  .sounds             WMPInfo.xml
.VolumeIcon.icns    .videos             autorun.inf
._               DCIM                backups
.apt-archive-cache  Downloads           cities
.documents          GPO Update.txt      fuelpad.db
.images             Mac OS              mstardict
.map_tile_cache     Music               qtirreco
.mediabox           Podcasts            tmp
.n900.ico           SetPesence.py
~/MyDocs $ SetPresence.py
-sh: SetPresence.py: not found
~/MyDocs $ /home/user/MyDocs/SetPresence.py
-sh: /home/user/MyDocs/SetPresence.py: not found
~/MyDocs $
 
Posts: 5 | Thanked: 15 times | Joined on Aug 2010
#7
Code:
$ chmod a+x SetPresence.py
$ ls -lF SetPresence.py
 
nicolai's Avatar
Posts: 1,637 | Thanked: 4,424 times | Joined on Apr 2009 @ Germany
#8
You can not set the executable bit on files
in /home/user/MyDocs
chmod a+x SetPresence.py can not work

Move the file to your home folder
mv /home/user/MyDocs/SetPresence.py /home/user/
chmod u+x /home/user/SetPresence.py

regards
Nicolai
 

The Following User Says Thank You to nicolai For This Useful Post:
Posts: 992 | Thanked: 738 times | Joined on Jun 2010 @ Low Earth Orbit
#9
Your directory listing shows "SetPesence.py" it should be "SetPresence.py"

"ls Set" only shows files that are named "Set". To show files with name starting with "Set" use "ls Set*"

Last edited by kureyon; 2010-10-10 at 10:00.
 

The Following User Says Thank You to kureyon For This Useful Post:
Posts: 47 | Thanked: 3 times | Joined on May 2010 @ Johannesburg, South Africa
#10
Thanks for the replies guys, but I'm still not able to get this to work.

I moved the file to /home/user and tried the chmod command, but it returned No such file or directory. So I tried this as root, and it gave no errors - assuming this means that it worked, I tried to execute it again, but I'm back to square one - Not found.

I tried to execute in both user mode and as root but the same problem.

Is there anything else that I could try?
 
Reply


 
Forum Jump


All times are GMT. The time now is 08:37.