maint: added make-yum.sh for creating yum repositories under build/
[barry/progweb.git] / src / r_contact.cc
blob6fbd6b3f8c52c361b7fed1191833c5ed9537c639
1 ///
2 /// \file r_contact.cc
3 /// Blackberry database record parser class for contact records.
4 ///
6 /*
7 Copyright (C) 2005-2011, 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 "trim.h"
32 #include <iostream>
33 #include <iomanip>
34 #include <sstream>
35 #include <time.h>
36 #include <stdexcept>
37 #include "ios_state.h"
39 #define __DEBUG_MODE__
40 #include "debug.h"
42 using namespace std;
43 using namespace Barry::Protocol;
45 namespace Barry {
49 ///////////////////////////////////////////////////////////////////////////////
50 // Contact class
52 // Contact field codes
53 #define CFC_EMAIL 1
54 #define CFC_PHONE 2
55 #define CFC_FAX 3
56 #define CFC_WORK_PHONE 6
57 #define CFC_HOME_PHONE 7
58 #define CFC_MOBILE_PHONE 8
59 #define CFC_PAGER 9
60 #define CFC_PIN 10
61 #define CFC_RADIO 14 // 0x0e
62 #define CFC_WORK_PHONE_2 16 // 0x10
63 #define CFC_HOME_PHONE_2 17 // 0x11
64 #define CFC_OTHER_PHONE 18 // 0x12
65 #define CFC_MOBILE_PHONE_2 19 // 0x13
66 #define CFC_HOME_FAX 20 // 0x14
67 #define CFC_NAME 32 // 0x20 used twice, in first/last name order
68 #define CFC_COMPANY 33
69 #define CFC_DEFAULT_COMM_METHOD 34
70 #define CFC_ADDRESS1 35
71 #define CFC_ADDRESS2 36
72 #define CFC_ADDRESS3 37
73 #define CFC_CITY 38
74 #define CFC_PROVINCE 39
75 #define CFC_POSTAL_CODE 40
76 #define CFC_COUNTRY 41
77 #define CFC_TITLE 42 // 0x2a
78 #define CFC_PUBLIC_KEY 43
79 #define CFC_GROUP_FLAG 44
80 #define CFC_GROUP_LINK 52
81 #define CFC_URL 54 // 0x36
82 #define CFC_PREFIX 55 // 0x37
83 #define CFC_CATEGORY 59 // 0x3B
84 #define CFC_HOME_ADDRESS1 61 // 0x3D
85 #define CFC_HOME_ADDRESS2 62 // 0x3E
86 // If the address 3 isn't mapped then it appears
87 // in the same field as address2 with a space
88 #define CFC_HOME_ADDRESS3 63 // 0x3F
89 #define CFC_NOTES 64 // 0x40
90 #define CFC_USER_DEFINED_1 65 // 0x41
91 #define CFC_USER_DEFINED_2 66 // 0x42
92 #define CFC_USER_DEFINED_3 67 // 0x43
93 #define CFC_USER_DEFINED_4 68 // 0x44
94 #define CFC_HOME_CITY 69 // 0x45
95 #define CFC_HOME_PROVINCE 70 // 0x46
96 #define CFC_HOME_POSTAL_CODE 71 // 0x47
97 #define CFC_HOME_COUNTRY 72 // 0x48
98 #define CFC_IMAGE 77 // 0x4D
99 #define CFC_BIRTHDAY 82 // 0x52
100 #define CFC_ANNIVERSARY 83 // 0x53
101 #define CFC_MAYBE_CATEGORYID 84 // 0x54
102 #define CFC_UNIQUEID 85 // 0x55
103 #define CFC_NICKNAME 86 // 0x56
104 #define CFC_INVALID_FIELD 255
106 // Contact code to field table
107 static FieldLink<Contact> ContactFieldLinks[] = {
108 { CFC_NICKNAME, "Nickname", 0,0, &Contact::Nickname, 0, 0, 0, 0, true },
109 { CFC_PHONE, "Phone", 0,0, &Contact::Phone, 0, 0, 0, 0, true },
110 { CFC_FAX, "Fax", "facsimileTelephoneNumber",0, &Contact::Fax, 0, 0, 0, 0, true },
111 { CFC_HOME_FAX, "HomeFax", 0,0, &Contact::HomeFax, 0, 0, 0, 0, true },
112 { CFC_WORK_PHONE, "WorkPhone", "telephoneNumber",0, &Contact::WorkPhone, 0, 0, 0, 0, true },
113 { CFC_HOME_PHONE, "HomePhone", "homePhone",0, &Contact::HomePhone, 0, 0, 0, 0, true },
114 { CFC_MOBILE_PHONE, "MobilePhone","mobile",0, &Contact::MobilePhone, 0, 0, 0, 0, true },
115 { CFC_MOBILE_PHONE_2,"MobilePhone2",0,0, &Contact::MobilePhone2, 0, 0, 0, 0, true },
116 { CFC_PAGER, "Pager", "pager",0, &Contact::Pager, 0, 0, 0, 0, true },
117 { CFC_PIN, "PIN", 0,0, &Contact::PIN, 0, 0, 0, 0, true },
118 { CFC_RADIO, "Radio", 0,0, &Contact::Radio, 0, 0, 0, 0, true },
119 { CFC_WORK_PHONE_2, "WorkPhone2", 0,0, &Contact::WorkPhone2, 0, 0, 0, 0, true },
120 { CFC_HOME_PHONE_2, "HomePhone2", 0,0, &Contact::HomePhone2, 0, 0, 0, 0, true },
121 { CFC_OTHER_PHONE, "OtherPhone", 0,0, &Contact::OtherPhone, 0, 0, 0, 0, true },
122 { CFC_COMPANY, "Company", "o",0, &Contact::Company, 0, 0, 0, 0, true },
123 { CFC_DEFAULT_COMM_METHOD,"DefaultCommMethod",0,0, &Contact::DefaultCommunicationsMethod, 0, 0, 0, 0, true },
124 { CFC_ADDRESS1, "WorkAddress1", 0,0, 0, 0, 0, &Contact::WorkAddress, &PostalAddress::Address1, true },
125 { CFC_ADDRESS2, "WorkAddress2", 0,0, 0, 0, 0, &Contact::WorkAddress, &PostalAddress::Address2, true },
126 { CFC_ADDRESS3, "WorkAddress3", 0,0, 0, 0, 0, &Contact::WorkAddress, &PostalAddress::Address3, true },
127 { CFC_CITY, "WorkCity", "l",0, 0, 0, 0, &Contact::WorkAddress, &PostalAddress::City, true },
128 { CFC_PROVINCE, "WorkProvince", "st",0, 0, 0, 0, &Contact::WorkAddress, &PostalAddress::Province, true },
129 { CFC_POSTAL_CODE, "WorkPostalCode", "postalCode",0, 0, 0, 0, &Contact::WorkAddress, &PostalAddress::PostalCode, true },
130 { CFC_COUNTRY, "WorkCountry", "c", "country", 0, 0, 0, &Contact::WorkAddress, &PostalAddress::Country, true },
131 { CFC_TITLE, "JobTitle", "title",0, &Contact::JobTitle, 0, 0, 0, 0, true },
132 { CFC_PUBLIC_KEY, "PublicKey", 0,0, &Contact::PublicKey, 0, 0, 0, 0, false },
133 { CFC_URL, "URL", 0,0, &Contact::URL, 0, 0, 0, 0, true },
134 { CFC_PREFIX, "Prefix", 0,0, &Contact::Prefix, 0, 0, 0, 0, true },
135 { CFC_HOME_ADDRESS1,"HomeAddress1", 0,0, 0, 0, 0, &Contact::HomeAddress, &PostalAddress::Address1, true },
136 { CFC_HOME_ADDRESS2,"HomeAddress2", 0,0, 0, 0, 0, &Contact::HomeAddress, &PostalAddress::Address2, true },
137 { CFC_HOME_ADDRESS3,"HomeAddress3", 0,0, 0, 0, 0, &Contact::HomeAddress, &PostalAddress::Address3, true },
138 { CFC_NOTES, "Notes", 0,0, &Contact::Notes, 0, 0, 0, 0, true },
139 { CFC_USER_DEFINED_1, "UserDefined1", 0,0, &Contact::UserDefined1, 0, 0, 0, 0, true },
140 { CFC_USER_DEFINED_2, "UserDefined2", 0,0, &Contact::UserDefined2, 0, 0, 0, 0, true },
141 { CFC_USER_DEFINED_3, "UserDefined3", 0,0, &Contact::UserDefined3, 0, 0, 0, 0, true },
142 { CFC_USER_DEFINED_4, "UserDefined4", 0,0, &Contact::UserDefined4, 0, 0, 0, 0, true },
143 { CFC_HOME_CITY, "HomeCity", 0,0, 0, 0, 0, &Contact::HomeAddress, &PostalAddress::City, true },
144 { CFC_HOME_PROVINCE,"HomeProvince", 0,0, 0, 0, 0, &Contact::HomeAddress, &PostalAddress::Province, true },
145 { CFC_HOME_POSTAL_CODE, "HomePostalCode", 0,0, 0, 0, 0, &Contact::HomeAddress, &PostalAddress::PostalCode, true },
146 { CFC_HOME_COUNTRY, "HomeCountry",0,0, 0, 0, 0, &Contact::HomeAddress, &PostalAddress::Country, true },
147 { CFC_IMAGE, "Image", 0,0, &Contact::Image, 0, 0, 0, 0, false },
148 { CFC_INVALID_FIELD,"EndOfList", 0, 0, 0, 0, 0, 0, 0, false }
151 Contact::Contact()
152 : RecType(Contact::GetDefaultRecType()),
153 RecordId(0),
154 m_FirstNameSeen(false)
158 Contact::~Contact()
162 const unsigned char* Contact::ParseField(const unsigned char *begin,
163 const unsigned char *end,
164 const IConverter *ic)
166 const CommonField *field = (const CommonField *) begin;
168 // advance and check size
169 begin += COMMON_FIELD_HEADER_SIZE + btohs(field->size);
170 if( begin > end ) // if begin==end, we are ok
171 return begin;
173 if( !btohs(field->size) ) // if field has no size, something's up
174 return begin;
176 // cycle through the type table
177 for( FieldLink<Contact> *b = ContactFieldLinks;
178 b->type != CFC_INVALID_FIELD;
179 b++ )
181 if( b->type == field->type ) {
182 if( b->strMember ) {
183 std::string &s = this->*(b->strMember);
184 s = ParseFieldString(field);
185 if( b->iconvNeeded && ic )
186 s = ic->FromBB(s);
187 return begin; // done!
189 else if( b->postMember && b->postField ) {
190 std::string &s = (this->*(b->postMember)).*(b->postField);
191 s = ParseFieldString(field);
192 if( b->iconvNeeded && ic )
193 s = ic->FromBB(s);
194 return begin;
196 else {
197 break; // fall through to special handling
202 // if not found in the type table, check for special handling
203 switch( field->type )
205 case CFC_EMAIL: {
206 std::string s = ParseFieldString(field);
207 if( ic )
208 s = ic->FromBB(s);
209 EmailAddresses.push_back( s );
211 return begin;
213 case CFC_NAME: {
214 // can be used multiple times, for first/last names
215 std::string *name;
216 if( FirstName.size() || m_FirstNameSeen ) {
217 // first name already filled, use last name
218 name = &LastName;
219 m_FirstNameSeen = false;
221 else {
222 name = &FirstName;
223 m_FirstNameSeen = true;
226 *name = ParseFieldString(field);
227 if( ic )
228 *name = ic->FromBB(*name);
230 return begin;
232 case CFC_GROUP_LINK:
233 // just add the unique ID to the list
234 GroupLinks.push_back(
235 GroupLink(field->u.link.uniqueId,
236 field->u.link.unknown));
237 return begin;
239 case CFC_GROUP_FLAG:
240 // ignore the group flag... the presense of group link items
241 // behaves as the flag in this class
242 return begin;
244 case CFC_CATEGORY: {
245 std::string catstring = ParseFieldString(field);
246 if( ic )
247 catstring = ic->FromBB(catstring);
248 Categories.CategoryStr2List(catstring);
250 return begin;
252 case CFC_BIRTHDAY: {
253 std::string bstring = ParseFieldString(field);
254 Birthday.FromBBString(bstring);
256 return begin;
258 case CFC_ANNIVERSARY: {
259 std::string astring = ParseFieldString(field);
260 Anniversary.FromBBString(astring);
262 return begin;
264 case CFC_UNIQUEID:
265 // this is a duplicate of the UniqueID that comes from
266 // the envelope part of the protocol... just throw this
267 // away, since when we upload it, we need to use a
268 // consisten UniqueID / RecordID from the API
269 return begin;
272 // if still not handled, add to the Unknowns list
273 UnknownField uf;
274 uf.type = field->type;
275 uf.data.assign((const char*)field->u.raw, btohs(field->size));
276 Unknowns.push_back(uf);
278 // return new pointer for next field
279 return begin;
282 void Contact::ParseHeader(const Data &data, size_t &offset)
284 // no header to parse in Contact records
287 void Contact::ParseFields(const Data &data, size_t &offset, const IConverter *ic)
289 const unsigned char *finish = ParseCommonFields(*this,
290 data.GetData() + offset, data.GetData() + data.GetSize(), ic);
291 offset += finish - (data.GetData() + offset);
294 void Contact::BuildHeader(Data &data, size_t &offset) const
296 // no header in Contact records
300 // BuildFields
302 /// Build fields part of record
304 void Contact::BuildFields(Data &data, size_t &offset, const IConverter *ic) const
306 data.Zap();
308 // Sanity check: the Blackberry requires at least a name or
309 // a company name for each address record.
310 if( !GetFullName().size() && !Company.size() )
311 throw BadData("Contact must have name or company name.");
313 // check if this is a group link record, and if so, output
314 // the group flag
315 if( GroupLinks.size() )
316 BuildField(data, offset, CFC_GROUP_FLAG, 'G');
318 // special fields not in type table
319 if( FirstName.size() ) {
320 std::string s = ic ? ic->ToBB(FirstName) : FirstName;
321 BuildField(data, offset, CFC_NAME, s);
323 if( LastName.size() ) {
324 if( !FirstName.size() ) {
325 // order matters with first/last name, and if
326 // last name exists, and first name doesn't,
327 // insert blank first name ahead of it
328 BuildField(data, offset, CFC_NAME, "");
330 BuildField(data, offset, CFC_NAME, ic ? ic->ToBB(LastName) : LastName);
333 // FIXME
334 // // add unknown data
335 // char buffer[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
336 // BuildField(data, offset, 0x54, buffer, 8);
338 // With the BlackBerry Storm, I have to add this entry.
339 // Otherwise the uniqueId of this contact is reseted !
340 // The device seems accept the multiple contact with the same uniqueId,
341 // but the synchronization process uses this uniqueId to identify the contact.
342 // add uniqueId
343 BuildField(data, offset, CFC_UNIQUEID, RecordId);
345 // add all email addresses
346 EmailList::const_iterator eai = EmailAddresses.begin();
347 for( ; eai != EmailAddresses.end(); ++eai ) {
348 if( eai->size() ) {
349 BuildField(data, offset, CFC_EMAIL, ic ? ic->ToBB(*eai) : *eai);
353 // cycle through the type table
354 for( FieldLink<Contact> *b = ContactFieldLinks;
355 b->type != CFC_INVALID_FIELD;
356 b++ )
358 // print only fields with data
359 if( b->strMember ) {
360 const std::string &field = this->*(b->strMember);
361 if( field.size() ) {
362 std::string s = (b->iconvNeeded && ic) ? ic->ToBB(field) : field;
363 BuildField(data, offset, b->type, s);
366 else if( b->postMember && b->postField ) {
367 const std::string &field = (this->*(b->postMember)).*(b->postField);
368 if( field.size() ) {
369 std::string s = (b->iconvNeeded && ic) ? ic->ToBB(field) : field;
370 BuildField(data, offset, b->type, s);
375 // save any group links
376 GroupLinksType::const_iterator
377 gb = GroupLinks.begin(), ge = GroupLinks.end();
378 for( ; gb != ge; gb++ ) {
379 Barry::Protocol::GroupLink link;
380 link.uniqueId = htobl(gb->Link);
381 link.unknown = htobs(gb->Unknown);
382 BuildField(data, offset, CFC_GROUP_LINK, link);
385 // save categories
386 if( Categories.size() ) {
387 string store;
388 Categories.CategoryList2Str(store);
389 BuildField(data, offset, CFC_CATEGORY, ic ? ic->ToBB(store) : store);
392 // save Birthday and Anniversary
393 if( Birthday.HasData() )
394 BuildField(data, offset, CFC_BIRTHDAY, Birthday.ToBBString());
395 if( Anniversary.HasData() )
396 BuildField(data, offset, CFC_ANNIVERSARY, Anniversary.ToBBString());
398 // and finally save unknowns
399 UnknownsType::const_iterator
400 ub = Unknowns.begin(), ue = Unknowns.end();
401 for( ; ub != ue; ub++ ) {
402 BuildField(data, offset, *ub);
405 data.ReleaseBuffer(offset);
408 void Contact::Clear()
410 RecType = GetDefaultRecType();
411 RecordId = 0;
413 EmailAddresses.clear();
414 Phone.clear();
416 Fax.clear();
417 HomeFax.clear();
418 WorkPhone.clear();
419 HomePhone.clear();
420 MobilePhone.clear();
421 MobilePhone2.clear();
422 Pager.clear();
423 PIN.clear();
424 Radio.clear();
425 WorkPhone2.clear();
426 HomePhone2.clear();
427 OtherPhone.clear();
428 FirstName.clear();
429 LastName.clear();
430 Company.clear();
431 DefaultCommunicationsMethod.clear();
432 JobTitle.clear();
433 PublicKey.clear();
434 URL.clear();
435 Prefix.clear();
436 Notes.clear();
437 UserDefined1.clear();
438 UserDefined2.clear();
439 UserDefined3.clear();
440 UserDefined4.clear();
441 Image.clear();
442 Nickname.clear();
444 Birthday.Clear();
445 Anniversary.Clear();
447 WorkAddress.Clear();
448 HomeAddress.Clear();
450 Categories.clear();
452 GroupLinks.clear();
453 Unknowns.clear();
455 m_FirstNameSeen = false;
458 std::string Contact::GetDescription() const
460 return GetFullName();
464 // GetFullName
466 /// Helper function that returns a formatted full name
468 std::string Contact::GetFullName() const
470 std::string Full = FirstName;
471 if( Full.size() && LastName.size() )
472 Full += " ";
473 Full += LastName;
474 return Full;
478 // GetEmail
480 /// Helper function that always returns a valid string. The string
481 /// may be empty if there is no address at the specified index.
483 const std::string& Contact::GetEmail(unsigned int index) const
485 static const std::string blank;
486 if( index < EmailAddresses.size() )
487 return EmailAddresses[index];
488 return blank;
491 void Contact::Dump(std::ostream &os) const
493 ios_format_state state(os);
495 os.setf(ios::left);
496 os.fill(' ');
498 os << "Contact: 0x" << setbase(16) << GetID()
499 << " (" << (unsigned int)RecType << ")\n";
501 // special fields not in type table
502 os << " " << setw(20) << "FirstName";
503 os << ": " << FirstName << "\n";
504 os << " " << setw(20) << "LastName";
505 os << ": " << LastName << "\n";
507 // cycle through email addresses
508 EmailList::const_iterator eai = EmailAddresses.begin();
509 for( ; eai != EmailAddresses.end(); ++eai ) {
510 if( eai->size() ) {
511 os << " Email : " << *eai << "\n";
515 // cycle through the type table
516 for( FieldLink<Contact> *b = ContactFieldLinks;
517 b->type != CFC_INVALID_FIELD;
518 b++ )
520 // special case: don't dump the raw image data, but
521 // leave that for a special hex dump
522 if( b->type == CFC_IMAGE )
523 continue;
525 const std::string *pField = 0;
526 if( b->strMember ) {
527 pField = &(this->*(b->strMember));
529 else if( b->postMember && b->postField ) {
530 pField = &((this->*(b->postMember)).*(b->postField));
533 // print only fields with data
534 if( pField && pField->size() ) {
535 os << " " << setw(20) << b->name;
536 os << ": " << Cr2LfWrapper(*pField) << "\n";
540 if( Categories.size() ) {
541 string display;
542 Categories.CategoryList2Str(display);
543 os << " Categories : " << display << "\n";
546 // print Birthday and Anniversary
547 if( Birthday.HasData() ) {
548 os << " Birthday : " << Birthday << "\n";
550 if( Anniversary.HasData() ) {
551 os << " Anniversary : " << Anniversary << "\n";
554 // print any group links
555 GroupLinksType::const_iterator
556 gb = GroupLinks.begin(), ge = GroupLinks.end();
557 if( gb != ge )
558 os << " GroupLinks:\n";
559 for( ; gb != ge; gb++ ) {
560 os << " ID: 0x" << setbase(16) << gb->Link << "\n";
563 // print Image in hex dump format, if available
564 if( Image.size() ) {
565 Data image(Image.data(), Image.size());
566 os << " Photo image:\n";
567 os << image << "\n";
570 // and finally print unknowns
571 os << Unknowns;
574 bool Contact::operator<(const Contact &other) const
576 // old sorting mechanism, to put group links at the bottom
577 //return GroupLinks.size() == 0 && other.GroupLinks.size() > 0;
578 // testing - put group links at the top
579 //return GroupLinks.size() > 0 && other.GroupLinks.size() == 0;
581 // usually one of these fields is filled in, so compare
582 // them all in a ( LastName + FirstName + Company ) key style
583 int cmp = LastName.compare(other.LastName);
584 if( cmp == 0 )
585 cmp = FirstName.compare(other.FirstName);
586 if( cmp == 0 )
587 cmp = Company.compare(other.Company);
588 return cmp < 0;
591 void Contact::SplitName(const std::string &full, std::string &first, std::string &last)
593 first.clear();
594 last.clear();
596 string::size_type pos = full.find_last_of(' ');
597 if( pos != string::npos ) {
598 // has space, assume last word is last name
599 last = full.c_str() + pos + 1;
600 first = full.substr(0, pos);
602 else {
603 // no space, assume only first name
604 first = full.substr(0);
608 std::string Contact::Email2CommaString(const EmailList &list)
610 ostringstream oss;
611 for( EmailList::const_iterator i = list.begin(); i!=list.end(); ++i ) {
612 if( i != list.begin() )
613 oss << ", ";
614 oss << *i;
616 return oss.str();
619 /// Replaces the EmailAddresses list with the parsed results of
620 /// list. If list is empty, then EmailAddresses will also be empty.
621 /// Note that incoming addresses need to be in simple format, not
622 /// complex formats like "Name <user@example.com>" but just
623 /// "user@example.com". This is a device limitation.
625 /// Any complex email addresses found in the list will be dropped,
626 /// with a message sent to the debug output stream.
627 void Contact::CommaString2Email(const std::string &list, EmailList &result)
629 // start fresh
630 result.clear();
632 // parse the comma separated list
633 istringstream iss(list);
634 string address;
636 while( iss >> ws && getline(iss, address, ',') ) {
637 // trim any trailing whitespace in the address
638 Inplace::rtrim(address);
640 // is this a complex address? like:
641 // Chris Frey <cdfrey@foursquare.net>
642 // The device only accepts the plain
643 // "cdfrey@foursquare.net" part here
644 if( address.rfind('>') != string::npos ) {
645 dout("Error: Cannot convert complex name+address to a simple contact email address, skipping: " << address);
646 continue;
649 // add to list if anything left
650 if( address.size() ) {
651 result.push_back(address);
656 } // namespace Barry