summaryrefslogtreecommitdiff
path: root/protocols/WhatsApp/src/proto.h
blob: 04a557f8932f3263f8f6ef6069ae6f4fc9d1e837 (plain)
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
#if !defined(PROTO_H)
#define PROTO_H

class WASocketConnection;

#include "WhatsAPI++/WAConnection.h"

struct WAChatInfo
{
	WAChatInfo(wchar_t *_jid, wchar_t *_nick) :
		tszJid(_jid), tszNick(_nick)
	{
		bActive = false;
	}

	map<std::string, std::wstring> m_unsentMsgs;
	ptrT tszJid, tszNick, tszOwner;
	bool bActive;

	MCONTACT hContact;
};

class WhatsAppProto : public PROTO<WhatsAppProto>, public WAListener, public WAGroupListener
{
public:
	WhatsAppProto(const char *proto_name, const wchar_t *username);
	~WhatsAppProto();

	inline bool isOnline() const
	{	return (m_pConnection != NULL);
	}

	inline bool isOffline() const
	{	return (m_iStatus == ID_STATUS_OFFLINE);
	}

	inline bool isInvisible() const
	{	return (m_iStatus == ID_STATUS_INVISIBLE);
	}

	// PROTO_INTERFACE ///////////////////////////////////////////////////////////////////

	virtual	MCONTACT  __cdecl AddToList(int flags, PROTOSEARCHRESULT* psr);

	virtual	DWORD_PTR __cdecl GetCaps(int type, MCONTACT hContact = NULL);

	virtual	HANDLE    __cdecl SearchBasic(const wchar_t* id);

	virtual	int       __cdecl RecvMsg(MCONTACT hContact, PROTORECVEVENT*);

	virtual	int       __cdecl SendMsg(MCONTACT hContact, int flags, const char* msg);

	virtual	HANDLE    __cdecl SendFile(MCONTACT hContact, const wchar_t*, wchar_t **ppszFiles);

	virtual	int       __cdecl SetStatus(int iNewStatus);

	virtual	int       __cdecl UserIsTyping(MCONTACT hContact, int type);

	virtual	int       __cdecl OnEvent(PROTOEVENTTYPE iEventType, WPARAM wParam, LPARAM lParam);

	// Services //////////////////////////////////////////////////////////////////////////

	INT_PTR __cdecl SvcCreateAccMgrUI(WPARAM, LPARAM);

	// Events ////////////////////////////////////////////////////////////////////////////

	int __cdecl OnOptionsInit(WPARAM, LPARAM);
	int __cdecl OnUserInfo(WPARAM, LPARAM);
	int __cdecl OnBuildStatusMenu(WPARAM, LPARAM);

	// Worker Threads ////////////////////////////////////////////////////////////////////

	void __cdecl stayConnectedLoop(void*);
	void __cdecl sentinelLoop(void*);

	// Processing Threads ////////////////////////////////////////////////////////////////

	void __cdecl ProcessBuddyList(void*);
	void __cdecl SearchAckThread(void*);

	// Contacts handling /////////////////////////////////////////////////////////////////

	MCONTACT AddToContactList(const std::string &jid, const char *new_name = NULL);

	MCONTACT ContactIDToHContact(const std::string&);
	void     SetAllContactStatuses(int status, bool reset_client = false);
	void     UpdateStatusMsg(MCONTACT hContact);
	wchar_t*   GetContactDisplayName(const string &jid);
	void     RequestFriendship(MCONTACT hContact);

	// Group chats ///////////////////////////////////////////////////////////////////////

	std::vector<string> m_szInviteJids;
	map<std::string, WAChatInfo*> m_chats;
	mir_cs   m_csChats;
	ptrT     m_tszDefaultGroup;

	void     ChatLogMenuHook(WAChatInfo *pInfo, GCHOOK *gch);
	void     NickListMenuHook(WAChatInfo *pInfo, GCHOOK *gch);

	void     AddChatUser(WAChatInfo *pInfo, const wchar_t *ptszJid);
	void     EditChatSubject(WAChatInfo *pInfo);
	void     InviteChatUser(WAChatInfo *pInfo);
	void     KickChatUser(WAChatInfo *pInfo, const wchar_t *ptszJid);
	wchar_t*   GetChatUserNick(const std::string &jid);
	void     SetChatAvatar(WAChatInfo *pInfo);

	void     onGroupMessageReceived(const FMessage &fmsg);

	WAChatInfo* InitChat(const std::string &jidjid, const std::string &nick);
	WAChatInfo* SafeGetChat(const std::string &jid);

