summaryrefslogtreecommitdiff
path: root/protocols/IRCG/src/clist.cpp
blob: 3c3c0de534399c65d9c8e03772580c8cae22f17b (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

/*
IRC plugin for Miranda IM

Copyright (C) 2003-05 Jurgen Persson
Copyright (C) 2007-09 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.
*/

#include "irc.h"

BOOL CIrcProto::CList_AddDCCChat(const CMString& name, const CMString& hostmask, unsigned long adr, int port) 
{
	HANDLE hContact;
	HANDLE hc;
	TCHAR szNick[256];
	char szService[256];
	bool bFlag = false;

	CONTACT usertemp = { (TCHAR*)name.c_str(), NULL, NULL, false, false, true};
	hc = CList_FindContact( &usertemp );
	if ( hc && db_get_b( hc, "CList", "NotOnList", 0) == 0
		&& db_get_b(hc,"CList", "Hidden", 0) == 0)
	{
		bFlag = true;
	}

	CMString contactname = name; contactname += _T(DCCSTRING);

	CONTACT user = { (TCHAR*)contactname.c_str(), NULL, NULL, false, false, true};
	hContact = CList_AddContact(&user, false, false);
	setByte(hContact, "DCC", 1);

	DCCINFO* pdci = new DCCINFO;
	pdci->sHostmask = hostmask;
	pdci->hContact = hContact;
	pdci->dwAdr = (DWORD) adr;
	pdci->iPort = port;
	pdci->iType = DCC_CHAT;
	pdci->bSender = false;
	pdci->sContactName = name;

	if ( m_DCCChatAccept == 3 || m_DCCChatAccept == 2 && bFlag ) {
		CDccSession* dcc = new CDccSession( this, pdci );

		CDccSession* olddcc = FindDCCSession(hContact);
		if ( olddcc )
			olddcc->Disconnect();

		AddDCCSession(hContact, dcc);
		dcc->Connect();
		if (getByte( "MirVerAutoRequest", 1))
		  PostIrcMessage( _T("/PRIVMSG %s \001VERSION\001"), name.c_str());
	}
	else {
		CLISTEVENT cle = {0};
		cle.cbSize = sizeof(cle);
		cle.hContact = (HANDLE)hContact;
		cle.hDbEvent = (HANDLE)"dccchat";	
		cle.flags = CLEF_TCHAR;
		cle.hIcon = LoadIconEx(IDI_DCC);
		mir_snprintf( szService, sizeof(szService),"%s/DblClickEvent", m_szModuleName);
		cle.pszService = szService ;
		mir_sntprintf( szNick, SIZEOF(szNick), TranslateT("CTCP chat request from %s"), name.c_str());
		cle.ptszTooltip = szNick;
		cle.lParam = (LPARAM)pdci;

		if ( CallService(MS_CLIST_GETEVENT, (WPARAM)hContact, 0))
			CallService(MS_CLIST_REMOVEEVENT, (WPARAM)hContact, (LPARAM)"dccchat");
		CallService(MS_CLIST_ADDEVENT,(WPARAM) hContact,(LPARAM) &cle);
	}
	return TRUE;
}

HANDLE CIrcProto::CList_AddContact(CONTACT * user, bool InList, bool SetOnline)
{
	if (user->name == NULL)
		return 0;
	
	HANDLE hContact = CList_FindContact(user);
	if ( hContact ) {
		if ( InList )
			db_unset( hContact, "CList", "NotOnList" );
		setTString(hContact, "Nick", user->name);
		db_unset(hContact, "CList", "Hidden");
		if (SetOnline && getWord(hContact, "Status", ID_STATUS_OFFLINE)== ID_STATUS_OFFLINE)
			setWord(hContact, "Status", ID_STATUS_ONLINE);
		return hContact;
	}
	
	// here we create a new one since no one is to be found
	hContact = (HANDLE)CallService(MS_DB_CONTACT_ADD, 0, 0);
	if ( hContact ) {
		CallService(MS_PROTO_ADDTOCONTACT, (WPARAM) hContact, (LPARAM)m_szModuleName );

		if ( InList )
			db_unset(hContact, "CList", "NotOnList");
		else
			db_set_b(hContact, "CList", "NotOnList", 1);
		db_unset(hContact, "CList", "Hidden");
		setTString(hContact, "Nick", user->name);
		setTString(hContact, "Default", user->name);
		setWord(hContact, "Status", SetOnline ? ID_STATUS_ONLINE:ID_STATUS_OFFLINE);
		if ( !InList && getByte( "MirVerAutoRequestTemp", 0))
			PostIrcMessage( _T("/PRIVMSG %s \001VERSION\001"), user->name);
		return hContact;
	}
	return false;
}

HANDLE CIrcProto::CList_SetOffline(struct CONTACT * user)
{
	DBVARIANT dbv;
	HANDLE hContact = CList_FindContact(user);
	if ( hContact ) {
		if ( !getTString( hContact, "Default", &dbv )) {
			setString(hContact, "User", "");
			setString(hContact, "Host", "");
			setTString(hContact, "Nick", dbv.ptszVal);
			setWord(hContact, "Status", ID_STATUS_OFFLINE);
			db_free(&dbv);
			return hContact;
	}	}

	return 0;
}

bool CIrcProto::CList_SetAllOffline(BYTE ChatsToo)
{
	DBVARIANT dbv;

	DisconnectAllDCCSessions(false);

	for (HANDLE hContact = db_find_first(m_szModuleName); hContact; hContact = db_find_next(hContact, m_szModuleName)) {
		if ( getByte( hContact, "ChatRoom", 0 ))
			continue;

		if ( getByte(hContact, "DCC", 0 ) != 0 ) {
			if ( ChatsToo )
				setWord(hContact, "Status", ID_STATUS_OFFLINE);
		}
		else if ( !getTString( hContact, "Default", &dbv )) {
			setTString( hContact, "Nick", dbv.ptszVal);
			setWord( hContact, "Status", ID_STATUS_OFFLINE );
			db_free( &dbv );
		}
		db_unset( hContact, m_szModuleName, "IP" );
		setString( hContact, "User", "" );
		setString( hContact, "Host", "" );
	}
	return true;
}

HANDLE CIrcProto::CList_FindContact (CONTACT* user) 
{
	if ( !user || !user->name )
		return 0;
	
	TCHAR* lowercasename = mir_tstrdup( user->name );
	CharLower(lowercasename);
	
	DBVARIANT dbv1;
	DBVARIANT dbv2;	
	DBVARIANT dbv3;	
	DBVARIANT dbv4;	
	DBVARIANT dbv5;	

	for (HANDLE hContact = db_find_first(m_szModuleName); hContact; hContact = db_find_next(hContact, m_szModuleName)) {
		if ( getByte( hContact, "ChatRoom", 0))
			continue;

		HANDLE hContact_temp = NULL;
		TCHAR* DBDefault = NULL;
		TCHAR* DBNick = NULL;
		TCHAR* DBWildcard = NULL;
		TCHAR* DBUser = NULL;
		TCHAR* DBHost = NULL;
		if ( !getTString(hContact, "Default",   &dbv1)) DBDefault = dbv1.ptszVal;
		if ( !getTString(hContact, "Nick",      &dbv2)) DBNick = dbv2.ptszVal;
		if ( !getTString(hContact, "UWildcard", &dbv3)) DBWildcard = dbv3.ptszVal;
		if ( !getTString(hContact, "UUser",     &dbv4)) DBUser = dbv4.ptszVal;
		if ( !getTString(hContact, "UHost",     &dbv5)) DBHost = dbv5.ptszVal;
				
		if ( DBWildcard )
			CharLower( DBWildcard );
		if ( IsChannel( user->name )) {
			if ( DBDefault && !lstrcmpi( DBDefault, user->name ))
				hContact_temp = (HANDLE)-1;
		}
		else if ( user->ExactNick && DBNick && !lstrcmpi( DBNick, user->name ))
			hContact_temp = hContact;
				
		else if ( user->ExactOnly && DBDefault && !lstrcmpi( DBDefault, user->name ))
			hContact_temp = hContact;
			
		else if ( user->ExactWCOnly ) {
			if ( DBWildcard && !lstrcmpi( DBWildcard, lowercasename ) 
				|| ( DBWildcard && !lstrcmpi( DBNick, lowercasename ) && !WCCmp( DBWildcard, lowercasename ))
				|| ( !DBWildcard && !lstrcmpi(DBNick, lowercasename)))
			{
				hContact_temp = hContact;
			}
		}
		else if ( _tcschr(user->name, ' ' ) == 0 ) {
			if (( DBDefault && !lstrcmpi(DBDefault, user->name) || DBNick && !lstrcmpi(DBNick, user->name) || 
					DBWildcard && WCCmp( DBWildcard, lowercasename ))
				&& ( WCCmp(DBUser, user->user) && WCCmp(DBHost, user->host)))
			{
				hContact_temp = hContact;
		}	}

		if ( DBDefault )   db_free(&dbv1);
		if ( DBNick )      db_free(&dbv2);
		if ( DBWildcard )  db_free(&dbv3);
		if ( DBUser )      db_free(&dbv4);
		if ( DBHost )      db_free(&dbv5);

		if ( hContact_temp != NULL ) {
			mir_free(lowercasename);
			if ( hContact_temp != (HANDLE)-1 )
				return hContact_temp;
			return 0;
		}
	}
	mir_free(lowercasename);
	return 0;
}