I always forget to turn off my chat accounts at work at the end of the day. Below is a script to take my accounts offline when it’s time to go home. This is for Ubuntu 10.10 using Empathy for my Jabber accounts.
#!/usr/bin/env ruby
require 'rubygems'
require 'dbus'
# Get the proper DBus session address
pid = nil
programs = %w( gnome-session )
programs.each do |program|
pid = `pidof -s #{program}`
break unless pid.nil?
end
# Fail if we can't find the pid
exit if pid.nil?
# Get the address
info = IO.readlines("/proc/#{pid.strip}/environ").first.split("\0")
info.reject! { |x| !x.include? 'DBUS_SESSION_BUS_ADDRESS' }
address = info.first.split('=', 2)[1]
# Set the address
ENV['DBUS_SESSION_BUS_ADDRESS'] = address
# Get the objects
bus = DBus::SessionBus.instance
service = bus.service('org.freedesktop.Telepathy.AccountManager')
service.introspect
account_manager = service.object('/org/freedesktop/Telepathy/AccountManager');
account_manager.introspect
iface = account_manager['com.nokia.AccountManager.Interface.Query']
accounts = iface.FindAccounts []
accounts.first.each do |account|
account_object = service.object(account);
account_object.introspect
account_iface = account_object['org.freedesktop.DBus.Properties']
account_iface.Set 'org.freedesktop.Telepathy.Account', 'RequestedPresence', ['(uss)', [1,'offline','']]
end
You’ll need the ruby-dbus gem to use this. I just placed it in a file and call it from a cron job. The script finds all of your accounts via D-Bus and and then sends them a new presence.
Update 2011-01-31: Added code to find the correct DBus Session Address, since the cron job does not have it.
I’ve been working through Project Euler as a coding / math exercise. Problem 15 asks to find the amount of paths that exist between the top-left and bottom-right points of a 20×20 grid, without backtracking. I decided to visualize it, because just running a command line program and waiting can be boring.
Of course, this problem is silly to brute force, but by watching the visualization and working on a pad of paper, I came up with a much faster solution to solving the problem.
The code is available on github. It’s Ruby and Tk. Some Ruby installs don’t compile with Tk, so if the script doesn’t load the tk requirement, Google around for the solution.
Also note, this is a quick hack. It leaks memory, it’s buggy, it might eat your processor and will run for a very long time.
I had a need at work to send email out of our Rails application which met the following criteria:
It has both a plain text and HTML part.
It may have embedded images in the HTML part.
It may have attachments.
Below is the code, which you can put in your app/models directory.
class EmbeddedMailer < ActionMailer::Base
def embedded_mail(to, from, subject, html_message, text_message, files = {}, embeds = {})
# Hack TMail to allow embeds
TMail::HeaderField::FNAME_TO_CLASS.delete 'content-id'
recipients to
from from
subject subject
content_type 'multipart/mixed'
# The Message
part :content_type => 'multipart/alternative' do |m|
# Plain Text Message
m.part :content_type => 'text/plain' do |p|
p.transfer_encoding = 'base64'
p.body = text_message
end
# HTML Message
m.part :content_type => 'multipart/related' do |x|
x.part :content_type => 'text/html' do |p|
p.body = html_message
end
# Embedded
embeds.each do |key, embed|
x.part :content_type => embed[:content_type] do |p|
p.headers = { 'Content-ID' => "<#{key}>" }
p.transfer_encoding = 'base64'
p.body = File.read embed[:path]
end
end
end
end
# Attachments
files.each do |key, embed|
attachment :content_type => embed[:content_type] do |a|
a.filename = key
a.body = File.read embed[:path]
end
end
end
end
A Quick Explanation
You have to hack TMail, the built in email parser for ActionMailer, so that it doesn’t try to validate the Content-ID header.
To meet all of the criteria above, you have to create a MIME-type hierarchy as follows:
multipart/mixed – a mix of a message and attachments
multipart/alternative – to mark that the text and html parts are equivalent to each other
text/plain – the plain text section
multipart/related – the HTML section with embedded images
text/html – the HTML section
Embedded images go here
attachment/content-type – attachments are at the bottom
The format of the files and embeds hashes is as follows. The key is the file name of the attached file. The value is another hash with content_type set to the content type of the attachment and path is the full path to the file. For example:
Update 2010-11-30: Fixed the layout to add multipart/mixed for attachments. Update 2011-04-07: I have created a solution for Rails 3, which is much more simplistic.