2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 * @author Zbigniew Kostrzewa (z.kostrzewa@samsung.com)
22 #include <Commons/Exception.h>
23 #include <Commons/StringBuilder.h>
24 #include <Commons/StringUtils.h>
25 #include "EmailUtils.h"
28 const char* EMAIL_ADDRESS_REGEX =
29 "[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,6}";
31 using WrtDeviceApis::Commons::StringBuilder;
33 const std::string EMAIL_ADDRESS_REGEX_GROUP =
36 .append(EMAIL_ADDRESS_REGEX)
41 namespace WrtDeviceApis {
43 namespace EmailUtils {
44 std::string formatAddress(const std::string& address,
45 const std::string& name)
47 if (address.empty()) {
48 ThrowMsg(Commons::PlatformException, "Address is empty.");
53 ss << "\"" << name << "\" ";
55 ss << "<" << address << ">";
60 std::string formatAddressLine(const Api::Recipients& recipients)
63 for (size_t i = 0; i < recipients.getRecipientSize(); ++i) {
64 result += formatAddress(recipients.getRecipient(i)) + ";";
69 std::string stripAddress(const std::string& address)
72 pcrecpp::RE re(EMAIL_ADDRESS_REGEX_GROUP);
73 if (!re.PartialMatch(address, &result)) {
74 ThrowMsg(Commons::PlatformException, "Couldn't discover email address");
79 Api::Recipients stripAddressLine(const std::string& addressLine)
81 typedef std::vector<std::string> Addresses;
83 Api::Recipients result;
84 Addresses addresses = Commons::String::split(addressLine, ';');
85 for (Addresses::iterator it = addresses.begin();
86 it != addresses.end();
88 result.appendRecipient(stripAddress(*it));