summaryrefslogtreecommitdiff
path: root/protocols/Twitter/src/proto.h
blob: e3e5650b4d8e20218650f18d1dee8f2078e08e7f (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
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
/*
Copyright © 2012-23 Miranda NG team
Copyright © 2009 Jim Porter

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#pragma once

#include "..\..\..\..\miranda-private-keys\Twitter\oauth.dev.h"

typedef __int64 twitter_id;

class CTwitterProto;

struct twitter_status
{
	std::string text;
	twitter_id id;
	time_t time;
};

struct twitter_user
{
	std::string username;
	std::string real_name;
	std::string profile_image_url;
	twitter_status status;
};

time_t parse_time(const CMStringA &str);

struct CChatMark
{
	CChatMark(MEVENT _p1, const CMStringA &_p2) :
		hEvent(_p1),
		szId(_p2)
	{
	}

	MEVENT hEvent;
	CMStringA szId;
};

struct AsyncHttpRequest : public MTHttpRequest<CTwitterProto>
{
	AsyncHttpRequest(int type, const char *szUrl, MTHttpRequestHandler pHandler = nullptr);
};

class CTwitterProto : public PROTO<CTwitterProto>
{
	SESSION_INFO *m_si;

	// http server thread routines
	bool m_bTerminated;
	mir_cs m_csHttpQueue;
	HANDLE m_evRequestsQueue;
	LIST<AsyncHttpRequest> m_arHttpQueue;

	void Push(AsyncHttpRequest *req);
	void Execute(AsyncHttpRequest *req);

	void BeginConnection();
	void OnLoggedIn();
	void OnLoggedFail();

	int  m_hWorkerThreadId;
	void __cdecl ServerThread(void *);

	HNETLIBCONN m_hConnHttp;

	// internal data
	CMStringA m_szUserName;
	CMStringA m_szMyId;
	CMStringA m_szPassword;
	CMStringA m_szConsumerKey;
	CMStringA m_szConsumerSecret;
	CMStringA m_szAccessToken;
	CMStringA m_szAccessTokenSecret;
	CMStringA m_szPin;

	CMStringW GetAvatarFolder();

	mir_cs avatar_lock_;
	mir_cs twitter_lock_;

	OBJLIST<CChatMark> m_arChatMarks;

	HNETLIBUSER hAvatarNetlib_;
	HANDLE hMsgLoop_;

	twitter_id since_id_;
	twitter_id dm_since_id_;

	////////////////////////////////////////////////////////////////////////////////////////
	// utils

	twitter_id getId(const char *szSetting);
	void setId(const char *szSetting, twitter_id id);

	////////////////////////////////////////////////////////////////////////////////////////
	// oauth

	uint8_t code_verifier[32];
	CMStringA code_challenge;

	void RequestOauthAuth();
	void RequestOauthToken(const char *szPin = nullptr);
	void ResetOauthKeys();

	void Oauth2RequestAuth(NETLIBHTTPREQUEST *, AsyncHttpRequest *);
	void Oauth2RequestToken(NETLIBHTTPREQUEST *, AsyncHttpRequest *);

	CMStringA OAuthWebRequestSubmit(const CMStringA &url, const char *httpMethod, const char *postData);

	CMStringA UrlGetQuery(const CMStringA &url);

	CMStringA BuildSignedOAuthParameters(const CMStringA &requestParameters, const CMStringA &url, const char *httpMethod, const char *postData);

	CMStringA OAuthCreateNonce();
	CMStringA OAuthCreateSignature(const CMStringA &signatureBase, const CMStringA &consumerSecret, const CMStringA &requestTokenSecret);

	void UpdateStatuses(bool pre_read, bool popups, bool tweetToMsg);
	void UpdateMessages(bool pre_read);
	void UpdateFriends();
	void UpdateAvatar(MCONTACT, const CMStringA &, bool force = false);

	int  ShowPinDialog();
	void ShowPopup(const wchar_t *, int Error = 0);
	void ShowPopup(const char *, int Error = 0);
	void ShowContactPopup(MCONTACT, const CMStringA &, const CMStringA *);

	bool IsMyContact(MCONTACT, bool include_chat = false);
	MCONTACT UsernameToHContact(const char *);
	MCONTACT AddToClientList(const char *, const char *);
	MCONTACT FindContactById(const char *);

	void UpdateChat(const twitter_user &update);
	void AddChatContact(const char *name, const char *nick = nullptr);
	void DeleteChatContact(const char *name);
	void SetChatStatus(int);

	bool get_info(const CMStringA &name, twitter_user *);
	bool get_info_by_email(const CMStringA &email, twitter_user *);

	bool add_friend(const CMStringA &name, twitter_user &u);
	void remove_friend(const CMStringA &name);

	void mark_read(MCONTACT hContact, const CMStringA &msgId);

	void set_status(const CMStringA &text);
	void send_direct(const CMStringA &name, const CMStringA &text);

public:
	CTwitterProto(const char*,const wchar_t*);
	~CTwitterProto();

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

	MCONTACT AddToList(int,PROTOSEARCHRESULT *) override;

	INT_PTR  GetCaps(int, MCONTACT = 0) override;
	int      GetInfo(MCONTACT, int) override;

	HANDLE   SearchBasic(const wchar_t *) override;
	HANDLE   SearchByEmail(const wchar_t *) override;

	int      SendMsg(MCONTACT, MEVENT, const char *) override;

	int      SetStatus(int) override;

	HANDLE   GetAwayMsg(MCONTACT) override;

	bool     OnContactDeleted(MCONTACT) override;
	MWindow  OnCreateAccMgrUI(MWindow) override;
	void     OnMarkRead(MCONTACT, MEVENT) override;
	void     OnModulesLoaded() override;

	void UpdateSettings();

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

	INT_PTR __cdecl ReplyToTweet(WPARAM,LPARAM);
	INT_PTR __cdecl VisitHomepage(WPARAM,LPARAM);
	INT_PTR __cdecl GetAvatar(WPARAM,LPARAM);
	INT_PTR __cdecl SetAvatar(WPARAM,LPARAM);

	INT_PTR __cdecl OnJoinChat(WPARAM,LPARAM);
	INT_PTR __cdecl OnLeaveChat(WPARAM,LPARAM);

	INT_PTR __cdecl OnTweet(WPARAM,LPARAM);

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

	int  __cdecl OnBuildStatusMenu(WPARAM, LPARAM);
	int  __cdecl OnChatOutgoing(WPARAM, LPARAM);
	int  __cdecl OnOptionsInit(WPARAM,LPARAM);
	int  __cdecl OnPrebuildContactMenu(WPARAM,LPARAM);

	void __cdecl SendTweetWorker(void *);

	//////////////////////////////////////////////////////////////////////////////////////
	// Threads

	void __cdecl AddToListWorker(void *p);
	void __cdecl DoSearch(void *);
	void __cdecl MessageLoop(void *);
	void __cdecl GetAwayMsgWorker(void *);
	void __cdecl UpdateAvatarWorker(void *);
	void __cdecl UpdateInfoWorker(void *);
};

struct CMPlugin : public ACCPROTOPLUGIN<CTwitterProto>
{
	CMPlugin();

	int Load() override;
};