Main Page   Namespace List   Class Hierarchy   Compound List   File List   Compound Members  

jabberoo-JID.cpp

00001 /* jabberoo.cc
00002  * Jabber client library
00003  *
00004  * Original Code Copyright (C) 1999-2001 Dave Smith (dave@jabber.org)
00005  *
00006  * This library is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU Lesser General Public
00008  * License as published by the Free Software Foundation; either
00009  * version 2.1 of the License, or (at your option) any later version.
00010  * 
00011  * This library is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014  * Lesser General Public License for more details.
00015  * 
00016  * You should have received a copy of the GNU Lesser General Public
00017  * License along with this library; if not, write to the Free Software
00018  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019  *
00020  * Contributor(s): Julian Missig
00021  *
00022  * This Original Code has been modified by IBM Corporation. Modifications 
00023  * made by IBM described herein are Copyright (c) International Business 
00024  * Machines Corporation, 2002.
00025  *
00026  * Date             Modified by     Description of modification
00027  * 01/20/2002       IBM Corp.       Updated to libjudo 1.1.1
00028  */
00029 
00030 
00031 #include <JID.hh>
00032 
00033 namespace jabberoo {
00034 
00035 std::string JID::getResource(const std::string& jid)
00036 {     // Search for resource divider
00037      std::string::size_type i = jid.find("/");
00038      // If it was found, extract the resource and return it..
00039      if (i != std::string::npos)
00040         return jid.substr(i+1, jid.length()-i);
00041      else
00042         return "";
00043 }
00044 
00045 std::string JID::getUserHost(const std::string& jid)
00046 {
00047      // Search for resource divider..
00048      std::string::size_type i = jid.find("/");
00049      // If it was found, extract he userhost and return..
00050      if (i != std::string::npos)
00051           return jid.substr(0,i);
00052      else
00053           return jid;
00054 }
00055 
00056 std::string JID::getHost(const std::string& jid)
00057 {
00058      std::string::size_type d1 = jid.find("@"); // Search for @ divider
00059      std::string::size_type d2 = jid.find("/"); // Search for / divider
00060      if ((d1 != std::string::npos) && (d2 != std::string::npos))
00061           return jid.substr(d1+1,d2-d1-1);
00062      else if ((d1 != std::string::npos) && (d2 == std::string::npos))
00063           return jid.substr(d1+1, jid.length());
00064      else if ((d1 == std::string::npos) && (d2 != std::string::npos))
00065           return jid.substr(0,d2);
00066 //     else if ((d1 == std::string::npos) && (d2 == std::string::npos))
00067      else
00068           return jid;
00069 }
00070 
00071 std::string JID::getUser(const std::string& jid)
00072 {
00073         std::string::size_type d1 = jid.find("@");
00074         if (d1 != std::string::npos)
00075                 return jid.substr(0, d1);
00076         else
00077                 return jid;
00078 }
00079 
00080 bool JID::isValidUser(const std::string& user)
00081 {
00082      if ( (user.empty()) ||
00083           (user.find(' ') != std::string::npos) ||
00084           (user.find('@') != std::string::npos) ||
00085           (user.find('/') != std::string::npos) ||
00086           (user.find('\'') != std::string::npos) ||
00087           (user.find('\"') != std::string::npos) ||
00088           (user.find(':') != std::string::npos)
00089           // Insert other invalid chars here
00090           )
00091      {
00092           return false;
00093      }
00094      return true;
00095 }
00096 
00097 bool JID::isValidHost(const std::string& host)
00098 {
00099      if ( (host.empty()) ||
00100           (host.find(' ') != std::string::npos) ||
00101           (host.find('@') != std::string::npos) ||
00102           (host.find('/') != std::string::npos) ||
00103           (host.find('\'') != std::string::npos) ||
00104           (host.find('\"') != std::string::npos)
00105           // Insert other invalid chars here
00106           )
00107      {
00108           return false;
00109      }
00110      return true;
00111 }
00112 
00113 int JID::compare(const std::string& ljid, const std::string& rjid)
00114 {
00115      // User and Host are case insensitive, Resource is case sensitive
00116      int userhost = _stricmp(JID::getUserHost(ljid).c_str(), JID::getUserHost(rjid).c_str());
00117      int resource = JID::getResource(ljid).compare(JID::getResource(rjid));
00118 
00119      // If the user and host of both are equal, return whether the resource is
00120      if (userhost == 0)
00121           return resource;
00122      return userhost;
00123 }
00124 
00125 bool JID::Compare::operator()(const std::string& lhs, const std::string& rhs) const
00126 {
00127      return (JID::compare(lhs, rhs) < 0);
00128 }
00129 
00130 } // namespace jabberoo

Generated on Thu Jul 24 13:31:50 2003 for jabberoo by doxygen1.3-rc3