summaryrefslogtreecommitdiff
path: root/plugins/Updater/scan.cpp
blob: ac068ca3475da5c00c847db1ff644c75c216e5df (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
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
#include "common.h"
#include "scan.h"

typedef PLUGININFO * (__cdecl * Miranda_Plugin_Info) ( DWORD mirandaVersion );
typedef PLUGININFOEX * (__cdecl * Miranda_Plugin_Info_Ex) ( DWORD mirandaVersion );

struct AlternateShortName
{
    const char* from;
    const char* to;
};

static const AlternateShortName alternate_shortname_map[] =
{
	{ "Version Informations", "Version Information" },
	{ "Jabber Protocol", "JabberG Protocol" },
	{ "Jabber Protocol (Unicode)", "JabberG Protocol (Unicode)" },
#ifdef _UNICODE
	{ "PopUp Interoperability", "PopUp Plus (Unicode)" },
#else
	{ "PopUp Interoperability", "PopUp Plus" },
#endif
	//{ "Messaging Style Conversation", "nConvers++" }, // will this conflict with other nConvers'?
	{ "MimQQ-libeva", "MirandaQQ (libeva Version)" },

	// grr
	{ "Updater", __PLUGIN_NAME },
	{ "Updater (Unicode)", __PLUGIN_NAME },
};

char* findAlternateShortName(const char* name)
{
    for (int i = 0; i < SIZEOF(alternate_shortname_map); ++i)
    {
        if (strcmp(name, alternate_shortname_map[i].from) == 0)
            return mir_strdup(alternate_shortname_map[i].to);
    }
#ifdef _UNICODE
	if (!strstr(name, "Unicode"))
	{
		char *buf = (char*)mir_alloc(256);
		mir_snprintf(buf, 256, "%s (Unicode)", name);
		return buf;
	}
#endif
    return NULL;
}

bool valDllName(TCHAR* name)
{
	TCHAR *p = _tcsrchr(name, '.');
	return p && _tcsicmp(p, _T(".dll")) == 0;
}

void ScanPlugins(FilenameMap *fn_map, UpdateList *update_list) 
{
	if (!XMLDataAvailable(MC_PLUGINS)) return;

	TCHAR plugins_folder[MAX_PATH], dll_path[MAX_PATH];
	TCHAR *dll_name;
	Miranda_Plugin_Info dll_info_func;
	Miranda_Plugin_Info_Ex dll_info_func_ex;
	DWORD mirandaVersion = (DWORD)CallService(MS_SYSTEM_GETVERSION, 0, 0);
	PLUGININFO *pluginInfo;

	GetRootDir(plugins_folder);
	_tcscat(plugins_folder, _T("\\Plugins"));

	_tcscpy(dll_path, plugins_folder);
	_tcscat(dll_path, _T("\\"));

	// set dll_name to point into the dll_path string, at the point where we can write the plugin name
	// to end up with the full dll path
	dll_name = dll_path + _tcslen(dll_path);

	// add filemask
	_tcscat(plugins_folder, _T("\\*.dll"));

	WIN32_FIND_DATA findData;
	HANDLE hFileSearch = FindFirstFile(plugins_folder, &findData);
	if (hFileSearch != INVALID_HANDLE_VALUE) 
	{
		do {
			HMODULE hModule = NULL;
			bool notLoaded = false;
			if (valDllName(findData.cFileName))
			{
				_tcscpy(dll_name, findData.cFileName);
				if (hModule = GetModuleHandle(dll_path)) 
				{
					hModule = LoadLibrary(dll_path);
					notLoaded = true;
				}
			}
			if (hModule) 
            {
				dll_info_func = (Miranda_Plugin_Info)GetProcAddress(hModule, "MirandaPluginInfo");
				dll_info_func_ex = (Miranda_Plugin_Info_Ex)GetProcAddress(hModule, "MirandaPluginInfoEx");
				if((dll_info_func_ex && (pluginInfo = (PLUGININFO *)dll_info_func_ex(mirandaVersion))) || (dll_info_func && (pluginInfo = dll_info_func(mirandaVersion)))) 
                {
					// *** This is a dodgy and unfair hack...
					// In order to disable new plugins that may be unintentionally installed with an update,
					// updater will check for the 'plugindisabled' setting for each dll. The problem is that
					// this setting may not be there for running plugins - and isn't there for new ones. So,
					// we'll disable anything new when the setting isn't found anyway - but we write the
					// value below for all plugins so that we can expect to find it
					char *lowname = _strlwr(mir_t2a(findData.cFileName));
					if(DBGetContactSettingByte(0, "PluginDisable", lowname, 255) == 255) // setting not present
						DBWriteContactSettingByte(0, "PluginDisable", lowname, 0);
					mir_free(lowname);

					bool found = false;
					char* alternateName = findAlternateShortName(pluginInfo->shortName);
					if (alternateName) 
                    {
                        int file_id = FindFileID(alternateName, MC_PLUGINS, update_list);
						if (file_id != -1) 
                        {
							found = true;
							if (FindFileInList(pluginInfo->shortName) >= 0)
								RegisterForFileListing(file_id, pluginInfo->shortName, pluginInfo->version, true, MC_PLUGINS);
							else
								RegisterForFileListing(file_id, alternateName, pluginInfo->version, true, MC_PLUGINS);

                            if (fn_map)
                            {
                                FileNameStruct* fns = fn_map->find((FileNameStruct*)&file_id);
                                if (fns == NULL)
                                {
                                    fns = new FileNameStruct(file_id);
                                    fn_map->insert(fns);
                                }
                                fns->list.insert(mir_tstrdup(findData.cFileName));
                            }
						}
						mir_free(alternateName);
					} 

                    if (!found) 
                    {
						int file_id = FindFileID(pluginInfo->shortName, MC_PLUGINS, update_list);
						if (file_id != -1) 
                        {
							RegisterForFileListing(file_id, pluginInfo, true);
                            if (fn_map)
                            {
                                FileNameStruct* fns = fn_map->find((FileNameStruct*)&file_id);
                                if (fns == NULL)
                                {
                                    fns = new FileNameStruct(file_id);
                                    fn_map->insert(fns);
                                }
                                fns->list.insert(mir_tstrdup(findData.cFileName));
                            }
						}
					}
				}
				if (notLoaded) FreeLibrary(hModule);
			}
		} while(FindNextFile(hFileSearch, &findData));
		FindClose(hFileSearch);
	}
}

typedef struct LangpackData_tag {
	DWORD version;
	char *fl_name;
} LangpackData;


bool GetLangpackData(const TCHAR *filename, LangpackData *ld) 
{
    char line[1024], *ver, *conv;

    FILE *fp = _tfopen(filename, _T("r"));
    if (fp == NULL) return false;

    while (fgets(line, sizeof(line), fp))
    {
        conv = strstr(line, "; FLID:");
		if (conv) 
        {
            conv += 7;

            while (*conv == ' ') conv++;
            
            ver = strchr(conv, 0) + 1;
            while (ver >= conv) if (*ver != ' ') break; else *ver-- = 0;

            ver = strrchr(conv, ' ');
            if (ver) 
            { 
                *ver = 0; 
                VersionFromString(++ver, &ld->version);
            }
            ld->fl_name = mir_strdup(conv);
            fclose(fp);
            return true;
        }
    }
    fclose(fp);
    return false;
}

void ScanLangpacks(FilenameMap *fn_map, UpdateList *update_list) 
{
	if(!XMLDataAvailable(MC_LOCALIZATION)) return;

	TCHAR mir_folder[MAX_PATH], langpack_path[MAX_PATH], *langpack_name;

	GetRootDir(mir_folder); _tcscat(mir_folder, _T("\\"));
	_tcscpy(langpack_path, mir_folder);

	// set langpack_name to point into the langpack_name string, at the point where we can write the file name
	// to end up with the full path
	langpack_name = langpack_path + _tcslen(langpack_path);

	// add filemask
	_tcscat(mir_folder, _T("langpack_*.txt"));

	WIN32_FIND_DATA findData;
	HANDLE hFileSearch = FindFirstFile(mir_folder, &findData);
	if (hFileSearch != INVALID_HANDLE_VALUE) 
    {
		do {
			_tcscpy(langpack_name, findData.cFileName);
			LangpackData ld = {0};
			if (GetLangpackData(langpack_path, &ld)) 
            {
                int file_id = FindFileID(ld.fl_name, MC_LOCALIZATION, update_list);
				if (file_id != -1) 
                {
					RegisterForFileListing(file_id, ld.fl_name, ld.version, true, MC_LOCALIZATION);
					if (fn_map) 
                    {
                        FileNameStruct* fns = fn_map->find((FileNameStruct*)&file_id);
                        if (fns == NULL)
                        {
                            fns = new FileNameStruct(file_id);
                            fn_map->insert(fns);
                        }
                        fns->list.insert(mir_tstrdup(findData.cFileName));
                    }
				}
				mir_free(ld.fl_name);
			}
		} while(FindNextFile(hFileSearch, &findData));
		FindClose(hFileSearch);
	}
}

bool RearrangeDllsWorker(char *shortName, StrList &filenames, TCHAR *basedir) 
{
	bool dll_enabled = false;
	BYTE disabled_val;

	TCHAR file_path[MAX_PATH];

	Miranda_Plugin_Info dll_info_func;
	Miranda_Plugin_Info_Ex dll_info_func_ex;
	DWORD mirandaVersion = (DWORD)CallService(MS_SYSTEM_GETVERSION, 0, 0);
	PLUGININFO *pluginInfo;
	HMODULE hModule;

	// add filemask
	mir_sntprintf(file_path, SIZEOF(file_path), _T("%s\\*.dll"), basedir);

	WIN32_FIND_DATA findData;
	HANDLE hFileSearch = FindFirstFile(file_path, &findData);
	if (hFileSearch != INVALID_HANDLE_VALUE) 
	{
		do 
		{
			mir_sntprintf(file_path, SIZEOF(file_path), _T("%s\\%s"), basedir, findData.cFileName);
			if (valDllName(findData.cFileName) && (hModule = LoadLibrary(file_path))) 
			{
				dll_info_func = (Miranda_Plugin_Info)GetProcAddress(hModule, "MirandaPluginInfo");
				dll_info_func_ex = (Miranda_Plugin_Info_Ex)GetProcAddress(hModule, "MirandaPluginInfoEx");
				if((dll_info_func_ex && (pluginInfo = (PLUGININFO *)dll_info_func_ex(mirandaVersion))) || (dll_info_func && (pluginInfo = dll_info_func(mirandaVersion)))) 
                {
					bool found = !_stricmp(pluginInfo->shortName, shortName);
					if (!found)
					{
						char* alternateName = findAlternateShortName(pluginInfo->shortName);
						found = alternateName && !_stricmp(alternateName, shortName);
						mir_free(alternateName);
					}

					if (found) 
                    {
						bool moved = false;
						TCHAR* newname = NULL;
						for (int j = 0; j < filenames.getCount(); j++) 
                        {
							TCHAR new_filename[MAX_PATH];
                            TCHAR* fileName = filenames[j];
							mir_sntprintf(new_filename, SIZEOF(new_filename), _T("%s\\%s"), basedir, fileName);

							// disable any new plugins (i.e. not installed before) that somehome got into the
							// dowloaded archives (e.g. loadavatars comes with loadavatarsw - installing both is not good!)
                            char *temp_str = _strlwr(mir_t2a(fileName));
							disabled_val = DBGetContactSettingByte(0, "PluginDisable", temp_str, 255);
							if (disabled_val == 255) { // assume this means setting not in db (should be 1 or 0)
								DBWriteContactSettingByte(0, "PluginDisable", temp_str, 1);
								disabled_val = 1;
							}
                            mir_free(temp_str);

							dll_enabled |= (disabled_val == 0);						

							if (!moved) 
							{
								if (_tcsicmp(findData.cFileName, fileName))
									MoveFile(file_path, new_filename);

                                mir_free(newname); newname = mir_tstrdup(new_filename);
								moved = true;
							} else
								CopyFile(newname, new_filename, FALSE);
						}
                        mir_free(newname);
						FreeLibrary(hModule);
						break;
					}
				}
				FreeLibrary(hModule);
			} 
		} 
		while(FindNextFile(hFileSearch, &findData));
		FindClose(hFileSearch);
	}
	return dll_enabled;
}


bool RearrangeDlls(char *shortName, StrList &filenames) 
{
	bool dll_enabled = false;

	TCHAR dir[MAX_PATH];
	mir_sntprintf(dir, SIZEOF(dir), _T("%s\\Plugins"), options.temp_folder);

	return 
		RearrangeDllsWorker(shortName, filenames, options.temp_folder) ||
		RearrangeDllsWorker(shortName, filenames, dir);
}

bool RearrangeLangpacks(char *shortName, StrList &filenames) 
{
	
	TCHAR file_path[MAX_PATH], updates_folder[MAX_PATH], new_filename[MAX_PATH], *langpack_name;
	// do exactly the same thing again, for the updates/plugins folder... :(

	mir_sntprintf(file_path, SIZEOF(file_path), _T("%s\\"), options.temp_folder);
	langpack_name = file_path + _tcslen(file_path);

	// add filemask
	mir_sntprintf(updates_folder, SIZEOF(updates_folder), _T("%s\\langpack_*.txt"), options.temp_folder);

	WIN32_FIND_DATA findData;
	HANDLE hFileSearch = FindFirstFile(updates_folder, &findData);
	if(hFileSearch != INVALID_HANDLE_VALUE) {
		do {
			_tcscpy(langpack_name, findData.cFileName);

			LangpackData ld = {0};
			if (GetLangpackData(file_path, &ld)) 
            {
				if (_stricmp(ld.fl_name, shortName) == 0) 
                {
					bool moved = false;
					TCHAR *newname = NULL;
					for (int j = 0; j < filenames.getCount(); j++) 
                    {
                        TCHAR *fileName = filenames[j];
						mir_sntprintf(new_filename, SIZEOF(new_filename), _T("%s\\%s"), options.temp_folder, fileName);
						
                        if (!moved) 
                        {
							if (_tcscmp(findData.cFileName, fileName) != 0)
								MoveFile(file_path, new_filename);
                            mir_free(newname); newname = mir_tstrdup(new_filename);
							moved = true;
						} else
							CopyFile(newname, new_filename, FALSE);
					}
                    mir_free(newname);
					break;
				}
			}
		} while(FindNextFile(hFileSearch, &findData));
		FindClose(hFileSearch);
	}

	return true;
}