This file is indexed.

/usr/lib/python2.7/dist-packages/jabber/jabber.py is in python-jabber 0.5.0-1.6.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

   1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
##   jabber.py
##
##   Copyright (C) 2001 Matthew Allum
##
##   This program is free software; you can redistribute it and/or modify
##   it under the terms of the GNU Lesser General Public License as published
##   by the Free Software Foundation; either version 2, or (at your option)
##   any later version.
##
##   This program is distributed in the hope that it will be useful,
##   but WITHOUT ANY WARRANTY; without even the implied warranty of
##   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
##   GNU Lesser General Public License for more details.
##


"""\

__intro__

jabber.py is a Python module for the jabber instant messaging protocol.
jabber.py deals with the xml parsing and socket code, leaving the programmer
to concentrate on developing quality jabber based applications with Python.

The eventual aim is to produce a fully featured easy to use library for
creating both jabber clients and servers.

jabber.py requires at least python 2.0 and the XML expat parser module
( included in the standard Python distrubution ).

It is developed on Linux but should run happily on over Unix's and win32.

__Usage__

jabber.py basically subclasses the xmlstream classs and provides the
processing of jabber protocol elements into object instances as well
'helper' functions for parts of the protocol such as authentication
and roster management.

An example of usage for a simple client would be ( only psuedo code !)

<> Read documentation on jabber.org for the jabber protocol.

<> Birth a jabber.Client object with your jabber servers host

<> Define callback functions for the protocol elements you want to use
   and optionally a disconnection.

<> Authenticate with the server via auth method, or register via the
   reg methods to get an account.

<> Call requestRoster() and sendPresence()

<> loop over process(). Send Iqs,messages and presences by birthing
   them via there respective clients , manipulating them and using
   the Client's send() method.

<> Respond to incoming elements passed to your callback functions.

<> Find bugs :)


"""

# $Id: jabber.py,v 1.58 2004/01/18 05:27:10 snakeru Exp $

import xmlstream
import sha, time

debug=xmlstream.debug

VERSION = xmlstream.VERSION

False = 0;
True  = 1;

timeout = 300

DBG_INIT, DBG_ALWAYS = debug.DBG_INIT, debug.DBG_ALWAYS
DBG_DISPATCH = 'jb-dispatch'            ; debug.debug_flags.append( DBG_DISPATCH )
DBG_NODE = 'jb-node'                    ; debug.debug_flags.append( DBG_NODE)
DBG_NODE_IQ = 'jb-node-iq'              ; debug.debug_flags.append( DBG_NODE_IQ )
DBG_NODE_MESSAGE = 'jb-node-message'    ; debug.debug_flags.append( DBG_NODE_MESSAGE )
DBG_NODE_PRESENCE = 'jb-node-pressence' ; debug.debug_flags.append( DBG_NODE_PRESENCE )
DBG_NODE_UNKNOWN = 'jb-node-unknown'    ; debug.debug_flags.append( DBG_NODE_UNKNOWN )


#
# JANA core namespaces
#  from http://www.jabber.org/jana/namespaces.php as of 2003-01-12
#  "myname" means that namespace didnt have a name in the jabberd headers
#
NS_AGENT      = "jabber:iq:agent"
NS_AGENTS     = "jabber:iq:agents"
NS_AUTH       = "jabber:iq:auth"
NS_CLIENT     = "jabber:client"
NS_DELAY      = "jabber:x:delay"
NS_OOB        = "jabber:iq:oob"
NS_REGISTER   = "jabber:iq:register"
NS_ROSTER     = "jabber:iq:roster"
NS_XROSTER    = "jabber:x:roster" # myname
NS_SERVER     = "jabber:server"
NS_TIME       = "jabber:iq:time"
NS_VERSION    = "jabber:iq:version"

NS_COMP_ACCEPT  = "jabber:component:accept" # myname
NS_COMP_CONNECT = "jabber:component:connect" # myname



#
# JANA JEP namespaces, ordered by JEP
#  from http://www.jabber.org/jana/namespaces.php as of 2003-01-12
#  all names by jaclu
#
_NS_PROTOCOL  = "http://jabber.org/protocol" # base for other
NS_PASS       = "jabber:iq:pass" # JEP-0003
NS_XDATA      = "jabber:x:data" # JEP-0004
NS_RPC        = "jabber:iq:rpc" # JEP-0009
NS_BROWSE     = "jabber:iq:browse" # JEP-0011
NS_LAST       = "jabber:iq:last" #JEP-0012
NS_PRIVACY    = "jabber:iq:privacy" # JEP-0016
NS_XEVENT     = "jabber:x:event" # JEP-0022
NS_XEXPIRE    = "jabber:x:expire" # JEP-0023
NS_XENCRYPTED = "jabber:x:encrypted" # JEP-0027
NS_XSIGNED    = "jabber:x:signed" # JEP-0027
NS_P_MUC      = _NS_PROTOCOL + "/muc" # JEP-0045
NS_VCARD      = "vcard-temp" # JEP-0054


#
# Non JANA aproved, ordered by JEP
#  all names by jaclu
#
_NS_P_DISCO     = _NS_PROTOCOL + "/disco" # base for other
NS_P_DISC_INFO  = _NS_P_DISCO + "#info" # JEP-0030
NS_P_DISC_ITEMS = _NS_P_DISCO + "#items" # JEP-0030
NS_P_COMMANDS   = _NS_PROTOCOL + "/commands" # JEP-0050


