summaryrefslogtreecommitdiffstats
path: root/src/kvilib/irc/kvi_nickserv.cpp
blob: a1809f84309d9f8018b7eebaef33f6c302debeef (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
//=============================================================================
//
//   File : kvi_nickserv.cpp
//   Creation date : Thu Aug 09 2001 17:44:56 by Szymon Stefanek
//
//   This file is part of the KVirc irc client distribution
//   Copyright (C) 2001-2004 Szymon Stefanek (pragma at kvirc dot net)
//
//   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 opinion) 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. ,51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
//
//=============================================================================

#define __KVILIB__


#include "kvi_nickserv.h"
#include "kvi_config.h"
#include "kvi_ircmask.h"

#include <qregexp.h>



/*
	@doc: nickserv_proto
	@title:
		Authentication with NickServ
	@keyterms:
		NickServ, automatic authentication with NickServ
	@type:
		generic
	@short:
		Automatic authentication with NickServ
	@body:
		KVIrc supports automatic authentication with the NickServ service.[br]
		This service is commonly implemented on major IRC networks: basically
		it is a program that allows users to register their nickname and protect
		it from being stolen by others.[br] The NickServ protocol is
		not standardized (at the time that I'm writing this doc) and automatic
		authentication is a pure experimental protocol.[br]
		Once you get on IRC with a registered nickname , the NickServ will
		ask you for identification by sending you a NOTICE.[br]
		The message will look in a way similar to the following:[br]
		<b>You're using a registered nickname: if this is your nick,
		please type /msg NickServ IDENTIFY password, otherwise please
		choose another nickname</b>.[br]
		The message is often broken in two or three lines of text.[br]
		Please note that many network policies suggest to avoid automatic authentication
		with NickServ.[br]I have implemented it because I know that it works on the networks
		that I'm usually on.[br]You have to check that this protocol works on your network and
		then eventually use it at your own risk.[br]
*/


// FIXME: The doc above is a bit outdated , fix it

KviNickServRuleSet::KviNickServRuleSet()
: KviHeapObject()
{
	m_bEnabled = false;
	m_pRules = 0;
}

KviNickServRuleSet::KviNickServRuleSet(const KviNickServRuleSet &s)
{
	m_pRules = 0;
	copyFrom(s);
}


KviNickServRuleSet::~KviNickServRuleSet()
{
	if(m_pRules)delete m_pRules;
}

void KviNickServRuleSet::save(KviConfig * cfg,const QString &prefix)
{
	if(!m_pRules)return; // nothing to save
	if(m_pRules->isEmpty())return; // should never happen anyway
	QString tmp;
	if(m_bEnabled)
	{
		KviQString::sprintf(tmp,"%QNSEnabled",&prefix);
		cfg->writeEntry(tmp,m_bEnabled);
	}
	KviQString::sprintf(tmp,"%QNSRules",&prefix);
	cfg->writeEntry(tmp,m_pRules->count());
	int idx = 0;
	for(KviNickServRule * r = m_pRules->first();r;r = m_pRules->next())
	{
		KviQString::sprintf(tmp,"%QNSRule%d_",&prefix,idx);
		r->save(cfg,tmp);
		idx++;
	}
}

KviNickServRuleSet * KviNickServRuleSet::load(KviConfig * cfg,const QString &prefix)
{
	QString tmp;
	KviQString::sprintf(tmp,"%QNSRules",&prefix);
	unsigned int cnt = cfg->readUIntEntry(tmp,0);
	if(cnt == 0)return 0;
	KviNickServRuleSet * s = new KviNickServRuleSet();
	if(s->loadPrivate(cfg,prefix,cnt))return s;
	delete s;
	return 0;
}

void KviNickServRuleSet::load(const QString &szConfigFile)
{
	clear();
	KviConfig cfg(szConfigFile,KviConfig::Read);

	QString tmp;
	KviQString::sprintf(tmp,"NSRules");
	unsigned int cnt = cfg.readUIntEntry(tmp,0);
	if(cnt == 0)return;
	loadPrivate(&cfg,QString(""),cnt);
}

void KviNickServRuleSet::save(const QString &szConfigFile)
{
	KviConfig cfg(szConfigFile,KviConfig::Write);
	cfg.clear();
	save(&cfg,QString(""));
}

bool KviNickServRuleSet::loadPrivate(KviConfig * cfg,const QString &prefix,unsigned int nEntries)
{
	if(m_pRules)m_pRules->clear();
	else {
		m_pRules = new KviPointerList<KviNickServRule>;
		m_pRules->setAutoDelete(true);
	}

	if(nEntries != 0)
	{
		QString tmp;
		KviQString::sprintf(tmp,"%QNSEnabled",&prefix);
		m_bEnabled = cfg->readBoolEntry(tmp,false);
		for(unsigned int u=0;u<nEntries;u++)
		{
			KviQString::sprintf(tmp,"%QNSRule%u_",&prefix,u);
			KviNickServRule * r = new KviNickServRule();
			if(!r->load(cfg,tmp))delete r;
			else m_pRules->append(r);
		}
	}

	if(m_pRules->isEmpty())
	{
		m_bEnabled = false;
		delete m_pRules;
		m_pRules = 0;
		return false;
	}
	return true;
}

void KviNickServRuleSet::clear()
{
	if(m_pRules)
	{
		delete m_pRules;
		m_pRules = 0;
	}
	m_bEnabled = false;
}

void KviNickServRuleSet::addRule(KviNickServRule * r)
{
	if(!m_pRules)
	{
		m_pRules = new KviPointerList<KviNickServRule>;
		m_pRules->setAutoDelete(true);
	}
	m_pRules->append(r);
}

KviNickServRuleSet * KviNickServRuleSet::createInstance()
{
	return new KviNickServRuleSet();
}


KviNickServRule * KviNickServRuleSet::matchRule(const QString &szNick,const KviIrcMask *nickServ,const QString &szMsg,const QString &szServer)
{
	if(!m_pRules)return 0;
	for(KviNickServRule *r = m_pRules->first();r;r = m_pRules->next())
	{
		if(!KviQString::matchStringCI(r->registeredNick(),szNick,false,true)) continue;
		if(!szServer.isEmpty())
		{
#ifdef COMPILE_USE_QT4
			QRegExp res(r->serverMask(),Qt::CaseInsensitive,QRegExp::Wildcard);
#else
			QRegExp res(r->serverMask(),false,true);
#endif
			if(!res.exactMatch(szServer))continue;
		}
		if(!nickServ->matchedBy(KviIrcMask(r->nickServMask())))continue;
#ifdef COMPILE_USE_QT4
		QRegExp re(r->messageRegexp(),Qt::CaseInsensitive,QRegExp::Wildcard);
#else
		QRegExp re(r->messageRegexp(),false,true);
#endif
		if(re.exactMatch(szMsg))return r;
	}
	return 0;
}

void KviNickServRuleSet::copyFrom(const KviNickServRuleSet &src)
{
	if(src.m_pRules)
	{
		if(m_pRules)m_pRules->clear();
		else {
			m_pRules = new KviPointerList<KviNickServRule>;
			m_pRules->setAutoDelete(true);
		}
		for(KviNickServRule * r = src.m_pRules->first();r;r = src.m_pRules->next())
		{
			KviNickServRule * c = new KviNickServRule();
			c->copyFrom(*r);
			m_pRules->append(c);
		}
		if(m_pRules->isEmpty())
		{
			m_bEnabled = false;
			delete m_pRules;
			m_pRules = 0;
		} else {
			m_bEnabled = src.m_bEnabled;
		}
	} else {
		m_bEnabled = false;
		if(m_pRules)
		{
			delete m_pRules;
			m_pRules = 0;
		}
	}
}


void KviNickServRule::copyFrom(const KviNickServRule &src)
{
	m_szRegisteredNick = src.m_szRegisteredNick;
	m_szNickServMask = src.m_szNickServMask;
	m_szMessageRegexp = src.m_szMessageRegexp;
	m_szIdentifyCommand = src.m_szIdentifyCommand;
	m_szServerMask = src.m_szServerMask;
}

void KviNickServRule::save(KviConfig * cfg,const QString &prefix)
{
	QString tmp;
	KviQString::sprintf(tmp,"%QRegisteredNick",&prefix);
	cfg->writeEntry(tmp,m_szRegisteredNick);
	KviQString::sprintf(tmp,"%QNickServMask",&prefix);
	cfg->writeEntry(tmp,m_szNickServMask);
	KviQString::sprintf(tmp,"%QMessageRegexp",&prefix);
	cfg->writeEntry(tmp,m_szMessageRegexp);
	KviQString::sprintf(tmp,"%QIdentifyCommand",&prefix);
	cfg->writeEntry(tmp,m_szIdentifyCommand);
	KviQString::sprintf(tmp,"%QServerMask",&prefix);
	cfg->writeEntry(tmp,m_szServerMask);
}

bool KviNickServRule::load(KviConfig * cfg,const QString &prefix)
{
	QString tmp;
	KviQString::sprintf(tmp,"%QRegisteredNick",&prefix);
	m_szRegisteredNick = KviQString::trimmed(cfg->readQStringEntry(tmp));
	if(m_szRegisteredNick.isEmpty())return false;
	KviQString::sprintf(tmp,"%QNickServMask",&prefix);
	m_szNickServMask = cfg->readQStringEntry(tmp);
	if(m_szNickServMask.isEmpty())return false;
	KviQString::sprintf(tmp,"%QServerMask",&prefix);
	m_szServerMask = cfg->readQStringEntry(tmp,QString::null);
	KviQString::sprintf(tmp,"%QMessageRegexp",&prefix);
	m_szMessageRegexp = cfg->readQStringEntry(tmp);
	if(m_szMessageRegexp.isEmpty())return false;
	KviQString::sprintf(tmp,"%QIdentifyCommand",&prefix);
	m_szIdentifyCommand = cfg->readQStringEntry(tmp);
	return !m_szIdentifyCommand.isEmpty();
}

KviNickServRule * KviNickServRule::createInstance(const QString &szRegisteredNick,
		const QString &szNickServMask,
		const QString &szMessageRegexp,
		const QString &szIdentifyCommand,
		const QString &szServerMask)
{
	return new KviNickServRule(szRegisteredNick,szNickServMask,szMessageRegexp,szIdentifyCommand,szServerMask);
}