Added missing string.h header to src/m_javaloader.cc
[barry.git] / src / r_contact.cc
blobe34fbd21a0ffd7d9a68137d457bdc0ca1252c4d4
1 ///
2 /// \file r_contact.cc
3 /// Blackberry database record parser class for contact records.
4 ///
6 /*
7 Copyright (C) 2005-2009, 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 #include "r_contact.h"
23 #include "record-internal.h"
24 #include "protocol.h"
25 #include "protostructs.h"
26 #include "data.h"
27 #include "time.h"
28 #include "error.h"
29 #include "endian.h"
30 #include "iconv.h"
31 #include <ostream>
32 #include <iomanip>
33 #include <time.h>
34 #include <stdexcept>
36 #define __DEBUG_MODE__
37 #include "debug.h"
39 using namespace std;
40 using namespace Barry::Protocol;
42 namespace Barry {
46 ///////////////////////////////////////////////////////////////////////////////
47 // Contact class
49 // Contact field codes
50 #define CFC_EMAIL 1
51 #define CFC_PHONE 2
52 #define CFC_FAX 3
53 #define CFC_WORK_PHONE 6
54 #define CFC_HOME_PHONE 7
55 #define CFC_MOBILE_PHONE 8
56 #define CFC_PAGER 9
57 #define CFC_PIN 10
58 #define CFC_RADIO 14 // 0x0e
59 #define CFC_WORK_PHONE_2 16 // 0x10
60 #define CFC_HOME_PHONE_2 17 // 0x11
61 #define CFC_OTHER_PHONE 18 // 0x12
62 #define CFC_NAME 32 // 0x20 used twice, in first/last name order
63 #define CFC_COMPANY 33
64 #define CFC_DEFAULT_COMM_METHOD 34
65 #define CFC_ADDRESS1 35
66 #define CFC_ADDRESS2 36
67 #define CFC_ADDRESS3 37
68 #define CFC_CITY 38
69 #define CFC_PROVINCE 39
70 #define CFC_POSTAL_CODE 40
71 #define CFC_COUNTRY 41
72 #define CFC_TITLE 42 // 0x2a
73 #define CFC_PUBLIC_KEY 43
74 #define CFC_GROUP_FLAG 44
75 #define CFC_GROUP_LINK 52
76 #define CFC_URL 54 // 0x36
77 #define CFC_PREFIX 55 // 0x37
78 #define CFC_CATEGORY 59 // 0x3B
79 #define CFC_HOME_ADDRESS1 61 // 0x3D
80 #define CFC_HOME_ADDRESS2 62 // 0x3E
81 // If the address 3 isn't mapped then it appears
82 // in the same field as address2 with a space
83 #define CFC_HOME_ADDRESS3 63 // 0x3F
84 #define CFC_NOTES 64 // 0x40
85 #define CFC_USER_DEFINED_1 65 // 0x41
86 #define CFC_USER_DEFINED_2 66 // 0x42
87 #define CFC_USER_DEFINED_3 67 // 0x43
88 #define CFC_USER_DEFINED_4 68 // 0x44
89 #define CFC_HOME_CITY 69 // 0x45
90 #define CFC_HOME_PROVINCE 70 // 0x46
91 #define CFC_HOME_POSTAL_CODE 71 // 0x47
92 #define CFC_HOME_COUNTRY 72 // 0x48
93 #define CFC_IMAGE 77 // 0x4D
94 #define CFC_BIRTHDAY 82 // 0x52
95 #define CFC_ANNIVERSARY 83 // 0x53
96 #define CFC_INVALID_FIELD 255
98 // Contact code to field table
99 static FieldLink<Contact> ContactFieldLinks[] = {
100 { CFC_PHONE, "Phone", 0,0, &Contact::Phone, 0, 0, 0, 0, true },
101 { CFC_FAX, "Fax", "facsimileTelephoneNumber",0, &Contact::Fax, 0, 0, 0, 0, true },
102 { CFC_WORK_PHONE, "WorkPhone", "telephoneNumber",0, &Contact::WorkPhone, 0, 0, 0, 0, true },
103 { CFC_HOME_PHONE, "HomePhone", "homePhone",0, &Contact::HomePhone, 0, 0, 0, 0, true },
104 { CFC_MOBILE_PHONE, "MobilePhone","mobile",0, &Contact::MobilePhone, 0, 0, 0, 0, true },
105 { CFC_PAGER, "Pager", "pager",0, &Contact::Pager, 0, 0, 0, 0, true },
106 { CFC_PIN, "PIN", 0,0, &Contact::PIN, 0, 0, 0, 0, true },
107 { CFC_RADIO, "Radio", 0,0, &Contact::Radio, 0, 0, 0, 0, true },
108 { CFC_WORK_PHONE_2, "WorkPhone2", 0,0, &Contact::WorkPhone2, 0, 0, 0, 0, true },
109 { CFC_HOME_PHONE_2, "HomePhone2", 0,0, &Contact::HomePhone2, 0, 0, 0, 0, true },
110 { CFC_OTHER_PHONE, "OtherPhone", 0,0, &Contact::OtherPhone, 0, 0, 0, 0, true },
111 { CFC_COMPANY, "Company", "o",0, &Contact::Company, 0, 0, 0, 0, true },
112 { CFC_DEFAULT_COMM_METHOD,"DefaultCommMethod",0,0, &Contact::DefaultCommunicationsMethod, 0, 0, 0, 0, true },
113 { CFC_ADDRESS1, "WorkAddress1", 0,0, 0, 0, 0, &Contact::WorkAddress, &PostalAddress::Address1, true },
114 { CFC_ADDRESS2, "WorkAddress2", 0,0, 0, 0, 0, &Contact::WorkAddress, &PostalAddress::Address2, true },
115 { CFC_ADDRESS3, "WorkAddress3", 0,0, 0, 0, 0, &Contact::WorkAddress, &PostalAddress::Address3, true },
116 { CFC_CITY, "WorkCity", "l",0, 0, 0, 0, &Contact::WorkAddress, &PostalAddress::City, true },
117 { CFC_PROVINCE, "WorkProvince", "st",0, 0, 0, 0, &Contact::WorkAddress, &PostalAddress::Province, true },
118 { CFC_POSTAL_CODE, "WorkPostalCode", "postalCode",0, 0, 0, 0, &Contact::WorkAddress, &PostalAddress::PostalCode, true },
119 { CFC_COUNTRY, "WorkCountry", "c", "country", 0, 0, 0, &Contact::WorkAddress, &PostalAddress::Country, true },
120 { CFC_TITLE, "JobTitle", "title",0, &Contact::JobTitle, 0, 0, 0, 0, true },
121 { CFC_PUBLIC_KEY, "PublicKey", 0,0, &Contact::PublicKey, 0, 0, 0, 0, false },
122 { CFC_URL, "URL", 0,0, &Contact::URL, 0, 0, 0, 0, true },
123 { CFC_PREFIX, "Prefix", 0,0, &Contact::Prefix, 0, 0, 0, 0, true },
124 { CFC_HOME_ADDRESS1,"HomeAddress1", 0,0, 0, 0, 0, &Contact::HomeAddress, &PostalAddress::Address1, true },
125 { CFC_HOME_ADDRESS2,"HomeAddress2", 0,0, 0, 0, 0, &Contact::HomeAddress, &PostalAddress::Address2, true },
126 { CFC_HOME_ADDRESS3,"HomeAddress3", 0,0, 0, 0, 0, &Contact::HomeAddress, &PostalAddress::Address3, true },
127 { CFC_NOTES, "Notes", 0,0, &Contact::Notes, 0, 0, 0, 0, true },
128 { CFC_USER_DEFINED_1, "UserDefined1", 0,0, &Contact::UserDefined1, 0, 0, 0, 0, true },
129 { CFC_USER_DEFINED_2, "UserDefined2", 0,0, &Contact::UserDefined2, 0, 0, 0, 0, true },
130 { CFC_USER_DEFINED_3, "UserDefined3", 0,0, &Contact::UserDefined3, 0, 0, 0, 0, true },
131 { CFC_USER_DEFINED_4, "UserDefined4", 0,0, &Contact::UserDefined4, 0, 0, 0, 0, true },
132 { CFC_HOME_CITY, "HomeCity", 0,0, 0, 0, 0, &Contact::HomeAddress, &PostalAddress::City, true },
133 { CFC_HOME_PROVINCE,"HomeProvince", 0,0, 0, 0, 0, &Contact::HomeAddress, &PostalAddress::Province, true },
134 { CFC_HOME_POSTAL_CODE, "HomePostalCode", 0,0, 0, 0, 0, &Contact::HomeAddress, &PostalAddress::PostalCode, true },
135 { CFC_HOME_COUNTRY, "HomeCountry",0,0, 0, 0, 0, &Contact::HomeAddress, &PostalAddress::Country, true },
136 { CFC_IMAGE, "Image", 0,0, &Contact::Image, 0, 0, 0, 0, false },
137 { CFC_INVALID_FIELD,"EndOfList", 0, 0, 0, 0, 0, 0, 0, false }
140 Contact::Contact()
141 : RecType(Contact::GetDefaultRecType()),
142 RecordId(0),
143 m_FirstNameSeen(false)
147 Contact::~Contact()
151 const unsigned char* Contact::ParseField(const unsigned char *begin,
152 const unsigned char *end,
153 const IConverter *ic)
155 const CommonField *field = (const CommonField *) begin;
157 // advance and check size
158 begin += COMMON_FIELD_HEADER_SIZE + btohs(field->size);
159 if( begin > end ) // if begin==end, we are ok
160 return begin;
162 if( !btohs(field->size) ) // if field has no size, something's up
163 return begin;
165 // cycle through the type table
166 for( FieldLink<Contact> *b = ContactFieldLinks;
167 b->type != CFC_INVALID_FIELD;
168 b++ )
170 if( b->type == field->type ) {
171 if( b->strMember ) {
172 std::string &s = this->*(b->strMember);
173 s = ParseFieldString(field);
174 if( b->iconvNeeded && ic )
175 s = ic->FromBB(s);
176 return begin; // done!
178 else if( b->postMember && b->postField ) {
179 std::string &s = (this->*(b->postMember)).*(b->postField);
180 s = ParseFieldString(field);
181 if( b->iconvNeeded && ic )
182 s = ic->FromBB(s);
183 return begin;
185 else {
186 break; // fall through to special handling
191 // if not found in the type table, check for special handling
192 switch( field->type )
194 case CFC_EMAIL: {
195 std::string s = ParseFieldString(field);
196 if( ic )
197 s = ic->FromBB(s);
198 EmailAddresses.push_back( s );
200 return begin;
202 case CFC_NAME: {
203 // can be used multiple times, for first/last names
204 std::string *name;
205 if( FirstName.size() || m_FirstNameSeen ) {
206 // first name already filled, use last name
207 name = &LastName;
208 m_FirstNameSeen = false;
210 else {
211 name = &FirstName;
212 m_FirstNameSeen = true;
215 *name = ParseFieldString(field);
216 if( ic )
217 *name = ic->FromBB(*name);
219 return begin;
221 case CFC_GROUP_LINK:
222 // just add the unique ID to the list
223 GroupLinks.push_back(
224 GroupLink(field->u.link.uniqueId,
225 field->u.link.unknown));
226 return begin;
228 case CFC_GROUP_FLAG:
229 // ignore the group flag... the presense of group link items
230 // behaves as the flag in this class
231 return begin;
233 case CFC_CATEGORY: {
234 std::string catstring = ParseFieldString(field);
235 if( ic )
236 catstring = ic->FromBB(catstring);
237 CategoryStr2List(catstring, Categories);
239 return begin;
241 case CFC_BIRTHDAY: {
242 std::string bstring = ParseFieldString(field);
243 Birthday.FromBBString(bstring);
245 return begin;
247 case CFC_ANNIVERSARY: {
248 std::string astring = ParseFieldString(field);
249 Anniversary.FromBBString(astring);
251 return begin;
254 // if still not handled, add to the Unknowns list
255 UnknownField uf;
256 uf.type = field->type;
257 uf.data.assign((const char*)field->u.raw, btohs(field->size));
258 Unknowns.push_back(uf);
260 // return new pointer for next field
261 return begin;
264 void Contact::ParseHeader(const Data &data, size_t &offset)
266 // no header to parse in Contact records
269 // this is called by the RecordParser<> class, which checks size for us
270 void Contact::ParseFields(const Data &data, size_t &offset, const IConverter *ic)
272 const unsigned char *finish = ParseCommonFields(*this,
273 data.GetData() + offset, data.GetData() + data.GetSize(), ic);
274 offset += finish - (data.GetData() + offset);
277 void Contact::BuildHeader(Data &data, size_t &offset) const
279 // no header in Contact records
283 // BuildFields
285 /// Build fields part of record
287 void Contact::BuildFields(Data &data, size_t &offset, const IConverter *ic) const
289 data.Zap();
291 // Sanity check: the Blackberry requires at least a name or
292 // a company name for each address record.
293 if( !GetFullName().size() && !Company.size() )
294 throw BadData("Contact must have name or company name.");
296 // check if this is a group link record, and if so, output
297 // the group flag
298 if( GroupLinks.size() )
299 BuildField(data, offset, CFC_GROUP_FLAG, 'G');
301 // special fields not in type table
302 if( FirstName.size() ) {
303 std::string s = ic ? ic->ToBB(FirstName) : FirstName;
304 BuildField(data, offset, CFC_NAME, s);
306 if( LastName.size() ) {
307 if( !FirstName.size() ) {
308 // order matters with first/last name, and if
309 // last name exists, and first name doesn't,
310 // insert blank first name ahead of it
311 BuildField(data, offset, CFC_NAME, "");
313 BuildField(data, offset, CFC_NAME, ic ? ic->ToBB(LastName) : LastName);
316 // add all email addresses
317 EmailList::const_iterator eai = EmailAddresses.begin();
318 for( ; eai != EmailAddresses.end(); ++eai ) {
319 if( eai->size() ) {
320 BuildField(data, offset, CFC_EMAIL, ic ? ic->ToBB(*eai) : *eai);
324 // cycle through the type table
325 for( FieldLink<Contact> *b = ContactFieldLinks;
326 b->type != CFC_INVALID_FIELD;
327 b++ )
329 // print only fields with data
330 if( b->strMember ) {
331 const std::string &field = this->*(b->strMember);
332 if( field.size() ) {
333 std::string s = (b->iconvNeeded && ic) ? ic->ToBB(field) : field;
334 BuildField(data, offset, b->type, s);
337 else if( b->postMember && b->postField ) {
338 const std::string &field = (this->*(b->postMember)).*(b->postField);
339 if( field.size() ) {
340 std::string s = (b->iconvNeeded && ic) ? ic->ToBB(field) : field;
341 BuildField(data, offset, b->type, s);
346 // save any group links
347 GroupLinksType::const_iterator
348 gb = GroupLinks.begin(), ge = GroupLinks.end();
349 for( ; gb != ge; gb++ ) {
350 Barry::Protocol::GroupLink link;
351 link.uniqueId = htobl(gb->Link);
352 link.unknown = htobs(gb->Unknown);
353 BuildField(data, offset, CFC_GROUP_LINK, link);
356 // save categories
357 if( Categories.size() ) {
358 string store;
359 CategoryList2Str(Categories, store);
360 BuildField(data, offset, CFC_CATEGORY, ic ? ic->ToBB(store) : store);
363 // save Birthday and Anniversary
364 if( Birthday.HasData() )
365 BuildField(data, offset, CFC_BIRTHDAY, Birthday.ToBBString());
366 if( Anniversary.HasData() )
367 BuildField(data, offset, CFC_ANNIVERSARY, Anniversary.ToBBString());
369 // and finally save unknowns
370 UnknownsType::const_iterator
371 ub = Unknowns.begin(), ue = Unknowns.end();
372 for( ; ub != ue; ub++ ) {
373 BuildField(data, offset, *ub);
376 data.ReleaseBuffer(offset);
379 void Contact::Clear()
381 RecType = Contact::GetDefaultRecType();
383 EmailAddresses.clear();
384 Phone.clear();
385 Fax.clear();
386 WorkPhone.clear();
387 HomePhone.clear();
388 MobilePhone.clear();
389 Pager.clear();
390 PIN.clear();
391 Radio.clear();
392 WorkPhone2.clear();
393 HomePhone2.clear();
394 OtherPhone.clear();
395 FirstName.clear();
396 LastName.clear();
397 Company.clear();
398 DefaultCommunicationsMethod.clear();
399 JobTitle.clear();
400 PublicKey.clear();
401 URL.clear();
402 Prefix.clear();
403 Notes.clear();
404 UserDefined1.clear();
405 UserDefined2.clear();
406 UserDefined3.clear();
407 UserDefined4.clear();
408 Image.clear();
410 Birthday.Clear();
411 Anniversary.Clear();
413 WorkAddress.Clear();
414 HomeAddress.Clear();
416 Categories.clear();
418 GroupLinks.clear();
419 Unknowns.clear();
421 m_FirstNameSeen = false;
425 // GetFullName
427 /// Helper function that returns a formatted full name
429 std::string Contact::GetFullName() const
431 std::string Full = FirstName;
432 if( Full.size() && LastName.size() )
433 Full += " ";
434 Full += LastName;
435 return Full;
439 // GetEmail
441 /// Helper function that always returns a valid string. The string
442 /// may be empty if there is no address at the specified index.
444 const std::string& Contact::GetEmail(unsigned int index) const
446 static const std::string blank;
447 if( index < EmailAddresses.size() )
448 return EmailAddresses[index];
449 return blank;
452 void Contact::Dump(std::ostream &os) const
454 ios::fmtflags oldflags = os.setf(ios::left);
455 char fill = os.fill(' ');
457 os << "Contact: 0x" << setbase(16) << GetID()
458 << " (" << (unsigned int)RecType << ")\n";
460 // special fields not in type table
461 os << " " << setw(20) << "FirstName";
462 os << ": " << FirstName << "\n";
463 os << " " << setw(20) << "LastName";
464 os << ": " << LastName << "\n";
466 // cycle through email addresses
467 EmailList::const_iterator eai = EmailAddresses.begin();
468 for( ; eai != EmailAddresses.end(); ++eai ) {
469 if( eai->size() ) {
470 os << " Email : " << *eai << "\n";
474 // cycle through the type table
475 for( FieldLink<Contact> *b = ContactFieldLinks;
476 b->type != CFC_INVALID_FIELD;
477 b++ )
479 const std::string *pField = 0;
480 if( b->strMember ) {
481 pField = &(this->*(b->strMember));
483 else if( b->postMember && b->postField ) {
484 pField = &((this->*(b->postMember)).*(b->postField));
487 // print only fields with data
488 if( pField && pField->size() ) {
489 os << " " << setw(20) << b->name;
490 os << ": " << *pField << "\n";
494 if( Categories.size() ) {
495 string display;
496 CategoryList2Str(Categories, display);
497 os << " Categories : " << display << "\n";
500 // print Birthday and Anniversary
501 if( Birthday.HasData() ) {
502 os << " Birthday : " << Birthday << "\n";
504 if( Anniversary.HasData() ) {
505 os << " Anniversary : " << Anniversary << "\n";
508 // print any group links
509 GroupLinksType::const_iterator
510 gb = GroupLinks.begin(), ge = GroupLinks.end();
511 if( gb != ge )
512 os << " GroupLinks:\n";
513 for( ; gb != ge; gb++ ) {
514 os << " ID: 0x" << setbase(16) << gb->Link << "\n";
517 // and finally print unknowns
518 os << Unknowns;
520 // cleanup the stream
521 os.flags(oldflags);
522 os.fill(fill);
525 void Contact::SplitName(const std::string &full, std::string &first, std::string &last)
527 first.clear();
528 last.clear();
530 string::size_type pos = full.find_last_of(' ');
531 if( pos != string::npos ) {
532 // has space, assume last word is last name
533 last = full.c_str() + pos + 1;
534 first = full.substr(0, pos);
536 else {
537 // no space, assume only first name
538 first = full.substr(0);
542 void Contact::CategoryStr2List(const std::string &str,
543 Barry::CategoryList &list)
545 // start fresh
546 list.clear();
548 if( !str.size() )
549 return;
551 // parse the comma-delimited string to a list, stripping away
552 // any white space around each category name
553 string::size_type start = 0, end = 0, delim = str.find(',', start);
554 while( start != string::npos ) {
555 if( delim == string::npos )
556 end = str.size() - 1;
557 else
558 end = delim - 1;
560 // strip surrounding whitespace
561 while( str[start] == ' ' )
562 start++;
563 while( end && str[end] == ' ' )
564 end--;
566 if( start <= end ) {
567 string token = str.substr(start, end-start+1);
568 list.push_back(token);
571 // next
572 start = delim;
573 if( start != string::npos )
574 start++;
575 delim = str.find(',', start);
579 void Contact::CategoryList2Str(const Barry::CategoryList &list,
580 std::string &str)
582 str.clear();
584 Barry::CategoryList::const_iterator i = list.begin();
585 for( ; i != list.end(); ++i ) {
586 if( str.size() )
587 str += ", ";
588 str += *i;
592 } // namespace Barry