3 /// Blackberry database record parser class for email records.
7 Copyright (C) 2005-2007, 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_message.h"
23 #include "record-internal.h"
25 #include "protostructs.h"
35 #define __DEBUG_MODE__
39 using namespace Barry::Protocol
;
43 ///////////////////////////////////////////////////////////////////////////////
47 // Email / message field codes
48 #define MFC_TO 0x01 // can occur multiple times
49 #define MFC_CC 0x02 // ditto
50 #define MFC_BCC 0x03 // ditto
51 #define MFC_SENDER 0x04
53 #define MFC_REPLY_TO 0x06
54 #define MFC_SUBJECT 0x0b
56 #define MFC_REPLY_UNKNOWN 0x12 // This shows up as 0x00 on replies but we don't do much with it now
57 #define MFC_ATTACHMENT 0x16
58 #define MFC_RECORDID 0x4b
59 #define MFC_END 0xffff
61 #define PRIORITY_MASK 0x003f
62 #define PRIORITY_HIGH 0x0008
63 #define PRIORITY_LOW 0x0002
65 #define SENSITIVE_MASK 0xff80
66 #define SENSITIVE_CONFIDENTIAL 0x0100
67 #define SENSITIVE_PERSONAL 0x0080
68 #define SENSITIVE_PRIVATE 0x0040 // actual pattern is 0x00C0
70 #define MESSAGE_READ 0x0800
71 #define MESSAGE_REPLY 0x0001
72 #define MESSAGE_SAVED 0x0002
73 #define MESSAGE_FORWARD 0x0008
74 #define MESSAGE_TRUNCATED 0x0020
75 #define MESSAGE_SAVED_DELETED 0x0080
77 FieldLink
<Message
> MessageFieldLinks
[] = {
78 { MFC_TO
, "To", 0, 0, 0, &Message::To
, 0 },
79 { MFC_CC
, "Cc", 0, 0, 0, &Message::Cc
, 0 },
80 { MFC_BCC
, "Bcc", 0, 0, 0, &Message::Bcc
, 0 },
81 { MFC_SENDER
, "Sender", 0, 0, 0, &Message::Sender
, 0 },
82 { MFC_FROM
, "From", 0, 0, 0, &Message::From
, 0 },
83 { MFC_REPLY_TO
, "ReplyTo", 0, 0, 0, &Message::ReplyTo
, 0 },
84 { MFC_SUBJECT
, "Subject", 0, 0, &Message::Subject
, 0, 0 },
85 { MFC_BODY
, "Body", 0, 0, &Message::Body
, 0, 0 },
86 { MFC_ATTACHMENT
, "Attachment", 0, 0, &Message::Attachment
, 0, 0 },
87 { MFC_END
, "End of List", 0, 0, 0, 0, 0 }
99 const unsigned char* Message::ParseField(const unsigned char *begin
,
100 const unsigned char *end
)
102 const CommonField
*field
= (const CommonField
*) begin
;
104 // advance and check size
105 begin
+= COMMON_FIELD_HEADER_SIZE
+ btohs(field
->size
);
106 if( begin
> end
) // if begin==end, we are ok
109 if( !btohs(field
->size
) ) // if field has no size, something's up
112 // cycle through the type table
113 for( FieldLink
<Message
> *b
= MessageFieldLinks
;
117 if( b
->type
== field
->type
) {
119 // parse regular string
120 std::string
&s
= this->*(b
->strMember
);
121 s
= ParseFieldString(field
);
122 return begin
; // done!
124 else if( b
->addrMember
) {
125 // parse email address
126 // get dual name+addr string first
127 const char *fa
= (const char*)field
->u
.addr
.addr
;
128 std::string
dual(fa
, btohs(field
->size
) - sizeof(field
->u
.addr
.unknown
));
130 // assign first string, using null terminator...letting std::string add it for us if it doesn't exist
131 EmailAddress
&a
= this->*(b
->addrMember
);
132 a
.Name
= dual
.c_str();
134 // assign second string, using first size as starting point
135 a
.Email
= dual
.c_str() + a
.Name
.size() + 1;
140 // handle special cases
142 switch( field
->type
)
145 MessageRecordId
= field
->u
.uint32
;
147 case MFC_REPLY_UNKNOWN
:
148 swallow
= field
->u
.raw
[0];
151 // if still not handled, add to the Unknowns list
153 uf
.type
= field
->type
;
154 uf
.data
.assign((const char*)field
->u
.raw
, btohs(field
->size
));
155 Unknowns
.push_back(uf
);
160 uint8_t Message::GetRecType() const
162 throw std::logic_error("Message::GetRecType() called, and not supported by the USB protocol. Should never get called.");
165 // empty API, not required by protocol
166 uint32_t Message::GetUniqueId() const
168 throw std::logic_error("Message::GetUniqueId() called, and not supported by the USB protocol. Should never get called.");
171 void Message::ParseHeader(const Data
&data
, size_t &offset
)
173 MAKE_RECORD(const Barry::Protocol::MessageRecord
, mr
, data
, offset
);
176 MessagePriority
= NormalPriority
;
177 if( mr
->priority
& PRIORITY_MASK
) {
178 if( mr
->priority
& PRIORITY_HIGH
) {
179 MessagePriority
= HighPriority
;
181 else if( mr
->priority
& PRIORITY_LOW
) {
182 MessagePriority
= LowPriority
;
185 MessagePriority
= UnknownPriority
;
188 MessageSensitivity
= NormalSensitivity
;
189 if( mr
->priority
& SENSITIVE_MASK
) {
190 if(( mr
->priority
& SENSITIVE_CONFIDENTIAL
) == SENSITIVE_CONFIDENTIAL
) {
191 MessageSensitivity
= Confidential
;
193 else if(( mr
->priority
& SENSITIVE_PRIVATE
) == SENSITIVE_PRIVATE
) {
194 MessageSensitivity
= Private
;
196 else if(( mr
->priority
& SENSITIVE_PERSONAL
) == SENSITIVE_PERSONAL
) {
197 MessageSensitivity
= Personal
;
200 MessageSensitivity
= UnknownSensitivity
;
202 // X-rim-org-message-ref-id // NOTE: I'm cheating a bit here and using this as a reply-to
203 if( mr
->inReplyTo
) // It's actually sent by BB with the actual UID in every message
204 MessageReplyTo
= mr
->inReplyTo
;
206 if( !( mr
->flags
& MESSAGE_READ
))
207 MessageRead
= true; // NOTE: A lot of these flags are 'backwards' but this seemed
208 // like the most logical way to interpret them for now
209 if(( mr
->flags
& MESSAGE_REPLY
) == MESSAGE_REPLY
)
210 MessageReply
= true; // NOTE: This is a reply, the original message's flags are not changed
211 // the inReplyTo field is updated with the original messages's UID
212 if( !( mr
->flags
& MESSAGE_TRUNCATED
))
213 MessageTruncated
= true; // NOTE: This bit is unset on truncation, around 4096 on my 7100g
214 // NOTE: bit 0x400 is set on REALLY huge messages, haven't tested
215 // the exact size yet
216 if( !( mr
->flags
& MESSAGE_SAVED
))
217 MessageSaved
= true; // NOTE: Saved to 'saved' folder
218 if( !( mr
->flags
& MESSAGE_SAVED_DELETED
))
219 MessageSavedDeleted
= true; // NOTE: Saved to 'saved' folder and then deleted from inbox
221 MessageDateSent
= ( mr
->dateSent
& 0x01ff ) - 0x29;
222 MessageDateSent
= DayToDate( MessageDateSent
);
223 MessageDateSent
+= (time_t)( mr
->timeSent
*1.77 );
225 MessageDateReceived
= ( mr
->dateReceived
& 0x01ff ) - 0x29;
226 MessageDateReceived
= DayToDate( MessageDateReceived
);
227 MessageDateReceived
+= (time_t)( mr
->timeReceived
*1.77 );
229 offset
+= MESSAGE_RECORD_HEADER_SIZE
;
232 void Message::ParseFields(const Data
&data
, size_t &offset
)
234 const unsigned char *finish
= ParseCommonFields(*this,
235 data
.GetData() + offset
, data
.GetData() + data
.GetSize());
236 offset
+= finish
- (data
.GetData() + offset
);
239 void Message::BuildHeader(Data
&data
, size_t &offset
) const
241 throw std::logic_error("Message::BuildHeader not yet implemented");
244 void Message::BuildFields(Data
&data
, size_t &offset
) const
246 throw std::logic_error("Message::BuildFields not yet implemented");
249 void Message::Clear()
264 MessageDateReceived
= 0;
265 MessageTruncated
= false;
267 MessageReply
= false;
268 MessageSaved
= false;
269 MessageSavedDeleted
= false;
274 std::string
Message::SimpleEmailAddress() const
276 if( From
.Email
.size() ) {
277 // remove all spaces from the email
279 for( size_t i
= 0; i
< From
.Email
.size(); i
++ )
280 if( From
.Email
[i
] != ' ' )
281 ret
+= From
.Email
[i
];
290 // dump message in mbox format
291 void Message::Dump(std::ostream
&os
) const
293 static const char *MessageImportance
[] =
294 { "Low", "Normal", "High", "Unknown Priority" };
295 static const char *MessageSensitivityString
[] =
296 { "Normal", "Personal", "Private", "Confidential", "Unknown Sensivity" };
298 os
<< "From " << SimpleEmailAddress() << " " << ctime( &MessageDateReceived
);
299 os
<< "X-Record-ID: (" << setw(8) << std::hex
<< MessageRecordId
<< ")\n";
301 os
<< "X-rim-org-msg-ref-id: " << std::dec
<< MessageReplyTo
<< "\n";
303 os
<< "X-Message-Status: Saved\n";
304 else if( MessageRead
)
305 os
<< "Message Status: Opened\n";
306 if( MessagePriority
!= NormalPriority
)
307 os
<< "Importance: " << MessageImportance
[MessagePriority
] << "\n";
308 if( MessageSensitivity
!= NormalSensitivity
)
309 os
<< "Sensitivity: " << MessageSensitivityString
[MessageSensitivity
] << "\n";
310 os
<< "Date: " << ctime(&MessageDateSent
);
311 os
<< "From: " << From
<< "\n";
312 if( To
.Email
.size() )
313 os
<< "To: " << To
<< "\n";
314 if( Cc
.Email
.size() )
315 os
<< "Cc: " << Cc
<< "\n";
316 if( Bcc
.Email
.size() )
317 os
<< "Bcc: " << Bcc
<< "\n";
318 if( Sender
.Email
.size() )
319 os
<< "Sender: " << Sender
<< "\n";
320 if( ReplyTo
.Email
.size())
321 os
<< "Reply To: " << ReplyTo
<< "\n";
323 os
<< "Subject: " << Subject
<< "\n";
325 for( std::string::const_iterator i
= Body
.begin();
326 i
!= Body
.end() && *i
;
335 if( Attachment
.size() )
336 os
<< "Attachments: " << Attachment
<< "\n";