forked from davidgfnet/whatsapp-purple
-
Notifications
You must be signed in to change notification settings - Fork 0
/
message.cc
211 lines (174 loc) · 5.98 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
#include "message.h"
#include "wacommon.h"
#include "wa_connection.h"
#include "tree.h"
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);
}
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 ip,
const std::string hash, const std::string filetype)
: Message(wc, from, time, id, author) {
this->url = url;
this->hash = hash;
this->filetype = filetype;
this->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;
}
DataBuffer ChatMessage::serialize() const
{
Tree tbody("body");
tbody.setData(this->message);
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);
}
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 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, 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
{
return new ImageMessage(wc, from, t, id, author, url, ip, width, height, size, encoding, hash, filetype, preview);
}
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 hash,
const std::string filetype)
:MediaMessage(wc, from, time, id, author, url, "", hash, filetype)
{
}
Message * SoundMessage::copy() const
{
return new SoundMessage(wc, from, t, id, author, url, 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 hash,
const std::string filetype)
:MediaMessage(wc, from, time, id, author, url, "", hash, filetype)
{
}
Message * VideoMessage::copy() const
{
return new VideoMessage(wc, from, t, id, author, url, 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);
}
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);
}