Release tarball for barry-0.9
[barry.git] / src / ldif.h
bloba8d19a95fdeb2d984575daa2465e08627423737c
1 ///
2 /// \file ldif.h
3 /// Routines for reading and writing LDAP LDIF data.
4 ///
6 /*
7 Copyright (C) 2005-2007, Net Direct Inc. (http://www.netdirect.ca/)
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
18 See the GNU General Public License in the COPYING file at the
19 root directory of this project for more details.
22 #ifndef __BARRY_LDIF_H__
23 #define __BARRY_LDIF_H__
25 #include <string>
26 #include <map>
28 // forward declarations
29 namespace Barry {
30 class Contact;
33 namespace Barry {
36 // ContactLdif
38 /// Class for generating LDIF output based on a Barry::Contact record object.
39 /// This class supports LDIF attribute mapping, and a heuristics mechanism
40 /// for parsing LDIF fields that may not have consistent data.
41 ///
42 /// To use this class, create an instance of it, then call DumpLdif(), passing
43 /// the Contact record object to base the work on. Output will be written
44 /// to the stream you provide. ReadLdif() goes in the other direction.
45 ///
46 /// To override LDIF attribute mapping, call Map() or Unmap() as appropriate.
47 ///
48 /// To get a list of supported Barry::Contact field names, call GetFieldNames().
49 /// This function returns a pointer to an array of ContactLdif::NameToFunc
50 /// structures, ending with NameToFunc::name as null. You can cycle through the
51 /// array with code like this:
52 ///
53 /// <pre>
54 /// for( ContactLdif::NameToFunc *n = o.GetFieldNames(); n->name; n++ ) {
55 /// ...
56 /// }
57 /// </pre>
58 ///
59 /// Note that all Get/Set functions used in attribute mapping are virtual,
60 /// and can be overridden by a derived class. This includes the heuristics
61 /// functions, which are called by DumpLdif().
62 ///
63 class ContactLdif
65 public:
66 typedef std::string (ContactLdif::*GetFunctionType)(const Barry::Contact&) const;
67 typedef void (ContactLdif::*SetFunctionType)(Barry::Contact&, const std::string &) const;
69 /// Used to create a List of supported Barry field names, including
70 /// calculated names, such as full postal address.
71 struct NameToFunc
73 const char *name;
74 const char *description;
75 GetFunctionType read;
76 SetFunctionType write;
79 struct LdifAttribute
81 std::string name;
82 std::string objectClass;
83 int order;
85 LdifAttribute() : order(0) {}
86 LdifAttribute(const char *name, const std::string &oc = "")
87 : name(name), objectClass(oc), order(0)
89 LdifAttribute(const std::string &name, const std::string &oc = "")
90 : name(name), objectClass(oc), order(0)
93 bool operator<(const LdifAttribute &other) const;
94 bool operator==(const LdifAttribute &other) const;
97 struct AccessPair
99 GetFunctionType read;
100 SetFunctionType write;
102 AccessPair() : read(0), write(0) {}
103 AccessPair(GetFunctionType r, SetFunctionType w)
104 : read(r), write(w)
108 typedef std::map<LdifAttribute, AccessPair> AccessMapType;
109 typedef std::map<std::string, std::string*> HookMapType;
111 protected:
112 static const NameToFunc FieldMap[];
113 AccessMapType m_map;
114 std::string m_baseDN;
115 HookMapType m_hookMap;
116 LdifAttribute m_dnAttr;
118 void DoWrite(Barry::Contact &con, const std::string &attr,
119 const std::string &data);
121 // name heuristics
122 std::string m_cn, m_displayName, m_sn, m_givenName;
124 // heuristics hooking - saves each found value in the variable
125 // pointed at by var
126 void Hook(const std::string &ldifname, std::string *var);
128 public:
129 explicit ContactLdif(const std::string &baseDN);
130 virtual ~ContactLdif();
132 const NameToFunc* GetFieldNames() const { return FieldMap; }
133 const NameToFunc* GetField(const std::string &fieldname) const;
134 std::string GetFieldReadName(GetFunctionType read) const;
135 std::string GetFieldWriteName(SetFunctionType write) const;
137 bool Map(const LdifAttribute &ldifname, const std::string &readField,
138 const std::string &writeField);
139 void Map(const LdifAttribute &ldifname, GetFunctionType read,
140 SetFunctionType write);
141 void Unmap(const LdifAttribute &ldifname);
143 void SetBaseDN(const std::string &baseDN) { m_baseDN = baseDN; }
144 bool SetDNAttr(const LdifAttribute &name);
145 bool SetObjectClass(const LdifAttribute &name, const std::string &objectClass);
146 bool SetObjectOrder(const LdifAttribute &name, int order);
149 // Access functions
152 virtual std::string Email(const Barry::Contact &con) const;
153 virtual std::string Phone(const Barry::Contact &con) const;
154 virtual std::string Fax(const Barry::Contact &con) const;
155 virtual std::string WorkPhone(const Barry::Contact &con) const;
156 virtual std::string HomePhone(const Barry::Contact &con) const;
157 virtual std::string MobilePhone(const Barry::Contact &con) const;
158 virtual std::string Pager(const Barry::Contact &con) const;
159 virtual std::string PIN(const Barry::Contact &con) const;
160 virtual std::string FirstName(const Barry::Contact &con) const;
161 virtual std::string LastName(const Barry::Contact &con) const;
162 virtual std::string Company(const Barry::Contact &con) const;
163 virtual std::string DefaultCommunicationsMethod(const Barry::Contact &con) const;
164 virtual std::string Address1(const Barry::Contact &con) const;
165 virtual std::string Address2(const Barry::Contact &con) const;
166 virtual std::string Address3(const Barry::Contact &con) const;
167 virtual std::string City(const Barry::Contact &con) const;
168 virtual std::string Province(const Barry::Contact &con) const;
169 virtual std::string PostalCode(const Barry::Contact &con) const;
170 virtual std::string Country(const Barry::Contact &con) const;
171 virtual std::string JobTitle(const Barry::Contact &con) const;
172 virtual std::string PublicKey(const Barry::Contact &con) const;
173 virtual std::string Notes(const Barry::Contact &con) const;
174 // calculated values...
175 virtual std::string PostalAddress(const Barry::Contact &con) const;
176 virtual std::string FullName(const Barry::Contact &con) const;
177 virtual std::string FQDN(const Barry::Contact &con) const;
180 // Write functions
183 virtual void SetEmail(Barry::Contact &con, const std::string &val) const;
184 virtual void SetPhone(Barry::Contact &con, const std::string &val) const;
185 virtual void SetFax(Barry::Contact &con, const std::string &val) const;
186 virtual void SetWorkPhone(Barry::Contact &con, const std::string &val) const;
187 virtual void SetHomePhone(Barry::Contact &con, const std::string &val) const;
188 virtual void SetMobilePhone(Barry::Contact &con, const std::string &val) const;
189 virtual void SetPager(Barry::Contact &con, const std::string &val) const;
190 virtual void SetPIN(Barry::Contact &con, const std::string &val) const;
191 virtual void SetFirstName(Barry::Contact &con, const std::string &val) const;
192 virtual void SetLastName(Barry::Contact &con, const std::string &val) const;
193 virtual void SetCompany(Barry::Contact &con, const std::string &val) const;
194 virtual void SetDefaultCommunicationsMethod(Barry::Contact &con, const std::string &val) const;
195 virtual void SetAddress1(Barry::Contact &con, const std::string &val) const;
196 virtual void SetAddress2(Barry::Contact &con, const std::string &val) const;
197 virtual void SetAddress3(Barry::Contact &con, const std::string &val) const;
198 virtual void SetCity(Barry::Contact &con, const std::string &val) const;
199 virtual void SetProvince(Barry::Contact &con, const std::string &val) const;
200 virtual void SetPostalCode(Barry::Contact &con, const std::string &val) const;
201 virtual void SetCountry(Barry::Contact &con, const std::string &val) const;
202 virtual void SetJobTitle(Barry::Contact &con, const std::string &val) const;
203 virtual void SetPublicKey(Barry::Contact &con, const std::string &val) const;
204 virtual void SetNotes(Barry::Contact &con, const std::string &val) const;
205 virtual void SetPostalAddress(Barry::Contact &con, const std::string &val) const;
206 virtual void SetFullName(Barry::Contact &con, const std::string &val) const;
207 virtual void SetFQDN(Barry::Contact &con, const std::string &val) const;
211 // Name heuristics
214 virtual void ClearHeuristics();
215 virtual bool RunHeuristics(Barry::Contact &con);
218 // Operations
221 void DumpLdif(std::ostream &os, const Barry::Contact &contact) const;
222 bool ReadLdif(std::istream &is, Barry::Contact &contact);
223 // returns true on success
224 void DumpMap(std::ostream &os) const;
227 static std::string MakeLdifData(const std::string &str);
228 static bool NeedsEncoding(const std::string &str);
231 inline std::ostream& operator<< (std::ostream &os, const ContactLdif &ldif) {
232 ldif.DumpMap(os);
233 return os;
236 } // namespace Barry
238 #endif