"""
 2002-01-11 jaclu

 Defined in jabberd/lib/lib.h, but not JANA aproved and not used in jabber.py
 so commented out, should/could propably be removed...

 NS_ADMIN      = "jabber:iq:admin"
 NS_AUTH_OK    = "jabber:iq:auth:0k"
 NS_CONFERENCE = "jabber:iq:conference"
 NS_ENVELOPE   = "jabber:x:envelope"
 NS_FILTER     = "jabber:iq:filter"
 NS_GATEWAY    = "jabber:iq:gateway"
 NS_OFFLINE    = "jabber:x:offline"
 NS_PRIVATE    = "jabber:iq:private"
 NS_SEARCH     = "jabber:iq:search"
 NS_XDBGINSERT = "jabber:xdb:ginsert"
 NS_XDBNSLIST  = "jabber:xdb:nslist"
 NS_XHTML      = "http://www.w3.org/1999/xhtml"
 NS_XOOB       = "jabber:x:oob"
 NS_COMP_EXECUTE = "jabber:component:execute" # myname
"""


## Possible constants for Roster class .... hmmm ##
RS_SUB_BOTH    = 0
RS_SUB_FROM    = 1
RS_SUB_TO      = 2

RS_ASK_SUBSCRIBE   = 1
RS_ASK_UNSUBSCRIBE = 0

RS_EXT_ONLINE   = 2
RS_EXT_OFFLINE  = 1
RS_EXT_PENDING  = 0

#############################################################################

def ustr(what):
    """If sending object is already a unicode str, just
       return it, otherwise convert it using xmlstream.ENCODING"""
    if type(what) == type(u''):
        r = what
    else:
        try: r = what.__str__()
        except AttributeError: r = str(what)
        # make sure __str__() didnt return a unicode
        if type(r) <> type(u''):
            r = unicode(r,xmlstream.ENCODING,'replace')
    return r
xmlstream.ustr = ustr

class NodeProcessed(Exception): pass   # currently only for Connection._expectedIqHandler

class Connection(xmlstream.Client):
    """Forms the base for both Client and Component Classes"""
    def __init__(self, host, port, namespace,
                 debug=[], log=False, connection=xmlstream.TCP, hostIP=None, proxy=None):

        xmlstream.Client.__init__(self, host, port, namespace,
                                  debug=debug, log=log,
                                  connection=connection,
                                  hostIP=hostIP, proxy=proxy)
        self.handlers={}
        self.registerProtocol('unknown', Protocol)
        self.registerProtocol('iq', Iq)
        self.registerProtocol('message', Message)
        self.registerProtocol('presence', Presence)

        self.registerHandler('iq',self._expectedIqHandler,system=True)

        self._expected = {}

        self._id = 0;

        self.lastErr = ''
        self.lastErrCode = 0

    def setMessageHandler(self, func, type='', chainOutput=False):
        """Back compartibility method"""
        print "WARNING! setMessageHandler(...) method is obsolette, use registerHandler('message',...) instead."
        return self.registerHandler('message', func, type, chained=chainOutput)

    def setPresenceHandler(self, func, type='', chainOutput=False):
        """Back compartibility method"""
        print "WARNING! setPresenceHandler(...) method is obsolette, use registerHandler('presence',...) instead."
        return self.registerHandler('presence', func, type, chained=chainOutput)

    def setIqHandler(self, func, type='', ns=''):
        """Back compartibility method"""
        print "WARNING! setIqHandler(...) method is obsolette, use registerHandler('iq',...) instead."
        return self.registerHandler('iq', func, type, ns)

    def header(self):
        self.DEBUG("stream: sending initial header",DBG_INIT)
        str = u"<?xml version='1.0' encoding='UTF-8' ?>   \
                <stream:stream to='%s' xmlns='%s'" % ( self._host,
                                                       self._namespace )

        if self._outgoingID: str = str + " id='%s' " % self._outgoingID
        str = str + " xmlns:stream='http://etherx.jabber.org/streams'>"
        self.send(str)
        self.process(timeout)

    def send(self, what):
        """Sends a jabber protocol element (Node) to the server"""
        xmlstream.Client.write(self,ustr(what))

    def _expectedIqHandler(self, conn, iq_obj):
        if iq_obj.getAttr('id') and \
           self._expected.has_key(iq_obj.getAttr('id')):
            self._expected[iq_obj.getAttr('id')] = iq_obj
            raise NodeProcessed('No need for further Iq processing.')

    def dispatch(self,stanza):
        """Called internally when a 'protocol element' is received.
           Builds the relevant jabber.py object and dispatches it
           to a relevant function or callback."""
        name=stanza.getName()
        if not self.handlers.has_key(name):
            self.DEBUG("whats a tag -> " + name,DBG_NODE_UNKNOWN)
            name='unknown'
        else:
            self.DEBUG("Got %s stanza"%name, DBG_NODE)

        stanza=self.handlers[name][type](node=stanza)

        typ=stanza.getType()
        if not typ: typ=''
        try:
            ns=stanza.getQuery()
            if not ns: ns=''
        except: ns=''
        self.DEBUG("dispatch called for: name->%s ns->%s"%(name,ns),DBG_DISPATCH)

        typns=typ+ns
        if not self.handlers[name].has_key(ns): ns=''
        if not self.handlers[name].has_key(typ): typ=''
        if not self.handlers[name].has_key(typns): typns=''

        chain=[]
        for key in ['default',typ,ns,typns]: # we will use all handlers: from very common to very particular
            if key: chain += self.handlers[name][key]

        output=''
        user=True
        for handler in chain:
            try:
                if user or handler['system']:
                    if handler['chain']: output=handler['func'](self,stanza,output)
                    else: handler['func'](self,stanza)
            except NodeProcessed: user=False

    def registerProtocol(self,tag_name,Proto):
        """Registers a protocol in protocol processing chain. You MUST register
           a protocol before you register any handler function for it.
           First parameter, that passed to this function is the tag name that
           belongs to all protocol elements. F.e.: message, presence, iq, xdb, ...
           Second parameter is the [ancestor of] Protocol class, which instance will
           built from the received node with call

                if received_packet.getName()==tag_name:
                    stanza = Proto(node = received_packet)
        """
        self.handlers[tag_name]={type:Proto, 'default':[]}

    def registerHandler(self,name,handler,type='',ns='',chained=False, makefirst=False, system=False):
        """Sets the callback func for processing incoming stanzas.
           Multiple callback functions can be set which are called in
           succession. Callback can optionally raise an NodeProcessed error to
           stop stanza from further processing. A type and namespace attributes can
           also be optionally passed so the callback is only called when a stanza of
           this type is received. Namespace attribute MUST be omitted if you
           registering an Iq processing handler.

           If 'chainOutput' is set to False (the default), the given function
           should be defined as follows:

                def myCallback(c, p)

           Where the first parameter is the Client object, and the second
           parameter is the [ancestor of] Protocol object representing the stanza
           which was received.

           If 'chainOutput' is set to True, the output from the various
           handler functions will be chained together.  In this case,
           the given callback function should be defined like this:

                def myCallback(c, p, output)

           Where 'output' is the value returned by the previous
           callback function.  For the first callback routine, 'output' will be
           set to an empty string.

           'makefirst' argument gives you control over handler prioriy in its type
           and namespace scope. Note that handlers for particular type or namespace always
           have lower priority that common handlers.
        """
        if not type and not ns: type='default'
        if not self.handlers[name].has_key(type+ns): self.handlers[name][type+ns]=[]
        if makefirst: self.handlers[name][type+ns].insert({'chain':chained,'func':handler,'system':system})
        else: self.handlers[name][type+ns].append({'chain':chained,'func':handler,'system':system})

    def setDisconnectHandler(self, func):
        """Set the callback for a disconnect.
           The given function will be called with a single parameter (the
           connection object) when the connection is broken unexpectedly (eg,
           in response to sending badly formed XML).  self.lastErr and
           self.lastErrCode will be set to the error which caused the
           disconnection, if any.
        """
        self.disconnectHandler = func

    ## functions for sending element with ID's ##

    def waitForResponse(self, ID, timeout=timeout):
        """Blocks untils a protocol element with the given id is received.
           If an error is received, waitForResponse returns None and
           self.lastErr and self.lastErrCode is set to the received error.  If
           the operation times out (which only happens if a timeout value is
           given), waitForResponse will return None and self.lastErr will be
           set to "Timeout".
           Changed default from timeout=0 to timeout=300 to avoid hangs in
           scripts and such.
           If you _really_ want no timeout, just set it to 0"""
        ID = ustr(ID)
        self._expected[ID] = None
        has_timed_out = False

        abort_time = time.time() + timeout
        if timeout:
            self.DEBUG("waiting with timeout:%s for %s" % (timeout,ustr(ID)),DBG_NODE_IQ)
        else:
            self.DEBUG("waiting for %s" % ustr(ID),DBG_NODE_IQ)

        while (not self._expected[ID]) and not has_timed_out:
            if not self.process(0.2): return None
            if timeout and (time.time() > abort_time):
                has_timed_out = True
        if has_timed_out:
            self.lastErr = "Timeout"
            return None
        response = self._expected[ID]
        del self._expected[ID]
        if response.getErrorCode():
            self.lastErr     = response.getError()
            self.lastErrCode = response.getErrorCode()
            return None
        return response

    def SendAndWaitForResponse(self, obj, ID=None, timeout=timeout):
        """Sends a protocol element object and blocks until a response with
           the same ID is received.  The received protocol object is returned
           as the function result. """
        if ID is None :
            ID = obj.getID()
            if ID is None:
                ID = self.getAnID()
                obj.setID(ID)
        ID = ustr(ID)
        self.send(obj)
        return self.waitForResponse(ID,timeout)

    def getAnID(self):
        """Returns a unique ID"""
        self._id = self._id + 1
        return ustr(self._id)

