]>
code.delx.au - pymsnt/blob - src/tlib/jabber/jid.py
dccc7b6bf24320f9a79cf03e430817cbc3c11a8c
1 # -*- test-case-name: twisted.test.test_jabberjid -*-
3 # Twisted, the Framework of Your Internet
4 # Copyright (C) 2001 Matthew W. Lefkowitz
6 # This library is free software; you can redistribute it and/or
7 # modify it under the terms of version 2.1 of the GNU Lesser General Public
8 # License as published by the Free Software Foundation.
10 # This library is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 # Lesser General Public License for more details.
15 # You should have received a copy of the GNU Lesser General Public
16 # License along with this library; if not, write to the Free Software
17 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 from twisted
.internet
import reactor
, protocol
, defer
20 from tlib
import domish
21 from tlib
.jabber
.xmpp_stringprep
import nodeprep
, resourceprep
, nameprep
23 class InvalidFormat(Exception):
31 # Search for delimiters
32 user_sep
= jidstring
.find("@")
33 res_sep
= jidstring
.find("/")
41 server
= jidstring
[0:res_sep
]
42 resource
= jidstring
[res_sep
+ 1:] or None
46 user
= jidstring
[0:user_sep
] or None
47 server
= jidstring
[user_sep
+ 1:]
49 if user_sep
< res_sep
:
51 user
= jidstring
[0:user_sep
] or None
52 server
= jidstring
[user_sep
+ 1:user_sep
+ (res_sep
- user_sep
)]
53 resource
= jidstring
[res_sep
+ 1:] or None
55 # server/resource (with an @ in resource)
56 server
= jidstring
[0:res_sep
]
57 resource
= jidstring
[res_sep
+ 1:] or None
60 return prep(user
, server
, resource
)
63 def prep(user
, server
, resource
):
64 """ Stringprep, backported from Twisted 2.0 """
68 user
= nodeprep
.prepare(unicode(user
))
70 raise InvalidFormat
, "Invalid character in username"
75 raise InvalidFormat
, "Server address required."
78 server
= nameprep
.prepare(unicode(server
))
80 raise InvalidFormat
, "Invalid character in resource"
84 resource
= resourceprep
.prepare(unicode(resource
))
86 raise InvalidFormat
, "Invalid character in resource"
90 return (user
, server
, resource
)
97 # XXX: Ensure that stringprep'd jids map to same JID
98 if str in __internJIDs
:
99 return __internJIDs
[str]
102 __internJIDs
[str] = j
106 def __init__(self
, str = None, tuple = None):
107 assert (str or tuple)
110 user
, host
, res
= parse(str)
112 user
, host
, res
= tuple
120 return "%s@%s" % (self
.user
, self
.host
)
124 def userhostJID(self
):
126 if "_uhjid" not in self
.__dict
__:
127 self
._uhjid
= jid
.intern(self
.userhost())
135 return "%s@%s/%s" % (self
.user
, self
.host
, self
.resource
)
137 return "%s@%s" % (self
.user
, self
.host
)
140 return "%s/%s" % (self
.host
, self
.resource
)
144 def __eq__(self
, other
):
145 return (self
.user
== other
.user
and
146 self
.host
== other
.host
and
147 self
.resource
== other
.resource
)
149 def __ne__(self
, other
):
150 return not (self
.user
== other
.user
and
151 self
.host
== other
.host
and
152 self
.resource
== other
.resource
)