~ubuntu-branches/ubuntu/edgy/psi/edgy

« back to all changes in this revision

Viewing changes to iris/xmpp-core/jid.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Lucas Nussbaum
  • Date: 2006-03-28 11:11:02 UTC
  • mfrom: (2.1.2 etch)
  • Revision ID: james.westby@ubuntu.com-20060328111102-v1diqpbwqr4yijoy
Tags: 0.10-2build1
Manual sync from Debian. No Ubuntu-specific changes.

Show diffs side-by-side

added added

removed removed

Lines of Context:
20
20
 
21
21
#include"xmpp.h"
22
22
 
 
23
#include<qdict.h>
23
24
#include<stringprep.h>
24
25
 
25
26
using namespace XMPP;
26
27
 
 
28
//----------------------------------------------------------------------------
 
29
// StringPrepCache
 
30
//----------------------------------------------------------------------------
 
31
class StringPrepCache
 
32
{
 
33
public:
 
34
        static bool nameprep(const QString &in, int maxbytes, QString *out)
 
35
        {
 
36
                if(in.isEmpty())
 
37
                {
 
38
                        if(out)
 
39
                                *out = QString();
 
40
                        return true;
 
41
                }
 
42
 
 
43
                StringPrepCache *that = get_instance();
 
44
 
 
45
                Result *r = that->nameprep_table.find(in);
 
46
                if(r)
 
47
                {
 
48
                        if(!r->norm)
 
49
                                return false;
 
50
                        if(out)
 
51
                                *out = *(r->norm);
 
52
                        return true;
 
53
                }
 
54
 
 
55
                QCString cs = in.utf8();
 
56
                cs.resize(maxbytes);
 
57
                if(stringprep(cs.data(), maxbytes, (Stringprep_profile_flags)0, stringprep_nameprep) != 0)
 
58
                {
 
59
                        that->nameprep_table.insert(in, new Result);
 
60
                        return false;
 
61
                }
 
62
 
 
63
                QString norm = QString::fromUtf8(cs);
 
64
                that->nameprep_table.insert(in, new Result(norm));
 
65
                if(out)
 
66
                        *out = norm;
 
67
                return true;
 
68
        }
 
69
 
 
70
        static bool nodeprep(const QString &in, int maxbytes, QString *out)
 
71
        {
 
72
                if(in.isEmpty())
 
73
                {
 
74
                        if(out)
 
75
                                *out = QString();
 
76
                        return true;
 
77
                }
 
78
 
 
79
                StringPrepCache *that = get_instance();
 
80
 
 
81
                Result *r = that->nodeprep_table.find(in);
 
82
                if(r)
 
83
                {
 
84
                        if(!r->norm)
 
85
                                return false;
 
86
                        if(out)
 
87
                                *out = *(r->norm);
 
88
                        return true;
 
89
                }
 
90
 
 
91
                QCString cs = in.utf8();
 
92
                cs.resize(maxbytes);
 
93
                if(stringprep(cs.data(), maxbytes, (Stringprep_profile_flags)0, stringprep_xmpp_nodeprep) != 0)
 
94
                {
 
95
                        that->nodeprep_table.insert(in, new Result);
 
96
                        return false;
 
97
                }
 
98
 
 
99
                QString norm = QString::fromUtf8(cs);
 
100
                that->nodeprep_table.insert(in, new Result(norm));
 
101
                if(out)
 
102
                        *out = norm;
 
103
                return true;
 
104
        }
 
105
 
 
106
        static bool resourceprep(const QString &in, int maxbytes, QString *out)
 
107
        {
 
108
                if(in.isEmpty())
 
109
                {
 
110
                        if(out)
 
111
                                *out = QString();
 
112
                        return true;
 
113
                }
 
114
 
 
115
                StringPrepCache *that = get_instance();
 
116
 
 
117
                Result *r = that->resourceprep_table.find(in);
 
118
                if(r)
 
119
                {
 
120
                        if(!r->norm)
 
121
                                return false;
 
122
                        if(out)
 
123
                                *out = *(r->norm);
 
124
                        return true;
 
125
                }
 
126
 
 
127
                QCString cs = in.utf8();
 
128
                cs.resize(maxbytes);
 
129
                if(stringprep(cs.data(), maxbytes, (Stringprep_profile_flags)0, stringprep_xmpp_resourceprep) != 0)
 
130
                {
 
131
                        that->resourceprep_table.insert(in, new Result);
 
132
                        return false;
 
133
                }
 
134
 
 
135
                QString norm = QString::fromUtf8(cs);
 
136
                that->resourceprep_table.insert(in, new Result(norm));
 
137
                if(out)
 
138
                        *out = norm;
 
139
                return true;
 
140
        }
 
141
 
 
142
private:
 
143
        class Result
 
144
        {
 
145
        public:
 
146
                QString *norm;
 
147
 
 
148
                Result() : norm(0)
 
149
                {
 
150
                }
 
151
 
 
152
                Result(const QString &s) : norm(new QString(s))
 
153
                {
 
154
                }
 
155
 
 
156
                ~Result()
 
157
                {
 
158
                        delete norm;
 
159
                }
 
160
        };
 
161
 
 
162
        QDict<Result> nameprep_table;
 
163
        QDict<Result> nodeprep_table;
 
164
        QDict<Result> resourceprep_table;
 
165
 
 
166
        static StringPrepCache *instance;
 
167
 
 
168
        static StringPrepCache *get_instance()
 
169
        {
 
170
                if(!instance)
 
171
                        instance = new StringPrepCache;
 
172
                return instance;
 
173
        }
 
174
 
 
175
        StringPrepCache()
 
176
        {
 
177
                nameprep_table.setAutoDelete(true);
 
178
                nodeprep_table.setAutoDelete(true);
 
179
                resourceprep_table.setAutoDelete(true);
 
180
        }
 
181
};
 
