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 "Target.h"
00022 #include "MessageIn.h"
00023 #include "Network.h"
00024 #include "ClientSession.h"
00025 #include "Identity.h"
00026 #include "exceptions.h"
00027 #include "is_channel.h"
00028 #include "Channel.h"
00029
00030 Target::~Target()
00031 {
00032 #if 0 // FIXME
00033 ASSERT(!debug::secondary_connection);
00034 Dout(dc::objects, "Destructing Target \"" << M_serverside_name << "\" of network \"" << M_network->name() << "\".");
00035
00036
00037 if (!M_network->is_private())
00038 {
00039 target_lookup_map_type::iterator iter = M_network->targets().find(M_serverside_name);
00040
00041 if (iter != M_network->targets().end())
00042 {
00043 Dout(dc::maps, "Erasing " << M_serverside_name << " from Network map.");
00044 M_network->targets().erase(iter);
00045 }
00046 #ifdef CWDEBUG
00047 else
00048 Dout(dc::notice, "Could not find \"" << M_serverside_name << "\" in network map " << M_network->targets());
00049 #endif
00050 }
00051 #endif
00052 }
00053
00054 boost::shared_ptr<Nick> Target::get_joined_nick(Identity& identity, std::string const& nick_name, boost::shared_ptr<Channel>& channel)
00055 {
00056 #if 0
00057 ASSERT(!debug::secondary_connection);
00058 DoutEntering(dc::debug, "Target::get_joined_nick(" << identity << ", \"" << nick_name << "\", \"" << channel->serverside_name() << "\")");
00059 ASSERT(is_nick(nick_name));
00060
00061
00062 Network& network(identity.server_connection().network());
00063 ASSERT(!network.is_private());
00064
00065 Channel::members_type::iterator iter = channel->members().find(nick_name);
00066 if (iter == channel->members().end())
00067 THROW_EXCEPTION(unknown_target(), "The identity " << identity.key() << " has no server side joined nick \"" << nick_name << "\" on channel " << channel);
00068
00069 return iter->second.get_nick();
00070 #endif
00071 }
00072
00073 boost::shared_ptr<Nick> Target::add_joined_nick(Identity& identity, std::string const& nick_name, boost::shared_ptr<Channel>& channel)
00074 {
00075 #if 0
00076 ASSERT(!debug::secondary_connection);
00077 DoutEntering(dc::maps|dc::debug, "Target::add_joined_nick(" << identity << ", \"" << nick_name << "\", \"" << channel->serverside_name() << "\")");
00078 ASSERT(is_nick(nick_name));
00079
00080 boost::shared_ptr<Nick> new_nick;
00081
00082 Network& network(identity.server_connection().network());
00083 ASSERT(!network.is_private());
00084
00085 target_lookup_map_type::iterator network_iter = network.targets().find(nick_name);
00086 if (network_iter != network.targets().end())
00087 new_nick = boost::static_pointer_cast<Nick>(network_iter->second.lock());
00088 else
00089 {
00090 Nick* nick = new Nick(identity, nick_name, network);
00091 AllocTag(nick, nick_name);
00092 new_nick.reset(nick);
00093 Dout(dc::maps, "Inserting " << nick_name << " in Network map.");
00094 #if defined(CWDEBUG) || defined(DEBUG)
00095 std::pair<target_lookup_map_type::iterator, bool> res =
00096 #endif
00097 network.targets().insert(target_lookup_map_type::value_type(nick_name, new_nick));
00098 ASSERT(res.second);
00099 }
00100
00101 Dout(dc::maps, "Adding nick " << nick_name << " as member to " << channel->serverside_name());
00102 #if defined(CWDEBUG) || defined(DEBUG)
00103 std::pair<Channel::members_type::iterator, bool> res =
00104 #endif
00105 channel->members().insert(Channel::members_type::value_type(nick_name, Member(new_nick)));
00106 ASSERT(res.second);
00107 new_nick->add_channel(channel);
00108 return new_nick;
00109 #endif
00110 }
00111
00112
00113
00114
00115
00116 void Target::serverside_add_channel(ServerSession& server_session, boost::shared_ptr<Channel> new_channel)
00117 {
00118 #if 0
00119 std::string channel_name(new_channel->serverside_name());
00120 ASSERT(is_channel(channel_name));
00121 std::pair<target_lookup_map_type::iterator, bool> res =
00122 server_session.server_connection().network().targets().insert(target_lookup_map_type::value_type(channel_name, new_channel));
00123 #ifdef CWDEBUG
00124 if (res.second)
00125 Dout(dc::maps, "Inserted " << channel_name << " in Network map.");
00126 #endif
00127 Dout(dc::maps, "Inserting " << channel_name << " in serverside joined_channels map.");
00128 #if defined(CWDEBUG) || defined(DEBUG)
00129 std::pair<ClientSession::joined_channels_type::iterator, bool> serverside_res =
00130 #endif
00131 server_session.joined_channels().insert(ServerSession::joined_channels_type::value_type(channel_name, new_channel));
00132 ASSERT(serverside_res.second);
00133 #endif
00134 }
00135
00136 boost::shared_ptr<Channel> Target::add_channel(Identity& identity, std::string const& channel_name)
00137 {
00138 ASSERT(!debug::secondary_connection);
00139 DoutEntering(dc::maps|dc::debug, "Target::add_channel(" << identity << ", \"" << channel_name << "\")");
00140
00141 #ifdef CWDEBUG
00142 ASSERT(is_channel(channel_name));
00143 ServerSession::joined_channels_type::iterator iter1 = identity.server_session().joined_channels().find(channel_name);
00144 ClientSession::joined_channels_type::iterator iter2 = identity.client_session().joined_channels().find(channel_name);
00145 ASSERT(iter1 == identity.server_session().joined_channels().end() &&
00146 iter2 == identity.client_session().joined_channels().end());
00147 #endif
00148 boost::shared_ptr<Channel> new_channel;
00149 Network& network(identity.server_connection().network());
00150 target_lookup_map_type::iterator network_iter = network.targets().find(channel_name);
00151 if (network_iter != network.targets().end())
00152 new_channel = boost::static_pointer_cast<Channel>(network_iter->second.lock());
00153 else
00154 {
00155 Channel* channel = new Channel(identity, channel_name, network);
00156 AllocTag(channel, channel_name);
00157 new_channel.reset(channel);
00158 }
00159 Target::serverside_add_channel(identity.server_session(), new_channel);
00160 Dout(dc::maps, "Inserting " << channel_name << " in clientside joined_channels map.");
00161 #if defined(CWDEBUG) || defined(DEBUG)
00162 std::pair<ClientSession::joined_channels_type::iterator, bool> clientside_res =
00163 #endif
00164 identity.client_session().joined_channels().insert(ClientSession::joined_channels_type::value_type(channel_name, new_channel));
00165 ASSERT(clientside_res.second);
00166 return new_channel;
00167 }
00168
00169 void Target::sub_channel(Identity& identity)
00170 {
00171 #if 0 // FIXME
00172 ASSERT(!debug::secondary_connection);
00173 DoutEntering(dc::maps|dc::debug, "Target::sub_channel(" << identity << ") for " << *this);
00174 ASSERT(is_channel(M_serverside_name));
00175 Channel& channel(*static_cast<Channel*>(this));
00176 channel.sub_joined_nick(identity.mynick());
00177 for (Channel::members_type::iterator iter = channel.members().begin(); iter != channel.members().end(); ++iter)
00178 iter->second.get_nick()->sub_channel(channel);
00179 ServerSession::joined_channels_type::iterator iter1 = identity.server_session().joined_channels().find(M_serverside_name);
00180 ClientSession::joined_channels_type::iterator iter2 = identity.client_session().joined_channels().find(M_clientside_name);
00181 ASSERT(iter1 != identity.server_session().joined_channels().end() &&
00182 iter2 != identity.client_session().joined_channels().end());
00183 Dout(dc::maps, "Erasing " << M_serverside_name << " from serverside joined_channels map.");
00184 identity.server_session().joined_channels().erase(iter1);
00185 Dout(dc::maps, "Erasing " << M_clientside_name << " from clientside joined_channels map.");
00186 identity.client_session().joined_channels().erase(iter2);
00187 #endif
00188 }
00189
00190 boost::shared_ptr<Channel> Target::serverside_get_channel(Identity& identity, std::string const& channel_name, bool joined)
00191 {
00192 ASSERT(!debug::secondary_connection);
00193 ASSERT(is_channel(channel_name));
00194
00195
00196 ServerSession::joined_channels_type::iterator iter =
00197 (channel_name[0] == '@') ? identity.server_session().joined_channels().find(channel_name.substr(1))
00198 : identity.server_session().joined_channels().find(channel_name);
00199 if (iter != identity.server_session().joined_channels().end())
00200 return boost::static_pointer_cast<Channel>(iter->second);
00201
00202 if (joined)
00203 return Target::add_channel(identity, channel_name);
00204
00205 THROW_EXCEPTION(unknown_target(), "The identity " << identity.key() << " has no server side channel \"" << channel_name << "\"");
00206 }
00207
00208 boost::shared_ptr<Channel> Target::clientside_get_channel(Identity const& identity, std::string const& channel_name)
00209 {
00210 ASSERT(!debug::secondary_connection);
00211 ASSERT(is_channel(channel_name));
00212
00213
00214 ClientSession::private_targets_type::const_iterator priv_iter = identity.client_session().private_targets().find(channel_name);
00215 if (priv_iter != identity.client_session().private_targets().end())
00216 return boost::static_pointer_cast<Channel>(priv_iter->second);
00217
00218
00219 ClientSession::joined_channels_type::const_iterator iter = identity.client_session().joined_channels().find(channel_name);
00220 if (iter != identity.client_session().joined_channels().end())
00221 return boost::static_pointer_cast<Channel>(iter->second);
00222
00223 THROW_EXCEPTION(unknown_target(), "The identity " << identity.key() << " has no client side channel \"" << channel_name << "\"");
00224 }