1#!/usr/bin/python
2# -*- coding: utf-8 -*-
3
4import sys
5import select
6from optparse import OptionParser
7
8import iksemel
9
10
11class MyStream(iksemel.Stream):
12    def on_stanza(self, doc):
13        if doc.name() == "iq" and doc.get("type") == "result" and doc.get("id") == "auth":
14            iq = iksemel.Document("iq")
15            iq.set("type", "get")
16            iq.set("id", "roster")
17            query = iq.insert("query")
18            query.set("xmlns",  "jabber:iq:roster")
19            self.send(iq)
20        if doc.name() == "iq" and doc.get("type") == "result" and doc.get("id") == "roster":
21            print "Roster:"
22            print doc
23            # FIXME: exit
24
25    def on_xml(self,  text,  is_incoming):
26        if not self.options.verbose:
27            return
28        pre = "SEND"
29        if is_incoming:
30            pre = "RECV"
31        print "%s [%s]" % (pre,  text)
32
33    def ask_password(self):
34        pw = self.options.password
35        if pw is None:
36            pw = raw_input("Password? ")
37        return pw
38
39
40def get_roster(options,  jid):
41    my = MyStream()
42    my.options = options
43    my.connect(jid=iksemel.JID(jid),  tls=False)
44
45    while 1:
46        select.select([my], [], [])
47        my.recv()
48
49if __name__ == "__main__":
50    parser = OptionParser()
51    parser.add_option("-v",  "--verbose",  dest="verbose", action="store_true",  default=False,  help="Print XML communication")
52    parser.add_option("-p",  "--password",  dest="password", action="store",  default=None,  help="Use given password")
53    options,  args = parser.parse_args()
54
55    get_roster(options,  args[0])
56