Blog

Make some new friends automatically using iChat Server and launchd

By Iain Morris posted on January 15th, 2010

The iChat Server module in Mac OS X Server provides a great interface to the jabberd daemon, getting you up and running quickly with standard and Kerberos authentication using your Open Directory users. Unfortunately, there are a few useful features that are not yet accessible in Server Admin. One of these is a simple tool for automatically setting everyone to be a buddy with each other. In smaller organizations, it’s a great way to get in touch with everyone without having to ask everyone to become a buddy individually.

The executable for this function is /usr/sbin/jabber_autobuddy. The documentation says it’s not intended to be invoked by users directly, but we’ll do it with a scheduled script here. jabber_autobuddy is a simple binary that modifies entries in the SQLite database (/var/jabberd/sqlite/jabberd2.db) containing your jabberd users.

First, let’s create a new script as an administrator called auto_buddy.sh. To that file, add:

#!/bin/sh
/usr/sbin/jabber_autobuddy -D -m

The -D provides some verbose output to keep track of issues, while the -m does the actual “buddifying” of the users in your chat user database.

We’ll need to make this script executable:

chmod u+x auto_buddy.sh

Put it in a location where you keep other scripts you like to run, such as /usr/local/bin, or ~/bin.

It might seem a bit over the top to make a new script for a single command, but makes things a little more flexible using launchd, as we’ll see in a moment.

Running this script will make any initialized user in iChat server (any user that has logged in before) a buddy of everyone else. But what if I want to add a new user? They won’t be included until the next run of jabber_autobuddy. We need to schedule a job for this, and we’ll do it with Apple’s preferred scheduler and master process, launchd.

Launchd uses xml plist files to decide what to do. We’re going to add a new file to /Library/LaunchDaemons called com.mycompany.autobuddy.plist.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN"
   "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">

<dict>
	<key>Disabled</key>
	<false/>

	<key>Label</key>
	<string>com.mycompany.autobuddy</string>

	<key>ProgramArguments</key>
	<array>
		<string>/private/var/root/bin/auto_buddy.sh</string>
	</array>

	<key>KeepAlive</key>
	<false/>

	<key>StartInterval</key>
	<integer>1800</integer>
</dict>

</plist>

Here, I’m assuming the script auto_buddy.sh will sit in ~root/bin, and I want it to execute every 30 minutes (1800 seconds).

The final step is to load the plist file into launchd:

launchctl load com.mycompany.autobuddy.plist

You should see your process loaded in launchd with:

launchctl list | grep autobuddy

Whenever anyone joins the iChat server for the first time, they are registered in the SQLite database. Once autobuddy runs in its scheduled window, at the next login, the new user will be buddied up with everyone else in the list.

If you ever want to unload the process from launchd, use:

launchctl unload com.mycompany.autobuddy

You will need to do this if you change the StartInterval value. You might see the value of putting more complex commands in a container script, so you can edit the script without having to unload/load the launchctl plist file each time you change the switches on the ProgramArguments line. Note that it would seem simpler to just add a cron entry for this process, and you would probably be right. However, cron is technically deprecated in the latest versions of Mac OS X Server. My guess is cron isn’t going anywhere anytime soon, but who knows?

About Iris Professional Services
Iris Professional Services is a computer consulting company operating offices in both Seattle and Portland. Businesses throughout the Pacific Northwest rely on our expert IT consultants for all their network IT support services.

Posted in Mac OS X Server, Scripts, System Administration, Tips and Tricks

5 Responses to “Make some new friends automatically using iChat Server and launchd”

  1. Mat X says:

    Very cool. Learn something new every day. w00t.

  2. Iain Morris says:

    I should note that this feature is built-in to Mac OS X Server 10.6, replaced basically with a single check-box!

  3. Jeri says:

    I was just looking at this and there’s another simpler option…

    Run:

    serveradmin stop jabber
    /usr/bin/jabber_autobuddy -V -i @
    /usr/bin/jabber_autobuddy -V -g (-m as you use)
    serveradmin start jabber

  4. Brennan says:

    This really helped me out. Just one minor issue, since I’m using a Snow Leopard server, the actual jabber_autobuddy command is:

    /usr/bin/jabber_autobuddy -V -m

    -D has depreciated in favor of the more common -V

    I also dumped the .plist here:
    /System/Library/LaunchDaemons/

    Rather than:
    /Library/LaunchDaemons

    Cheers,
    Brennan

  5. BiL says:

    Thanks for this excellent article.

    I’ve seen references to jabber_autobuddy with the “-D” argument on other blogs as well, but using a fully patch 10.6.4 server, I get ‘Invalid command argument “-D”‘

    Regardless, I can’t seem to add users from a bound ActiveDirectory domain. Other services allow users to authenticate via AD, as does the Jabber service. It’s just the autobuddy feature that doesn’t seem to work.

Leave a Reply