debian: bumped opensync 0.4x plugin to compat 6 (with similar fixes)
[barry/progweb.git] / src / r_sms.cc
blobdda7d84eb015d2285ee5c7a6bbb00ec07b54b2c2
1 ///
2 /// \file r_sms.cc
3 /// Record parsing class for the SMS database.
4 ///
6 /*
7 Copyright (C) 2005-2011, 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 const unsigned char* Sms::ParseField(const unsigned char *begin,
84 const unsigned char *end,
85 const IConverter *ic)
87 const CommonField *field = (const CommonField *)begin;
89 // advance and check size
90 begin += COMMON_FIELD_HEADER_SIZE + btohs(field->size);
91 if (begin > end) // if begin==end, we are ok
92 return begin;
94 if (!btohs(field->size)) // if field has no size, something's up
95 return begin;
97 switch (field->type)
99 case SMSFC_METADATA:
101 if (btohs(field->size) < SMS_METADATA_SIZE)
102 break; // size not match
104 const SMSMetaData &metadata = field->u.sms_metadata;
105 NewConversation = metadata.flags & SMS_FLG_NEW_CONVERSATION;
106 Saved = metadata.flags & SMS_FLG_SAVED;
107 Deleted = metadata.flags & SMS_FLG_DELETED;
108 Opened = metadata.flags & SMS_FLG_OPENED;
110 IsNew = metadata.new_flag;
112 uint32_t status = btohl(metadata.status);
114 switch (status)
116 case SMS_STA_RECEIVED:
117 MessageStatus = Received;
118 break;
119 case SMS_STA_DRAFT:
120 MessageStatus = Draft;
121 break;
122 default:
123 MessageStatus = Sent; // consider all others as sent
126 ErrorId = btohl(metadata.error_id);
128 Timestamp = btohll(metadata.timestamp);
129 ServiceCenterTimestamp = btohll(metadata.service_center_timestamp);
131 switch (metadata.dcs)
133 case SMS_DCS_7BIT:
134 DataCodingScheme = SevenBit;
135 break;
136 case SMS_DCS_8BIT:
137 DataCodingScheme = EightBit;
138 break;
139 case SMS_DCS_UCS2:
140 DataCodingScheme = UCS2;
141 break;
142 default:
143 DataCodingScheme = SevenBit; // consider all unknowns as 7bit
146 return begin;
149 case SMSFC_ADDRESS:
151 uint16_t length = btohs(field->size);
152 if (length < SMS_ADDRESS_HEADER_SIZE + 1) // trailing '\0'
153 break; // too short
155 length -= SMS_ADDRESS_HEADER_SIZE;
156 const char *address = (const char *)field->u.raw + SMS_ADDRESS_HEADER_SIZE;
157 Addresses.push_back(std::string(address, strnlen(address, length)));
158 return begin;
161 case SMSFC_BODY:
164 // Some SMS bodies contain a null terminator
165 // in the middle, and it is unknown at the moment
166 // why this is. For regular 8bit char strings,
167 // we just strip out the nulls. For UCS2
168 // 16bit char strings, we strip out the
169 // 16bit nulls.
171 // Any further information on why these null
172 // terminators appear is welcome.
174 const char *str = (const char *)field->u.raw;
175 uint16_t maxlen = btohs(field->size);
176 if (DataCodingScheme != UCS2) {
177 for (uint16_t i = 0; i < maxlen; ++i) {
178 if (str[i]) // if not null, push it
179 Body += str[i];
182 else {
183 for (uint16_t i = 0; maxlen && i < (maxlen-1); i += 2) {
184 if (str[i] || str[i + 1]) // if not null, push it
185 Body += std::string(str + i, 2);
188 if (ic) {
189 if (DataCodingScheme == SevenBit) {
190 // SevenBit -> UTF-8 -> ic's tocode
191 IConvHandle utf8("UTF-8", *ic);
192 Body = ic->Convert(utf8, ConvertGsmToUtf8(Body)); // convert the Body string from GSM 03.38 defined to UTF-8
194 else if (DataCodingScheme == EightBit)
195 Body = ic->FromBB(Body);
196 else {
197 IConvHandle ucs2("UCS-2BE", *ic);
198 Body = ic->Convert(ucs2, Body);
201 return begin;
205 // if still not handled, add to the Unknowns list
206 UnknownField uf;
207 uf.type = field->type;
208 uf.data.assign((const char*)field->u.raw, btohs(field->size));
209 Unknowns.push_back(uf);
211 // return new pointer for next field
212 return begin;
215 void Sms::ParseHeader(const Data &data, size_t &offset)
217 // no header in SMS records
220 void Sms::ParseFields(const Data &data, size_t &offset, const IConverter *ic)
222 const unsigned char *finish = ParseCommonFields(*this,
223 data.GetData() + offset, data.GetData() + data.GetSize(), ic);
224 offset += finish - (data.GetData() + offset);
227 void Sms::BuildHeader(Data &data, size_t &offset) const
229 // not yet implemented
232 void Sms::BuildFields(Data &data, size_t &offset, const IConverter *ic) const
234 // not yet implemented
237 void Sms::Clear()
239 RecType = GetDefaultRecType();
240 RecordId = 0;
242 MessageStatus = Unknown;
243 DeliveryStatus = NoReport;
245 IsNew = NewConversation = Saved = Deleted = Opened = false;
247 Timestamp = ServiceCenterTimestamp = 0;
249 DataCodingScheme = SevenBit;
251 ErrorId = 0;
253 Addresses.clear();
254 Body.clear();
256 Unknowns.clear();
259 std::string Sms::GetDescription() const
261 if( Addresses.size() )
262 return Addresses[0];
263 else
264 return "Unknown destination";
267 void Sms::Dump(std::ostream &os) const
269 ios_format_state state(os);
271 os << "SMS record: 0x" << setbase(16) << RecordId
272 << " (" << (unsigned int)RecType << ")\n";
273 time_t t = GetTime();
274 os << "\tTimestamp: " << ctime(&t);
276 if (MessageStatus == Received) {
277 t = GetServiceCenterTime();
278 os << "\tService Center Timestamp: " << ctime(&t);
281 if (ErrorId)
282 os << "\tSend Error: 0x" << setbase(16) << ErrorId << "\n";
284 switch (MessageStatus)
286 case Received:
287 os << "\tReceived From:\n";
288 break;
289 case Sent:
290 os << "\tSent to:\n";
291 break;
292 case Draft:
293 os << "\tDraft for:\n";
294 break;
295 case Unknown:
296 os << "\tUnknown status for:\n";
297 break;
300 os << "\t";
301 for (std::vector<std::string>::const_iterator i = Addresses.begin(); i < Addresses.end(); ++i) {
302 if (i != Addresses.begin())
303 os << ", ";
304 os << *i;
306 os << "\n";
308 if (IsNew || Opened || Saved || Deleted || NewConversation) {
309 os << "\t";
310 if (IsNew)
311 os << "New ";
312 if (Opened)
313 os << "Opened ";
314 if (Saved)
315 os << "Saved ";
316 if (Deleted)
317 os << "Deleted ";
318 os << "Message" << (NewConversation ? " that starts a new conversation" : "") << "\n";
320 os << "\tContent: " << Body << "\n";
321 os << "\n";
325 // This function helps to convert GSM 03.38 defined 7-bit
326 // SMS to UTF-8.
327 // Detailed information can be found in:
328 // ftp://ftp.3gpp.org/Specs/html-info/0338.htm (Official)
329 // http://en.wikipedia.org/wiki/SMS#GSM
332 std::string Sms::ConvertGsmToUtf8(const std::string &s)
335 // This array stores the GSM 03.38 defined encoding's
336 // corresponding UTF-8 values.
337 // For example: GsmTable[0] = "\x40", which refers to
338 // a "@" in UTF-8 encoding.
339 // The 0x1b item, leads to the extension table, using
340 // the char right next to it as the index.
341 // According to the official specification, when not
342 // able to handle it, it should be treated simply as
343 // a space, which is denoted in UTF-8 as "\x20".
345 static const std::string GsmTable[0x80] = {
346 // 0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7
347 "\x40", "\xc2\xa3", "\x24", "\xc2\xa5", "\xc3\xa8", "\xc3\xa9", "\xc3\xb9", "\xc3\xac", // 0x00
348 "\xc3\xb2", "\xc3\x87", "\x0a", "\xc3\x98", "\xc3\xb8", "\x0d", "\xc3\x85", "\xc3\xa5", // 0x08
349 "\xce\x94", "\x5f", "\xce\xa6", "\xce\x93", "\xce\x9b", "\xce\xa9", "\xce\xa0", "\xce\xa8", // 0x10
350 "\xce\xa3", "\xce\x98", "\xce\x9e", "\x20", "\xc3\x86", "\xc3\xa6", "\xc3\x9f", "\xc3\x89", // 0x18
351 "\x20", "\x21", "\x22", "\x23", "\xc2\xa4", "\x25", "\x26", "\x27", // 0x20
352 "\x28", "\x29", "\x2a", "\x2b", "\x2c", "\x2d", "\x2e", "\x2f", // 0x28
353 "\x30", "\x31", "\x32", "\x33", "\x34", "\x35", "\x36", "\x37", // 0x30
354 "\x38", "\x39", "\x3a", "\x3b", "\x3c", "\x3d", "\x3e", "\x3f", // 0x38
355 "\xc2\xa1", "\x41", "\x42", "\x43", "\x44", "\x45", "\x46", "\x47", // 0x40
356 "\x48", "\x49", "\x4a", "\x4b", "\x4c", "\x4d", "\x4e", "\x4f", // 0x48
357 "\x50", "\x51", "\x52", "\x53", "\x54", "\x55", "\x56", "\x57", // 0x50
358 "\x58", "\x59", "\x5a", "\xc3\x84", "\xc3\x96", "\xc3\x91", "\xc3\x9c", "\xc2\xa7", // 0x58
359 "\xc2\xbf", "\x61", "\x62", "\x63", "\x64", "\x65", "\x66", "\x67", // 0x60
360 "\x68", "\x69", "\x6a", "\x6b", "\x6c", "\x6d", "\x6e", "\x6f", // 0x68
361 "\x70", "\x71", "\x72", "\x73", "\x74", "\x75", "\x76", "\x77", // 0x70
362 "\x78", "\x79", "\x7a", "\xc3\xa4", "\xc3\xb6", "\xc3\xb1", "\xc3\xbc", "\xc3\xa0" // 0x78
365 // This sparse array stores the GSM 03.38 defined
366 // encoding extension table.
367 // The \x1b item is also preserved, for possibly
368 // another extension table.
370 static const std::string GsmExtensionTable[0x80] = {
371 // 0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7
372 "", "", "", "", "", "", "", "", // 0x00
373 "", "", "\x0c", "", "", "", "", "", // 0x08
374 "", "", "", "", "\x5e", "", "", "", // 0x10
375 "", "", "", " ", "", "", "", "", // 0x18
376 "", "", "", "", "", "", "", "", // 0x20
377 "\x7b", "\x7d", "", "", "", "", "", "\x5c", // 0x28
378 "", "", "", "", "", "", "", "", // 0x30
379 "", "", "", "", "\x5b", "\x7e", "\x5d", "", // 0x38
380 "\x7c", "", "", "", "", "", "", "", // 0x40
381 "", "", "", "", "", "", "", "", // 0x48
382 "", "", "", "", "", "", "", "", // 0x50
383 "", "", "", "", "", "", "", "", // 0x58
384 "", "", "", "", "", "\xe2\x82\xac", "", "", // 0x60
385 "", "", "", "", "", "", "", "", // 0x68
386 "", "", "", "", "", "", "", "", // 0x70
387 "", "", "", "", "", "", "", "" // 0x78
389 std::string ret;
390 unsigned len = s.length();
391 for (unsigned i = 0; i < len; ++i) {
392 unsigned char c = (unsigned char) s[i];
393 if (c > 0x7f) // prevent from illegal index
394 continue;
395 else if (c == 0x1b) { // go to extension table
396 if (i < len - 1) {
397 c = (unsigned char) s[++i];
398 if (c <= 0x7f) // prevent from illegal index
399 ret += GsmExtensionTable[c];
402 else
403 ret += GsmTable[c];
405 return ret;
408 } // namespace Barry