Home | History | Annotate | Download | only in xmpp
      1 /*
      2  *  Copyright 2004 The WebRTC Project Authors. All rights reserved.
      3  *
      4  *  Use of this source code is governed by a BSD-style license
      5  *  that can be found in the LICENSE file in the root of the source
      6  *  tree. An additional intellectual property rights grant can be found
      7  *  in the file PATENTS.  All contributing project authors may
      8  *  be found in the AUTHORS file in the root of the source tree.
      9  */
     10 
     11 #ifndef WEBRTC_LIBJINGLE_XMPP_JID_H_
     12 #define WEBRTC_LIBJINGLE_XMPP_JID_H_
     13 
     14 #include <string>
     15 #include "webrtc/libjingle/xmllite/xmlconstants.h"
     16 #include "webrtc/base/basictypes.h"
     17 
     18 namespace buzz {
     19 
     20 // The Jid class encapsulates and provides parsing help for Jids. A Jid
     21 // consists of three parts: the node, the domain and the resource, e.g.:
     22 //
     23 // node@domain/resource
     24 //
     25 // The node and resource are both optional. A valid jid is defined to have
     26 // a domain. A bare jid is defined to not have a resource and a full jid
     27 // *does* have a resource.
     28 class Jid {
     29 public:
     30   explicit Jid();
     31   explicit Jid(const std::string& jid_string);
     32   explicit Jid(const std::string& node_name,
     33                const std::string& domain_name,
     34                const std::string& resource_name);
     35   ~Jid();
     36 
     37   const std::string & node() const { return node_name_; }
     38   const std::string & domain() const { return domain_name_;  }
     39   const std::string & resource() const { return resource_name_; }
     40 
     41   std::string Str() const;
     42   Jid BareJid() const;
     43 
     44   bool IsEmpty() const;
     45   bool IsValid() const;
     46   bool IsBare() const;
     47   bool IsFull() const;
     48 
     49   bool BareEquals(const Jid& other) const;
     50   void CopyFrom(const Jid& jid);
     51   bool operator==(const Jid& other) const;
     52   bool operator!=(const Jid& other) const { return !operator==(other); }
     53 
     54   bool operator<(const Jid& other) const { return Compare(other) < 0; };
     55   bool operator>(const Jid& other) const { return Compare(other) > 0; };
     56 
     57   int Compare(const Jid & other) const;
     58 
     59 private:
     60   void ValidateOrReset();
     61 
     62   static std::string PrepNode(const std::string& node, bool* valid);
     63   static char PrepNodeAscii(char ch, bool* valid);
     64   static std::string PrepResource(const std::string& start, bool* valid);
     65   static char PrepResourceAscii(char ch, bool* valid);
     66   static std::string PrepDomain(const std::string& domain, bool* valid);
     67   static void PrepDomain(const std::string& domain,
     68                          std::string* buf, bool* valid);
     69   static void PrepDomainLabel(
     70       std::string::const_iterator start, std::string::const_iterator end,
     71       std::string* buf, bool* valid);
     72   static char PrepDomainLabelAscii(char ch, bool *valid);
     73 
     74   std::string node_name_;
     75   std::string domain_name_;
     76   std::string resource_name_;
     77 };
     78 
     79 }
     80 
     81 #endif  // WEBRTC_LIBJINGLE_XMPP_JID_H_
     82