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
|
/* Multi-language support.
*
* (C) 2003-2010 Anope Team
* Contact us at team@anope.org
*
* Please read COPYING and README for further details.
*
* Based on the original code of Epona by Lara.
* Based on the original code of Services by Andy Church.
*/
#include "services.h"
#include "language.h"
/*************************************************************************/
/* The list of lists of messages. */
char **langtexts[NUM_LANGS];
/* The list of names of languages. */
char *langnames[NUM_LANGS];
/* Indexes of available languages: */
int langlist[NUM_LANGS];
/* Order in which languages should be displayed: (alphabetical) */
static int langorder[NUM_LANGS] = {
LANG_EN_US, /* English (US) */
LANG_FR, /* French */
LANG_DE, /* German */
LANG_IT, /* Italian */
LANG_JA_JIS, /* Japanese (JIS encoding) */
LANG_JA_EUC, /* Japanese (EUC encoding) */
LANG_JA_SJIS, /* Japanese (SJIS encoding) */
LANG_PT, /* Portugese */
LANG_ES, /* Spanish */
LANG_TR, /* Turkish */
LANG_CAT, /* Catalan */
LANG_GR, /* Greek */
LANG_NL, /* Dutch */
LANG_RU, /* Russian */
LANG_HUN, /* Hungarian */
LANG_PL, /* Polish */
};
/*************************************************************************/
/* Load a language file. */
static int read_int32(int32 *ptr, FILE *f)
{
int a = fgetc(f);
int b = fgetc(f);
int c = fgetc(f);
int d = fgetc(f);
if (a == EOF || b == EOF || c == EOF || d == EOF)
return -1;
*ptr = a << 24 | b << 16 | c << 8 | d;
return 0;
}
static void load_lang(int index, const char *filename)
{
char buf[256];
FILE *f;
int32 num, i;
Log(LOG_DEBUG) << "Loading language " << index << " from file `languages/" << filename << "'";
snprintf(buf, sizeof(buf), "languages/%s", filename);
#ifndef _WIN32
const char *mode = "r";
#else
const char *mode = "rb";
#endif
if (!(f = fopen(buf, mode)))
{
throw CoreException("Failed to load language " + stringify(index) + " (" + stringify(filename) + ")");
}
else if (read_int32(&num, f) < 0)
{
Log() << "Failed to read number of strings for language " << index << "(" << filename << ")";
return;
}
else if (num != NUM_STRINGS)
Log() << "Warning: Bad number of strings (" << num << " , wanted " << NUM_STRINGS << ") for language " << index << " (" << filename << ")";
langtexts[index] = static_cast<char **>(scalloc(sizeof(char *), NUM_STRINGS));
if (num > NUM_STRINGS)
num = NUM_STRINGS;
for (i = 0; i < num; ++i)
{
int32 pos, len;
fseek(f, i * 8 + 4, SEEK_SET);
if (read_int32(&pos, f) < 0 || read_int32(&len, f) < 0)
{
Log() << "Failed to read entry " << i << " in language " << index << " (" << filename << ") TOC";
while (--i >= 0)
{
if (langtexts[index][i])
free(langtexts[index][i]); // XXX
}
free(langtexts[index]);
langtexts[index] = NULL;
return;
}
if (!len)
langtexts[index][i] = NULL;
else if (len >= 65536)
{
Log() << "Entry " << i << " in language " << index << " (" << filename << ") is too long (over 64k) -- corrupt TOC?";
while (--i >= 0)
{
if (langtexts[index][i])
free(langtexts[index][i]); // XXX
}
free(langtexts[index]);
langtexts[index] = NULL;
return;
}
else if (len < 0)
{
Log() << "Entry " << i << " in language " << index << " (" << filename << ") has negative length -- corrupt TOC?";
while (--i >= 0)
{
if (langtexts[index][i])
free(langtexts[index][i]); // XXX
}
free(langtexts[index]);
langtexts[index] = NULL;
return;
}
else
{
langtexts[index][i] = static_cast<char *>(malloc(len + 1));
fseek(f, pos, SEEK_SET);
if (fread(langtexts[index][i], 1, len, f) != len)
{
Log() << "Failed to read string " << i << " in language " << index << "(" << filename << ")";
while (--i >= 0)
{
if (langtexts[index][i])
free(langtexts[index][i]);
}
free(langtexts[index]);
langtexts[index] = NULL;
return;
}
langtexts[index][i][len] = 0;
}
}
fclose(f);
}
/*************************************************************************/
/* Replace all %M's with "/msg " or "/" */
void lang_sanitize()
{
int i = 0, j = 0;
int len = 0;
char tmp[2000];
char *newstr = NULL;
for (i = 0; i < NUM_LANGS; ++i)
{
for (j = 0; j < NUM_STRINGS; ++j)
{
if (strstr(langtexts[i][j], "%R"))
{
len = strlen(langtexts[i][j]);
strscpy(tmp, langtexts[i][j], sizeof(tmp));
if (Config->UseStrictPrivMsg)
strnrepl(tmp, sizeof(tmp), "%R", "/");
else
strnrepl(tmp, sizeof(tmp), "%R", "/msg ");
newstr = strdup(tmp);
free(langtexts[i][j]); // XXX
langtexts[i][j] = newstr;
}
}
}
}
/* Initialize list of lists. */
void lang_init()
{
int i, j, n = 0;
load_lang(LANG_CAT, "cat");
load_lang(LANG_DE, "de");
load_lang(LANG_EN_US, "en_us");
load_lang(LANG_ES, "es");
load_lang(LANG_FR, "fr");
load_lang(LANG_GR, "gr");
load_lang(LANG_PT, "pt");
load_lang(LANG_TR, "tr");
load_lang(LANG_IT, "it");
load_lang(LANG_NL, "nl");
load_lang(LANG_RU, "ru");
load_lang(LANG_HUN, "hun");
load_lang(LANG_PL, "pl");
for (i = 0; i < NUM_LANGS; ++i)
{
if (langtexts[langorder[i]] != NULL)
{
langnames[langorder[i]] = langtexts[langorder[i]][LANG_NAME];
langlist[n++] = langorder[i];
for (j = 0; j < NUM_STRINGS; ++j)
{
if (!langtexts[langorder[i]][j])
langtexts[langorder[i]][j] = langtexts[DEF_LANGUAGE][j];
if (!langtexts[langorder[i]][j])
langtexts[langorder[i]][j] = langtexts[LANG_EN_US][j];
}
}
}
while (n < NUM_LANGS)
langlist[n++] = -1;
/* Not what I intended to do, but these services are so archaïc
* that it's difficult to do more. */
if ((Config->NSDefLanguage = langlist[Config->NSDefLanguage]) < 0)
Config->NSDefLanguage = DEF_LANGUAGE;
if (!langtexts[DEF_LANGUAGE])
throw CoreException("Unable to load default language");
for (i = 0; i < NUM_LANGS; ++i)
{
if (!langtexts[i])
langtexts[i] = langtexts[DEF_LANGUAGE];
}
lang_sanitize();
}
/*************************************************************************/
/*************************************************************************/
/* Format a string in a strftime()-like way, but heed the user's language
* setting for month and day names. The string stored in the buffer will
* always be null-terminated, even if the actual string was longer than the
* buffer size.
* Assumption: No month or day name has a length (including trailing null)
* greater than BUFSIZE.
*/
int strftime_lang(char *buf, int size, User *u, int format, struct tm *tm)
{
int language = u && u->Account() ? u->Account()->language : Config->NSDefLanguage;
char tmpbuf[BUFSIZE], buf2[BUFSIZE];
char *s;
int i, ret;
if (!tm)
return 0;
strscpy(tmpbuf, langtexts[language][format], sizeof(tmpbuf));
if ((s = langtexts[language][STRFTIME_DAYS_SHORT]))
{
for (i = 0; i < tm->tm_wday; ++i)
s += strcspn(s, "\n") + 1;
i = strcspn(s, "\n");
strncpy(buf2, s, i);
buf2[i] = 0;
strnrepl(tmpbuf, sizeof(tmpbuf), "%a", buf2);
}
if ((s = langtexts[language][STRFTIME_DAYS_LONG]))
{
for (i = 0; i < tm->tm_wday; ++i)
s += strcspn(s, "\n") + 1;
i = strcspn(s, "\n");
strncpy(buf2, s, i);
buf2[i] = 0;
strnrepl(tmpbuf, sizeof(tmpbuf), "%A", buf2);
}
if ((s = langtexts[language][STRFTIME_MONTHS_SHORT]))
{
for (i = 0; i < tm->tm_mon; ++i)
s += strcspn(s, "\n") + 1;
i = strcspn(s, "\n");
strncpy(buf2, s, i);
buf2[i] = 0;
strnrepl(tmpbuf, sizeof(tmpbuf), "%b", buf2);
}
if ((s = langtexts[language][STRFTIME_MONTHS_LONG]))
{
for (i = 0; i < tm->tm_mon; ++i)
s += strcspn(s, "\n") + 1;
i = strcspn(s, "\n");
strncpy(buf2, s, i);
buf2[i] = 0;
strnrepl(tmpbuf, sizeof(tmpbuf), "%B", buf2);
}
ret = strftime(buf, size, tmpbuf, tm);
if (ret == size)
buf[size - 1] = 0;
return ret;
}
/*************************************************************************/
/*************************************************************************/
/* Send a syntax-error message to the user. */
void syntax_error(const Anope::string &service, User *u, const Anope::string &command, int msgnum)
{
const char *str;
if (!u)
return;
str = getstring(u, msgnum);
notice_lang(service, u, SYNTAX_ERROR, str);
notice_lang(service, u, MORE_INFO, service.c_str(), command.c_str());
}
const char *getstring(NickAlias *na, int index)
{
// Default to config
int langidx = Config->NSDefLanguage;
// If they are registered (na->nc), and NOT forbidden
if (na && na->nc && !na->HasFlag(NS_FORBIDDEN))
langidx = na->nc->language; // set language to their nickcore's language
return langtexts[langidx][index];
}
const char *getstring(const NickCore *nc, int index)
{
// Default to config
int langidx = Config->NSDefLanguage;
if (nc)
langidx = nc->language;
return langtexts[langidx][index];
}
const char *getstring(const User *u, int index)
{
return getstring(u->Account(), index);
}
const char *getstring(int index)
{
// Default to config
int langidx = Config->NSDefLanguage;
return langtexts[langidx][index];
}
/*************************************************************************/
|