summaryrefslogtreecommitdiffstats
path: root/kopete/protocols/jabber/jingle/libjingle/talk/xmpp/jid.cc
blob: b742e03a455c8a1440ebe1fe1d01b2e539edd955 (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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
/*
 * libjingle
 * Copyright 2004--2005, Google Inc.
 *
 * Redistribution and use in source and binary forms, with or without 
 * modification, are permitted provided that the following conditions are met:
 *
 *  1. Redistributions of source code must retain the above copyright notice, 
 *     this list of conditions and the following disclaimer.
 *  2. Redistributions in binary form must reproduce the above copyright notice,
 *     this list of conditions and the following disclaimer in the documentation
 *     and/or other materials provided with the distribution.
 *  3. The name of the author may not be used to endorse or promote products 
 *     derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

extern "C" {
#include <ctype.h>
}
#include <string>
#include "talk/xmpp/jid.h"
#include "talk/xmpp/constants.h"
#include "talk/base/common.h"
#include <algorithm>

#define new TRACK_NEW

namespace buzz {

static int AsciiToLower(int x) {
  return (x <= 'Z' && x >= 'A') ? (x + ('a' - 'A')) : x;
}

Jid::Jid() : data_(NULL) {
}

Jid::Jid(bool is_special, const std::string & special) {
  data_ = is_special ? new Data(special, STR_EMPTY, STR_EMPTY) : NULL;
}

Jid::Jid(const std::string & jid_string) {
  if (jid_string == STR_EMPTY) {
    data_ = NULL;
    return;
  }

  // First find the slash and slice of that part
  size_t slash = jid_string.find('/');
  std::string resource_name = (slash == std::string::npos ? STR_EMPTY :
                    jid_string.substr(slash + 1));

  // Now look for the node
  std::string node_name;
  size_t at = jid_string.find('@');
  size_t domain_begin;
  if (at < slash && at != std::string::npos) {
    node_name = jid_string.substr(0, at);
    domain_begin = at + 1;
  } else {
    domain_begin = 0;
  }

  // Now take what is left as the domain
  size_t domain_length =
    (  slash == std::string::npos
     ? jid_string.length() - domain_begin
     : slash - domain_begin);

  // avoid allocating these constants repeatedly
  std::string domain_name;

  if (domain_length == 9  && jid_string.find("gmail.com", domain_begin) == domain_begin) {
    domain_name = STR_GMAIL_COM;
  }
  else if (domain_length == 14 && jid_string.find("googlemail.com", domain_begin) == domain_begin) {
    domain_name = STR_GOOGLEMAIL_COM;
  }
  else if (domain_length == 10 && jid_string.find("google.com", domain_begin) == domain_begin) {
    domain_name = STR_GOOGLE_COM;
  }
  else {
    domain_name = jid_string.substr(domain_begin, domain_length);
  }

  // If the domain is empty we have a non-valid jid and we should empty
  // everything else out
  if (domain_name.empty()) {
    data_ = NULL;
    return;
  }
  
  bool valid_node;
  std::string validated_node = prepNode(node_name, 
      node_name.begin(), node_name.end(), &valid_node);
  bool valid_domain;
  std::string validated_domain = prepDomain(domain_name,
      domain_name.begin(), domain_name.end(), &valid_domain);
  bool valid_resource;
  std::string validated_resource = prepResource(resource_name,
      resource_name.begin(), resource_name.end(), &valid_resource);

  if (!valid_node || !valid_domain || !valid_resource) {
    data_ = NULL;
    return;
  }

  data_ = new Data(validated_node, validated_domain, validated_resource);
}

Jid::Jid(const std::string & node_name,
         const std::string & domain_name,
         const std::string & resource_name) {
  if (domain_name.empty()) {
    data_ = NULL;
    return;
  }

  bool valid_node;
  std::string validated_node = prepNode(node_name, 
      node_name.begin(), node_name.end(), &valid_node);
  bool valid_domain;
  std::string validated_domain = prepDomain(domain_name,
      domain_name.begin(), domain_name.end(), &valid_domain);
  bool valid_resource;
  std::string validated_resource = prepResource(resource_name,
      resource_name.begin(), resource_name.end(), &valid_resource);

  if (!valid_node || !valid_domain || !valid_resource) {
    data_ = NULL;
    return;
  }

  data_ = new Data(validated_node, validated_domain, validated_resource);
}

std::string Jid::Str() const {
  if (!IsValid())
    return STR_EMPTY;

  std::string ret;

  if (!data_->node_name_.empty())
    ret = data_->node_name_ + "@";

  ASSERT(data_->domain_name_ != STR_EMPTY);
  ret += data_->domain_name_;

  if (!data_->resource_name_.empty())
    ret += "/" + data_->resource_name_;

  return ret;
}

bool
Jid::IsValid() const {
  return data_ != NULL && !data_->domain_name_.empty();
}

bool
Jid::IsBare() const {
  return IsValid() &&
         data_->resource_name_.empty();
}

bool
Jid::IsFull() const {
  return IsValid() &&
         !data_->resource_name_.empty();
}

Jid
Jid::BareJid() const {
  if (!IsValid())
    return Jid();
  if (!IsFull())
    return *this;
  return Jid(data_->node_name_, data_->domain_name_, STR_EMPTY);
}

#if 0
void
Jid::set_node(const std::string & node_name) {
    data_->node_name_ = node_name;
}
void
Jid::set_domain(const std::string & domain_name) {
    data_->domain_name_ = domain_name;
}
void
Jid::set_resource(const std::string & res_name) {
    data_->resource_name_ = res_name;
}
#endif

bool
Jid::BareEquals(const Jid & other) const {
  return (other.data_ == data_ ||
          data_ != NULL &&
          other.data_ != NULL &&
          other.data_->node_name_ == data_->node_name_ &&
          other.data_->domain_name_ == data_->domain_name_);
}

bool
Jid::operator==(const Jid & other) const {
  return (other.data_ == data_ ||
          data_ != NULL &&
          other.data_ != NULL &&
          other.data_->node_name_ == data_->node_name_ &&
          other.data_->domain_name_ == data_->domain_name_ &&
          other.data_->resource_name_ == data_->resource_name_);
}

int
Jid::Compare(const Jid & other) const {
  if (other.data_ == data_)
    return 0;
  if (data_ == NULL)
    return -1;
  if (other.data_ == NULL)
    return 1;
  
  int compare_result;
  compare_result = data_->node_name_.compare(other.data_->node_name_);
  if (0 != compare_result)
    return compare_result;
  compare_result = data_->domain_name_.compare(other.data_->domain_name_);
  if (0 != compare_result)
    return compare_result;
  compare_result = data_->resource_name_.compare(other.data_->resource_name_);
  return compare_result;
}


// --- JID parsing code: ---

// Checks and normalizes the node part of a JID.
std::string
Jid::prepNode(const std::string str, std::string::const_iterator start, 
    std::string::const_iterator end, bool *valid) {
  *valid = false;
  std::string result;

  for (std::string::const_iterator i = start; i < end; i++) {
    bool char_valid = true;
    char ch = *i;
    if (ch <= 0x7F) {
      result += prepNodeAscii(ch, &char_valid);
    }
    else {
      // TODO: implement the correct stringprep protocol for these
      result += tolower(ch);
    }
    if (!char_valid) {
      return STR_EMPTY;
    }
  }

  if (result.length() > 1023) {
    return STR_EMPTY;
  }
  *valid = true;
  return result;
}


// Returns the appropriate mapping for an ASCII character in a node.
char
Jid::prepNodeAscii(char ch, bool *valid) {
  *valid = true;
  switch (ch) {
    case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': case 'G':
    case 'H': case 'I': case 'J': case 'K': case 'L': case 'M': case 'N':
    case 'O': case 'P': case 'Q': case 'R': case 'S': case 'T': case 'U':
    case 'V': case 'W': case 'X': case 'Y': case 'Z':
      return (char)(ch + ('a' - 'A'));

    case 0x00: case 0x01: case 0x02: case 0x03: case 0x04: case 0x05:
    case 0x06: case 0x07: case 0x08: case 0x09: case 0x0A: case 0x0B:
    case 0x0C: case 0x0D: case 0x0E: case 0x0F: case 0x10: case 0x11:
    case 0x12: case 0x13: case 0x14: case 0x15: case 0x16: case 0x17:
    case ' ': case '&': case '/': case ':': case '<': case '>': case '@':
    case '\"': case '\'':
    case 0x7F:
      *valid = false;
      return 0;

    default:
      return ch;
  }
}


// Checks and normalizes the resource part of a JID.
std::string
Jid::prepResource(const std::string str, std::string::const_iterator start, 
    std::string::const_iterator end, bool *valid) {
  *valid = false;
  std::string result;

  for (std::string::const_iterator i = start; i < end; i++) {
    bool char_valid = true;
    char ch = *i;
    if (ch <= 0x7F) {
      result += prepResourceAscii(ch, &char_valid);
    }
    else {
      // TODO: implement the correct stringprep protocol for these
      result += ch;
    }
  }

  if (result.length() > 1023) {
    return STR_EMPTY;
  }
  *valid = true;
  return result;
}

// Returns the appropriate mapping for an ASCII character in a resource.
char
Jid::prepResourceAscii(char ch, bool *valid) {
  *valid = true;
  switch (ch) {
    case 0x00: case 0x01: case 0x02: case 0x03: case 0x04: case 0x05:
    case 0x06: case 0x07: case 0x08: case 0x09: case 0x0A: case 0x0B:
    case 0x0C: case 0x0D: case 0x0E: case 0x0F: case 0x10: case 0x11:
    case 0x12: case 0x13: case 0x14: case 0x15: case 0x16: case 0x17:
    case 0x7F:
      *valid = false;
      return 0;

    default:
      return ch;
  }
}

// Checks and normalizes the domain part of a JID.
std::string 
Jid::prepDomain(const std::string str, std::string::const_iterator start, 
    std::string::const_iterator end, bool *valid) {
  *valid = false;
  std::string result;

  // TODO: if the domain contains a ':', then we should parse it
  // as an IPv6 address rather than giving an error about illegal domain.
  prepDomain(str, start, end, &result, valid);
  if (!*valid) {
    return STR_EMPTY;
  }

  if (result.length() > 1023) {
    return STR_EMPTY;
  }
  *valid = true;
  return result;
}


// Checks and normalizes an IDNA domain.
void
Jid::prepDomain(const std::string str, std::string::const_iterator start, 
    std::string::const_iterator end, std::string *buf, bool *valid) {
  *valid = false;
  std::string::const_iterator last = start;
  for (std::string::const_iterator i = start; i < end; i++) {
    bool label_valid = true;
    char ch = *i;
    switch (ch) {
      case 0x002E:
#if 0 // FIX: This isn't UTF-8-aware.
      case 0x3002:
      case 0xFF0E:
      case 0xFF61:
#endif
        prepDomainLabel(str, last, i, buf, &label_valid);
        *buf += '.';
        last = i + 1;
        break;
    }
    if (!label_valid) {
      return;
    }
  }
  prepDomainLabel(str, last, end, buf, valid);
}

// Checks and normalizes a domain label.
void
Jid::prepDomainLabel(const std::string str, std::string::const_iterator start, 
    std::string::const_iterator end, std::string *buf, bool *valid) {
  *valid = false;

  int startLen = buf->length();
  for (std::string::const_iterator i = start; i < end; i++) {
    bool char_valid = true;
    char ch = *i;
    if (ch <= 0x7F) {
      *buf += prepDomainLabelAscii(ch, &char_valid);
    }
    else {
      // TODO: implement ToASCII for these
      *buf += ch;
    }
    if (!char_valid) {
      return;
    }
  }

  int count = buf->length() - startLen;
  if (count == 0) {
    return;
  }
  else if (count > 63) {
    return;
  }

  // Is this check needed? See comment in prepDomainLabelAscii.
  if ((*buf)[startLen] == '-') {
    return;
  }
  if ((*buf)[buf->length() - 1] == '-') {
    return;
  }
  *valid = true;
}


// Returns the appropriate mapping for an ASCII character in a domain label.
char
Jid::prepDomainLabelAscii(char ch, bool *valid) {
  *valid = true;
  // TODO: A literal reading of the spec seems to say that we do
  // not need to check for these illegal characters (an "internationalized
  // domain label" runs ToASCII with UseSTD3... set to false).  But that
  // can't be right.  We should at least be checking that there are no '/'
  // or '@' characters in the domain.  Perhaps we should see what others
  // do in this case.

  switch (ch) {
    case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': case 'G':
    case 'H': case 'I': case 'J': case 'K': case 'L': case 'M': case 'N':
    case 'O': case 'P': case 'Q': case 'R': case 'S': case 'T': case 'U':
    case 'V': case 'W': case 'X': case 'Y': case 'Z':
      return (char)(ch + ('a' - 'A'));

    case 0x00: case 0x01: case 0x02: case 0x03: case 0x04: case 0x05:
    case 0x06: case 0x07: case 0x08: case 0x09: case 0x0A: case 0x0B:
    case 0x0C: case 0x0D: case 0x0E: case 0x0F: case 0x10: case 0x11:
    case 0x12: case 0x13: case 0x14: case 0x15: case 0x16: case 0x17:
    case 0x18: case 0x19: case 0x1A: case 0x1B: case 0x1C: case 0x1D:
    case 0x1E: case 0x1F: case 0x20: case 0x21: case 0x22: case 0x23:
    case 0x24: case 0x25: case 0x26: case 0x27: case 0x28: case 0x29:
    case 0x2A: case 0x2B: case 0x2C: case 0x2E: case 0x2F: case 0x3A:
    case 0x3B: case 0x3C: case 0x3D: case 0x3E: case 0x3F: case 0x40:
    case 0x5B: case 0x5C: case 0x5D: case 0x5E: case 0x5F: case 0x60:
    case 0x7B: case 0x7C: case 0x7D: case 0x7E: case 0x7F:
      *valid = false;
      return 0;

    default:
      return ch;
  }
}

}