#!/usr/bin/env python

from gi.repository import GObject
GObject.threads_init()

from gi.repository import TelepathyGLib

def observe_channels_cb(observer, account, connection, channels, do,
    requests, context, user_data):
    for channel in channels:
        if not isinstance(channel, TelepathyGLib.TextChannel):
            continue

        print "Members of %s :" % (channel.get_identifier())

        for member in channel.group_dup_members_contacts():
            print "%s (%s)" % (member.get_alias(), member.get_identifier())
        print ""

    context.accept()

if __name__ == '__main__':
    #TelepathyGLib.debug_set_flags("all")

    dbus = TelepathyGLib.DBusDaemon.dup()

    observer = TelepathyGLib.SimpleObserver.new(dbus, True,
        'ObserveMuc', True, observe_channels_cb, None)

    observer.add_observer_filter({
        TelepathyGLib.PROP_CHANNEL_CHANNEL_TYPE: TelepathyGLib.IFACE_CHANNEL_TYPE_TEXT,
        TelepathyGLib.PROP_CHANNEL_TARGET_HANDLE_TYPE: int(TelepathyGLib.HandleType.ROOM),
    })

    observer.register()

    main_loop = GObject.MainLoop()
    main_loop.run()
