summaryrefslogtreecommitdiff
path: root/src/address.cc
blob: 94c812f2d792d3f4d26ac7df5d80e29ff1e8b962 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/**
 * @file   address.cc
 * @brief  Implementation of the Address class
 * @author Andreas Aardal Hanssen
 * @date   2005
 */

#include "address.h"

#include "convert.h"

#include <string>

using namespace Binc;
using std::string;
using std::string_view;

Address Address::from(string name, string_view addr)
{
  if (auto pos = addr.find('@'); pos != string::npos)
    return Address{std::move(name), string{addr.substr(0, pos)}, string{addr.substr(pos + 1)}};
  else
    return Address{std::move(name), string{addr}};
}

Address Address::from(string_view wholeaddress)
{
  auto start = wholeaddress.find('<');
  auto addr = start != string::npos ? wholeaddress.substr(start + 1) : wholeaddress;
  auto name = start != string::npos ? wholeaddress.substr(0, start) : "";

  trim(addr, "<>");
  trim(name);
  trim(name, "\"");

  start = addr.find('@');
  auto local = addr.substr(0, start);
  auto host = addr.substr(start + 1);

  trim(local);
  trim(host);

  return Address{string{name}, string{local}, string{host}};
}

string Address::toParenList() const
{
  string tmp = "(";
  tmp += name == "" ? "NIL" : toImapString(name);
  tmp += " NIL ";
  tmp += local == "" ? "\"\"" : toImapString(local);
  tmp += " ";
  tmp += host == "" ? "\"\"" : toImapString(host);
  tmp += ")";

  return tmp;
}