i18n work in progress... desperately needs some housecleaning.
[barry.git] / src / r_saved_message.cc
blobe63a2fb4d6bd7a8cee3d555031374e5553f7c52a
1 ///
2 /// \file r_saved_message.cc
3 /// Blackberry database record parser class for saved email
4 /// message records.
5 ///
7 /*
8 Copyright (C) 2005-2008, Net Direct Inc. (http://www.netdirect.ca/)
9 Copyright (C) 2005-2007, Brian Edginton (edge@edginton.net)
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.
24 #include "r_saved_message.h"
25 #include "record-internal.h"
26 #include "protocol.h"
27 #include "protostructs.h"
28 #include "data.h"
29 #include "time.h"
30 #include "error.h"
31 #include "endian.h"
32 #include <ostream>
33 #include <iomanip>
34 #include <time.h>
35 #include <stdexcept>
37 #define __DEBUG_MODE__
38 #include "debug.h"
40 using namespace std;
41 using namespace Barry::Protocol;
43 namespace Barry {
45 ///////////////////////////////////////////////////////////////////////////////
46 // Message class
49 // Email / message field codes
50 #define SEMFC_TO 0x01 // can occur multiple times
51 #define SEMFC_CC 0x02 // ditto
52 #define SEMFC_BCC 0x03 // ditto
53 #define SEMFC_SENDER 0x04
54 #define SEMFC_FROM 0x05
55 #define SEMFC_REPLY_TO 0x06
56 #define SEMFC_SUBJECT 0x0b
57 #define SEMFC_BODY 0x0c
58 #define SEMFC_ATTACHMENT 0x16
59 #define SEMFC_RECORDID 0x4b
60 #define SEMFC_END 0xffff
62 #define PRIORITY_MASK 0x003f
63 #define PRIORITY_HIGH 0x0008
64 #define PRIORITY_LOW 0x0002
66 #define SENSITIVE_MASK 0xff80
67 #define SENSITIVE_CONFIDENTIAL 0x0100
68 #define SENSITIVE_PERSONAL 0x0080
69 #define SENSITIVE_PRIVATE 0x0040 // actual pattern is 0x00C0
71 #define MESSAGE_READ 0x0800
72 #define MESSAGE_REPLY 0x0001
73 #define MESSAGE_SAVED 0x0002
74 #define MESSAGE_FORWARD 0x0008
75 #define MESSAGE_TRUNCATED 0x0020
76 #define MESSAGE_SAVED_DELETED 0x0080
78 FieldLink<SavedMessage> SavedMessageFieldLinks[] = {
79 { SEMFC_TO, "To", 0, 0, 0, &SavedMessage::To, 0 },
80 { SEMFC_CC, "Cc", 0, 0, 0, &SavedMessage::Cc, 0 },
81 { SEMFC_BCC, "Bcc", 0, 0, 0, &SavedMessage::Bcc, 0 },
82 { SEMFC_SENDER, "Sender", 0, 0, 0, &SavedMessage::Sender, 0 },
83 { SEMFC_FROM, "From", 0, 0, 0, &SavedMessage::From, 0 },
84 { SEMFC_REPLY_TO, "ReplyTo", 0, 0, 0, &SavedMessage::ReplyTo, 0 },
85 { SEMFC_SUBJECT, "Subject", 0, 0, &SavedMessage::Subject, 0, 0 },
86 { SEMFC_BODY, "Body", 0, 0, &SavedMessage::Body, 0, 0 },
87 { SEMFC_ATTACHMENT, "Attachment", 0, 0, &SavedMessage::Attachment, 0, 0 },
88 { SEMFC_END, "End of List", 0, 0, 0, 0, 0 }
91 SavedMessage::SavedMessage()
93 Clear();
96 SavedMessage::~SavedMessage()
100 const unsigned char* SavedMessage::ParseField(const unsigned char *begin,
101 const unsigned char *end)
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<SavedMessage> *b = SavedMessageFieldLinks;
115 b->type != SEMFC_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 return begin; // done!
125 else if( b->addrMember ) {
126 // parse email address
127 // get dual name+addr string first
128 const char *fa = (const char*)field->u.addr.addr;
129 std::string dual(fa, btohs(field->size) - sizeof(field->u.addr.unknown));
131 // assign first string, using null terminator...letting std::string add it for us if it doesn't exist
132 EmailAddress &a = this->*(b->addrMember);
133 a.Name = dual.c_str();
135 // assign second string, using first size as starting point
136 a.Email = dual.c_str() + a.Name.size() + 1;
137 return begin;
142 // handle special cases
143 switch( field->type )
145 case SEMFC_RECORDID:
146 MessageRecordId = btohl(field->u.uint32);
147 return begin;
150 // if still not handled, add to the Unknowns list
151 UnknownField uf;
152 uf.type = field->type;
153 uf.data.assign((const char*)field->u.raw, btohs(field->size));
154 Unknowns.push_back(uf);
156 return begin;
159 void SavedMessage::ParseHeader(const Data &data, size_t &offset)
161 Protocol::CheckSize(data, offset + MESSAGE_RECORD_HEADER_SIZE);
163 MAKE_RECORD(const Barry::Protocol::MessageRecord, mr, data, offset);
165 // Priority
166 MessagePriority = NormalPriority;
168 uint16_t priority = btohs(mr->priority); // deal with endian swap once
169 if( priority & PRIORITY_MASK ) {
170 if( priority & PRIORITY_HIGH ) {
171 MessagePriority = HighPriority;
173 else if( priority & PRIORITY_LOW ) {
174 MessagePriority = LowPriority;
176 else
177 MessagePriority = UnknownPriority;
179 // Sensitivity
180 MessageSensitivity = NormalSensitivity;
181 if( priority & SENSITIVE_MASK ) {
182 if(( priority & SENSITIVE_CONFIDENTIAL ) == SENSITIVE_CONFIDENTIAL ) {
183 MessageSensitivity = Confidential;
185 else if(( priority & SENSITIVE_PRIVATE ) == SENSITIVE_PRIVATE ) {
186 MessageSensitivity = Private;
188 else if(( 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 = btohl(mr->inReplyTo);
198 // Status Flags
199 uint32_t flags = btohl(mr->flags);
200 if( !( flags & MESSAGE_READ ))
201 MessageRead = true; // NOTE: A lot of these flags are 'backwards' but this seemed
202 // like the most logical way to interpret them for now
203 if(( flags & MESSAGE_REPLY ) == MESSAGE_REPLY )
204 MessageReply = true; // NOTE: This is a reply, the original message's flags are not changed
205 // the inReplyTo field is updated with the original messages's UID
206 if( !( flags & MESSAGE_TRUNCATED ))
207 MessageTruncated = true; // NOTE: This bit is unset on truncation, around 4096 on my 7100g
208 // NOTE: bit 0x400 is set on REALLY huge messages, haven't tested
209 // the exact size yet
210 if( !( flags & MESSAGE_SAVED ))
211 MessageSaved = true; // NOTE: Saved to 'saved' folder
212 if( !( flags & MESSAGE_SAVED_DELETED ))
213 MessageSavedDeleted = true; // NOTE: Saved to 'saved' folder and then deleted from inbox
215 MessageDateSent = Message2Time(mr->dateSent, mr->timeSent);
216 MessageDateReceived = Message2Time(mr->dateReceived, mr->timeReceived);
218 offset += MESSAGE_RECORD_HEADER_SIZE;
221 void SavedMessage::ParseFields(const Data &data, size_t &offset)
223 const unsigned char *finish = ParseCommonFields(*this,
224 data.GetData() + offset, data.GetData() + data.GetSize());
225 offset += finish - (data.GetData() + offset);
228 void SavedMessage::BuildHeader(Data &data, size_t &offset) const
230 throw std::logic_error("SavedMessage::BuildHeader not yet implemented");
233 void SavedMessage::BuildFields(Data &data, size_t &offset) const
235 throw std::logic_error("SavedMessage::BuildFields not yet implemented");
238 void SavedMessage::Clear()
240 From.clear();
241 To.clear();
242 Cc.clear();
243 Bcc.clear();
244 Sender.clear();
245 ReplyTo.clear();
246 Subject.clear();
247 Body.clear();
248 Attachment.clear();
250 MessageRecordId = 0;
251 MessageReplyTo = 0;
252 MessageDateSent = 0;
253 MessageDateReceived = 0;
254 MessageTruncated = false;
255 MessageRead = false;
256 MessageReply = false;
257 MessageSaved = false;
258 MessageSavedDeleted = false;
259 Unknowns.clear();
262 // dump message in mbox format
263 void SavedMessage::Dump(std::ostream &os) const
265 static const char *MessageImportance[] =
266 { "Low", "Normal", "High", "Unknown Priority" };
267 static const char *MessageSensitivityString[] =
268 { "Normal", "Personal", "Private", "Confidential", "Unknown Sensivity" };
270 os << "From " << (From.Email.size() ? From.Email.c_str() : "unknown")
271 << " " << ctime( &MessageDateSent );
272 os << "X-Record-ID: (" << setw(8) << std::hex << MessageRecordId << ")\n";
273 if( MessageReplyTo )
274 os << "X-rim-org-msg-ref-id: " << std::dec << MessageReplyTo << "\n";
275 if( MessageSaved )
276 os << "Message Status: Saved\n";
277 else if( MessageRead )
278 os << "Message Status: Opened\n";
279 if( MessagePriority != NormalPriority )
280 os << "Importance: " << MessageImportance[MessagePriority] << "\n";
281 if( MessageSensitivity != NormalSensitivity )
282 os << "Sensitivity: " << MessageSensitivityString[MessageSensitivity] << "\n";
283 os << "Date: " << ctime(&MessageDateSent);
284 os << "From: " << From << "\n";
285 if( To.Email.size() )
286 os << "To: " << To << "\n";
287 if( Cc.Email.size() )
288 os << "Cc: " << Cc << "\n";
289 if( Bcc.Email.size() )
290 os << "Bcc: " << Bcc << "\n";
291 if( Sender.Email.size() )
292 os << "Sender: " << Sender << "\n";
293 if( ReplyTo.Email.size())
294 os << "Reply To: " << ReplyTo << "\n";
295 if( Subject.size() )
296 os << "Subject: " << Subject << "\n";
297 os << "\n";
298 for( std::string::const_iterator i = Body.begin();
299 i != Body.end() && *i;
300 i++)
302 if( *i == '\r' )
303 os << '\n';
304 else
305 os << *i;
307 os << "\n";
308 if( Attachment.size() )
309 os << "Attachments: " << Attachment << "\n";
311 os << Unknowns;
312 os << "\n\n";
316 } // namespace Barry