- fixed bug in vcard.cc that was using the FN field
[barry.git] / src / r_pin_message.cc
blobf42929c628e0a4511f1559d82f54e9188131edd8
1 ///
2 /// \file r_pin_message.cc
3 /// Blackberry database record parser class for pin message records.
4 ///
6 /*
7 Copyright (C) 2005-2007, 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"
25 #include "protocol.h"
26 #include "protostructs.h"
27 #include "data.h"
28 #include "time.h"
29 #include "error.h"
30 #include "endian.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 {
45 ///////////////////////////////////////////////////////////////////////////////
46 // PINMessage class
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
102 return begin;
104 if( !btohs(field->size) ) // if field has no size, something's up
105 return begin;
107 // cycle through the type table
108 for( FieldLink<PINMessage> *b = PINMessageFieldLinks;
109 b->type != PNMFC_END;
110 b++ )
112 if( b->type == field->type ) {
113 if( b->strMember ) {
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;
131 return begin;
136 // handle special cases
137 switch( field->type )
139 case PNMFC_RECORDID:
140 MessageRecordId = field->u.uint32;
141 return begin;
144 // if still not handled, add to the Unknowns list
145 UnknownField uf;
146 uf.type = field->type;
147 uf.data.assign((const char*)field->u.raw, btohs(field->size));
148 Unknowns.push_back(uf);
150 return begin;
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 MAKE_RECORD(const Barry::Protocol::MessageRecord, mr, data, offset);
167 // Priority
168 MessagePriority = NormalPriority;
169 if( mr->priority & PRIORITY_MASK ) {
170 if( mr->priority & PRIORITY_HIGH ) {
171 MessagePriority = HighPriority;
173 else if( mr->priority & PRIORITY_LOW ) {
174 MessagePriority = LowPriority;
176 else
177 MessagePriority = UnknownPriority;
179 // Sensitivity
180 MessageSensitivity = NormalSensitivity;
181 if( mr->priority & SENSITIVE_MASK ) {
182 if(( mr->priority & SENSITIVE_CONFIDENTIAL ) == SENSITIVE_CONFIDENTIAL ) {
183 MessageSensitivity = Confidential;
185 else if(( mr->priority & SENSITIVE_PRIVATE ) == SENSITIVE_PRIVATE ) {
186 MessageSensitivity = Private;
188 else if(( mr->priority & SENSITIVE_PERSONAL ) == SENSITIVE_PERSONAL ) {
189 MessageSensitivity = Personal;
191 else
192 MessageSensitivity = UnknownSensitivity;
194 // X-rim-org-message-ref-id // NOTE: I'm cheating a bit here and using this as a reply-to
195 if( mr->inReplyTo ) // It's actually sent by BB with the actual UID in every message
196 MessageReplyTo = mr->inReplyTo;
197 // Status Flags
198 if( !( mr->flags & MESSAGE_READ ))
199 MessageRead = true; // NOTE: A lot of these flags are 'backwards' but this seemed
200 // like the most logical way to interpret them for now
201 if(( mr->flags & MESSAGE_REPLY ) == MESSAGE_REPLY )
202 MessageReply = true; // NOTE: This is a reply, the original message's flags are not changed
203 // the inReplyTo field is updated with the original messages's UID
204 if( !( mr->flags & MESSAGE_TRUNCATED ))
205 MessageTruncated = true; // NOTE: This bit is unset on truncation, around 4096 on my 7100g
206 // NOTE: bit 0x400 is set on REALLY huge messages, haven't tested
207 // the exact size yet
208 if( !( mr->flags & MESSAGE_SAVED ))
209 MessageSaved = true; // NOTE: Saved to 'saved' folder
210 if( !( mr->flags & MESSAGE_SAVED_DELETED ))
211 MessageSavedDeleted = true; // NOTE: Saved to 'saved' folder and then deleted from inbox
213 MessageDateSent = ( mr->dateSent & 0x01ff ) - 0x29;
214 MessageDateSent = DayToDate( MessageDateSent );
215 MessageDateSent += (time_t)( mr->timeSent*1.77 );
217 MessageDateReceived = ( mr->dateReceived & 0x01ff ) - 0x29;
218 MessageDateReceived = DayToDate( MessageDateReceived );
219 MessageDateReceived += (time_t)( mr->timeReceived*1.77 );
221 offset += MESSAGE_RECORD_HEADER_SIZE;
224 void PINMessage::ParseFields(const Data &data, size_t &offset)
226 const unsigned char *finish = ParseCommonFields(*this,
227 data.GetData() + offset, data.GetData() + data.GetSize());
228 offset += finish - (data.GetData() + offset);
231 void PINMessage::BuildHeader(Data &data, size_t &offset) const
233 throw std::logic_error("PINMessage::BuildHeader not yet implemented");
236 void PINMessage::BuildFields(Data &data, size_t &offset) const
238 throw std::logic_error("PINMessage::BuildFields not yet implemented");
241 void PINMessage::Clear()
243 From.clear();
244 To.clear();
245 Cc.clear();
246 Bcc.clear();
248 Subject.clear();
249 Body.clear();
251 MessageRecordId = 0;
252 MessageReplyTo = 0;
253 MessageDateSent = 0;
254 MessageDateReceived = 0;
255 MessageTruncated = false;
256 MessageRead = false;
257 MessageReply = false;
258 MessageSaved = false;
259 MessageSavedDeleted = false;
261 Unknowns.clear();
264 // dump message in mbox format
265 void PINMessage::Dump(std::ostream &os) const
267 static const char *MessageImportance[] =
268 { "Low", "Normal", "High", "Unknown Priority" };
269 static const char *MessageSensitivityString[] =
270 { "Normal", "Personal", "Private", "Confidential", "Unknown Sensivity" };
272 os << "From " << (From.Email.size() ? From.Email.c_str() : "unknown")
273 << " " << ctime( &MessageDateSent );
274 os << "X-Record-ID: (" << setw(8) << std::hex << MessageRecordId << ")\n";
275 if( MessageReplyTo )
276 os << "X-rim-org-msg-ref-id: " << std::dec << MessageReplyTo << "\n";
277 if( MessageSaved )
278 os << "Message Status: Saved\n";
279 else if( MessageRead )
280 os << "Message Status: Opened\n";
281 if( MessagePriority != NormalPriority )
282 os << "Importance: " << MessageImportance[MessagePriority] << "\n";
283 if( MessageSensitivity != NormalSensitivity )
284 os << "Sensitivity: " << MessageSensitivityString[MessageSensitivity] << "\n";
285 os << "Date: " << ctime(&MessageDateSent);
287 if( From.Name.size()) {
288 os << " From: " << From.Name << " <" << From.Email << ">\n";
290 if( To.Name.size()) {
291 os << " To: " << To.Name << " <" << To.Email << ">\n";
293 if( Cc.Name.size()) {
294 os << " Cc: " << Cc.Name << " <" << Cc.Email << ">\n";
296 if( Bcc.Name.size()) {
297 os << " Bcc: " << Bcc.Name << " <" << Bcc.Email << ">\n";
300 if( Subject.size() )
301 os << " Subject: " << Subject << "\n";
302 else
303 os << " Subject: <>\n";
304 os << "\n";
306 for( std::string::const_iterator i = Body.begin();
307 i != Body.end() && *i;
308 i++)
310 if( *i == '\r' )
311 os << '\n';
312 else
313 os << *i;
315 os << "\n";
317 os << Unknowns;
318 os << "\n\n";
322 } // namespace Barry