{"id":56,"date":"2007-11-17T14:02:36","date_gmt":"2007-11-17T14:02:36","guid":{"rendered":"http:\/\/blog.danplanet.com\/wordpress\/?p=56"},"modified":"2011-11-29T16:53:35","modified_gmt":"2011-11-29T16:53:35","slug":"automatic-pidgin-status-with-dbus-and-networkmanager","status":"publish","type":"post","link":"https:\/\/www.danplanet.com\/blog\/2007\/11\/17\/automatic-pidgin-status-with-dbus-and-networkmanager\/","title":{"rendered":"Automatic Pidgin status with DBUS and NetworkManager"},"content":{"rendered":"<p>It&#8217;s official.\u00a0 I have a new mistress and her name is DBUS.\u00a0 After seeing how <a href=\"http:\/\/www.danplanet.com\/content\/view\/45\/76\/\">easy it was<\/a> to take an action when a particular storage device was attached, I was hooked.\u00a0 Since I know NetworkManager has a <a href=\"http:\/\/people.redhat.com\/dcbw\/NetworkManager\/NetworkManager%20DBUS%20API.txt\">robust DBUS interface<\/a> , I decided to try something a little more interesting.<\/p>\n<p>Something I&#8217;ve always wanted is for Pidgin to be aware of my network connectivity and adjust my status accordingly.\u00a0 I work from home most of the time, so I set my status to indicate this fact and expose my home office phone number for colleagues.\u00a0 I go into the office so occasionally that I never remember to change my Pidgin status accordingly, thus confusing people who were expecting me to be in that day.<\/p>\n<p>I set out to write something to effectively bridge NetworkManager&#8217;s DBUS interface with that of Pidgin.\u00a0 There would need to be some logic in the middle to determine the proper status message given the current state of the network.\u00a0 First, I <a href=\"http:\/\/static.danplanet.com\/location\/devices.py\">wrote<\/a> a python module to query NetworkManager for details about activated devices.\u00a0 Next, I wrote a (probably overly complicated) <a href=\"http:\/\/static.danplanet.com\/location\/locations.py\">location module<\/a> that reads a config file and provides an interface to return properties of the current location, based on the information about the current active device, as determined by the previous module.\u00a0 The config file looks something like this:<\/p>\n<blockquote>\n<p style=\"font-family: Courier New,Courier,mono;\">[Home]<br \/>\ndevices = eth0<br \/>\nsubnet = 192.168.1<br \/>\ncaption = At Home<br \/>\nfoobar = baz<\/p>\n<p>[Home-Wireless]<br \/>\ndevices = ath0<br \/>\nsubnet = 192.168.1<br \/>\ncaption = At Home (wireless)<\/p>\n<p>[Work]<br \/>\ndevices = eth0<br \/>\nsubnet = 10.0.12<br \/>\ncaption = At the office<\/p><\/blockquote>\n<p>This lets me match settings based on not only a bit of the IP address, but also on the device name.\u00a0 With those two data points, I can have separate status messages for Home and Home-Wireless.<\/p>\n<p>Next, all I needed was something to watch for NetworkManager&#8217;s DBUS signal that the active device has changed, determine the new location, and tell Pidgin about it.\u00a0 After seeing some <a href=\"http:\/\/arstechnica.com\/reviews\/apps\/pidgin-2-0.ars\/4\">examples<\/a> , I found that it is surprisingly <a href=\"http:\/\/static.danplanet.com\/location\/pidgin_location_changer.py\">simple<\/a> :<\/p>\n<blockquote>\n<p style=\"font-family: Courier New,Courier,mono;\">import dbus<br \/>\nimport gobject<br \/>\nimport dbus.mainloop.glib<\/p>\n<p>import locations<br \/>\nimport sys<\/p>\n<p>def change_status(id, caption):<br \/>\nglobal pidgin<\/p>\n<p>status = pidgin.PurpleSavedstatusNew(&#8220;&#8221;, 2)<br \/>\npidgin.PurpleSavedstatusSetMessage(status, caption)<br \/>\npidgin.PurpleSavedstatusActivate(status)<\/p>\n<p>def dev_changed(*args, **kwargs):<br \/>\nif not kwargs[&#8220;member&#8221;] == &#8220;DeviceNowActive&#8221;:<br \/>\nreturn<\/p>\n<p>loc = locations.LocationService(sys.argv[1]).current_location()<br \/>\nchange_status(loc.name, loc.caption)<\/p>\n<p>def monitor_nm():<br \/>\ndbus.mainloop.glib.DBusGMainLoop(set_as_default=True)<\/p>\n<p>bus = dbus.SystemBus()<br \/>\nbus.add_signal_receiver(dev_changed,<br \/>\ninterface_keyword=&#8221;dbus_interface&#8221;,<br \/>\nmember_keyword=&#8221;member&#8221<img decoding=\"async\" src=\"https:\/\/www.danplanet.com\/blog\/wp-content\/themes\/wordpress-grey-opaque-master\/images\/smilies\/icon_wink.gif\" alt=\"Smilie: ;)\" title=\"Smilie: ;)\" \/><\/p>\n<p>loop = gobject.MainLoop()<br \/>\nloop.run()<\/p>\n<p>def connect_to_pidgin():<br \/>\nglobal pidgin<\/p>\n<p>bus = dbus.SessionBus()<br \/>\nobj = bus.get_object(&#8220;im.pidgin.purple.PurpleService&#8221;,<br \/>\n&#8220;\/im\/pidgin\/purple\/PurpleObject&#8221<img decoding=\"async\" src=\"https:\/\/www.danplanet.com\/blog\/wp-content\/themes\/wordpress-grey-opaque-master\/images\/smilies\/icon_wink.gif\" alt=\"Smilie: ;)\" title=\"Smilie: ;)\" \/><br \/>\npidgin = dbus.Interface(obj, &#8220;im.pidgin.purple.PurpleInterface&#8221<img decoding=\"async\" src=\"https:\/\/www.danplanet.com\/blog\/wp-content\/themes\/wordpress-grey-opaque-master\/images\/smilies\/icon_wink.gif\" alt=\"Smilie: ;)\" title=\"Smilie: ;)\" \/><\/p>\n<p>if len(sys.argv) &lt; 2:<br \/>\nprint &#8220;Usage: %s &lt;location_config&gt;&#8221; % sys.argv[0]<br \/>\nsys.exit(1)<\/p>\n<p>connect_to_pidgin()<br \/>\nmonitor_nm()<\/p><\/blockquote>\n<p>The dev_changed() function gets called for each signal (I think there must be a better way to select specific signals you&#8217;re interested, but this is quick and it works).\u00a0 If the signal matches the correct name, then we determine the current location and call change_status() to tell Pidgin about it.\u00a0 How awesome is that?<\/p>\n<p>I&#8217;ve thought about generalizing this into a &#8220;Location Awareness Service&#8221; that would emit signals on DBUS to notify other applications.\u00a0 It would be really awesome to write something that would tell Asterisk to forward my extension to another line, depending on where I am:<\/p>\n<ul>\n<li>If I&#8217;m at &#8220;Home&#8221;, ring my desk line<\/li>\n<li>If I&#8217;m at &#8220;Home-Wireless&#8221;, ring the house phones<\/li>\n<li>If I&#8217;m at &#8220;Work&#8221;, forward to my work line<\/li>\n<li>If I&#8217;m anywhere else, forward to my cell phone<\/li>\n<\/ul>\n<p>On the other hand, maybe I&#8217;ll hold off on that last item&#8230; <img decoding=\"async\" src=\"https:\/\/www.danplanet.com\/blog\/wp-content\/themes\/wordpress-grey-opaque-master\/images\/smilies\/icon_smile.gif\" alt=\"Smilie: :)\" title=\"Smilie: :)\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>It&#8217;s official.\u00a0 I have a new mistress and her name is DBUS.\u00a0 After seeing how easy it was to take an action when a particular storage device was attached, I was hooked.\u00a0 Since I know NetworkManager has a robust DBUS &hellip; <a href=\"https:\/\/www.danplanet.com\/blog\/2007\/11\/17\/automatic-pidgin-status-with-dbus-and-networkmanager\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12,6],"tags":[],"class_list":["post-56","post","type-post","status-publish","format-standard","hentry","category-codemonkeying","category-linux"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.6 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Automatic Pidgin status with DBUS and NetworkManager - Right Angles<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.danplanet.com\/blog\/2007\/11\/17\/automatic-pidgin-status-with-dbus-and-networkmanager\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Automatic Pidgin status with DBUS and NetworkManager - Right Angles\" \/>\n<meta property=\"og:description\" content=\"It&#8217;s official.\u00a0 I have a new mistress and her name is DBUS.\u00a0 After seeing how easy it was to take an action when a particular storage device was attached, I was hooked.\u00a0 Since I know NetworkManager has a robust DBUS &hellip; Continue reading &rarr;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.danplanet.com\/blog\/2007\/11\/17\/automatic-pidgin-status-with-dbus-and-networkmanager\/\" \/>\n<meta property=\"og:site_name\" content=\"Right Angles\" \/>\n<meta property=\"article:published_time\" content=\"2007-11-17T14:02:36+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2011-11-29T16:53:35+00:00\" \/>\n<meta name=\"author\" content=\"Dan\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Dan\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.danplanet.com\/blog\/2007\/11\/17\/automatic-pidgin-status-with-dbus-and-networkmanager\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.danplanet.com\/blog\/2007\/11\/17\/automatic-pidgin-status-with-dbus-and-networkmanager\/\"},\"author\":{\"name\":\"Dan\",\"@id\":\"https:\/\/www.danplanet.com\/blog\/#\/schema\/person\/0f6920aa6d63cae437bf8b122200287c\"},\"headline\":\"Automatic Pidgin status with DBUS and NetworkManager\",\"datePublished\":\"2007-11-17T14:02:36+00:00\",\"dateModified\":\"2011-11-29T16:53:35+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.danplanet.com\/blog\/2007\/11\/17\/automatic-pidgin-status-with-dbus-and-networkmanager\/\"},\"wordCount\":622,\"publisher\":{\"@id\":\"https:\/\/www.danplanet.com\/blog\/#\/schema\/person\/0f6920aa6d63cae437bf8b122200287c\"},\"articleSection\":[\"Codemonkeying\",\"Linux\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.danplanet.com\/blog\/2007\/11\/17\/automatic-pidgin-status-with-dbus-and-networkmanager\/\",\"url\":\"https:\/\/www.danplanet.com\/blog\/2007\/11\/17\/automatic-pidgin-status-with-dbus-and-networkmanager\/\",\"name\":\"Automatic Pidgin status with DBUS and NetworkManager - Right Angles\",\"isPartOf\":{\"@id\":\"https:\/\/www.danplanet.com\/blog\/#website\"},\"datePublished\":\"2007-11-17T14:02:36+00:00\",\"dateModified\":\"2011-11-29T16:53:35+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/www.danplanet.com\/blog\/2007\/11\/17\/automatic-pidgin-status-with-dbus-and-networkmanager\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.danplanet.com\/blog\/2007\/11\/17\/automatic-pidgin-status-with-dbus-and-networkmanager\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.danplanet.com\/blog\/2007\/11\/17\/automatic-pidgin-status-with-dbus-and-networkmanager\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.danplanet.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Automatic Pidgin status with DBUS and NetworkManager\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.danplanet.com\/blog\/#website\",\"url\":\"https:\/\/www.danplanet.com\/blog\/\",\"name\":\"Right Angles\",\"description\":\"If they&#039;re not right...they&#039;re wrong\",\"publisher\":{\"@id\":\"https:\/\/www.danplanet.com\/blog\/#\/schema\/person\/0f6920aa6d63cae437bf8b122200287c\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.danplanet.com\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\/\/www.danplanet.com\/blog\/#\/schema\/person\/0f6920aa6d63cae437bf8b122200287c\",\"name\":\"Dan\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.danplanet.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/9b73782704be64dd8c030087af2d1ae0c1dc488cad69093ff0366dbaad2de673?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/9b73782704be64dd8c030087af2d1ae0c1dc488cad69093ff0366dbaad2de673?s=96&d=mm&r=g\",\"caption\":\"Dan\"},\"logo\":{\"@id\":\"https:\/\/www.danplanet.com\/blog\/#\/schema\/person\/image\/\"},\"url\":\"https:\/\/www.danplanet.com\/blog\/author\/dan\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Automatic Pidgin status with DBUS and NetworkManager - Right Angles","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.danplanet.com\/blog\/2007\/11\/17\/automatic-pidgin-status-with-dbus-and-networkmanager\/","og_locale":"en_US","og_type":"article","og_title":"Automatic Pidgin status with DBUS and NetworkManager - Right Angles","og_description":"It&#8217;s official.\u00a0 I have a new mistress and her name is DBUS.\u00a0 After seeing how easy it was to take an action when a particular storage device was attached, I was hooked.\u00a0 Since I know NetworkManager has a robust DBUS &hellip; Continue reading &rarr;","og_url":"https:\/\/www.danplanet.com\/blog\/2007\/11\/17\/automatic-pidgin-status-with-dbus-and-networkmanager\/","og_site_name":"Right Angles","article_published_time":"2007-11-17T14:02:36+00:00","article_modified_time":"2011-11-29T16:53:35+00:00","author":"Dan","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Dan","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.danplanet.com\/blog\/2007\/11\/17\/automatic-pidgin-status-with-dbus-and-networkmanager\/#article","isPartOf":{"@id":"https:\/\/www.danplanet.com\/blog\/2007\/11\/17\/automatic-pidgin-status-with-dbus-and-networkmanager\/"},"author":{"name":"Dan","@id":"https:\/\/www.danplanet.com\/blog\/#\/schema\/person\/0f6920aa6d63cae437bf8b122200287c"},"headline":"Automatic Pidgin status with DBUS and NetworkManager","datePublished":"2007-11-17T14:02:36+00:00","dateModified":"2011-11-29T16:53:35+00:00","mainEntityOfPage":{"@id":"https:\/\/www.danplanet.com\/blog\/2007\/11\/17\/automatic-pidgin-status-with-dbus-and-networkmanager\/"},"wordCount":622,"publisher":{"@id":"https:\/\/www.danplanet.com\/blog\/#\/schema\/person\/0f6920aa6d63cae437bf8b122200287c"},"articleSection":["Codemonkeying","Linux"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/www.danplanet.com\/blog\/2007\/11\/17\/automatic-pidgin-status-with-dbus-and-networkmanager\/","url":"https:\/\/www.danplanet.com\/blog\/2007\/11\/17\/automatic-pidgin-status-with-dbus-and-networkmanager\/","name":"Automatic Pidgin status with DBUS and NetworkManager - Right Angles","isPartOf":{"@id":"https:\/\/www.danplanet.com\/blog\/#website"},"datePublished":"2007-11-17T14:02:36+00:00","dateModified":"2011-11-29T16:53:35+00:00","breadcrumb":{"@id":"https:\/\/www.danplanet.com\/blog\/2007\/11\/17\/automatic-pidgin-status-with-dbus-and-networkmanager\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.danplanet.com\/blog\/2007\/11\/17\/automatic-pidgin-status-with-dbus-and-networkmanager\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.danplanet.com\/blog\/2007\/11\/17\/automatic-pidgin-status-with-dbus-and-networkmanager\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.danplanet.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Automatic Pidgin status with DBUS and NetworkManager"}]},{"@type":"WebSite","@id":"https:\/\/www.danplanet.com\/blog\/#website","url":"https:\/\/www.danplanet.com\/blog\/","name":"Right Angles","description":"If they&#039;re not right...they&#039;re wrong","publisher":{"@id":"https:\/\/www.danplanet.com\/blog\/#\/schema\/person\/0f6920aa6d63cae437bf8b122200287c"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.danplanet.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/www.danplanet.com\/blog\/#\/schema\/person\/0f6920aa6d63cae437bf8b122200287c","name":"Dan","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.danplanet.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/9b73782704be64dd8c030087af2d1ae0c1dc488cad69093ff0366dbaad2de673?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/9b73782704be64dd8c030087af2d1ae0c1dc488cad69093ff0366dbaad2de673?s=96&d=mm&r=g","caption":"Dan"},"logo":{"@id":"https:\/\/www.danplanet.com\/blog\/#\/schema\/person\/image\/"},"url":"https:\/\/www.danplanet.com\/blog\/author\/dan\/"}]}},"_links":{"self":[{"href":"https:\/\/www.danplanet.com\/blog\/wp-json\/wp\/v2\/posts\/56","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.danplanet.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.danplanet.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.danplanet.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.danplanet.com\/blog\/wp-json\/wp\/v2\/comments?post=56"}],"version-history":[{"count":2,"href":"https:\/\/www.danplanet.com\/blog\/wp-json\/wp\/v2\/posts\/56\/revisions"}],"predecessor-version":[{"id":148,"href":"https:\/\/www.danplanet.com\/blog\/wp-json\/wp\/v2\/posts\/56\/revisions\/148"}],"wp:attachment":[{"href":"https:\/\/www.danplanet.com\/blog\/wp-json\/wp\/v2\/media?parent=56"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.danplanet.com\/blog\/wp-json\/wp\/v2\/categories?post=56"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.danplanet.com\/blog\/wp-json\/wp\/v2\/tags?post=56"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}