summaryrefslogtreecommitdiff
path: root/spamfilter/trunk/popup.c
blob: 818d3a9aaf1ab45255ea6f16015c6da54f578cc6 (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
/*

"Spam Filter"-Plugin for Miranda IM

Copyright 2003-2006 Heiko Herkenrath

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program ("SpamFilter-License.txt"); if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/


// -- Includes
#include "common.h"

// -----------------------------------------


// -- Popup Menu Item --

static int InternalServicePopupMenuItemCommand(WPARAM wParam, LPARAM lParam)
{
	BOOL bPopupState = !DBGetContactSettingByte(NULL, DB_MODULE_NAME, DB_SETTING_POPUP, DEFAULT_SETTING_POPUP);

	DBWriteContactSettingByte(NULL, DB_MODULE_NAME, DB_SETTING_POPUP, (BYTE)bPopupState);
	SetPopupMenuItem(bPopupState, FALSE);

	return 0;
}

void SetPopupMenuItem(BOOL bPopupsActivated, BOOL bGrayItem)
{
	static HANDLE hPopupMenuItem = NULL;

	CLISTMENUITEM cmi;

	ZeroMemory(&cmi, sizeof(cmi));
	cmi.cbSize = sizeof(cmi);

	if (bPopupsActivated)
	{
		cmi.pszName = Translate("Disable &spam popups");
		// Getting icons out of from popup.dll (Plus/Non-Plus) (so they are always up-to-date)
		cmi.hIcon = SkinGetIcon("popup_enabled", 0, FALSE);
		if (!cmi.hIcon)
			cmi.hIcon = GetModuleHandle(_T("popup")) ? (HICON)LoadImage(GetModuleHandleA("popup"), MAKEINTRESOURCE(118), IMAGE_ICON, GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON), LR_SHARED) : NULL;

	} else {
		cmi.pszName = Translate("Enable &spam popups");
		// Getting icons out of from popup.dll (Plus/Non-Plus) (so they are always up-to-date)
		cmi.hIcon = SkinGetIcon("popup_disabled", 0, FALSE);
		if (!cmi.hIcon)
			cmi.hIcon = GetModuleHandle(_T("popup")) ? (HICON)LoadImage(GetModuleHandleA("popup"), MAKEINTRESOURCE(120), IMAGE_ICON, GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON), LR_SHARED) : NULL;
	}

	if (hPopupMenuItem)
	{
		cmi.flags = CMIM_NAME|CMIM_ICON|CMIM_FLAGS;

		if (bGrayItem)
			cmi.flags |= CMIF_GRAYED;
	
		CallService(MS_CLIST_MODIFYMENUITEM, (WPARAM)hPopupMenuItem, (LPARAM)&cmi);

	} else {

		// Item can't be grayed out the first time
		//cmi.flags = bGrayItem ? CMIF_GRAYED : 0;

		cmi.pszPopupName = Translate("PopUps");
		cmi.pszService = "SpamFilter/PopupMenuItemCommand";
		
		CreateServiceFunction(cmi.pszService, InternalServicePopupMenuItemCommand);
		hPopupMenuItem = (HANDLE)CallService(MS_CLIST_ADDMAINMENUITEM, (WPARAM)0, (LPARAM)&cmi);
		
		// Call twice to initilialize the grayed out setting
		if (bGrayItem)
			SetPopupMenuItem(bPopupsActivated, bGrayItem);
	}
}



// -- Spam Popup --
BOOL ShowSpamPopup(const char* pszMsgTypeSection, const char* pszMsgTypeName, const WCHAR* pszCustomUserName, HANDLE hContact, DWORD dwFilterType)
{
	BOOL bReturn = FALSE;
	int iMsgType;
	
	// Support for Popup / PopupPlus
	#if defined(UNICODE)
		if (ServiceExists(MS_POPUP_ADDPOPUPW))
		{
			POPUPDATAW ppd;
			HICON hIconBuf;
			WCHAR* pszText;

			switch (dwFilterType)
			{
				case SFT_DISLIKEDMESSAGES_FILTER:
					pszText = TranslateW("Disliked Message Alert!");
					break;
				case SFT_ADVERTISMENT_FILTER:
					pszText =  TranslateW("Advertisment Alert!");
					break;
				case SFT_ROBOT_FILTER:
					pszText = TranslateW("Robot Alert!");
					break;
				default:
					pszText = NULL;
			}

			ZeroMemory(&ppd, sizeof(ppd));
			ppd.lchContact = hContact;
			ppd.colorText = SkinGetColor(DB_COLOR_POPUPTEXT_SETTING, Translate("Popup Text"), Translate("Spam Filter"), RGB(255,255,255), FALSE);
			ppd.colorBack = SkinGetColor(DB_COLOR_POPUPBACKGROUND_SETTING, Translate("Popup Background"), Translate("Spam Filter"), RGB(0,0,0), FALSE);
			if (pszText) mir_sntprintf(ppd.lpwzText, ARRAYSIZE(ppd.lpwzText), _T("%s"), pszText); 
			if (pszCustomUserName) mir_sntprintf(ppd.lpwzContactName, ARRAYSIZE(ppd.lpwzContactName), _T("%s"), pszCustomUserName);

			// Determine main icon
			EnterCriticalSection(&csMsgTypes); // thread safety
			iMsgType = GetMsgTypeID(pszMsgTypeSection, pszMsgTypeName);

			if ((iMsgType >= 0) && ((int)pamtdMsgTypes[iMsgType].hIcon < 0))
				hIconBuf = LoadSkinnedIcon((-1)*(int)pamtdMsgTypes[iMsgType].hIcon); // skinned icon
			else if ((iMsgType >= 0) && pamtdMsgTypes[iMsgType].hIcon)
				hIconBuf = pamtdMsgTypes[iMsgType].hIcon;
			else
				hIconBuf = (HICON)LoadImage(hInstance, MAKEINTRESOURCE(IDI_DEFAULT), IMAGE_ICON, GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON), LR_SHARED);

			LeaveCriticalSection(&csMsgTypes); // thread safety

			// Overlay small spam icon
			if (hIconBuf) 
				ppd.lchIcon = OverlayIcon(hIconBuf, SkinGetIcon(DB_ICON_SPAMLAYER_SETTING, IDI_SPAM_LAYER, FALSE), GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON));

			// I don't know if popup module author made it thread safe...
			if (CallServiceSync(MS_POPUP_ADDPOPUPW, (WPARAM)&ppd, 0) > 0)
				bReturn = TRUE;

			if (ppd.lchIcon) DestroyIcon(ppd.lchIcon);
		}
	#else
		if (ServiceExists(MS_POPUP_ADDPOPUPEX))
		{
			POPUPDATAEX ppd;
			HICON hIconBuf;
			char* pszText;

			switch (dwFilterType)
			{
				case SFT_DISLIKEDMESSAGES_FILTER:
					pszText = Translate("Disliked Message Alert!");
					break;
				case SFT_ADVERTISMENT_FILTER:
					pszText =  Translate("Advertisment Alert!");
					break;
				case SFT_ROBOT_FILTER:
					pszText = Translate("Robot Alert!");
					break;
				default:
					pszText = NULL;
			}

			ZeroMemory(&ppd, sizeof(ppd));
			ppd.lchContact = hContact;
			ppd.colorText = SkinGetColor(DB_COLOR_POPUPTEXT_SETTING, Translate("Popup Text"), Translate("Spam Filter"), RGB(255,255,255), FALSE);
			ppd.colorBack = SkinGetColor(DB_COLOR_POPUPBACKGROUND_SETTING, Translate("Popup Background"), Translate("Spam Filter"), RGB(0,0,0), FALSE);
			if (pszText)
				mir_snprintf(ppd.lpzText, ARRAYSIZE(ppd.lpzText), "%s", pszText); 
			if (pszCustomUserName)
				mir_snprintf(ppd.lpzContactName, ARRAYSIZE(ppd.lpzContactName), "%s", pszCustomUserName);

			// Determine main icon
			EnterCriticalSection(&csMsgTypes); // thread safety
			iMsgType = GetMsgTypeID(pszMsgTypeSection, pszMsgTypeName);

			if ((iMsgType >= 0) && ((int)pamtdMsgTypes[iMsgType].hIcon < 0))
				hIconBuf = LoadSkinnedIcon((-1)*(int)pamtdMsgTypes[iMsgType].hIcon); // skinned icon
			else if ((iMsgType >= 0) && pamtdMsgTypes[iMsgType].hIcon)
				hIconBuf = pamtdMsgTypes[iMsgType].hIcon;
			else
				hIconBuf = (HICON)LoadImage(hInstance, MAKEINTRESOURCE(IDI_DEFAULT), IMAGE_ICON, GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON), LR_SHARED);

			LeaveCriticalSection(&csMsgTypes); // thread safety

			// Overlay small spam icon
			if (hIconBuf)
				ppd.lchIcon = OverlayIcon(hIconBuf, SkinGetIcon(DB_ICON_SPAMLAYER_SETTING, IDI_SPAM_LAYER, FALSE), GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON));

			// Support for Popup Plus
			if (ServiceExists(MS_POPUP_ADDCLASS))
			{
				ppd.skinBack = ppd.colorBack;
				ppd.colorBack = POPUP_USE_SKINNED_BG;
				ppd.lpzClass = (LPCTSTR)POPUP_CLASS_SPAM; // type "LPCTSTR" of lpzClass is wrong (or: POPUP_CLASS_DEFAULT)
			}
			
			// I don't know if popup module author made it thread safe...
			if (CallServiceSync(MS_POPUP_ADDPOPUPEX, (WPARAM)&ppd, 0) > 0)
				bReturn = TRUE;

			if (ppd.lchIcon) DestroyIcon(ppd.lchIcon);
		}
	#endif

	// Support for OSD / WanneBeOSD
	if (ServiceExists("OSD/Announce")) // no m_osd.h file yet available.. .don't like it :-/
	{
		WCHAR* pszText;

		switch (dwFilterType)
		{
			case SFT_DISLIKEDMESSAGES_FILTER:
				pszText = TranslateT("Disliked Message Alert!");
				break;

			case SFT_ADVERTISMENT_FILTER:
				pszText = TranslateT("Advertisment Alert!");
				break;

			case SFT_ROBOT_FILTER:
				pszText = TranslateT("Robot Alert!");
				break;

			default:
				pszText = NULL;
		}

		if (pszText)
		{
			// I don't know if OSD module author made it thread safe...
			// quite bad: return value of OSDAnnounce does not indicate anything (SendMessage result)
			// bad unicode implementation (simply expects unicode text when wbosdW.dll is used)
			// -> might cuase crashes (buffer overruns) when SF-Unicode is run with OSD-Ansi
			if (CallService("OSD/Announce", (WPARAM)pszText, (LPARAM)0) == 0);
				bReturn = TRUE;
		}
	}

	return bReturn;
}


// -- Popup Available --

BOOL IsPopupAvailable(BOOL bAlsoOSD)
{	
	#if defined(UNICODE)
		return (ServiceExists(MS_POPUP_ADDPOPUPW) || (bAlsoOSD ? ServiceExists("OSD/Announce") : FALSE));
	#else
		return (ServiceExists(MS_POPUP_ADDPOPUPEX) || (bAlsoOSD ? ServiceExists("OSD/Announce") : FALSE));
	#endif
}


// ------------------------------------


void InitPopup(void)
{
	// Add class (Support for PopupPlus)
	#if !defined(UNICODE)
		if (ServiceExists(MS_POPUP_ADDCLASS))
			CallService(MS_POPUP_ADDCLASS, 0, (LPARAM)POPUP_CLASS_SPAM);
	#endif

	if (IsPopupAvailable(FALSE))
		SkinAddNewIcon(DB_ICON_SPAMLAYER_SETTING, TranslateT("Spam Filter"), TranslateT("Spam Message Overlay"), IDI_SPAM_LAYER, FALSE);
}


void UninitPopup(void)
{
}