Transfering Jabber Rosters

written on Sun, 22 Oct 2006 14:23

After two hours of playing with ejabberd i found out that the new server was unable to read the ejabberd database from the old one. great -.- Because neither me nor Georg were able to fix that issue i wrote a script that dumps the account data from one jabber account so that you can import it into another one.

Here the code for dumping and restoring a roster. It requires that you have xmpppy installed

import sys
from xmpp import JID, Client
from pickle import dump, load

try:
    MODE, FILE, USERNAME, PASSWORD = sys.argv[1:5]
except ValueError:
    print 'usage: %s <mode> <file> <username> <password> % sys.argv[0]
    sys.exit(1)

jid = JID(USERNAME)
cl = Client(jid.getDomain(), debug=[])
if not cl.connect() or not cl.auth(jid.getNode(), PASSWORD):
    print 'couldn not connect to server'
else:
    cl.sendInitPresence()
    roster = cl.getRoster()
    if MODE == 'dump':
        data = {}
        for jid in roster.getItems():
            data[jid] = roster.getRawItem(jid)
        f = file(FILE, 'w')
        dump(data, f)
        f.close()
    elif MODE == 'load':
        for jid, data in load(file(FILE)).iteritems():
            roster.setItem(jid, data['name'], data['groups'] or [])
            roster.Authorize(jid)
            roster.Subscribe(jid)
    else:
        print 'error: mode must be load or dump'
        sys.exit(1)

Use it like this:

python rosterdump.py dump roster.dump yourname@example.com yourpassword

And restore it using this command:

python rosterdump.py load roster.dump yourname@otherserver.com yournewpassword

Note that i won't further work on that script and it's possible that are some errors in the script. I don't guarantee that it actually works :D Nevertheless I hope it might be useful for you.

view page source