#############################################################################

class Client(Connection):
    """Class for managing a client connection to a jabber server."""
    def __init__(self, host, port=5222, debug=[], log=False,
                 connection=xmlstream.TCP, hostIP=None, proxy=None):

        Connection.__init__(self, host, port, NS_CLIENT, debug, log,
                            connection=connection, hostIP=hostIP, proxy=proxy)

        self.registerHandler('iq',self._IqRosterManage,'result',NS_ROSTER,system=True)
        self.registerHandler('iq',self._IqRosterManage,'set',NS_ROSTER,system=True)
        self.registerHandler('iq',self._IqRegisterResult,'result',NS_REGISTER,system=True)
        self.registerHandler('iq',self._IqAgentsResult,'result',NS_AGENTS,system=True)
        self.registerHandler('presence',self._presenceHandler,system=True)

        self._roster = Roster()
        self._agents = {}
        self._reg_info = {}
        self._reg_agent = ''

    def disconnect(self):
        """Safely disconnects from the connected server"""
        self.send(Presence(type='unavailable'))
        xmlstream.Client.disconnect(self)

    def sendPresence(self,type=None,priority=None,show=None,status=None):
        """Sends a presence protocol element to the server.
           Used to inform the server that you are online"""
        self.send(Presence(type=type,priority=priority,show=show,status=status))

    sendInitPresence=sendPresence

    def _presenceHandler(self, conn, pres_obj):
        who = ustr(pres_obj.getFrom())
        type = pres_obj.getType()
        self.DEBUG("presence type is %s" % type,DBG_NODE_PRESENCE)
        if type == 'available' or not type:
            self.DEBUG("roster setting %s to online" % who,DBG_NODE_PRESENCE)
            self._roster._setOnline(who,'online')
        elif type == 'unavailable':
            self.DEBUG("roster setting %s to offline" % who,DBG_NODE_PRESENCE)
            self._roster._setOnline(who,'offline')
        self._roster._setShow(who,pres_obj.getShow())
        self._roster._setStatus(who,pres_obj.getStatus())

    def _IqRosterManage(self, conn, iq_obj):
        "NS_ROSTER and type in [result,set]"
        for item in iq_obj.getQueryNode().getChildren():
            jid  = item.getAttr('jid')
            name = item.getAttr('name')
            sub  = item.getAttr('subscription')
            ask  = item.getAttr('ask')

            groups = []
            for group in item.getTags("group"):
                groups.append(group.getData())

            if jid:
                if sub == 'remove' or sub == 'none':
                    self._roster._remove(jid)
                else:
                    self._roster._set(jid=jid, name=name,
                                      groups=groups, sub=sub,
                                      ask=ask)
            else:
                self.DEBUG("roster - jid not defined ?",DBG_NODE_IQ)

    def _IqRegisterResult(self, conn, iq_obj):
        "NS_REGISTER and type==result"
        self._reg_info = {}
        for item in iq_obj.getQueryNode().getChildren():
            self._reg_info[item.getName()] = item.getData()

    def _IqAgentsResult(self, conn, iq_obj):
        "NS_AGENTS and type==result"
        self.DEBUG("got agents result",DBG_NODE_IQ)
        self._agents = {}
        for agent in iq_obj.getQueryNode().getChildren():
            if agent.getName() == 'agent': ## hmmm
                self._agents[agent.getAttr('jid')] = {}
                for info in agent.getChildren():
                    self._agents[agent.getAttr('jid')][info.getName()] = info.getData()

    def auth(self,username,passwd,resource):
        """Authenticates and logs in to the specified jabber server
           Automatically selects the 'best' authentication method
           provided by the server.
           Supports plain text, digest and zero-k authentication.

           Returns True if the login was successful, False otherwise.
        """
        auth_get_iq = Iq(type='get')
        auth_get_iq.setID('auth-get')
        q = auth_get_iq.setQuery(NS_AUTH)
        q.insertTag('username').insertData(username)
        self.send(auth_get_iq)

        auth_response = self.waitForResponse("auth-get")
        if auth_response == None:
            return False # Error
        else:
            auth_ret_node = auth_response

        auth_ret_query = auth_ret_node.getTag('query')
        self.DEBUG("auth-get node arrived!",(DBG_INIT,DBG_NODE_IQ))

        auth_set_iq = Iq(type='set')
        auth_set_iq.setID('auth-set')

        q = auth_set_iq.setQuery(NS_AUTH)
        q.insertTag('username').insertData(username)
        q.insertTag('resource').insertData(resource)

        if auth_ret_query.getTag('token'):

            token = auth_ret_query.getTag('token').getData()
            seq = auth_ret_query.getTag('sequence').getData()
            self.DEBUG("zero-k authentication supported",(DBG_INIT,DBG_NODE_IQ))
            hash = sha.new(sha.new(passwd).hexdigest()+token).hexdigest()
            for foo in xrange(int(seq)): hash = sha.new(hash).hexdigest()
            q.insertTag('hash').insertData(hash)

        elif auth_ret_query.getTag('digest'):

            self.DEBUG("digest authentication supported",(DBG_INIT,DBG_NODE_IQ))
            digest = q.insertTag('digest')
            digest.insertData(sha.new(
                self.getIncomingID() + passwd).hexdigest() )
        else:
            self.DEBUG("plain text authentication supported",(DBG_INIT,DBG_NODE_IQ))
            q.insertTag('password').insertData(passwd)

        iq_result = self.SendAndWaitForResponse(auth_set_iq)

        if iq_result==None:
             return False
        if iq_result.getError() is None:
            return True
        else:
           self.lastErr     = iq_result.getError()
           self.lastErrCode = iq_result.getErrorCode()
           # raise error(iq_result.getError()) ?
           return False
        return True

    ## Roster 'helper' func's - also see the Roster class ##

    def requestRoster(self):
        """Requests the roster from the server and returns a
           Roster() class instance."""
        rost_iq = Iq(type='get')
        rost_iq.setQuery(NS_ROSTER)
        self.SendAndWaitForResponse(rost_iq)
        self.DEBUG("got roster response",DBG_NODE_IQ)
        self.DEBUG("roster -> %s" % ustr(self._roster),DBG_NODE_IQ)
        return self._roster


    def getRoster(self):
        """Returns the current Roster() class instance. Does
           not contact the server."""
        return self._roster


    def addRosterItem(self, jid):
        """ Send off a request to subscribe to the given jid.
        """
        self.send(Presence(to=jid, type="subscribe"))


    def updateRosterItem(self, jid, name=None, groups=None):
        """ Update the information stored in the roster about a roster item.

            'jid' is the Jabber ID of the roster entry; 'name' is the value to
            set the entry's name to, and 'groups' is a list of groups to which
            this roster entry can belong.  If either 'name' or 'groups' is not
            specified, that value is not updated in the roster.
        """
        iq = Iq(type='set')
        item = iq.setQuery(NS_ROSTER).insertTag('item')
        item.putAttr('jid', ustr(jid))
        if name != None: item.putAttr('name', name)
        if groups != None:
            for group in groups:
                item.insertTag('group').insertData(group)
        dummy = self.SendAndWaitForResponse(iq) # Do we need to wait??


    def removeRosterItem(self,jid):
        """Removes an item with Jabber ID jid from both the
           server's roster and the local internal Roster()
           instance"""
        rost_iq = Iq(type='set')
        q = rost_iq.setQuery(NS_ROSTER).insertTag('item')
        q.putAttr('jid', ustr(jid))
        q.putAttr('subscription', 'remove')
        self.SendAndWaitForResponse(rost_iq)
        return self._roster

    ## Registration 'helper' funcs ##

    def requestRegInfo(self,agent=''):
        """Requests registration info from the server.
           Returns the Iq object received from the server."""
        if agent: agent = agent + '.'
        self._reg_info = {}
        reg_iq = Iq(type='get', to = agent + self._host)
        reg_iq.setQuery(NS_REGISTER)
        self.DEBUG("Requesting reg info from %s%s:" % (agent, self._host), DBG_NODE_IQ)
        self.DEBUG(ustr(reg_iq),DBG_NODE_IQ)
        return self.SendAndWaitForResponse(reg_iq)


    def getRegInfo(self):
        """Returns a dictionary of fields requested by the server for a
           registration attempt.  Each dictionary entry maps from the name of
           the field to the field's current value (either as returned by the
           server or set programmatically by calling self.setRegInfo(). """
        return self._reg_info


    def setRegInfo(self,key,val):
        """Sets a name/value attribute. Note: requestRegInfo must be
           called before setting."""
        self._reg_info[key] = val


    def sendRegInfo(self, agent=None):
        """Sends the populated registration dictionary back to the server"""
        if agent: agent = agent + '.'
        if agent is None: agent = ''
        reg_iq = Iq(to = agent + self._host, type='set')
        q = reg_iq.setQuery(NS_REGISTER)
        for info in self._reg_info.keys():
            q.insertTag(info).putData(self._reg_info[info])
        return self.SendAndWaitForResponse(reg_iq)


    def deregister(self, agent=None):
        """ Send off a request to deregister with the server or with the given
            agent.  Returns True if successful, else False.

            Note that you must be authorised before attempting to deregister.
        """
        if agent:
            agent = agent + '.'
            self.send(Presence(to=agent+self._host,type='unsubscribed'))       # This is enough f.e. for icqv7t or jit
        if agent is None: agent = ''
        q = self.requestRegInfo()
        kids = q.getQueryPayload()
        keyTag = kids.getTag("key")

        iq = Iq(to=agent+self._host, type="set")
        iq.setQuery(NS_REGISTER)
        iq.setQueryNode("")
        q = iq.getQueryNode()
        if keyTag != None:
            q.insertXML("<key>" + keyTag.getData() + "</key>")
        q.insertXML("<remove/>")

        result = self.SendAndWaitForResponse(iq)

        if result == None:
            return False
        elif result.getType() == "result":
            return True
        else:
            return False

    ## Agent helper funcs ##

    def requestAgents(self):
        """Requests a list of available agents.  Returns a dictionary
           containing information about each agent; each entry in the
           dictionary maps the agent's JID to a dictionary of attributes
           describing what that agent can do (as returned by the
           NS_AGENTS query)."""
        self._agents = {}
        agents_iq = Iq(type='get')
        agents_iq.setQuery(NS_AGENTS)
        self.SendAndWaitForResponse(agents_iq)
        self.DEBUG("agents -> %s" % ustr(self._agents),DBG_NODE_IQ)
        return self._agents

    def _discover(self,ns,jid,node=None):
        iq=Iq(to=jid,type='get',query=ns)
        if node: iq.putAttr('node',node)
        rep=self.SendAndWaitForResponse(iq)
        if rep: return rep.getQueryPayload()

    def discoverItems(self,jid,node=None):
        """ According to JEP-0030: jid is mandatory, name, node, action is optional. """
        ret=[]
        for i in self._discover(NS_P_DISC_ITEMS,jid,node):
            ret.append(i.attrs)
        return ret

    def discoverInfo(self,jid,node=None):
        """ According to JEP-0030:
            For identity: category, name is mandatory, type is optional.
            For feature: var is mandatory"""
        identities , features = [] , []
        for i in self._discover(NS_P_DISC_INFO,jid,node):
            if i.getName()=='identity': identities.append(i.attrs)
            elif i.getName()=='feature': features.append(i.getAttr('var'))
        return identities , features

