3 Jappix - An open social platform
4 These are the CAPS JS script for Jappix
6 -------------------------------------------------
10 Last revision: 26/08/11
14 // Returns an array of the Jappix disco#infos
15 function myDiscoInfos() {
16 var fArray = new Array(
37 NS_ACTIVITY + NS_NOTIFY,
39 NS_GEOLOC + NS_NOTIFY,
40 NS_URN_MBLOG + NS_NOTIFY,
41 NS_URN_INBOX + NS_NOTIFY,
70 // Gets the disco#infos of an entity
71 function getDiscoInfos(to, caps) {
74 logThis('No CAPS: ' + to, 2);
76 displayDiscoInfos(to, '');
81 // Get the stored disco infos
82 var xml = XMLFromString(getPersistent('caps', caps));
86 logThis('CAPS from cache: ' + to, 3);
88 displayDiscoInfos(to, xml);
93 logThis('CAPS from the network: ' + to, 3);
95 // Not stored: get the disco#infos
96 var iq = new JSJaCIQ();
100 iq.setQuery(NS_DISCO_INFO);
102 con.send(iq, handleDiscoInfos);
107 // Handles the disco#infos of an entity
108 function handleDiscoInfos(iq) {
109 if(!iq || (iq.getType() == 'error'))
112 // IQ received, get some values
113 var from = fullXID(getStanzaFrom(iq));
114 var query = iq.getQuery();
116 // Generate the CAPS-processing values
117 var identities = new Array();
118 var features = new Array();
119 var data_forms = new Array();
122 $(query).find('identity').each(function() {
123 var pCategory = $(this).attr('category');
124 var pType = $(this).attr('type');
125 var pLang = $(this).attr('xml:lang');
126 var pName = $(this).attr('name');
137 identities.push(pCategory + '/' + pType + '/' + pLang + '/' + pName);
141 $(query).find('feature').each(function() {
142 var pVar = $(this).attr('var');
144 // Add the current value to the array
150 $(query).find('x[xmlns=' + NS_XDATA + ']').each(function() {
151 // Initialize some stuffs
153 var sortVar = new Array();
155 // Add the form type field
156 $(this).find('field[var=FORM_TYPE] value').each(function() {
157 var cText = $(this).text();
160 pString += cText + '<';
163 // Add the var attributes into an array
164 $(this).find('field:not([var=FORM_TYPE])').each(function() {
165 var cVar = $(this).attr('var');
171 // Sort the var attributes
172 sortVar = sortVar.sort();
174 // Loop this sorted var attributes
176 // Initialize the value sorting
177 var sortVal = new Array();
179 // Append it to the string
180 pString += sortVar[i] + '<';
182 // Add each value to the array
183 $(this).find('field[var=' + sortVar[i] + '] value').each(function() {
184 sortVal.push($(this).text());
188 sortVal = sortVal.sort();
190 // Append the values to the string
192 pString += sortVal[j] + '<';
197 // Remove the undesired double '<' from the string
198 if(pString.match(/(.+)(<)+$/))
199 pString = pString.substring(0, pString.length - 1);
201 // Add the current string to the array
202 data_forms.push(pString);
207 var caps = processCaps(identities, features, data_forms);
209 // Get the XML string
210 var xml = xmlToString(query);
212 // Store the disco infos
213 setPersistent('caps', caps, xml);
215 // This is our server
216 if(from == getServer()) {
217 // Handle the features
220 logThis('Got our server CAPS', 3);
224 // Display the disco infos
225 displayDiscoInfos(from, xml);
227 logThis('Got CAPS: ' + from, 3);
231 // Displays the disco#infos everywhere needed for an entity
232 function displayDiscoInfos(from, xml) {
233 // Generate the chat path
234 var xid = bareXID(from);
236 // This comes from a private groupchat chat?
242 // Support indicators
243 var xhtml_im = false;
246 var receipts = false;
248 // Display the supported features
249 $(xml).find('feature').each(function() {
250 var current = $(this).attr('var');
253 if(current == NS_XHTML_IM)
256 // Out of Band Data (IQ)
257 if(current == NS_IQOOB)
260 // Out of Band Data (X)
261 if(current == NS_XOOB)
265 else if(current == NS_URN_RECEIPTS)
270 var path = $('#' + hash);
271 var message_area = path.find('.message-area');
272 var style = path.find('.chat-tools-style');
273 var file = path.find('.chat-tools-file');
279 // Remove the tooltip elements
281 style.find('.bubble-style').remove();
284 message_area.removeAttr('style')
285 .removeAttr('data-color')
286 .removeAttr('data-bold')
287 .removeAttr('data-italic')
288 .removeAttr('data-underline');
291 // Apply Out of Band Data
292 if(iq_oob || x_oob) {
297 file.attr('data-oob', 'iq');
299 file.attr('data-oob', 'x');
303 // Remove the tooltip elements
305 file.find('.bubble-style').remove();
308 file.removeAttr('data-oob');
313 message_area.attr('data-receipts', 'true');
315 message_area.removeAttr('data-receipts');
318 // Generates the CAPS hash
319 function processCaps(cIdentities, cFeatures, cDataForms) {
324 cIdentities = cIdentities.sort();
325 cFeatures = cFeatures.sort();
326 cDataForms = cDataForms.sort();
328 // Process the sorted identity string
329 for(a in cIdentities)
330 cString += cIdentities[a] + '<';
332 // Process the sorted feature string
334 cString += cFeatures[b] + '<';
336 // Process the sorted data-form string
338 cString += cDataForms[c] + '<';
340 // Process the SHA-1 hash
341 var cHash = b64_sha1(cString);
346 // Generates the Jappix CAPS hash
348 return processCaps(new Array('client/web//Jappix'), myDiscoInfos(), new Array());