dbus ruby script to set away/available status in pidgin
1#!/usr/bin/ruby
2#
3# pidgin_status.rb
4# a d-bus script to set your pidgin status and optional status message
5#
6# Copyright (c) 2008, 2010 joshua stein <jcs@jcs.org>
7#
8# Redistribution and use in source and binary forms, with or without
9# modification, are permitted provided that the following conditions
10# are met:
11#
12# 1. Redistributions of source code must retain the above copyright
13# notice, this list of conditions and the following disclaimer.
14# 2. Redistributions in binary form must reproduce the above copyright
15# notice, this list of conditions and the following disclaimer in the
16# documentation and/or other materials provided with the distribution.
17# 3. The name of the author may not be used to endorse or promote products
18# derived from this software without specific prior written permission.
19#
20# THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR
21# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30#
31
32require "dbus"
33
34# magic!
35STATUS_AVAILABLE = 2
36STATUS_AWAY = 5
37
38status = case ARGV[0].to_s
39when "away"
40 STATUS_AWAY
41when "available"
42 STATUS_AVAILABLE
43else
44 STDERR.puts "usage: #{$0} <available|away> <optional message>"
45 exit -1
46end
47
48message = ARGV[1]
49
50sb = DBus::SessionBus.instance
51if !sb.proxy.ListNames.first.include?("im.pidgin.purple.PurpleService")
52 STDERR.puts "pidgin not running, exiting"
53 exit -1
54end
55
56pd = sb.service("im.pidgin.purple.PurpleService")
57pidgin = pd.object("/im/pidgin/purple/PurpleObject")
58pidgin.default_iface = "im.pidgin.purple.PurpleInterface"
59
60pidgin.introspect
61
62if (ss = pidgin.PurpleSavedstatusGetCurrent()).is_a? Array
63 if pidgin.PurpleSavedstatusGetType(ss.first).first == status &&
64 pidgin.PurpleSavedstatusGetMessage(ss.first).first.to_s == message.to_s
65 # already away or available with this message, don't change
66 exit
67 end
68
69 ps = pidgin.PurpleSavedstatusNew("", status).first
70 pidgin.PurpleSavedstatusSetMessage(ps, message.to_s)
71 pidgin.PurpleSavedstatusActivate(ps)
72end