]> git.mxchange.org Git - friendica-addons.git/blob - jappixmini/jappix/js/oob.js
Merge pull request #520 from rabuzarus/20180206_-_membersince_frio_support
[friendica-addons.git] / jappixmini / jappix / js / oob.js
1 /*
2
3 Jappix - An open social platform
4 These are the Out of Band Data JS scripts for Jappix
5
6 -------------------------------------------------
7
8 License: AGPL
9 Author: Vanaryon
10 Last revision: 27/08/11
11
12 */
13
14 // Sends an OOB request to someone
15 function sendOOB(to, type, url, desc) {
16         // IQ stanza?
17         if(type == 'iq') {
18                 // Get some values
19                 var id = hex_md5(genID() + to + url + desc);
20                 to = getHighestResource(to);
21                 
22                 // IQs cannot be sent to offline users
23                 if(!to)
24                         return;
25                 
26                 // Register the ID
27                 setDB('send/url', id, url);
28                 setDB('send/desc', id, desc);
29                 
30                 var aIQ = new JSJaCIQ();
31                 aIQ.setTo(fullXID(to));
32                 aIQ.setType('set');
33                 aIQ.setID(id);
34                 
35                 // Append the query content
36                 var aQuery = aIQ.setQuery(NS_IQOOB);
37                 aQuery.appendChild(aIQ.buildNode('url', {'xmlns': NS_IQOOB}, url));
38                 aQuery.appendChild(aIQ.buildNode('desc', {'xmlns': NS_IQOOB}, desc));
39                 
40                 con.send(aIQ);
41         }
42         
43         // Message stanza?
44         else {
45                 var aMsg = new JSJaCMessage();
46                 aMsg.setTo(bareXID(to));
47                 
48                 // Append the content
49                 aMsg.setBody(desc);
50                 var aX = aMsg.appendNode('x', {'xmlns': NS_XOOB});
51                 aX.appendChild(aMsg.buildNode('url', {'xmlns': NS_XOOB}, url));
52                 
53                 con.send(aMsg);
54         }
55         
56         logThis('Sent OOB request to: ' + to + ' (' + desc + ')');
57 }
58
59 // Handles an OOB request
60 function handleOOB(from, id, type, node) {
61         var xid = url = desc = '';
62         
63         // IQ stanza?
64         if(type == 'iq') {
65                 xid = fullXID(from);
66                 url = $(node).find('url').text();
67                 desc = $(node).find('desc').text();
68         }
69         
70         // Message stanza?
71         else {
72                 xid = bareXID(from);
73                 url = $(node).find('url').text();
74                 desc = $(node).find('body').text();
75         }
76         
77         // No desc?
78         if(!desc)
79                 desc = url;
80         
81         // Open a new notification
82         if(type && xid && url && desc)
83                 newNotification('send', xid, [xid, url, type, id, node], desc, hex_md5(xid + url + desc + id));
84 }
85
86 // Replies to an OOB request
87 function replyOOB(to, id, choice, type, node) {
88         // Not IQ type?
89         if(type != 'iq')
90                 return;
91         
92         // New IQ
93         var aIQ = new JSJaCIQ();
94         aIQ.setTo(to);
95         aIQ.setID(id);
96         
97         // OOB request accepted
98         if(choice == 'accept') {
99                 aIQ.setType('result');
100                 
101                 logThis('Accepted file request from: ' + to, 3);
102         }
103         
104         // OOB request rejected
105         else {
106                 aIQ.setType('error');
107                 
108                 // Append stanza content
109                 for(var i = 0; i < node.childNodes.length; i++)
110                         aIQ.getNode().appendChild(node.childNodes.item(i).cloneNode(true));
111                 
112                 // Append error content
113                 var aError = aIQ.appendNode('error', {'xmlns': NS_CLIENT, 'code': '406', 'type': 'modify'});
114                 aError.appendChild(aIQ.buildNode('not-acceptable', {'xmlns': NS_STANZAS}));
115                 
116                 logThis('Rejected file request from: ' + to, 3);
117         }
118         
119         con.send(aIQ);
120 }
121
122 // Wait event for OOB upload
123 function waitUploadOOB() {
124         // Append the wait icon
125         $('#page-engine .chat-tools-file:not(.mini) .tooltip-subitem *').hide();
126         $('#page-engine .chat-tools-file:not(.mini) .tooltip-subitem').append('<div class="wait wait-medium"></div>');
127         
128         // Lock the bubble
129         $('#page-engine .chat-tools-file:not(.mini)').addClass('mini');
130 }
131
132 // Success event for OOB upload
133 function handleUploadOOB(responseXML) {
134         // Data selector
135         var dData = $(responseXML).find('jappix');
136         
137         // Get the values
138         var fID = dData.find('id').text();
139         var fURL = dData.find('url').text();
140         var fDesc = dData.find('desc').text();
141         
142         // Get the OOB values
143         var oob_has;
144         
145         // No ID provided?
146         if(!fID)
147                 oob_has = ':has(.wait)';
148         else
149                 oob_has = ':has(#oob-upload input[value=' + fID + '])';
150         
151         var xid = $('#page-engine .page-engine-chan' + oob_has).attr('data-xid');
152         var oob_type = $('#page-engine .chat-tools-file' + oob_has).attr('data-oob');
153         
154         // Reset the file send tool
155         $('#page-engine .chat-tools-file' + oob_has).removeClass('mini');
156         $('#page-engine .bubble-file' + oob_has).remove();
157         
158         // Not available?
159         if($('#page-engine .chat-tools-file' + oob_has).is(':hidden') && (oob_type == 'iq')) {
160                 openThisError(4);
161                 
162                 // Remove the file we sent
163                 if(fURL)
164                         $.get(fURL + '&action=remove');
165         }
166         
167         // Everything okay?
168         else if(fURL && fDesc && !dData.find('error').size()) {
169                 // Send the OOB request
170                 sendOOB(xid, oob_type, fURL, fDesc);
171                 
172                 // Notify the sender
173                 newNotification('send_pending', xid, [xid, fURL, oob_type, '', ''], fDesc, hex_md5(fURL + fDesc + fID));
174                 
175                 logThis('File request sent.', 3);
176         }
177         
178         // Upload error?
179         else {
180                 openThisError(4);
181                 
182                 logThis('Error while sending the file: ' + dData.find('error').text(), 1);
183         }
184 }