#############################################################################

class Protocol(xmlstream.Node):
    """Base class for jabber 'protocol elements' - messages, presences and iqs.
       Implements methods that are common to all these"""
    def __init__(self, name=None, to=None, type=None, attrs=None, frm=None, payload=[], node=None):
        if not attrs: attrs={}
        if to: attrs['to']=to
        if frm: attrs['from']=frm
        if type: attrs['type']=type
        self._node=self
        xmlstream.Node.__init__(self, tag=name, attrs=attrs, payload=payload, node=node)

    def asNode(self):
        """Back compartibility method"""
        print 'WARNING! "asNode()" method is obsolette, use Protocol object as Node object instead.'
        return self

    def getError(self):
        """Returns the error string, if any"""
        try: return self.getTag('error').getData()
        except: return None


    def getErrorCode(self):
        """Returns the error code, if any"""
        try: return self.getTag('error').getAttr('code')
        except: return None


    def setError(self,val,code):
        """Sets an error string and code"""
        err = self.getTag('error')
        if not err:
            err = self.insertTag('error')
        err.putData(val)
        err.putAttr('code',str(code))


    def __repr__(self):
        return self.__str__()


    def getTo(self):
        """Returns the 'to' attribute as a JID object."""
        try: return JID(self.getAttr('to'))
        except: return None


    def getFrom(self):
        """Returns the 'from' attribute as a JID object."""
        try: return JID(self.getAttr('from'))
        except: return None


    def getType(self):
        """Returns the 'type' attribute of the protocol element."""
        try: return self.getAttr('type')
        except: return None


    def getID(self):
        """Returns the 'id' attribute of the protocol element."""
        try: return self.getAttr('id')
        except: return None


    def setTo(self,val):
        """Sets the 'to' element to the given JID."""
        self.putAttr('to', ustr(val))


    def setFrom(self,val):
        """Sets the 'from' element to the given JID."""
        self.putAttr('from', ustr(val))


    def setType(self,val):
        """Sets the 'type' attribute of the protocol element"""
        self.putAttr('type', val)


    def setID(self,val):
        """Sets the ID of the protocol element"""
        self.putAttr('id', val)


    def getX(self,index=0):
        """Returns the x namespace, optionally passed an index if there are
           multiple tags."""
        try: return self.getXNodes()[index].namespace
        except: return None


    def setX(self,namespace,index=0):
        """Sets the name space of the x tag. It also creates the node
           if it doesn't already exist."""
        x = self.getTag('x',index)
        if not x: x = self.insertTag('x')
        x.setNamespace(namespace)
        return x


    def setXPayload(self, payload, namespace=''):
        """Sets the Child of an 'x' tag. Can be a Node instance or an
           XML document"""
        x = self.setX(namespace)

        if type(payload) == type('') or type(payload) == type(u''):
                payload = xmlstream.NodeBuilder(payload).getDom()

        x.kids = [] # should be a method for this realy
        x.insertNode(payload)


    def getXPayload(self, val=None):
        """Returns the x tags' payload as a list of Node instances."""
        nodes = []
        if val is not None:
            if type(val) == type(""):
                for xnode in self.getTags('x'):
                    if xnode.getNamespace() == val: nodes.append(xnode.kids[0])
                return nodes
            else:
                try: return self.getTags('x')[val].kids[0]
                except: return None

        for xnode in self.getTags('x'):
            nodes.append(xnode.kids[0])
        return nodes


    def getXNode(self, val=None):
        """Returns the x Node instance. If there are multiple tags
           the first Node is returned. For multiple X nodes use getXNodes
           or pass an index integer value or namespace string to getXNode
           and if a match is found it will be returned."""
        if val is not None:
            nodes = []
            if type(val) == type(""):
                for xnode in self.getTags('x'):
                    if xnode.getNamespace() == val: nodes.append(xnode)
                return nodes
            else:
                try: return self.getTags('x')[val]
                except: return None
        else:
            try: return self.getTag('x')
            except: return None

    def getXNodes(self):
        """Returns a list of X nodes."""
        return self.getTags('x')

    def setXNode(self, val=''):
        """Sets the x tag's data to the given textual value."""
        self.insertTag('x').putData(val)

    def fromTo(self):
        """Swaps the element's from and to attributes.
           Note that this is only useful for writing components; if you are
           writing a Jabber client you shouldn't use this, because the Jabber
           server will set the 'from' field automatically."""
        tmp = self.getTo()
        self.setTo(self.getFrom())
        self.setFrom(tmp)