	int      __cdecl onGroupChatEvent(WPARAM, LPARAM);
	int      __cdecl OnDeleteChat(WPARAM, LPARAM);
	int      __cdecl OnChatMenu(WPARAM, LPARAM);
	INT_PTR  __cdecl OnCreateGroup(WPARAM, LPARAM);

	// Registration //////////////////////////////////////////////////////////////////////

	bool Register(int state, const string &cc, const string &number, const string &code, string &password);

private:
	// Helpers 	//////////////////////////////////////////////////////////////////////////

	LONG m_iSerial;
	__forceinline LONG GetSerial()
	{	return ::_InterlockedIncrement(&m_iSerial);
	}

	void ToggleStatusMenuItems(bool bEnable);

	/// Avatars //////////////////////////////////////////////////////////////////////////

	std::wstring GetAvatarFileName(MCONTACT);
	std::wstring m_tszAvatarFolder;

	INT_PTR __cdecl GetAvatarInfo(WPARAM, LPARAM);
	INT_PTR __cdecl GetAvatarCaps(WPARAM, LPARAM);
	INT_PTR __cdecl GetMyAvatar(WPARAM, LPARAM);
	INT_PTR __cdecl SetMyAvatar(WPARAM, LPARAM);

	int InternalSetAvatar(MCONTACT hContact, const char *szJid, const wchar_t *ptszFileName);

	// Private data //////////////////////////////////////////////////////////////////////
	
	void InitMenu();
	HGENMENU m_hMenuCreateGroup;

	HANDLE  update_loop_lock_;

	WASocketConnection *m_pSocket;
	WAConnection *m_pConnection;
	Mutex connMutex;
	time_t m_tLastWriteTime;

	std::vector<unsigned char> m_Challenge;
	std::string m_szPhoneNumber;
	std::string m_szJid, m_szNick;
	std::map<string, MCONTACT> m_hContactByJid;
	map<MCONTACT, map<MCONTACT, bool>> isMemberByGroupContact;

protected:
	// WAListener methods ////////////////////////////////////////////////////////////////
	virtual void onMessageForMe(const FMessage &paramFMessage);
	virtual void onMessageStatusUpdate(const FMessage &paramFMessage);
	virtual void onMessageError(const FMessage&, int) { }
	virtual void onPing(const std::string &id) throw (WAException);
	virtual void onPingResponseReceived() { }
	virtual void onAvailable(const std::string &paramString, bool paramBoolean, DWORD lastSeenTime);
	virtual void onClientConfigReceived(const std::string&) { }
	virtual void onIsTyping(const std::string &paramString, bool paramBoolean);
	virtual void onAccountChange(int, time_t) { }
	virtual void onPrivacyBlockListAdd(const std::string&) { }
	virtual void onPrivacyBlockListClear() { }
	virtual void onDirty(const std::map<string, string>&) { }
	virtual void onDirtyResponse(int) { }
	virtual void onRelayRequest(const std::string&, int, const std::string&) { }
	virtual void onSendGetPicture(const std::string &jid, const std::vector<unsigned char>& data, const std::string &id);
	virtual void onPictureChanged(const std::string &jid, const std::string &id, bool set);
	virtual void onContactChanged(const std::string &jid, bool added);
	virtual void onDeleteAccount(bool) {}

	// WAGroupListener methods ///////////////////////////////////////////////////////////
	virtual void onGroupAddUser(const std::string &gjid, const std::string &ujid, int ts);
	virtual void onGroupRemoveUser(const std::string &gjid, const std::string &ujid, int ts);
	virtual void onGroupNewSubject(const std::string &from, const std::string &author, const std::string &newSubject, int ts);
	virtual void onGroupMessage(const FMessage &paramFMessage);
	virtual void onServerProperties(std::map<std::string, std::string>*) { }
	virtual void onGroupCreated(const std::string &gjid, const std::string &nick);
	virtual void onGroupInfo(const std::string &jid, const std::string &owner, const std::string &subject, const std::string &subject_owner, int time_subject, int time_created);
	virtual void onSetSubject(const std::string&) { }
	virtual void onAddGroupParticipants(const std::string&, const std::vector<string>&, int) { }
	virtual void onRemoveGroupParticipants(const std::string&, const std::vector<string>&, int) { }
	virtual void onGetParticipants(const std::string &gjid, const std::vector<string> &participants);
	virtual void onLeaveGroup(const std::string &paramString);

	// Information providing /////////////////////////////////////////////////////////////

	void NotifyEvent(const wchar_t *title, const wchar_t *info, MCONTACT contact, DWORD flags, wchar_t *url = NULL);
	void NotifyEvent(const std::string &title, const std::string &info, MCONTACT contact, DWORD flags, wchar_t *url = NULL);
};

#endif