00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016 #ifndef USE_PCH
00017 #include "sys.h"
00018 #include "debug.h"
00019 #endif
00020
00021 #include "Prefix.h"
00022 #include "MessageIn.h"
00023 #include "exceptions.h"
00024 #include "ServerSession.h"
00025 #include "ClientSession.h"
00026
00027 template<class SESSION>
00028 void Prefix::init(SESSION& session, std::string const& prefix)
00029 {
00030 Dout(dc::objects, "Constructing Prefix(" << session << ", \"" << prefix << "\")");
00031
00032
00033 std::string::size_type nicklen = prefix.find('!');
00034 if (nicklen != std::string::npos)
00035 {
00036 std::string nickname = prefix.substr(0, nicklen);
00037 std::string::size_type hostpos = prefix.find('@', nicklen + 1);
00038 ASSERT(hostpos != std::string::npos);
00039 std::string::size_type usernamelen = hostpos - nicklen - 1;
00040 std::string username = prefix.substr(nicklen + 1, usernamelen);
00041 std::string hostname = prefix.substr(hostpos + 1);
00042
00043 Debug(attach_gdb());
00044 M_nick = session.get_nick(nickname, true);
00045 M_nick->data().set_username(username);
00046 M_nick->data().set_hostname(hostname);
00047 }
00048 else
00049 {
00050
00051
00052 M_server_name = prefix;
00053 }
00054 }
00055
00056 Prefix::Prefix(ServerMessageIn const& msg)
00057 {
00058 if (msg.has_prefix())
00059 init(msg.server_session(), msg.prefix_str());
00060 }
00061
00062 Prefix::Prefix(ClientMessageIn const& msg)
00063 {
00064 if (msg.has_prefix())
00065 init(msg.client_session(), msg.prefix_str());
00066 }
00067
00068
00069 std::string Prefix::out_prefix(ServerSession const& server_session, bool allow_uninitialized) const
00070 {
00071 if (!M_server_name.empty())
00072 return M_server_name;
00073 bool username_initialized = (M_nick->data().initialized() & username_bit);
00074 bool hostname_initialized = (M_nick->data().initialized() & hostname_bit);
00075 ASSERT(!M_nick->is_me() || !debug::secondary_connection);
00076 if (M_nick->is_me() && !(username_initialized && hostname_initialized))
00077 return M_nick->out_name(server_session);
00078 std::string result = M_nick->out_name(server_session);
00079 if (!username_initialized && allow_uninitialized)
00080 result += "!~unknown";
00081 else
00082 {
00083 result += '!';
00084 result += M_nick->data().username();
00085 }
00086 if (!hostname_initialized && allow_uninitialized)
00087 result += "@unknown.ircproxy.net";
00088 else
00089 {
00090 result += '@';
00091 result += M_nick->data().hostname();
00092 }
00093 return result;
00094 }
00095
00096
00097 std::string Prefix::out_prefix(ClientSession const& client_session, bool allow_uninitialized) const
00098 {
00099 if (!M_server_name.empty())
00100 return M_server_name;
00101 bool username_initialized = (M_nick->data().initialized() & username_bit);
00102 bool hostname_initialized = (M_nick->data().initialized() & hostname_bit);
00103 if (M_nick->is_me() && !(username_initialized && hostname_initialized))
00104 return M_nick->out_name(client_session);
00105 std::string result = M_nick->out_name(client_session);
00106 if (!username_initialized && allow_uninitialized)
00107 result += "!~unknown";
00108 else
00109 {
00110 result += '!';
00111 result += M_nick->data().username();
00112 }
00113 if (!hostname_initialized && allow_uninitialized)
00114 result += "@unknown.ircproxy.net";
00115 else
00116 {
00117 result += '@';
00118 result += M_nick->data().hostname();
00119 }
00120 return result;
00121 }