i18n work in progress... desperately needs some housecleaning.
[barry.git] / src / ldif.h
blob2054b9086ed15b376373b0f0d7b8e8dcdfd6e937
1 ///
2 /// \file ldif.h
3 /// Routines for reading and writing LDAP LDIF data.
4 ///
6 /*
7 Copyright (C) 2005-2008, 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 "dll.h"
26 #include <string>
27 #include <map>
29 // forward declarations
30 namespace Barry {
31 class Contact;
34 namespace Barry {
37 // ContactLdif
39 /// Class for generating LDIF output based on a Barry::Contact record object.
40 /// This class supports LDIF attribute mapping, and a heuristics mechanism
41 /// for parsing LDIF fields that may not have consistent data.
42 ///
43 /// To use this class, create an instance of it, then call DumpLdif(), passing
44 /// the Contact record object to base the work on. Output will be written
45 /// to the stream you provide. ReadLdif() goes in the other direction.
46 ///
47 /// To override LDIF attribute mapping, call Map() or Unmap() as appropriate.
48 ///
49 /// To get a list of supported Barry::Contact field names, call GetFieldNames().
50 /// This function returns a pointer to an array of ContactLdif::NameToFunc
51 /// structures, ending with NameToFunc::name as null. You can cycle through the
52 /// array with code like this:
53 ///
54 /// <pre>
55 /// for( ContactLdif::NameToFunc *n = o.GetFieldNames(); n->name; n++ ) {
56 /// ...
57 /// }
58 /// </pre>
59 ///
60 /// Note that all Get/Set functions used in attribute mapping are virtual,
61 /// and can be overridden by a derived class. This includes the heuristics
62 /// functions, which are called by DumpLdif().
63 ///
64 class BXEXPORT ContactLdif
66 public:
67 typedef std::string (ContactLdif::*GetFunctionType)(const Barry::Contact&) const;
68 typedef void (ContactLdif::*SetFunctionType)(Barry::Contact&, const std::string &) const;
70 /// Used to create a List of supported Barry field names, including
71 /// calculated names, such as full postal address.
72 struct NameToFunc
74 const char *name;
75 const char *description;
76 GetFunctionType read;
77 SetFunctionType write;
80 struct LdifAttribute
82 std::string name;
83 std::string objectClass;
84 int order;
86 LdifAttribute() : order(0) {}
87 LdifAttribute(const char *name, const std::string &oc = "")
88 : name(name), objectClass(oc), order(0)
90 LdifAttribute(const std::string &name, const std::string &oc = "")
91 : name(name), objectClass(oc), order(0)
94 bool operator<(const LdifAttribute &other) const;
95 bool operator==(const LdifAttribute &other) const;
98 struct AccessPair
100 GetFunctionType read;
101 SetFunctionType write;
103 AccessPair() : read(0), write(0) {}
104 AccessPair(GetFunctionType r, SetFunctionType w)
105 : read(r), write(w)
109 typedef std::map<LdifAttribute, AccessPair> AccessMapType;
110 typedef std::map<std::string, std::string*> HookMapType;
112 protected:
113 static const NameToFunc FieldMap[];
114 AccessMapType m_map;
115 std::string m_baseDN;
116 HookMapType m_hookMap;
117 LdifAttribute m_dnAttr;
119 void DoWrite(Barry::Contact &con, const std::string &attr,
120 const std::string &data);
122 // name heuristics
123 std::string m_cn, m_displayName, m_sn, m_givenName;
125 // heuristics hooking - saves each found value in the variable
126 // pointed at by var
127 void Hook(const std::string &ldifname, std::string *var);
129 public:
130 explicit ContactLdif(const std::string &baseDN);
131 virtual ~ContactLdif();
133 const NameToFunc* GetFieldNames() const { return FieldMap; }
134 const NameToFunc* GetField(const std::string &fieldname) const;
135 std::string GetFieldReadName(GetFunctionType read) const;
136 std::string GetFieldWriteName(SetFunctionType write) const;
138 bool Map(const LdifAttribute &ldifname, const std::string &readField,
139 const std::string &writeField);
140 void Map(const LdifAttribute &ldifname, GetFunctionType read,
141 SetFunctionType write);
142 void Unmap(const LdifAttribute &ldifname);
144 void SetBaseDN(const std::string &baseDN) { m_baseDN = baseDN; }
145 bool SetDNAttr(const LdifAttribute &name);
146 bool SetObjectClass(const LdifAttribute &name, const std::string &objectClass);
147 bool SetObjectOrder(const LdifAttribute &name, int order);
150 // Access functions
153 virtual std::string Email(const Barry::Contact &con) const;
154 virtual std::string Phone(const Barry::Contact &con) const;
155 virtual std::string Fax(const Barry::Contact &con) const;
156 virtual std::string WorkPhone(const Barry::Contact &con) const;
157 virtual std::string HomePhone(const Barry::Contact &con) const;
158 virtual std::string MobilePhone(const Barry::Contact &con) const;
159 virtual std::string Pager(const Barry::Contact &con) const;
160 virtual std::string PIN(const Barry::Contact &con) const;
161 virtual std::string FirstName(const Barry::Contact &con) const;
162 virtual std::string LastName(const Barry::Contact &con) const;
163 virtual std::string Company(const Barry::Contact &con) const;
164 virtual std::string DefaultCommunicationsMethod(const Barry::Contact &con) const;
165 virtual std::string Address1(const Barry::Contact &con) const;
166 virtual std::string Address2(const Barry::Contact &con) const;
167 virtual std::string Address3(const Barry::Contact &con) const;
168 virtual std::string City(const Barry::Contact &con) const;
169 virtual std::string Province(const Barry::Contact &con) const;
170 virtual std::string PostalCode(const Barry::Contact &con) const;
171 virtual std::string Country(const Barry::Contact &con) const;
172 virtual std::string JobTitle(const Barry::Contact &con) const;
173 virtual std::string PublicKey(const Barry::Contact &con) const;
174 virtual std::string Notes(const Barry::Contact &con) const;
175 // calculated values...
176 virtual std::string PostalAddress(const Barry::Contact &con) const;
177 virtual std::string FullName(const Barry::Contact &con) const;
178 virtual std::string FQDN(const Barry::Contact &con) const;
181 // Write functions
184 virtual void SetEmail(Barry::Contact &con, const std::string &val) const;
185 virtual void SetPhone(Barry::Contact &con, const std::string &val) const;
186 virtual void SetFax(Barry::Contact &con, const std::string &val) const;
187 virtual void SetWorkPhone(Barry::Contact &con, const std::string &val) const;
188 virtual void SetHomePhone(Barry::Contact &con, const std::string &val) const;
189 virtual void SetMobilePhone(Barry::Contact &con, const std::string &val) const;
190 virtual void SetPager(Barry::Contact &con, const std::string &val) const;
191 virtual void SetPIN(Barry::Contact &con, const std::string &val) const;
192 virtual void SetFirstName(Barry::Contact &con, const std::string &val) const;
193 virtual void SetLastName(Barry::Contact &con, const std::string &val) const;
194 virtual void SetCompany(Barry::Contact &con, const std::string &val) const;
195 virtual void SetDefaultCommunicationsMethod(Barry::Contact &con, const std::string &val) const;
196 virtual void SetAddress1(Barry::Contact &con, const std::string &val) const;
197 virtual void SetAddress2(Barry::Contact &con, const std::string &val) const;
198 virtual void SetAddress3(Barry::Contact &con, const std::string &val) const;
199 virtual void SetCity(Barry::Contact &con, const std::string &val) const;
200 virtual void SetProvince(Barry::Contact &con, const std::string &val) const;
201 virtual void SetPostalCode(Barry::Contact &con, const std::string &val) const;
202 virtual void SetCountry(Barry::Contact &con, const std::string &val) const;
203 virtual void SetJobTitle(Barry::Contact &con, const std::string &val) const;
204 virtual void SetPublicKey(Barry::Contact &con, const std::string &val) const;
205 virtual void SetNotes(Barry::Contact &con, const std::string &val) const;
206 virtual void SetPostalAddress(Barry::Contact &con, const std::string &val) const;
207 virtual void SetFullName(Barry::Contact &con, const std::string &val) const;
208 virtual void SetFQDN(Barry::Contact &con, const std::string &val) const;
212 // Name heuristics
215 virtual void ClearHeuristics();
216 virtual bool RunHeuristics(Barry::Contact &con);
219 // Operations
222 void DumpLdif(std::ostream &os, const Barry::Contact &contact) const;
223 bool ReadLdif(std::istream &is, Barry::Contact &contact);
224 // returns true on success
225 void DumpMap(std::ostream &os) const;
228 static std::string MakeLdifData(const std::string &str);
229 static bool NeedsEncoding(const std::string &str);
232 BXEXPORT inline std::ostream& operator<< (std::ostream &os, const ContactLdif &ldif) {
233 ldif.DumpMap(os);
234 return os;
237 } // namespace Barry
239 #endif