-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTapiEventProcessor.cpp
More file actions
303 lines (246 loc) · 7.85 KB
/
TapiEventProcessor.cpp
File metadata and controls
303 lines (246 loc) · 7.85 KB
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
/*
* Copyright (C) 2003, 2004 Amit Schreiber <gnobal@yahoo.com>
*
* This file is part of Caller ID plugin for Miranda IM.
*
* Caller ID plugin for Miranda IM 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.
*
* Caller ID plugin for Miranda IM 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 Caller ID plugin for Miranda IM ; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "TapiEventProcessor.h"
#include "random/plugins/newpluginapi.h"
#include "m_callerid.h"
#include <string>
#include <memory>
using std::auto_ptr;
using std::string;
CComAutoCriticalSection csGlobals;
CComPtr<ITCallInfo> g_pCurrCallInfo;
int g_nNumRings = 0;
BOOL g_bCallWasNotified = FALSE;
HRESULT HandleEvent(TAPI_EVENT TapiEvent, IDispatch* pEvent)
{
USES_CONVERSION;
HRESULT hr = S_OK;
CComQIPtr<ITCallInfo> pCallInfo;
if (TapiEvent == TE_CALLNOTIFICATION)
{
CComQIPtr<ITCallNotificationEvent> pNotification = pEvent;
ATLASSERT(pNotification != NULL);
if (pNotification == NULL)
return E_POINTER;
hr = pNotification->get_Call(&pCallInfo);
ATLASSERT(SUCCEEDED(hr));
if (FAILED(hr))
return hr;
csGlobals.Lock();
// ATLASSERT(g_pCurrCallInfo == NULL);
g_pCurrCallInfo = pCallInfo;
g_bCallWasNotified = FALSE;
g_nNumRings = 0;
csGlobals.Unlock();
// Return quickly here... usually no caller ID information will be available now
// so we just save the CallInfo object for later in hope that it'll be quick enough
// to catch the TE_CALLINFOCHANGE event
// If it's not quick enough, we'll get the information in the next ring
// This is a known annoyance in the TAPI 3 events handling, which forces to save
// the latest call in a global variable
return S_OK;
}
else if (TapiEvent == TE_CALLINFOCHANGE)
{
CComQIPtr<ITCallInfoChangeEvent> pNotification = pEvent;
ATLASSERT(pNotification != NULL);
if (pNotification == NULL)
return E_POINTER;
CALLINFOCHANGE_CAUSE eCause = CIC_OTHER;
hr = pNotification->get_Cause(&eCause);
ATLASSERT(SUCCEEDED(hr));
if (FAILED(hr))
return hr;
if (eCause != CIC_CALLERID)
return S_OK;
hr = pNotification->get_Call(&pCallInfo);
ATLASSERT(SUCCEEDED(hr));
if (FAILED(hr))
return hr;
}
else if (TapiEvent == TE_ADDRESS)
{
CComQIPtr<ITAddressEvent> pNotification = pEvent;
ATLASSERT(pNotification != NULL);
if (pNotification == NULL)
return E_POINTER;
ADDRESS_EVENT eEvent = AE_STATE;
hr = pNotification->get_Event(&eEvent);
ATLASSERT(SUCCEEDED(hr));
if (FAILED(hr))
return hr;
if (eEvent != AE_RINGING)
return S_OK;
csGlobals.Lock();
pCallInfo = g_pCurrCallInfo;
g_nNumRings++;
csGlobals.Unlock();
}
else if (TapiEvent == TE_CALLSTATE)
{
CComQIPtr<ITCallStateEvent> pNotification = pEvent;
ATLASSERT(pNotification != NULL);
if (pNotification == NULL)
return E_POINTER;
hr = pNotification->get_Call(&pCallInfo);
ATLASSERT(SUCCEEDED(hr));
if (FAILED(hr))
return hr;
CALL_STATE eState;
hr = pCallInfo->get_CallState(&eState);
ATLASSERT(SUCCEEDED(hr));
if (FAILED(hr))
return hr;
if (eState != CS_DISCONNECTED)
return S_OK;
csGlobals.Lock();
g_pCurrCallInfo = (ITCallInfo*) NULL;
g_nNumRings = 0;
csGlobals.Unlock();
return S_OK;
}
// if (pCallInfo == NULL) // Happens after we've already notified the event
// return S_OK;
CComBSTR bstrString;
hr = pCallInfo->get_CallInfoString(CIS_CALLERIDNUMBER, &bstrString);
csGlobals.Lock();
// Do not notify the call:
// 1. Again
// 2. If there's no call info available and the phone rang less than twice (give
// caller ID info time to become available)
if ((bstrString.Length() == 0 && g_nNumRings < 2) || g_bCallWasNotified == TRUE)
{
csGlobals.Unlock();
return S_OK;
}
csGlobals.Unlock();
CALLERIDINCOMINGCALLINFO ici = {0};
ici.cbSize = sizeof(CALLERIDINCOMINGCALLINFO);
if (bstrString.m_str != NULL)
{
ici.szCallerIdNumber = OLE2CA(bstrString.m_str);
}
else
{
ici.szCallerIdNumber = "";
}
NotifyEventHooks(g_hEventIncomingCall, 0, reinterpret_cast<LPARAM>(&ici));
bstrString.Empty();
csGlobals.Lock();
// g_pCurrCallInfo = NULL;
g_nNumRings = 0;
g_bCallWasNotified = TRUE;
csGlobals.Unlock();
return S_OK;
}
DWORD WINAPI EventProcessingThreadProc(LPVOID lpParameter)
{
MSG msg;
while (GetMessage(&msg, NULL, 0, 0))
{
if (msg.message == WM_USERTAPIEVENT)
{
TAPI_EVENT TapiEvent = (TAPI_EVENT)msg.wParam;
IDispatch *pEvent = (IDispatch*)msg.lParam;
HandleEvent(TapiEvent, pEvent);
pEvent->Release();
pEvent = NULL;
}
}
g_pCurrCallInfo = NULL; // Release (in case we're in the middle of handling an incoming call)
return 0;
}
/*
hr = pCallInfo->get_CallInfoString(CIS_CALLEDIDNAME, &bstrString);
bstrString.Empty();
hr = pCallInfo->get_CallInfoString(CIS_CALLEDIDNUMBER, &bstrString);
bstrString.Empty();
hr = pCallInfo->get_CallInfoString(CIS_CONNECTEDIDNAME, &bstrString);
bstrString.Empty();
hr = pCallInfo->get_CallInfoString(CIS_CONNECTEDIDNUMBER, &bstrString);
bstrString.Empty();
hr = pCallInfo->get_CallInfoString(CIS_REDIRECTIONIDNAME, &bstrString);
bstrString.Empty();
hr = pCallInfo->get_CallInfoString(CIS_REDIRECTIONIDNUMBER, &bstrString);
bstrString.Empty();
hr = pCallInfo->get_CallInfoString(CIS_REDIRECTINGIDNAME, &bstrString);
bstrString.Empty();
hr = pCallInfo->get_CallInfoString(CIS_REDIRECTINGIDNUMBER, &bstrString);
bstrString.Empty();
hr = pCallInfo->get_CallInfoString(CIS_CALLEDPARTYFRIENDLYNAME, &bstrString);
bstrString.Empty();
hr = pCallInfo->get_CallInfoString(CIS_COMMENT, &bstrString);
bstrString.Empty();
hr = pCallInfo->get_CallInfoString(CIS_DISPLAYABLEADDRESS, &bstrString);
bstrString.Empty();
hr = pCallInfo->get_CallInfoString(CIS_CALLINGPARTYID, &bstrString);
bstrString.Empty();
*/
/* // Get the call info using the VB interface (TE_ADDRESS)
CComPtr<ITAddress> pAddress;
hr = pNotification->get_Address(&pAddress);
ATLASSERT(SUCCEEDED(hr));
if (FAILED(hr))
return hr;
CComVariant varCalls;
hr = pAddress->get_Calls(&varCalls);
ATLASSERT(SUCCEEDED(hr));
if (FAILED(hr))
return hr;
CComQIPtr<ITCollection> pEnumCalls = varCalls.pdispVal;
ATLASSERT(pEnumCalls != NULL);
if (pEnumCalls == NULL)
return E_POINTER;
long lCallCount = 0;
hr = pEnumCalls->get_Count(&lCallCount);
ATLASSERT(SUCCEEDED(hr));
if (FAILED(hr))
return hr;
if (lCallCount == 0)
return S_OK;
CComVariant varCall;
hr = pEnumCalls->get_Item(1, &varCall);
ATLASSERT(SUCCEEDED(hr));
if (FAILED(hr))
return hr;
pCallInfo = varCall.pdispVal;
ATLASSERT(pCallInfo != NULL);
if (pCallInfo == NULL)
return E_POINTER;
*/
/* // Get the call info using the C++ interface TE_ADDRESS)
CComPtr<ITAddress> pAddress;
hr = pNotification->get_Address(&pAddress);
ATLASSERT(SUCCEEDED(hr));
if (FAILED(hr))
return hr;
CComPtr<IEnumCall> pEnumCalls;
hr = pAddress->EnumerateCalls(&pEnumCalls);
ATLASSERT(SUCCEEDED(hr));
if (FAILED(hr))
return hr;
ULONG ulFetched = 0;
hr = pEnumCalls->Next(1, &pCallInfo, &ulFetched);
ATLASSERT(SUCCEEDED(hr));
if (FAILED(hr))
return hr;
if (hr == S_FALSE)
return S_OK;
*/