#############################################################################

class Message(Protocol):
    """Builds on the Protocol class to provide an interface for sending
       message protocol elements"""
    def __init__(self, to=None, body=None, type=None, subject=None, attrs=None, frm=None, payload=[], node=None):
        Protocol.__init__(self, 'message', to=to, type=type, attrs=attrs, frm=frm, payload=payload, node=node)
        if body: self.setBody(body)
        if subject: self.setSubject(subject)
        # examine x tag and set timestamp if pressent
        try: self.setTimestamp( self.getTag('x').getAttr('stamp') )
        except: self.setTimestamp()

    def getBody(self):
        """Returns the message body."""
        try: return self.getTag('body').getData()
        except: return None


    def getSubject(self):
        """Returns the message's subject."""
        try: return self.getTag('subject').getData()
        except: return None


    def getThread(self):
        """Returns the message's thread ID."""
        try: return self.getTag('thread').getData()
        except: return None


    def getTimestamp(self):
        return self.time_stamp


    def setBody(self,val):
        """Sets the message body text."""
        body = self.getTag('body')
        if body:
            body.putData(val)
        else:
            body = self.insertTag('body').putData(val)


    def setSubject(self,val):
        """Sets the message subject text."""
        subj = self.getTag('subject')
        if subj:
            subj.putData(val)
        else:
            self.insertTag('subject').putData(val)


    def setThread(self,val):
        """Sets the message thread ID."""
        thread = self.getTag('thread')
        if thread:
            thread.putData(val)
        else:
            self.insertTag('thread').putData(val)


    def setTimestamp(self,val=None):
        if not val:
            val = time.strftime( '%Y%m%dT%H:%M:%S', time.gmtime( time.time()))
        self.time_stamp = val


    def buildReply(self, reply_txt=''):
        """Returns a new Message object as a reply to itself.
           The reply message has the 'to', 'type' and 'thread' attributes
           automatically set."""
        m = Message(to=self.getFrom(), body=reply_txt)
        if not self.getType() == None:
            m.setType(self.getType())
        t = self.getThread()
        if t: m.setThread(t)
        return m

    def build_reply(self, reply_txt=''):
        print "WARNING: build_reply method is obsolette. Use buildReply instead."
        return self.buildReply(reply_txt)