182
 
 
183
StringPrepCache *StringPrepCache::instance = 0;
 
184
 
 
185
//----------------------------------------------------------------------------
 
186
// Jid
 
187
//----------------------------------------------------------------------------
27
188
Jid::Jid()
28
189
{
29
190
        valid = false;
210
371
 
211
372
bool Jid::validDomain(const QString &s, QString *norm)
212
373
{
213
 
        QCString cs = s.utf8();
 
374
        /*QCString cs = s.utf8();
214
375
        cs.resize(1024);
215
376
        if(stringprep(cs.data(), 1024, (Stringprep_profile_flags)0, stringprep_nameprep) != 0)
216
377
                return false;
217
378
        if(norm)
218
379
                *norm = QString::fromUtf8(cs);
219
 
        return true;
 
380
        return true;*/
 
381
        return StringPrepCache::nameprep(s, 1024, norm);
220
382
}
221
383
 
222
384
bool Jid::validNode(const QString &s, QString *norm)
223
385
{
224
 
        QCString cs = s.utf8();
 
386
        /*QCString cs = s.utf8();
225
387
        cs.resize(1024);
226
388
        if(stringprep(cs.data(), 1024, (Stringprep_profile_flags)0, stringprep_xmpp_nodeprep) != 0)
227
389
                return false;
228
390
        if(norm)
229
391
                *norm = QString::fromUtf8(cs);
230
 
        return true;
 
392
        return true;*/
 
393
        return StringPrepCache::nodeprep(s, 1024, norm);
231
394
}
232
395
 
233
396
bool Jid::validResource(const QString &s, QString *norm)
234
397
{
235
 
        QCString cs = s.utf8();
 
398
        /*QCString cs = s.utf8();
236
399
        cs.resize(1024);
237
400
        if(stringprep(cs.data(), 1024, (Stringprep_profile_flags)0, stringprep_xmpp_resourceprep) != 0)
238
401
                return false;
239
402
        if(norm)
240
403
                *norm = QString::fromUtf8(cs);
241
 
        return true;
 
404
        return true;*/
 
405
        return StringPrepCache::resourceprep(s, 1024, norm);
242
406
}