2
* jid.cpp - class for verifying and manipulating XMPP addresses
3
* Copyright (C) 2003 Justin Karneges
5
* This library is free software; you can redistribute it and/or
6
* modify it under the terms of the GNU Lesser General Public
7
* License as published by the Free Software Foundation; either
8
* version 2.1 of the License, or (at your option) any later version.
10
* This library is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
* Lesser General Public License for more details.
15
* You should have received a copy of the GNU Lesser General Public
16
* License along with this library; if not, write to the Free Software
17
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21
//#undef IRIS_XMPP_JID_DEPRECATED
22
#include "xmpp/jid/jid.h"
24
#include <QCoreApplication>
25
#include <libidn/stringprep.h>
28
#include "irisnetglobal_p.h"
34
//----------------------------------------------------------------------------
36
//----------------------------------------------------------------------------
37
StringPrepCache *StringPrepCache::instance = 0;
39
bool StringPrepCache::nameprep(const QString &in, int maxbytes, QString& out)
41
if (in.trimmed().isEmpty()) {
43
return false; // empty names or just spaces are disallowed (rfc5892+rfc6122)
46
StringPrepCache *that = get_instance();
48
Result *r = that->nameprep_table[in];
57
QByteArray cs = in.toUtf8();
59
if(stringprep(cs.data(), maxbytes, (Stringprep_profile_flags)0, stringprep_nameprep) != 0)
61
that->nameprep_table.insert(in, new Result);
65
QString norm = QString::fromUtf8(cs);
66
that->nameprep_table.insert(in, new Result(norm));
71
bool StringPrepCache::nodeprep(const QString &in, int maxbytes, QString& out)
78
StringPrepCache *that = get_instance();
80
Result *r = that->nodeprep_table[in];
89
QByteArray cs = in.toUtf8();
91
if(stringprep(cs.data(), maxbytes, (Stringprep_profile_flags)0, stringprep_xmpp_nodeprep) != 0) {
92
that->nodeprep_table.insert(in, new Result);
96
QString norm = QString::fromUtf8(cs);
97
that->nodeprep_table.insert(in, new Result(norm));
102
bool StringPrepCache::resourceprep(const QString &in, int maxbytes, QString& out)
109
StringPrepCache *that = get_instance();
111
Result *r = that->resourceprep_table[in];
120
QByteArray cs = in.toUtf8();
122
if(stringprep(cs.data(), maxbytes, (Stringprep_profile_flags)0, stringprep_xmpp_resourceprep) != 0) {
123
that->resourceprep_table.insert(in, new Result);
127
QString norm = QString::fromUtf8(cs);
128
that->resourceprep_table.insert(in, new Result(norm));
133
bool StringPrepCache::saslprep(const QString &in, int maxbytes, QString& out)
140
StringPrepCache *that = get_instance();
142
Result *r = that->saslprep_table[in];
151
QByteArray cs = in.toUtf8();
153
if(stringprep(cs.data(), maxbytes, (Stringprep_profile_flags)0, stringprep_saslprep) != 0) {
154
that->saslprep_table.insert(in, new Result);
158
QString norm = QString::fromUtf8(cs);
159
that->saslprep_table.insert(in, new Result(norm));
164
void StringPrepCache::cleanup()
170
StringPrepCache *StringPrepCache::get_instance()
174
instance = new StringPrepCache;
176
irisNetAddPostRoutine(cleanup);
182
StringPrepCache::StringPrepCache()
183
: QObject(QCoreApplication::instance())
187
StringPrepCache::~StringPrepCache()
189
foreach(Result* r, nameprep_table) {
192
nameprep_table.clear();
193
foreach(Result* r, nodeprep_table) {
196
nodeprep_table.clear();
197
foreach(Result* r, resourceprep_table) {
200
resourceprep_table.clear();
201
foreach(Result* r, saslprep_table) {
204
saslprep_table.clear();
207
//----------------------------------------------------------------------------
209
//----------------------------------------------------------------------------
211
static inline bool validDomain(const QString &s, QString& norm)
213
return StringPrepCache::nameprep(s, 1024, norm);
216
static inline bool validNode(const QString &s, QString& norm)
218
return StringPrepCache::nodeprep(s, 1024, norm);
221
static inline bool validResource(const QString &s, QString& norm)
223
return StringPrepCache::resourceprep(s, 1024, norm);
236
Jid::Jid(const QString &s)
241
Jid::Jid(const QString &node, const QString& domain, const QString& resource)
243
set(domain, node, resource);
246
Jid::Jid(const char *s)
251
Jid & Jid::operator=(const QString &s)
257
Jid & Jid::operator=(const char *s)
276
// build 'bare' and 'full' jids
287
null = f.isEmpty() && r.isEmpty();
290
void Jid::set(const QString &s)
292
QString rest, domain, node, resource;
293
QString norm_domain, norm_node, norm_resource;
294
int x = s.indexOf('/');
297
resource = s.mid(x+1);
301
resource = QString();
303
if(!validResource(resource, norm_resource)) {
308
x = rest.indexOf('@');
310
node = rest.mid(0, x);
311
domain = rest.mid(x+1);
317
if(!validDomain(domain, norm_domain) || !validNode(node, norm_node)) {
330
void Jid::set(const QString &domain, const QString &node, const QString &resource)
332
QString norm_domain, norm_node, norm_resource;
333
if(!validDomain(domain, norm_domain) ||
334
!validNode(node, norm_node) ||
335
!validResource(resource, norm_resource))
348
void Jid::setDomain(const QString &s)
353
if(!validDomain(s, norm)) {
361
void Jid::setNode(const QString &s)
366
if(!validNode(s, norm)) {
374
void Jid::setResource(const QString &s)
379
if(!validResource(s, norm)) {
387
Jid Jid::withNode(const QString &s) const
394
Jid Jid::withDomain(const QString &s) const
401
Jid Jid::withResource(const QString &s) const
408
bool Jid::isValid() const
413
bool Jid::isEmpty() const
418
bool Jid::compare(const Jid &a, bool compareRes) const
423
// only compare valid jids
424
if(!valid || !a.valid)
427
if(compareRes ? (f != a.f) : (b != a.b))