#############################################################################

class Presence(Protocol):
    """Class for creating and managing jabber <presence> protocol
       elements"""
    def __init__(self, to=None, type=None, priority=None, show=None, status=None, attrs=None, frm=None, payload=[], node=None):
        Protocol.__init__(self, 'presence', to=to, type=type, attrs=attrs, frm=frm, payload=payload, node=node)
        if priority: self.setPriority(priority)
        if show: self.setShow(show)
        if status: self.setStatus(status)

    def getStatus(self):
        """Returns the presence status"""
        try: return self.getTag('status').getData()
        except: return None

    def getShow(self):
        """Returns the presence show"""
        try: return self.getTag('show').getData()
        except: return None

    def getPriority(self):
        """Returns the presence priority"""
        try: return self.getTag('priority').getData()
        except: return None

    def setShow(self,val):
        """Sets the presence show"""
        show = self.getTag('show')
        if show: show.putData(val)
        else: self.insertTag('show').putData(val)

    def setStatus(self,val):
        """Sets the presence status"""
        status = self.getTag('status')
        if status: status.putData(val)
        else: self.insertTag('status').putData(val)

    def setPriority(self,val):
        """Sets the presence priority"""
        pri = self.getTag('priority')
        if pri: pri.putData(val)
        else: self.insertTag('priority').putData(val)

#############################################################################

