Small fixups to Nicolas Vivien's bjavaloader patch
[barry.git] / src / r_message_base.cc
blob12cbc5bdeb07179ac7784e2d37853ad02bdfa573
1 ///
2 /// \file r_message_base.cc
3 /// Base class for email-oriented Blackberry database records
4 ///
6 /*
7 Copyright (C) 2005-2009, 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_message_base.h"
24 #include "record-internal.h"
25 #include "protostructs.h"
26 #include "data.h"
27 #include "time.h"
28 #include "endian.h"
29 #include "iconv.h"
30 #include <ostream>
31 #include <iomanip>
32 #include <stdexcept>
34 #define __DEBUG_MODE__
35 #include "debug.h"
37 using namespace std;
38 using namespace Barry::Protocol;
40 namespace Barry {
42 ///////////////////////////////////////////////////////////////////////////////
43 // Message class
46 // Email / message field codes
47 #define MBFC_TO 0x01 // can occur multiple times
48 #define MBFC_CC 0x02 // ditto
49 #define MBFC_BCC 0x03 // ditto
50 #define MBFC_SENDER 0x04
51 #define MBFC_FROM 0x05
52 #define MBFC_REPLY_TO 0x06
53 #define MBFC_SUBJECT 0x0b
54 #define MBFC_BODY 0x0c
55 #define MBFC_REPLY_UNKNOWN 0x12 // This shows up as 0x00 on replies
56 // but we don't do much with it now
57 #define MBFC_ATTACHMENT 0x16
58 #define MBFC_RECORDID 0x4b // Internal Message ID, mimics header RecNumber
59 #define MBFC_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 static FieldLink<MessageBase> MessageBaseFieldLinks[] = {
78 { MBFC_TO, "To", 0, 0, 0, &MessageBase::To, 0, 0, 0, true },
79 { MBFC_CC, "Cc", 0, 0, 0, &MessageBase::Cc, 0, 0, 0, true },
80 { MBFC_BCC, "Bcc", 0, 0, 0, &MessageBase::Bcc, 0, 0, 0, true },
81 { MBFC_SENDER, "Sender", 0, 0, 0, &MessageBase::Sender, 0, 0, 0, true },
82 { MBFC_FROM, "From", 0, 0, 0, &MessageBase::From, 0, 0, 0, true },
83 { MBFC_REPLY_TO, "ReplyTo", 0, 0, 0, &MessageBase::ReplyTo, 0, 0, 0, true },
84 { MBFC_SUBJECT, "Subject", 0, 0, &MessageBase::Subject, 0, 0, 0, 0, true },
85 { MBFC_BODY, "Body", 0, 0, &MessageBase::Body, 0, 0, 0, 0, true },
86 { MBFC_ATTACHMENT, "Attachment", 0, 0, &MessageBase::Attachment, 0, 0, 0, 0, false },
87 { MBFC_END, "End of List", 0, 0, 0, 0, 0, 0, 0, false }
90 MessageBase::MessageBase()
92 Clear();
95 MessageBase::~MessageBase()
99 const unsigned char* MessageBase::ParseField(const unsigned char *begin,
100 const unsigned char *end,
101 const IConverter *ic)
103 const CommonField *field = (const CommonField *) begin;
105 // advance and check size
106 begin += COMMON_FIELD_HEADER_SIZE + btohs(field->size);
107 if( begin > end ) // if begin==end, we are ok
108 return begin;
110 if( !btohs(field->size) ) // if field has no size, something's up
111 return begin;
113 // cycle through the type table
114 for( FieldLink<MessageBase> *b = MessageBaseFieldLinks;
115 b->type != MBFC_END;
116 b++ )
118 if( b->type == field->type ) {
119 if( b->strMember ) {
120 // parse regular string
121 std::string &s = this->*(b->strMember);
122 s = ParseFieldString(field);
123 if( b->iconvNeeded && ic )
124 s = ic->FromBB(s);
125 return begin; // done!
127 else if( b->addrMember ) {
128 // parse email address
129 // get dual name+addr string first
130 const char *fa = (const char*)field->u.addr.addr;
131 std::string dual(fa, btohs(field->size) - sizeof(field->u.addr.unknown));
133 // assign first string, using null terminator
134 // letting std::string add it for us if it
135 // doesn't exist
136 EmailAddress a;
137 a.Name = dual.c_str();
139 // assign second string, using first size
140 // as starting point
141 a.Email = dual.c_str() + a.Name.size() + 1;
143 // if the address is non-empty, add to list
144 if( a.size() ) {
145 // i18n convert if needed
146 if( b->iconvNeeded && ic ) {
147 a.Name = ic->FromBB(a.Name);
148 a.Email = ic->FromBB(a.Email);
151 EmailAddressList &al = this->*(b->addrMember);
152 al.push_back(a);
155 return begin;
160 // handle special cases
161 char swallow;
162 switch( field->type )
164 case MBFC_RECORDID:
165 MessageRecordId = btohl(field->u.uint32);
166 return begin;
168 case MBFC_REPLY_UNKNOWN: // FIXME - not available in SavedMessage?
169 swallow = field->u.raw[0];
170 return begin;
173 // if still not handled, add to the Unknowns list
174 UnknownField uf;
175 uf.type = field->type;
176 uf.data.assign((const char*)field->u.raw, btohs(field->size));
177 Unknowns.push_back(uf);
179 return begin;
182 uint8_t MessageBase::GetRecType() const
184 throw std::logic_error("MessageBase::GetRecType() called, and not supported by the USB protocol. Should never get called.");
187 uint32_t MessageBase::GetUniqueId() const
189 throw std::logic_error("MessageBase::GetUniqueId() called, and not supported by the USB protocol. Should never get called.");
192 void MessageBase::ParseHeader(const Data &data, size_t &offset)
194 Protocol::CheckSize(data, offset + MESSAGE_RECORD_HEADER_SIZE);
196 MAKE_RECORD(const Barry::Protocol::MessageRecord, mr, data, offset);
198 // Priority
199 MessagePriority = NormalPriority;
201 uint16_t priority = btohs(mr->priority); // deal with endian swap once
202 if( priority & PRIORITY_MASK ) {
203 if( priority & PRIORITY_HIGH ) {
204 MessagePriority = HighPriority;
206 else if( priority & PRIORITY_LOW ) {
207 MessagePriority = LowPriority;
209 else
210 MessagePriority = UnknownPriority;
213 // Sensitivity
214 MessageSensitivity = NormalSensitivity;
216 if( priority & SENSITIVE_MASK ) {
217 if(( priority & SENSITIVE_CONFIDENTIAL ) == SENSITIVE_CONFIDENTIAL ) {
218 MessageSensitivity = Confidential;
220 else if(( priority & SENSITIVE_PRIVATE ) == SENSITIVE_PRIVATE ) {
221 MessageSensitivity = Private;
223 else if(( priority & SENSITIVE_PERSONAL ) == SENSITIVE_PERSONAL ) {
224 MessageSensitivity = Personal;
226 else
227 MessageSensitivity = UnknownSensitivity;
230 // X-rim-org-message-ref-id
231 // NOTE: I'm cheating a bit here and using this as a reply-to
232 // It's actually sent by BB with the actual UID in every message
233 if( mr->inReplyTo )
234 MessageReplyTo = btohl(mr->inReplyTo);
236 // Status Flags
237 uint32_t flags = btohl(mr->flags);
239 // NOTE: A lot of these flags are 'backwards' but this seemed
240 // like the most logical way to interpret them for now
241 if( !( flags & MESSAGE_READ ))
242 MessageRead = true;
244 // NOTE: This is a reply, the original message's flags are not changed
245 // the inReplyTo field is updated with the original messages's UID
246 if(( flags & MESSAGE_REPLY ) == MESSAGE_REPLY )
247 MessageReply = true;
249 // NOTE: This bit is unset on truncation, around 4096 on my 7100g
250 // NOTE: bit 0x400 is set on REALLY huge messages, haven't tested
251 // the exact size yet
252 if( !( flags & MESSAGE_TRUNCATED ))
253 MessageTruncated = true;
255 // NOTE: Saved to 'saved' folder
256 if( !( flags & MESSAGE_SAVED ))
257 MessageSaved = true;
259 // NOTE: Saved to 'saved' folder and then deleted from inbox
260 if( !( flags & MESSAGE_SAVED_DELETED ))
261 MessageSavedDeleted = true;
263 MessageDateSent = Message2Time(mr->dateSent, mr->timeSent);
264 MessageDateReceived = Message2Time(mr->dateReceived, mr->timeReceived);
266 offset += MESSAGE_RECORD_HEADER_SIZE;
269 void MessageBase::ParseFields(const Data &data, size_t &offset, const IConverter *ic)
271 const unsigned char *finish = ParseCommonFields(*this,
272 data.GetData() + offset, data.GetData() + data.GetSize(), ic);
273 offset += finish - (data.GetData() + offset);
276 void MessageBase::BuildHeader(Data &data, size_t &offset) const
278 throw std::logic_error("MessageBase::BuildHeader not yet implemented");
281 void MessageBase::BuildFields(Data &data, size_t &offset, const IConverter *ic) const
283 throw std::logic_error("MessageBase::BuildFields not yet implemented");
286 void MessageBase::Clear()
288 From.clear();
289 To.clear();
290 Cc.clear();
291 Bcc.clear();
292 Sender.clear();
293 ReplyTo.clear();
295 Subject.clear();
296 Body.clear();
297 Attachment.clear();
299 MessageRecordId = 0;
300 MessageReplyTo = 0;
301 MessageDateSent = 0;
302 MessageDateReceived = 0;
303 MessageTruncated = false;
304 MessageRead = false;
305 MessageReply = false;
306 MessageSaved = false;
307 MessageSavedDeleted = false;
309 MessagePriority = NormalPriority;
310 MessageSensitivity = NormalSensitivity;
312 Unknowns.clear();
315 std::string MessageBase::SimpleFromAddress() const
317 if( From.size() ) {
318 // remove all spaces from the email
319 std::string ret;
320 for( size_t i = 0; i < From[0].Email.size(); i++ )
321 if( From[0].Email[i] != ' ' )
322 ret += From[0].Email[i];
324 return ret;
326 else {
327 return "unknown";
331 // dump message in mbox format
332 void MessageBase::Dump(std::ostream &os) const
334 static const char *MessageImportance[] =
335 { "Low", "Normal", "High", "Unknown Priority" };
336 static const char *MessageSensitivityString[] =
337 { "Normal", "Personal", "Private", "Confidential", "Unknown Sensivity" };
339 os << "From " << SimpleFromAddress() << " " << ctime( &MessageDateReceived );
342 FIXME
343 // savedmessage prints like this:
344 os << "From " << SimpleFromAddress() << " " << ctime( &MessageDateSent );
345 // pinmessage prints like this:
346 os << "From " << SimpleFromAddress() << " " << ctime( &MessageDateSent );
349 os << "X-Record-ID: (" << setw(8) << std::hex << MessageRecordId << ")\n";
351 if( MessageReplyTo )
352 os << "X-rim-org-msg-ref-id: " << std::dec << MessageReplyTo << "\n";
353 if( MessageSaved )
354 os << "X-Message-Status: Saved\n";
355 else if( MessageRead )
356 os << "Message Status: Opened\n";
357 if( MessagePriority != NormalPriority )
358 os << "Importance: " << MessageImportance[MessagePriority] << "\n";
359 if( MessageSensitivity != NormalSensitivity )
360 os << "Sensitivity: " << MessageSensitivityString[MessageSensitivity] << "\n";
361 os << "Date: " << ctime(&MessageDateSent);
362 if( From.size() )
363 os << "From: " << From[0] << "\n";
364 if( To.size() )
365 os << "To: " << To << "\n";
366 if( Cc.size() )
367 os << "Cc: " << Cc << "\n";
368 if( Bcc.size() )
369 os << "Bcc: " << Bcc << "\n";
370 if( Sender.size() )
371 os << "Sender: " << Sender << "\n";
372 if( ReplyTo.size())
373 os << "Reply To: " << ReplyTo << "\n";
374 if( Subject.size() )
375 os << "Subject: " << Subject << "\n";
376 os << "\n";
377 for( std::string::const_iterator i = Body.begin();
378 i != Body.end() && *i;
379 i++)
381 if( *i == '\r' )
382 os << '\n';
383 else
384 os << *i;
386 os << "\n";
388 if( Attachment.size() )
389 os << "Attachments: " << Data(Attachment.data(), Attachment.size()) << "\n";
391 os << Unknowns;
392 os << "\n\n";
395 } // namespace Barry