lib: Moving default Barry::Data size into constants.
[barry.git] / src / r_sms.cc
blobd0e4d197007dd2e4cb20bd4930b01ed8d90f0ff0
1 ///
2 /// \file r_sms.cc
3 /// Record parsing class for the SMS database.
4 ///
6 /*
7 Copyright (C) 2005-2012, Net Direct Inc. (http://www.netdirect.ca/)
8 Copyright (C) 2009, Ryan Li(ryan@ryanium.com)
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_sms.h"
24 #include "record-internal.h"
25 #include "protostructs.h"
26 #include "data.h"
27 #include "time.h"
28 #include "debug.h"
29 #include "iconv.h"
30 #include "strnlen.h"
31 #include <ostream>
32 #include <iomanip>
33 #include <string.h>
34 #include "ios_state.h"
36 using namespace std;
37 using namespace Barry::Protocol;
39 namespace Barry {
41 ///////////////////////////////////////////////////////////////////////////////
42 // Sms Class
44 // SMS Field Codes
45 #define SMSFC_METADATA 0x01
46 #define SMSFC_ADDRESS 0x02
47 #define SMSFC_BODY 0x04
49 // SMS Field Sizes and Header Sizes
50 #define SMS_ADDRESS_HEADER_SIZE 0x04
52 #define MILLISECONDS_IN_A_SECOND 1000
54 time_t Sms::GetTime() const
56 return (time_t)(Timestamp / MILLISECONDS_IN_A_SECOND);
59 time_t Sms::GetServiceCenterTime() const
61 return (time_t)(ServiceCenterTimestamp / MILLISECONDS_IN_A_SECOND);
64 void Sms::SetTime(const time_t timestamp, const unsigned milliseconds)
66 Timestamp = (uint64_t)timestamp * MILLISECONDS_IN_A_SECOND + milliseconds;
69 void Sms::SetServiceCenterTime(const time_t timestamp, const unsigned milliseconds)
71 ServiceCenterTimestamp = (uint64_t)timestamp * MILLISECONDS_IN_A_SECOND + milliseconds;
74 Sms::Sms()
76 Clear();
79 Sms::~Sms()
83 #define CHECK_FLAG(flags, bitfield) ((flags & (bitfield)) != 0)
85 const unsigned char* Sms::ParseField(const unsigned char *begin,
86 const unsigned char *end,
87 const IConverter *ic)
89 const CommonField *field = (const CommonField *)begin;
91 // advance and check size
92 begin += COMMON_FIELD_HEADER_SIZE + btohs(field->size);
93 if (begin > end) // if begin==end, we are ok
94 return begin;
96 if (!btohs(field->size)) // if field has no size, something's up
97 return begin;
99 switch (field->type)
101 case SMSFC_METADATA:
103 if (btohs(field->size) < SMS_METADATA_SIZE)
104 break; // size not match
106 const SMSMetaData &metadata = field->u.sms_metadata;
107 NewConversation = CHECK_FLAG(metadata.flags, SMS_FLG_NEW_CONVERSATION);
108 Saved = CHECK_FLAG(metadata.flags, SMS_FLG_SAVED);
109 Deleted = CHECK_FLAG(metadata.flags, SMS_FLG_DELETED);
110 Opened = CHECK_FLAG(metadata.flags, SMS_FLG_OPENED);
112 IsNew = metadata.new_flag != 0;
114 uint32_t status = btohl(metadata.status);
116 switch (status)
118 case SMS_STA_RECEIVED:
119 MessageStatus = Received;
120 break;
121 case SMS_STA_DRAFT:
122 MessageStatus = Draft;
123 break;
124 default:
125 MessageStatus = Sent; // consider all others as sent
128 ErrorId = btohl(metadata.error_id);
130 Timestamp = btohll(metadata.timestamp);
131 ServiceCenterTimestamp = btohll(metadata.service_center_timestamp);
133 switch (metadata.dcs)
135 case SMS_DCS_7BIT:
136 DataCodingScheme = SevenBit;
137 break;
138 case SMS_DCS_8BIT:
139 DataCodingScheme = EightBit;
140 break;
141 case SMS_DCS_UCS2:
142 DataCodingScheme = UCS2;
143 break;
144 default:
145 DataCodingScheme = SevenBit; // consider all unknowns as 7bit
148 return begin;
151 case SMSFC_ADDRESS:
153 uint16_t length = btohs(field->size);
154 if (length < SMS_ADDRESS_HEADER_SIZE + 1) // trailing '\0'
155 break; // too short
157 length -= SMS_ADDRESS_HEADER_SIZE;
158 const char *address = (const char *)field->u.raw + SMS_ADDRESS_HEADER_SIZE;
159 Addresses.push_back(std::string(address, strnlen(address, length)));
160 return begin;
163 case SMSFC_BODY:
166 // Some SMS bodies contain a null terminator
167 // in the middle, and it is unknown at the moment
168 // why this is. For regular 8bit char strings,
169 // we just strip out the nulls. For UCS2
170 // 16bit char strings, we strip out the
171 // 16bit nulls.
173 // Any further information on why these null
174 // terminators appear is welcome.
176 const char *str = (const char *)field->u.raw;
177 uint16_t maxlen = btohs(field->size);
178 if (DataCodingScheme != UCS2) {
179 for (uint16_t i = 0; i < maxlen; ++i) {
180 if (str[i]) // if not null, push it
181 Body += str[i];
184 else {
185 for (uint16_t i = 0; maxlen && i < (maxlen-1); i += 2) {
186 if (str[i] || str[i + 1]) // if not null, push it
187 Body += std::string(str + i, 2);
190 if (ic) {
191 if (DataCodingScheme == SevenBit) {
192 // SevenBit -> UTF-8 -> ic's tocode
193 IConvHandle utf8("UTF-8", *ic);
194 Body = ic->Convert(utf8, ConvertGsmToUtf8(Body)); // convert the Body string from GSM 03.38 defined to UTF-8
196 else if (DataCodingScheme == EightBit)
197 Body = ic->FromBB(Body);
198 else {
199 IConvHandle ucs2("UCS-2BE", *ic);
200 Body = ic->Convert(ucs2, Body);
203 return begin;
207 // if still not handled, add to the Unknowns list
208 UnknownField uf;
209 uf.type = field->type;
210 uf.data.assign((const char*)field->u.raw, btohs(field->size));
211 Unknowns.push_back(uf);
213 // return new pointer for next field
214 return begin;
217 void Sms::ParseHeader(const Data &data, size_t &offset)
219 // no header in SMS records
222 void Sms::ParseFields(const Data &data, size_t &offset, const IConverter *ic)
224 const unsigned char *finish = ParseCommonFields(*this,
225 data.GetData() + offset, data.GetData() + data.GetSize(), ic);
226 offset += finish - (data.GetData() + offset);
229 void Sms::Validate() const
233 void Sms::BuildHeader(Data &data, size_t &offset) const
235 // not yet implemented
238 void Sms::BuildFields(Data &data, size_t &offset, const IConverter *ic) const
240 // not yet implemented
243 void Sms::Clear()
245 RecType = GetDefaultRecType();
246 RecordId = 0;
248 MessageStatus = Unknown;
249 DeliveryStatus = NoReport;
251 IsNew = NewConversation = Saved = Deleted = Opened = false;
253 Timestamp = ServiceCenterTimestamp = 0;
255 DataCodingScheme = SevenBit;
257 ErrorId = 0;
259 Addresses.clear();
260 Body.clear();
262 Unknowns.clear();
265 const FieldHandle<Sms>::ListT& Sms::GetFieldHandles()
267 static FieldHandle<Sms>::ListT fhv;
269 if( fhv.size() )
270 return fhv;
272 #undef CONTAINER_OBJECT_NAME
273 #define CONTAINER_OBJECT_NAME fhv
275 #undef RECORD_CLASS_NAME
276 #define RECORD_CLASS_NAME Sms
278 FHP(RecType, "Record Type Code");
279 FHP(RecordId, "Unique Record ID");
281 FHE(mt, MessageType, MessageStatus, "Message Status");
282 FHE_CONST(mt, Unknown, "Unknown");
283 FHE_CONST(mt, Received, "Received");
284 FHE_CONST(mt, Sent, "Sent");
285 FHE_CONST(mt, Draft, "Draft");
287 FHE(dt, DeliveryType, DeliveryStatus, "Delivery Status");
288 FHE_CONST(dt, NoReport, "No Report");
289 FHE_CONST(dt, Failed, "Failed");
290 FHE_CONST(dt, Succeeded, "Succeeded");
292 FHP(IsNew, "Is New?");
293 FHP(NewConversation, "New Conversation");
294 FHP(Saved, "Saved");
295 FHP(Deleted, "Deleted");
296 FHP(Opened, "Opened");
298 FHP(Timestamp, "Timestamp in Milliseconds");
299 FHP(ServiceCenterTimestamp, "Service Center Timestamp");
301 FHE(dcst, DataCodingSchemeType, DataCodingScheme, "Data Coding Scheme");
302 FHE_CONST(dcst, SevenBit, "7bit");
303 FHE_CONST(dcst, EightBit, "8bit");
304 FHE_CONST(dcst, UCS2, "UCS2");
306 FHP(ErrorId, "Error ID");
308 FHD(Addresses, "Addresses", SMSFC_ADDRESS, true);
309 FHD(Body, "Body", SMSFC_BODY, true);
311 FHP(Unknowns, "Unknown Fields");
313 return fhv;
316 std::string Sms::GetDescription() const
318 if( Addresses.size() )
319 return Addresses[0];
320 else
321 return "Unknown destination";
324 void Sms::Dump(std::ostream &os) const
326 ios_format_state state(os);
328 os << "SMS record: 0x" << setbase(16) << RecordId
329 << " (" << (unsigned int)RecType << ")\n";
330 time_t t = GetTime();
331 os << "\tTimestamp: " << ctime(&t);
333 if (MessageStatus == Received) {
334 t = GetServiceCenterTime();
335 os << "\tService Center Timestamp: " << ctime(&t);
338 if (ErrorId)
339 os << "\tSend Error: 0x" << setbase(16) << ErrorId << "\n";
341 switch (MessageStatus)
343 case Received:
344 os << "\tReceived From:\n";
345 break;
346 case Sent:
347 os << "\tSent to:\n";
348 break;
349 case Draft:
350 os << "\tDraft for:\n";
351 break;
352 case Unknown:
353 os << "\tUnknown status for:\n";
354 break;
357 os << "\t";
358 os << Addresses << "\n";
360 if (IsNew || Opened || Saved || Deleted || NewConversation) {
361 os << "\t";
362 if (IsNew)
363 os << "New ";
364 if (Opened)
365 os << "Opened ";
366 if (Saved)
367 os << "Saved ";
368 if (Deleted)
369 os << "Deleted ";
370 os << "Message" << (NewConversation ? " that starts a new conversation" : "") << "\n";
372 os << "\tContent: " << Body << "\n";
373 os << "\n";
377 // This function helps to convert GSM 03.38 defined 7-bit
378 // SMS to UTF-8.
379 // Detailed information can be found in:
380 // ftp://ftp.3gpp.org/Specs/html-info/0338.htm (Official)
381 // http://en.wikipedia.org/wiki/SMS#GSM
384 std::string Sms::ConvertGsmToUtf8(const std::string &s)
387 // This array stores the GSM 03.38 defined encoding's
388 // corresponding UTF-8 values.
389 // For example: GsmTable[0] = "\x40", which refers to
390 // a "@" in UTF-8 encoding.
391 // The 0x1b item, leads to the extension table, using
392 // the char right next to it as the index.
393 // According to the official specification, when not
394 // able to handle it, it should be treated simply as
395 // a space, which is denoted in UTF-8 as "\x20".
397 static const std::string GsmTable[0x80] = {
398 // 0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7
399 "\x40", "\xc2\xa3", "\x24", "\xc2\xa5", "\xc3\xa8", "\xc3\xa9", "\xc3\xb9", "\xc3\xac", // 0x00
400 "\xc3\xb2", "\xc3\x87", "\x0a", "\xc3\x98", "\xc3\xb8", "\x0d", "\xc3\x85", "\xc3\xa5", // 0x08
401 "\xce\x94", "\x5f", "\xce\xa6", "\xce\x93", "\xce\x9b", "\xce\xa9", "\xce\xa0", "\xce\xa8", // 0x10
402 "\xce\xa3", "\xce\x98", "\xce\x9e", "\x20", "\xc3\x86", "\xc3\xa6", "\xc3\x9f", "\xc3\x89", // 0x18
403 "\x20", "\x21", "\x22", "\x23", "\xc2\xa4", "\x25", "\x26", "\x27", // 0x20
404 "\x28", "\x29", "\x2a", "\x2b", "\x2c", "\x2d", "\x2e", "\x2f", // 0x28
405 "\x30", "\x31", "\x32", "\x33", "\x34", "\x35", "\x36", "\x37", // 0x30
406 "\x38", "\x39", "\x3a", "\x3b", "\x3c", "\x3d", "\x3e", "\x3f", // 0x38
407 "\xc2\xa1", "\x41", "\x42", "\x43", "\x44", "\x45", "\x46", "\x47", // 0x40
408 "\x48", "\x49", "\x4a", "\x4b", "\x4c", "\x4d", "\x4e", "\x4f", // 0x48
409 "\x50", "\x51", "\x52", "\x53", "\x54", "\x55", "\x56", "\x57", // 0x50
410 "\x58", "\x59", "\x5a", "\xc3\x84", "\xc3\x96", "\xc3\x91", "\xc3\x9c", "\xc2\xa7", // 0x58
411 "\xc2\xbf", "\x61", "\x62", "\x63", "\x64", "\x65", "\x66", "\x67", // 0x60
412 "\x68", "\x69", "\x6a", "\x6b", "\x6c", "\x6d", "\x6e", "\x6f", // 0x68
413 "\x70", "\x71", "\x72", "\x73", "\x74", "\x75", "\x76", "\x77", // 0x70
414 "\x78", "\x79", "\x7a", "\xc3\xa4", "\xc3\xb6", "\xc3\xb1", "\xc3\xbc", "\xc3\xa0" // 0x78
417 // This sparse array stores the GSM 03.38 defined
418 // encoding extension table.
419 // The \x1b item is also preserved, for possibly
420 // another extension table.
422 static const std::string GsmExtensionTable[0x80] = {
423 // 0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7
424 "", "", "", "", "", "", "", "", // 0x00
425 "", "", "\x0c", "", "", "", "", "", // 0x08
426 "", "", "", "", "\x5e", "", "", "", // 0x10
427 "", "", "", " ", "", "", "", "", // 0x18
428 "", "", "", "", "", "", "", "", // 0x20
429 "\x7b", "\x7d", "", "", "", "", "", "\x5c", // 0x28
430 "", "", "", "", "", "", "", "", // 0x30
431 "", "", "", "", "\x5b", "\x7e", "\x5d", "", // 0x38
432 "\x7c", "", "", "", "", "", "", "", // 0x40
433 "", "", "", "", "", "", "", "", // 0x48
434 "", "", "", "", "", "", "", "", // 0x50
435 "", "", "", "", "", "", "", "", // 0x58
436 "", "", "", "", "", "\xe2\x82\xac", "", "", // 0x60
437 "", "", "", "", "", "", "", "", // 0x68
438 "", "", "", "", "", "", "", "", // 0x70
439 "", "", "", "", "", "", "", "" // 0x78
441 std::string ret;
442 unsigned len = s.length();
443 for (unsigned i = 0; i < len; ++i) {
444 unsigned char c = (unsigned char) s[i];
445 if (c > 0x7f) // prevent from illegal index
446 continue;
447 else if (c == 0x1b) { // go to extension table
448 if (i < len - 1) {
449 c = (unsigned char) s[++i];
450 if (c <= 0x7f) // prevent from illegal index
451 ret += GsmExtensionTable[c];
454 else
455 ret += GsmTable[c];
457 return ret;
460 } // namespace Barry