/wrt-plugins-tizen/src/platform/Tizen/Messaging/EmailUtils.cpp
C++ | 96 lines | 67 code | 11 blank | 18 comment | 6 complexity | a982b039b1f09aeb6aa54702baaf373b MD5 | raw file
Possible License(s): GPL-3.0, AGPL-3.0, GPL-2.0, MPL-2.0, JSON, WTFPL, CC-BY-SA-4.0, CC-BY-3.0, BSD-3-Clause, LGPL-2.0, MPL-2.0-no-copyleft-exception, AGPL-1.0, 0BSD, Zlib, Unlicense, BSD-2-Clause, Apache-2.0, LGPL-3.0, ISC, MIT, CC-BY-SA-3.0, CC0-1.0, LGPL-2.1
- /*
- * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- /**
- * @author Zbigniew Kostrzewa (z.kostrzewa@samsung.com)
- */
- #include <sstream>
- #include <pcrecpp.h>
- #include <Commons/Exception.h>
- #include <Commons/StringBuilder.h>
- #include <Commons/StringUtils.h>
- #include "EmailUtils.h"
- using namespace WrtDeviceApis::Commons;
- namespace {
- const char* EMAIL_ADDRESS_REGEX =
- "[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,6}";
- const std::string EMAIL_ADDRESS_REGEX_GROUP =
- StringBuilder()
- .append("(")
- .append(EMAIL_ADDRESS_REGEX)
- .append(")")
- .toString();
- }
- namespace TizenApis {
- namespace Platform {
- namespace Messaging {
- namespace EmailUtils {
- std::string formatAddress(const std::string& address,
- const std::string& name)
- {
- if (address.empty()) {
- ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Address is empty.");
- }
- std::stringstream ss;
- if (!name.empty()) {
- ss << "\"" << name << "\" ";
- }
- ss << "<" << address << ">";
- return ss.str();
- }
- std::string formatAddressLine(const Api::Messaging::Recipients& recipients)
- {
- std::string result;
- for (size_t i = 0; i < recipients.getRecipientSize(); ++i) {
- result += formatAddress(recipients.getRecipient(i)) + ";";
- }
- return result;
- }
- std::string stripAddress(const std::string& address)
- {
- std::string result;
- pcrecpp::RE re(EMAIL_ADDRESS_REGEX_GROUP);
- if (!re.PartialMatch(address, &result)) {
- ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Couldn't discover email address");
- }
- return result;
- }
- Api::Messaging::Recipients stripAddressLine(const std::string& addressLine)
- {
- typedef std::vector<std::string> Addresses;
- Api::Messaging::Recipients result;
- Addresses addresses = String::split(addressLine, ';');
- for (Addresses::iterator it = addresses.begin();
- it != addresses.end();
- ++it) {
- result.appendRecipient(stripAddress(*it));
- }
- return result;
- }
- }
- }
- }
- }