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
|
#include "templates.h"
#include <stdio.h>
#include <stdlib.h>
#include <tchar.h>
extern "C"
{
#include <newpluginapi.h>
#include <time.h>
#include <win2k.h>
#include <m_system.h>
#include <m_options.h>
#include <m_langpack.h>
#include <m_database.h>
#include <m_utils.h>
#include <m_protocols.h>
#include <m_notify.h>
#include "../utils/mir_memory.h"
}
const char *defaultVariables[] = { "%title%", "%text%" };
const WCHAR *defaultVariablesW[] = { L"%title%", L"%text%" };
const char *defaultVariableDescriptions[] = { "Notification Title", "Notification Text" };
const WCHAR *defaultVariableDescriptionsW[] = { L"Notification Title", L"Notification Text" };
#define MAX_REGS(_A_) ( sizeof(_A_) / sizeof(_A_[0]) )
#define DATA_BLOCK 128
// ASCII VERSIONS ///////////////////////////////////////////////////////////////////////
struct StringHelper
{
char *text;
size_t allocated;
size_t used;
};
int CopyData(StringHelper *str, const char *text, size_t len)
{
if (len == 0)
return 0;
if (text == NULL)
return 0;
size_t totalSize = str->used + len + 1;
if (totalSize > str->allocated)
{
totalSize += DATA_BLOCK - (totalSize % DATA_BLOCK);
if (str->text != NULL)
{
char *tmp = (char *) mir_realloc(str->text, sizeof(char) * totalSize);
if (tmp == NULL)
{
mir_free(str->text);
return -1;
}
str->text = tmp;
}
else
{
str->text = (char *) mir_alloc(sizeof(char) * totalSize);
if (str->text == NULL)
{
return -2;
}
}
str->allocated = totalSize;
}
memmove(&str->text[str->used], text, sizeof(char) * len);
str->used += len;
str->text[str->used] = '\0';
return 0;
}
char * ParseText(const char *text,
const char **variables, size_t variablesSize,
const char **data, size_t dataSize)
{
size_t length = strlen(text);
size_t nextPos = 0;
StringHelper ret = {0};
// length - 1 because a % in last char will be a % and point
for (size_t i = 0 ; i < length - 1 ; i++)
{
if (text[i] == '%')
{
bool found = false;
if (CopyData(&ret, &text[nextPos], i - nextPos))
return NULL;
if (text[i + 1] == '%')
{
if (CopyData(&ret, "%", 1))
return NULL;
i++;
found = true;
}
else
{
size_t size = min(variablesSize, dataSize);
// See if can find it
for(size_t j = 0 ; j < size ; j++)
{
size_t vlen = strlen(variables[j]);
if (strnicmp(&text[i], variables[j], vlen) == 0)
{
if (CopyData(&ret, data[j], strlen(data[j])))
return NULL;
i += vlen - 1;
found = true;
break;
}
}
}
if (found)
nextPos = i + 1;
else
nextPos = i;
}
}
if (nextPos < length)
if (CopyData(&ret, &text[nextPos], length - nextPos))
return NULL;
return ret.text;
}
// UNICODE VERSIONS /////////////////////////////////////////////////////////////////////
struct StringHelperW
{
WCHAR *text;
size_t allocated;
size_t used;
};
int CopyDataW(StringHelperW *str, const WCHAR *text, size_t len)
{
if (len == 0)
return 0;
if (text == NULL)
return 0;
size_t totalSize = str->used + len + 1;
if (totalSize > str->allocated)
{
totalSize += DATA_BLOCK - (totalSize % DATA_BLOCK);
if (str->text != NULL)
{
WCHAR *tmp = (WCHAR *) mir_realloc(str->text, sizeof(WCHAR) * totalSize);
if (tmp == NULL)
{
mir_free(str->text);
return -1;
}
str->text = tmp;
}
else
{
str->text = (WCHAR *) mir_alloc(sizeof(WCHAR) * totalSize);
if (str->text == NULL)
{
return -2;
}
}
str->allocated = totalSize;
}
memmove(&str->text[str->used], text, sizeof(WCHAR) * len);
str->used += len;
str->text[str->used] = '\0';
return 0;
}
WCHAR * ParseTextW(const WCHAR *text,
const WCHAR **variables, size_t variablesSize,
const WCHAR **data, size_t dataSize)
{
size_t length = lstrlenW(text);
size_t nextPos = 0;
StringHelperW ret = {0};
// length - 1 because a % in last char will be a % and point
for (size_t i = 0 ; i < length - 1 ; i++)
{
if (text[i] == L'%')
{
bool found = false;
if (CopyDataW(&ret, &text[nextPos], i - nextPos))
return NULL;
if (text[i + 1] == L'%')
{
if (CopyDataW(&ret, L"%", 1))
return NULL;
i++;
found = true;
}
else
{
size_t size = min(variablesSize, dataSize);
// See if can find it
for(size_t j = 0 ; j < size ; j++)
{
size_t vlen = lstrlenW(variables[j]);
if (_wcsnicmp(&text[i], variables[j], vlen) == 0)
{
if (CopyDataW(&ret, data[j], lstrlenW(data[j])))
return NULL;
i += vlen - 1;
found = true;
break;
}
}
}
if (found)
nextPos = i + 1;
else
nextPos = i;
}
}
if (nextPos < length)
if (CopyDataW(&ret, &text[nextPos], length - nextPos))
return NULL;
return ret.text;
}
// INTERFACE ////////////////////////////////////////////////////////////////////////////
// In future maybe pre-parse here
void MNotifySetTemplate(HANDLE notifyORtype, const char *name, const char *value)
{
MNotifySetString(notifyORtype, name, value);
}
void MNotifySetWTemplate(HANDLE notifyORtype, const char *name, const WCHAR *value)
{
MNotifySetWString(notifyORtype, name, value);
}
// Well, why not?
const char *MNotifyGetTemplate(HANDLE notifyORtype, const char *name, const char *defValue)
{
return MNotifyGetString(notifyORtype, name, defValue);
}
const WCHAR *MNotifyGetWTemplate(HANDLE notifyORtype, const char *name, const WCHAR *defValue)
{
return MNotifyGetWString(notifyORtype, name, defValue);
}
// You must free the return with mir_sys_free
char *MNotifyGetParsedTemplate(HANDLE notifyORtype, const char *name, const char *defValue)
{
const char *temp = MNotifyGetString(notifyORtype, name, defValue);
if (MNotifyHasVariables(notifyORtype))
{
const char ** vars = (const char **) MNotifyGetDWord(notifyORtype, NFOPT_VARIABLES_STRS, NULL);
size_t varsSize = (size_t) MNotifyGetDWord(notifyORtype, NFOPT_VARIABLES_SIZE, 0);
const char ** data = (const char **) MNotifyGetDWord(notifyORtype, NFOPT_DATA_STRS, NULL);
size_t dataSize = (size_t) MNotifyGetDWord(notifyORtype, NFOPT_DATA_SIZE, 0);
return ParseText(temp, vars, varsSize, data, dataSize);
}
else
{
// Default values
const char * data[MAX_REGS(defaultVariables)];
data[0] = MNotifyGetString(notifyORtype, NFOPT_TITLE, NULL);
data[1] = MNotifyGetString(notifyORtype, NFOPT_TEXT, NULL);
return ParseText(temp, defaultVariables, MAX_REGS(defaultVariables), data, MAX_REGS(defaultVariables));
}
}
WCHAR *MNotifyGetWParsedTemplate(HANDLE notifyORtype, const char *name, const WCHAR *defValue)
{
const WCHAR *temp = MNotifyGetWString(notifyORtype, name, defValue);
if (MNotifyHasWVariables(notifyORtype))
{
const WCHAR ** vars = (const WCHAR **) MNotifyGetDWord(notifyORtype, NFOPT_VARIABLES_STRSW, NULL);
size_t varsSize = (size_t) MNotifyGetDWord(notifyORtype, NFOPT_VARIABLES_SIZE, 0);
const WCHAR ** data = (const WCHAR **) MNotifyGetDWord(notifyORtype, NFOPT_DATA_STRSW, NULL);
size_t dataSize = (size_t) MNotifyGetDWord(notifyORtype, NFOPT_DATA_SIZE, 0);
return ParseTextW(temp, vars, varsSize, data, dataSize);
}
else
{
// Default values
const WCHAR * data[MAX_REGS(defaultVariablesW)];
data[0] = MNotifyGetWString(notifyORtype, NFOPT_TITLEW, NULL);
data[1] = MNotifyGetWString(notifyORtype, NFOPT_TEXTW, NULL);
return ParseTextW(temp, defaultVariablesW, MAX_REGS(defaultVariablesW), data, MAX_REGS(defaultVariablesW));
}
}
BOOL MNotifyHasVariables(HANDLE notifyORtype)
{
return MNotifyGetDWord(notifyORtype, NFOPT_VARIABLES_STRS, NULL) != NULL &&
MNotifyGetDWord(notifyORtype, NFOPT_VARIABLES_SIZE, 0) != 0;
}
BOOL MNotifyHasWVariables(HANDLE notifyORtype)
{
return MNotifyGetDWord(notifyORtype, NFOPT_VARIABLES_STRSW, NULL) != NULL &&
MNotifyGetDWord(notifyORtype, NFOPT_VARIABLES_SIZE, 0) != 0;
}
void MNotifyShowVariables(HANDLE notifyORtype)
{
StringHelper ret = {0};
const char ** vars;
size_t varsSize;
const char ** descs;
size_t descsSize;
if (MNotifyHasVariables(notifyORtype))
{
vars = (const char **) MNotifyGetDWord(notifyORtype, NFOPT_VARIABLES_STRS, NULL);
varsSize = (size_t) MNotifyGetDWord(notifyORtype, NFOPT_VARIABLES_SIZE, 0);
descs = (const char **) MNotifyGetDWord(notifyORtype, NFOPT_VARIABLES_DESCRIPTIONS_STRS, NULL);
descsSize = (size_t) MNotifyGetDWord(notifyORtype, NFOPT_VARIABLES_DESCRIPTIONS_SIZE, 0);
}
else
{
// Default values
vars = defaultVariables;
varsSize = MAX_REGS(defaultVariables);
descs = defaultVariableDescriptions;
descsSize = MAX_REGS(defaultVariableDescriptions);
}
for(size_t i = 0 ; i < varsSize ; i++)
{
if (vars[i] != NULL)
{
if (CopyData(&ret, vars[i], strlen(vars[i])))
return;
if (i < descsSize && descs[i] != NULL && descs[i] != L'\0')
{
if (CopyData(&ret, " -> ", 4))
return;
if (CopyData(&ret, descs[i], strlen(descs[i])))
return;
}
if (CopyData(&ret, "\r\n", 2))
return;
}
}
MessageBoxA(NULL, ret.text, "Variables", MB_OK);
mir_free(ret.text);
}
void MNotifyShowWVariables(HANDLE notifyORtype)
{
StringHelperW ret = {0};
const WCHAR ** vars;
size_t varsSize;
const WCHAR ** descs;
size_t descsSize;
if (MNotifyHasWVariables(notifyORtype))
{
vars = (const WCHAR **) MNotifyGetDWord(notifyORtype, NFOPT_VARIABLES_STRSW, NULL);
varsSize = (size_t) MNotifyGetDWord(notifyORtype, NFOPT_VARIABLES_SIZE, 0);
descs = (const WCHAR **) MNotifyGetDWord(notifyORtype, NFOPT_VARIABLES_DESCRIPTIONS_STRSW, NULL);
descsSize = (size_t) MNotifyGetDWord(notifyORtype, NFOPT_VARIABLES_DESCRIPTIONS_SIZE, 0);
}
else
{
// Default values
vars = defaultVariablesW;
varsSize = MAX_REGS(defaultVariablesW);
descs = defaultVariableDescriptionsW;
descsSize = MAX_REGS(defaultVariableDescriptionsW);
}
for(size_t i = 0 ; i < varsSize ; i++)
{
if (vars[i] != NULL)
{
if (CopyDataW(&ret, vars[i], lstrlenW(vars[i])))
return;
if (i < descsSize && descs[i] != NULL && descs[i] != L'\0')
{
if (CopyDataW(&ret, L" -> ", 3))
return;
if (CopyDataW(&ret, descs[i], lstrlenW(descs[i])))
return;
}
if (CopyDataW(&ret, L"\r\n", 2))
return;
}
}
MessageBoxW(NULL, ret.text, L"Variables", MB_OK);
mir_free(ret.text);
}
|