#!/usr/bin/ruby # $Id: dbus-pidgin.rb,v 1.3 2008/02/01 19:22:55 jcs Exp $ # # dbus-pidgin.rb # a d-bus script to send notifications of pidgin signons, signoffs, and # new instant messages (only while the pidgin window is not focused) # # Copyright (c) 2008 joshua stein # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions # are met: # # 1. Redistributions of source code must retain the above copyright # notice, this list of conditions and the following disclaimer. # 2. Redistributions in binary form must reproduce the above copyright # notice, this list of conditions and the following disclaimer in the # documentation and/or other materials provided with the distribution. # 3. The name of the author may not be used to endorse or promote products # derived from this software without specific prior written permission. # # THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR # IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES # OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. # IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT # NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF # THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. # require "dbus" PIDGIN_ICON = "/usr/local/share/icons/hicolor/32x32/apps/pidgin.png" def notify(message, title = nil) @notify.Notify( "pidgin", 0, (File.exists?(PIDGIN_ICON) ? PIDGIN_ICON : ""), (title.to_s == "" ? "" : title.to_s), message.to_s, [], {}, -1 ) end def pidgin_alias(id, account = nil) begin if account if newid = @pidgin.PurpleFindBuddy(account, id.to_s) id = newid.first.to_i else return id end end id = @pidgin.PurpleBuddyGetAlias(id) rescue DBus::Error => e puts "pidgin_alias error: #{e}" end return id end session_bus = DBus::SessionBus.instance pidgin_dbus = session_bus.service("im.pidgin.purple.PurpleService") notify_bus = session_bus.service("org.freedesktop.Notifications") begin @pidgin = pidgin_dbus.object("/im/pidgin/purple/PurpleObject") @pidgin.default_iface = "im.pidgin.purple.PurpleInterface" @pidgin.introspect rescue DBus::Error # wait for pidgin to start sleep 5 retry end @notify = notify_bus.object("/org/freedesktop/Notifications") @notify.default_iface = "org.freedesktop.Notifications" @notify.introspect @pidgin.on_signal("BuddySignedOff") { |who| notify "#{pidgin_alias(who)} signed off", pidgin_alias(who) } @pidgin.on_signal("BuddySignedOn") { |who| notify "#{pidgin_alias(who)} signed on", pidgin_alias(who) } @pidgin.on_signal("ReceivedImMsg") { |account, who, msg, conversation| msg.gsub!(/
/i, "\n") msg.gsub!(/<[^>]*>/, "") if @pidgin.PurpleConversationHasFocus(conversation).first == 0 notify msg, pidgin_alias(who, account) end } main = DBus::Main.new main << session_bus notify "#{__FILE__} started", "pidgin" begin main.run rescue DBus::Error => e puts "dbus error: #{e}" end