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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
|
#include "common.h"
#include "extern.h"
// write data needed by the external process, and restart miranda
// returns 1 if any error, 0 if shutdown is imminent
int ExternProcess(bool restart)
{
//HWND hWndMiranda = (HWND)CallService(MS_CLUI_GETHWND, 0, 0);
// spawn a process that will:
// -- wait for miranda to exit
// -- move downloaded plugins from the temp folder to the Plugins folder, possibly backing up old ones
// -- restart miranda
char msg[1024];
mir_snprintf(msg, SIZEOF(msg), "spawning external process, restart = %s", restart ? "true" : "false");
NLog(msg);
TCHAR data_filename[MAX_PATH];
mir_sntprintf(data_filename, SIZEOF(data_filename), _T("%s\\ud_data.txt"), options.data_folder);
// write data to file for external process to use
HANDLE hDatFile = CreateFile(data_filename, GENERIC_WRITE, FILE_SHARE_WRITE, 0, CREATE_ALWAYS, 0, 0);
if (hDatFile == INVALID_HANDLE_VALUE)
{
//MessageBox(0, TranslateT("Could not create data file for restart."), TranslateT("Error"), MB_OK | MB_ICONERROR);
ShowError(TranslateT("Could not create data file for restart"));
return 1;
}
TCHAR db_pathT[MAX_PATH];
if (CallService(MS_SYSTEM_GETVERSION, 0, 0) >= PLUGIN_MAKE_VERSION(0,9,0,12))
{
TCHAR *p = Utils_ReplaceVarsT(_T("%miranda_profile%\\%miranda_profilename%\\%miranda_profilename%.dat"));
mir_sntprintf(db_pathT, SIZEOF(db_pathT), _T("%s"), p);
mir_free(p);
}
else
{
char db_name[100], db_path[MAX_PATH];
// Get Miranda exe path and profile
CallService(MS_DB_GETPROFILENAME, SIZEOF(db_name), (WPARAM)db_name);
CallService(MS_DB_GETPROFILEPATH, SIZEOF(db_path), (WPARAM)db_path);
TCHAR *t1, *t2;
mir_sntprintf(db_pathT, SIZEOF(db_pathT), _T("%s\\%s"), (t1=mir_a2t(db_path)), (t2=mir_a2t(db_name)));
mir_free(t1); mir_free(t2);
}
// get plugin folder
TCHAR* plugins_folder = Utils_ReplaceVarsT(_T("%miranda_path%\\Plugins"));
TCHAR mir_exe[MAX_PATH];
GetModuleFileName(NULL, mir_exe, SIZEOF(mir_exe));
unsigned long bytes_written;
static const TCHAR tnl[] = _T("\r\n");
WriteFile(hDatFile, mir_exe, (unsigned)_tcslen(mir_exe) * sizeof(TCHAR), &bytes_written, FALSE);
WriteFile(hDatFile, tnl, 2 * sizeof(TCHAR), &bytes_written, FALSE);
WriteFile(hDatFile, plugins_folder, (unsigned)_tcslen(plugins_folder) * sizeof(TCHAR), &bytes_written, FALSE);
WriteFile(hDatFile, tnl, 2 * sizeof(TCHAR), &bytes_written, FALSE);
WriteFile(hDatFile, db_pathT, (unsigned)_tcslen(db_pathT) * sizeof(TCHAR), &bytes_written, FALSE);
WriteFile(hDatFile, tnl, 2 * sizeof(TCHAR), &bytes_written, FALSE);
WriteFile(hDatFile, options.temp_folder, (unsigned)_tcslen(options.temp_folder) * sizeof(TCHAR), &bytes_written, FALSE);
WriteFile(hDatFile, tnl, 2 * sizeof(TCHAR), &bytes_written, FALSE);
if(options.backup)
WriteFile(hDatFile, options.backup_folder, (unsigned)_tcslen(options.backup_folder) * sizeof(TCHAR), &bytes_written, FALSE);
else
WriteFile(hDatFile, _T("no backups"),(unsigned) _tcslen(_T("no backups")) * sizeof(TCHAR), &bytes_written, FALSE);
WriteFile(hDatFile, tnl, 2 * sizeof(TCHAR), &bytes_written, FALSE);
TCHAR buf[64];
mir_sntprintf(buf, SIZEOF(buf), _T("%d"), (unsigned int)GetCurrentProcessId());
//MessageBox(0, buf, "Writing process id", MB_OK);
WriteFile(hDatFile, buf, (unsigned)_tcslen(buf) * sizeof(TCHAR), &bytes_written, FALSE);
WriteFile(hDatFile, tnl, 2 * sizeof(TCHAR), &bytes_written, FALSE);
mir_sntprintf(buf, SIZEOF(buf), restart ? _T("restart") : _T("no_restart"));
WriteFile(hDatFile, buf, (unsigned)_tcslen(buf) * sizeof(TCHAR), &bytes_written, FALSE);
WriteFile(hDatFile, tnl, 2 * sizeof(TCHAR), &bytes_written, FALSE);
CloseHandle(hDatFile);
mir_free(plugins_folder);
/*
if (!CallService(MS_SYSTEM_OKTOEXIT,0,0)) {
DeleteFile(data_filename);
MessageBox(0, TranslateT("Miranda's not 'OK TO EXIT'."), TranslateT("Error"), MB_OK | MB_ICONERROR);
return;
}
*/
TCHAR szParams[MAX_PATH], szBuf[MAX_PATH], szProcDir[MAX_PATH];
// try to fire up external process from new dll (if present), so we can overwrite the old one
mir_sntprintf(szBuf, SIZEOF(szBuf), _T("%s\\plugins\\updater.dll"), options.temp_folder);
if (_taccess(szBuf, 0))
{
mir_sntprintf(szBuf, SIZEOF(szBuf), _T("%s\\updater.dll"), options.temp_folder);
if (_taccess(szBuf, 0))
{
GetModuleFileName(hInst, szBuf, MAX_PATH);
}
}
GetRootDir(szProcDir); _tcscat(szProcDir, _T("\\"));
if (IsWinVerXPPlus())
{
mir_sntprintf(szParams, SIZEOF(szParams), _T("RUNDLL32.EXE \"%s\",ExternalUpdate %s"), szBuf, data_filename);
}
else
{
TCHAR* p = _tcsrchr(szBuf, _T('\\')); if (p) *p = 0;
// rundll32 hates spaces in the <dll name> arg, but quotes aren't allowed in earlier versions...
// GetShortPath can return paths with spaces (at least on XP with 8.3 filenames disabled)...
// so we must 'CreateProcess' with the updater.dll location as the startup directory and pass only updater.dll as the arg
mir_sntprintf(szParams, SIZEOF(szParams), _T("RUNDLL32.EXE .\\plugins\\updater.dll,ExternalUpdate %s"), data_filename);
}
BOOL res;
if (IsAdminRequired())
{
SHELLEXECUTEINFO info = {0};
info.cbSize = sizeof(info);
TCHAR *p = _tcschr(szParams, ' '); if (p) *(p++) = 0;
info.lpVerb = _T("runas");
info.lpFile = szParams;
info.lpParameters = p;
info.lpDirectory = szProcDir;
info.nShow = SW_HIDE;
res = ShellExecuteEx(&info);
info.cbSize = sizeof(info);
}
else
{
PROCESS_INFORMATION pi = {0};
STARTUPINFO si = {0};
si.cb = sizeof(si);
res = CreateProcess(0, szParams, 0, 0, 0,
CREATE_NO_WINDOW | DETACHED_PROCESS | NORMAL_PRIORITY_CLASS,
0, szProcDir, &si, &pi);
if (res)
{
CloseHandle(pi.hThread);
CloseHandle(pi.hProcess);
}
}
if (res)
{
PostMessage((HWND)CallService(MS_CLUI_GETHWND, 0, 0), WM_COMMAND, ID_ICQ_EXIT, 0);
}
else
{
TCHAR msg[256];
mir_sntprintf(msg, SIZEOF(msg), _T("Error code: %d"), GetLastError());
MessageBox(0, msg, TranslateT("CreateProcess"), MB_OK | MB_ICONERROR);
}
return !res;
}
#ifdef _UD_LOGGING
void mWriteFile(HANDLE hFile, char *line) {
unsigned long bytes_written;
const char *nl = "\r\n";
WriteFile(hFile, line, (unsigned)strlen(line), &bytes_written, FALSE);
WriteFile(hFile, nl, 2, &bytes_written, FALSE);
}
void mWriteFile(HANDLE hFile, wchar_t *line) {
unsigned long bytes_written;
const char *nl = "\r\n";
char buf[267];
WideCharToMultiByte(CP_ACP, 0, line, -1, buf, SIZEOF(buf), NULL, NULL);
WriteFile(hFile, buf, (unsigned)strlen(buf), &bytes_written, FALSE);
WriteFile(hFile, nl, 2, &bytes_written, FALSE);
}
#endif
// move all files in src_folder to dst_folder - put replaced files in backup folder
// if a file in src_folder is a directory, copy it's contents to the same dir in the root folder and
// set that dir as the new root (so that dirs in dirs go in the right place)
void MoveFiles(HANDLE hLogFile, TCHAR *src_folder, TCHAR *dst_folder, TCHAR *backup_folder, TCHAR *root_folder)
{
// move files from src_folder to dst_folder
if (!src_folder || _tcslen(src_folder) == 0) {
MessageBox(0, _T("Your 'temporary files' folder is set to NULL. Install aborted."), _T("Updater Error"), MB_OK | MB_ICONERROR);
return;
}
TCHAR szFilesPath[MAX_PATH], szOldFileName[MAX_PATH], szNewFileName[MAX_PATH], szBackupFileName[MAX_PATH];
bool do_backups = backup_folder ? (_tcscmp(backup_folder, _T("no backups")) != 0) : false;
// ensure the destination folder exists
if (!CreatePath(dst_folder))
return;
_sntprintf(szFilesPath, SIZEOF(szFilesPath), _T("%s\\*.*"), src_folder);
szFilesPath[SIZEOF(szFilesPath) - 1] = 0;
bool move_file;
WIN32_FIND_DATA findData;
HANDLE hFileSearch = FindFirstFile(szFilesPath, &findData);
if (hFileSearch != INVALID_HANDLE_VALUE)
{
do
{
if (findData.cFileName[0] != '.')
{
_sntprintf(szOldFileName, SIZEOF(szOldFileName), _T("%s\\%s"), src_folder, findData.cFileName);
szOldFileName[SIZEOF(szOldFileName) - 1] = 0;
if(findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
// use szNewFileName as destination and new root folder
if (_tcslen(findData.cFileName) < SIZEOF(ROOT_FILES_FOLDER) - 1 || _tcsicmp(findData.cFileName, ROOT_FILES_FOLDER))
{
_sntprintf(szNewFileName, SIZEOF(szNewFileName), _T("%s\\%s"), root_folder, findData.cFileName);
szNewFileName[SIZEOF(szNewFileName) - 1] = 0;
}
else
_tcscpy(szNewFileName, root_folder);
// recurse
MoveFiles(hLogFile, szOldFileName, szNewFileName, backup_folder, szNewFileName);
}
else
{
// exception for langpack files - move to root_folder
// exception for dbtool.exe (e.g. translated) - move to root_folder
if ((_tcsnicmp(findData.cFileName, _T("dbtool.exe"), _tcslen(_T("dbtool.exe"))) == 0)
|| (_tcsnicmp(findData.cFileName, _T("langpack_"), _tcslen(_T("langpack_"))) == 0))
{
_sntprintf(szNewFileName, SIZEOF(szNewFileName), _T("%s\\%s"), root_folder, findData.cFileName);
szNewFileName[SIZEOF(szNewFileName) - 1] = 0;
}
else
{
_sntprintf(szNewFileName, SIZEOF(szNewFileName), _T("%s\\%s"), dst_folder, findData.cFileName);
szNewFileName[SIZEOF(szNewFileName) - 1] = 0;
}
move_file = false;
if (do_backups)
{
_sntprintf(szBackupFileName, SIZEOF(szBackupFileName), _T("%s\\%s"), backup_folder, findData.cFileName);
szBackupFileName[SIZEOF(szBackupFileName) - 1] = 0;
move_file = true;
DeleteFile(szBackupFileName);
if (!MoveFile(szNewFileName, szBackupFileName))
{
// MessageBox(0, szNewFileName, __T("Could not backup!"), MB_OK | MB_ICONWARNING);
}
}
else
{
move_file = true;
if (!DeleteFile(szNewFileName))
{
// MessageBox(0, szNewFileName, _T("Could not delete!"), MB_OK | MB_ICONWARNING);
}
}
if (move_file)
{
if (!MoveFile(szOldFileName, szNewFileName))
{
//MessageBox(0, szOldFileName, _T("Could not move!"), MB_OK | MB_ICONWARNING);
// try a copy - possibly win98 etc. will not move the updater.dll when it is being used by this process
CopyFile(szOldFileName, szNewFileName, FALSE);
DeleteFile(szOldFileName); // docs say it is marked for delete and actually removed when the last handle is closed...hmm
}
}
else
DeleteFile(szOldFileName);
}
}
}
while(FindNextFile(hFileSearch, &findData));
FindClose(hFileSearch);
}
RemoveDirectory(src_folder);
}
bool ReadTLine(HANDLE hDatFile, TCHAR *line, int bsize, int &offset) {
unsigned long bytes_read;
BOOL bResult;
while((bResult = ReadFile(hDatFile, line + offset, sizeof(TCHAR), &bytes_read, 0)) && offset < bsize && bytes_read == sizeof(TCHAR) && line[offset] && (line[offset] != _T('\n') || (offset > 0 && line[offset - 1] != _T('\r')))) offset++;
if(offset > 0) line[offset - 1] = 0; // cut off /r/n
return true;
}
void CALLBACK ExternalUpdate(HWND hwnd, HINSTANCE hInstance, LPSTR lpszCmdLine, int nCmdShow)
{
//MessageBox(0, _T("ExternalUpdate"), _T("Updater"), MB_OK);
HANDLE hDatFile = CreateFileA(lpszCmdLine, GENERIC_READ, FILE_SHARE_READ, 0, OPEN_EXISTING, 0, 0);
if(hDatFile == INVALID_HANDLE_VALUE) {
char msg[1024];
_snprintf(msg, SIZEOF(msg), "Could not read data file:\n%s", lpszCmdLine);
MessageBoxA(0, msg, "Updater Error", MB_OK | MB_ICONERROR);
} else {
TCHAR *mir_exe = (TCHAR *)malloc(MAX_PATH * sizeof(TCHAR)),
*plugins_folder = (TCHAR *)malloc(MAX_PATH * sizeof(TCHAR)),
*temp_folder = (TCHAR *)malloc(MAX_PATH * sizeof(TCHAR)),
*backup_folder = (TCHAR *)malloc(MAX_PATH * sizeof(TCHAR)),
*root_folder = (TCHAR *)malloc(MAX_PATH * sizeof(TCHAR)),
*db_path = (TCHAR *)malloc(MAX_PATH * sizeof(TCHAR)),
*pid = (TCHAR *)malloc(64 * sizeof(TCHAR));
bool restart = true;
{
int i = 0, offset;
TCHAR line[MAX_PATH];
BOOL bResult = TRUE;
do {
offset = 0;
ReadTLine(hDatFile, line, MAX_PATH, offset);
switch(i) {
case 0: _tcsncpy(mir_exe, line, MAX_PATH); break;
case 1: _tcsncpy(plugins_folder, line, MAX_PATH); break;
case 2: _tcsncpy(db_path, line, MAX_PATH); break;
case 3: _tcsncpy(temp_folder, line, MAX_PATH); break;
case 4: _tcsncpy(backup_folder, line, MAX_PATH); break;
case 5: _tcsncpy(pid, line, 64); break;
case 6:
restart = (_tcsncmp(line, _T("restart"), 7) == 0);
offset = 0; // end loop
break;
default:
offset = 0; // end loop
}
i++;
} while(offset > 0);
}
CloseHandle(hDatFile);
// use data file to log to
#ifndef _UD_LOGGING
DeleteFileA(lpszCmdLine);
#else
HANDLE hDatFile = CreateFileA(lpszCmdLine, GENERIC_WRITE, FILE_SHARE_WRITE, 0, CREATE_ALWAYS, 0, 0);
#endif
#ifdef _UD_LOGGING
mWriteFile(hDatFile, "Inside external process...");
#endif
_tcscpy(root_folder, mir_exe);
TCHAR *p = _tcsrchr(root_folder, _T('\\')); if (p) *p = 0;
// ensure miranda has exited
DWORD mpi = (DWORD)_ttol(pid);
bool exited = false;
#ifdef _UD_LOGGING
char logmsg[1024];
sprintf(logmsg, "Opening process #%d...", mpi);
mWriteFile(hDatFile, logmsg);
#endif
HANDLE hMiranda = OpenProcess(SYNCHRONIZE | PROCESS_TERMINATE, FALSE, mpi);
if(hMiranda) {
int mbFlags, idRetry, idCancel, idContinue;
const int MAX_SIZE = 2048;
TCHAR message[MAX_SIZE];
int exitStatus;
OSVERSIONINFO vi = {0};
vi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
GetVersionEx(&vi);
_tcsncpy(message, _T("Miranda did not exit - cannot install or restart.\n"), MAX_SIZE);
if (vi.dwMajorVersion = 5) {
//windows 2000+
mbFlags = 0x00000006L; //MB_CANCELTRYCONTINUE;
idRetry = 10; //IDTRYAGAIN
idCancel = IDCANCEL;
idContinue = 11; //IDCONTINUE
_tcsncat(message, _T("Press 'Try again' to check Miranda's status again, press 'Continue' to kill the process or press 'Cancel' to abort."), MAX_SIZE);
} else {
//windows 98, me
mbFlags = MB_ABORTRETRYIGNORE;
idRetry = IDRETRY;
idCancel = IDCANCEL;
idContinue = IDIGNORE;
_tcsncat(message, _T("Press 'Retry' to check Miranda's status again, press 'Ignore' to kill the process or press 'Abort' to abort."), MAX_SIZE);
}
while ((exitStatus = WaitForSingleObjectEx(hMiranda, 20 * 1000, FALSE)) == WAIT_TIMEOUT) {
int res = MessageBox(0, message, _T("Updater Error"), mbFlags | MB_ICONERROR);
if (res == idContinue) {
//if the user chooses Continue then kill the application
TerminateProcess(hMiranda, 1);
if ((exitStatus = WaitForSingleObjectEx(hMiranda, 5 * 1000, FALSE)) == WAIT_TIMEOUT) {
//hMiranda = OpenProcess(SYNCHRONIZE, FALSE, mpi);
//if(hMiranda) {
//CloseHandle(hMiranda);
MessageBox(0, _T("It seems Miranda is still running. Aborting update."), _T("Updater Error"), MB_OK | MB_ICONERROR);
} else {
#ifdef _UD_LOGGING
mWriteFile(hDatFile, "Wait for miranda processs to 'Terminate' interrupted - assuming it exited");
#endif
}
} else {
if (res == idRetry) {
//if the user selected 'Try again' then wait a bit more.
continue; //wait again
}
}
break; //don't update anymore (happens when user choses 'Continue' or 'Cacel'
}
#ifdef _UD_LOGGING
mWriteFile(hDatFile, "Wait for miranda processs interrupted - assuming it exited");
#endif
exited = (exitStatus != WAIT_TIMEOUT);
CloseHandle(hMiranda);
} else {
#ifdef _UD_LOGGING
mWriteFile(hDatFile, "Could not open miranda processs - assuming it exited");
#endif
//MessageBox(0, "Could not open Miranda process", "Update Error", MB_OK | MB_ICONERROR);
exited = true;
}
if(exited) {
#ifdef _UD_LOGGING
mWriteFile(hDatFile, "Miranda exited - moving files");
MoveFiles(hDatFile, temp_folder, plugins_folder, backup_folder, root_folder);
#else
MoveFiles(0, temp_folder, plugins_folder, backup_folder, root_folder);
RemoveDirectory(temp_folder);
#endif
// move files
// restart miranda
if(restart) {
#ifdef _UD_LOGGING
mWriteFile(hDatFile, "Restarting");
#endif
TCHAR szArgs[MAX_PATH];
//wsprintf(szArgs, "\"%s\" \"%s\"", db_path, db_name);
_sntprintf(szArgs, SIZEOF(szArgs), _T("\"%s\" \"%s\""), mir_exe, db_path); // includes name, dummy instead of executable?
//wsprintf(szArgs, "\"%s\"", db_name);
PROCESS_INFORMATION pi = {0};
STARTUPINFO si = {0};
si.cb = sizeof(si);
if (!CreateProcess(mir_exe, szArgs, 0, 0, 0, DETACHED_PROCESS | NORMAL_PRIORITY_CLASS, 0, 0, &si, &pi))
{
MessageBox(0, _T("Failed to restart Miranda"), _T("Updater Error"), MB_OK | MB_ICONERROR);
//MessageBox(0, szArgs, mir_exe, MB_OK);
}
else
{
CloseHandle(pi.hThread);
CloseHandle(pi.hProcess);
}
//ShellExecute(0, 0, mir_exe, szArgs, 0, SW_NORMAL);
}
}
#ifdef _UD_LOGGING
else mWriteFile(hDatFile, "Miranda did not exit");
#endif
free(pid);
free(db_path);
free(root_folder);
free(backup_folder);
free(temp_folder);
free(plugins_folder);
free(mir_exe);
#ifdef _UD_LOGGING
CloseHandle(hDatFile);
#endif
}
FreeLibraryAndExitThread(hInstance, TRUE);
}
|