This repository has been archived by the owner on Sep 23, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjid.coffee
186 lines (150 loc) · 4.05 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
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
unless window?
{ StringPrep, toUnicode } = require 'node-stringprep'
### Fallbacks ###
toUnicode ?= (require 'punycode')?.toUnicode or (a) -> a
StringPrep ?= class StringPrep
constructor: (operation) ->
@prepare = (v) ->
switch operation
when "nodeprep", "nameprep" then v.toLowerCase()
else v
###*
# JID implements
# - Xmpp addresses according to RFC6122
# - XEP-0106: JID Escaping
#
# @see http://tools.ietf.org/html/rfc6122#section-2
# @see http://xmpp.org/extensions/xep-0106.html
###
class JID
constructor: (a, b, c) ->
@local = @domain = @resource = null
if a and not (b or c)
@parseJID a
else if b
@setLocal a
@setDomain b
@setResource c
else
throw new Error 'Argument error'
parseJID: (s) ->
if s.indexOf('@') >= 0
@setLocal s.substr 0, s.indexOf '@'
s = s.substr s.indexOf('@') + 1
if s.indexOf('/') >= 0
@setResource s.substr s.indexOf('/') + 1
s = s.substr 0, s.indexOf '/'
@setDomain s
toString: (unescape) ->
s = @domain
if @local then s = @getLocal(unescape) + '@' + s
if @resource then s = s + '/' + @resource
s
###*
# Convenience method to distinguish users
###
bare: ->
if @resource
new JID @local, @domain, null
else
@
###*
# Comparison function
###
equals: (other) ->
(@local is other.local ) and
(@domain is other.domain ) and
(@resource is other.resource )
### Deprecated, use setLocal() [see RFC6122] ###
setUser: (user) -> @setLocal user
###*
# Setters that do stringprep normalization.
###
setLocal: (local, escape) ->
escape = escape or @detectEscape local
if escape
local = @escapeLocal local
@local = @user = local and @prep 'nodeprep', local
@
###*
# http://xmpp.org/rfcs/rfc6122.html#addressing-domain
###
setDomain: (domain) ->
@domain =
domain and @prep 'nameprep', domain.split('.').map(toUnicode).join '.'
@
setResource: (resource) ->
@resource = resource and @prep('resourceprep', resource)
@
getLocal: (unescape) ->
unescape = unescape or false
if unescape
@unescapeLocal(@local)
else
@local
prep: (operation, value) -> (new StringPrep operation).prepare value
### Deprecated, use getLocal() [see RFC6122] ###
getUser: -> @getLocal()
getDomain: -> @domain
getResource: -> @resource
detectEscape: (local) ->
return false unless local
# remove all escaped secquences
tmp = local.replace /\\20/g, ''
.replace /\\22/g, ''
.replace /\\26/g, ''
.replace /\\27/g, ''
.replace /\\2f/g, ''
.replace /\\3a/g, ''
.replace /\\3c/g, ''
.replace /\\3e/g, ''
.replace /\\40/g, ''
.replace /\\5c/g, ''
### detect if we have unescaped sequences ###
search = tmp.search /\\| |\"|\&|\'|\/|:|<|>|@/g
if search is -1
false
else
true
###*
# Escape the local part of a JID.
#
# @see http://xmpp.org/extensions/xep-0106.html
# @param String local local part of a jid
# @return An escaped local part
###
escapeLocal: (local) ->
return null if local is null
### jshint -W044 ###
local.replace /^\s+|\s+$/g, ''
.replace /\\/g, '\\5c'
.replace /\s/g, '\\20'
.replace /\"/g, '\\22'
.replace /\&/g, '\\26'
.replace /\'/g, '\\27'
.replace /\//g, '\\2f'
.replace /:/g, '\\3a'
.replace /</g, '\\3c'
.replace />/g, '\\3e'
.replace /@/g, '\\40'
.replace /\3a/g,'\\5c3a'
###*
# Unescape a local part of a JID.
#
# @see http://xmpp.org/extensions/xep-0106.html
# @param String local local part of a jid
# @return unescaped local part
###
unescapeLocal: (local) ->
return null if local is null
local.replace /\\20/g, ' '
.replace /\\22/g, '\"'
.replace /\\26/g, '&'
.replace /\\27/g, '\''
.replace /\\2f/g, '/'
.replace /\\3a/g, ':'
.replace /\\3c/g, '<'
.replace /\\3e/g, '>'
.replace /\\40/g, '@'
.replace /\\5c/g, '\\'
module.exports = JID