1
//=============================================================================
3
// File : kvi_nickserv.cpp
4
// Creation date : Thu Aug 09 2001 17:44:56 by Szymon Stefanek
6
// This file is part of the KVirc irc client distribution
7
// Copyright (C) 2001-2008 Szymon Stefanek (pragma at kvirc dot net)
9
// This program is FREE software. You can redistribute it and/or
10
// modify it under the terms of the GNU General Public License
11
// as published by the Free Software Foundation; either version 2
12
// of the License, or (at your opinion) any later version.
14
// This program is distributed in the HOPE that it will be USEFUL,
15
// but WITHOUT ANY WARRANTY; without even the implied warranty of
16
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
17
// See the GNU General Public License for more details.
19
// You should have received a copy of the GNU General Public License
20
// along with this program. If not, write to the Free Software Foundation,
21
// Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23
//=============================================================================
27
#include "kvi_nickserv.h"
28
#include "kvi_config.h"
29
#include "kvi_ircmask.h"
38
Authentication with NickServ
40
NickServ, automatic authentication with NickServ
44
Automatic authentication with NickServ
46
KVIrc supports automatic authentication with the NickServ service.[br]
47
This service is commonly implemented on major IRC networks: basically
48
it is a program that allows users to register their nickname and protect
49
it from being stolen by others.[br] The NickServ protocol is
50
not standardized (at the time that I'm writing this doc) and automatic
51
authentication is a pure experimental protocol.[br]
52
Once you get on IRC with a registered nickname , the NickServ will
53
ask you for identification by sending you a NOTICE.[br]
54
The message will look in a way similar to the following:[br]
55
<b>You're using a registered nickname: if this is your nick,
56
please type /msg NickServ IDENTIFY password, otherwise please
57
choose another nickname</b>.[br]
58
The message is often broken in two or three lines of text.[br]
59
Please note that many network policies suggest to avoid automatic authentication
60
with NickServ.[br]I have implemented it because I know that it works on the networks
61
that I'm usually on.[br]You have to check that this protocol works on your network and
62
then eventually use it at your own risk.[br]
66
// FIXME: The doc above is a bit outdated , fix it
68
KviNickServRuleSet::KviNickServRuleSet()
75
KviNickServRuleSet::KviNickServRuleSet(const KviNickServRuleSet &s)
82
KviNickServRuleSet::~KviNickServRuleSet()
84
if(m_pRules)delete m_pRules;
87
void KviNickServRuleSet::save(KviConfig * cfg,const QString &prefix)
89
if(!m_pRules)return; // nothing to save
90
if(m_pRules->isEmpty())return; // should never happen anyway
94
KviQString::sprintf(tmp,"%QNSEnabled",&prefix);
95
cfg->writeEntry(tmp,m_bEnabled);
97
KviQString::sprintf(tmp,"%QNSRules",&prefix);
98
cfg->writeEntry(tmp,m_pRules->count());
100
for(KviNickServRule * r = m_pRules->first();r;r = m_pRules->next())
102
KviQString::sprintf(tmp,"%QNSRule%d_",&prefix,idx);
108
KviNickServRuleSet * KviNickServRuleSet::load(KviConfig * cfg,const QString &prefix)
111
KviQString::sprintf(tmp,"%QNSRules",&prefix);
112
unsigned int cnt = cfg->readUIntEntry(tmp,0);
113
if(cnt == 0)return 0;
114
KviNickServRuleSet * s = new KviNickServRuleSet();
115
if(s->loadPrivate(cfg,prefix,cnt))return s;
120
void KviNickServRuleSet::load(const QString &szConfigFile)
123
KviConfig cfg(szConfigFile,KviConfig::Read);
126
KviQString::sprintf(tmp,"NSRules");
127
unsigned int cnt = cfg.readUIntEntry(tmp,0);
129
loadPrivate(&cfg,QString(""),cnt);
132
void KviNickServRuleSet::save(const QString &szConfigFile)
134
KviConfig cfg(szConfigFile,KviConfig::Write);
136
save(&cfg,QString(""));
139
bool KviNickServRuleSet::loadPrivate(KviConfig * cfg,const QString &prefix,unsigned int nEntries)
141
if(m_pRules)m_pRules->clear();
143
m_pRules = new KviPointerList<KviNickServRule>;
144
m_pRules->setAutoDelete(true);
150
KviQString::sprintf(tmp,"%QNSEnabled",&prefix);
151
m_bEnabled = cfg->readBoolEntry(tmp,false);
152
for(unsigned int u=0;u<nEntries;u++)
154
KviQString::sprintf(tmp,"%QNSRule%u_",&prefix,u);
155
KviNickServRule * r = new KviNickServRule();
156
if(!r->load(cfg,tmp))delete r;
157
else m_pRules->append(r);
161
if(m_pRules->isEmpty())
171
void KviNickServRuleSet::clear()
181
void KviNickServRuleSet::addRule(KviNickServRule * r)
185
m_pRules = new KviPointerList<KviNickServRule>;
186
m_pRules->setAutoDelete(true);
191
KviNickServRuleSet * KviNickServRuleSet::createInstance()
193
return new KviNickServRuleSet();
197
KviNickServRule * KviNickServRuleSet::matchRule(const QString & szNick, const KviIrcMask * pNickServ, const QString & szMsg, const QString & szServer)
199
if(!m_pRules) return 0;
201
for(KviNickServRule *r = m_pRules->first();r;r = m_pRules->next())
203
if(!KviQString::matchString(r->registeredNick(),szNick,false,true))
206
if(!szServer.isEmpty())
208
QRegExp res(r->serverMask(),Qt::CaseInsensitive,QRegExp::Wildcard);
209
if(!res.exactMatch(szServer))
212
if(!pNickServ->matchedBy(KviIrcMask(r->nickServMask())))
214
QRegExp re(r->messageRegexp(),Qt::CaseInsensitive,QRegExp::Wildcard);
215
if(re.exactMatch(szMsg)) return r;
220
void KviNickServRuleSet::copyFrom(const KviNickServRuleSet &src)
224
if(m_pRules)m_pRules->clear();
226
m_pRules = new KviPointerList<KviNickServRule>;
227
m_pRules->setAutoDelete(true);
229
for(KviNickServRule * r = src.m_pRules->first();r;r = src.m_pRules->next())
231
KviNickServRule * c = new KviNickServRule();
235
if(m_pRules->isEmpty())
241
m_bEnabled = src.m_bEnabled;
254
void KviNickServRule::copyFrom(const KviNickServRule &src)
256
m_szRegisteredNick = src.m_szRegisteredNick;
257
m_szNickServMask = src.m_szNickServMask;
258
m_szMessageRegexp = src.m_szMessageRegexp;
259
m_szIdentifyCommand = src.m_szIdentifyCommand;
260
m_szServerMask = src.m_szServerMask;
263
void KviNickServRule::save(KviConfig * cfg,const QString &prefix)
266
KviQString::sprintf(tmp,"%QRegisteredNick",&prefix);
267
cfg->writeEntry(tmp,m_szRegisteredNick);
268
KviQString::sprintf(tmp,"%QNickServMask",&prefix);
269
cfg->writeEntry(tmp,m_szNickServMask);
270
KviQString::sprintf(tmp,"%QMessageRegexp",&prefix);
271
cfg->writeEntry(tmp,m_szMessageRegexp);
272
KviQString::sprintf(tmp,"%QIdentifyCommand",&prefix);
273
cfg->writeEntry(tmp,m_szIdentifyCommand);
274
KviQString::sprintf(tmp,"%QServerMask",&prefix);
275
cfg->writeEntry(tmp,m_szServerMask);
278
bool KviNickServRule::load(KviConfig * cfg,const QString &prefix)
281
KviQString::sprintf(tmp,"%QRegisteredNick",&prefix);
282
m_szRegisteredNick = KviQString::trimmed(cfg->readQStringEntry(tmp));
283
if(m_szRegisteredNick.isEmpty())return false;
284
KviQString::sprintf(tmp,"%QNickServMask",&prefix);
285
m_szNickServMask = cfg->readQStringEntry(tmp);
286
if(m_szNickServMask.isEmpty())return false;
287
KviQString::sprintf(tmp,"%QServerMask",&prefix);
288
m_szServerMask = cfg->readQStringEntry(tmp,QString());
289
KviQString::sprintf(tmp,"%QMessageRegexp",&prefix);
290
m_szMessageRegexp = cfg->readQStringEntry(tmp);
291
if(m_szMessageRegexp.isEmpty())return false;
292
KviQString::sprintf(tmp,"%QIdentifyCommand",&prefix);
293
m_szIdentifyCommand = cfg->readQStringEntry(tmp);
294
return !m_szIdentifyCommand.isEmpty();
297
KviNickServRule * KviNickServRule::createInstance(const QString &szRegisteredNick,
298
const QString &szNickServMask,
299
const QString &szMessageRegexp,
300
const QString &szIdentifyCommand,
301
const QString &szServerMask)
303
return new KviNickServRule(szRegisteredNick,szNickServMask,szMessageRegexp,szIdentifyCommand,szServerMask);