summaryrefslogtreecommitdiff
path: root/protocols/Slack/src/slack_proto.h
blob: f47c70f94626a2d238a5c2bc92c078ac9523e54b (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
#ifndef _SLACK_PROTO_H_
#define _SLACK_PROTO_H_

struct CSlackProto : public PROTO<CSlackProto>
{
	friend class CSlackOptionsMain;
	friend class CSlackOAuth;

	typedef void(CSlackProto::*HttpCallback)(HttpResponse&);
	typedef void(CSlackProto::*JsonCallback)(JSONNode&);
	struct RequestQueueItem
	{
		HttpRequest *request;
		HttpCallback httpCallback;
		JsonCallback jsonCallback;
	};

public:
	//////////////////////////////////////////////////////////////////////////////////////
	//Ctors

	CSlackProto(const char *protoName, const wchar_t *userName);
	~CSlackProto();

	//////////////////////////////////////////////////////////////////////////////////////
	// Virtual functions

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

	virtual	int      __cdecl AuthRecv(MCONTACT hContact, PROTORECVEVENT*);
	virtual	int      __cdecl AuthRequest(MCONTACT hContact, const wchar_t* szMessage);

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

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

	virtual	int       __cdecl SetStatus(int iNewStatus);

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

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

	// accounts
	static CSlackProto*       InitAccount(const char *protoName, const TCHAR *userName);
	static int                UninitAccount(CSlackProto *proto);

	// icons
	static void InitIcons();

	// menus
	static void InitMenus();

	static int OnModulesLoaded(WPARAM, LPARAM);

private:
	HANDLE hNetlib;
	bool isTerminated, isConnected;
	mir_cs requestQueueLock;
	HANDLE hRequestsQueueEvent;
	HANDLE hRequestQueueThread;
	LIST<RequestQueueItem> requestQueue;

	// requests
	void SendRequest(HttpRequest *request);
	void SendRequest(HttpRequest *request, HttpCallback callback);
	void SendRequest(HttpRequest *request, JsonCallback callback);
	void PushRequest(HttpRequest *request);
	void PushRequest(HttpRequest *request, HttpCallback callback);
	void PushRequest(HttpRequest *request, JsonCallback callback);
	void __cdecl RequestQueueThread(void*);
	
	// network
	bool IsOnline();

	// accounts
	static LIST<CSlackProto> Accounts;
	static int CompareAccounts(const CSlackProto *p1, const CSlackProto *p2);

	static CSlackProto* GetContactAccount(MCONTACT hContact);

	int __cdecl OnAccountLoaded(WPARAM, LPARAM);

	INT_PTR __cdecl OnAccountManagerInit(WPARAM, LPARAM);

	// netlib
	void InitNetlib();
	void UninitNetlib();

	// login
	void Login();
	void LogOut();
	void OnAuthorize(JSONNode &root);

	// icons
	static IconItemT Icons[];
	static HANDLE GetIconHandle(int iconId);

	// menus
	static HGENMENU ContactMenuItems[CMI_MAX];
	int OnPrebuildContactMenu(WPARAM hContact, LPARAM);
	static int PrebuildContactMenu(WPARAM hContact, LPARAM lParam);

	// options
	int __cdecl OnOptionsInit(WPARAM wParam, LPARAM lParam);

	// contacts
	WORD GetContactStatus(MCONTACT hContact);
	void SetContactStatus(MCONTACT hContact, WORD status);
	void SetAllContactsStatus(WORD status);

	MCONTACT GetContact(const char *userId);
	MCONTACT AddContact(const char *userId, const char *nick, bool isTemporary = false);

	void OnGotUserProfile(JSONNode &root);
	void OnGotUserProfile(MCONTACT hContact, JSONNode &root);
	void OnGotUserList(JSONNode &root);

	INT_PTR __cdecl OnRequestAuth(WPARAM hContact, LPARAM lParam);
	INT_PTR __cdecl OnGrantAuth(WPARAM hContact, LPARAM);

	int __cdecl OnContactDeleted(MCONTACT, LPARAM);

	// messages
	int OnReceiveMessage(MCONTACT hContact, PROTORECVEVENT *pre);
	int OnSendMessage(MCONTACT hContact, int flags, const char *message);

	int OnUserIsTyping(MCONTACT hContact, int type);

	int __cdecl OnPreCreateMessage(WPARAM wParam, LPARAM lParam);

	// utils
	static void ShowNotification(const TCHAR *message, int flags = 0, MCONTACT hContact = NULL);
	static void ShowNotification(const TCHAR *caption, const TCHAR *message, int flags = 0, MCONTACT hContact = NULL);

	MEVENT AddEventToDb(MCONTACT hContact, WORD type, DWORD timestamp, DWORD flags, PBYTE pBlob, DWORD cbBlob);

	template<INT_PTR(__cdecl CSlackProto::*Service)(WPARAM, LPARAM)>
	static INT_PTR __cdecl GlobalService(WPARAM wParam, LPARAM lParam)
	{
		CSlackProto *proto = CSlackProto::GetContactAccount((MCONTACT)wParam);
		return proto ? (proto->*Service)(wParam, lParam) : 0;
	}
};

#endif //_SLACK_PROTO_H_