3 Jappix - An open social platform
4 These are the groupchat JS scripts for Jappix
6 -------------------------------------------------
9 Authors: Vanaryon, Maranda, Eric
10 Last revision: 28/08/11
14 // Displays the MUC admin elements
15 function displayMucAdmin(affiliation, id, xid, statuscode) {
16 // We must be in the "login" mode
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();
24 // We check if the room hasn't been yet created
28 // We add the click event
29 $('#' + id + ' .tools-mucadmin').click(function() {
30 openMucAdmin(xid, affiliation);
34 // Initializes a connection with a MUC groupchat
35 function getMUC(room, nickname, password) {
37 var hash = hex_md5(room);
40 $('#' + hash + ' .muc-ask').remove();
41 $('#' + hash + ' .compose').show();
49 nickname = ANONYMOUS_NICK;
51 // If the nickname could not be retrieved, ask it
53 generateMUCAsk('nickname', room, hash, nickname, password);
58 // Get our general presence
59 var show = getDB('presence-show', 1);
60 var status = getDB('options', 'presence-status');
63 $('#' + hash).attr('data-nick', escape(nickname));
65 // Send the appropriate presence
66 sendPresence(room + '/' + nickname, '', show, status, '', true, password, handleMUC);
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);
81 // No ID: must fix M-Link bug
82 if(presence.getID() == null)
85 logThis('First MUC presence: ' + from, 3);
88 if(!handleError(xml)) {
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'));
95 handlePresence(presence);
97 // Check if I am a room owner
98 displayMucAdmin(affiliation, hash, room, statuscode);
100 // Tell the MUC we can notify the incoming presences
101 $(document).oneTime('15s', function() {
102 $('#' + hash).attr('data-initial', 'true');
105 // Enable the chatting input
106 $(document).oneTime(10, function() {
107 $('#' + hash + ' .message-area').removeAttr('disabled').focus();
111 // A password is required
112 else if($(xml).find('error[type=auth] not-authorized').size())
113 generateMUCAsk('password', room, hash, nickname);
115 // There's a nickname conflict
116 else if($(xml).find('error[type=cancel] conflict').size())
117 generateMUCAsk('nickname', room, hash);
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';
125 // Define the label text
130 label_text = _e("Nickname");
134 label_text = _e("Password");
138 // Create the HTML markup
139 $('#' + hash + ' .compose').hide();
141 $('#' + hash).append(
142 '<div class="muc-ask text">' +
143 '<label>' + label_text + '</label>' +
144 '<input class="focusable" type="text" />' +
148 // When a key is pressed in the input
149 $(path_to + ' input').keyup(function(e) {
150 var value_input = $(this).val();
153 if((e.keyCode == 13) && value_input) {
154 if(type == 'nickname')
155 nickname = value_input;
156 else if(type == 'password')
157 password = value_input;
159 return getMUC(room, nickname, password);
163 // Focus on the input
164 $(document).oneTime(10, function() {
165 $(path_to + ' input').focus();
169 // Creates a new groupchat
170 function groupchatCreate(hash, room, chan, nickname, password) {
171 /* REF: http://xmpp.org/extensions/xep-0045.html */
173 logThis('New groupchat: ' + room, 3);
175 // Create the chat content
176 generateChat('groupchat', hash, room, chan);
178 // Create the chat switcher
179 generateSwitch('groupchat', hash, room, chan);
181 // The icons-hover functions
182 tooltipIcons(room, hash);
184 // Click event on the add tool
185 $('#' + hash + ' .tools-add').click(function() {
186 // Hide the icon (to tell the user all is okay)
189 // Add the groupchat to the user favorites
190 addThisFavorite(room, chan);
193 // Must show the add button?
194 if(!existDB('favorites', room))
195 $('#' + hash + ' .tools-add').show();
197 // The event handlers
198 var inputDetect = $('#' + hash + ' .message-area');
201 inputDetect.focus(function() {
202 chanCleanNotify(hash);
206 inputDetect.blur(function() {
207 resetAutocompletion(hash);
211 inputDetect.keypress(function(e) {
213 if(e.keyCode == 13) {
216 inputDetect.val(inputDetect.val() + '\n');
220 sendMessage(hash, 'groupchat');
222 // Reset the composing database entry
223 setDB('chatstate', room, 'off');
230 else if(e.keyCode == 9) {
231 createAutocompletion(hash);
236 // Reset the autocompleter
238 resetAutocompletion(hash);
242 eventsChatState(inputDetect, room, hash);
244 // Get the current muc informations and content
245 getMUC(room, nickname, password);
248 // Joins the defined groupchats
249 function joinConfGroupchats() {
255 var muc_arr = [GROUPCHATS_JOIN];
259 if(GROUPCHATS_JOIN.indexOf(',') != -1)
260 muc_arr = GROUPCHATS_JOIN.split(',');
263 // Get the current value
264 var muc_current = trim(muc_arr[i]);
270 // Filter the current value
271 muc_current = generateXID(muc_current, 'groupchat');
273 // Add the current value
274 if(!existArrayValue(new_arr, muc_current))
275 new_arr.push(muc_current);
281 checkChatCreate(new_arr[g], 'groupchat');