]> git.mxchange.org Git - friendica-addons.git/blob - jappixmini/jappix/js/caps.js
Merge pull request #520 from rabuzarus/20180206_-_membersince_frio_support
[friendica-addons.git] / jappixmini / jappix / js / caps.js
1 /*
2
3 Jappix - An open social platform
4 These are the CAPS JS script for Jappix
5
6 -------------------------------------------------
7
8 License: AGPL
9 Author: Vanaryon
10 Last revision: 26/08/11
11
12 */
13
14 // Returns an array of the Jappix disco#infos
15 function myDiscoInfos() {
16         var fArray = new Array(
17                 NS_MUC,
18                 NS_MUC_USER,
19                 NS_MUC_ADMIN,
20                 NS_MUC_OWNER,
21                 NS_MUC_CONFIG,
22                 NS_DISCO_INFO,
23                 NS_DISCO_ITEMS,
24                 NS_PUBSUB_RI,
25                 NS_BOSH,
26                 NS_CAPS,
27                 NS_MOOD,
28                 NS_ACTIVITY,
29                 NS_TUNE,
30                 NS_GEOLOC,
31                 NS_NICK,
32                 NS_URN_ADATA,
33                 NS_URN_AMETA,
34                 NS_URN_MBLOG,
35                 NS_URN_INBOX,
36                 NS_MOOD + NS_NOTIFY,
37                 NS_ACTIVITY + NS_NOTIFY,
38                 NS_TUNE + NS_NOTIFY,
39                 NS_GEOLOC + NS_NOTIFY,
40                 NS_URN_MBLOG + NS_NOTIFY,
41                 NS_URN_INBOX + NS_NOTIFY,
42                 NS_URN_DELAY,
43                 NS_ROSTER,
44                 NS_ROSTERX,
45                 NS_HTTP_AUTH,
46                 NS_CHATSTATES,
47                 NS_XHTML_IM,
48                 NS_IPV6,
49                 NS_LAST,
50                 NS_PRIVATE,
51                 NS_REGISTER,
52                 NS_SEARCH,
53                 NS_COMMANDS,
54                 NS_VERSION,
55                 NS_XDATA,
56                 NS_VCARD,
57                 NS_URN_TIME,
58                 NS_URN_PING,
59                 NS_URN_ARCHIVE,
60                 NS_URN_AR_PREF,
61                 NS_URN_RECEIPTS,
62                 NS_PRIVACY,
63                 NS_IQOOB,
64                 NS_XOOB
65         );
66         
67         return fArray;
68 }
69
70 // Gets the disco#infos of an entity
71 function getDiscoInfos(to, caps) {
72         // No CAPS
73         if(!caps) {
74                 logThis('No CAPS: ' + to, 2);
75                 
76                 displayDiscoInfos(to, '');
77                 
78                 return false;
79         }
80         
81         // Get the stored disco infos
82         var xml = XMLFromString(getPersistent('caps', caps));
83         
84         // Yet stored
85         if(xml) {
86                 logThis('CAPS from cache: ' + to, 3);
87                 
88                 displayDiscoInfos(to, xml);
89                 
90                 return true;
91         }
92         
93         logThis('CAPS from the network: ' + to, 3);
94         
95         // Not stored: get the disco#infos
96         var iq = new JSJaCIQ();
97         
98         iq.setTo(to);
99         iq.setType('get');
100         iq.setQuery(NS_DISCO_INFO);
101         
102         con.send(iq, handleDiscoInfos);
103         
104         return true;
105 }
106
107 // Handles the disco#infos of an entity
108 function handleDiscoInfos(iq) {
109         if(!iq || (iq.getType() == 'error'))
110                 return;
111         
112         // IQ received, get some values
113         var from = fullXID(getStanzaFrom(iq));
114         var query = iq.getQuery();
115         
116         // Generate the CAPS-processing values
117         var identities = new Array();
118         var features = new Array();
119         var data_forms = new Array();
120         
121         // Identity values
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');
127                 
128                 if(!pCategory)
129                         pCategory = '';
130                 if(!pType)
131                         pType = '';
132                 if(!pLang)
133                         pLang = '';
134                 if(!pName)
135                         pName = '';
136                 
137                 identities.push(pCategory + '/' + pType + '/' + pLang + '/' + pName);
138         });
139         
140         // Feature values
141         $(query).find('feature').each(function() {
142                 var pVar = $(this).attr('var');
143                 
144                 // Add the current value to the array
145                 if(pVar)
146                         features.push(pVar);
147         });
148         
149         // Data-form values
150         $(query).find('x[xmlns=' + NS_XDATA + ']').each(function() {
151                 // Initialize some stuffs
152                 var pString = '';
153                 var sortVar = new Array();
154                 
155                 // Add the form type field
156                 $(this).find('field[var=FORM_TYPE] value').each(function() {
157                         var cText = $(this).text();
158                         
159                         if(cText)
160                                 pString += cText + '<';
161                 });
162                 
163                 // Add the var attributes into an array
164                 $(this).find('field:not([var=FORM_TYPE])').each(function() {
165                         var cVar = $(this).attr('var');
166                         
167                         if(cVar)
168                                 sortVar.push(cVar);
169                 });
170                 
171                 // Sort the var attributes
172                 sortVar = sortVar.sort();
173                 
174                 // Loop this sorted var attributes
175                 for(i in sortVar) {
176                         // Initialize the value sorting
177                         var sortVal = new Array();
178                         
179                         // Append it to the string
180                         pString += sortVar[i] + '<';
181                         
182                         // Add each value to the array
183                         $(this).find('field[var=' + sortVar[i] + '] value').each(function() {
184                                 sortVal.push($(this).text());
185                         });
186                         
187                         // Sort the values
188                         sortVal = sortVal.sort();
189                         
190                         // Append the values to the string
191                         for(j in sortVal)
192                                 pString += sortVal[j] + '<';
193                 }
194                 
195                 // Any string?
196                 if(pString) {
197                         // Remove the undesired double '<' from the string
198                         if(pString.match(/(.+)(<)+$/))
199                                 pString = pString.substring(0, pString.length - 1);
200                         
201                         // Add the current string to the array
202                         data_forms.push(pString);
203                 }
204         });
205         
206         // Process the CAPS
207         var caps = processCaps(identities, features, data_forms);
208         
209         // Get the XML string
210         var xml = xmlToString(query);
211         
212         // Store the disco infos
213         setPersistent('caps', caps, xml);
214         
215         // This is our server
216         if(from == getServer()) {
217                 // Handle the features
218                 handleFeatures(xml);
219                 
220                 logThis('Got our server CAPS', 3);
221         }
222         
223         else {
224                 // Display the disco infos
225                 displayDiscoInfos(from, xml);
226                 
227                 logThis('Got CAPS: ' + from, 3);
228         }
229 }
230
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);
235         
236         // This comes from a private groupchat chat?
237         if(isPrivate(xid))
238                 xid = from;
239         
240         hash = hex_md5(xid);
241         
242         // Support indicators
243         var xhtml_im = false;
244         var iq_oob = false;
245         var x_oob = false;
246         var receipts = false;
247         
248         // Display the supported features
249         $(xml).find('feature').each(function() {
250                 var current = $(this).attr('var');
251                 
252                 // xHTML-IM
253                 if(current == NS_XHTML_IM)
254                         xhtml_im = true;
255                 
256                 // Out of Band Data (IQ)
257                 if(current == NS_IQOOB)
258                         iq_oob = true;
259                 
260                 // Out of Band Data (X)
261                 if(current == NS_XOOB)
262                         x_oob = true;
263                 
264                 // Receipts
265                 else if(current == NS_URN_RECEIPTS)
266                         receipts = true;
267         });
268         
269         // Paths
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');
274         
275         // Apply xHTML-IM
276         if(xhtml_im)
277                 style.show();
278         else {
279                 // Remove the tooltip elements
280                 style.hide();
281                 style.find('.bubble-style').remove();
282                 
283                 // Reset the markers
284                 message_area.removeAttr('style')
285                             .removeAttr('data-color')
286                             .removeAttr('data-bold')
287                             .removeAttr('data-italic')
288                             .removeAttr('data-underline');
289         }
290         
291         // Apply Out of Band Data
292         if(iq_oob || x_oob) {
293                 file.show();
294                 
295                 // Set a marker
296                 if(iq_oob)
297                         file.attr('data-oob', 'iq');
298                 else
299                         file.attr('data-oob', 'x');
300         }
301         
302         else {
303                 // Remove the tooltip elements
304                 file.hide();
305                 file.find('.bubble-style').remove();
306                 
307                 // Reset the marker
308                 file.removeAttr('data-oob');
309         }
310         
311         // Apply receipts
312         if(receipts)
313                 message_area.attr('data-receipts', 'true');
314         else
315                 message_area.removeAttr('data-receipts');
316 }
317
318 // Generates the CAPS hash
319 function processCaps(cIdentities, cFeatures, cDataForms) {
320         // Initialize
321         var cString = '';
322         
323         // Sort the arrays
324         cIdentities = cIdentities.sort();
325         cFeatures = cFeatures.sort();
326         cDataForms = cDataForms.sort();
327         
328         // Process the sorted identity string
329         for(a in cIdentities)
330                 cString += cIdentities[a] + '<';
331         
332         // Process the sorted feature string
333         for(b in cFeatures)
334                 cString += cFeatures[b] + '<';
335         
336         // Process the sorted data-form string
337         for(c in cDataForms)
338                 cString += cDataForms[c] + '<';
339         
340         // Process the SHA-1 hash
341         var cHash = b64_sha1(cString);
342         
343         return cHash;
344 }
345
346 // Generates the Jappix CAPS hash
347 function myCaps() {
348         return processCaps(new Array('client/web//Jappix'), myDiscoInfos(), new Array());
349 }