Home | History | Annotate | Download | only in xmpp
      1 /*
      2  * libjingle
      3  * Copyright 2004--2005, Google Inc.
      4  *
      5  * Redistribution and use in source and binary forms, with or without
      6  * modification, are permitted provided that the following conditions are met:
      7  *
      8  *  1. Redistributions of source code must retain the above copyright notice,
      9  *     this list of conditions and the following disclaimer.
     10  *  2. Redistributions in binary form must reproduce the above copyright notice,
     11  *     this list of conditions and the following disclaimer in the documentation
     12  *     and/or other materials provided with the distribution.
     13  *  3. The name of the author may not be used to endorse or promote products
     14  *     derived from this software without specific prior written permission.
     15  *
     16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
     17  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
     18  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
     19  * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     20  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     21  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
     22  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
     23  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
     24  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
     25  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     26  */
     27 #ifndef _jid_h_
     28 #define _jid_h_
     29 
     30 #include <string>
     31 #include "talk/base/basictypes.h"
     32 #include "talk/xmllite/xmlconstants.h"
     33 
     34 namespace buzz {
     35 
     36 //! The Jid class encapsulates and provides parsing help for Jids
     37 //! A Jid consists of three parts. The node, the domain and the resource.
     38 //!
     39 //! node@domain/resource
     40 //!
     41 //! The node and resource are both optional.  A valid jid is defined to have
     42 //! a domain.  A bare jid is defined to not have a resource and a full jid
     43 //! *does* have a resource.
     44 class Jid {
     45 public:
     46   explicit Jid();
     47   explicit Jid(const std::string & jid_string);
     48   explicit Jid(const std::string & node_name,
     49                const std::string & domain_name,
     50                const std::string & resource_name);
     51   explicit Jid(bool special, const std::string & special_string);
     52   Jid(const Jid & jid) : data_(jid.data_) {
     53     if (data_ != NULL) {
     54       data_->AddRef();
     55     }
     56   }
     57   Jid & operator=(const Jid & jid) {
     58     if (jid.data_ != NULL) {
     59       jid.data_->AddRef();
     60     }
     61     if (data_ != NULL) {
     62       data_->Release();
     63     }
     64     data_ = jid.data_;
     65     return *this;
     66   }
     67   ~Jid() {
     68     if (data_ != NULL) {
     69       data_->Release();
     70     }
     71   }
     72 
     73 
     74   const std::string & node() const { return !data_ ? STR_EMPTY : data_->node_name_; }
     75   // void set_node(const std::string & node_name);
     76   const std::string & domain() const { return !data_ ? STR_EMPTY : data_->domain_name_; }
     77   // void set_domain(const std::string & domain_name);
     78   const std::string & resource() const { return !data_ ? STR_EMPTY : data_->resource_name_; }
     79   // void set_resource(const std::string & res_name);
     80 
     81   std::string Str() const;
     82   Jid BareJid() const;
     83 
     84   bool IsValid() const;
     85   bool IsBare() const;
     86   bool IsFull() const;
     87 
     88   bool BareEquals(const Jid & other) const;
     89 
     90   bool operator==(const Jid & other) const;
     91   bool operator!=(const Jid & other) const { return !operator==(other); }
     92 
     93   bool operator<(const Jid & other) const { return Compare(other) < 0; };
     94   bool operator>(const Jid & other) const { return Compare(other) > 0; };
     95 
     96   int Compare(const Jid & other) const;
     97 
     98   // A quick and dirty hash.  Don't count on this producing a great
     99   // distribution.
    100   uint32 ComputeLameHash() const;
    101 
    102 private:
    103 
    104   static std::string prepNode(const std::string str,
    105       std::string::const_iterator start, std::string::const_iterator end,
    106       bool *valid);
    107   static char prepNodeAscii(char ch, bool *valid);
    108   static std::string prepResource(const std::string str,
    109       std::string::const_iterator start, std::string::const_iterator end,
    110       bool *valid);
    111   static char prepResourceAscii(char ch, bool *valid);
    112   static std::string prepDomain(const std::string str,
    113       std::string::const_iterator start,  std::string::const_iterator end,
    114       bool *valid);
    115   static void prepDomain(const std::string str,
    116       std::string::const_iterator start, std::string::const_iterator end,
    117       std::string *buf, bool *valid);
    118   static void prepDomainLabel(const std::string str,
    119       std::string::const_iterator start, std::string::const_iterator end,
    120       std::string *buf, bool *valid);
    121   static char prepDomainLabelAscii(char ch, bool *valid);
    122 
    123   class Data {
    124   public:
    125     Data() : refcount_(1) {}
    126     Data(const std::string & node, const std::string &domain, const std::string & resource) :
    127       node_name_(node),
    128       domain_name_(domain),
    129       resource_name_(resource),
    130       refcount_(1) {}
    131     const std::string node_name_;
    132     const std::string domain_name_;
    133     const std::string resource_name_;
    134 
    135     void AddRef() { refcount_++; }
    136     void Release() { if (!--refcount_) delete this; }
    137   private:
    138     int refcount_;
    139   };
    140 
    141   Data * data_;
    142 };
    143 
    144 }
    145 
    146 
    147 
    148 #endif
    149