1 /*
  2  * ***** BEGIN LICENSE BLOCK *****
  3  * Zimbra Collaboration Suite Web Client
  4  * Copyright (C) 2014, 2016 Synacor, Inc.
  5  *
  6  * The contents of this file are subject to the Common Public Attribution License Version 1.0 (the "License");
  7  * you may not use this file except in compliance with the License.
  8  * You may obtain a copy of the License at: https://www.zimbra.com/license
  9  * The License is based on the Mozilla Public License Version 1.1 but Sections 14 and 15
 10  * have been added to cover use of software over a computer network and provide for limited attribution
 11  * for the Original Developer. In addition, Exhibit A has been modified to be consistent with Exhibit B.
 12  *
 13  * Software distributed under the License is distributed on an "AS IS" basis,
 14  * WITHOUT WARRANTY OF ANY KIND, either express or implied.
 15  * See the License for the specific language governing rights and limitations under the License.
 16  * The Original Code is Zimbra Open Source Web Client.
 17  * The Initial Developer of the Original Code is Zimbra, Inc.  All rights to the Original Code were
 18  * transferred by Zimbra, Inc. to Synacor, Inc. on September 14, 2015.
 19  *
 20  * All portions of the code are Copyright (C) 2014, 2016 Synacor, Inc. All Rights Reserved.
 21  * ***** END LICENSE BLOCK *****
 22  */
 23 
 24 /**
 25  * @class
 26  * Internal handler for email addresses.
 27  *
 28  * @author Conrad Damon
 29  * @extends	ZmObjectHandler
 30  */
 31 ZmEmailObjectHandler = function() {
 32 	ZmObjectHandler.call(this, 'email');
 33 };
 34 
 35 ZmEmailObjectHandler.prototype = new ZmObjectHandler;
 36 ZmEmailObjectHandler.prototype.constructor = ZmEmailObjectHandler;
 37 
 38 ZmEmailObjectHandler.prototype.isZmEmailObjectHandler = true;
 39 ZmEmailObjectHandler.prototype.toString = function() {
 40 	return 'ZmEmailObjectHandler';
 41 };
 42 
 43 // email regex that recognizes mailto: links as well
 44 //ZmEmailObjectHandler.RE = /\b(mailto:[ ]*)?([0-9a-zA-Z]+[.&#!$%'*+-\/=?^_`{}|~])*[0-9a-zA-Z_-]+@([-0-9a-zA-Z]+[.])+[a-zA-Z]{2,6}([\w\/_\.]*(\?\S+)?)/gi;
 45 ZmEmailObjectHandler.RE = /\b(mailto:[ ]*)?([0-9a-zA-Z\u00C0-\u00ff]+[.&#!$%'*+-\/=?^_`{}|~])*[0-9a-zA-Z_-\u00C0-\u00ff]+@([-0-9a-zA-Z]+[.])+[a-zA-Z]{2,6}([\w\/_\.]*(\?\S+)?)/gi;
 46 
 47 
 48 ZmEmailObjectHandler.prototype.match = function(content, startIndex, objectMgr) {
 49 
 50 	ZmEmailObjectHandler.RE.lastIndex = startIndex;
 51 	var ret = ZmEmailObjectHandler.RE.exec(content);
 52 	if (ret) {
 53 		ret.context = ret;
 54 		ret.objectMgr = objectMgr;  // obj mgr can get us back to containing view
 55 	}
 56 	return ret;
 57 };
 58 
 59 // See if a zimlet wants to handle email hover; if not, do the default thing
 60 ZmEmailObjectHandler.prototype.hoverOver = function(object, context, x, y, span) {
 61 	object = AjxStringUtil.parseMailtoLink(object).to;
 62 	if (!appCtxt.notifyZimlets('onEmailHover', [ object, context, x, y, span ])) {
 63 		ZmObjectHandler.prototype.hoverOver.apply(this, arguments);
 64 	}
 65 };
 66 
 67 ZmEmailObjectHandler.prototype.getToolTipText = function(obj, context) {
 68 
 69 	// Return a callback since we may need to make an async request to get data for the tooltip content.
 70 	return new AjxCallback(this,
 71 		function(callback) {
 72 			appCtxt.getToolTipMgr().getToolTip(ZmToolTipMgr.PERSON, { address: AjxStringUtil.parseMailtoLink(obj).to }, callback);
 73 		});
 74 };
 75 
 76 // Left-click starts a compose session
 77 ZmEmailObjectHandler.prototype.clicked = function(spanElement, contentObjText, matchContext, ev) {
 78 
 79 	var	ctlr = this._getController(matchContext),
 80 		parts = AjxStringUtil.parseMailtoLink(contentObjText);
 81 
 82 	var params = {
 83 		action:         ZmOperation.NEW_MESSAGE,
 84 		inNewWindow:    ctlr && ctlr._app && ctlr._app._inNewWindow(ev),
 85 		toOverride:     parts.to,
 86 		subjOverride:   parts.subject,
 87 		extraBodyText:  parts.body
 88 	};
 89 
 90 	AjxDispatcher.run("Compose", params);
 91 };
 92 
 93 // Borrow the bubble action menu from the owning controller. Object framework doesn't explicitly call an
 94 // action listener, so we do it here.
 95 ZmEmailObjectHandler.prototype.getActionMenu = function(obj, span, context, ev) {
 96 
 97 	var ctlr = this._getController(context);
 98 	if (!ctlr) {
 99 		return null;
100 	}
101 
102 	ctlr._actionEv = ev;
103 	ctlr._actionEv.address = AjxStringUtil.parseMailtoLink(obj).to;
104 	ctlr._actionEv.handler = this;
105 
106 	if (!this._actionMenu && ctlr._getBubbleActionMenu) {
107 		this._actionMenu = ctlr._getBubbleActionMenu();
108 	}
109 
110 	ctlr._bubbleActionListener(ev, obj);
111 
112 	return this._actionMenu;
113 };
114 
115 ZmEmailObjectHandler.prototype._getController = function(context) {
116 
117 	var om = context && context.objectMgr,
118 		view = om && om.getView();
119 
120 	return view && view._controller;
121 };
122 
123 // Tell the object framework we're here. The 'email' type arg is probably not needed.
124 ZmObjectManager.registerHandler("ZmEmailObjectHandler", 'email', 4);
125