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