summaryrefslogtreecommitdiff
path: root/utilities.cpp
blob: b7475f658ddd3e526a2e11f761d861fc6207e646 (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
#include "headers.h"

tstring DBGetContactSettingStringPAN(HANDLE hContact, char const * szModule, char const * szSetting, tstring errorValue)
{
	DBVARIANT dbv;
	//if(DBGetContactSetting(hContact, szModule, szSetting, &dbv))
	if(DBGetContactSettingTString(hContact, szModule, szSetting, &dbv))
		return errorValue;
//	if(DBVT_TCHAR == dbv.type )
		errorValue = dbv.ptszVal;
	DBFreeVariant(&dbv);
	return errorValue;
}

std::string DBGetContactSettingStringPAN_A(HANDLE hContact, char const * szModule, char const * szSetting, std::string errorValue)
{
	DBVARIANT dbv;
	//if(DBGetContactSetting(hContact, szModule, szSetting, &dbv))
	if(DBGetContactSettingString(hContact, szModule, szSetting, &dbv))
		return errorValue;
//	if(DBVT_ASCIIZ == dbv.type )
		errorValue = dbv.pszVal;
	DBFreeVariant(&dbv);
	return errorValue;
}

tstring &GetDlgItemString(HWND hwnd, int id)
{
	HWND h = GetDlgItem(hwnd, id);
	int len = GetWindowTextLength(h);
	TCHAR * buf = new TCHAR[len + 1];
	GetWindowText(h, buf, len + 1);
	static tstring s;
	s = buf;
	delete []buf;
	return s;
}					

std::string &GetProtoList()
{
	static std::string s;
	return s = DBGetContactSettingStringPAN_A(NULL, pluginName, "protoList", "ICQ\r\n");
}


bool ProtoInList(std::string proto)
{
	return std::string::npos != GetProtoList().find(proto + "\r\n");
}

int CreateCListGroup(TCHAR* szGroupName)
{
  int hGroup;
  CLIST_INTERFACE *clint = NULL;

  if (ServiceExists(MS_CLIST_RETRIEVE_INTERFACE))
    clint = (CLIST_INTERFACE*)CallService(MS_CLIST_RETRIEVE_INTERFACE, 0, 0);

  hGroup = CallService(MS_CLIST_GROUPCREATE, 0, 0);

    TCHAR* usTmp = szGroupName;

    clint->pfnRenameGroup(hGroup, usTmp);

  return hGroup;
}


void RemoveExcludedUsers()
{
	HANDLE hContact;
	HANDLE ToRemove[4096];
	int i = 0,a = 0;
	hContact = (HANDLE)CallService(MS_DB_CONTACT_FINDFIRST, 0, 0);
	if(hContact)
	{
		do
		{
			if(DBGetContactSettingByte(hContact, "CList", "NotOnList", 0) && DBGetContactSettingByte(hContact, pluginName, "Excluded", 0))
			{
				ToRemove[i] = hContact;
				i++;
			}
		}		
		while(hContact = (HANDLE)CallService(MS_DB_CONTACT_FINDNEXT,(WPARAM)hContact, 0));
		ToRemove[i] = INVALID_HANDLE_VALUE;
		while(ToRemove[a] != INVALID_HANDLE_VALUE)
		{
			CallService(MS_DB_CONTACT_DELETE, (WPARAM)ToRemove[a], 0);
			a++;
		}
	}
}

void RemoveTemporaryUsers()
{
	HANDLE hContact;
	HANDLE ToRemove[4096];
	int i = 0, a= 0;
	hContact = (HANDLE)CallService(MS_DB_CONTACT_FINDFIRST, 0, 0);
	if(hContact)
	{
		do
		{
			if(DBGetContactSettingByte(hContact, "CList", "NotOnList", 0))
			{
				ToRemove[i] = hContact;
				i++;
			}
		}		
		while(hContact = (HANDLE)CallService(MS_DB_CONTACT_FINDNEXT,(WPARAM)hContact, 0));
		ToRemove[i] = INVALID_HANDLE_VALUE;
		while(ToRemove[a] != INVALID_HANDLE_VALUE)
		{
			CallService(MS_DB_CONTACT_DELETE, (WPARAM)ToRemove[a], 0);
			a++;
		}
	}
}
int RemoveTmp(WPARAM,LPARAM)
{
	RemoveTemporaryUsers();
	return 0;
}
tstring variables_parse(tstring const &tstrFormat, HANDLE hContact){
	if (gbVarsServiceExist) {
		FORMATINFO fi;
		TCHAR *tszParsed;
		tstring tstrResult;

		ZeroMemory(&fi, sizeof(fi));
		fi.cbSize = sizeof(fi);
		fi.tszFormat = _tcsdup(tstrFormat.c_str());
		fi.hContact = hContact;
		fi.flags |= FIF_TCHAR;
		tszParsed = (TCHAR *)CallService(MS_VARS_FORMATSTRING, (WPARAM)&fi, 0);
		free(fi.tszFormat);
		if (tszParsed) {
			tstrResult = tszParsed;
			CallService(MS_VARS_FREEMEMORY, (WPARAM)tszParsed, 0);
			return tstrResult;
		}
	}
	return tstrFormat;
}

// case-insensitive _tcscmp
//by nullbie as i remember...
#define NEWTSTR_MALLOC(A) (A==NULL)?NULL:_tcscpy((TCHAR*)mir_alloc(sizeof(TCHAR)*(_tcslen(A)+1)),A)
const int Stricmp(const TCHAR *str, const TCHAR *substr)
{
	int i = 0;

	TCHAR *str_up = NEWTSTR_MALLOC(str);
	TCHAR *substr_up = NEWTSTR_MALLOC(substr);

	CharUpperBuff(str_up, lstrlen(str_up));
	CharUpperBuff(substr_up, lstrlen(substr_up));

	i = _tcscmp(str_up, substr_up);

	mir_free(str_up);
	mir_free(substr_up);
	
	return i;
}
/*
const int Stristr(const TCHAR *str, const TCHAR *substr)
{
	int i = 0;
	TCHAR *str_up = NEWTSTR_MALLOC(str);
	TCHAR *substr_up = NEWTSTR_MALLOC(substr);

	CharUpperBuff(str_up, lstrlen(str_up));
	CharUpperBuff(substr_up, lstrlen(substr_up));

	if(_tcsstr (str_up, substr_up))
		i = 1;

	mir_free(str_up);
	mir_free(substr_up);
	
	return i;
}
*/
TCHAR* ReqGetText(DBEVENTINFO* dbei)
{
	if ( !dbei->pBlob ) 
		return 0;

	char * ptr=(char *)&dbei->pBlob[sizeof(DWORD)*2];
	int len=dbei->cbBlob-sizeof(DWORD)*2; 
	int i=0;
		
	while(len&&(i<4))
	{
		if(!ptr[0]) i++;
		ptr++;
		len--;
	};

	if(len)
	{
		char * tstr=(char *)mir_alloc(len+1);
		memcpy(tstr, ptr, len);
		tstr[len]=0;
		WCHAR* msg = NULL;
		msg=(dbei->flags&DBEF_UTF)?mir_utf8decodeW(tstr):mir_a2u(tstr);
		mir_free(tstr);
		return (TCHAR *)msg;
	};
	return 0;
}


BOOL IsUrlContains(TCHAR * Str)
{
	const int CountUrl=11;
	const TCHAR  URL[CountUrl][5]=
	{
		_T("http"),
		_T("www"),
		_T(".ru"),
		_T(".com"),
		_T(".de"),
		_T(".cz"),
		_T(".org"),
		_T(".net"),
		_T(".su"),
		_T(".ua"),
		_T(".tv")
	}; 

	if(Str&&_tcslen(Str)>0) {
		TCHAR *StrLower = NEWTSTR_MALLOC(Str);
		CharLowerBuff(StrLower, lstrlen(StrLower)); 
		for (int i=0; i<CountUrl; i++)
			if(_tcsstr (StrLower, URL[i]))
			{
				mir_free(StrLower);
				return 1;
			}
		mir_free(StrLower);
	}
	return 0;
}