~ubuntu-branches/ubuntu/trusty/kvirc/trusty

« back to all changes in this revision

Viewing changes to src/kvilib/irc/kvi_nickserv.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Kai Wasserbäch, Kai Wasserbäch, Raúl Sánchez Siles
  • Date: 2011-02-12 10:40:21 UTC
  • mfrom: (14.1.3 sid)
  • Revision ID: james.westby@ubuntu.com-20110212104021-5mh4f75jlku20mnt
The combined "Twisted Experiment" and "Nocturnal Raid" release.

[ Kai Wasserbäch ]
* Synced to upstream's SVN revision 5467.
* debian/rules:
  - Added .PHONY line.
  - Resurrect -DMANUAL_REVISION, got lost somewhere and we build SVN
    revisions again.
  - Replace "-DWITH_NO_EMBEDDED_CODE=YES" with "-DWANT_CRYPTOPP=YES".
  - Change the remaining -DWITH/-DWITHOUT to the new -DWANT syntax.
* debian/control:
  - Removed DMUA, I'm a DD now.
  - Changed my e-mail address.
  - Removed unneeded relationships (no upgrades over two releases are
    supported).
  - Fix Suggests for kvirc-dbg.
  - kvirc-data: Make the "Suggests: kvirc" a Recommends, doesn't make much
    sense to install the -data package without the program.
* debian/source/local-options: Added with "unapply-patches".
* debian/kvirc.lintian-overrides: Updated to work for 4.1.1.
* debian/patches/21_make_shared-mime-info_B-D_superfluous.patch: Updated.
* debian/kvirc-data.install: Added .notifyrc.

[ Raúl Sánchez Siles ]
* Stating the right version where kvirc-data break and replace should happen.
* Fixing link to license file.
* Added French and Portuguese man pages.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
//=============================================================================
2
 
//
3
 
//   File : kvi_nickserv.cpp
4
 
//   Creation date : Thu Aug 09 2001 17:44:56 by Szymon Stefanek
5
 
//
6
 
//   This file is part of the KVirc irc client distribution
7
 
//   Copyright (C) 2001-2008 Szymon Stefanek (pragma at kvirc dot net)
8
 
//
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.
13
 
//
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.
18
 
//
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.
22
 
//
23
 
//=============================================================================
24
 
 
25
 
 
26
 
 
27
 
#include "kvi_nickserv.h"
28
 
#include "kvi_config.h"
29
 
#include "kvi_ircmask.h"
30
 
 
31
 
#include <QRegExp>
32
 
 
33
 
 
34
 
 
35
 
/*
36
 
        @doc: nickserv_proto
37
 
        @title:
38
 
                Authentication with NickServ
39
 
        @keyterms:
40
 
                NickServ, automatic authentication with NickServ
41
 
        @type:
42
 
                generic
43
 
        @short:
44
 
                Automatic authentication with NickServ
45
 
        @body:
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]
63
 
*/
64
 
 
65
 
 
66
 
// FIXME: The doc above is a bit outdated , fix it
67
 
 
68
 
KviNickServRuleSet::KviNickServRuleSet()
69
 
: KviHeapObject()
70
 
{
71
 
        m_bEnabled = false;
72
 
        m_pRules = 0;
73
 
}
74
 
 
75
 
KviNickServRuleSet::KviNickServRuleSet(const KviNickServRuleSet &s)
76
 
{
77
 
        m_pRules = 0;
78
 
        copyFrom(s);
79
 
}
80
 
 
81
 
 
82
 
KviNickServRuleSet::~KviNickServRuleSet()
83
 
{
84
 
        if(m_pRules)delete m_pRules;
85
 
}
86
 
 
87
 
void KviNickServRuleSet::save(KviConfig * cfg,const QString &prefix)
88
 
{
89
 
        if(!m_pRules)return; // nothing to save
90
 
        if(m_pRules->isEmpty())return; // should never happen anyway
91
 
        QString tmp;
92
 
        if(m_bEnabled)
93
 
        {
94
 
                KviQString::sprintf(tmp,"%QNSEnabled",&prefix);
95
 
                cfg->writeEntry(tmp,m_bEnabled);
96
 
        }
97
 
        KviQString::sprintf(tmp,"%QNSRules",&prefix);
98
 
        cfg->writeEntry(tmp,m_pRules->count());
99
 
        int idx = 0;
100
 
        for(KviNickServRule * r = m_pRules->first();r;r = m_pRules->next())
101
 
        {
102
 
                KviQString::sprintf(tmp,"%QNSRule%d_",&prefix,idx);
103
 
                r->save(cfg,tmp);
104
 
                idx++;
105
 
        }
106
 
}
107
 
 
108
 
KviNickServRuleSet * KviNickServRuleSet::load(KviConfig * cfg,const QString &prefix)
109
 
{
110
 
        QString tmp;
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;
116
 
        delete s;
117
 
        return 0;
118
 
}
119
 
 
120
 
void KviNickServRuleSet::load(const QString &szConfigFile)
121
 
{
122
 
        clear();
123
 
        KviConfig cfg(szConfigFile,KviConfig::Read);
124
 
 
125
 
        QString tmp;
126
 
        KviQString::sprintf(tmp,"NSRules");
127
 
        unsigned int cnt = cfg.readUIntEntry(tmp,0);
128
 
        if(cnt == 0)return;
129
 
        loadPrivate(&cfg,QString(""),cnt);
130
 
}
131
 
 
132
 
