forked from strophe/strophejs-plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjid.coffee
64 lines (51 loc) · 1.45 KB
/
jid.coffee
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
###
This program is distributed under the terms of the MIT license.
Copyright 2012 (c) Markus Kohlhase <[email protected]>
###
###
This is coffee-script fork of
https://github.com/astro/node-xmpp/blob/master/lib/xmpp/jid.js
###
toUnicode = punycode?.toUnicode or (a) -> a
class JID
constructor: (a, b, c) ->
if a and not b? and not c?
@parseJID a
else if b?
@setUser a
@setDomain b
@resource = c
else
throw new Error 'Argument error'
user : null
resource : null
domain : null
parseJID: (s) ->
if s.indexOf('@') >= 0
@setUser(s.substr(0, s.indexOf('@')))
s = s.substr(s.indexOf('@') + 1)
if (s.indexOf('/') >= 0)
@resource = s.substr(s.indexOf('/') + 1)
s = s.substr(0, s.indexOf('/'))
@setDomain s
toString: ->
s = @domain
s = @user + '@' + s if @user
s += '/' + @resource if @resource
s
# Convenience method to distinguish users
bare : ->
if @resource then new JID @user, @domain, null
else @
# Comparison function
equals: (other) ->
@user is other.user and
@domain is other.domain and
@resource is other.resource
# Setters that do stringprep normalization.
setUser: (user) -> @user = user and user.toLowerCase()
# http://xmpp.org/rfcs/rfc6122.html#addressing-domain
setDomain: (domain) ->
@domain = domain and
(domain.split(".").map(toUnicode).join(".")).toLowerCase()
window.JID = JID