summaryrefslogtreecommitdiff
path: root/plugins/!NotAdopted/Non-IM Contact/namereplacing.c
blob: 25dc8f5a3cf580055ee12dee7996ae3f0f8b2b91 (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
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
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
#include "commonheaders.h"

int readFileIntoArray(int fileNumber, char *FileContents[])
{
	int i =0;
	FILE* file;
	char FileName[MAX_PATH], temp[MAX_STRING_LENGTH];
	wsprintf(FileName,"fn%d",fileNumber);
	DBGetContactSettingString(NULL,modname, FileName, FileName);

	if (!strncmp("http://", FileName, strlen("http://")))
		wsprintf(FileName, "%s\\plugins\\fn%d.html",getMimDir(temp),fileNumber);

	file = fopen(FileName, "r");
	if (file == NULL) return 0;
	// read the file into the FileContents array
	// free this array before stringReplacer() returns
	while (fgets(temp, MAX_STRING_LENGTH-1,file))
	{
		if(temp[strlen(temp)-1]=='\n') 
			temp[strlen(temp)-1]='\0';
		else temp[strlen(temp)]='\0';

		FileContents[i] = (char*)malloc(strlen(temp)+1);
		if (FileContents[i] == NULL) return i;
		strcpy(FileContents[i], temp);
		i++;
	}
	fclose(file);
	return i;
}

int getNumber(const char* line)
{
	int i;
	if (sscanf(line, "%d", &i) == 1) return i; else return -1;
}
	
int findWordInString(const char* line, const char* string, int* lengthOfWord, int flag) /* flag = 0 %from, flag = 1 %until */
{
	unsigned int i, j=0;
	char word[64]="", OpenDivider[8], CloseDivider[8];
	strcpy(OpenDivider, Translate("(\""));
	strcpy(CloseDivider, Translate("\")"));
	/* get the word we r looking for */
	if (!strncmp(string, OpenDivider, strlen(OpenDivider)))
	{
		for (i=2; strncmp(&string[i], CloseDivider, strlen(CloseDivider)); i++)
		{
			word[j] = string[i];
			word[++j] = '\0';
		}
	}
	i=0;
	*lengthOfWord = strlen(word)+strlen(CloseDivider)+strlen(OpenDivider);
	/* find the word in the line */
	while (i < (strlen(line) - strlen(word) ) )
	{
		if (!strncmp(&line[i], word, strlen(word)))
		{
			if (!flag) return i + strlen(word); /* the next char after the word */
			else return i; /* the char before the word */
		}
		i++;
	}
	return -1;
}

int findLine(char* FileContents[], const char* string, int linesInFile,int startLine, int *positionInOldString)
{
	int i;
	char tmp[5];
	i = getNumber(&string[*positionInOldString]);
	// check if blank
	if (string[*positionInOldString] == ')')
		return startLine;
	// check if its a number
	if ( i != -1)
	{
		*positionInOldString += strlen(itoa(i,tmp,10)) - 1; 
		return i;
	}
	// lastline
	else if (!strncmp(&string[*positionInOldString], Translate("lastline("), strlen(Translate("lastline("))))
	{
		*positionInOldString += strlen(Translate("lastline("));
		i = getNumber(&string[*positionInOldString]);
		if ( i != -1)
		{
			*positionInOldString += strlen(itoa(i,tmp,10)); 
			return linesInFile - (i+1);
		}
		else 
		{
			*positionInOldString ++; 
			return (linesInFile - 1);
		}
	}
	// string
	else if (string[*positionInOldString] == '\"')
	{
		char string2Find[256];
		int j=0;
		// get the word to find
		for (i=(*positionInOldString+1); strncmp(&string[i], "\")", 2); i++)
		{
			string2Find[j] = string[i];
			string2Find[++j] = '\0';
		}
		// find the word
		for (j=startLine; j<linesInFile;j++)
		{
			if (strstr(FileContents[j], string2Find))
			{
				i = j;
				break;
			}
			i=-1;
		}
		*positionInOldString += strlen(string2Find) + strlen(Translate("\"\")"));
		if (i==-1) return i;
		// allow for a +- after the word to go up or down lines
		if (string[*positionInOldString] == '+')
		{
			*positionInOldString += 1;
			j = getNumber(&string[*positionInOldString]);
			if (j != -1)
			{
				*positionInOldString += strlen(itoa(j,tmp,10))-2;
				return i+j;
			}
		}
		else if (string[*positionInOldString] == '-')
		{
			*positionInOldString+=1;
			j = getNumber(&string[*positionInOldString]);
			if (j != -1)
			{
				*positionInOldString += strlen(itoa(j,tmp,10))-2;
				return i-j;
			}
		}
		else 
		{
			*positionInOldString -= 2;
			return i;
		}
	}
/*	else if (!strncmp(&string[*positionInOldString] , Translate("regexp(\""), strlen(Translate("regexp(\""))))
	{
		char string2Find[256];
		int j=0;
		// get the word to find
		for (i=(*positionInOldString+1); strncmp(&string[i], "\")", 2); i++)
		{
			string2Find[j] = string[i];
			string2Find[++j] = '\0';
		}
		*positionInOldString += strlen(Translate("regexp(\"")) + strlen(string2Find) +2;
	}
*/	return -1;
}

int findChar(char* FileContents[], const char* string, int linesInFile,int startLine, int *positionInOldString, int startChar, int startEnd) // 0=start, 1=end for startEnd
{
	int i;
	char tmp[5];
	i = getNumber(&string[*positionInOldString]);
	// check if its a number
	if ( i != -1)
	{
		*positionInOldString += strlen(itoa(i,tmp,10)) - 1; 
		return i;
	}
	// string
	else if (string[*positionInOldString] == '\"')
	{
		char string2Find[256];
		unsigned int j=0;
		// get the word to find
		for (i=(*positionInOldString+1); strncmp(&string[i], "\")", 2); i++)
		{
			string2Find[j] = string[i];
			string2Find[++j] = '\0';
		}
		// find the word
		for (j=0;j<strlen(FileContents[startLine]);j++)
		{
			if (!strncmp(&FileContents[startLine][j], string2Find, strlen(string2Find)))
				break;
		}
		if (j==strlen(FileContents[startLine])) return -1;
		else 
		{
			*positionInOldString += strlen(string2Find)+1;
			if (startEnd) return j;
			else return j+strlen(string2Find);
		}
	}
	// csv(
	else if (!strncmp(&string[*positionInOldString], Translate("csv("), strlen(Translate("csv("))))
	{
		char seperator;
		int j=0, k=startChar;
		*positionInOldString += strlen(Translate("csv("));
		if (!strncmp(&string[*positionInOldString], "tab",3))
		{
			*positionInOldString += 3;
			seperator = '\t';
		}
		else if (!strncmp(&string[*positionInOldString], "space",5))
		{
			*positionInOldString += 5;
			seperator = ' ';
		}
		else
		{
			seperator =string[*positionInOldString];
			*positionInOldString += 1;
		}
		i = getNumber(&string[*positionInOldString]);
		if ( i == -1) return -1;
		*positionInOldString += strlen(itoa(i,tmp,10));
		while (j<i)
		{
			if (FileContents[startLine][k] == '\0') break;
			if (FileContents[startLine][k] == seperator)
				j++;
			k++;
		}
		return k;
	}
	return -1;
}
// do the compare("A","B","X","Y")
void checkStringForcompare(char *str)
{
	char *A,*B, *X, *Y , *newStr = (char*)malloc(strlen(str)), *copyOfStr = strdup(str);
	unsigned int i, j=0, s=strlen(str);
	newStr[0] = '\0';
	if (!strstr(str,Translate("compare(\""))) return;
	for (i=0; i<s; i++)
	{
		if (!strncmp(&str[i], Translate("compare(\""), strlen(Translate("compare(\""))))
		{
			i += strlen(Translate("compare(\""));
			A = strtok(&copyOfStr[i], "\",\"");
			B = strtok(NULL, "\",\"");
			X = strtok(NULL, "\",\"");
			Y = strtok(NULL, ",\")");
			j = Y - &copyOfStr[i] + strlen(Y)+1;
			if (A && B && X && Y)
			{
				if (!strcmp(A,B))
					strcat(newStr, X);
				else strcat(newStr, Y);
			}
			else strncat(newStr, &str[i], j);
			i += j;
		}
		else 
		{
			strncat(newStr, &str[i], 1);
		}
	}
	strcpy(str, newStr);
	free(newStr);
	free(copyOfStr);
}

// do save("A","B") A is DBVar name, B is value
void checkStringForSave(HANDLE hContact, char* str)
{
	char *A,*B,*newStr = (char*)malloc(strlen(str)),*copyOfStr = strdup(str);
	unsigned int i, j=0, s=strlen(str);
	newStr[0] = '\0';
	if (!strstr(str,Translate("save(\""))) return;
	for (i=0; i<s; i++)
	{
		if (!strncmp(&str[i], Translate("save(\""), strlen(Translate("save(\""))))
		{
			i += strlen(Translate("save(\""));
			A = strtok(&copyOfStr[i], "\",\"");
			B = strtok(NULL, ",\")");
			j = B - &copyOfStr[i] + strlen(B)+1;
			if (A && B)
			{
				DBWriteContactSettingString(hContact,modname, A, B);
			}
			else strncat(newStr, &str[i], j);
			i += j;
		}
		else 
		{
			strncat(newStr, &str[i], 1);
		}
	}
	strcpy(str, newStr);
	free(newStr);
	free(copyOfStr);
}

// do load("A") A is DBVar name
void checkStringForLoad(HANDLE hContact, char* str)
{
	char *A,*newStr = (char*)malloc(strlen(str)),*copyOfStr = strdup(str);
	unsigned int i, j=0, s=strlen(str);
	newStr[0] = '\0';
	if (!strstr(str,Translate("load(\""))) return;
	for (i=0; i<s; i++)
	{
		if (!strncmp(&str[i], Translate("load(\""), strlen(Translate("load(\""))))
		{
			i += strlen(Translate("load(\""));
			A = strtok(&copyOfStr[i], "\")");
			j = A - &copyOfStr[i] + strlen(A)+1;
			if (A)
			{
				DBVARIANT dbv;
				if (!DBGetContactSetting(hContact,modname, A, &dbv))
				{
					if (dbv.type == DBVT_ASCIIZ)
						strcat(newStr, dbv.pszVal);
					DBFreeVariant(&dbv);
				}
			}
			else strncat(newStr, &str[i], j);
			i += j;
		}
		else 
		{
			strncat(newStr, &str[i], 1);
		}
	}
	strcpy(str, newStr);
	free(newStr);
	free(copyOfStr);
}

// do saveN("A","B","C","D") A is module, B is setting, c is value, D is type 0/b 1/w 2/d 3/s
void checkStringForSaveN(char* str)
{
	char *A,*B,*C,*D,*newStr = (char*)malloc(strlen(str)),*copyOfStr = strdup(str);
	unsigned int i, j=0, s=strlen(str);
	newStr[0] = '\0';
	if (!strstr(str,Translate("saveN(\""))) return;
	for (i=0; i<s; i++)
	{
		if (!strncmp(&str[i], Translate("saveN(\""), strlen(Translate("saveN(\""))))
		{
			i += strlen(Translate("saveN(\""));
			A = strtok(&copyOfStr[i], "\",\"");
			B = strtok(NULL, ",\"");
			C = strtok(NULL, ",\"");
			D = strtok(NULL, ",\")");
			j = D - &copyOfStr[i] + strlen(D)+1;
			if (A && B && C && D)
			{
				switch (D[0])
				{
					case '0':
					case 'b':
						DBWriteContactSettingByte(NULL, A, B, (BYTE)atoi(C));
					break;
					case '1':
					case 'w':
						DBWriteContactSettingWord(NULL, A, B, (WORD)atoi(C));
					break;
					case '2':
					case 'd':
						DBWriteContactSettingDword(NULL, A, B, (DWORD)atoi(C));
					break;
					case '3':
					case 's':
						DBWriteContactSettingString(NULL, A, B, C);
					break;
				}
			}
			else strncat(newStr, &str[i], j);
			i += j;
		}
		else 
		{
			strncat(newStr, &str[i], 1);
		}
	}
	strcpy(str, newStr);
	free(newStr);
	free(copyOfStr);
}

// do loadN("A","B") A is module, B is setting
void checkStringForLoadN(char* str)
{
	char *A,*B,*newStr = (char*)malloc(strlen(str)),*copyOfStr = strdup(str), temp[32];
	unsigned int i, j=0, s=strlen(str);
	newStr[0] = '\0';
	if (!strstr(str,Translate("loadN(\""))) return;
	for (i=0; i<s; i++)
	{
		if (!strncmp(&str[i], Translate("loadN(\""), strlen(Translate("loadN(\""))))
		{
			i += strlen(Translate("loadN(\""));
			A = strtok(&copyOfStr[i], "\",\"");
			B = strtok(NULL, ",\")");
			j = B - &copyOfStr[i] + strlen(B)+1;
			if (A && B)
			{
				DBVARIANT dbv;
				if (!DBGetContactSetting(NULL,A, B, &dbv))
				{	
					switch (dbv.type)
					{
						case DBVT_BYTE:
							strcat(newStr,itoa(dbv.bVal,temp,10));
						break;
						case DBVT_WORD:
							strcat(newStr,itoa(dbv.wVal,temp,10));
						break;
						case DBVT_DWORD:
							strcat(newStr,itoa(dbv.dVal,temp,10));
						break;
						case DBVT_ASCIIZ:
							strcat(newStr, dbv.pszVal);
						break;
					}
					DBFreeVariant(&dbv);
				}
			}
			else strncat(newStr, &str[i], j);
			i += j;
		}
		else 
		{
			strncat(newStr, &str[i], 1);
		}
	}
	strcpy(str, newStr);
	free(newStr);
	free(copyOfStr);
}

BOOL GetLastWriteTime(HANDLE hFile, LPTSTR lpszString)
{
    FILETIME ftCreate, ftAccess, ftWrite;
    SYSTEMTIME stUTC, stLocal;

    // Retrieve the file times for the file.
    if (!GetFileTime(hFile, &ftCreate, &ftAccess, &ftWrite))
        return FALSE;

    // Convert the last-write time to local time.
    FileTimeToSystemTime(&ftWrite, &stUTC);
    SystemTimeToTzSpecificLocalTime(NULL, &stUTC, &stLocal);

    // Build a string showing the date and time.
    wsprintf(lpszString, TEXT("%02d/%02d/%d  %02d:%02d"),
        stLocal.wDay, stLocal.wMonth, stLocal.wYear,
        stLocal.wHour, stLocal.wMinute);

    return TRUE;
}

// do lastchecked(file(X)) returns amount of chars to add to str pointer
int lastChecked(char* newStr, const char* str)
{
	if (!strncmp(str, Translate("lastchecked(file("), strlen(Translate("lastchecked(file("))))
	{
		int file;
		HANDLE hFile;
		char FileName[MAX_PATH], temp[MAX_PATH];
		sscanf(&str[strlen(Translate("lastchecked(file("))], "%d", &file);
		_snprintf(FileName, MAX_PATH, "fn%d", file);
		DBGetContactSettingString(NULL,modname, FileName, FileName);

		if (!strncmp("http://", FileName, strlen("http://")) || !strncmp("https://", FileName, strlen("https://")))
			wsprintf(FileName, "%s\\plugins\\fn%d.html",getMimDir(temp),file);
		hFile= CreateFile(FileName, 0 , FILE_SHARE_READ , 0 ,OPEN_ALWAYS , FILE_ATTRIBUTE_NORMAL , NULL );
		if (hFile == INVALID_HANDLE_VALUE) return 0;
		if (GetLastWriteTime(hFile, FileName))
		{
			strcat(newStr, FileName);
			_snprintf(FileName, MAX_PATH, "%s%d))",Translate("lastchecked(file("), file);
			return strlen(FileName);
		}
		return 0;
	}
	return 0;
}


// do icon(x) 0=offline, 1=online, 10=lunch
void checkIcon(HANDLE hContact, char* string)
{
	char* str = strstr(string,"icon(");
	if (str)
	{
		int icon = getNumber(str+5);
		if (icon >=0)
		{
			DBWriteContactSettingWord(hContact, modname, "Status", (WORD)(ID_STATUS_OFFLINE+icon));
		}
	}
}

int stringReplacer(const char* oldString, char* newString, HANDLE hContact)
{
	char var_file[8];
	int tempInt;
	int startLine = 0, endLine = 0, startChar=0, endChar = 0, wholeLine=-1, linesInFile;
	unsigned int positionInOldString = 0;
	char *fileContents[MAXLINES] = {NULL}, tempString[MAX_STRING_LENGTH];
	
//	setup the variable names
	strcpy(newString, "");
	strcpy(var_file, Translate("file("));


	while ((positionInOldString < strlen(oldString)) && (oldString[positionInOldString] != '\0'))
	{
		// load the file... must be first
		if (!strncmp(&oldString[positionInOldString], var_file, strlen(var_file)))
		{
			positionInOldString += strlen(var_file);
			// check if its a number
			tempInt = getNumber(&oldString[positionInOldString]);
			if (tempInt == -1)
			{
				// not a number so check vars..
				// there are none yet
				return ERROR_NO_FILE;
			}
			// read the file
			linesInFile = readFileIntoArray(tempInt, fileContents);
			if (linesInFile == 0) return ERROR_NO_FILE;
			positionInOldString += strlen(itoa(tempInt, tempString,10)) + 1; // +1 for the closing )
		
			// wholeline()
			if (!strncmp(&oldString[positionInOldString], Translate("wholeline(line("), strlen(Translate("wholeline(line("))))
			{
				positionInOldString += strlen(Translate("wholeline(line("));
				tempInt = findLine(fileContents,oldString, linesInFile, startLine,&positionInOldString);
				if (tempInt == -1|| !fileContents[tempInt])
					return ERROR_NO_LINE_AFTER_VAR_F;
				else wholeLine =tempInt;
				positionInOldString += 3; // add 2 for the )) for wholeline(line())
			}
			
			if (!strncmp(&oldString[positionInOldString], Translate("start("), strlen(Translate("start("))))
			{
				positionInOldString += strlen(Translate("start(line("));
				tempInt = findLine(fileContents,oldString, linesInFile, startLine,&positionInOldString);
				if (tempInt == -1 || !fileContents[tempInt])
					return ERROR_NO_LINE_AFTER_VAR_F;
				else 
				{
					positionInOldString+=2;
					startLine = tempInt;
					if (!endChar ) endChar = strlen(fileContents[startLine]);
					tempInt = findChar(fileContents,oldString, linesInFile, startLine,&positionInOldString, startChar,0);
					if (tempInt == -1)
						return ERROR_NO_LINE_AFTER_VAR_F;
					else startChar = tempInt;
				}
				positionInOldString += 2; // add 2 for the )) for start(line())
			}
			if (!strncmp(&oldString[positionInOldString], Translate("end("), strlen(Translate("end("))))
			{
				positionInOldString += strlen(Translate("end(line("));
				tempInt = findLine(fileContents,oldString, linesInFile, startLine,&positionInOldString);
				if (tempInt == -1 || !fileContents[tempInt])
					return ERROR_NO_LINE_AFTER_VAR_F;
				else 
				{
					positionInOldString+=2;
					endLine = tempInt;
					tempInt = findChar(fileContents,oldString, linesInFile, startLine,&positionInOldString, startChar,1);
					if (tempInt == -1)
						return ERROR_NO_LINE_AFTER_VAR_F;
					else endChar = tempInt;
				}
				positionInOldString += 2; // add 2 for the )) for end(line())
			}
			// check for both start() and end() otherwise, only copying 1 line
			if (!strstr(oldString, Translate("start("))) startLine = endLine;
			if (!strstr(oldString, Translate("end("))) endLine = startLine;
			// after all the options copy the line across and add 2 to positionInOldString for the file(print(....))
			if (wholeLine >= 0) strcat(newString, fileContents[wholeLine]);
			else
			{
				// only copying from 1 line
				if (startLine == endLine)
					strncat(newString, &fileContents[startLine][startChar], endChar - startChar);
				else
				{
					int i;
					// copy the whole first line from startChar
					strcat(newString, &fileContents[startLine][startChar]);
					// copy the middle lines across
					for (i=(startLine+1);i<endLine;i++)
					{
						strcat(newString, fileContents[i]);
					}
					// copy the last line untill endChar
					strncat(newString, fileContents[endLine], endChar);
				}
			}
		}
		// filename()
		else if (!strncmp(&oldString[positionInOldString], Translate("filename("), strlen(Translate("filename("))))
		{
			positionInOldString += strlen(Translate("filename("));
			tempInt = getNumber(&oldString[positionInOldString]);
			if (tempInt == -1)
			{
				return ERROR_NO_FILE;
			}
			else 
			{
				wsprintf(tempString, "fn%d", tempInt);
				if (DBGetContactSettingString(NULL, modname,tempString, tempString))
					strcat(newString, tempString);
				else return ERROR_NO_FILE;
				positionInOldString += strlen(itoa(tempInt, tempString, 10))+1;
			}
		}
		// lastchecked(file(X))
		else if (!strncmp(&oldString[positionInOldString], Translate("lastchecked(file("), strlen(Translate("lastchecked(file("))))
		{
			positionInOldString += lastChecked(newString, &oldString[positionInOldString]);
		}
		else
		{
			strncat(newString, &oldString[positionInOldString], 1);
			positionInOldString++;
		}
	}
	// free the file strings
	for (tempInt=0; (fileContents[tempInt] != NULL) && (tempInt<MAXLINES); tempInt++)
		free(fileContents[tempInt]);



	// check for load("A","B")
	checkStringForLoad(hContact, newString);
	// and loadN(...)
	checkStringForLoadN(newString);
	// check for compare("A","B","X","Y")
	checkStringForcompare(newString);
	// check for save("A","B")
	checkStringForSave(hContact, newString);
	// and saveN(...)
	checkStringForSaveN(newString);
	return 1;
}

void stripWhiteSpace(char* string)
{
	int i=0;
	while (string[i] != '\0')
	{
		if (  (string[i] == '\t') || (string[i] == '\r') || (string[i] == '\n') )
			string[i] = ' ';
		i++;
	}
}
void WriteSetting(HANDLE hContact, char* module1, char* setting1 , char* module2, char* setting2)
{
	char text[MAX_STRING_LENGTH], newString[MAX_STRING_LENGTH];
	int error = 0, status = GetLCStatus(0,0);
	if (DBGetContactSettingString(hContact, module1, setting1, text))
	{
		switch (stringReplacer(text, newString, hContact))
		{
			case ERROR_NO_LINE_AFTER_VAR_F:
				wsprintf(newString, Translate("%s - ERROR: no line specified or line not found (in %s)"),text, setting1);
				error = 1;
			break;
			case ERROR_LINE_NOT_READ:
				wsprintf(newString, Translate("%s - ERROR: file couldnt be opened (in %s)"), text, setting1);
				error = 1;
			break;
			case ERROR_NO_FILE:
				wsprintf(newString, Translate("%s - ERROR: no file specified in settings (in %s)"), text, setting1);
				error = 1;
			break;
			default:
				error = 0;
			break;
		}
		// strip the tab and new lines from all except the tooltip
		if (!error && strcmp(setting1, "ToolTip")) stripWhiteSpace(newString);
		DBWriteContactSettingString(hContact, module2, setting2, newString);
	}
	else DBWriteContactSettingString(hContact, module2, setting2, "");
	if (!error)
	{
		if ( (status == ID_STATUS_ONLINE) || (status == ID_STATUS_AWAY) || 
			 (status == DBGetContactSettingWord(hContact, modname, "Icon", ID_STATUS_ONLINE) ) ||
			 DBGetContactSettingByte(hContact, modname ,"AlwaysVisible", 0)
		   )
			DBWriteContactSettingWord(hContact, modname, "Status", (WORD)DBGetContactSettingWord(hContact, modname, "Icon", ID_STATUS_ONLINE));
		else DBWriteContactSettingWord(hContact, modname, "Status", ID_STATUS_OFFLINE);
	}
	else DBWriteContactSettingWord(hContact, modname, "Status", ID_STATUS_OFFLINE);
}
                      
void replaceAllStrings(HANDLE hContact)
{
	char tmp1[256], tmp2[256], tmp3[256];
	WriteSetting(hContact, modname, "Name", modname, "Nick");
	WriteSetting(hContact, modname, "ProgramString", modname, "Program");
	WriteSetting(hContact, modname, "ProgramParamsString", modname, "ProgramParams");
	/* tooltips*/
	WriteSetting(hContact, modname, "ToolTip", "UserInfo", "MyNotes");
	if (DBGetContactSettingString(hContact, modname, "Program", tmp1) && DBGetContactSettingString(hContact, modname, "ProgramParams", tmp2))
	{
		wsprintf(tmp3, "%s %s", tmp1, tmp2);
		DBWriteContactSettingString(hContact, "UserInfo", "FirstName", tmp3);
	}
	else if (DBGetContactSettingString(hContact, modname, "Program", tmp1))
	{
		DBWriteContactSettingString(hContact, "UserInfo", "FirstName", tmp1);
	}
	else DBWriteContactSettingString(hContact, "UserInfo", "FirstName", "");
}