iChat Server autobuddy with local accounts

written by justin on February 13th, 2008 @ 11:39 AM

I wrote a script in Ruby to do the autobuddy thing for an advanced server mode install of leopard server. I run it every hour and it takes care of new accounts and removed accounts.

#!/usr/bin/ruby

require 'rubygems'
require 'sqlite3'

# Create our sqlite3 db object
db = SQLite3::Database.new( "/var/jabberd/sqlite/jabberd2.db" )

# get all of our jabber users both contact owers and contacts
rows = db.execute("select distinct \"collection-owner\" from 'roster-items'")

rows << db.execute("select distinct jid from 'roster-items'")
all_in_jabber = rows.uniq.flatten
all_in_jabber.map! {|a| "'" + a + "'"}

# an array of accounts we don't want to process
excludes = ['admin.plist','daemon.plist','nobody.plist','root.plist','.','..']

# container for our active accounts
all_contacts = []


Dir.foreach("/var/db/dslocal/nodes/Default/users") do |f|
	unless f =~ /^_/ || excludes.include?(f)
	  name, extension = f.split(".")
	  all_contacts << "'#{name}@domain.org'"
	end
	
	all_contacts.each do |c|
	  # lets loop the all contacts again and query for them and then do an insert if need be
	  all_contacts.each do |x|
	    next if c == x
	    #puts "select * from 'roster-items' where 'collection-owner' = #{c} AND jid = #{x}"
	    rows = db.execute("select * from 'roster-items' where \"collection-owner\" = #{c} AND jid = #{x}")
	    if rows.empty?
	    puts "insert into 'roster-items' values(#{c},NULL,#{x},#{x},1,1,0)"
	      db.execute("insert into 'roster-items' values(#{c},NULL,#{x},#{x},1,1,0)")
      end
	  end
	end
	
end

removals = all_in_jabber - all_contacts

# gets rid of accounts that have been deleted
removals.each do |r|
  db.execute("delete from 'roster-items' where jid=#{r}")
  db.execute("delete from 'roster-items' where \"collection-owner\"=#{r}")
end

# the table roster-items
# collection-owner
# object-sequence
# jid
# name
# to
# from
# ask

Post a comment