-
Notifications
You must be signed in to change notification settings - Fork 207
/
Copy pathwa_connection.h
245 lines (197 loc) · 7.76 KB
/
wa_connection.h
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
#ifndef __WACONNECTION__H__
#define __WACONNECTION__H__
#include <string>
#include <vector>
#include <map>
#include <memory>
#include <time.h>
#include <stdint.h>
#include "wacommon.h"
#include "databuffer.h"
#include "contacts.h"
#include "inmemoryaxolotlstore.h"
#include "axolotl_groups.h"
//#include "liteaxolotlstore.h"
class SessionCipher;
class ChatMessage;
class ImageMessage;
class Message;
class RC4Decoder;
class Tree;
struct t_fileupload {
std::string to, from;
std::string file, hash;
int rid;
std::string type;
std::string uploadurl, host, ip;
std::string thumbnail;
std::string msgid;
bool uploading;
int totalsize;
};
enum ReceptionType { rSent, rDelivered, rRead };
struct t_message_reception {
std::string id;
ReceptionType type;
unsigned long long t;
std::string from;
};
class WhatsappConnection {
friend class ChatMessage;
friend class ImageMessage;
friend class Message;
friend class CallMessage;
friend class VCardMessage;
public:
enum ErrorCode { errorNoError = 0, errorAuth, errorUnknown };
private:
enum SessionStatus { SessionNone = 0, SessionConnecting = 1, SessionWaitingChallenge = 2, SessionWaitingAuthOK = 3, SessionConnected = 4 };
/* Current dissection classes */
RC4Decoder * in, *out;
unsigned char session_key[20*4]; // V1.4 update
unsigned int frame_seq;
DataBuffer inbuffer, outbuffer;
DataBuffer sslbuffer, sslbuffer_in;
std::string challenge_data, challenge_response;
std::string phone, password;
SessionStatus conn_status;
time_t last_keepalive;
/* State stuff */
unsigned int msgcounter, iqid;
std::string nickname;
std::string whatsappserver, whatsappservergroup;
std::string mypresence, mymessage;
bool sendRead;
std::vector < std::pair<ErrorCode,std::string> > error_queue;
/* Various account info */
std::string account_type, account_status, account_expiration, account_creation;
/* Privacy */
std::string show_last_seen, show_profile_pic, show_status_msg;
/* Groups stuff */
std::map < std::string, Group > groups;
bool groups_updated;
/* Blist stuff */
std::map < std::string, BList > blists;
bool blists_updated;
/* Contact sync stuff */
std::map <std::string, std::vector<std::string> > sync_result;
/* Contacts & msg */
std::map < std::string, Contact > contacts;
std::vector < Message * >recv_messages;
std::vector < Message * >queue_messages;
std::vector < std::string > user_changes, user_icons, user_typing;
/* Reception queue */
std::vector < t_message_reception > received_messages;
void processIncomingData();
void processSSLIncomingData();
DataBuffer serialize_tree(Tree * tree, bool crypt = true);
DataBuffer write_tree(Tree * tree);
bool parse_tree(DataBuffer * data, Tree & t);
/* Upload */
std::vector < t_fileupload > uploadfile_queue;
/* SSL / HTTPS interface */
int sslstatus; /* 0 Idle, 1 sending request, 2 getting response */
/* 5/6 for image upload */
/* New Axolotl stuff */
std::shared_ptr<AxolotlStore> axolotlStore;
std::map<uint64_t, SessionCipher*> cipherHash;
std::map<std::string, GroupCipher*> gcipherHash;
bool send_ciphered;
std::string resource;
void sendEncrypt(bool);
bool receiveCipheredMessage(std::string, std::string, std::string, unsigned long long, Tree, std::string);
bool parseWhisperMessage(std::string, std::string, std::string, unsigned long long, Tree, std::string);
bool parsePreKeyWhisperMessage(std::string, std::string, std::string, unsigned long long, Tree, std::string);
bool parseGroupWhisperMessage(std::string, std::string, std::string, unsigned long long, Tree, std::string);
SessionCipher *getSessionCipher(uint64_t recepient);
GroupCipher *getGroupCipher(std::string recepient);
void sendMessageRetry(const std::string &from, const std::string &part, const std::string &msgid, unsigned long long t);
void sendGetCipherKeysFromUser(std::string jid);
void protobufIncomingMessage(std::string mtype, std::string jid, unsigned long long time,
std::string id, std::string author, std::string plaintext, Tree & enc);
void receiveMessage(const Message & m);
void notifyPresence(std::string from, std::string presence, std::string last);
void updatePrivacy();
void addPreviewPicture(std::string from, std::string picture);
void addFullsizePicture(std::string from, std::string picture);
void sendResponse();
void doPong(std::string id, std::string from);
void subscribePresence(std::string user);
void getLast(std::string user);
void queryPreview(std::string user);
void queryFullSize(std::string user);
void gotTyping(std::string who, std::string tstat);
void updateGroups();
void updateBlists();
void queryStatuses();
void notifyMyMessage();
void notifyMyPresence();
void sendInitial();
void notifyError(ErrorCode err, const std::string & reason);
DataBuffer generateResponse(std::string from, std::string type, std::string id);
std::string generateUploadPOST(t_fileupload * fu);
void processUploadQueue();
void updateFileUpload(std::string);
std::string getNextIqId();
std::string tohex(uint64_t);
public:
bool read_tree(DataBuffer * data, Tree & tt);
WhatsappConnection(std::string phone, std::string password, std::string nick, std::string axolotldb = "");
~WhatsappConnection();
std::string saveAxolotlDatabase();
std::string getPhone() const { return phone; }
void doLogin(std::string, bool);
void receiveCallback(const char *data, int len);
int sendCallback(char *data, int len);
void sentCallback(int len);
bool hasDataToSend();
ErrorCode getErrors(std::string & reason);
Message * getReceivedMessage();
bool queryReceivedMessage(std::string & msgid, int & type, unsigned long long & t, std::string & sender);
void updatePrivacy(const std::string &, const std::string &, const std::string &);
void queryPrivacy(std::string &, std::string &, std::string &);
std::string decodeImage(std::string, std::string, std::string);
std::string getMessageId();
void addContacts(std::vector < std::string > clist);
void contactsUpdate();
std::string syncContacts(std::vector < std::string > clist);
bool getSyncResult(std::string, std::vector<std::string>&);
void sendChat(std::string msgid, std::string to, std::string message);
void sendGroupChat(std::string msgid, std::string to, std::string message);
bool query_status(std::string & from, int &status);
bool query_icon(std::string & from, std::string & icon, std::string & hash);
bool query_avatar(std::string user, std::string & icon);
bool query_typing(std::string & from, int &status);
void send_avatar(const std::string & avatar, const std::string & avatarp);
void account_info(unsigned long long &creation, unsigned long long &freeexp, std::string & status);
int getUserStatus(const std::string & who);
std::string getUserStatusString(const std::string & who);
unsigned long long getLastSeen(const std::string & who);
void processMsgQueue();
void retryMessage(std::string id);
void manageParticipant(std::string group, std::string participant, std::string command);
void leaveGroup(std::string group);
void deleteBlist(std::string id);
void notifyTyping(std::string who, int status);
void setMyPresence(std::string s, std::string msg);
std::map < std::string, Group > getGroups();
bool groupsUpdated();
bool blistsUpdated();
void addGroup(std::string subject);
int loginStatus() const
{
return ((int)conn_status) - 1;
}
int sendImage(std::string mid, std::string to, int w, int h, unsigned int size, const char *fp);
void sendVCard(const std::string msgid, const std::string to, const std::string name, const std::string vcard);
int sendSSLCallback(char *buffer, int maxbytes);
int sentSSLCallback(int bytessent);
void receiveSSLCallback(char *buffer, int bytesrecv);
bool hasSSLDataToSend();
bool closeSSLConnection();
void SSLCloseCallback();
bool hasSSLConnection(std::string & host, int & port);
int uploadProgress(int &rid, int &bs);
int uploadComplete(int);
};
#endif