]> git.mxchange.org Git - friendica-addons.git/blob - jappixmini/jappix/js/groupchat.js
Merge pull request #520 from rabuzarus/20180206_-_membersince_frio_support
[friendica-addons.git] / jappixmini / jappix / js / groupchat.js
1 /*
2
3 Jappix - An open social platform
4 These are the groupchat JS scripts for Jappix
5
6 -------------------------------------------------
7
8 License: AGPL
9 Authors: Vanaryon, Maranda, Eric
10 Last revision: 28/08/11
11
12 */
13
14 // Displays the MUC admin elements
15 function displayMucAdmin(affiliation, id, xid, statuscode) {
16         // We must be in the "login" mode
17         if(isAnonymous())
18                 return;
19         
20         // We check if the user is a room owner or administrator to give him privileges
21         if(affiliation == 'owner' || affiliation == 'admin')
22                 $('#' + id + ' .tools-mucadmin').show();
23         
24         // We check if the room hasn't been yet created
25         if(statuscode == 201)
26                 openThisInfo(4);
27         
28         // We add the click event
29         $('#' + id + ' .tools-mucadmin').click(function() {
30                 openMucAdmin(xid, affiliation);
31         });
32 }
33
34 // Initializes a connection with a MUC groupchat
35 function getMUC(room, nickname, password) {
36         // Room hash
37         var hash = hex_md5(room);
38         
39         // Reset the elements
40         $('#' + hash + ' .muc-ask').remove();
41         $('#' + hash + ' .compose').show();
42         
43         // No nickname?
44         if(!nickname) {
45                 // Get some values
46                 if(!isAnonymous())
47                         nickname = getNick();
48                 else
49                         nickname = ANONYMOUS_NICK;
50                 
51                 // If the nickname could not be retrieved, ask it
52                 if(!nickname)
53                         generateMUCAsk('nickname', room, hash, nickname, password);
54         }
55         
56         // Got our nickname?
57         if(nickname) {
58                 // Get our general presence
59                 var show = getDB('presence-show', 1);
60                 var status = getDB('options', 'presence-status');
61         
62                 // Set my nick
63                 $('#' + hash).attr('data-nick', escape(nickname));
64         
65                 // Send the appropriate presence
66                 sendPresence(room + '/' + nickname, '', show, status, '', true, password, handleMUC);
67         }
68         
69         return false;
70 }
71
72 // Handles the MUC main elements
73 function handleMUC(presence) {
74         // We get the xml content
75         var xml = presence.getNode();
76         var from = fullXID(getStanzaFrom(presence));
77         var room = bareXID(from);
78         var nickname = thisResource(from);
79         var hash = hex_md5(room);
80         
81         // No ID: must fix M-Link bug
82         if(presence.getID() == null)
83                 presence.setID(1);
84         
85         logThis('First MUC presence: ' + from, 3);
86         
87         // Catch the errors
88         if(!handleError(xml)) {
89                 // Define some stuffs
90                 var muc_user = $(xml).find('x[xmlns=' + NS_MUC_USER + ']');
91                 var affiliation = muc_user.find('item').attr('affiliation');
92                 var statuscode = parseInt(muc_user.find('status').attr('code'));
93                 
94                 // Handle my presence
95                 handlePresence(presence);
96                 
97                 // Check if I am a room owner
98                 displayMucAdmin(affiliation, hash, room, statuscode);
99                 
100                 // Tell the MUC we can notify the incoming presences
101                 $(document).oneTime('15s', function() {
102                         $('#' + hash).attr('data-initial', 'true');
103                 });
104                 
105                 // Enable the chatting input
106                 $(document).oneTime(10, function() {
107                         $('#' + hash + ' .message-area').removeAttr('disabled').focus();
108                 });
109         }
110         
111         // A password is required
112         else if($(xml).find('error[type=auth] not-authorized').size())
113                 generateMUCAsk('password', room, hash, nickname);
114         
115         // There's a nickname conflict
116         else if($(xml).find('error[type=cancel] conflict').size())
117                 generateMUCAsk('nickname', room, hash);
118 }
119
120 // Generates a correct MUC asker
121 function generateMUCAsk(type, room, hash, nickname, password) {
122         // Generate the path to the elements
123         var path_to = '#' + hash + ' .muc-ask';
124         
125         // Define the label text
126         var label_text;
127         
128         switch(type) {
129                 case 'nickname':
130                         label_text = _e("Nickname");
131                         break;
132                 
133                 case 'password':
134                         label_text = _e("Password");
135                         break;
136         }
137         
138         // Create the HTML markup
139         $('#' + hash + ' .compose').hide();
140         
141         $('#' + hash).append(
142                 '<div class="muc-ask text">' + 
143                         '<label>' + label_text + '</label>' + 
144                         '<input class="focusable" type="text" />' + 
145                 '</div>'
146         );
147         
148         // When a key is pressed in the input
149         $(path_to + ' input').keyup(function(e) {
150                 var value_input = $(this).val();
151                 
152                 // Enter key pressed
153                 if((e.keyCode == 13) && value_input) {
154                         if(type == 'nickname')
155                                 nickname = value_input;
156                         else if(type == 'password')
157                                 password = value_input;
158                         
159                         return getMUC(room, nickname, password);
160                 }
161         });
162         
163         // Focus on the input
164         $(document).oneTime(10, function() {
165                 $(path_to + ' input').focus();
166         });
167 }
168
169 // Creates a new groupchat
170 function groupchatCreate(hash, room, chan, nickname, password) {
171         /* REF: http://xmpp.org/extensions/xep-0045.html */
172         
173         logThis('New groupchat: ' + room, 3);
174         
175         // Create the chat content
176         generateChat('groupchat', hash, room, chan);
177         
178         // Create the chat switcher
179         generateSwitch('groupchat', hash, room, chan);
180         
181         // The icons-hover functions
182         tooltipIcons(room, hash);
183         
184         // Click event on the add tool
185         $('#' + hash + ' .tools-add').click(function() {
186                 // Hide the icon (to tell the user all is okay)
187                 $(this).hide();
188                 
189                 // Add the groupchat to the user favorites
190                 addThisFavorite(room, chan);
191         });
192         
193         // Must show the add button?
194         if(!existDB('favorites', room))
195                 $('#' + hash + ' .tools-add').show();
196         
197         // The event handlers
198         var inputDetect = $('#' + hash + ' .message-area');
199         
200         // Focus event
201         inputDetect.focus(function() {
202                 chanCleanNotify(hash);
203         })
204         
205         // Blur event
206         inputDetect.blur(function() {
207                 resetAutocompletion(hash);
208         })
209         
210         // Lock to the input
211         inputDetect.keypress(function(e) {
212                 // Enter key
213                 if(e.keyCode == 13) {
214                         // Add a new line
215                         if(e.shiftKey)
216                                 inputDetect.val(inputDetect.val() + '\n');
217                         
218                         // Send the message
219                         else {
220                                 sendMessage(hash, 'groupchat');
221                                 
222                                 // Reset the composing database entry
223                                 setDB('chatstate', room, 'off');
224                         }
225                         
226                         return false;
227                 }
228                 
229                 // Tabulation key
230                 else if(e.keyCode == 9) {
231                         createAutocompletion(hash);
232                         
233                         return false;
234                 }
235                 
236                 // Reset the autocompleter
237                 else
238                         resetAutocompletion(hash);
239         });
240         
241         // Chatstate events
242         eventsChatState(inputDetect, room, hash);
243         
244         // Get the current muc informations and content
245         getMUC(room, nickname, password);
246 }
247
248 // Joins the defined groupchats
249 function joinConfGroupchats() {
250         // Nothing to join?
251         if(!GROUPCHATS_JOIN)
252                 return;
253         
254         // Values array
255         var muc_arr = [GROUPCHATS_JOIN];
256         var new_arr = [];
257         
258         // Try to split it
259         if(GROUPCHATS_JOIN.indexOf(',') != -1)
260                 muc_arr = GROUPCHATS_JOIN.split(',');
261         
262         for(i in muc_arr) {
263                 // Get the current value
264                 var muc_current = trim(muc_arr[i]);
265                 
266                 // No current value?
267                 if(!muc_current)
268                         continue;
269                 
270                 // Filter the current value
271                 muc_current = generateXID(muc_current, 'groupchat');
272                 
273                 // Add the current value
274                 if(!existArrayValue(new_arr, muc_current))
275                         new_arr.push(muc_current);
276         }
277         
278         // Join the chats
279         if(new_arr.length) {
280                 for(g in new_arr)
281                         checkChatCreate(new_arr[g], 'groupchat');
282         }
283 }