1 /* -*- Mode: c++; -*- */
2 /*  --------------------------------------------------------------------
3  *  Filename:
4  *    address.cc
5  *
6  *  Description:
7  *    Implementation of the Address class.
8  *
9  *  Authors:
10  *    Andreas Aardal Hanssen <andreas-binc curly bincimap spot org>
11  *
12  *  Bugs:
13  *
14  *  ChangeLog:
15  *
16  *  --------------------------------------------------------------------
17  *  Copyright 2002-2005 Andreas Aardal Hanssen
18  *
19  *  This program is free software; you can redistribute it and/or modify
20  *  it under the terms of the GNU General Public License as published by
21  *  the Free Software Foundation; either version 2 of the License, or
22  *  (at your option) any later version.
23  *
24  *  This program is distributed in the hope that it will be useful,
25  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
26  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
27  *  GNU General Public License for more details.
28  *
29  *  You should have received a copy of the GNU General Public License
30  *  along with this program; if not, write to the Free Software
31  *  Foundation, Inc., 59 Temple Street #330, Boston, MA 02111-1307, USA.
32  *  --------------------------------------------------------------------
33  */
34 #ifdef HAVE_CONFIG_H
35 #include <config.h>
36 #endif
37 
38 #include "address.h"
39 #include "convert.h"
40 #include <string>
41 
42 using namespace ::std;
43 using namespace Binc;
44 
45 //------------------------------------------------------------------------
Address(const string & name,const string & addr)46 Address::Address(const string &name, const string &addr)
47 {
48   string::size_type pos = addr.find('@');
49   this->name = name;
50   if (pos != string::npos) {
51     this->local = addr.substr(0, pos);
52     this->host = addr.substr(pos + 1);
53   } else this->local = addr;
54 }
55 
56 //------------------------------------------------------------------------
Address(const string & wholeaddress)57 Address::Address(const string &wholeaddress)
58 {
59   string::size_type start = wholeaddress.find('<');
60   string addr;
61   if (start != string::npos)
62     addr = wholeaddress.substr(start + 1);
63   else
64     addr = wholeaddress;
65 
66   trim(addr, "<>");
67 
68   if (start != string::npos)
69     name = wholeaddress.substr(0, start);
70   else
71     name = "";
72   trim(name);
73   trim(name, "\"");
74 
75   start = addr.find('@');
76   local = addr.substr(0, start);
77   host = addr.substr(start + 1);
78 
79   trim(local);
80   trim(host);
81   trim(name);
82 }
83 
84 //------------------------------------------------------------------------
toParenList(void) const85 string Address::toParenList(void) const
86 {
87   string tmp = "(";
88   tmp += name == "" ? "NIL" : toImapString(name);
89   tmp += " NIL ";
90   tmp += local == "" ? "\"\"" : toImapString(local);
91   tmp += " ";
92   tmp += host == "" ? "\"\"" : toImapString(host);
93   tmp += ")";
94 
95   return tmp;
96 }
97