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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
|
/*
Miranda SmileyAdd Plugin
Copyright (C) 2007 - 2011 Boris Krasnovskiy
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 version 2
of the License.
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/>.
*/
#include "general.h"
#include "m_smileyadd.h"
#include "m_folders.h"
extern HANDLE hEvent1;
HANDLE hNetlibUser;
static HANDLE hFolder, hFolderHook;
struct QueueElem
{
bkstring url;
bkstring fname;
bool needext;
QueueElem(bkstring& purl, bkstring& pfname, bool ne)
: url(purl), fname(pfname), needext(ne) {}
};
static HANDLE g_hDlMutex;
static OBJLIST<QueueElem> dlQueue(10);
static TCHAR cachepath[MAX_PATH];
static bool threadRunning;
bool InternetDownloadFile(const char *szUrl, char* szDest, HANDLE &hHttpDwnl)
{
int result = 0xBADBAD;
char* szRedirUrl = NULL;
NETLIBHTTPREQUEST nlhr = {0};
// initialize the netlib request
nlhr.cbSize = sizeof(nlhr);
nlhr.requestType = REQUEST_GET;
nlhr.flags = NLHRF_NODUMP;
nlhr.szUrl = (char*)szUrl;
nlhr.nlc = hHttpDwnl;
if (CallService(MS_SYSTEM_GETVERSION, 0, 0) >= PLUGIN_MAKE_VERSION(0,9,0,5))
nlhr.flags |= NLHRF_HTTP11 | NLHRF_PERSISTENT | NLHRF_REDIRECT;
// change the header so the plugin is pretended to be IE 6 + WinXP
nlhr.headersCount = 2;
nlhr.headers=(NETLIBHTTPHEADER*)alloca(sizeof(NETLIBHTTPHEADER)*nlhr.headersCount);
nlhr.headers[0].szName = "User-Agent";
nlhr.headers[0].szValue = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)";
nlhr.headers[1].szName = "Connection";
nlhr.headers[1].szValue = "close";
while (result == 0xBADBAD)
{
// download the page
NETLIBHTTPREQUEST *nlhrReply = (NETLIBHTTPREQUEST*)CallService(MS_NETLIB_HTTPTRANSACTION,
(WPARAM)hNetlibUser,(LPARAM)&nlhr);
if (nlhrReply)
{
hHttpDwnl = nlhrReply->nlc;
// if the recieved code is 200 OK
if(nlhrReply->resultCode == 200)
{
char* delim = strrchr(szDest, '\\');
if (delim) *delim = '\0';
CallService(MS_UTILS_CREATEDIRTREE, 0, (LPARAM)szDest);
if (delim) *delim = '\\';
int res = -1;
int fh = _open(szDest, _O_BINARY | _O_WRONLY | _O_CREAT, _S_IREAD | _S_IWRITE);
if (fh != -1)
{
res = _write(fh, nlhrReply->pData, nlhrReply->dataLength);
_close(fh);
}
if (res < 0)
remove(szDest);
else
result = 0;
}
// if the recieved code is 302 Moved, Found, etc
// workaround for url forwarding
else if(nlhrReply->resultCode == 302 || nlhrReply->resultCode == 301 || nlhrReply->resultCode == 307) // page moved
{
// get the url for the new location and save it to szInfo
// look for the reply header "Location"
for (int i=0; i<nlhrReply->headersCount; i++)
{
if (!strcmp(nlhrReply->headers[i].szName, "Location"))
{
size_t rlen = 0;
if (nlhrReply->headers[i].szValue[0] == '/')
{
const char* szPath;
const char* szPref = strstr(szUrl, "://");
szPref = szPref ? szPref + 3 : szUrl;
szPath = strchr(szPref, '/');
rlen = szPath != NULL ? szPath - szUrl : strlen(szUrl);
}
szRedirUrl = (char*)mir_realloc(szRedirUrl,
rlen + strlen(nlhrReply->headers[i].szValue)*3 + 1);
strncpy(szRedirUrl, szUrl, rlen);
strcpy(szRedirUrl+rlen, nlhrReply->headers[i].szValue);
nlhr.szUrl = szRedirUrl;
break;
}
}
}
else
result = 1;
}
else
{
hHttpDwnl = NULL;
result = 1;
}
CallService(MS_NETLIB_FREEHTTPREQUESTSTRUCT,0,(LPARAM)nlhrReply);
}
mir_free(szRedirUrl);
return result == 0;
}
void __cdecl SmileyDownloadThread(void*)
{
bool needext = false;
HANDLE hHttpDwnl = NULL;
WaitForSingleObject(g_hDlMutex, 3000);
while (!Miranda_Terminated() && dlQueue.getCount())
{
ReleaseMutex(g_hDlMutex);
if (_taccess(dlQueue[0].fname.c_str(), 0) != 0)
{
InternetDownloadFile(T2A_SM(dlQueue[0].url.c_str()), T2A_SM(dlQueue[0].fname.c_str()), hHttpDwnl);
WaitForSingleObject(g_hDlMutex, 3000);
bkstring fname(dlQueue[0].fname);
if (dlQueue[0].needext) { fname += GetImageExt(fname); needext = true; }
_trename(dlQueue[0].fname.c_str(), fname.c_str());
}
else
WaitForSingleObject(g_hDlMutex, 3000);
dlQueue.remove(0);
}
dlQueue.destroy();
if (hHttpDwnl) Netlib_CloseHandle(hHttpDwnl);
threadRunning = false;
ReleaseMutex(g_hDlMutex);
if (!Miranda_Terminated())
{
if (needext)
CallServiceSync(MS_SMILEYADD_RELOAD, 0, 0);
else
NotifyEventHooks(hEvent1, 0, 0);
}
}
bool GetSmileyFile(bkstring& url, const bkstring& packstr)
{
_TPattern * urlsplit = _TPattern::compile(_T(".*/(.*)"));
_TMatcher * m0 = urlsplit->createTMatcher(url);
m0->findFirstMatch();
bkstring filename;
filename.appendfmt(_T("%s\\%s\\"), cachepath, packstr.c_str());
size_t pathpos = filename.size();
filename += m0->getGroup(1);
delete m0;
delete urlsplit;
bool needext = filename.find('.') == filename.npos;
if (needext) filename += _T(".*");
_tfinddata_t c_file;
INT_PTR hFile = _tfindfirst((TCHAR*)filename.c_str(), &c_file);
if (hFile > -1)
{
_findclose(hFile);
filename.erase(pathpos);
filename += c_file.name;
url = filename;
return false;
}
if (needext) filename.erase(filename.size()-1);
WaitForSingleObject(g_hDlMutex, 3000);
dlQueue.insert(new QueueElem(url, filename, needext));
ReleaseMutex(g_hDlMutex);
if (!threadRunning)
{
threadRunning = true;
mir_forkthread(SmileyDownloadThread, NULL);
}
url = filename;
return false;
}
void GetDefaultSmileyCacheFolder(TCHAR* szPath, size_t cbLen)
{
TCHAR *tmpPath = Utils_ReplaceVarsT(_T("%miranda_userdata%\\SmileyCache"));
if ((INT_PTR)tmpPath != CALLSERVICE_NOTFOUND)
{
mir_sntprintf(szPath, cbLen, _T("%s"), tmpPath);
mir_free(tmpPath);
}
else
{
char dbPath[MAX_PATH];
CallService(MS_DB_GETPROFILEPATH, SIZEOF(dbPath), (LPARAM)dbPath);
mir_sntprintf(szPath, cbLen, _T("%s\\SmileyCache"), A2T_SM(dbPath));
}
}
int FolderChanged(WPARAM, LPARAM)
{
FOLDERSGETDATA fgd = {0};
fgd.cbSize = sizeof(FOLDERSGETDATA);
fgd.nMaxPathSize = SIZEOF(cachepath);
fgd.szPathT = cachepath;
if (CallService(MS_FOLDERS_GET_PATH, (WPARAM) hFolder, (LPARAM) &fgd))
{
GetDefaultSmileyCacheFolder(cachepath, SIZEOF(cachepath));
}
return 0;
}
void GetSmileyCacheFolder(void)
{
TCHAR defaultPath[MAX_PATH];
GetDefaultSmileyCacheFolder(defaultPath, SIZEOF(defaultPath));
FOLDERSDATA fd = {0};
fd.cbSize = sizeof(FOLDERSDATA);
strcpy(fd.szSection, "SmileyAdd");
strcpy(fd.szName,"Smiley Cache");
fd.szFormatT = defaultPath;
fd.flags = FF_TCHAR;
hFolder = (HANDLE)CallService(MS_FOLDERS_REGISTER_PATH, 0, (LPARAM) &fd);
FolderChanged(0, 0);
hFolderHook = HookEvent(ME_FOLDERS_PATH_CHANGED, FolderChanged);
}
void DownloadInit(void)
{
NETLIBUSER nlu = {0};
nlu.cbSize = sizeof(nlu);
nlu.flags = NUF_OUTGOING|NUF_HTTPCONNS|NUF_NOHTTPSOPTION;
nlu.szSettingsModule = "SmileyAdd";
nlu.szDescriptiveName = Translate("SmileyAdd HTTP connections");
hNetlibUser = (HANDLE)CallService(MS_NETLIB_REGISTERUSER, 0, (LPARAM)&nlu);
GetSmileyCacheFolder();
g_hDlMutex = CreateMutex(NULL, FALSE, NULL);
}
void DownloadClose(void)
{
UnhookEvent(hFolderHook);
CloseHandle(g_hDlMutex);
Netlib_CloseHandle(hNetlibUser);
}
|