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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
|
/*
Jabber Protocol Plugin for Miranda IM
Copyright ( C ) 2002-04 Santithorn Bunchua
Copyright ( C ) 2005-06 George Hazan
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.
File name : $Source: /cvsroot/miranda/miranda/protocols/JabberG/jabber.cpp,v $
Revision : $Revision: 3611 $
Last change on : $Date: 2006-08-27 23:36:15 +0400 (Вск, 27 Авг 2006) $
Last change by : $Author: ghazan $
*/
#include "jabber.h"
#include "jabber_ssl.h"
#include "jabber_iq.h"
#include "resource.h"
#include "version.h"
HINSTANCE hInst;
PLUGINLINK *pluginLink;
PLUGININFO pluginInfo = {
sizeof( PLUGININFO ),
#if defined( _UNICODE )
"Jabber Protocol (Unicode)",
#else
"Jabber Protocol",
#endif
__VERSION_DWORD,
"Jabber protocol plugin for Miranda IM ( "__DATE__" )",
"George Hazan",
"ghazan@miranda-im.org",
"( c ) 2002-05 Santithorn Bunchua, George Hazan",
"http://miranda-im.org/download/details.php?action=viewfile&id=437",
0,
0
};
MM_INTERFACE memoryManagerInterface;
LIST_INTERFACE li;
HANDLE hMainThread = NULL;
DWORD jabberMainThreadId;
char* jabberProtoName; // "JABBER"
char* jabberModuleName; // "Jabber"
CRITICAL_SECTION mutex;
HANDLE hNetlibUser;
// Main jabber server connection thread global variables
struct ThreadData *jabberThreadInfo = NULL;
BOOL jabberConnected = FALSE;
time_t jabberLoggedInTime = 0;
BOOL jabberOnline = FALSE;
BOOL jabberChatDllPresent = FALSE;
int jabberStatus = ID_STATUS_OFFLINE;
int jabberDesiredStatus;
BOOL modeMsgStatusChangePending = FALSE;
BOOL jabberChangeStatusMessageOnly = FALSE;
TCHAR* jabberJID = NULL;
char* streamId = NULL;
DWORD jabberLocalIP;
UINT jabberCodePage;
JABBER_MODEMSGS modeMsgs;
//char* jabberModeMsg;
CRITICAL_SECTION modeMsgMutex;
char* jabberVcardPhotoFileName = NULL;
char* jabberVcardPhotoType = NULL;
BOOL jabberSendKeepAlive;
// SSL-related global variable
HMODULE hLibSSL = NULL;
PVOID jabberSslCtx;
const char xmlnsAdmin[] = "http://jabber.org/protocol/muc#admin";
const char xmlnsOwner[] = "http://jabber.org/protocol/muc#owner";
HWND hwndJabberAgents = NULL;
HWND hwndJabberGroupchat = NULL;
HWND hwndJabberJoinGroupchat = NULL;
HWND hwndAgentReg = NULL;
HWND hwndAgentRegInput = NULL;
HWND hwndAgentManualReg = NULL;
HWND hwndRegProgress = NULL;
HWND hwndJabberVcard = NULL;
HWND hwndMucVoiceList = NULL;
HWND hwndMucMemberList = NULL;
HWND hwndMucModeratorList = NULL;
HWND hwndMucBanList = NULL;
HWND hwndMucAdminList = NULL;
HWND hwndMucOwnerList = NULL;
HWND hwndJabberChangePassword = NULL;
// Service and event handles
HANDLE heventRawXMLIn;
HANDLE heventRawXMLOut;
int JabberOptInit( WPARAM wParam, LPARAM lParam );
int JabberUserInfoInit( WPARAM wParam, LPARAM lParam );
int JabberMsgUserTyping( WPARAM wParam, LPARAM lParam );
void JabberMenuInit( void );
int JabberSvcInit( void );
int JabberSvcUninit( void );
extern "C" BOOL WINAPI DllMain( HINSTANCE hModule, DWORD dwReason, LPVOID lpvReserved )
{
#ifdef _DEBUG
_CrtSetDbgFlag( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF );
#endif
hInst = hModule;
return TRUE;
}
extern "C" __declspec( dllexport ) PLUGININFO *MirandaPluginInfo( DWORD mirandaVersion )
{
if ( mirandaVersion < PLUGIN_MAKE_VERSION( 0,5,0,0 )) {
MessageBoxA( NULL, "The Jabber protocol plugin cannot be loaded. It requires Miranda IM 0.5 or later.", "Jabber Protocol Plugin", MB_OK|MB_ICONWARNING|MB_SETFOREGROUND|MB_TOPMOST );
return NULL;
}
return &pluginInfo;
}
///////////////////////////////////////////////////////////////////////////////
// OnPreShutdown - prepares Miranda to be shut down
static int OnPreShutdown( WPARAM wParam, LPARAM lParam )
{
if ( hwndJabberAgents ) SendMessage( hwndJabberAgents, WM_CLOSE, 0, 0 );
if ( hwndJabberGroupchat ) SendMessage( hwndJabberGroupchat, WM_CLOSE, 0, 0 );
if ( hwndJabberJoinGroupchat ) SendMessage( hwndJabberJoinGroupchat, WM_CLOSE, 0, 0 );
if ( hwndAgentReg ) SendMessage( hwndAgentReg, WM_CLOSE, 0, 0 );
if ( hwndAgentRegInput ) SendMessage( hwndAgentRegInput, WM_CLOSE, 0, 0 );
if ( hwndRegProgress ) SendMessage( hwndRegProgress, WM_CLOSE, 0, 0 );
if ( hwndJabberVcard ) SendMessage( hwndJabberVcard, WM_CLOSE, 0, 0 );
if ( hwndMucVoiceList ) SendMessage( hwndMucVoiceList, WM_CLOSE, 0, 0 );
if ( hwndMucMemberList ) SendMessage( hwndMucMemberList, WM_CLOSE, 0, 0 );
if ( hwndMucModeratorList ) SendMessage( hwndMucModeratorList, WM_CLOSE, 0, 0 );
if ( hwndMucBanList ) SendMessage( hwndMucBanList, WM_CLOSE, 0, 0 );
if ( hwndMucAdminList ) SendMessage( hwndMucAdminList, WM_CLOSE, 0, 0 );
if ( hwndMucOwnerList ) SendMessage( hwndMucOwnerList, WM_CLOSE, 0, 0 );
if ( hwndJabberChangePassword ) SendMessage( hwndJabberChangePassword, WM_CLOSE, 0, 0 );
hwndJabberAgents = NULL;
hwndJabberGroupchat = NULL;
hwndJabberJoinGroupchat = NULL;
hwndAgentReg = NULL;
hwndAgentRegInput = NULL;
hwndAgentManualReg = NULL;
hwndRegProgress = NULL;
hwndJabberVcard = NULL;
hwndMucVoiceList = NULL;
hwndMucMemberList = NULL;
hwndMucModeratorList = NULL;
hwndMucBanList = NULL;
hwndMucAdminList = NULL;
hwndMucOwnerList = NULL;
hwndJabberChangePassword = NULL;
return 0;
}
///////////////////////////////////////////////////////////////////////////////
// OnModulesLoaded - execute some code when all plugins are initialized
int JabberGcEventHook( WPARAM, LPARAM );
int JabberGcMenuHook( WPARAM, LPARAM );
int JabberGcInit( WPARAM, LPARAM );
static COLORREF crCols[16] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
HANDLE hChatEvent = NULL,
hChatMenu = NULL,
hChatMess = NULL,
hInitChat = NULL,
hEvInitChat = NULL,
hEvModulesLoaded = NULL,
hEvOptInit = NULL,
hEvPreShutdown = NULL,
hEvUserInfoInit = NULL;
static int OnModulesLoaded( WPARAM wParam, LPARAM lParam )
{
JabberWsInit();
JabberSslInit();
HookEvent( ME_USERINFO_INITIALISE, JabberUserInfoInit );
if ( ServiceExists( MS_GC_REGISTER )) {
jabberChatDllPresent = true;
GCREGISTER gcr = {0};
gcr.cbSize = sizeof( GCREGISTER );
gcr.dwFlags = GC_TYPNOTIF|GC_CHANMGR;
gcr.iMaxText = 0;
gcr.nColors = 16;
gcr.pColors = &crCols[0];
gcr.pszModuleDispName = jabberProtoName;
gcr.pszModule = jabberProtoName;
JCallService( MS_GC_REGISTER, NULL, ( LPARAM )&gcr );
hChatEvent = HookEvent( ME_GC_EVENT, JabberGcEventHook );
hChatMenu = HookEvent( ME_GC_BUILDMENU, JabberGcMenuHook );
char szEvent[ 200 ];
mir_snprintf( szEvent, sizeof szEvent, "%s\\ChatInit", jabberProtoName );
hInitChat = CreateHookableEvent( szEvent );
hEvInitChat = HookEvent( szEvent, JabberGcInit );
}
return 0;
}
///////////////////////////////////////////////////////////////////////////////
// OnLoad - initialize the plugin instance
extern "C" int __declspec( dllexport ) Load( PLUGINLINK *link )
{
pluginLink = link;
// set the memory manager
memoryManagerInterface.cbSize = sizeof(MM_INTERFACE);
JCallService(MS_SYSTEM_GET_MMI,0,(LPARAM)&memoryManagerInterface);
// set the lists manager;
li.cbSize = sizeof( li );
if ( CallService(MS_SYSTEM_GET_LI,0,(LPARAM)&li) == CALLSERVICE_NOTFOUND ) {
MessageBoxA( NULL, "This plugin requires Miranda IM 0.5 or later", "Fatal error", MB_OK );
return 1;
}
if ( !ServiceExists( MS_DB_CONTACT_GETSETTING_STR )) {
MessageBoxA( NULL, "This plugin requires db3x plugin version 0.5.1.0 or later", "Jabber", MB_OK );
return 1;
}
char text[_MAX_PATH];
char* p, *q;
GetModuleFileNameA( hInst, text, sizeof( text ));
p = strrchr( text, '\\' );
p++;
q = strrchr( p, '.' );
*q = '\0';
jabberProtoName = mir_strdup( p );
_strupr( jabberProtoName );
mir_snprintf( text, sizeof( text ), "%s/Status", jabberProtoName );
JCallService( MS_DB_SETSETTINGRESIDENT, TRUE, ( LPARAM )text );
jabberModuleName = mir_strdup( jabberProtoName );
_strlwr( jabberModuleName );
jabberModuleName[0] = toupper( jabberModuleName[0] );
JabberLog( "Setting protocol/module name to '%s/%s'", jabberProtoName, jabberModuleName );
DuplicateHandle( GetCurrentProcess(), GetCurrentThread(), GetCurrentProcess(), &hMainThread, THREAD_SET_CONTEXT, FALSE, 0 );
jabberMainThreadId = GetCurrentThreadId();
hEvOptInit = HookEvent( ME_OPT_INITIALISE, JabberOptInit );
hEvModulesLoaded = HookEvent( ME_SYSTEM_MODULESLOADED, OnModulesLoaded );
hEvPreShutdown = HookEvent( ME_SYSTEM_PRESHUTDOWN, OnPreShutdown );
// Register protocol module
PROTOCOLDESCRIPTOR pd;
ZeroMemory( &pd, sizeof( PROTOCOLDESCRIPTOR ));
pd.cbSize = sizeof( PROTOCOLDESCRIPTOR );
pd.szName = jabberProtoName;
pd.type = PROTOTYPE_PROTOCOL;
JCallService( MS_PROTO_REGISTERMODULE, 0, ( LPARAM )&pd );
// Set all contacts to offline
HANDLE hContact = ( HANDLE ) JCallService( MS_DB_CONTACT_FINDFIRST, 0, 0 );
while ( hContact != NULL ) {
char* szProto = ( char* )JCallService( MS_PROTO_GETCONTACTBASEPROTO, ( WPARAM ) hContact, 0 );
if ( szProto != NULL && !strcmp( szProto, jabberProtoName ))
if ( JGetWord( hContact, "Status", ID_STATUS_OFFLINE ) != ID_STATUS_OFFLINE )
JSetWord( hContact, "Status", ID_STATUS_OFFLINE );
hContact = ( HANDLE ) JCallService( MS_DB_CONTACT_FINDNEXT, ( WPARAM ) hContact, 0 );
}
memset(( char* )&modeMsgs, 0, sizeof( JABBER_MODEMSGS ));
//jabberModeMsg = NULL;
jabberCodePage = JGetWord( NULL, "CodePage", CP_ACP );
InitializeCriticalSection( &mutex );
InitializeCriticalSection( &modeMsgMutex );
srand(( unsigned ) time( NULL ));
JabberSerialInit();
JabberIqInit();
JabberListInit();
JabberSvcInit();
JabberMenuInit();
return 0;
}
///////////////////////////////////////////////////////////////////////////////
// Unload - destroy the plugin instance
extern "C" int __declspec( dllexport ) Unload( void )
{
if ( hChatEvent ) UnhookEvent( hChatEvent );
if ( hChatMenu ) UnhookEvent( hChatMenu );
if ( hChatMess ) UnhookEvent( hChatMess );
if ( hEvInitChat ) UnhookEvent( hEvInitChat );
if ( hEvModulesLoaded ) UnhookEvent( hEvModulesLoaded );
if ( hEvOptInit ) UnhookEvent( hEvOptInit );
if ( hEvPreShutdown ) UnhookEvent( hEvPreShutdown );
if ( hEvUserInfoInit ) UnhookEvent( hEvUserInfoInit );
if ( hInitChat )
DestroyHookableEvent( hInitChat );
JabberSvcUninit();
JabberSslUninit();
JabberListUninit();
JabberIqUninit();
JabberSerialUninit();
JabberWsUninit();
DeleteCriticalSection( &modeMsgMutex );
DeleteCriticalSection( &mutex );
mir_free( modeMsgs.szOnline );
mir_free( modeMsgs.szAway );
mir_free( modeMsgs.szNa );
mir_free( modeMsgs.szDnd );
mir_free( modeMsgs.szFreechat );
mir_free( jabberModuleName );
mir_free( jabberProtoName );
if ( jabberVcardPhotoFileName ) {
DeleteFileA( jabberVcardPhotoFileName );
mir_free( jabberVcardPhotoFileName );
}
if ( jabberVcardPhotoType ) mir_free( jabberVcardPhotoType );
if ( streamId ) mir_free( streamId );
if ( hMainThread ) CloseHandle( hMainThread );
return 0;
}
|