3 /// Misc. Blackberry database record helper classes and functions.
4 /// Helps translate data from data packets to useful structures,
9 Copyright (C) 2005-2010, Net Direct Inc. (http://www.netdirect.ca/)
11 This program is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 2 of the License, or
14 (at your option) any later version.
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
20 See the GNU General Public License in the COPYING file at the
21 root directory of this project for more details.
25 #include "record-internal.h"
27 #include "protostructs.h"
36 #include <stdio.h> // for sscanf()
39 #define __DEBUG_MODE__
43 using namespace Barry::Protocol
;
47 //////////////////////////////////////////////////////////////////////////////
48 // Field builder helper functions
50 void BuildField1900(Data
&data
, size_t &size
, uint8_t type
, time_t t
)
52 size_t timesize
= COMMON_FIELD_MIN1900_SIZE
;
53 size_t fieldsize
= COMMON_FIELD_HEADER_SIZE
+ timesize
;
54 unsigned char *pd
= data
.GetBuffer(size
+ fieldsize
) + size
;
55 CommonField
*field
= (CommonField
*) pd
;
57 field
->size
= htobs(timesize
);
59 field
->u
.min1900
= time2min(t
);
64 void BuildField(Data
&data
, size_t &size
, uint8_t type
, char c
)
66 BuildField(data
, size
, type
, (uint8_t)c
);
69 void BuildField(Data
&data
, size_t &size
, uint8_t type
, uint8_t c
)
72 size_t fieldsize
= COMMON_FIELD_HEADER_SIZE
+ strsize
;
73 unsigned char *pd
= data
.GetBuffer(size
+ fieldsize
) + size
;
74 CommonField
*field
= (CommonField
*) pd
;
76 field
->size
= htobs(strsize
);
78 memcpy(field
->u
.raw
, &c
, strsize
);
83 void BuildField(Data
&data
, size_t &size
, uint8_t type
, uint16_t value
)
86 size_t fieldsize
= COMMON_FIELD_HEADER_SIZE
+ strsize
;
87 unsigned char *pd
= data
.GetBuffer(size
+ fieldsize
) + size
;
88 CommonField
*field
= (CommonField
*) pd
;
90 field
->size
= htobs(strsize
);
93 uint16_t store
= htobs(value
);
94 memcpy(field
->u
.raw
, &store
, strsize
);
99 void BuildField(Data
&data
, size_t &size
, uint8_t type
, uint32_t value
)
102 size_t fieldsize
= COMMON_FIELD_HEADER_SIZE
+ strsize
;
103 unsigned char *pd
= data
.GetBuffer(size
+ fieldsize
) + size
;
104 CommonField
*field
= (CommonField
*) pd
;
106 field
->size
= htobl(strsize
);
109 uint32_t store
= htobl(value
);
110 memcpy(field
->u
.raw
, &store
, strsize
);
115 void BuildField(Data
&data
, size_t &size
, uint8_t type
, uint64_t value
)
118 size_t fieldsize
= COMMON_FIELD_HEADER_SIZE
+ strsize
;
119 unsigned char *pd
= data
.GetBuffer(size
+ fieldsize
) + size
;
120 CommonField
*field
= (CommonField
*) pd
;
122 field
->size
= htobl(strsize
);
125 uint64_t store
= htobll(value
);
126 memcpy(field
->u
.raw
, &store
, strsize
);
131 void BuildField(Data
&data
, size_t &size
, uint8_t type
, const std::string
&str
)
133 // include null terminator
134 BuildField(data
, size
, type
, str
.c_str(), str
.size() + 1);
137 void BuildField(Data
&data
, size_t &size
, uint8_t type
,
138 const void *buf
, size_t bufsize
)
140 // include null terminator
141 size_t fieldsize
= COMMON_FIELD_HEADER_SIZE
+ bufsize
;
142 unsigned char *pd
= data
.GetBuffer(size
+ fieldsize
) + size
;
143 CommonField
*field
= (CommonField
*) pd
;
145 field
->size
= htobs(bufsize
);
147 memcpy(field
->u
.raw
, buf
, bufsize
);
152 void BuildField(Data
&data
, size_t &size
, const Barry::UnknownField
&field
)
154 BuildField(data
, size
, field
.type
,
155 field
.data
.raw_data
.data(), field
.data
.raw_data
.size());
158 void BuildField(Data
&data
, size_t &size
, uint8_t type
, const Barry::Protocol::GroupLink
&link
)
160 size_t linksize
= sizeof(Barry::Protocol::GroupLink
);
161 size_t fieldsize
= COMMON_FIELD_HEADER_SIZE
+ linksize
;
162 unsigned char *pd
= data
.GetBuffer(size
+ fieldsize
) + size
;
163 CommonField
*field
= (CommonField
*) pd
;
165 field
->size
= htobs(linksize
);
167 field
->u
.link
= link
;
172 std::string
ParseFieldString(const Barry::Protocol::CommonField
*field
)
174 // make no assumptions here, and pass the full size in as
175 // the maxlen, even though 99% of the time, it will be a null...
176 // this function can be used by non-null terminated strings as well
177 return ParseFieldString(field
->u
.raw
, btohs(field
->size
));
180 std::string
ParseFieldString(const void *data
, uint16_t maxlen
)
182 const char *str
= (const char *)data
;
184 // find last non-null character, since some fields
185 // can have multiple null terminators
186 while( maxlen
&& str
[maxlen
-1] == 0 )
189 return std::string(str
, maxlen
);
193 ///////////////////////////////////////////////////////////////////////////////
196 std::ostream
& operator<< (std::ostream
&os
, const std::vector
<UnknownField
> &unknowns
)
198 std::vector
<UnknownField
>::const_iterator
199 ub
= unknowns
.begin(), ue
= unknowns
.end();
201 os
<< " Unknowns:\n";
202 for( ; ub
!= ue
; ub
++ ) {
203 os
<< " Type: 0x" << setbase(16)
204 << (unsigned int) ub
->type
205 << " Data:\n" << Data(ub
->data
.data(), ub
->data
.size());
211 ///////////////////////////////////////////////////////////////////////////////
212 // EmailAddress class
214 std::ostream
& operator<<(std::ostream
&os
, const EmailAddress
&msga
) {
215 os
<< msga
.Name
<< " <" << msga
.Email
<< ">";
219 std::ostream
& operator<<(std::ostream
&os
, const EmailAddressList
&elist
) {
220 for( EmailAddressList::const_iterator i
= elist
.begin(); i
!= elist
.end(); ++i
) {
221 if( i
!= elist
.begin() )
229 ///////////////////////////////////////////////////////////////////////////////
230 // PostalAddress class
235 /// Format a mailing address into a single string, handling missing fields.
237 std::string
PostalAddress::GetLabel() const
239 std::string address
= Address1
;
240 if( Address2
.size() ) {
245 if( Address3
.size() ) {
253 address
+= City
+ " ";
254 if( Province
.size() )
255 address
+= Province
+ " ";
260 if( PostalCode
.size() )
261 address
+= PostalCode
;
266 void PostalAddress::Clear()
277 std::ostream
& operator<<(std::ostream
&os
, const PostalAddress
&post
) {
278 os
<< post
.GetLabel();
284 ///////////////////////////////////////////////////////////////////////////////
287 Date::Date(const struct tm
*timep
)
294 Month
= Day
= Year
= 0;
297 void Date::ToTm(struct tm
*timep
) const
299 memset(timep
, 0, sizeof(tm
));
300 timep
->tm_year
= Year
- 1900;
301 timep
->tm_mon
= Month
;
302 timep
->tm_mday
= Day
;
305 std::string
Date::ToYYYYMMDD() const
307 std::ostringstream oss
;
308 // setfill and setw not sticky.
309 oss
<< setw(4) << setfill('0') << Year
310 << setw(2) << setfill('0') << Month
+ 1
311 << setw(2) << setfill('0') << Day
;
318 /// The Blackberry stores Birthday and Anniversary date fields
319 /// with the format: DD/MM/YYYY
321 std::string
Date::ToBBString() const
323 std::ostringstream oss
;
324 // setw() ain't 'sticky'!
325 oss
<< setw(2) << setfill('0') << Day
<< '/'
326 << setw(2) << setfill('0') << Month
+ 1 << '/'
327 << setw(2) << setfill('0') << Year
;
331 bool Date::FromTm(const struct tm
*timep
)
333 Year
= timep
->tm_year
+ 1900;
334 Month
= timep
->tm_mon
;
335 Day
= timep
->tm_mday
;
339 bool Date::FromBBString(const std::string
&str
)
342 if( 3 == sscanf(str
.c_str(), "%d/%d/%d", &d
, &m
, &y
) ) {
351 bool Date::FromYYYYMMDD(const std::string
&str
)
354 if( 3 == sscanf(str
.c_str(), "%4d%2d%2d", &y
, &m
, &d
) ) {
363 std::ostream
& operator<<(std::ostream
&os
, const Date
&date
)
365 os
<< setw(4) << date
.Year
<< '/'
366 << setw(2) << date
.Month
<< '/'
367 << setw(2) << date
.Day
;
372 ///////////////////////////////////////////////////////////////////////////////
373 // CategoryList class
375 /// Parses the given comma delimited category string into
376 /// this CategoryList object, appending each token to the vector.
377 /// Will clear vector beforehand.
378 void CategoryList::CategoryStr2List(const std::string
&str
)
386 // parse the comma-delimited string to a list, stripping away
387 // any white space around each category name
388 string::size_type start
= 0, end
= 0, delim
= str
.find(',', start
);
389 while( start
!= string::npos
) {
390 if( delim
== string::npos
)
391 end
= str
.size() - 1;
395 // strip surrounding whitespace
396 while( str
[start
] == ' ' )
398 while( end
&& str
[end
] == ' ' )
402 string token
= str
.substr(start
, end
-start
+1);
408 if( start
!= string::npos
)
410 delim
= str
.find(',', start
);
414 /// Turns the current vectory into a comma delimited category
415 /// string suitable for use in Calendar, Task, and Memo protocol values.
416 void CategoryList::CategoryList2Str(std::string
&str
) const
420 Barry::CategoryList::const_iterator i
= begin();
421 for( ; i
!= end(); ++i
) {
436 int main(int argc
, char *argv
[])
439 cerr
<< "Usage: test <datafile>" << endl
;
443 std::vector
<Data
> array
;
444 if( !LoadDataArray(argv
[1], array
) ) {
445 cerr
<< "Unable to load file: " << argv
[1] << endl
;
449 cout
<< "Loaded " << array
.size() << " items" << endl
;
451 for( std::vector
<Data
>::iterator b
= array
.begin(), e
= array
.end();
455 // cout << d << endl;
456 if( d
.GetSize() > 13 && d
.GetData()[6] == 0x4f ) {
457 Barry::Contact contact
;
459 contact
.ParseFields(d
, size
);
460 cout
<< contact
<< endl
;
461 contact
.DumpLdif(cout
, "ou=People,dc=example,dc=com");
463 else if( d
.GetSize() > 13 && d
.GetData()[6] == 0x44 ) {
466 cal
.ParseFields(d
, size
);