1/* 2 * Hanldes Message transportation within WPLC 3*/ 4var gifExtensionPattern = new RegExp(/\.(gif)\b/); 5 6var wplc_server_method = null; 7var wplc_supress_server_logs = true; //We are now surpressing server logs 8var wplc_node_socket = null; //Will not be set unless 9var wplc_node_send_queue = new Array(); 10var wplc_node_message_receiver = null; 11var wplc_node_message_restart_handler = null; 12var wplc_node_client_event_logger = null; 13var wplc_node_sockets_ready = false; 14var wplc_transport_prepared = false; 15 16var wplc_node_async_array = new Array(); //Array which will be sent to our async URL for storage 17var wplc_node_async_send_rate = 1; //Amount of messages that need to be present before we sent the async request 18var wplc_node_async_cookie_check_complete = false; 19 20var wplc_node_port_open = true; //This can be set to false to prevent any future data being sent 21var wplc_node_is_client_typing = false; 22var wplc_node_is_pair_typing_indicator_visible = false; 23var wplc_node_pair_name = ""; 24 25var wplc_node_switch_ajax_complete = false; 26var wplc_node_retry_count = 0; 27 28var wplc_msg_history = new Array(); 29 30var wplc_xhr; //XHR global (allows us to abort previous AJAX request when the agent flips between screens quickly) 31 32 33 34function WPLCServer(){ 35 var wplc_server_ref = this; 36 //Default to ajax until chat starts 37 wplc_server_method = WPLCServer.Ajax; 38 wplc_server_ref.send = wplc_server_method.send; 39 40 wplc_server_ref.isInSocketMode = wplc_server_method.isInSocketMode; 41 wplc_server_ref.isPreparingSocketMode = wplc_server_method.isPreparingSocketMode; 42 wplc_server_ref.transportPrepared = wplc_server_method.transportPrepared; 43 wplc_server_ref.asyncStorage = wplc_server_method.asyncStorage; 44 wplc_server_ref.forceClosePort = wplc_server_method.forceClosePort; 45 wplc_server_ref.sendMessage = wplc_server_method.sendMessage; 46 47 48 wplc_server_ref.prepareTransport = function(callback, messageHandler, restartHandler, clientEventLog){ 49 wplc_server_log("-------------------"); 50 wplc_server_log("Preparing Transport"); 51 if(typeof wplc_use_node_server !== "undefined" && wplc_use_node_server === "true"){ 52 if(window.WebSocket){ 53 //Sockets are supported 54 wplc_server_method = WPLCServer.Socket; 55 wplc_server_log("Socket Mode"); 56 } else { 57 wplc_server_method = WPLCServer.Ajax; 58 wplc_server_log("Ajax Mode"); 59 } 60 } else { 61 wplc_server_method = WPLCServer.Ajax; 62 wplc_server_log("Ajax Mode"); 63 } 64 65 wplc_server_method.init(function(){ 66 wplc_server_ref.send = wplc_server_method.send; 67 wplc_server_ref.isInSocketMode = wplc_server_method.isInSocketMode; 68 wplc_server_ref.isPreparingSocketMode = wplc_server_method.isPreparingSocketMode; 69 wplc_server_ref.transportPrepared = wplc_server_method.transportPrepared; 70 wplc_server_ref.asyncStorage = wplc_server_method.asyncStorage; 71 wplc_server_ref.forceClosePort = wplc_server_method.forceClosePort; 72 wplc_server_ref.sendMessage = wplc_server_method.sendMessage; 73 74 if(typeof callback === "function"){ 75 callback(); 76 } 77 }, messageHandler, function(){ 78 wplc_server_method = WPLCServer.Ajax; 79 wplc_server_log("Ajax Mode - Fail Over"); 80 81 wplc_server_ref.send = wplc_server_method.send; 82 wplc_server_ref.isInSocketMode = wplc_server_method.isInSocketMode; 83 wplc_server_ref.isPreparingSocketMode = function(){ return false; }; 84 wplc_server_ref.transportPrepared = wplc_server_method.transportPrepared; 85 wplc_server_ref.asyncStorage = wplc_server_method.asyncStorage; 86 wplc_server_ref.forceClosePort = wplc_server_method.forceClosePort; 87 wplc_server_ref.sendMessage = wplc_server_method.sendMessage; 88 89 if(typeof wplc_ajaxurl !== "undefined" && typeof wplc_nonce !== "undefined" && typeof wplc_cid !== "undefined"){ 90 var wplc_fail_over_data = { 91 action: 'wplc_node_switch_to_ajax', 92 security: wplc_nonce, 93 cid: wplc_cid 94 }; 95 96 jQuery.ajax({ 97 url : wplc_ajaxurl, 98 data : wplc_fail_over_data, 99 type : "POST", 100 timeout : 120000, 101 success : function(response){ 102 wplc_server_log("Ajax Mode Enabled"); 103 }, 104 error : function(error, exception){ 105 wplc_server_log("Chat Fail Over Could Not Be Setup"); 106 }, 107 complete : function(response){ 108 if(typeof callback === "function"){ 109 callback(); 110 } 111 } 112 }); 113 } 114 }, restartHandler, clientEventLog); 115 116 wplc_server_log("Transport Prepared"); 117 wplc_server_log("-------------------"); 118 wplc_transport_prepared = true; 119 } 120 121 wplc_server_ref.browserIsSocketReady = function(){ 122 if(typeof wplc_use_node_server !== "undefined" && wplc_use_node_server === "true"){ 123 if(window.WebSocket){ 124 return true; 125 } else { 126 return false; 127 } 128 } else { 129 return false; 130 } 131 } 132} 133 134WPLCServer.Socket = { 135}; 136 137WPLCServer.Ajax = { 138 init : function(callback, messageHandler, failOver, restartHandler, clientEventLog){ 139 wplc_server_log("Ajax Init"); 140 if(typeof callback === "function"){ 141 callback(); 142 } 143 }, 144 send : function (wplc_send_url, wplc_send_data, wplc_send_type, wplc_send_timeout, wplc_send_success_callback, wplc_send_fail_callback, wplc_send_complete_callback){ 145 jQuery.ajax({ 146 url : wplc_send_url, 147 data : wplc_send_data, 148 type : wplc_send_type, 149 timeout : wplc_send_timeout, 150 success : function(response){ 151 if(typeof wplc_send_success_callback === "function"){ 152 if(typeof wplc_send_data['action'] !== "undefined" && wplc_send_data['action'] !== "wplc_start_chat"){ //Is this the start? 153 wplc_send_success_callback(response); 154 } else { 155 //Check if we are going to go into socket mode after this? 156 if(typeof wplc_use_node_server !== "undefined" && wplc_use_node_server === "true"){ 157 if(window.WebSocket){ 158 wplc_send_success_callback(response); //Send the data if we are going to sockets after this 159 } 160 } 161 } 162 } 163 }, 164 error : function(error, exception){ 165 if(typeof wplc_send_fail_callback === "function"){ 166 wplc_send_fail_callback(error, exception); 167 } 168 }, 169 complete : function(response){ 170 if(typeof wplc_send_complete_callback === "function"){ 171 wplc_send_complete_callback(response, wplc_send_data); 172 } 173 } 174 }); 175 }, 176 isInSocketMode : function (){ 177 return wplc_node_sockets_ready; 178 }, 179 isPreparingSocketMode : function(){ 180 var preparing = false; 181 if(typeof wplc_use_node_server !== "undefined" && wplc_use_node_server === "true"){ 182 if(window.WebSocket){ 183 preparing = true; 184 } 185 } 186 187 return preparing; 188 }, 189 transportPrepared : function(){ 190 return wplc_transport_prepared; 191 }, 192 asyncStorage : function(wplc_send_url, wplc_send_data, wplc_send_timeout){ 193 //Do nothing -> Ajax handles 194 }, 195 forceClosePort : function(){ 196 //Do Nothing ajax doesnt use socket ports 197 }, 198 sendMessage : function(wplc_send_url, wplc_send_data, wplc_send_type, wplc_send_timeout, wplc_send_success_callback, wplc_send_fail_callback, wplc_send_complete_callback){ 199 WPLCServer.Ajax.send(wplc_send_url, wplc_send_data, wplc_send_type, wplc_send_timeout, wplc_send_success_callback, wplc_send_fail_callback, wplc_send_complete_callback); 200 } 201}; 202 203 204function wplc_server_log(msg){ 205 if(wplc_supress_server_logs !== true && window.console){ 206 console.log("WPLC SERVER: " + msg); 207 } 208} 209 210function wplc_server_error(msg){ 211 if(window.console){ 212 console.error("WPLC SERVER ERROR: " + msg); 213 } 214} 215 216function wplc_socket_send(data, success, fail, complete){ 217 if(wplc_node_port_open){ 218 wplc_socket_add_to_queue(data, success, fail, complete); 219 220 //if(data.action !== "wplc_user_send_msg" && data.action !== "wplc_admin_send_msg"){ 221 var wplc_current_queue_item = wplc_socket_get_next_in_queue(); 222 if(wplc_current_queue_item !== false){ 223 if(typeof wplc_node_socket !== "undefined" && wplc_node_socket !== null){ 224 if(wplc_node_socket.readyState !== WebSocket.CONNECTING && wplc_node_socket.readyState !== WebSocket.CLOSING && wplc_node_socket.readyState !== WebSocket.CLOSED){ 225 wplc_current_queue_item.data.is_typing = typeof wplc_node_is_client_typing !== "undefined" ? wplc_node_is_client_typing : false; 226 wplc_node_socket.send(JSON.stringify(wplc_current_queue_item.data)); 227 228 if(typeof wplc_current_queue_item.success === "function"){ 229 wplc_current_queue_item.success(); 230 } 231 } else { 232 //Try again in a sec just now -> Add it to the queue 233 setTimeout(function(){ 234 wplc_socket_send(data, success, fail, complete); 235 }, 500); 236 } 237 } else { 238 setTimeout(function(){ 239 //Try again in a sec just now -> Add it to the queue 240 wplc_socket_send(data, success, fail, complete); 241 }, 500); 242 } 243 } 244 //} 245 } 246} 247 248function wplc_sanitize_attributes( str ){ 249 var wplc_allowed_attributes = "onload,onclick,alert,onerror,dalert"; 250 var allowed_attr = wplc_allowed_attributes.split(","); 251 for( i = 0; i < allowed_attr.length; i++ ){ 252 str = str.replace(allowed_attr[i], ''); 253 } 254 255 return str; 256} 257 258function wplc_socket_add_to_queue(data, success, fail, complete){ 259 if(typeof data.server_token === "undefined"){ 260 if(typeof bleeper_api_key !== "undefined"){ 261 data.server_token = bleeper_api_key; 262 } else { 263 wplc_server_error("No Server Token Present, Something will go wrong"); 264 } 265 } 266 267 268 var queue_item = { 269 data: data, 270 success: success, 271 fail: fail, 272 complete: complete 273 } 274 275 if(wplc_node_send_queue.length > 0){ 276 var last_item = wplc_node_send_queue[wplc_node_send_queue.length - 1]; 277 if(JSON.stringify(last_item.data) !== JSON.stringify(data)){ 278 wplc_node_send_queue.push(queue_item); 279 } 280 } else { 281 wplc_node_send_queue.push(queue_item); 282 } 283} 284 285function wplc_socket_get_next_in_queue(){ 286 if(wplc_node_send_queue.length > 0){ 287 return wplc_node_send_queue.shift(); 288 } else { 289 return false; 290 } 291} 292 293function wplc_rest_api(type, wplc_send_data, wplc_send_timeout, next) { 294 295 if (typeof wplc_xhr !== "undefined" && typeof wplc_xhr.abort() !== "undefined") { wplc_xhr.abort(); } 296 if (typeof next === "undefined" || next === null) { next = function() {}; } 297 var wplc_node_ajax_action = type; 298 //Send the data to the Async 299 if(typeof wplc_restapi_enabled !== "undefined" && parseInt(wplc_restapi_enabled) === 1 && typeof wplc_restapi_endpoint !== "undefined"){ 300 //REST API is ready to rumble 301 302 var anti_cache = Date.now(); 303 304 wplc_send_url = wplc_restapi_endpoint + "/" + type + "?nocache="+anti_cache; 305 306 307 308 var prepared_data = wplc_send_data; 309 310 var x = { 311 action : wplc_node_ajax_action, 312 relay_action : wplc_send_data.action, 313 chat_id : wplc_send_data.cid, 314 security : wplc_send_data.security, 315 message : wplc_send_data.msg, 316 server_token : wplc_restapi_token, 317 wplc_extra_data:document.wplc_extra_data, 318 wplc_data : wplc_send_data 319 }; 320 321 322 prepared_data.server_token = wplc_restapi_token; 323 prepared_data.token = wplc_restapi_token; /* backwards compat */ 324 if (typeof prepared_data.wplc_extra_data === "undefined") { 325 prepared_data.wplc_extra_data = document.wplc_extra_data; 326 } 327 prepared_data._wpnonce = wplc_restapi_nonce; 328 329 wplc_xhr = jQuery.ajax({ 330 url : wplc_send_url, 331 data : prepared_data, 332 type : "POST", 333 timeout : wplc_send_timeout, 334 success : function(response){ 335 wplc_server_log("REST SEND = SUCCESS"); 336 next(response); 337 }, 338 error : function(error, exception){ 339 wplc_server_log("REST SEND = FAIL"); 340 next(); 341 }, 342 complete : function(response){ 343 next(response); 344 } 345 }); 346 } else { 347 wplc_server_log("NO REST API :("); 348 next(); 349 } 350 351} 352 353function wplc_socket_async_storage_handler(wplc_send_url, wplc_send_data, wplc_send_timeout){ 354 var wplc_node_ajax_action = "wplc_node_async_storage_ajax"; 355 //Send the data to the Async 356 if(typeof wplc_restapi_enabled !== "undefined" && parseInt(wplc_restapi_enabled) === 1 && typeof wplc_restapi_endpoint !== "undefined"){ 357 //REST API is ready to rumble 358 wplc_send_url = wplc_restapi_endpoint + "/async_storage"; 359 } else { 360 //Ajax time! Cause the Rest she Left 361 } 362 363 var prepared_data = { 364 action : wplc_node_ajax_action, 365 relay_action : wplc_send_data.action, 366 chat_id : wplc_send_data.cid, 367 security : wplc_send_data.security, 368 messages : JSON.stringify(wplc_node_async_array), 369 wplc_extra_data:document.wplc_extra_data 370 }; 371 372 if(typeof bleeper_api_key !== "undefined"){ 373 prepared_data.server_token = bleeper_api_key; 374 } 375 376/* 377 wplc_node_async_array = new Array(); //Clearing the storage array before the next data can be sent through 378 if(typeof Cookies !== "undefined" && typeof Cookies === "function"){ 379 Cookies.remove('wplc_server_async_storage'); //Clear the cookies now so that we don't do this again. 380 } 381*/ 382 jQuery.ajax({ 383 url : wplc_send_url, 384 data : prepared_data, 385 type : "POST", 386 timeout : wplc_send_timeout, 387 success : function(response){ 388 wplc_server_log("ASYNC STORAGE = SUCCESS"); 389 }, 390 error : function(error, exception){ 391 wplc_server_log("ASYNC STORAGE = FAIL"); 392 }, 393 complete : function(response){ 394 395 } 396 }); 397} 398 399function wplc_json_validator(str) { 400 try { 401 JSON.parse(str); 402 } catch (e) { 403 return false; 404 } 405 return true; 406} 407 408function wplc_node_parse_async_from_object(obj, complete){ 409 for(var i in obj){ 410 if(obj.hasOwnProperty(i)){ 411 wplc_node_async_array.push(obj[i]); 412 } 413 } 414 415 if(typeof complete === "function"){ 416 complete(); 417 } 418} 419 420function wplc_node_global_message_receiver(data){ 421 if(data){ 422 if (typeof data !== "object") { 423 data = JSON.parse(data); 424 } 425 } 426 427 if(typeof data['pair_name'] !== "undefined"){ 428 if(data['pair_name'] !== wplc_node_pair_name){ 429 wplc_node_pair_name = data['pair_name']; 430 } 431 } 432 433 if(typeof data['pair_typing'] !== "undefined"){ 434 435 if(data['pair_typing'] === true || data['pair_typing'] === "true"){ 436 if(wplc_node_is_pair_typing_indicator_visible === false){ 437 if (jQuery("#wplc_user_typing").length>0) { } else { 438 jQuery(".typing_indicator").html("<span id='wplc_user_typing'>"+wplc_node_pair_name+" "+wplc_localized_string_is_typing_single+"</span>"); 439 jQuery(".typing_indicator").addClass("typing_indicator_active"); 440 } 441 } 442 wplc_node_is_pair_typing_indicator_visible = true; 443 } else { 444 if(wplc_node_is_pair_typing_indicator_visible === true){ 445 if (jQuery("#wplc_user_typing").length>0) { 446 jQuery("#wplc_user_typing").fadeOut("slow").remove(); 447 jQuery(".typing_indicator").removeClass("typing_indicator_active"); 448 } 449 } 450 wplc_node_is_pair_typing_indicator_visible = false; 451 } 452 453 } 454} 455String.prototype.wplcStripSlashes = function(){ 456 return this.replace(/\\(.)/mg, "$1"); 457} 458 459function wplc_add_date_and_time(the_message,originates) { 460 461 if (parseInt(originates) === 1 || parseInt(originates) === 2) { 462 463 var time_msg = ''; 464 465 /* identfy the timestamp */ 466 if (typeof the_message.other === "undefined" || typeof the_message.other.datetime === "undefined" || the_message.other === false) { 467 /* only run if it hasnt got a timestamp in the .other.datetime key */ 468 if (typeof the_message.timestamp !== "undefined") {
469 /* most likely came from node as node */ 470 471 if (typeof the_message.other !== "object") { the_message.other = {}; } 472 the_message.other.datetime = the_message.timestamp; 473 474 } 475 } 476 477 if (typeof the_message.other === "undefined" || typeof the_message.other.datetime === "undefined") { 478 /* there is no datetime so return nothing */ 479 return ''; 480 } else { 481 if (typeof wplc_show_chat_detail !== "undefined") { 482 483 var datetimestamp = the_message.other.hasOwnProperty( 'datetimeUTC' ) ? the_message.other.datetimeUTC : the_message.other.datetime; 484 var dateTime = new Date( parseInt( datetimestamp ) * 1000 ); 485 486 if ( typeof wplc_show_chat_detail.date !== 'undefined' && '1' === wplc_show_chat_detail.date ) { 487 488 var date_format = typeof wplc_datetime_format !== 'undefined' && wplc_datetime_format.hasOwnProperty( 'date_format' ) ? wplc_datetime_format.date_format : 'n/d'; 489 var date_month = dateTime.getMonth(); 490 var date_day = dateTime.getDate(); 491 var date_months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']; 492 var date_days = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday']; 493 var date_day_no = dateTime.getDay(); 494 var date_year = dateTime.getFullYear(); 495 496 time_msg += date_format 497 .replace( /S/g, '%S%' ) 498 .replace( /D/g, '%D%' ) 499 .replace( /l/g, '%l%' ) 500 .replace( /F/g, '%F%' ) 501 .replace( /M/g, '%M%' ) 502 .replace( /j/g, date_day ) 503 .replace( /d/g, date_day < 10 ? '0' + date_day : date_day ) 504 .replace( /w/g, date_day_no ) 505 .replace( /n/g, date_month + 1 ) 506 .replace( /m/g, date_month + 1 < 10 ? '0' + ( date_month + 1 ) : date_month + 1 ) 507 .replace( /Y/g, date_year ) 508 .replace( /y/g, date_year.toString().substr( 2, 2 ) ) 509 .replace( /%S%/g, 1 === date_day ? 'st' : ( 2 === date_day ? 'nd' : ( 3 === date_day ? 'rd' : 'th' ) ) ) 510 .replace( /%D%/g, date_days[ date_day_no ].substr( 0, 3 ) ) 511 .replace( /%l%/g, date_days[ date_day_no ] ) 512 .replace( /%F%/g, date_months[ date_month ] ) 513 .replace( /%M%/g, date_months[ date_month ].substr( 0, 3 ) ) + ' '; 514 515 } 516 if ( typeof wplc_show_chat_detail.time !== "undefined" && '1' === wplc_show_chat_detail.time ) { 517 518 var time_format = typeof wplc_datetime_format !== 'undefined' && wplc_datetime_format.hasOwnProperty( 'time_format' ) ? wplc_datetime_format.time_format : 'H:i'; 519 var time_hours_12 = dateTime.getHours() > 12 ? dateTime.getHours() - 12 : ( dateTime.getHours() < 1 ? 12 : dateTime.getHours() ); 520 var time_hours_24 = dateTime.getHours(); 521 var time_am_pm = time_hours_24 > 11 ? 'pm' : 'am'; 522 var time_minutes = dateTime.getMinutes(); 523 524 time_msg += time_format 525 .replace( /g/g, time_hours_12 ) 526 .replace( /h/g, time_hours_12 < 10 ? '0' + time_hours_12 : time_hours_12 ) 527 .replace( /G/g, time_hours_24 ) 528 .replace( /H/g, time_hours_24 < 10 ? '0' + time_hours_24 : time_hours_24 ) 529 .replace( /i/g, time_minutes < 10 ? '0' + time_minutes : time_minutes ) 530 .replace( /a/g, time_am_pm.toLowerCase() ) 531 .replace( /A/g, time_am_pm.toUpperCase() ); 532 533 } 534 if (time_msg !== '') { 535 if (parseInt(originates) === 1) { aoru_class = 'wplc-msg-float-left'; } else { aoru_class = 'wplc-msg-float-right'; } 536 time_msg = '<span class="timedate '+aoru_class+'">'+time_msg+'</span>'; 537 } 538 } 539 540 541 return time_msg; 542 } 543 } else { 544 return ''; 545 } 546 547} 548 549function wplc_get_chat_person_name_msg_field(name) { 550 try { 551 return '<span class="wplc-chat-person-name">' + name + ': </span>'; 552 } catch(err) { 553 //console.log("Could not wplc_get_chat_person_name_msg_field() " + err); 554 } 555} 556 557/** 558 * Removes undesired strings from a message which contains a GIF URL and returns only the gif url 559 * @param {*} message_content 560 */ 561function getCleanedGif(message_content) { 562 if (typeof(message_content) !== "undefined") { 563 var msgParts = message_content.split(" "); 564 565 if (typeof(msgParts) !== "undefined") { 566 567 for (var i = 0; i < msgParts.length; i++) { 568 var msgPart = msgParts[i];
569 570 if (typeof(msgPart) !== "undefined") { 571 572 if (gifExtensionPattern.test(msgPart)) { 573 var cleanedGifUrl = msgPart.replace("href=", ""); 574 cleanedGifUrl = cleanedGifUrl.replace(/\"/g, ""); 575 576 return cleanedGifUrl; 577 } 578 } 579 } 580 } 581 } 582 583 return ""; 584} 585 586/** 587 * Pushes the message object to the chat box 588 * 589 * @param {object} the_message The message object 590 * @param {string} aoru a for Agent, u for User 591 * @return void 592 */ 593function wplc_push_message_to_chatbox(the_message, aoru, next) { 594 595 /** 596 * First check if we have processed this message already, by comparing the ID 597 * 598 * Some system notifications may not come through with an ID so we can accept those. 599 */ 600 var add_message = true; 601 if (typeof the_message.mid !== "undefined" && aoru === "u") { 602 if (parseInt(the_message.mid) === 0 || the_message.mid === null) { 603 add_message = true; 604 } else { 605 if (typeof wplc_msg_history[the_message.mid] !== "undefined") { 606 /* we have this message already */ 607 add_message = false; 608 } else { 609 /* add this to our history */ 610 wplc_msg_history[the_message.mid] = true; 611 } 612 } 613 } 614 if (add_message) { 615 if (typeof the_message.originates !== "undefined" && the_message.originates !== null && the_message.originates !== "null"){ 616 var message_class = ""; 617 var grav_hash = ""; 618 var message_grav = ""; 619 var message_from = ""; 620 var message_content = ""; 621 var message_aid; 622 623 var message_edit_string = ""; 624 625 var audioPattern = new RegExp(/blob.wav/); 626 var isAudioPattern = false; 627 628 if (parseInt(the_message.originates) === 1) { 629 //From Admin 630 631 /* Define which agent it was sent from */ 632 if (typeof the_message.other !== "undefined" && typeof the_message.other.aid !== "undefined") { 633 message_aid = the_message.other.aid.toString(); /* set it to a string because wp_localize doesnt know how to set keys as integers */ 634 } else if (typeof the_message.other !== "undefined" && typeof the_message.other.agent_id !== "undefined") { 635 /* cloud server uses "agent_id" instead of "aid" */ 636 message_aid = the_message.other.agent_id.toString(); 637 } else { 638 message_aid = false; 639 } 640 message_class = "wplc-admin-message wplc-color-bg-4 wplc-color-2 wplc-color-border-4"; 641 642 // If it is audio message 643 isAudioPattern = audioPattern.test(the_message.msg); 644 if (isAudioPattern) { 645 message_class += " wplc-user-message-audio"; 646 } 647 648 if (aoru === 'u') { 649 650 /* message came from admin, intended for user */ 651 if (message_aid !== false && typeof wplc_agent_data !== "undefined" && typeof wplc_agent_data[message_aid] !== "undefined") { 652 /* we know who the agent was that sent this message (v7.1.00+) */ 653 if (typeof wplc_show_chat_detail !== "undefined") { 654 if (typeof wplc_show_chat_detail.avatar !== "undefined" && wplc_show_chat_detail.avatar === "1") { message_grav = (typeof wplc_agent_data[message_aid].md5 !== "undefined" ? "<img src='"+wplc_user_avatars[message_aid]+"?s=80&d=mm' class='wplc-admin-message-avatar' />" : ""); } 655 if (typeof wplc_show_chat_detail.name !== "undefined" && wplc_show_chat_detail.name === "1") { message_from = (typeof wplc_agent_data[message_aid].name !== "undefined" ? wplc_get_chat_person_name_msg_field(wplc_agent_data[message_aid].name) : ""); } 656 } 657 658 } else { 659 /* we do'nt know which agent sent this message, so lets set it as the current user instead (backwards compat) */ 660 if (typeof wplc_show_chat_detail !== "undefined") { 661 if (typeof wplc_show_chat_detail.avatar !== "undefined" && wplc_show_chat_detail.avatar === "1") { message_grav = (typeof wplc_current_agent.email !== "undefined" ? "<img src='"+wplc_user_avatars[message_aid]+"?s=80&d=mm' class='wplc-admin-message-avatar' />" : ""); } 662 if (typeof wplc_show_chat_detail.name !== "undefined" && wplc_show_chat_detail.name === "1") { message_from = (typeof wplc_current_agent.name !== "undefined" ? wplc_get_chat_person_name_msg_field(wplc_current_agent.name) : ""); } 663 } 664 } 665 } else { 666 667 if(typeof agent_id !== "undefined" && typeof message_aid !== "undefined"){ 668 if(parseInt(agent_id) === parseInt(message_aid)){ 669 //I own this message 670 message_edit_string = "<span class='bleeper-edit-message' style='display:none;'>Edit</span>"; 671 672 } 673 } 674 675 if (message_aid !== false && typeof wplc_agent_data !== "undefined" && typeof wplc_agent_data[message_aid] !== "undefined") { 676
677 /* we know who the agent was that sent this message (v7.1.00+) */ 678 if (typeof wplc_show_chat_detail !== "undefined") { 679 if (typeof wplc_show_chat_detail.avatar !== "undefined" && wplc_show_chat_detail.avatar === "1") { message_grav = (typeof wplc_agent_data[message_aid].md5 !== "undefined" ? "<img src='//www.gravatar.com/avatar/"+wplc_agent_data[message_aid].md5+"?s=80&d=mm' class='wplc-admin-message-avatar' />" : ""); } 680 if (typeof wplc_show_chat_detail.name !== "undefined" && wplc_show_chat_detail.name === "1") { message_from = (typeof wplc_agent_data[message_aid].name !== "undefined" ? wplc_get_chat_person_name_msg_field(wplc_agent_data[message_aid].name) : ""); } 681 } 682 683 } else { 684 if (typeof wplc_show_chat_detail.avatar !== "undefined" && wplc_show_chat_detail.avatar === "1") { 685 message_grav = (typeof wplc_admin_agent_email !== "undefined" ? "<img src='//www.gravatar.com/avatar/"+wplc_admin_agent_email+"?s=80&d=mm' class='wplc-admin-message-avatar' />" : ""); 686 } 687 if (typeof wplc_show_chat_detail.name !== "undefined" && wplc_show_chat_detail.name === "1") { 688 message_from = (typeof wplc_admin_agent_name !== "undefined" ? wplc_get_chat_person_name_msg_field(wplc_admin_agent_name) : ""); 689 } 690 } 691 } 692 693 message_content = the_message.msg.wplcStripSlashes(); 694 wplc_new_message_sound = true; 695 696 } else if (parseInt(the_message.originates) === 0 || parseInt(the_message.originates) === 3) { 697 //System Notification 698 message_class = "wplc_system_notification wplc-color-4"; 699 message_content = the_message.msg; 700 if (typeof the_message.other.ntype !== "undefined") { 701 if (the_message.other.ntype === "joined") { 702 jQuery.event.trigger({type: "wplc_agent_joined", ndata:the_message}); 703 } 704 } 705 706 } else { 707 /* most likely from the user */ 708 message_class = "wplc-user-message wplc-color-bg-1 wplc-color-2 wplc-color-border-1"; 709 710 isAudioPattern = audioPattern.test(the_message.msg); 711 if (isAudioPattern) { 712 message_class += " wplc-user-message-audio"; 713 } 714 715 if (aoru === 'u') { 716 message_edit_string = "<span class='bleeper-edit-message' style='display:none;'>Edit</span>"; 717 718 if (jQuery(wplc_email).val() !== "") { 719 message_grav = md5(jQuery(wplc_email).val()); 720 721 } else { 722 if (typeof visitor_list !== "undefined" && typeof visitor_list[active_chatid] !== "undefined" && typeof visitor_list[active_chatid].email !== "undefined") { 723 message_grav = md5(visitor_list[active_chatid].email); 724 } else { 725 if(typeof wplc_cookie_email !== "undefined" && wplc_cookie_email !== ""){ 726 message_grav = md5(wplc_cookie_email); 727 } else { 728 message_grav = 'x'; 729 } 730 } 731 } 732 733 message_grav = "<img src='//www.gravatar.com/avatar/" + message_grav + "?s=80&d=mm' class='wplc-user-message-avatar' />"; 734 if (typeof Cookies.get("wplc_name") === "undefined") { 735 message_from = wplc_get_chat_person_name_msg_field("Guest"); 736 } else { 737 message_from = wplc_get_chat_person_name_msg_field(Cookies.get("wplc_name"));
738 } 739 } else { 740 if (typeof wplc_chat_email !== "undefined") { 741 message_grav = wplc_chat_email; 742 743 } else { 744 if (typeof visitor_list !== "undefined" && typeof visitor_list[active_chatid] !== "undefined" && typeof visitor_list[active_chatid].email !== "undefined") { 745 message_grav = md5(visitor_list[active_chatid].email); 746 } else { 747 if(typeof wplc_cookie_email !== "undefined" && wplc_cookie_email !== ""){ 748 message_grav = md5(wplc_cookie_email); 749 } else { 750 message_grav = 'x'; 751 } 752 } 753 } 754 message_grav = "<img src='//www.gravatar.com/avatar/" + message_grav + "?s=80&d=mm' class='wplc-user-message-avatar' />"; 755 if (typeof wplc_chat_name !== "undefined") { 756 message_from = wplc_get_chat_person_name_msg_field(wplc_chat_name); 757 } 758 } 759 message_content = the_message.msg.wplcStripSlashes(); 760 } 761 762 if (typeof the_message.other !== "undefined" && typeof the_message.other.from_an_agent !== "undefined"){ 763 if(the_message.other.from_an_agent === true || the_message.other.from_an_agent === "true"){ 764 //Cannot edit this 765 message_edit_string = ""; 766 } 767 } 768 message_class += " message_"+the_message.mid; 769 770 if (message_content !== ""){ 771 message_content = wplc_sanitize_attributes(message_content); 772 773 // If it is audio message 774 isAudioPattern = audioPattern.test(message_content); 775 776 // Open the HTML of a message 777 var concatenated_message = "<span class='" + message_class + "' mid='"+the_message.mid+"'>"; 778 779 if (typeof wplc_show_chat_detail !== "undefined") { 780 781 if (typeof wplc_show_chat_detail.avatar !== "undefined" && wplc_show_chat_detail.avatar === "1") { 782 concatenated_message += message_grav; 783 } 784 785 // Add a wrapper for the person name and the message, this wrapper is necessary to implement the UI of the admin chat 786 if (isAudioPattern) { 787 concatenated_message += "<div class='wplc-msg-content wplc-msg-content-audio' mid='" + the_message.mid + "'>"; 788 } else { 789 concatenated_message += "<div class='wplc-msg-content' mid='"+the_message.mid+"'>"; 790 } 791 792 if (isAudioPattern) { 793 concatenated_message += "<span class='wplc-msg-content-audio-icon'></span>"; 794 } else if (typeof wplc_show_chat_detail.name !== "undefined" && wplc_show_chat_detail.name === "1") { 795 concatenated_message += message_from; 796 } 797 } else { 798 // Add a wrapper for the person name and the message, this wrapper is necessary to implement the UI of the admin chat 799 concatenated_message += "<div class='wplc-msg-content'>"; 800 } 801 802 var original_message = message_content; 803 if (isAudioPattern) { 804 message_content = "<a href='" + message_content + "' target='_blank'>" + (typeof wplc_visitor_voice !== 'undefined' && typeof wplc_visitor_voice.play_sound !== 'undefined' ? wplc_visitor_voice.play_sound : 'Open Voice Note') + "</a>"; 805 } else if (typeof wplcFormatParser !== "undefined"){ 806 if(typeof the_message.other !== 'undefined' && typeof the_message.other.ignore_style_tags !== 'undefined' && the_message.other.ignore_style_tags === true){ 807 //Don't nifty parse this 808 } else { 809 message_content = wplcFormatParser(message_content); 810 } 811 } 812 813 //Cleanup the original message 814 var original_message_stripper = document.createElement("DIV"); 815 original_message_stripper.innerHTML = original_message; 816 original_message = original_message_stripper.textContent || original_message_stripper.innerText || ""; 817 818 // If it is a GIF message 819 if (gifExtensionPattern.test(message_content)) { 820 cleanedGif = getCleanedGif(message_content); 821 concatenated_message += "<span class='messageBody' data-message='"+ cleanedGif +"'><img src='"+ cleanedGif + "' class='gif-img'/></span>"+ message_edit_string; 822 } else if (isAudioPattern) { 823 // If it is audio pattern 824 concatenated_message += "<span class='messageBody'>"+message_content+"</span>"; 825 } else { 826 // If it is a regular message 827 concatenated_message += "<span class='messageBody' data-message='" + original_message + "'>"+message_content+"</span>"+ message_edit_string; 828 } 829 830 // Close the person name/message wrapper, if it was added 831 concatenated_message += "</div>"; 832 833 // Close the HTML of a message 834 concatenated_message += "</span>"; 835 concatenated_message += wplc_add_date_and_time(the_message,the_message.originates); 836 837 if (aoru === 'u') { 838 wplc_chat_box_elemn = "#wplc_chatbox"; 839 } else { 840 841 if (typeof bleeper_remote_enabled !== "undefined"){ 842 wplc_chat_box_elemn = "#messages"; 843 } else { 844 wplc_chat_box_elemn = "#admin_chat_box_area_"+wplc_cid; 845 } 846 } 847 848 849 jQuery(wplc_chat_box_elemn).append(concatenated_message); 850 851 } 852 } 853 } 854 next(); 855} 856 857jQuery(function(){ 858 jQuery(function(){ 859 var wplc_node_searchTimeout; 860 861 jQuery("body").on("keydown","#wplc_chatmsg, #wplc_admin_chatmsg", function(e) { 862 if(typeof wplc_node_sockets_ready !== "undefined" && wplc_node_sockets_ready === true){ 863 if(typeof wplc_node_is_client_typing !== "undefined"){ 864 if (e.which <= 90 && e.which >= 48) { 865 if (wplc_node_is_client_typing) { 866 wplc_node_renew_typing(); 867 return; 868 } 869 wplc_node_is_client_typing = true; 870 871 wplc_node_searchTimeout = setTimeout(wplc_node_clear_typing, 1000); 872 } 873 } 874 } 875 }); 876 877 jQuery("body").on("click", "#wplc_na_msg_btn", function() { 878 var wplc_is_gdpr_enabled = jQuery(this).attr('data-wplc-gdpr-enabled'); 879 if(typeof wplc_is_gdpr_enabled !== "undefined" && (wplc_is_gdpr_enabled === 'true' )){ 880 var wplc_gdpr_opt_in_checked = jQuery("#wplc_chat_gdpr_opt_in").is(':checked'); 881 if(typeof wplc_gdpr_opt_in_checked === "undefined" || wplc_gdpr_opt_in_checked === false){ 882 /* GDPR requirements not met */ 883 jQuery("#wplc_chat_gdpr_opt_in").addClass('incomplete'); 884 return false; 885 } 886 jQuery("#wplc_chat_gdpr_opt_in").removeClass('incomplete'); 887 } 888 889 var wplc_name = jQuery("#wplc_name").val(); 890 var wplc_email = jQuery("#wplc_email").val(); 891 var wplc_msg = jQuery("#wplc_message").val(); 892 var wplc_domain = jQuery("#wplc_domain_offline").val(); 893 var ip_address = jQuery("#wplc_ip_address").val(); 894 895 if (wplc_name.length <= 0) { alert(wplc_error_messages.val
895id_name); return false; } 896 if (wplc_email.length <= 0) { alert(wplc_error_messages.valid_email); return false; } 897 var testEmail = /^[A-Z0-9._%+-]+@([A-Z0-9-]+\.)+[A-Z]{2,12}$/i; 898 if (!testEmail.test(wplc_email)){ 899 alert(wplc_error_messages.valid_email); return false; 900 } 901 if (wplc_msg.length <= 0) { alert(wplc_error_messages.empty_message); return false; } 902 jQuery("#wp-live-chat-2-info").hide(); 903 jQuery("#wplc_message_div").html(wplc_offline_msg); 904 905 wplc_cid = Cookies.get('wplc_cid'); 906 907 var data = { 908 action: 'wplc_user_send_offline_message', 909 security: wplc_nonce, 910 cid: wplc_cid, 911 name: wplc_name, 912 email: wplc_email, 913 msg: wplc_msg, 914 ip: ip_address, 915 domain: wplc_domain, 916 wplc_extra_data:wplc_extra_data 917 }; 918 919 jQuery.post(wplc_ajaxurl_site, data, function(response) { 920 jQuery("#wplc_message_div").html(wplc_offline_msg3); 921 }); 922 if (typeof wplc_enable_ga !== "undefined" && wplc_enable_ga === '1') { 923 if (typeof ga !== "undefined") { 924 ga('send', { 925 hitType: 'event', 926 eventCategory: 'WP_Live_Chat_Support', 927 eventAction: 'Event', 928 eventLabel: 'User Send Offline Message' 929 }); 930 } 931 } 932 }); 933 934 935 function wplc_node_renew_typing() { 936 clearTimeout(wplc_node_searchTimeout); 937 wplc_node_searchTimeout = setTimeout(wplc_node_clear_typing, 1000); 938 } 939 function wplc_node_clear_typing() { 940 wplc_node_is_client_typing = false; 941 clearTimeout(wplc_node_searchTimeout); 942 } 943 }); 944}); 945 946var wplc_generate_system_notification_object = function(msg,other,originates) { 947 the_message = {}; 948 the_message.originates = originates; 949 the_message.msg = msg; 950 the_message.other = other; 951 var wplc_d = new Date(); 952 the_message.other.datetime = Math.round( wplc_d.getTime() / 1000 ); 953 return the_message; 954} 955 956function wplc_display_system_notification(message) { 957 message_output = '<div id="bleeper_notification_message" class="wplc-color-bg-1 wplc-color-2">' + message.msg + '</div>'; 958 959 if (jQuery('#bleeper_notification_message').length == 0) { 960 jQuery('#wp-live-chat-4').prepend(message_output); 961 } 962} 963 964function wplc_clear_system_notification() { 965 if (jQuery('#bleeper_notification_message').length !== 0) { 966 jQuery('#bleeper_notification_message').remove(); 967 } 968} 969 970/* 971 * Updates the message element with a new message. 972 * - mid: Message ID which is appended to class selector as .message_{mid} 973 * - message: The html/text to insert into this container 974 * - inner_container_class: Optional, used to select the first child of the class type within the primary message container 975 * Nice for selecting the 'messageBody' class specifically 976*/ 977function wplc_update_message_element_by_mid(mid, message, inner_container_class){ 978 var element_selector = ".message_" + mid; 979 element_selector += (typeof inner_container_class !== "undefined" && inner_container_class !== false) ? (" ." + inner_container_class) : ""; 980 981 jQuery(element_selector).html(message); 982}
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.