-
Notifications
You must be signed in to change notification settings - Fork 5
/
strophe.CAPS.coffee
153 lines (103 loc) · 4.19 KB
/
strophe.CAPS.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
# This plugin is distributed under the terms of the MIT licence.
# Please see the LICENCE file for details.
#
# Copyright (c) Markus Kohlhase, 2011
# File: strophe.caps.js
# A Strophe plugin for ( http://xmpp.org/extensions/xep-0115.html )
# NOTE: This plugin has following dependencies:
#
# - strophe.disco.js (by François de Metz)
# - sha1.js
Strophe.addConnectionPlugin 'caps', (->
conn = null
init = ( c ) ->
conn = c
Strophe.addNamespace 'CAPS', "http://jabber.org/protocol/caps"
if conn.disco is undefined
throw new Error "disco plugin required!"
if b64_sha1 is undefined
throw new Error "SHA-1 library required!"
conn.disco.addFeature Strophe.NS.CAPS
conn.disco.addFeature Strophe.NS.DISCO_INFO
# chek if there are identities
if conn.disco._identities.length is 0
conn.disco.addIdentity "client","pc", "strophejs", ""
addFeature = (feature) -> conn.disco.addFeature feature
removeFeature = (feature) -> conn.disco.removeFeature feature
sendPres = -> conn.send $pres().cnode( createCapsNode().tree() )
createCapsNode = ->
if conn.disco._identities.length > 0
node = conn.disco._identities[0].name || ""
else
node = dummyId.name
$build "c",
xmlns: Strophe.NS.CAPS
hash: "sha-1"
node: node
ver: generateVerificationString()
propertySort = (array, property) ->
array.sort (a,b) ->
if (a[property] > b[property]) then -1 else 1
generateVerificationString = ->
## Prepare
# copy the original identities
ids = []
ids.push(i) for i in conn.disco._identities
# copy the original features
features = []
features.push(k) for k in conn.disco._features
## Generate string
# 1. Initialize an empty string S.
S = ""
# 2. Sort the service discovery identities by category and then by type and
# then by xml:lang (if it exists), formatted as CATEGORY '/' [TYPE] '/' [LANG]
# '/' [NAME]. Note that each slash is included even if the LANG or NAME is not
# included (in accordance with XEP-0030, the category and type MUST be
# included.
propertySort( ids, "category" )
propertySort( ids, "type" )
propertySort( ids, "lang" )
# 3. For each identity, append the 'category/type/lang/name' to S, followed by
# the '<' character.
for key, id of ids
S += "#{id.category}/#{id.type}/#{id.lang}/#{id.name}<"
# 4. Sort the supported service discovery features.
features.sort()
# 5. For each feature, append the feature to S, followed by the '<' character.
for ns in features
S += "#{ns}<"
# 6. If the service discovery information response includes XEP-0128 data forms,
# sort the forms by the FORM_TYPE (i.e., by the XML character data of the
# <value/> element).
# not implemented yet
# 7. For each extended service discovery information form:
# a) Append the XML character data of the FORM_TYPE field's <value/> element,
# followed by the '<' character.
# not implemented yet
# b) Sort the fields by the value of the "var" attribute.
# not implemented yet
# c) For each field other than FORM_TYPE:
# i. Append the value of the "var" attribute, followed by the '<'
# character.
# not implemented yet
# ii. Sort values by the XML character data of the <value/> element.
# not implemented yet
# iii. For each <value/> element, append the XML character data, followed by
# the '<' character.
# not implemented yet
# 8. Ensure that S is encoded according to the UTF-8 encoding.
# not implemented yet
# 9. Compute the verification string by hashing S using the algorithm
# specified in the 'hash' attribute (e.g., SHA-1 as defined in RFC 3174 [19]).
# The hashed data MUST be generated with binary output and encoded using
# Base64 as specified in Section 4 of RFC 4648 [20] (note: the Base64 output
# MUST NOT include whitespace and MUST set padding bits to zero).
"#{b64_sha1 S}="
# Public API
init: init
removeFeature: removeFeature
addFeature: addFeature
sendPres: sendPres
generateVerificationString: generateVerificationString
createCapsNode: createCapsNode
)()