-
Notifications
You must be signed in to change notification settings - Fork 208
/
message.cc
303 lines (243 loc) · 8.87 KB
/
message.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
#include "message.h"
#include "wacommon.h"
#include "wa_connection.h"
#include "tree.h"
#include "wa_util.h"
using namespace wapurple;
std::string basename(std::string s) {
while (s.find("/") != std::string::npos)
s = s.substr(s.find("/")+1);
return s;
}
Message::Message(const WhatsappConnection * wc, const std::string from, const unsigned long long time, const std::string id, const std::string author)
{
size_t pos = from.find('@');
if (pos != std::string::npos) {
this->from = from.substr(0, pos);
this->server = from.substr(pos + 1);
} else
this->from = from;
this->t = time;
this->wc = const_cast < WhatsappConnection * >(wc);
this->id = id;
this->author = getusername(author);
this->retries = 0;
this->axolotl = true;
}
MediaMessage::MediaMessage(const WhatsappConnection * wc, const std::string from, const unsigned long long time,
const std::string id, const std::string author, const std::string url, const std::string caption,
const std::string ip, const std::string hash, const std::string filetype)
: Message(wc, from, time, id, author),
url(url), caption(caption), hash(hash), filetype(filetype), ip(ip)
{
}
ChatMessage::ChatMessage(const WhatsappConnection * wc, const std::string from, const unsigned long long time,
const std::string id, const std::string message, const std::string author) :
Message(wc, from, time, id, author)
{
this->message = message;
this->msg_body = "body";
}
DataBuffer ChatMessage::serialize() const
{
Tree tbody(this->msg_body);
tbody.setData(this->message);
if (this->ctype != "") {
tbody.setAttributes(makeat({"type", this->ctype, "v", "2", "count", "1"}));
}
std::string stime = std::to_string(t);
std::map < std::string, std::string > attrs;
if (server.size())
attrs["to"] = from + "@" + server;
else
attrs["to"] = from + "@" + wc->whatsappserver;
attrs["type"] = "text";
attrs["id"] = id;
attrs["t"] = stime;
Tree mes("message", attrs);
mes.addChild(tbody);
return wc->serialize_tree(&mes);
}
Message *ChatMessage::copy() const
{
return new ChatMessage(wc, from, t, id, message, author);
}
ChatMessage ChatMessage::parseProtobuf(const WhatsappConnection * wc, const std::string from, const unsigned long long time,
const std::string id, const std::string author, const std::string & buf) {
AxolotlMessage pbuf;
pbuf.ParseFromString(buf);
return ChatMessage(wc, from, time, id, pbuf.textmsg(), author);
}
std::string ChatMessage::getProtoBuf() const {
AxolotlMessage pbuf;
pbuf.set_textmsg(message);
std::string ret;
pbuf.SerializeToString(&ret);
return ret + '\1';
}
CipheredChatMessage::CipheredChatMessage(const WhatsappConnection * wc, const std::string from, const unsigned long long time,
const std::string id, const std::string message, const std::string author, const std::string ctype) :
ChatMessage(wc, from, time, id, message, author)
{
this->msg_body = "enc";
this->ctype = ctype;
}
CallMessage::CallMessage(const WhatsappConnection * wc, const std::string from, const unsigned long long time,
const std::string id) :
Message(wc, from, time, id, "")
{
}
DataBuffer CallMessage::serialize() const
{
Tree mes("call");
return wc->serialize_tree(&mes);
}
Message *CallMessage::copy() const
{
return new CallMessage(wc, from, t, id);
}
ImageMessage::ImageMessage(const WhatsappConnection * wc, const std::string from, const unsigned long long time,
const std::string id, const std::string author, const std::string url, const std::string caption,
const std::string ip, const unsigned int width, const unsigned int height, const unsigned int size,
const std::string encoding, const std::string hash, const std::string filetype, const std::string preview)
:MediaMessage(wc, from, time, id, author, url, caption, ip, hash, filetype)
{
this->width = width;
this->height = height;
this->size = size;
this->encoding = encoding;
this->preview = preview;
}
DataBuffer ImageMessage::serialize() const
{
std::map < std::string, std::string > mattrs;
mattrs["encoding"] = "raw";
mattrs["filehash"] = this->hash;
mattrs["mimetype"] = this->filetype;
mattrs["width"] = std::to_string(this->width);
mattrs["height"] = std::to_string(this->height);
mattrs["type"] = "image";
mattrs["url"] = url;
mattrs["size"] = std::to_string(size);
mattrs["file"] = basename(url);
mattrs["ip"] = this->ip;
Tree tmedia("media", mattrs);
tmedia.setData(preview); /* ICON DATA! */
std::string stime = std::to_string(t);
std::map < std::string, std::string > attrs;
if (server.size())
attrs["to"] = from + "@" + server;
else
attrs["to"] = from + "@" + wc->whatsappserver;
attrs["type"] = "media";
attrs["id"] = id;
attrs["t"] = stime;
Tree mes("message", attrs);
mes.addChild(tmedia);
return wc->serialize_tree(&mes);
}
Message *ImageMessage::copy() const
{
ImageMessage * im = new ImageMessage(wc, from, t, id, author, url, caption, ip, width, height, size, encoding, hash, filetype, preview);
im->e2e_key = this->e2e_key;
im->e2e_aeskey = this->e2e_aeskey;
im->e2e_iv = this->e2e_iv;
return im;
}
ImageMessage ImageMessage::parseProtobuf(const WhatsappConnection * wc, const std::string from, const unsigned long long time,
const std::string id, const std::string author, const std::string & buf) {
AxolotlMessage pbuf;
pbuf.ParseFromString(buf);
ImageMessage im(wc, from, time, id, author,
pbuf.imagemsg().url(), pbuf.imagemsg().caption(), "",
pbuf.imagemsg().width(), pbuf.imagemsg().height(), pbuf.imagemsg().length(), "",
pbuf.imagemsg().sha256(), pbuf.imagemsg().mimetype(), pbuf.imagemsg().thumbnail());
// Fill in the RefKey needed to decrypt the image
im.e2e_key = pbuf.imagemsg().refkey();
// Calculate keys
HKDF keygen(3);
std::string keys = keygen.deriveSecrets(im.e2e_key, "WhatsApp Image Keys", 112, "");
im.e2e_iv = keys.substr(0, 16);
im.e2e_aeskey = keys.substr(16, 32);
//std::strings macKey = keys.substr(48, 80);
//std::strings refKey = keys.substr(80);
return im;
}
SoundMessage::SoundMessage(const WhatsappConnection * wc, const std::string from, const unsigned long long time,
const std::string id, const std::string author, const std::string url, const std::string caption,
const std::string hash, const std::string filetype)
:MediaMessage(wc, from, time, id, author, url, caption, "", hash, filetype)
{
}
Message * SoundMessage::copy() const
{
return new SoundMessage(wc, from, t, id, author, url, caption, hash, filetype);
}
VideoMessage::VideoMessage(const WhatsappConnection * wc, const std::string from, const unsigned long long time,
const std::string id, const std::string author, const std::string url, const std::string caption, const std::string hash,
const std::string filetype)
:MediaMessage(wc, from, time, id, author, url, caption, "", hash, filetype)
{
}
Message * VideoMessage::copy() const
{
return new VideoMessage(wc, from, t, id, author, url, caption, hash, filetype);
}
LocationMessage::LocationMessage(const WhatsappConnection * wc, const std::string from,
const unsigned long long time, const std::string id, const std::string author,
double lat, double lng, const std::string name, std::string preview)
:Message(wc, from, time, id, author), latitude(lat), longitude(lng),
name(name), preview(preview)
{
}
Message * LocationMessage::copy() const
{
return new LocationMessage(wc, from, t, id, author, latitude, longitude, name, preview);
}
LocationMessage LocationMessage::parseProtobuf(const WhatsappConnection * wc, const std::string from, const unsigned long long time,
const std::string id, const std::string author, const std::string & buf) {
AxolotlMessage pbuf;
pbuf.ParseFromString(buf);
return LocationMessage(wc, from, time, id, author, pbuf.locationmsg().latitude(),
pbuf.locationmsg().longitude(), pbuf.locationmsg().name() + " (" + pbuf.locationmsg().address() + ")",
pbuf.locationmsg().jpeg_thumbnail());
}
VCardMessage::VCardMessage(const WhatsappConnection * wc, const std::string from, const unsigned long long time,
const std::string id, const std::string author, const std::string name, const std::string vcard)
: Message(wc, from, time, id, author), name(name), vcard(vcard)
{}
Message *VCardMessage::copy() const
{
return new VCardMessage(wc, from, t, id, author, name, vcard);
}
DataBuffer VCardMessage::serialize() const
{
Tree vcardt("vcard", makeat({"name", this->name}));
vcardt.setData(this->vcard);
Tree tmedia("media", makeat({"encoding", "text", "type", "vcard"}));
tmedia.addChild(vcardt);
std::string stime = std::to_string(t);
std::map < std::string, std::string > attrs;
if (server.size())
attrs["to"] = from + "@" + server;
else
attrs["to"] = from + "@" + wc->whatsappserver;
attrs["type"] = "media";
attrs["id"] = id;
attrs["t"] = stime;
Tree mes("message", attrs);
mes.addChild(tmedia);
return wc->serialize_tree(&mes);
}
DataBuffer LocationMessage::serialize() const
{
return DataBuffer();
}
DataBuffer VideoMessage::serialize() const
{
return DataBuffer();
}
DataBuffer SoundMessage::serialize() const
{
return DataBuffer();
}