summaryrefslogtreecommitdiff
path: root/plugins/LangMan/src/langpack.cpp
blob: 1fb31928de3c942ed81a3ca1f9d928d69dbb5a6a (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
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
/*

'Language Pack Manager'-Plugin for Miranda IM

Copyright (C) 2005-2007 H. Herkenrath

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 (LangMan-License.txt); if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/

#include "common.h"

/************************* Load ***************************************/

static void TrimString(char *str)
{
	int len, start;
	len = lstrlenA(str);
	while (str[0]!='\0' && (unsigned char)str[len-1] <= ' ') str[--len] = 0;
	for (start = 0; str[start] && (unsigned char)str[start] <= ' '; ++start);
	MoveMemory(str, str+start, len-start+1);
}

static BOOL IsEmpty(const char *str)
{
	int i;
	for(i = 0;str[i]!='\0';i++)
		if (str[i]!=' ' && str[i]!='\r' && str[i]!='\n')
			return FALSE;
	return TRUE;
}

static void CleanupLanguage(char *szLanguage)
{
	char *p;
	/* remove any appended ' (default)' */
	p = strstr(szLanguage, " (default)");
	if (p!=NULL) *p = '\0';
}

static void CleanupAuthors(char *szAuthors)
{
	char *p, *p2;
	/* remove trailing dot (if any) */
	p = &szAuthors[lstrlenA(szAuthors)-1];
	if (*p == '.') *p = '\0';
	/* remove any extra info in parentheses, which is ok
	 * but makes the list very long for some packs */
	for (;;) {
		p = strchr(szAuthors, '(');
		p2 = strchr(szAuthors, ')');
		if (p == NULL || p2 == NULL) {
			p = strchr(szAuthors, '[');
			p2 = strchr(szAuthors, ']');
			if (p == NULL || p2 == NULL) break;
		}
		if (*(p-1) == ' ') --p;
		MoveMemory(p, p2+1, lstrlenA(p2+1)+1);
	}
}

static void CleanupEmail(char *szAuthorEmail)
{
	char c, *p, *pAt;
	/* replace ' dot ' with '.' (may be removed) */
	p = strstr(szAuthorEmail, " dot ");
	if (p!=NULL) {
		*p = '.';
		MoveMemory(p+1, p+5, lstrlenA(p+5)+1);
	}
	/* also allow ' at ' instead of '@' for obfuscation */
	p = strstr(szAuthorEmail, " at ");
	if (p!=NULL) {
		*p = '@';
		MoveMemory(p+1, p+4, lstrlenA(p+4)+1);
	}
	/* is valid? */
	pAt = strchr(szAuthorEmail, '@');
	if (pAt == NULL) {
		szAuthorEmail[0] = '\0';
		return;
	}
	/* strip-off extra text except exactly one email address
	 * this is needed as a click on the email addres brings up the mail client */
	for(c = ' ';;c = ',') {
		p = strchr(pAt, c);
		if (p!=NULL) *p = '\0';
		p = strrchr(szAuthorEmail, c);
		if (p!=NULL) MoveMemory(szAuthorEmail, p+1, lstrlenA(p+1)+1);
		if (c == ',') break;
	}
	p = strstr(szAuthorEmail, "__");
	if (p!=NULL) MoveMemory(szAuthorEmail, p+2, lstrlenA(p+2)+1);
	/* lower case */
	CharLowerA(szAuthorEmail);
	/* 'none' specified */
	if (!lstrcmpiA(szAuthorEmail, "none")) szAuthorEmail[0] = '\0';
}

static void CleanupLastModifiedUsing(char *szLastModifiedUsing, int nSize)
{
	char *p;
	/* remove 'Unicode', as it doesn't matter */
	p = strstr(szLastModifiedUsing, " Unicode");
	if (p!=NULL) MoveMemory(p, p+8, lstrlenA(p+8)+1);
	/* use 'Miranda IM' instead of 'Miranda' */
	p = strstr(szLastModifiedUsing, "Miranda");
	if (p!=NULL && strncmp(p+7, " IM", 3)) {
		MoveMemory(p+10, p+7, lstrlenA(p+7)+1);
		CopyMemory(p+7, " IM", 3);
	}
	/* use 'Plugin' instead of 'plugin' */
	p = strstr(szLastModifiedUsing, " plugin");
	if (p!=NULL) CopyMemory(p, " Plugin", 7);
	/* remove 'v' prefix */
	p = strstr(szLastModifiedUsing, " v0.");
	if (p!=NULL) MoveMemory(p+1, p+2, lstrlenA(p+2)+1);
	/* default if empty */
	if (!szLastModifiedUsing[0]) {
		lstrcpynA(szLastModifiedUsing, MIRANDANAME" ", nSize);
		CallService(MS_SYSTEM_GETVERSIONTEXT, nSize-lstrlenA(szLastModifiedUsing), (LPARAM)szLastModifiedUsing+lstrlenA(szLastModifiedUsing));
	}
}

// pack struct should be initialized to zero before call
// pack->szFileName needs to be filled in before call
static BOOL LoadPackData(LANGPACK_INFO *pack, BOOL fEnabledPacks, const char *pszFileVersionHeader)
{
	FILE *fp;
	TCHAR szFileName[MAX_PATH];
	char line[4096], *pszColon, *buf;
	char szLanguageA[64]; /* same size as pack->szLanguage */
	/*
		Miranda Language Pack Version 1
		Language: (optional)
		Locale: 0809
		Authors: Miranda NG Development Team (multiple tags allowed)
		Author-email: project-info at miranda-ng.org (" at " instead of "@" allowed)
		Last-Modified-Using: Miranda IM 0.7
		Plugins-included: (multiple tags allowed)
		X-FLName: name as used on the file listing (non-standard extension)
		X-Version: 1.2.3.4 (non-standard extension)
		see 'LangMan-Translation.txt' for some header quidelines 
	*/
	if ( !GetPackPath( szFileName, SIZEOF(szFileName), fEnabledPacks, pack->szFileName))
		return FALSE;

	fp = _tfopen(szFileName, _T("rt"));
	if (fp  ==  NULL)
		return FALSE;

	fgets(line, sizeof(line), fp);
	TrimString(line);
	buf = line;

	if (strlen(line) >=  3 && line[0]  ==  '\xef' && line[1]  ==  '\xbb' && line[2]  ==  '\xbf') {
		pack->codepage = CP_UTF8;
		buf += 3;
	}

	if ( lstrcmpA(buf, pszFileVersionHeader )) {
		fclose(fp);
		return FALSE;
	}
	pack->flags = LPF_NOLOCALE;
	szLanguageA[0] = '\0';
	while( !feof( fp )) {
		if ( fgets(line, sizeof(line), fp)  ==  NULL) break;
		TrimString(line);
		if ( IsEmpty(line) || line[0] == ';' || line[0] == '\0') continue;
		if ( line[0] == '[' ) break;
		pszColon = strchr(line, ':');
		if ( pszColon  ==  NULL ) continue;
		*pszColon = '\0';
		TrimString(pszColon+1);
		if ( !lstrcmpA(line, "Language") && !pack->szLanguage[0] )
			lstrcpynA(szLanguageA, pszColon+1, sizeof(szLanguageA)); /* buffer safe */
		else if ( !lstrcmpA(line, "Last-Modified-Using") && !pack->szLastModifiedUsing[0] )
			lstrcpynA(pack->szLastModifiedUsing, pszColon+1, sizeof(pack->szLastModifiedUsing)); /* buffer safe */
		else if ( !lstrcmpA(line, "Authors")) {
			buf = pack->szAuthors+lstrlenA(pack->szAuthors); /* allow multiple tags */
			if ((sizeof(pack->szAuthors)-lstrlenA(pack->szAuthors))>0) /* buffer safe */
				mir_snprintf(buf, sizeof(pack->szAuthors)-lstrlenA(pack->szAuthors), (pack->szAuthors[0] == '\0')?"%s":" %s", pszColon+1);
		} else if ( !lstrcmpA(line, "Author-email") && !pack->szAuthorEmail[0])
			lstrcpynA(pack->szAuthorEmail, pszColon+1, sizeof(pack->szAuthorEmail)); /* buffer safe */
		else if ( !lstrcmpA(line, "Locale") && (pack->flags & LPF_NOLOCALE)) {
			pack->Locale = MAKELCID((USHORT)strtol(pszColon+1, NULL, 16), SORT_DEFAULT);
			if (pack->Locale) pack->flags &= ~LPF_NOLOCALE;
		}
		else if ( !lstrcmpA(line, "Plugins-included")) {
			buf = pack->szPluginsIncluded + lstrlenA(pack->szPluginsIncluded); /* allow multiple tags */
			if (( sizeof(pack->szPluginsIncluded)-lstrlenA(pack->szPluginsIncluded)) > 0 ) /* buffer safe */
				mir_snprintf(buf, sizeof(pack->szPluginsIncluded)-lstrlenA(pack->szPluginsIncluded), (pack->szPluginsIncluded[0] == '\0')?"%s":", %s", CharLowerA(pszColon+1));
		}
		else if ( !lstrcmpA(line, "X-Version") && !pack->szVersion[0] )
			lstrcpynA(pack->szVersion, pszColon+1, sizeof(pack->szVersion)); /* buffer safe */
		else if ( !lstrcmpA(line, "X-FLName") && !pack->szFLName[0] )
			lstrcpynA(pack->szFLName, pszColon+1, sizeof(pack->szFLName)); /* buffer safe */
	}
	CleanupLanguage(szLanguageA);
	CleanupAuthors(pack->szAuthors);
	CleanupEmail(pack->szAuthorEmail);
	CleanupLastModifiedUsing(pack->szLastModifiedUsing, sizeof(pack->szLastModifiedUsing));
	/* codepage */
	if (!(pack->flags&LPF_NOLOCALE))
		if (GetLocaleInfoA(pack->Locale, LOCALE_IDEFAULTANSICODEPAGE, line, 6))
			pack->codepage = (WORD)atoi(line); /* CP_ACP on error */
	/* language */

	MultiByteToWideChar(pack->codepage, 0, szLanguageA, -1, pack->szLanguage, SIZEOF(pack->szLanguage));

	/* ensure the pack always has a language name */
	if (!pack->szLanguage[0] && !GetLocaleInfo(pack->Locale, LOCALE_SENGLANGUAGE, pack->szLanguage, SIZEOF(pack->szLanguage))) {
		TCHAR *p;
		lstrcpyn(pack->szLanguage, pack->szFileName, SIZEOF(pack->szLanguage)); /* buffer safe */
		p = _tcsrchr(pack->szLanguage, _T('.'));
		if (p!=NULL) *p = '\0';
	}
	/* ensure the pack always has a filelisting name */
	if (!pack->szFLName[0])
		lstrcatA(lstrcpyA(pack->szFLName, szLanguageA), " Language Pack"); /* buffer safe */
	fclose(fp);
	return TRUE;
}

/************************* Enum ***************************************/

BOOL GetPackPath(TCHAR *pszPath, int nSize, BOOL fEnabledPacks, const TCHAR *pszFile)
{
	TCHAR *p;
	/* main path */
	if (!GetModuleFileName(NULL, pszPath, nSize)) return FALSE;
	p = _tcsrchr(pszPath, _T('\\'));
	if (p!=NULL) *(p+1) = _T('\0');
	/* subdirectory */
	if (!fEnabledPacks) {
		if (nSize<(lstrlen(pszPath)+10)) return FALSE;
		lstrcat(pszPath, _T("Plugins\\Language\\"));
	}
	/* file name */
	if (pszFile!=NULL) {
		if (nSize<(lstrlen(pszFile)+11)) return FALSE;
		lstrcat(pszPath, pszFile);
	}
	return TRUE;
}

// callback is allowed to be NULL
// returns TRUE if any pack exists except default
BOOL EnumPacks(ENUM_PACKS_CALLBACK callback, const TCHAR *pszFilePattern, const char *pszFileVersionHeader, BOOL fEnglishDefault, WPARAM wParam, LPARAM lParam)
{
	BOOL fPackFound = FALSE;
	BOOL res = FALSE;
	LANGPACK_INFO pack;
	WIN32_FIND_DATA wfd;	
	HANDLE hFind;

	/* enabled packs */
	if (GetPackPath(pack.szFileName, SIZEOF(pack.szFileName), TRUE, pszFilePattern)) {
		hFind = FindFirstFile(pack.szFileName, &wfd);
		if (hFind!=INVALID_HANDLE_VALUE) {
			do {
				if (wfd.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY) continue;
				if ((lstrlen(wfd.cFileName)<4) || wfd.cFileName[lstrlen(wfd.cFileName)-4]!=_T('.')) continue;
				/* get data */
				ZeroMemory(&pack, sizeof(pack));
				lstrcpy(pack.szFileName, CharLower(wfd.cFileName)); /* buffer safe */
				if (LoadPackData(&pack, TRUE, pszFileVersionHeader)) {
					pack.ftFileDate = wfd.ftLastWriteTime;
					/* enabled? */
					if (!fPackFound) pack.flags |= LPF_ENABLED;
					fPackFound = TRUE;
					/* callback */
					if (callback!=NULL) res = callback(&pack, wParam, lParam);
					if (!res) { FindClose(hFind); return FALSE; }
				}
			} while(FindNextFile(hFind, &wfd));
			FindClose(hFind);
		}
	}

	/* default: English (GB) */
	if (fEnglishDefault && callback!=NULL) {
		ZeroMemory(&pack, sizeof(pack));
		pack.Locale = LOCALE_USER_DEFAULT; /* miranda uses default locale in this case */
		lstrcpy(pack.szLanguage, _T("English (default)")); /* buffer safe */
		lstrcpyA(pack.szAuthors, "Miranda NG Development Team"); /* buffer safe */
		lstrcpyA(pack.szAuthorEmail, "project-info at miranda-ng.org"); /* buffer safe */
		CleanupEmail(pack.szAuthorEmail); /* correct " at " */
		CleanupLastModifiedUsing(pack.szLastModifiedUsing, sizeof(pack.szLastModifiedUsing));
		/* file date */
		if (GetModuleFileName(NULL, pack.szFileName, SIZEOF(pack.szFileName))) {
			HANDLE hFile;
			hFile = CreateFile(pack.szFileName, 0, FILE_SHARE_READ, 0, OPEN_EXISTING, 0, 0);
			if (hFile!=INVALID_HANDLE_VALUE) {
				GetFileTime(hFile, NULL, NULL, &pack.ftFileDate);
				CloseHandle(hFile);
			}
		}
		pack.flags = LPF_NOLOCALE|LPF_DEFAULT;
		if (!fPackFound) pack.flags |= LPF_ENABLED;
		/* callback */
		if (!callback(&pack, wParam, lParam)) return FALSE;
	}

	/* disabled packs */
	if (GetPackPath(pack.szFileName, SIZEOF(pack.szFileName), FALSE, pszFilePattern)) {
		hFind = FindFirstFile(pack.szFileName, &wfd);
		if (hFind!=INVALID_HANDLE_VALUE) {
			do {
				if (wfd.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY) continue;
				if (lstrlen(wfd.cFileName)<4 || wfd.cFileName[lstrlen(wfd.cFileName)-4]!=_T('.')) continue;
				/* get data */
				ZeroMemory(&pack, sizeof(pack));
				lstrcpy(pack.szFileName, CharLower(wfd.cFileName)); /* buffer safe */
				if (LoadPackData(&pack, FALSE, pszFileVersionHeader)) {
					pack.ftFileDate = wfd.ftLastWriteTime;
					fPackFound = TRUE;
					/* callback */
					if (callback!=NULL) res = callback(&pack, wParam, lParam);
					if (!res) { FindClose(hFind); return FALSE; }
				}
			} while(FindNextFile(hFind, &wfd));
			FindClose(hFind);
		}
	}
	return fPackFound;
}

BOOL IsPluginIncluded(const LANGPACK_INFO *pack, char *pszFileBaseName)
{
	char *p;
	if (!lstrcmpiA(pszFileBaseName, "png2dib") || !lstrcmpiA(pszFileBaseName, "loadavatars"))
		return TRUE; /* workaround: does not need no translation */
	for(p = (char*)pack->szPluginsIncluded;;) {
		p = strstr(p, CharLowerA(pszFileBaseName));
		if (p == NULL) return FALSE;
		if (p == pack->szPluginsIncluded || *(p-1) == ' ' || *(p-1) == ',') {
			p+=lstrlenA(pszFileBaseName)+1;
			if (*p == ',' || *p == ' ' || *p == 0) return TRUE;
		}
		else p+=lstrlenA(pszFileBaseName)+1;
	}
}

/************************* Switch *************************************/

BOOL EnablePack(const LANGPACK_INFO *pack, const TCHAR *pszFilePattern)
{
	TCHAR szFrom[MAX_PATH], szDest[MAX_PATH];
	HANDLE hFind;
	WIN32_FIND_DATA wfd;
	
	/* disable previous pack */
	if (GetPackPath(szFrom, SIZEOF(szFrom), TRUE, pszFilePattern)) {
		hFind = FindFirstFile(szFrom, &wfd);
		if (hFind!=INVALID_HANDLE_VALUE) {
			do {
				if (wfd.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY) continue;
				if (lstrlen(wfd.cFileName)<4 || wfd.cFileName[lstrlen(wfd.cFileName)-4]!=_T('.')) continue;
				/* ensure dir exists */
				if (GetPackPath(szFrom, SIZEOF(szFrom), FALSE, NULL))
					CreateDirectory(szFrom, NULL);
				/* move file */
				if (GetPackPath(szFrom, SIZEOF(szFrom), TRUE, wfd.cFileName))
					if (GetPackPath(szDest, SIZEOF(szDest), FALSE, wfd.cFileName))
						if (!MoveFile(szFrom, szDest) && GetLastError() == ERROR_ALREADY_EXISTS) {
							DeleteFile(szDest);
							MoveFile(szFrom, szDest);
						}
				break;
			} while(FindNextFile(hFind, &wfd));
			FindClose(hFind);
		}
	}

	/* enable current pack */
	if (pack->flags&LPF_DEFAULT) return TRUE;
	if (GetPackPath(szFrom, SIZEOF(szFrom), FALSE, pack->szFileName))
		if (GetPackPath(szDest, SIZEOF(szDest), TRUE, pack->szFileName)) 
			return MoveFile(szFrom, szDest);
	return FALSE;
}

void CorrectPacks(const TCHAR *pszFilePattern, BOOL fDisableAll)
{
	TCHAR szFrom[MAX_PATH], szDest[MAX_PATH], szDir[MAX_PATH], *pszFile;
	BOOL fDirCreated = FALSE;
	HANDLE hFind;
	WIN32_FIND_DATA wfd;

	/* main path */
	if (!GetModuleFileName(NULL, szDir, SIZEOF(szDir))) return;
	pszFile = _tcsrchr(szDir, _T('\\'));
	if (pszFile!=NULL) *pszFile = _T('\0');

	/* move wrongly placed packs from 'Plugins' to 'Language' */
	mir_sntprintf(szFrom, SIZEOF(szFrom), _T("%s\\Plugins\\%s"), szDir, pszFilePattern);
	hFind = FindFirstFile(szFrom, &wfd);
	if (hFind!=INVALID_HANDLE_VALUE) {
		do {
			if (wfd.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY) continue;
			if (lstrlen(wfd.cFileName)<4 || wfd.cFileName[lstrlen(wfd.cFileName)-4]!=_T('.')) continue;
			/* ensure dir exists */
			if (!fDirCreated && GetPackPath(szFrom, SIZEOF(szFrom), FALSE, NULL))
				fDirCreated = CreateDirectory(szFrom, NULL);
			/* move file */
			if (GetPackPath(szDest, SIZEOF(szDest), FALSE, wfd.cFileName)) {
				mir_sntprintf(szFrom, SIZEOF(szFrom), _T("%s\\Plugins\\%s"), szDir, wfd.cFileName);
				if (!MoveFile(szFrom, szDest) && GetLastError() == ERROR_ALREADY_EXISTS) {
					DeleteFile(szDest);
					MoveFile(szFrom, szDest);
				}
			}
		} while(FindNextFile(hFind, &wfd));
		FindClose(hFind);
	}

	/* disable all packs except one */
	if (GetPackPath(szFrom, SIZEOF(szFrom), TRUE, pszFilePattern)) {
		hFind = FindFirstFile(szFrom, &wfd);
		if (hFind!=INVALID_HANDLE_VALUE) {
			do {
				if (wfd.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY) continue;
				if (lstrlen(wfd.cFileName)<4 || wfd.cFileName[lstrlen(wfd.cFileName)-4]!=_T('.')) continue;
				/* skip first file */
				if (!fDisableAll) { fDisableAll = TRUE; continue; }
				/* ensure dir exists */
				if (!fDirCreated && GetPackPath(szFrom, SIZEOF(szFrom), FALSE, NULL))
					fDirCreated = CreateDirectory(szFrom, NULL);
				/* move file */
				if (GetPackPath(szFrom, SIZEOF(szFrom), TRUE, wfd.cFileName))
					if (GetPackPath(szDest, SIZEOF(szDest), FALSE, wfd.cFileName)) {
						if (!MoveFile(szFrom, szDest) && GetLastError() == ERROR_ALREADY_EXISTS) {
							DeleteFile(szDest);
							MoveFile(szFrom, szDest);
						}
					}
			} while(FindNextFile(hFind, &wfd));
			FindClose(hFind);
		}
	}
}