void KviNickServRuleSet::save(const QString &szConfigFile)
133
 
{
134
 
        KviConfig cfg(szConfigFile,KviConfig::Write);
135
 
        cfg.clear();
136
 
        save(&cfg,QString(""));
137
 
}
138
 
 
139
 
bool KviNickServRuleSet::loadPrivate(KviConfig * cfg,const QString &prefix,unsigned int nEntries)
140
 
{
141
 
        if(m_pRules)m_pRules->clear();
142
 
        else {
143
 
                m_pRules = new KviPointerList<KviNickServRule>;
144
 
                m_pRules->setAutoDelete(true);
145
 
        }
146
 
 
147
 
        if(nEntries != 0)
148
 
        {
149
 
                QString tmp;
150
 
                KviQString::sprintf(tmp,"%QNSEnabled",&prefix);
151
 
                m_bEnabled = cfg->readBoolEntry(tmp,false);
152
 
                for(unsigned int u=0;u<nEntries;u++)
153
 
                {
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);
158
 
                }
159
 
        }
160
 
 
161
 
        if(m_pRules->isEmpty())
162
 
        {
163
 
                m_bEnabled = false;
164
 
                delete m_pRules;
165
 
                m_pRules = 0;
166
 
                return false;
167
 
        }
168
 
        return true;
169
 
}
170
 
 
171
 
void KviNickServRuleSet::clear()
172
 
{
173
 
        if(m_pRules)
174
 
        {
175
 
                delete m_pRules;
176
 
                m_pRules = 0;
177
 
        }
178
 
        m_bEnabled = false;
179
 
}
180
 
 
181
 
void KviNickServRuleSet::addRule(KviNickServRule * r)
182
 
{
183
 
        if(!m_pRules)
184
 
        {
185
 
                m_pRules = new KviPointerList<KviNickServRule>;
186
 
                m_pRules->setAutoDelete(true);
187
 
        }
188
 
        m_pRules->append(r);
189
 
}
190
 
 
191
 
KviNickServRuleSet * KviNickServRuleSet::createInstance()
192
 
{
193
 
        return new KviNickServRuleSet();
194
 
}
195
 
 
196
 
 
197
 
KviNickServRule * KviNickServRuleSet::matchRule(const QString & szNick, const KviIrcMask * pNickServ, const QString & szMsg, const QString & szServer)
198
 
{
199
 
        if(!m_pRules) return 0;
200
 
        
201
 
        for(KviNickServRule *r = m_pRules->first();r;r = m_pRules->next())
202
 
        {
203
 
                if(!KviQString::matchString(r->registeredNick(),szNick,false,true))
204
 
                        continue;
205
 
                
206
 
                if(!szServer.isEmpty())
207
 
                {
208
 
                        QRegExp res(r->serverMask(),Qt::CaseInsensitive,QRegExp::Wildcard);
209
 
                        if(!res.exactMatch(szServer))
210
 
                                continue;
211
 
                }
212
 
                if(!pNickServ->matchedBy(KviIrcMask(r->nickServMask())))
213
 
                        continue;
214
 
                QRegExp re(r->messageRegexp(),Qt::CaseInsensitive,QRegExp::Wildcard);
215
 
                if(re.exactMatch(szMsg)) return r;
216
 
        }
217
 
        return 0;
218
 
}
219
 
 
220
 
void KviNickServRuleSet::copyFrom(const KviNickServRuleSet &src)
221
 
{
222
 
        if(src.m_pRules)
223
 
        {
224
 
                if(m_pRules)m_pRules->clear();
225
 
                else {
226
 
                        m_pRules = new KviPointerList<KviNickServRule>;
227
 
                        m_pRules->setAutoDelete(true);
228
 
                }
229
 
                for(KviNickServRule * r = src.m_pRules->first();r;r = src.m_pRules->next())
230
 
                {
231
 
                        KviNickServRule * c = new KviNickServRule();
232
 
                        c->copyFrom(*r);
233
 
                        m_pRules->append(c);
234
 
                }
235
 
                if(m_pRules->isEmpty())
236
 
                {
237
 
                        m_bEnabled = false;
238
 
                        delete m_pRules;
239
 
                        m_pRules = 0;
240
 
                } else {
241
 
                        m_bEnabled = src.m_bEnabled;
242
 
                }
243
 
        } else {
244
 
                m_bEnabled = false;
245
 
                if(m_pRules)
246
 
                {
247
 
                        delete m_pRules;
248
 
                        m_pRules = 0;
249
 
                }
250
 
        }
251
 
}
252
 
 
253
 
 
254
 
void KviNickServRule::copyFrom(const KviNickServRule &src)
255
 
{
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;
261
 
}
262
 
 
263
 
void KviNickServRule::save(KviConfig * cfg,const QString &prefix)
264
 
{
265
 
        QString tmp;
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);
276
 
}
277
 
 
278
 
bool KviNickServRule::load(KviConfig * cfg,const QString &prefix)
279
 
{
280
 
        QString tmp;
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();
295
 
}
296
 
 
297
 
KviNickServRule * KviNickServRule::createInstance(const QString &szRegisteredNick,
298
 
                const QString &szNickServMask,
299
 
                const QString &szMessageRegexp,
300
 
                const QString &szIdentifyCommand,
301
 
                const QString &szServerMask)
302
 
{
303
 
        return new KviNickServRule(szRegisteredNick,szNickServMask,szMessageRegexp,szIdentifyCommand,szServerMask);
304
 
}