1 /*
  2  * ***** BEGIN LICENSE BLOCK *****
  3  * Zimbra Collaboration Suite Web Client
  4  * Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2013, 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) 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2013, 2014, 2016 Synacor, Inc. All Rights Reserved.
 21  * ***** END LICENSE BLOCK *****
 22  */
 23 
 24 /**
 25  * @class
 26  * This class represents a list of domains that have shown up in email addresses.
 27  * 
 28  * @extends		ZmModel
 29  */
 30 ZmDomainList = function() {
 31 	ZmModel.call(this);
 32 };
 33 
 34 ZmDomainList.prototype = new ZmModel;
 35 ZmDomainList.prototype.constructor = ZmDomainList;
 36 
 37 ZmDomainList.prototype.isZmDomainList = true;
 38 ZmDomainList.prototype.toString = function() { return "ZmDomainList"; };
 39 
 40 ZmDomainList.DOMAIN_RE = new RegExp("\\.\\w{2,3}$");
 41 
 42 /**
 43  * Gets a sorted list of domains matching the given string (if any).
 44  * 
 45  * @param {String}		str			the string to search for
 46  * @param {int}			limit		the max number of domains to return
 47  * @param {AjxCallback}	callback	the callback to run when response is received
 48  */
 49 ZmDomainList.prototype.search =
 50 function(str, limit, callback) {
 51 
 52 	var jsonObj = {BrowseRequest:{_jsns:"urn:zimbraMail"}};
 53 	var request = jsonObj.BrowseRequest;
 54 	request.browseBy = "domains";
 55 	if (str && (/[a-z]/i.test(str))) {
 56 		if (ZmDomainList.DOMAIN_RE.test(str)) {
 57 			request.regex = [".*", AjxStringUtil.regExEscape(str), "$"].join("");
 58 		} else {
 59 			request.regex = ["^", str, ".*"].join("");
 60 		}
 61 	}
 62 	if (limit) {
 63 		request.maxToReturn = limit;
 64 	}
 65 	var respCallback = ZmDomainList._handleResponseSearch.bind(null, str, callback);
 66 	appCtxt.getAppController().sendRequest({jsonObj:jsonObj, asyncMode:true, callback:respCallback});
 67 };
 68 
 69 ZmDomainList._handleResponseSearch =
 70 function(str, callback, result) {
 71 	var domains = result.getResponse().BrowseResponse.bd;
 72 	var list = [];
 73 	if (domains) {
 74 		for (var i = 0; i < domains.length; i++) {
 75 			var domain = domains[i];
 76 			list[i] = new ZmDomain(domain._content, domain.h);
 77 		}
 78 	}
 79 	list.sort(ZmDomain.sortCompare);
 80 	callback.run(list);
 81 };
 82 
 83 
 84 
 85 
 86 /**
 87  * @class
 88  * This class represents a domain.
 89  * 
 90  * @param	{String}	name			the name
 91  * @param	{String}	headerFlags		header flags (where domain was found)
 92  * 
 93  * @extends	ZmModel
 94  */
 95 ZmDomain = function(name, headerFlags) {
 96 	
 97 	ZmModel.call(this);
 98 
 99 	this.name = name.toLowerCase();
100 	this._headerFlags = headerFlags;
101 };
102 
103 ZmDomain.prototype = new ZmModel;
104 ZmDomain.prototype.constructor = ZmDomain;
105 
106 ZmDomain.prototype.isZmDomain = true;
107 ZmDomain.prototype.toString = function() { return "ZmDomain"; };
108 
109 
110 ZmDomain.ADDR_FLAG = {};
111 ZmDomain.ADDR_FLAG[AjxEmailAddress.FROM]	= "f";
112 ZmDomain.ADDR_FLAG[AjxEmailAddress.TO]		= "t";
113 ZmDomain.ADDR_FLAG[AjxEmailAddress.CC]		= "c";
114 
115 
116 /**
117  * Compares two domains by name.
118  * 
119  * @param	{ZmDomain}	a		the first domain
120  * @param	{ZmDomain}	b		the second domain
121  * @return	{int}	0 if the domains match; 1 if "a" is before "b"; -1 if "b" is before "a"
122  */
123 ZmDomain.sortCompare = 
124 function(a, b) {
125 	var check = ZmOrganizer.checkSortArgs(a, b);
126 	if (check != null) { return check; }
127 
128 	if (a.name < b.name) { return -1; }
129 	if (a.name > b.name) { return 1; }
130 	return 0;
131 };
132 
133 ZmDomain.prototype.hasAddress =
134 function(addressType) {
135 	var flag = ZmDomain.ADDR_FLAG[addressType];
136 	return flag && this._headerFlags && (this._headerFlags.indexOf(flag) != -1);
137 };
138