1 |
|
2 |
#include <string> |
3 |
#include <vector> |
4 |
|
5 |
#include "Sms.h" |
6 |
#include "util.h" |
7 |
#include "SmsPdu.h" |
8 |
|
9 |
|
10 |
|
11 |
std::string GetSmsCommand(SMS& sms) |
12 |
{ |
13 |
unsigned int pos = sms.message.find(" "); |
14 |
|
15 |
if (pos == std::string::npos) |
16 |
return sms.message; |
17 |
else |
18 |
return sms.message.substr(0, pos); |
19 |
} |
20 |
|
21 |
std::string GetSmsData(SMS& sms) |
22 |
{ |
23 |
unsigned int pos = sms.message.find(" "); |
24 |
|
25 |
if (pos == std::string::npos) |
26 |
return ""; |
27 |
else |
28 |
{ |
29 |
pos++; |
30 |
|
31 |
std::string data = sms.message.substr(pos, sms.message.length()-pos ); |
32 |
|
33 |
data = Util::str_trim(data); |
34 |
return data; |
35 |
} |
36 |
} |
37 |
|
38 |
SMS::SMS() |
39 |
{ |
40 |
} |
41 |
|
42 |
SMS::SMS(std::string _sender, std::string _message, std::string _timestamp) |
43 |
: sender(_sender), message(_message), timestamp(_timestamp) |
44 |
{ |
45 |
} |
46 |
|
47 |
SMS SMS::FromRawString(const std::string& input) |
48 |
{ |
49 |
std::string smsline = input.substr(7, input.length() -7); //strip "+CMGL: " |
50 |
|
51 |
std::vector<std::string> lines = Util::str_split(smsline, "\r\n"); |
52 |
std::vector<std::string> fields = Util::str_split(lines[0],","); |
53 |
|
54 |
std::string body; |
55 |
for (unsigned i=1; i<lines.size(); ++i) |
56 |
{ |
57 |
std::string body_line = lines[i]; |
58 |
|
59 |
|
60 |
if (body_line != "") |
61 |
{ |
62 |
if (body.length() > 0) |
63 |
body += "\r\n"; |
64 |
body += body_line; |
65 |
} |
66 |
} |
67 |
|
68 |
body = Util::str_trim(body); |
69 |
|
70 |
SMS newsms; |
71 |
|
72 |
newsms.sms_index = fields[0]; |
73 |
|
74 |
newsms.sender = fields[2]; |
75 |
newsms.sender = Util::str_replace(newsms.sender, "\""); |
76 |
|
77 |
newsms.timestamp = fields[4] + std::string(",") + fields[5]; |
78 |
newsms.timestamp = Util::str_replace(newsms.timestamp, "\""); |
79 |
|
80 |
newsms.message = body; |
81 |
|
82 |
return newsms; |
83 |
} |
84 |
|
85 |
SMS SMS::FromPduString(const std::string& input) |
86 |
{ |
87 |
std::string smsline = input.substr(7, input.length() -7); //strip "+CMGL: " |
88 |
|
89 |
std::vector<std::string> lines = Util::str_split(smsline, "\r\n"); |
90 |
std::vector<std::string> fields = Util::str_split(lines[0],","); |
91 |
|
92 |
SMS newsms = SmsPdu::ParseSmsPdu(lines[1]); |
93 |
|
94 |
newsms.sms_index = fields[0]; |
95 |
|
96 |
return newsms; |
97 |
} |