00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014 #include "jid.h"
00015
00016 #include "prep.h"
00017
00018 namespace gloox
00019 {
00020
00021 JID::JID()
00022 {
00023 }
00024
00025 JID::JID( const std::string& jid )
00026 {
00027 setJID( jid );
00028 }
00029
00030 JID::~JID()
00031 {
00032 }
00033
00034 void JID::setJID( const std::string& jid )
00035 {
00036 if( jid.empty() )
00037 return;
00038
00039 size_t at = jid.find( "@", 0 );
00040 size_t slash = jid.find( "/", 0 );
00041
00042 if( ( at == std::string::npos ) && ( slash == std::string::npos ) )
00043 {
00044 m_serverRaw = jid;
00045 }
00046
00047 if( ( at != std::string::npos ) && ( slash != std::string::npos ) )
00048 {
00049 m_username = Prep::nodeprep( jid.substr( 0, at ) );
00050 m_serverRaw = jid.substr( at + 1, slash - at - 1 );
00051 m_resource = Prep::resourceprep( jid.substr( slash + 1 ) );
00052 }
00053
00054 if( ( at == std::string::npos ) && ( slash != std::string::npos ) )
00055 {
00056 m_serverRaw = jid.substr( 0, slash );
00057 m_resource = Prep::resourceprep( jid.substr( slash + 1 ) );
00058 }
00059
00060 if( ( at != std::string::npos ) && ( slash == std::string::npos ) )
00061 {
00062 m_username = Prep::nodeprep( jid.substr( 0, at ) );
00063 m_serverRaw = jid.substr( at + 1 );
00064 }
00065
00066 m_server = Prep::nameprep( m_serverRaw );
00067 }
00068
00069 void JID::setUsername( const std::string& username )
00070 {
00071 m_username = Prep::nodeprep( username );
00072 }
00073
00074 void JID::setServer( const std::string& server )
00075 {
00076 m_serverRaw = server;
00077 m_server = Prep::nameprep( m_serverRaw );
00078 }
00079
00080 void JID::setResource( const std::string& resource )
00081 {
00082 m_resource = Prep::resourceprep( resource );
00083 }
00084
00085 std::string JID::full() const
00086 {
00087 if( m_server.empty() )
00088 return "";
00089 else if( m_username.empty() )
00090 if( m_resource.empty() )
00091 return m_server;
00092 else
00093 return ( m_server + "/" + m_resource );
00094 else
00095 if( m_resource.empty() )
00096 return ( m_username + "@" + m_server );
00097 else
00098 return ( m_username + "@" + m_server + "/" + m_resource );
00099 }
00100
00101 std::string JID::bare() const
00102 {
00103 if( m_server.empty() )
00104 return "";
00105 else if( m_username.empty() )
00106 return m_server;
00107 else
00108 return m_username + "@" + m_server;
00109 }
00110
00111 int JID::operator==( const JID& right ) const
00112 {
00113 return ( ( m_resource == right.m_resource )
00114 && ( m_server == right.m_server )
00115 && ( m_username == right.m_username ) );
00116 }
00117
00118 int JID::operator!=( const JID& right ) const
00119 {
00120 return !( *this == right );
00121 }
00122
00123 }