summaryrefslogtreecommitdiff
path: root/plugins/NewEventNotify/main.c
blob: 1822c748286e1fe366afa5a3ebc977e792ccbf94 (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
/*
  Name: NewEventNotify - Plugin for Miranda IM
  File: main.c - Main DLL procedures
  Version: 0.2.2.2
  Description: Notifies you about some events
  Author: jokusoftware, <jokusoftware@miranda-im.org>
  Date: 18.07.02 13:59 / Update: 22.10.07 19:56
  Copyright: (C) 2002 Starzinger Michael

    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; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

#include "neweventnotify.h"
#include <m_skin.h>
#include <m_clist.h>
#include <m_system.h>
#include <m_protocols.h>
//needed for ICQEVENTTYPE_* (Webpager & Emailexpress)
#include <m_protosvc.h>
//needed for reply instead of read
#include <m_message.h>
#include <m_metacontacts.h>
#include <m_popup.h>

#include <m_options.h>

#include <string.h>
#include <time.h>

int g_IsSrmmServiceAvail = 0;
int g_IsSrmmWindowAPI = 0;
int g_UnicodeCore = 0;

extern PLUGIN_DATA* PopUpList[20];

//---------------------------
//---Some global variables for the plugin


HINSTANCE hInst;
PLUGIN_OPTIONS pluginOptions;

int hLangpack;
PLUGININFOEX pluginInfo = {
	sizeof(PLUGININFOEX),
	"NewEventNotify",
	PLUGIN_MAKE_VERSION(0, VER_MAJOR, VER_MINOR, VER_BUILD),
	"Notifies you when you receive a message, url, file or any other event by displaying a popup. Uses the PopUp-Plugin by hrk. "
	"Original plugin was written by icebreaker, modified by Prezes and some "
	"bugfixes were made by vj, vlko, Nightwish, TheLeech and others. More fixes & updates by Joe @ Whale",
	"icebreaker, Prezes, vj, vlko, Joe @ Whale",
	"jokusoftware@miranda-im.org",
	"GNU GPL",
	"http://addons.miranda-im.org/details.php?action=viewfile&id=3637",
	0,
	0,
	{0x3503D584, 0x6234, 0x4BEF, {0xA5, 0x53, 0x6C, 0x1B, 0x9C, 0xD4, 0x71, 0xF2 } } // {3503D584-6234-4BEF-A553-6C1B9CD471F2}
};

//---------------------------
//---Hooks

//---Handles to my hooks, needed to unhook them again
HANDLE hHookedInit;
HANDLE hHookedOpt;
HANDLE hHookedNewEvent;
HANDLE hHookedDeletedEvent;

//---Called when a new event is added to the database
int HookedNewEvent(WPARAM wParam, LPARAM lParam)
//wParam: contact-handle
//lParam: dbevent-handle
{
  HANDLE hContact = (HANDLE)wParam;
  DBEVENTINFO dbe = {0};
  PLUGIN_DATA* pdata;
  DBEVENTTYPEDESCR* pei;

  //are popups currently enabled?
  if (pluginOptions.bDisable)
    return 0;

  //get DBEVENTINFO without pBlob
  dbe.cbSize = sizeof(dbe);
	CallService(MS_DB_EVENT_GET, (WPARAM)lParam, (LPARAM)&dbe);

	// Nightwish (no popups for RSS contacts at all...)
	if (pluginOptions.bNoRSS) 
	{						
		if (dbe.szModule != NULL) {
			if (!strncmp(dbe.szModule, "RSS", 3))
				return 0;
		}
	}

	//do not show popups for sub-contacts
	if (hContact && ServiceExists(MS_MC_GETMETACONTACT) && CallService(MS_MC_GETMETACONTACT, (WPARAM)hContact, 0))
		return 0;

	//is it an event info about online/offline status user
	if (dbe.eventType == 25368)
		return 0;

  //custom database event types
  if (ServiceExists(MS_DB_EVENT_GETTYPE))
  {
    pei = (DBEVENTTYPEDESCR*)CallService(MS_DB_EVENT_GETTYPE, (WPARAM)dbe.szModule, (LPARAM)dbe.eventType);
    if (pei && pei->cbSize >= DBEVENTTYPEDESCR_SIZE)
    { // ignore events according to flags
      if (pei->flags & DETF_NONOTIFY)
        return 0;
    }
  }

	//if event was allready read don't show it
	if (pluginOptions.bReadCheck && (dbe.flags & DBEF_READ))
		return 0;

	//is it an event sent by the user? -> don't show
  if (dbe.flags & DBEF_SENT)
  {
    if (pluginOptions.bHideSend && NumberPopupData(hContact, EVENTTYPE_MESSAGE) != -1)
    { // JK, only message event, do not influence others
      pdata = PopUpList[NumberPopupData(hContact, EVENTTYPE_MESSAGE)];
      PopupAct(pdata->hWnd, MASK_DISMISS, pdata); // JK, only dismiss, i.e. do not kill event (e.g. file transfer)
		}		
	  return 0; 
	}
  //which status do we have, are we allowed to post popups?
  //UNDER CONSTRUCTION!!!
  CallService(MS_CLIST_GETSTATUSMODE, 0, 0); /// TODO: JK: ????
	
	if (dbe.eventType == EVENTTYPE_MESSAGE && (pluginOptions.bMsgWindowCheck && hContact && CheckMsgWnd(hContact)))
		return 0;

	//is another popup for this contact already present? -> merge message popups if enabled
	if (dbe.eventType == EVENTTYPE_MESSAGE && (pluginOptions.bMergePopup && NumberPopupData(hContact, EVENTTYPE_MESSAGE) != -1))
  { // JK, only merge with message events, do not mess with others
		PopupUpdate(hContact, (HANDLE)lParam);
	}
	else
	{	//now finally show a popup
		PopupShow(&pluginOptions, hContact, (HANDLE)lParam, (UINT)dbe.eventType);
	}
  return 0;
}

//---Called when all the modules are loaded
int HookedInit(WPARAM wParam, LPARAM lParam)
{
	hHookedNewEvent = HookEvent(ME_DB_EVENT_ADDED, HookedNewEvent);
	// Plugin sweeper support
	if (ServiceExists("PluginSweeper/Add"))
    CallService("PluginSweeper/Add", (WPARAM)MODULE, (LPARAM)MODULE);

	if (ServiceExists(MS_MSG_GETWINDOWDATA))
		g_IsSrmmWindowAPI = 1;
	else
		g_IsSrmmWindowAPI = 0;

	if (ServiceExists(MS_MSG_MOD_MESSAGEDIALOGOPENED))
		g_IsSrmmServiceAvail = 1;
	else
		g_IsSrmmServiceAvail = 0;

	return 0;
}

//---Called when an options dialog has to be created
int HookedOptions(WPARAM wParam, LPARAM lParam)
{
  OptionsAdd(hInst, wParam);

  return 0;
}

//---------------------------
//---Exported Functions

PLUGININFOEX* handleMirandaPluginInfo(DWORD mirandaVersion)
{
  if (mirandaVersion >= PLUGIN_MAKE_VERSION(0, 3, 3, 0))
  { // Are we running under Unicode Windows version ?
    if ((GetVersion() & 0x80000000) == 0)
    {
      pluginInfo.flags = 1; // UNICODE_AWARE
    }
	  return &pluginInfo;
  }
  else
    return NULL;
}

__declspec(dllexport) PLUGININFOEX* MirandaPluginInfoEx(DWORD mirandaVersion)
{
  pluginInfo.cbSize = sizeof(PLUGININFOEX);

  return handleMirandaPluginInfo(mirandaVersion);
}

static const MUUID interfaces[] = {MIID_EVENTNOTIFY, MIID_LAST};
__declspec(dllexport) const MUUID* MirandaPluginInterfaces(void)
{
	return interfaces;
}

int __declspec(dllexport) Load(void)
{

  hHookedInit = HookEvent(ME_SYSTEM_MODULESLOADED, HookedInit);
  hHookedOpt = HookEvent(ME_OPT_INITIALISE, HookedOptions);
  mir_getLP(&pluginInfo);

  InitI18N();

  { // Are we running under unicode Miranda core ?
    char szVer[MAX_PATH];

    CallService(MS_SYSTEM_GETVERSIONTEXT, MAX_PATH, (LPARAM)szVer);
    _strlwr(szVer); // make sure it is lowercase
    g_UnicodeCore = (strstr(szVer, "unicode") != NULL);
  }

  OptionsInit(&pluginOptions);
  pluginOptions.hInst = hInst;

  if (pluginOptions.bMenuitem)
    MenuitemInit(!pluginOptions.bDisable);

	return 0;
}

int __declspec(dllexport) Unload(void)
{
  UnhookEvent(hHookedNewEvent);
  UnhookEvent(hHookedOpt);
  UnhookEvent(hHookedInit);
	return 0;
}

BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
{
	hInst=hinstDLL;
	return TRUE;
}

//-------------------------------------
//---Check Window Message function

// Took this snippet of code from "EventNotify" by micron-x, thx *g*
// checks if the message-dialog window is already opened
// return values:
//	0 - No window found
//	1 - Split-mode window found
//	2 - Single-mode window found

int CheckMsgWnd(HANDLE hContact)
{
	if (g_IsSrmmWindowAPI) {
		MessageWindowData mwd;
		MessageWindowInputData mwid;
		mwid.cbSize = sizeof(MessageWindowInputData);
		mwid.hContact = hContact;
		mwid.uFlags = MSG_WINDOW_UFLAG_MSG_BOTH;
		mwd.cbSize = sizeof(MessageWindowData);
		mwd.hContact = hContact;
		if (!CallService(MS_MSG_GETWINDOWDATA, (WPARAM) &mwid, (LPARAM) &mwd)) {
			if (mwd.hwndWindow != NULL && (mwd.uState & MSG_WINDOW_STATE_EXISTS)) return 1;
		}
	}
	if (g_IsSrmmServiceAvail) {   // use the service provided by tabSRMM
		if (CallService(MS_MSG_MOD_MESSAGEDIALOGOPENED, (WPARAM)hContact, 0))
			return 1;
		else 
			return 0;
	}
	/*else 
	{	// old way: find it by using the window class & title
		TCHAR *newtitle = _T("");
		TCHAR *szStatus, *contactName;
		char *szProto;

		szProto= (char *)CallService(MS_PROTO_GETCONTACTBASEPROTO, (WPARAM)hContact,0);
		contactName= (TCHAR *)CallService(MS_CLIST_GETCONTACTDISPLAYNAME, (WPARAM)hContact,0);
		szStatus= (TCHAR *)CallService(MS_CLIST_GETSTATUSMODEDESCRIPTION, szProto ? DBGetContactSettingWord(hContact,szProto,"Status",ID_STATUS_OFFLINE) : ID_STATUS_OFFLINE,0);

	// vj: This code was added by preeze and it does not work:
	// vlko: it maybe work with other plugins 
		_sntprintf(newtitle,sizeof(newtitle),_T("%s  (%s)"),contactName,szStatus);
		if(FindWindowA("TMsgWindow",(char *)newtitle))
			return 2;

		_sntprintf(newtitle,sizeof(newtitle),_T("[%s  (%s)]"),contactName,szStatus);
		if(FindWindowA("TfrmContainer",(char *)newtitle))
			return 1;

	// vj: I have restored this code from original plugin's source: (NewEventNotify 0.0.4)
		_sntprintf(newtitle,sizeof(newtitle),_T("%s (%s): %s"),contactName,szStatus,Translate("Message Session"));
		if(FindWindowA("#32770",(char *)newtitle)) // JK, this works for old SRMMs (1.0.4.x) and for mine SRMMJ
			return 1;

		_sntprintf(newtitle,sizeof(newtitle),_T("%s (%s): %s"),contactName,szStatus,Translate("Message Received"));
		if(FindWindowA("#32770",(char *)newtitle))
			return 2;

		return 0;
	}*/
	return 1;
}