summaryrefslogtreecommitdiff
path: root/nohtml/filter.cpp
blob: b66c4574f25e2547411126697a1fcc79fcd4f809 (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
#include "common.h"
#include "filter.h"
#include "options.h"
#include "conv.h"

#define MIR_VER_FILTERS_FIXED		0x0007001A

void LOG(char *message) {
#ifdef _DEBUG
	PUShowMessage(message, SM_NOTIFY);
#endif
}

void LOGW(wchar_t *message) {
#ifdef _DEBUG
	PUShowMessageW(message, SM_NOTIFY);
#endif
}

bool ContactIsOTREncrypted(HANDLE hContact) {
	return (DBGetContactSettingByte(hContact, "OTR", "Encrypted", 0) != 0);
}

INT_PTR FilterSendMessage(WPARAM wParam, LPARAM lParam) {
	CCSDATA *ccs = (CCSDATA *) lParam;
	char *old_message = (char *)ccs->lParam;
	char *buf = 0;

	if(options.filter_out) {
		if((options.apply_to & ATF_ALL) 
			|| ((options.apply_to & ATF_OTR) && ContactIsOTREncrypted(ccs->hContact)))
		{
			if(ccs->wParam & PREF_UTF) {
				int size = MultiByteToWideChar(CP_UTF8, 0, old_message, -1, 0, 0);
				wchar_t *messagew = (wchar_t *)malloc(size * sizeof(wchar_t));
				MultiByteToWideChar(CP_UTF8, 0, old_message, -1, messagew, size);
				wchar_t* smsg=strip_carrots(messagew);
				wchar_t* html_msg;
				if(options.html) {
					html_msg=bbcodes_to_html(smsg);
					delete[] smsg;
				} else
					html_msg = smsg;

				free(messagew);
				size = WideCharToMultiByte(CP_UTF8, 0, html_msg, -1, 0, 0, 0, 0);
				buf = (char *)malloc(size);
				WideCharToMultiByte(CP_UTF8, 0, html_msg, -1, buf, size, 0, 0);
				delete[] html_msg;
			} else if(ccs->wParam & PREF_UNICODE) {
				wchar_t *messagew = (wchar_t *)&old_message[strlen(old_message)+1];
				wchar_t* smsg=strip_carrots(messagew);
				wchar_t* html_msg;
				if(options.html) {
					html_msg=bbcodes_to_html(smsg);
					delete[] smsg;
				} else
					html_msg = smsg;

				buf=(char *)malloc(wcslen(html_msg)*3+3);
				WideCharToMultiByte( codepage, 0,html_msg, -1,buf,wcslen(html_msg)+1, NULL, NULL);
				memcpy(&buf[strlen(buf)+1],html_msg,lstrlen(buf)*2+2);
				delete[] html_msg;
			} else {
				char* smsg=strip_carrots(old_message);
				char* html_msg;
				if(options.html) {
					html_msg=bbcodes_to_html(smsg);
					delete[] smsg;
				} else
					html_msg = smsg;

				buf = strdup(html_msg);
				delete[] html_msg;
			}
			ccs->lParam = (LPARAM)buf;
		}
	}

	INT_PTR ret = CallService(MS_PROTO_CHAINSEND, wParam, lParam);
	ccs->lParam = (LPARAM)old_message;
	if(buf) free(buf);
	return ret;
}

INT_PTR FilterSendMessageW(WPARAM wParam, LPARAM lParam) {
    CCSDATA *ccs = (CCSDATA *) lParam;
	if(!(ccs->wParam & PREF_UTF))
		ccs->wParam |= PREF_UNICODE;

	return FilterSendMessage(wParam, lParam);
}

INT_PTR FilterRecvMessage(WPARAM wParam, LPARAM lParam) {
	CCSDATA *ccs = (CCSDATA *) lParam;
	PROTORECVEVENT *pre = (PROTORECVEVENT *) ccs->lParam;
	char *old_message = pre->szMessage;
	char* buf = 0;

	if(options.filter_in) {
		if((options.apply_to & ATF_ALL) 
			|| ((options.apply_to & ATF_OTR) && ContactIsOTREncrypted(ccs->hContact)))
		{
			if(pre->flags & PREF_UTF) {
				LOG("Recieved a utf8 message.");
				int size = MultiByteToWideChar(CP_UTF8, 0, old_message, -1, 0, 0);
				wchar_t *messagew = (wchar_t *)malloc(size * sizeof(wchar_t));
				MultiByteToWideChar(CP_UTF8, 0, old_message, -1, messagew, size);

				LOGW(messagew);
				wchar_t* st_wbuf;
				if(options.bbcodes)
				{
					LOG("Converting from html to bbcodes then stripping leftover html.(U)");
					wchar_t* bbuf=html_to_bbcodes(messagew);
					st_wbuf=strip_html(bbuf);
					delete[] bbuf;
				}
				else
				{
					LOG("Stripping html.(U)");
					st_wbuf=strip_html(messagew);
				}
				free(messagew);

				//delete[] pre->szMessage; not necessary - done in server.cpp
				buf=(char *)malloc(wcslen(st_wbuf)*3+3);
				WideCharToMultiByte(CP_UTF8, 0, st_wbuf, -1,buf,wcslen(st_wbuf)+1, NULL, NULL);
				memcpy(&buf[strlen(buf)+1], st_wbuf, lstrlen(buf)*2+2);
				delete[] st_wbuf;
				pre->szMessage = buf;
			} else if(pre->flags & PREF_UNICODE) {
				LOG("Recieved a unicode message.");
				wchar_t *messagew = (wchar_t *)&old_message[strlen(old_message)+1];
				//wchar_t* wbuf=(wchar_t*)&pre->szMessage[lstrlen(pre->szMessage)+1];
				LOGW(messagew);
				wchar_t* st_wbuf;
				if(options.bbcodes)
				{
					LOG("Converting from html to bbcodes then stripping leftover html.(U)");
					wchar_t* bbuf=html_to_bbcodes(messagew);
					st_wbuf=strip_html(bbuf);
					delete[] bbuf;
				}
				else
				{
					LOG("Stripping html.(U)");
					st_wbuf=strip_html(messagew);
				}
				//delete[] pre->szMessage; not necessary - done in server.cpp
				buf=(char *)malloc(wcslen(st_wbuf)*3+3);
				WideCharToMultiByte(codepage, 0, st_wbuf, -1,buf,wcslen(st_wbuf)+1, NULL, NULL);
				memcpy(&buf[strlen(buf)+1],st_wbuf,lstrlen(buf)*2+2);
				delete[] st_wbuf;
				pre->szMessage = buf;
			} else {
				LOG("Recieved a non-unicode message.");
				if(options.bbcodes)
				{
					LOG("Converting from html to bbcodes then stripping leftover html.");
					char* bbuf=html_to_bbcodes(pre->szMessage);
					buf=strip_html(bbuf);
					delete[] bbuf;
				}
				else
				{
					LOG("Stripping html.");
					buf=strip_html(pre->szMessage);
				}
				pre->szMessage = buf;
			}
		}
	}
	
	INT_PTR ret = CallService(MS_PROTO_CHAINRECV, wParam, lParam);
	pre->szMessage = old_message;
	if(buf) free(buf);
	return ret;
}

int NewContact(WPARAM wParam, LPARAM lParam) {
	// add filter
	HANDLE hContact = (HANDLE)wParam;
	CallService( MS_PROTO_ADDTOCONTACT, ( WPARAM )hContact, ( LPARAM )MODULE );
	
	return 0;
}

void RegisterFilter() {
	PROTOCOLDESCRIPTOR pd = {0};
	pd.cbSize = PROTOCOLDESCRIPTOR_V3_SIZE;//sizeof(pd);
	pd.szName = MODULE "Filter";	
	pd.type = PROTOTYPE_FILTER; 

	CallService(MS_PROTO_REGISTERMODULE,0,(LPARAM)&pd);
}

HANDLE hEventNewContact;
#define NUM_FILTER_SERVICES 3
HANDLE hServices[NUM_FILTER_SERVICES];

void AddFilterToContacts() {
	if(DBGetContactSettingByte(0, MODULE, "FilterOrderFix", 0) != 2) {
		// remove filter so it can be added again in the right position
		HANDLE hContact = ( HANDLE )CallService( MS_DB_CONTACT_FINDFIRST, 0, 0 );
		while ( hContact != NULL ) {
			CallService( MS_PROTO_REMOVEFROMCONTACT, ( WPARAM )hContact, ( LPARAM )(MODULE "Filter"));	
			hContact = ( HANDLE )CallService( MS_DB_CONTACT_FINDNEXT,( WPARAM )hContact, 0 );
		}	
		DBWriteContactSettingByte(0, MODULE, "FilterOrderFix", 2);
	}

	// this adds us as a filter to all existing and new contacts
	HANDLE hContact = ( HANDLE )CallService( MS_DB_CONTACT_FINDFIRST, 0, 0 );
	while ( hContact != NULL ) {
		CallService( MS_PROTO_ADDTOCONTACT, ( WPARAM )hContact, ( LPARAM )(MODULE "Filter"));	
		hContact = ( HANDLE )CallService( MS_DB_CONTACT_FINDNEXT,( WPARAM )hContact, 0 );
	}	

	hEventNewContact = HookEvent(ME_DB_CONTACT_ADDED, NewContact);
}

void CreateFilterServices() {
	// create our services
	int i = 0;
	hServices[i++] = CreateProtoServiceFunction(MODULE "Filter", PSS_MESSAGE, FilterSendMessage);
	hServices[i++] = CreateProtoServiceFunction(MODULE "Filter", PSS_MESSAGE"W", FilterSendMessageW);
	hServices[i++] = CreateProtoServiceFunction(MODULE "Filter", PSR_MESSAGE, FilterRecvMessage);

	// remember to modify the NUM_FILTER_SERVICES #define above if you add more services!
}

void DeinitFilter() {
	UnhookEvent(hEventNewContact);

	for(int i = 0; i < NUM_FILTER_SERVICES; i++)
		DestroyServiceFunction(hServices[i]);
}