Barry-fied the sha1 code.
[barry/pauldeden.git] / src / r_message.cc
blobca57554ac70cc2b6f66ce9016ab3ee7bf39824d1
1 ///
2 /// \file r_message.cc
3 /// Blackberry database record parser class for email records.
4 ///
6 /*
7 Copyright (C) 2005-2008, 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"
24 #include "protocol.h"
25 #include "protostructs.h"
26 #include "data.h"
27 #include "time.h"
28 #include "error.h"
29 #include "endian.h"
30 #include <ostream>
31 #include <iomanip>
32 #include <time.h>
33 #include <stdexcept>
35 #define __DEBUG_MODE__
36 #include "debug.h"
38 using namespace std;
39 using namespace Barry::Protocol;
41 namespace Barry {
43 ///////////////////////////////////////////////////////////////////////////////
44 // Message class
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
52 #define MFC_FROM 0x05
53 #define MFC_REPLY_TO 0x06
54 #define MFC_SUBJECT 0x0b
55 #define MFC_BODY 0x0c
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 }
90 Message::Message()
92 Clear();
95 Message::~Message()
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
107 return begin;
109 if( !btohs(field->size) ) // if field has no size, something's up
110 return begin;
112 // cycle through the type table
113 for( FieldLink<Message> *b = MessageFieldLinks;
114 b->type != MFC_END;
115 b++ )
117 if( b->type == field->type ) {
118 if( b->strMember ) {
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;
136 return begin;
140 // handle special cases
141 char swallow;
142 switch( field->type )
144 case MFC_RECORDID:
145 MessageRecordId = btohl(field->u.uint32);
146 return begin;
147 case MFC_REPLY_UNKNOWN:
148 swallow = field->u.raw[0];
149 return begin;
151 // if still not handled, add to the Unknowns list
152 UnknownField uf;
153 uf.type = field->type;
154 uf.data.assign((const char*)field->u.raw, btohs(field->size));
155 Unknowns.push_back(uf);
157 return begin;
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 Protocol::CheckSize(data, offset + MESSAGE_RECORD_HEADER_SIZE);
175 MAKE_RECORD(const Barry::Protocol::MessageRecord, mr, data, offset);
177 // Priority
178 MessagePriority = NormalPriority;
180 uint16_t priority = btohs(mr->priority); // deal with endian swap once
181 if( priority & PRIORITY_MASK ) {
182 if( priority & PRIORITY_HIGH ) {
183 MessagePriority = HighPriority;
185 else if( priority & PRIORITY_LOW ) {
186 MessagePriority = LowPriority;
188 else
189 MessagePriority = UnknownPriority;
191 // Sensitivity
192 MessageSensitivity = NormalSensitivity;
193 if( priority & SENSITIVE_MASK ) {
194 if(( priority & SENSITIVE_CONFIDENTIAL ) == SENSITIVE_CONFIDENTIAL ) {
195 MessageSensitivity = Confidential;
197 else if(( priority & SENSITIVE_PRIVATE ) == SENSITIVE_PRIVATE ) {
198 MessageSensitivity = Private;
200 else if(( priority & SENSITIVE_PERSONAL ) == SENSITIVE_PERSONAL ) {
201 MessageSensitivity = Personal;
203 else
204 MessageSensitivity = UnknownSensitivity;
206 // X-rim-org-message-ref-id // NOTE: I'm cheating a bit here and using this as a reply-to
207 if( mr->inReplyTo ) // It's actually sent by BB with the actual UID in every message
208 MessageReplyTo = btohl(mr->inReplyTo);
210 // Status Flags
211 uint32_t flags = btohl(mr->flags);
212 if( !( flags & MESSAGE_READ ))
213 MessageRead = true; // NOTE: A lot of these flags are 'backwards' but this seemed
214 // like the most logical way to interpret them for now
215 if(( flags & MESSAGE_REPLY ) == MESSAGE_REPLY )
216 MessageReply = true; // NOTE: This is a reply, the original message's flags are not changed
217 // the inReplyTo field is updated with the original messages's UID
218 if( !( flags & MESSAGE_TRUNCATED ))
219 MessageTruncated = true; // NOTE: This bit is unset on truncation, around 4096 on my 7100g
220 // NOTE: bit 0x400 is set on REALLY huge messages, haven't tested
221 // the exact size yet
222 if( !( flags & MESSAGE_SAVED ))
223 MessageSaved = true; // NOTE: Saved to 'saved' folder
224 if( !( flags & MESSAGE_SAVED_DELETED ))
225 MessageSavedDeleted = true; // NOTE: Saved to 'saved' folder and then deleted from inbox
227 MessageDateSent = Message2Time(mr->dateSent, mr->timeSent);
228 MessageDateReceived = Message2Time(mr->dateReceived, mr->timeReceived);
230 offset += MESSAGE_RECORD_HEADER_SIZE;
233 void Message::ParseFields(const Data &data, size_t &offset)
235 const unsigned char *finish = ParseCommonFields(*this,
236 data.GetData() + offset, data.GetData() + data.GetSize());
237 offset += finish - (data.GetData() + offset);
240 void Message::BuildHeader(Data &data, size_t &offset) const
242 throw std::logic_error("Message::BuildHeader not yet implemented");
245 void Message::BuildFields(Data &data, size_t &offset) const
247 throw std::logic_error("Message::BuildFields not yet implemented");
250 void Message::Clear()
252 From.clear();
253 To.clear();
254 Cc.clear();
255 Bcc.clear();
256 Sender.clear();
257 ReplyTo.clear();
258 Subject.clear();
259 Body.clear();
260 Attachment.clear();
262 MessageRecordId = 0;
263 MessageReplyTo = 0;
264 MessageDateSent = 0;
265 MessageDateReceived = 0;
266 MessageTruncated = false;
267 MessageRead = false;
268 MessageReply = false;
269 MessageSaved = false;
270 MessageSavedDeleted = false;
272 Unknowns.clear();
275 std::string Message::SimpleEmailAddress() const
277 if( From.Email.size() ) {
278 // remove all spaces from the email
279 std::string ret;
280 for( size_t i = 0; i < From.Email.size(); i++ )
281 if( From.Email[i] != ' ' )
282 ret += From.Email[i];
284 return ret;
286 else {
287 return "unknown";
291 // dump message in mbox format
292 void Message::Dump(std::ostream &os) const
294 static const char *MessageImportance[] =
295 { "Low", "Normal", "High", "Unknown Priority" };
296 static const char *MessageSensitivityString[] =
297 { "Normal", "Personal", "Private", "Confidential", "Unknown Sensivity" };
299 os << "From " << SimpleEmailAddress() << " " << ctime( &MessageDateReceived );
300 os << "X-Record-ID: (" << setw(8) << std::hex << MessageRecordId << ")\n";
301 if( MessageReplyTo )
302 os << "X-rim-org-msg-ref-id: " << std::dec << MessageReplyTo << "\n";
303 if( MessageSaved )
304 os << "X-Message-Status: Saved\n";
305 else if( MessageRead )
306 os << "Message Status: Opened\n";
307 if( MessagePriority != NormalPriority )
308 os << "Importance: " << MessageImportance[MessagePriority] << "\n";
309 if( MessageSensitivity != NormalSensitivity )
310 os << "Sensitivity: " << MessageSensitivityString[MessageSensitivity] << "\n";
311 os << "Date: " << ctime(&MessageDateSent);
312 os << "From: " << From << "\n";
313 if( To.Email.size() )
314 os << "To: " << To << "\n";
315 if( Cc.Email.size() )
316 os << "Cc: " << Cc << "\n";
317 if( Bcc.Email.size() )
318 os << "Bcc: " << Bcc << "\n";
319 if( Sender.Email.size() )
320 os << "Sender: " << Sender << "\n";
321 if( ReplyTo.Email.size())
322 os << "Reply To: " << ReplyTo << "\n";
323 if( Subject.size() )
324 os << "Subject: " << Subject << "\n";
325 os << "\n";
326 for( std::string::const_iterator i = Body.begin();
327 i != Body.end() && *i;
328 i++)
330 if( *i == '\r' )
331 os << '\n';
332 else
333 os << *i;
335 os << "\n";
336 if( Attachment.size() )
337 os << "Attachments: " << Attachment << "\n";
339 os << Unknowns;
340 os << "\n\n";
344 } // namespace Barry