2 /// \file r_pin_message.cc
3 /// Blackberry database record parser class for pin message records.
7 Copyright (C) 2005-2008, Net Direct Inc. (http://www.netdirect.ca/)
8 Copyright (C) 2007, Brian Edginton (edge@edginton.net)
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 #include "r_pin_message.h"
24 #include "record-internal.h"
26 #include "protostructs.h"
36 #define __DEBUG_MODE__
40 using namespace Barry::Protocol
;
45 ///////////////////////////////////////////////////////////////////////////////
49 // PIN message field codes
50 #define PNMFC_TO 0x01 // can occur multiple times
51 #define PNMFC_CC 0x02 // ditto
52 #define PNMFC_BCC 0x03 // ditto
53 #define PNMFC_FROM 0x05
54 #define PNMFC_SUBJECT 0x0b
55 #define PNMFC_BODY 0x0c
56 #define PNMFC_REPLY_UNKNOWN 0x12 // this appears on replies, always 0x00
57 #define PNMFC_RECORDID 0x4b // Internal Message ID, mimics header RecNumber
58 #define PNMFC_END 0xffff
60 #define PRIORITY_MASK 0x003f
61 #define PRIORITY_HIGH 0x0008
62 #define PRIORITY_LOW 0x0002
64 #define SENSITIVE_MASK 0xff80
65 #define SENSITIVE_CONFIDENTIAL 0x0100
66 #define SENSITIVE_PERSONAL 0x0080
67 #define SENSITIVE_PRIVATE 0x0040 // actual pattern is 0x00C0
69 #define MESSAGE_READ 0x0800
70 #define MESSAGE_REPLY 0x0001
71 #define MESSAGE_SAVED 0x0002
72 #define MESSAGE_FORWARD 0x0008
73 #define MESSAGE_TRUNCATED 0x0020
74 #define MESSAGE_SAVED_DELETED 0x0080
76 FieldLink
<PINMessage
> PINMessageFieldLinks
[] = {
77 { PNMFC_TO
, "To", 0, 0, 0, &PINMessage::To
, 0 },
78 { PNMFC_CC
, "Cc", 0, 0, 0, &PINMessage::Cc
, 0 },
79 { PNMFC_BCC
, "Bcc", 0, 0, 0, &PINMessage::Bcc
, 0 },
80 { PNMFC_FROM
, "From", 0, 0, 0, &PINMessage::From
, 0 },
81 { PNMFC_SUBJECT
, "Subject", 0, 0, &PINMessage::Subject
, 0, 0 },
82 { PNMFC_BODY
, "Body", 0, 0, &PINMessage::Body
, 0, 0 },
83 { PNMFC_END
, "End of List", 0, 0, 0, 0, 0 }
86 PINMessage::PINMessage()
90 PINMessage::~PINMessage()
94 const unsigned char* PINMessage::ParseField(const unsigned char *begin
,
95 const unsigned char *end
)
97 const CommonField
*field
= (const CommonField
*) begin
;
99 // advance and check size
100 begin
+= COMMON_FIELD_HEADER_SIZE
+ btohs(field
->size
);
101 if( begin
> end
) // if begin==end, we are ok
104 if( !btohs(field
->size
) ) // if field has no size, something's up
107 // cycle through the type table
108 for( FieldLink
<PINMessage
> *b
= PINMessageFieldLinks
;
109 b
->type
!= PNMFC_END
;
112 if( b
->type
== field
->type
) {
114 // parse regular string
115 std::string
&s
= this->*(b
->strMember
);
116 s
= ParseFieldString(field
);
117 return begin
; // done!
119 else if( b
->addrMember
) {
120 // parse email address
121 // get dual name+addr string first
122 const char *fa
= (const char*)field
->u
.addr
.addr
;
123 std::string
dual(fa
, btohs(field
->size
) - sizeof(field
->u
.addr
.unknown
));
125 // assign first string, using null terminator...letting std::string add it for us if it doesn't exist
126 EmailAddress
&a
= this->*(b
->addrMember
);
127 a
.Name
= dual
.c_str();
129 // assign second string, using first size as starting point
130 a
.Email
= dual
.c_str() + a
.Name
.size() + 1;
136 // handle special cases
137 switch( field
->type
)
140 MessageRecordId
= btohl(field
->u
.uint32
);
144 // if still not handled, add to the Unknowns list
146 uf
.type
= field
->type
;
147 uf
.data
.assign((const char*)field
->u
.raw
, btohs(field
->size
));
148 Unknowns
.push_back(uf
);
153 void PINMessage::ParseHeader(const Data
&data
, size_t &offset
)
155 // FIXME - we are using a Message (email) record header size
156 // for a PIN Message record... this is not necessarily guaranteed
157 // to be the same... someday we could use some more info on
158 // the message record header and pin message record header
160 // FIXED - the header structure for both the PIN messages and
161 // email messages are the same, as is the header structure for
162 // 'Saved Email Messages' although some of the fields may not directly apply.
164 // and someday is now here ;) - edge
166 Protocol::CheckSize(data
, offset
+ MESSAGE_RECORD_HEADER_SIZE
);
168 MAKE_RECORD(const Barry::Protocol::MessageRecord
, mr
, data
, offset
);
171 MessagePriority
= NormalPriority
;
173 uint16_t priority
= btohs(mr
->priority
); // deal with endian swap once
174 if( priority
& PRIORITY_MASK
) {
175 if( priority
& PRIORITY_HIGH
) {
176 MessagePriority
= HighPriority
;
178 else if( priority
& PRIORITY_LOW
) {
179 MessagePriority
= LowPriority
;
182 MessagePriority
= UnknownPriority
;
185 MessageSensitivity
= NormalSensitivity
;
186 if( priority
& SENSITIVE_MASK
) {
187 if(( priority
& SENSITIVE_CONFIDENTIAL
) == SENSITIVE_CONFIDENTIAL
) {
188 MessageSensitivity
= Confidential
;
190 else if(( priority
& SENSITIVE_PRIVATE
) == SENSITIVE_PRIVATE
) {
191 MessageSensitivity
= Private
;
193 else if(( priority
& SENSITIVE_PERSONAL
) == SENSITIVE_PERSONAL
) {
194 MessageSensitivity
= Personal
;
197 MessageSensitivity
= UnknownSensitivity
;
199 // X-rim-org-message-ref-id // NOTE: I'm cheating a bit here and using this as a reply-to
200 if( mr
->inReplyTo
) // It's actually sent by BB with the actual UID in every message
201 MessageReplyTo
= btohl(mr
->inReplyTo
);
204 uint32_t flags
= btohl(mr
->flags
);
205 if( !( flags
& MESSAGE_READ
))
206 MessageRead
= true; // NOTE: A lot of these flags are 'backwards' but this seemed
207 // like the most logical way to interpret them for now
208 if(( flags
& MESSAGE_REPLY
) == MESSAGE_REPLY
)
209 MessageReply
= true; // NOTE: This is a reply, the original message's flags are not changed
210 // the inReplyTo field is updated with the original messages's UID
211 if( !( flags
& MESSAGE_TRUNCATED
))
212 MessageTruncated
= true; // NOTE: This bit is unset on truncation, around 4096 on my 7100g
213 // NOTE: bit 0x400 is set on REALLY huge messages, haven't tested
214 // the exact size yet
215 if( !( flags
& MESSAGE_SAVED
))
216 MessageSaved
= true; // NOTE: Saved to 'saved' folder
217 if( !( flags
& MESSAGE_SAVED_DELETED
))
218 MessageSavedDeleted
= true; // NOTE: Saved to 'saved' folder and then deleted from inbox
220 MessageDateSent
= Message2Time(mr
->dateSent
, mr
->timeSent
);
221 MessageDateReceived
= Message2Time(mr
->dateReceived
, mr
->timeReceived
);
223 offset
+= MESSAGE_RECORD_HEADER_SIZE
;
226 void PINMessage::ParseFields(const Data
&data
, size_t &offset
)
228 const unsigned char *finish
= ParseCommonFields(*this,
229 data
.GetData() + offset
, data
.GetData() + data
.GetSize());
230 offset
+= finish
- (data
.GetData() + offset
);
233 void PINMessage::BuildHeader(Data
&data
, size_t &offset
) const
235 throw std::logic_error("PINMessage::BuildHeader not yet implemented");
238 void PINMessage::BuildFields(Data
&data
, size_t &offset
) const
240 throw std::logic_error("PINMessage::BuildFields not yet implemented");
243 void PINMessage::Clear()
256 MessageDateReceived
= 0;
257 MessageTruncated
= false;
259 MessageReply
= false;
260 MessageSaved
= false;
261 MessageSavedDeleted
= false;
266 // dump message in mbox format
267 void PINMessage::Dump(std::ostream
&os
) const
269 static const char *MessageImportance
[] =
270 { "Low", "Normal", "High", "Unknown Priority" };
271 static const char *MessageSensitivityString
[] =
272 { "Normal", "Personal", "Private", "Confidential", "Unknown Sensivity" };
274 os
<< "From " << (From
.Email
.size() ? From
.Email
.c_str() : "unknown")
275 << " " << ctime( &MessageDateSent
);
276 os
<< "X-Record-ID: (" << setw(8) << std::hex
<< MessageRecordId
<< ")\n";
278 os
<< "X-rim-org-msg-ref-id: " << std::dec
<< MessageReplyTo
<< "\n";
280 os
<< "Message Status: Saved\n";
281 else if( MessageRead
)
282 os
<< "Message Status: Opened\n";
283 if( MessagePriority
!= NormalPriority
)
284 os
<< "Importance: " << MessageImportance
[MessagePriority
] << "\n";
285 if( MessageSensitivity
!= NormalSensitivity
)
286 os
<< "Sensitivity: " << MessageSensitivityString
[MessageSensitivity
] << "\n";
287 os
<< "Date: " << ctime(&MessageDateSent
);
289 if( From
.Name
.size()) {
290 os
<< " From: " << From
.Name
<< " <" << From
.Email
<< ">\n";
292 if( To
.Name
.size()) {
293 os
<< " To: " << To
.Name
<< " <" << To
.Email
<< ">\n";
295 if( Cc
.Name
.size()) {
296 os
<< " Cc: " << Cc
.Name
<< " <" << Cc
.Email
<< ">\n";
298 if( Bcc
.Name
.size()) {
299 os
<< " Bcc: " << Bcc
.Name
<< " <" << Bcc
.Email
<< ">\n";
303 os
<< " Subject: " << Subject
<< "\n";
305 os
<< " Subject: <>\n";
308 for( std::string::const_iterator i
= Body
.begin();
309 i
!= Body
.end() && *i
;