class Iq(Protocol):
    """Class for creating and managing jabber <iq> protocol
       elements"""
    def __init__(self, to=None, type=None, query=None, attrs=None, frm=None, payload=[], node=None):
        Protocol.__init__(self, 'iq', to=to, type=type, attrs=attrs, frm=frm, payload=payload, node=node)
        if query: self.setQuery(query)

    def _getTag(self,tag):
        try: return self.getTag(tag).namespace
        except: return None

    def _setTag(self,tag,namespace):
        q = self.getTag(tag)
        if q:
            q.namespace = namespace
        else:
            q = self.insertTag(tag)
            q.setNamespace(namespace)
        return q


    def getList(self):
        "returns the list namespace"
        return self._getTag('list')

    def setList(self,namespace):
        return self._setTag('list',namespace)


    def getQuery(self):
        "returns the query namespace"
        return self._getTag('query')

    def setQuery(self,namespace):
        """Sets a query's namespace, and inserts a query tag if
           one doesn't already exist.  The resulting query tag
           is returned as the function result."""
        return self._setTag('query',namespace)


    def setQueryPayload(self, payload, add=False):
        """Sets a Iq's query payload.  'payload' can be either a Node
           structure or a valid xml document. The query tag is automatically
           inserted if it doesn't already exist."""
        q = self.getQueryNode()

        if q is None:
            q = self.insertTag('query')

        if type(payload) == type('') or type(payload) == type(u''):
                payload = xmlstream.NodeBuilder(payload).getDom()

        if not add: q.kids = []
        q.insertNode(payload)


    def getQueryPayload(self):
        """Returns the query's payload as a Node list"""
        q = self.getQueryNode()
        if q: return q.kids

    def getQueryNode(self):
        """Returns any textual data contained by the query tag"""
        try: return self.getTag('query')
        except: return None

    def setQueryNode(self, val):
        """Sets textual data contained by the query tag"""
        q = self.getTag('query')
        if q:
            q.putData(val)
        else:
            self.insertTag('query').putData(val)

#############################################################################

class Roster:
    """A Class for simplifying roster management. Also tracks roster
       item availability."""
    def __init__(self):
        self._data = {}
        self._listener = None
        ## unused for now ... ##
        self._lut = { 'both':RS_SUB_BOTH,
                      'from':RS_SUB_FROM,
                      'to':RS_SUB_TO }


    def setListener(self, listener):
        """ Set a listener function to be called whenever the roster changes.

            The given function will be called whenever the contents of the
            roster changes in response to a received <presence> or <iq> packet.
            The listener function should be defined as follows:

                def listener(action, jid, info)

            'action' is a string indicating what type of change has occurred:

                "add"     A new item has been added to the roster.
                "update"  An existing roster item has been updated.
                "remove"  A roster entry has been removed.

            'jid' is the Jabber ID (as a string) of the affected roster entry.

            'info' is a dictionary containing the information that has been
            added or updated for this roster entry.  This dictionary may
            contain any combination of the following:

                "name"    The associated name of this roster entry.
                "groups"  A list of groups associated with this roster entry.
                "online"  The roster entry's "online" value ("online",
                          "offline" or "pending").
                "sub"     The roster entry's subscription value ("none",
                          "from", "to" or "both").
                "ask"     The roster entry's ask value, if any (None,
                          "subscribe", "unsubscribe").
                "show"    The roster entry's show value, if any (None, "away",
                          "chat", "dnd", "normal", "xa").
                "status"  The roster entry's current 'status' value, if
                          specified.
        """
        self._listener = listener


    def getStatus(self, jid): ## extended
        """Returns the 'status' value for a Roster item with the given jid."""
        jid = ustr(jid)
        if self._data.has_key(jid):
            return self._data[jid]['status']
        return None


    def getShow(self, jid):   ## extended
        """Returns the 'show' value for a Roster item with the given jid."""
        jid = ustr(jid)
        if self._data.has_key(jid):
            return self._data[jid]['show']
        return None


    def getOnline(self,jid):  ## extended
        """Returns the 'online' status for a Roster item with the given jid.
           """
        jid = ustr(jid)
        if self._data.has_key(jid):
            return self._data[jid]['online']
        return None


    def getSub(self,jid):
        """Returns the 'subscription' status for a Roster item with the given
           jid."""
        jid = ustr(jid)
        if self._data.has_key(jid):
            return self._data[jid]['sub']
        return None


    def getName(self,jid):
        """Returns the 'name' for a Roster item with the given jid."""
        jid = ustr(jid)
        if self._data.has_key(jid):
            return self._data[jid]['name']
        return None


    def getGroups(self,jid):
        """ Returns the lsit of groups associated with the given roster item.
        """
        jid = ustr(jid)
        if self._data.has_key(jid):
            return self._data[jid]['groups']
        return None


    def getAsk(self,jid):
        """Returns the 'ask' status for a Roster item with the given jid."""
        jid = ustr(jid)
        if self._data.has_key(jid):
            return self._data[jid]['ask']
        return None


    def getSummary(self):
        """Returns a summary of the roster's contents.  The returned value is a
           dictionary mapping the basic (no resource) JIDs to their current
           availability status (online, offline, pending). """
        to_ret = {}
        for jid in self._data.keys():
            to_ret[jid] = self._data[jid]['online']
        return to_ret


    def getJIDs(self):
        """Returns a list of JIDs stored within the roster.  Each entry in the
           list is a JID object."""
        to_ret = [];
        for jid in self._data.keys():
            to_ret.append(JID(jid))
        return to_ret


    def getRaw(self):
        """Returns the internal data representation of the roster."""
        return self._data


    def isOnline(self,jid):
        """Returns True if the given jid is online, False if not."""
        jid = ustr(jid)
        if self.getOnline(jid) != 'online':
            return False
        else:
            return True


    def _set(self,jid,name,groups,sub,ask):
        # meant to be called by actual iq tag
        """Used internally - private"""
        jid = ustr(jid) # just in case
        online = 'offline'
        if ask: online = 'pending'
        if self._data.has_key(jid): # update it
            self._data[jid]['name'] = name
            self._data[jid]['groups'] = groups
            self._data[jid]['ask'] = ask
            self._data[jid]['sub'] = sub
            if self._listener != None:
                self._listener("update", jid, {'name' : name,
                                               'groups' : groups,
                                               'sub' : sub, 'ask' : ask})
        else:
            self._data[jid] = { 'name': name, 'groups' : groups, 'ask': ask,
                                'sub': sub, 'online': online, 'status': None,
                                'show': None}
            if self._listener != None:
                self._listener("add", jid, {'name' : name, 'groups' : groups,
                                            'sub' : sub, 'ask' : ask,
                                            'online' : online})


    def _setOnline(self,jid,val):
        """Used internally - private"""
        jid = ustr(jid)
        if self._data.has_key(jid):
            self._data[jid]['online'] = val
            if self._listener != None:
                self._listener("update", jid, {'online' : val})
        else:                      ## fall back
            jid_basic = JID(jid).getStripped()
            if self._data.has_key(jid_basic):
                self._data[jid_basic]['online'] = val
                if self._listener != None:
                    self._listener("update", jid_basic, {'online' : val})


    def _setShow(self,jid,val):
        """Used internally - private"""
        jid = ustr(jid)
        if self._data.has_key(jid):
            self._data[jid]['show'] = val
            if self._listener != None:
                self._listener("update", jid, {'show' : val})
        else:                      ## fall back
            jid_basic = JID(jid).getStripped()
            if self._data.has_key(jid_basic):
                self._data[jid_basic]['show'] = val
                if self._listener != None:
                    self._listener("update", jid_basic, {'show' : val})


    def _setStatus(self,jid,val):
        """Used internally - private"""
        jid = ustr(jid)
        if self._data.has_key(jid):
            self._data[jid]['status'] = val
            if self._listener != None:
                self._listener("update", jid, {'status' : val})
        else:                      ## fall back
            jid_basic = JID(jid).getStripped()
            if self._data.has_key(jid_basic):
                self._data[jid_basic]['status'] = val
                if self._listener != None:
                    self._listener("update", jid_basic, {'status' : val})


    def _remove(self,jid):
        """Used internally - private"""
        if self._data.has_key(jid):
            del self._data[jid]
            if self._listener != None:
                self._listener("remove", jid, {})

