/* Jappix - An open social platform These are the messages JS scripts for Jappix ------------------------------------------------- License: AGPL Authors: Valérian Saliou, Maranda */ // Bundle var Message = (function () { /** * Alias of this * @private */ var self = {}; /** * Handles the incoming message packets * @public * @param {object} message * @return {boolean} */ self.handle = function(message) { try { // Error packet? Stop! if(Errors.handleReply(message)) return; // Carbon-forwarded message? if(message.getChild('sent', NS_URN_CARBONS)) { Carbons.handleSent(message); return; } if(message.getChild('received', NS_URN_CARBONS)) { Carbons.handleReceived(message); return; } // MAM-forwarded message? var c_mam = message.getChild('result', NS_URN_MAM); if(c_mam) { var c_mam_sel = $(c_mam); var c_mam_delay = c_mam_sel.find('delay[xmlns="' + NS_URN_DELAY + '"]'); var c_mam_forward = c_mam_sel.find('forwarded[xmlns="' + NS_URN_FORWARD + '"]'); if(c_mam_forward.size()) { MAM.handleMessage(c_mam_forward, c_mam_delay); } return; } // We get the message items var from = Common.fullXID(Common.getStanzaFrom(message)); var id = message.getID(); var type = message.getType(); var body = $.trim(message.getBody()); var node = message.getNode(); var subject = $.trim(message.getSubject()); // Keep raw message body var raw_body = body; // We generate some values var xid = Common.bareXID(from); var resource = Common.thisResource(from); var hash = hex_md5(xid); var xHTML = $(node).find('html body').size(); var GCUser = false; // This message comes from a groupchat user if(Utils.isPrivate(xid) && ((type == 'chat') || !type) && resource) { GCUser = true; xid = from; hash = hex_md5(xid); } // Get message date var time, stamp; var delay = DateUtils.readMessageDelay(node); // Any delay? if(delay) { time = DateUtils.relative(delay); stamp = DateUtils.extractStamp(Date.jab2date(delay)); } else { time = DateUtils.getCompleteTime(); stamp = DateUtils.extractStamp(new Date()); } // Received message if(Receipts.hasReceived(message)) return Receipts.messageReceived(hash, id); // Chatstate message if(node && !delay && ((((type == 'chat') || !type) && !Common.exists('#page-switch .' + hash + ' .unavailable')) || (type == 'groupchat'))) { /* REF: http://xmpp.org/extensions/xep-0085.html */ // Re-process the hash var chatstate_hash = hash; if(type == 'groupchat') chatstate_hash = hex_md5(from); // Do something depending of the received state if($(node).find('active').size()) { ChatState.display('active', chatstate_hash, type); // Tell Jappix the entity supports chatstates $('#' + chatstate_hash + ' .message-area').attr('data-chatstates', 'true'); Console.log('Active chatstate received from: ' + from); } else if($(node).find('composing').size()) { ChatState.display('composing', chatstate_hash, type); Console.log('Composing chatstate received from: ' + from); } else if($(node).find('paused').size()) { ChatState.display('paused', chatstate_hash, type); Console.log('Paused chatstate received from: ' + from); } else if($(node).find('inactive').size()){ ChatState.display('inactive', chatstate_hash, type); Console.log('Inactive chatstate received from: ' + from); } else if($(node).find('gone').size()){ ChatState.display('gone', chatstate_hash, type); Console.log('Gone chatstate received from: ' + from); } } // Jappix App message if(message.getChild('app', 'jappix:app')) { // Get notification data var jappix_app_node = $(node).find('app[xmlns="jappix:app"]'); var jappix_app_name = jappix_app_node.find('name'); var jappix_app_name_id = jappix_app_name.attr('id'); var jappix_app_name_value = jappix_app_name.text(); // Jappix Me notification? if(jappix_app_name_id == 'me') { // Get more notification data var jappix_app_data = jappix_app_node.find('data[xmlns="jappix:app:me"]'); var jappix_app_data_action = jappix_app_data.find('action'); var jappix_app_data_url = jappix_app_data.find('url'); var jappix_app_data_action_type = jappix_app_data_action.attr('type'); var jappix_app_data_action_success = jappix_app_data_action.attr('success'); var jappix_app_data_action_job = jappix_app_data_action.attr('job'); var jappix_app_data_url_value = jappix_app_data_url.text(); // Validate data if(jappix_app_data_action_type && jappix_app_data_action_success && jappix_app_data_action_job) { // Filter success jappix_app_data_action_success = parseInt(jappix_app_data_action_success) == 1 ? 'success' : 'error'; // Generate notification namespace var jappix_me_notification_ns = jappix_app_name_id + '_' + jappix_app_data_action_type + '_' + jappix_app_data_action_job + '_' + jappix_app_data_action_success; // Open a new notification Notification.create(jappix_me_notification_ns, xid, [jappix_app_name_value, jappix_app_data_url_value], body); Console.log('Jappix Me notification from: ' + xid + ' with namespace: ' + jappix_me_notification_ns); return false; } } } // Invite message if($(node).find('x[xmlns="' + NS_MUC_USER + '"] invite').size()) { // We get the needed values var iFrom = $(node).find('x[xmlns="' + NS_MUC_USER + '"] invite').attr('from'); var iRoom = $(node).find('x[xmlns="' + NS_XCONFERENCE + '"]').attr('jid'); // Old invite method? if(!iRoom) iRoom = from; // We display the notification Notification.create('invite_room', iFrom, [iRoom], body); Console.log('Invite Request from: ' + iFrom + ' to join: ' + iRoom); return false; } // Request message if(message.getChild('confirm', NS_HTTP_AUTH)) { // Open a new notification Notification.create('request', xid, [message], body); Console.log('HTTP Request from: ' + xid); return false; } // OOB message if(message.getChild('x', NS_XOOB)) { OOB.handle(from, id, 'x', node); Console.log('Message OOB request from: ' + xid); return false; } // Roster Item Exchange message if(message.getChild('x', NS_ROSTERX)) { // Open a new notification Notification.create('rosterx', xid, [message], body); Console.log('Roster Item Exchange from: ' + xid); return false; } // Normal message if((type == 'normal') && body) { // Message date var messageDate = delay; // No message date? if(!messageDate) messageDate = DateUtils.getXMPPTime('utc'); // Message ID var messageID = hex_md5(xid + subject + messageDate); // We store the received message Inbox.storeMessage(xid, subject, body, 'unread', messageID, messageDate); // Display the inbox message if(Common.exists('#inbox')) Inbox.displayMessage(xid, subject, body, 'unread', messageID, messageDate); // Check we have new messages (play a sound if any unread messages) if(Inbox.checkMessages()) Audio.play('notification'); // Send it to the server Inbox.store(); return false; } // PubSub event if($(node).find('event').attr('xmlns') == NS_PUBSUB_EVENT) { // We get the needed values var iParse = $(node).find('event items'); var iNode = iParse.attr('node'); var tText; // Turn around the different result cases if(iNode) { switch(iNode) { // Mood case NS_MOOD: // Retrieve the values var iMood = iParse.find('mood'); var fValue = ''; tText = ''; // There's something if(iMood.children().size()) { // Read the value fValue = node.getElementsByTagName('mood').item(0).childNodes.item(0).nodeName; // Read the text tText = iMood.find('text').text(); // Avoid errors if(!fValue) fValue = ''; } // Store the PEP event (and display it) PEP.store(xid, 'mood', fValue, tText); break; // Activity case NS_ACTIVITY: // Retrieve the values var iActivity = iParse.find('activity'); var sValue = ''; tText = ''; // There's something if(iActivity.children().size()) { // Read the value fValue = node.getElementsByTagName('activity').item(0).childNodes.item(0).nodeName; // Read the text tText = iActivity.find('text').text(); // Avoid errors if(!fValue) fValue = ''; } // Store the PEP event (and display it) PEP.store(xid, 'activity', fValue, tText); break; // Tune case NS_TUNE: // Retrieve the values var iTune = iParse.find('tune'); var tArtist = iTune.find('artist').text(); var tSource = iTune.find('source').text(); var tTitle = iTune.find('title').text(); var tURI = iTune.find('uri').text(); // Store the PEP event (and display it) PEP.store(xid, 'tune', tArtist, tTitle, tSource, tURI); break; // Geolocation case NS_GEOLOC: // Retrieve the values var iGeoloc = iParse.find('geoloc'); var tLat = iGeoloc.find('lat').text(); var tLon = iGeoloc.find('lon').text(); // Any extra-values? var tLocality = iGeoloc.find('locality').text(); var tRegion = iGeoloc.find('region').text(); var tCountry = iGeoloc.find('country').text(); var tHuman = PEP.humanPosition(tLocality, tRegion, tCountry); // Store the PEP event (and display it) PEP.store(xid, 'geoloc', tLat, tLon, tHuman); break; // Microblog case NS_URN_MBLOG: Microblog.display(message, xid, hash, 'mixed', 'push'); break; // Inbox case NS_URN_INBOX: // Do not handle friend's notifications if(xid == Common.getXID()) Notification.handle(message); break; } } return false; } // If this is a room topic message if(subject && (type == 'groupchat')) { // Filter the vars var filter_subject = subject.replace(/\n+/g, ' '); var filteredSubject = Filter.message(filter_subject, resource, true); var filteredName = resource.htmlEnc(); // Display the new subject at the top $('#' + hash + ' .top .name .bc-infos .muc-topic').replaceWith('' + filteredSubject + ''); // Display the new subject as a system message if(resource) { var topic_body = filteredName + ' ' + Common._e("changed the subject to:") + ' ' + Filter.message(subject, resource, true); self.display(type, from, hash, filteredName, topic_body, time, stamp, 'system-message', false); } } // If the message has a content if(xHTML || body) { var filteredMessage; var html_escape = true; // IE bug fix if((BrowserDetect.browser == 'Explorer') && (BrowserDetect.version < 9)) { xHTML = 0; } //If this is a xHTML message if(xHTML) { html_escape = false; // Filter the xHTML message body = Filter.xhtml(node); } // Groupchat message if(type == 'groupchat') { /* REF: http://xmpp.org/extensions/xep-0045.html */ // We generate the message type and time var message_type = 'user-message'; // This is an old message if(delay && resource) { message_type = 'old-message'; } // This is a system message else if(!resource) { message_type = 'system-message'; } var nickQuote = ''; // If this is not an old message if(message_type == 'user-message') { var myNick = Name.getMUCNick(hash); // If an user quoted our nick (with some checks) var regex = new RegExp('((^)|( )|(@))' + Common.escapeRegex(myNick) + '(($)|(:)|(,)|( ))', 'gi'); if(body.match(regex) && (myNick != resource) && (message_type == 'user-message')) nickQuote = ' my-nick'; // We notify the user if there's a new personal message if(nickQuote) { Interface.messageNotify(hash, 'personal'); Board.quick(from, 'groupchat', raw_body, resource); Audio.play('receive-message'); } // We notify the user there's a new unread MUC message else { Interface.messageNotify(hash, 'unread'); // Play sound to all users in the MUC, except user who sent the message. if(myNick != resource) { Audio.play('receive-message'); } } } // Display the received message self.display(type, from, hash, resource.htmlEnc(), body, time, stamp, message_type, html_escape, nickQuote); } // Chat message else { // Gets the nickname of the user var fromName = resource; var chatType = 'chat'; // Must send a receipt notification? if(Receipts.has(message) && (id !== null)) Receipts.sendReceived(type, from, id); // It does not come from a groupchat user, get the full name if(!GCUser) { fromName = Name.getBuddy(xid); } else { chatType = 'private'; } // If the chat isn't yet opened, open it ! if(!Common.exists('#' + hash)) { // We create a new chat Chat.create(hash, xid, fromName, chatType); // We tell the user that a new chat has started Audio.play('new-chat'); } else { Audio.play('receive-message'); } // Display the received message self.display(type, xid, hash, fromName.htmlEnc(), body, time, stamp, 'user-message', html_escape, '', 'him'); // We notify the user Interface.messageNotify(hash, 'personal'); Board.quick(xid, 'chat', raw_body, fromName); } return false; } return false; } catch(e) { Console.error('Message.handle', e); } }; /** * Sends a given message * @public * @param {string} hash * @param {string} type * @return {boolean} */ self.send = function(hash, type) { try { // Get the values var message_area = $('#' + hash + ' .message-area'); var body = $.trim(message_area.val()); var xid = unescape(message_area.attr('data-to')); var nXID; // If the user didn't entered any message, stop if(!body || !xid) return false; // We send the message through the XMPP network var aMsg = new JSJaCMessage(); aMsg.setTo(xid); // Set an ID var id = genID(); aMsg.setID(id); // /help shortcut if(body.match(/^\/help\s*(.*)/)) { // Help text var help_text = '
';
help_text += '' + Common._e("Available shortcuts:") + '';
// Shortcuts array
var shortcuts = [];
// Common shortcuts
shortcuts.push(Common.printf(Common._e("%s removes the chat logs"), '/clear'));
shortcuts.push(Common.printf(Common._e("%s joins a groupchat"), '/join jid'));
shortcuts.push(Common.printf(Common._e("%s closes the chat"), '/part'));
shortcuts.push(Common.printf(Common._e("%s shows the user profile"), '/whois jid'));
// Groupchat shortcuts
if(type == 'groupchat') {
shortcuts.push(Common.printf(Common._e("%s sends a message to the room"), '/say message'));
shortcuts.push(Common.printf(Common._e("%s changes your nickname"), '/nick nickname'));
shortcuts.push(Common.printf(Common._e("%s sends a message to someone in the room"), '/msg nickname message'));
shortcuts.push(Common.printf(Common._e("%s changes the room topic"), '/topic subject'));
shortcuts.push(Common.printf(Common._e("%s kicks a user of the room"), '/kick [reason:] nickname'));
shortcuts.push(Common.printf(Common._e("%s bans a user of the room"), '/ban [reason:] nickname'));
shortcuts.push(Common.printf(Common._e("%s invites someone to join the room"), '/invite jid message'));
}
// Generate the code from the array
shortcuts = shortcuts.sort();
for(var s in shortcuts)
help_text += shortcuts[s] + '
';
help_text += '
' + cLine + '
')); } } return 'XHTML'; } return 'PLAIN'; } catch(e) { Console.error('Message.generate', e); } }; /** * Displays a given message in a chat tab * @public * @param {string} type * @param {string} xid * @param {string} hash * @param {string} name * @param {string} body * @param {string} time * @param {string} stamp * @param {string} message_type * @param {boolean} html_escape * @param {string} nick_quote * @param {string} mode * @param {string} id * @param {object} c_target_sel * @param {boolean} no_scroll * @return {undefined} */ self.display = function(type, xid, hash, name, body, time, stamp, message_type, html_escape, nick_quote, mode, id, c_target_sel, no_scroll) { try { // Target if(typeof c_target_sel === 'undefined') { c_target_sel = $('#' + hash + ' .content'); } // Generate some stuffs var has_avatar = false; var xid_hash = ''; if(!nick_quote) { nick_quote = ''; } if(message_type != 'system-message') { has_avatar = true; xid_hash = hex_md5(xid); } // Can scroll? var cont_scroll = document.getElementById('chat-content-' + hash); var can_scroll = false; if((!cont_scroll.scrollTop || ((cont_scroll.clientHeight + cont_scroll.scrollTop) == cont_scroll.scrollHeight)) && no_scroll !== true) { can_scroll = true; } // Any ID? var data_id = ''; if(id) { data_id = ' data-id="' + id + '"'; } // Filter the message var filteredMessage = Filter.message(body, name, html_escape); // Display the received message in the room var messageCode = ' '; // Must group it? if(!grouped) { // Generate message headers var message_head = ''; // Any avatar to add? if(has_avatar) { message_head += '