- added src/endian.h... still need to add configure support to
[barry.git] / src / record.h
blob4c28fdf0de7bb9ab00883d0fbba5ea1b9d92d053
1 ///
2 /// \file record.h
3 /// Blackberry database record classes. Help translate data
4 /// from data packets to useful structurs, and back.
5 ///
7 /*
8 Copyright (C) 2005-2006, Net Direct Inc. (http://www.netdirect.ca/)
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
19 See the GNU General Public License in the COPYING file at the
20 root directory of this project for more details.
23 #ifndef __BARRY_RECORD_H__
24 #define __BARRY_RECORD_H__
26 #include <iosfwd>
27 #include <string>
28 #include <vector>
29 #include <stdint.h>
31 // forward declarations
32 class Data;
34 namespace Barry {
37 // NOTE: All classes here must be container-safe! Perhaps add sorting
38 // operators in the future.
43 class CommandTable
45 public:
46 struct Command
48 unsigned int Code;
49 std::string Name;
52 typedef std::vector<Command> CommandArrayType;
54 CommandArrayType Commands;
56 private:
57 const unsigned char* ParseField(const unsigned char *begin,
58 const unsigned char *end);
59 public:
60 CommandTable();
61 ~CommandTable();
63 void Parse(const Data &data, size_t offset);
64 void Clear();
66 // returns 0 if unable to find command name, which is safe, since
67 // 0 is a special command that shouldn't be in the table anyway
68 unsigned int GetCommand(const std::string &name) const;
70 void Dump(std::ostream &os) const;
73 inline std::ostream& operator<< (std::ostream &os, const CommandTable &command) {
74 command.Dump(os);
75 return os;
80 class DatabaseDatabase
82 public:
83 struct Database
85 unsigned int Number;
86 unsigned int RecordCount;
87 std::string Name;
90 typedef std::vector<Database> DatabaseArrayType;
92 DatabaseArrayType Databases;
94 private:
95 template <class RecordType, class FieldType>
96 void ParseRec(const RecordType &rec, const unsigned char *end);
98 template <class FieldType>
99 const unsigned char* ParseField(const unsigned char *begin,
100 const unsigned char *end);
102 public:
103 DatabaseDatabase();
104 ~DatabaseDatabase();
106 void Parse(const Data &data);
107 void Clear();
109 // returns true on success, and fills target
110 bool GetDBNumber(const std::string &name, unsigned int &number) const;
111 bool GetDBName(unsigned int number, std::string &name) const;
113 void Dump(std::ostream &os) const;
116 inline std::ostream& operator<<(std::ostream &os, const DatabaseDatabase &dbdb) {
117 dbdb.Dump(os);
118 return os;
123 struct UnknownField
125 uint8_t type;
126 std::string data;
128 std::ostream& operator<< (std::ostream &os, const std::vector<UnknownField> &unknowns);
131 /// \addtogroup RecordParserClasses
132 /// Parser and data storage classes. These classes take a
133 /// Database Database record and convert them into C++ objects.
134 /// Each of these classes are safe to be used in standard
135 /// containers, and are meant to be used in conjunction with the
136 /// RecordParser<> template when calling Controller::LoadDatabase().
137 /// @{
139 class Contact
141 public:
142 struct GroupLink
144 uint32_t Link;
145 uint16_t Unknown;
147 GroupLink() : Link(0), Unknown(0) {}
148 GroupLink(uint32_t link, uint16_t unknown)
149 : Link(link), Unknown(unknown)
153 typedef std::vector<GroupLink> GroupLinksType;
154 typedef std::vector<UnknownField> UnknownsType;
156 // contact specific data
157 uint32_t RecordId;
158 std::string
159 Email,
160 Phone,
161 Fax,
162 WorkPhone,
163 HomePhone,
164 MobilePhone,
165 Pager,
166 PIN,
167 FirstName,
168 LastName,
169 Company,
170 DefaultCommunicationsMethod,
171 Address1,
172 Address2,
173 Address3,
174 City,
175 Province,
176 PostalCode,
177 Country,
178 Title,
179 PublicKey,
180 Notes;
182 GroupLinksType GroupLinks;
183 UnknownsType Unknowns;
186 //protected:
187 public:
188 const unsigned char* ParseField(const unsigned char *begin,
189 const unsigned char *end);
191 public:
192 Contact();
193 ~Contact();
195 uint64_t GetID() const { return RecordId; }
196 std::string GetPostalAddress() const;
198 void Parse(const Data &data, size_t offset, unsigned int operation);
199 void Build(Data &data, size_t offset) const;
200 void Clear(); // erase everything
202 void Dump(std::ostream &os) const;
203 void DumpLdif(std::ostream &os, const std::string &baseDN) const;
204 bool ReadLdif(std::istream &is); // returns true on success
206 // sorting - put group links at the end
207 bool operator<(const Contact &other) const {
208 return GroupLinks.size() == 0 && other.GroupLinks.size() > 0;
209 // // testing - put group links at the top
210 // return GroupLinks.size() > 0 && other.GroupLinks.size() == 0;
213 // protocol record sizes
214 static size_t GetOldProtocolRecordSize();
215 static size_t GetProtocolRecordSize();
217 // database name
218 static const char * GetDBName() { return "Address Book"; }
220 // helpers
221 static void SplitName(const std::string &full, std::string &first, std::string &last);
224 inline std::ostream& operator<< (std::ostream &os, const Contact &contact) {
225 contact.Dump(os);
226 return os;
229 class Message
231 public:
232 struct Address
234 std::string Name;
235 std::string Email;
239 Address From;
240 Address To;
241 Address Cc;
242 std::string Subject;
243 std::string Body;
244 std::vector<UnknownField> Unknowns;
246 public:
247 Message();
248 ~Message();
250 const unsigned char* ParseField(const unsigned char *begin,
251 const unsigned char *end);
252 void Parse(const Data &data, size_t offset, unsigned int operation);
253 void Clear();
255 void Dump(std::ostream &os) const;
257 // sorting
258 bool operator<(const Message &other) const { return Subject < other.Subject; }
260 // protocol record sizes
261 static size_t GetOldProtocolRecordSize();
262 static size_t GetProtocolRecordSize();
264 // database name
265 static const char * GetDBName() { return "Messages"; }
268 inline std::ostream& operator<<(std::ostream &os, const Message &msg) {
269 msg.Dump(os);
270 return os;
273 std::ostream& operator<<(std::ostream &os, const Message::Address &msga);
276 class Calendar
278 public:
279 typedef std::vector<UnknownField> UnknownsType;
281 uint64_t RecordId;
282 bool Recurring;
283 bool AllDayEvent;
284 std::string Subject;
285 std::string Notes;
286 std::string Location;
287 time_t NotificationTime;
288 time_t StartTime;
289 time_t EndTime;
290 UnknownsType Unknowns;
292 public:
293 Calendar();
294 ~Calendar();
296 const unsigned char* ParseField(const unsigned char *begin,
297 const unsigned char *end);
298 void Parse(const Data &data, size_t offset, unsigned int operation);
299 void Build(Data &data, size_t offset) const;
300 void Clear();
302 void Dump(std::ostream &os) const;
304 // sorting
305 bool operator<(const Calendar &other) const { return StartTime < other.StartTime; }
307 // protocol record sizes
308 static size_t GetOldProtocolRecordSize();
309 static size_t GetProtocolRecordSize();
311 // database name
312 static const char * GetDBName() { return "Calendar"; }
315 inline std::ostream& operator<<(std::ostream &os, const Calendar &msg) {
316 msg.Dump(os);
317 return os;
321 // This is a packed field, which is a group of fields packed in
322 // variable length records inside one larger field of a normal record.
323 class ServiceBookConfig
325 public:
326 typedef std::vector<UnknownField> UnknownsType;
328 uint8_t Format;
330 UnknownsType Unknowns;
332 public:
333 ServiceBookConfig();
334 ~ServiceBookConfig();
336 const unsigned char* ParseField(const unsigned char *begin,
337 const unsigned char *end);
338 void Parse(const Data &data, size_t offset, unsigned int operation);
339 void Build(Data &data, size_t offset) const;
340 void Clear();
342 void Dump(std::ostream &os) const;
345 inline std::ostream& operator<<(std::ostream &os, const ServiceBookConfig &msg) {
346 msg.Dump(os);
347 return os;
351 class ServiceBook
353 int NameType, DescType, UniqueIdType;
355 public:
356 typedef std::vector<UnknownField> UnknownsType;
358 uint64_t RecordId;
359 std::string Name;
360 std::string HiddenName;
361 std::string Description;
362 std::string DSID;
363 std::string BesDomain;
364 std::string UniqueId;
365 std::string ContentId;
366 ServiceBookConfig Config;
368 UnknownsType Unknowns;
370 public:
371 ServiceBook();
372 ~ServiceBook();
374 const unsigned char* ParseField(const unsigned char *begin,
375 const unsigned char *end);
376 void Parse(const Data &data, size_t offset, unsigned int operation);
377 void Build(Data &data, size_t offset) const;
378 void Clear();
380 void Dump(std::ostream &os) const;
382 // sorting
383 bool operator<(const ServiceBook &other) const { return RecordId < RecordId; }
385 // protocol record sizes
386 static size_t GetOldProtocolRecordSize();
387 static size_t GetProtocolRecordSize();
389 // database name
390 static const char * GetDBName() { return "Service Book"; }
393 inline std::ostream& operator<<(std::ostream &os, const ServiceBook &msg) {
394 msg.Dump(os);
395 return os;
398 /// @}
400 } // namespace Barry
402 #endif