summaryrefslogtreecommitdiff
path: root/irc_mod/main.cpp
blob: 1f57f01ce2613fe59b54b1816265ec431d949337 (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
/*
IRC plugin for Miranda IM

Copyright (C) 2003 Jörgen Persson

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 "irc.h"

CIrcSession			g_ircSession=NULL;			// Representation of the IRC-connection
CMyMonitor			*monitor;					// Object that processes data from the IRC server
MM_INTERFACE		mmi = {0};					// structure which keeps pointers to mirandas alloc, free and realloc
char *				IRCPROTONAME = NULL; 
char *				ALTIRCPROTONAME = NULL;
char *				pszServerFile = NULL;
char *				pszPerformFile = NULL;
char *				pszIgnoreFile = NULL;
char				mirandapath[MAX_PATH];
DWORD				mirVersion = NULL;
CRITICAL_SECTION	cs;
CRITICAL_SECTION	m_gchook;
PLUGINLINK *		pluginLink;
HINSTANCE			g_hInstance = NULL;	
PREFERENCES			* prefs;

//static HMODULE	m_libeay32;
HMODULE				m_ssleay32 = NULL;

// NUDGE
HANDLE	 hIRCNudge = NULL;

// Unicode
BYTE gbUnicodeAPI;
BYTE gbUnicodeCore;
BYTE gbUtfLangpack;

PLUGININFO			pluginInfo=
{						// Information about the plugin
						sizeof( PLUGININFO ),
						#ifdef _UNICODE
							"IRC Protocol (Unicode)",
						#else
							"IRC Protocol",
						#endif
						PLUGIN_MAKE_VERSION( 0,7,0,11 ),
						"IRC protocol for Miranda IM. (Nudge, Typing Notify, xStatus, Avatars)",
						"MatriX, Angeli-Ka, prog, IRoN, etc",
						"wortex@users.sourceforge.net",
						"© 2003 - 2005 Jörgen Persson, © 2006 Miranda IM Community",
						"http://dev.mirandaim.ru/iron/",
						0,	
						0
}; 

extern "C" BOOL WINAPI DllMain(HINSTANCE hinstDLL,DWORD fdwReason,LPVOID lpvReserved)
{
	g_hInstance=hinstDLL;

	return TRUE;
}

extern "C" __declspec(dllexport) PLUGININFO* MirandaPluginInfo(DWORD mirandaVersion)
{
	mirVersion = mirandaVersion;
	return &pluginInfo;
}

static void GetModuleName( void )	 // ripped from msn
{
	char * p = NULL;
	char * p1 = NULL;

	GetModuleFileName(g_hInstance, mirandapath, MAX_PATH);
	p = strrchr( mirandapath, '\\' );
	if(p)
	{
		char * p2;
		*p = '\0';
		p++;
		p1 = strrchr( p, '.' );
		*p1 = '\0';
		p2 = p;
		while( *p2 )
		{
			if(*p2 == ' ')
				*p2 = '_';
			p2++;
		}
		IRCPROTONAME = strdup( p );
		ALTIRCPROTONAME = new char[lstrlen( IRCPROTONAME ) + 7 ];
		CharUpper(IRCPROTONAME);

		if (lstrcmpi(IRCPROTONAME, "IRC"))
			mir_snprintf(ALTIRCPROTONAME, lstrlen( IRCPROTONAME ) + 7 , "IRC (%s)", IRCPROTONAME);
		else
			mir_snprintf(ALTIRCPROTONAME, lstrlen( IRCPROTONAME ) + 7 , "%s", IRCPROTONAME);
	}
}

static void RegisterProtocol( void )
{
	PROTOCOLDESCRIPTOR pd;
	ZeroMemory( &pd, sizeof( pd ) );
	pd.cbSize = sizeof( pd );
	pd.szName = IRCPROTONAME;
	pd.type = PROTOTYPE_PROTOCOL;
	CallService( MS_PROTO_REGISTERMODULE, 0, (LPARAM)&pd );
}

extern "C" int __declspec(dllexport) Load( PLUGINLINK *link )
{
	#ifndef NDEBUG //mem leak detector :-) Thanks Tornado!
	int flag = _CrtSetDbgFlag(_CRTDBG_REPORT_FLAG); // Get current flag
	flag |= _CRTDBG_LEAK_CHECK_DF; // Turn on leak-checking bit
	_CrtSetDbgFlag(flag); // Set flag to the new value
	#endif

	pluginLink=link;

	// Are we running under Unicode Windows version ?
	gbUnicodeAPI = (GetVersion() & 0x80000000) == 0;
	#ifdef _DEBUG
		DBGprintf("UnicodeAPI = %d\n", gbUnicodeAPI);
	#endif

	// Do we have new LangPack module ready ?
	gbUtfLangpack = ServiceExists(MS_LANGPACK_GETCODEPAGE);
	{ // Are we running under unicode Miranda core ?
		char szVer[MAX_PATH];
		CallService(MS_SYSTEM_GETVERSIONTEXT, MAX_PATH, (LPARAM)szVer);
		gbUnicodeCore = (strstr(szVer, "Unicode") != NULL);
	}
	#ifdef _DEBUG
		DBGprintf("UnicodeCore = %d\n", gbUnicodeCore);
	#endif

	// Initialize charset conversion routines
	InitI18N();

	// Initialize core modules      
	InitDB();       // DB interface

	if( !mirVersion || mirVersion<PLUGIN_MAKE_VERSION( 0, 4, 0 ,0 ) ) 
	{
		char szVersion[] = "0.4"; // minimum required version
		char szText[] = "The IRC protocol could not be loaded as it is dependant on Miranda IM version %s or later.\n\nDo you want to download an update from the Miranda website now?";
		char * szTemp = new char[lstrlen (szVersion) + lstrlen(szText) + 10];
		mir_snprintf(szTemp, lstrlen (szVersion) + lstrlen(szText) + 10, szText, szVersion);
		if(IDYES == MessageBoxA(0,Translate(szTemp),Translate("Information"),MB_YESNO|MB_ICONINFORMATION))
			CallService(MS_UTILS_OPENURL, 1, (LPARAM) "http://miranda-im.org/");
		delete[] szTemp;
		return 1;
	}

	GetModuleName();
	UpgradeCheck();
	CallService(MS_SYSTEM_GET_MMI, 0, (LPARAM) &mmi);
	
	InitializeCriticalSection(&cs);
	InitializeCriticalSection(&m_gchook);

//#ifdef IRC_SSL
	m_ssleay32 = LoadLibrary("ssleay32.dll");
//#endif

	monitor = new CMyMonitor;
	g_ircSession.AddIrcMonitor(monitor);

// Nudge
	char nudge[250];
	sprintf(nudge,"%s/Nudge",IRCPROTONAME);
	hIRCNudge = CreateHookableEvent(nudge);

	RegisterProtocol();
	HookEvents();
	CreateServiceFunctions();
	InitPrefs();
	CList_SetAllOffline(true);

// This must be here - the events are called too early, WTF?            
	InitXStatusEvents();

	return 0;
}

extern "C" int __declspec(dllexport) Unload(void)
{
	CList_SetAllOffline(TRUE);

	DeleteCriticalSection(&cs);
	DeleteCriticalSection(&m_gchook);

	if(m_ssleay32)
		FreeLibrary(m_ssleay32);

	UnhookEvents();
	UnInitOptions();
	free( IRCPROTONAME );
	delete [] ALTIRCPROTONAME;
	delete monitor;

	return 0;
}

void UpgradeCheck(void)
{
	DWORD dwVersion = DBGetContactSettingDword(NULL, IRCPROTONAME, "OldVersion", PLUGIN_MAKE_VERSION(0,6,0,0));
	if(	pluginInfo.version > dwVersion)
	{
		if(dwVersion < PLUGIN_MAKE_VERSION(0,6,1,0))
		{
			DBDeleteContactSetting(NULL, IRCPROTONAME,	"OnlineNotificationTime");
			DBDeleteContactSetting(NULL, IRCPROTONAME,	"AutoOnlineNotifTempAlso");
			
		}
		if(dwVersion < PLUGIN_MAKE_VERSION(0,6,3,7))
		{
			DBVARIANT dbv;
			char pw[600] = {0};
			if(!DBGetContactSetting(NULL, IRCPROTONAME, "Password", &dbv) && dbv.type==DBVT_ASCIIZ)
			{
				lstrcpyn(pw, dbv.pszVal, 599);
				DBFreeVariant(&dbv);
			}
			if(lstrlenA(pw) > 0)
			{
				CallService(MS_DB_CRYPT_ENCODESTRING, 499, (LPARAM)pw);
				DBWriteContactSettingString(NULL, IRCPROTONAME, "Password", pw);
				MessageBoxA(NULL, Translate("To increase security the saved password for your\n default network is now encrypted."), IRCPROTONAME, MB_OK|MB_ICONINFORMATION);			
			}
		}		
	}
	DBWriteContactSettingDword(NULL, IRCPROTONAME, "OldVersion", pluginInfo.version);
	return;
}