#############################################################################

class JID:
    """A Simple class for managing jabber users id's """
    def __init__(self, jid='', node='', domain='', resource=''):
        if jid:
            if jid.find('@') == -1:
                self.node = ''
            else:
                bits = jid.split('@', 1)
                self.node = bits[0]
                jid = bits[1]

            if jid.find('/') == -1:
                self.domain = jid
                self.resource = ''
            else:
                self.domain, self.resource = jid.split('/', 1)
        else:
            self.node = node
            self.domain = domain
            self.resource = resource


    def __str__(self):
        jid_str = self.domain
        if self.node: jid_str = self.node + '@' + jid_str
        if self.resource: jid_str += '/' + self.resource
        return jid_str

    __repr__ = __str__


    def getNode(self):
        """Returns JID Node as string"""
        return self.node


    def getDomain(self):
        """Returns JID domain as string or None if absent"""
        return self.domain


    def getResource(self):
        """Returns JID resource as string or None if absent"""
        return self.resource


    def setNode(self,val):
        """Sets JID Node from string"""
        self.node = val


    def setDomain(self,val):
        """Sets JID domain from string"""
        self.domain = val


    def setResource(self,val):
        """Sets JID resource from string"""
        self.resource = val


    def getStripped(self):
        """Returns a JID string with no resource"""
        if self.node: return self.node + '@' + self.domain
        else: return self.domain

    def __eq__(self, other):
        """Returns whether this JID is identical to another one.
           The "other" can be a JID object or a string."""
        return str(self) == str(other)

#############################################################################

## component types

## Accept  NS_COMP_ACCEPT
## Connect NS_COMP_CONNECT
## Execute NS_COMP_EXECUTE

class Component(Connection):
    """docs to come soon... """
    def __init__(self, host, port, connection=xmlstream.TCP,
                 debug=[], log=False, ns=NS_COMP_ACCEPT, hostIP=None, proxy=None):
        Connection.__init__(self, host, port, namespace=ns, debug=debug,
                            log=log, connection=connection, hostIP=hostIP, proxy=proxy)
        self._auth_OK = False
        self.registerProtocol('xdb', XDB)


    def auth(self,secret):
        """will disconnect on failure"""
        self.send( u"<handshake id='1'>%s</handshake>"
                   % sha.new( self.getIncomingID() + secret ).hexdigest()
                  )
        while not self._auth_OK:
            self.DEBUG("waiting on handshake")
            self.process(1)

        return True


    def dispatch(self, root_node):
        """Catch the <handshake/> here"""
        if root_node.name == 'handshake': # check id too ?
            self._auth_OK = True
        Connection.dispatch(self, root_node)

#############################################################################

## component protocol elements

class XDB(Protocol):
    def __init__(self, attrs=None, type=None, frm=None, to=None, payload=[], node=None):
        Protocol.__init__(self, 'xdb', attrs=attrs, type=type, frm=frm, to=to, payload=payload, node=node)

#############################################################################

class Log(Protocol):
    ## eg: <log type='warn' from='component'>Hello Log File</log>
    def __init__(self, attrs=None, type=None, frm=None, to=None, payload=[], node=None):
        Protocol.__init__(self, 'log', attrs=attrs, type=type, frm=frm, to=to, payload=payload, node=node)

    def setBody(self,val):
        "Sets the log message text."
        self.getTag('log').putData(val)

    def getBody(self):
        "Returns the log message text."
        return self.getTag('log').getData()

#############################################################################

class Server:
    pass