| ... |
... |
@@ -59,7 +59,6 @@ from encodings.punycode import punycode_encode |
|
59
|
59
|
from functools import wraps
|
|
60
|
60
|
|
|
61
|
61
|
import nbxmpp
|
|
62
|
|
-from nbxmpp.stringprepare import nameprep
|
|
63
|
62
|
from nbxmpp.const import Role
|
|
64
|
63
|
from nbxmpp.const import ConnectionProtocol
|
|
65
|
64
|
from nbxmpp.const import ConnectionType
|
| ... |
... |
@@ -101,48 +100,11 @@ class InvalidFormat(Exception): |
|
101
|
100
|
pass
|
|
102
|
101
|
|
|
103
|
102
|
|
|
104
|
|
-def decompose_jid(jidstring):
|
|
105
|
|
- user = None
|
|
106
|
|
- server = None
|
|
107
|
|
- resource = None
|
|
108
|
|
-
|
|
109
|
|
- # Search for delimiters
|
|
110
|
|
- user_sep = jidstring.find('@')
|
|
111
|
|
- res_sep = jidstring.find('/')
|
|
112
|
|
-
|
|
113
|
|
- if user_sep == -1:
|
|
114
|
|
- if res_sep == -1:
|
|
115
|
|
- # host
|
|
116
|
|
- server = jidstring
|
|
117
|
|
- else:
|
|
118
|
|
- # host/resource
|
|
119
|
|
- server = jidstring[0:res_sep]
|
|
120
|
|
- resource = jidstring[res_sep + 1:]
|
|
121
|
|
- else:
|
|
122
|
|
- if res_sep == -1:
|
|
123
|
|
- # user@host
|
|
124
|
|
- user = jidstring[0:user_sep]
|
|
125
|
|
- server = jidstring[user_sep + 1:]
|
|
126
|
|
- else:
|
|
127
|
|
- if user_sep < res_sep:
|
|
128
|
|
- # user@host/resource
|
|
129
|
|
- user = jidstring[0:user_sep]
|
|
130
|
|
- server = jidstring[user_sep + 1:user_sep + (res_sep - user_sep)]
|
|
131
|
|
- resource = jidstring[res_sep + 1:]
|
|
132
|
|
- else:
|
|
133
|
|
- # server/resource (with an @ in resource)
|
|
134
|
|
- server = jidstring[0:res_sep]
|
|
135
|
|
- resource = jidstring[res_sep + 1:]
|
|
136
|
|
- return user, server, resource
|
|
137
|
|
-
|
|
138
|
103
|
def parse_jid(jidstring):
|
|
139
|
|
- """
|
|
140
|
|
- Perform stringprep on all JID fragments from a string and return the full
|
|
141
|
|
- jid
|
|
142
|
|
- """
|
|
143
|
|
- # This function comes from http://svn.twistedmatrix.com/cvs/trunk/twisted/words/protocols/jabber/jid.py
|
|
144
|
|
-
|
|
145
|
|
- return prep(*decompose_jid(jidstring))
|
|
|
104
|
+ try:
|
|
|
105
|
+ return validate_jid(jidstring)
|
|
|
106
|
+ except Exception as error:
|
|
|
107
|
+ raise InvalidFormat(error)
|
|
146
|
108
|
|
|
147
|
109
|
def idn_to_ascii(host):
|
|
148
|
110
|
"""
|
| ... |
... |
@@ -193,71 +155,6 @@ def parse_resource(resource): |
|
193
|
155
|
except UnicodeError:
|
|
194
|
156
|
raise InvalidFormat('Invalid character in resource.')
|
|
195
|
157
|
|
|
196
|
|
-def prep(user, server, resource):
|
|
197
|
|
- """
|
|
198
|
|
- Perform stringprep on all JID fragments and return the full jid
|
|
199
|
|
- """
|
|
200
|
|
- # This function comes from
|
|
201
|
|
- #http://svn.twistedmatrix.com/cvs/trunk/twisted/words/protocols/jabber/jid.py
|
|
202
|
|
-
|
|
203
|
|
- ip_address = False
|
|
204
|
|
-
|
|
205
|
|
- try:
|
|
206
|
|
- socket.inet_aton(server)
|
|
207
|
|
- ip_address = True
|
|
208
|
|
- except socket.error:
|
|
209
|
|
- pass
|
|
210
|
|
-
|
|
211
|
|
- if not ip_address and hasattr(socket, 'inet_pton'):
|
|
212
|
|
- try:
|
|
213
|
|
- socket.inet_pton(socket.AF_INET6, server.strip('[]'))
|
|
214
|
|
- server = '[%s]' % server.strip('[]')
|
|
215
|
|
- ip_address = True
|
|
216
|
|
- except (socket.error, ValueError):
|
|
217
|
|
- pass
|
|
218
|
|
-
|
|
219
|
|
- if not ip_address:
|
|
220
|
|
- if server is not None:
|
|
221
|
|
- if server.endswith('.'): # RFC7622, 3.2
|
|
222
|
|
- server = server[:-1]
|
|
223
|
|
- if not server or len(server.encode('utf-8')) > 1023:
|
|
224
|
|
- raise InvalidFormat(_('Server must be between 1 and 1023 bytes'))
|
|
225
|
|
- try:
|
|
226
|
|
- server = nameprep.prepare(server)
|
|
227
|
|
- except UnicodeError:
|
|
228
|
|
- raise InvalidFormat(_('Invalid character in hostname.'))
|
|
229
|
|
- else:
|
|
230
|
|
- raise InvalidFormat(_('Server address required.'))
|
|
231
|
|
-
|
|
232
|
|
- if user is not None:
|
|
233
|
|
- if not user or len(user.encode('utf-8')) > 1023:
|
|
234
|
|
- raise InvalidFormat(_('Username must be between 1 and 1023 bytes'))
|
|
235
|
|
- try:
|
|
236
|
|
- user = user.encode('UsernameCaseMapped').decode('utf-8')
|
|
237
|
|
- except UnicodeError:
|
|
238
|
|
- raise InvalidFormat(_('Invalid character in username.'))
|
|
239
|
|
- else:
|
|
240
|
|
- user = None
|
|
241
|
|
-
|
|
242
|
|
- if resource is not None:
|
|
243
|
|
- if not resource or len(resource.encode('utf-8')) > 1023:
|
|
244
|
|
- raise InvalidFormat(_('Resource must be between 1 and 1023 bytes'))
|
|
245
|
|
- try:
|
|
246
|
|
- resource = resource.encode('OpaqueString').decode('utf-8')
|
|
247
|
|
- except UnicodeError:
|
|
248
|
|
- raise InvalidFormat(_('Invalid character in resource.'))
|
|
249
|
|
- else:
|
|
250
|
|
- resource = None
|
|
251
|
|
-
|
|
252
|
|
- if user:
|
|
253
|
|
- if resource:
|
|
254
|
|
- return '%s@%s/%s' % (user, server, resource)
|
|
255
|
|
- return '%s@%s' % (user, server)
|
|
256
|
|
-
|
|
257
|
|
- if resource:
|
|
258
|
|
- return '%s/%s' % (server, resource)
|
|
259
|
|
- return server
|
|
260
|
|
-
|
|
261
|
158
|
def windowsify(s):
|
|
262
|
159
|
if os.name == 'nt':
|
|
263
|
160
|
return s.capitalize()
|