00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016 #ifndef IDENTITY_H
00017 #define IDENTITY_H
00018
00019 #ifndef USE_PCH
00020 #include <map>
00021 #include <string>
00022 #include <boost/shared_ptr.hpp>
00023 #include "debug.h"
00024 #include <libcw/events.h>
00025 #endif
00026
00027 #include "Network.h"
00028 #include "ServerConnection.h"
00029 #include "question_nt.h"
00030
00031 class MessageIn;
00032 class Authentication;
00033 class ClientSession;
00034 class PersistXML;
00035
00036
00037 static char const* const unknown_ident = "~unknown-yet";
00038
00039
00040 static char const* const unknown_hostname = "unknown-yet.net";
00041
00042
00043 static char const* const unknown_real_name = "Unknown Yet";
00044
00045
00046 static char const* const unknown_network_name = "UnknowYet";
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058 class Identity : public virtual event_client_ct {
00059 private:
00060 std::string M_key;
00061 boost::shared_ptr<Authentication> M_authentication;
00062 boost::shared_ptr<Nick> M_mynick;
00063 std::string M_authpass;
00064 Network M_private_network;
00065 ClientSession* M_client_session;
00066 std::string M_ident;
00067 std::string M_real_hostname;
00068 std::string M_hostname;
00069 std::string M_real_name;
00070 std::string M_network_name;
00071 boost::shared_ptr<ServerConnection> M_primary_server_connection;
00072 boost::shared_ptr<ServerConnection> M_secondary_server_connection;
00073
00074 public:
00075
00076 Identity(void) :
00077 M_mynick(NEW(MyNick(*this))),
00078 M_private_network("private network", true),
00079 M_client_session(NULL),
00080 M_ident(unknown_ident),
00081 M_real_hostname(unknown_hostname),
00082 M_hostname(unknown_hostname),
00083 M_real_name(unknown_real_name),
00084 M_network_name(unknown_network_name),
00085 M_primary_server_connection(NEW(ServerConnection(this)))
00086 { Dout(dc::objects, "Constructing Identity"); }
00087
00088
00089 Identity(std::string const& key) :
00090 M_key(key),
00091 M_mynick(NEW(MyNick(*this))),
00092 M_private_network("private network of " + key, true),
00093 M_client_session(NULL),
00094 M_ident(unknown_ident),
00095 M_real_hostname(unknown_hostname),
00096 M_hostname(unknown_hostname),
00097 M_real_name(unknown_real_name),
00098 M_network_name(unknown_network_name),
00099 M_primary_server_connection(NEW(ServerConnection(this)))
00100 { Dout(dc::objects, "Constructing Identity(\"" << key << "\")"); }
00101
00102
00103 ~Identity();
00104
00105
00106 Identity(Identity const& identity) : event_client_ct(identity),
00107 M_key(identity.M_key),
00108 M_authentication(identity.M_authentication),
00109 M_mynick(NEW(MyNick(*this))),
00110 M_authpass(identity.M_authpass),
00111 M_private_network(identity.M_private_network),
00112 M_client_session(identity.M_client_session),
00113 M_ident(identity.M_ident),
00114 M_real_hostname(identity.M_real_hostname),
00115 M_hostname(identity.M_hostname),
00116 M_real_name(identity.M_real_name),
00117 M_network_name(identity.M_network_name),
00118 M_primary_server_connection(NEW(ServerConnection(this))),
00119 M_secondary_server_connection(identity.M_secondary_server_connection)
00120 { ASSERT(!M_secondary_server_connection.get()); ASSERT(M_mynick.use_count() <= 1); Dout(dc::objects, "Copy-constructing Identity from " << identity); }
00121
00122
00123 void serialize(PersistXML& xml);
00124
00125
00126 std::string const& key(void) const { return M_key; }
00127
00128
00129 void new_client_message_received(ClientMessageIn const& msg);
00130
00131
00132 void new_server_message_received(ServerMessageIn const& msg);
00133
00134
00135 boost::shared_ptr<Nick const> mynick(void) const { ASSERT(!debug::secondary_connection); return M_mynick; }
00136
00137
00138 boost::shared_ptr<Nick> mynick(void) { ASSERT(!debug::secondary_connection); return M_mynick; }
00139
00140
00141 std::string server_source(void) const;
00142
00143
00144 std::string ident(void) const { return M_ident; }
00145
00146
00147 std::string real_hostname(void) const { return M_real_hostname; }
00148
00149
00150 std::string hostname(void) const { return M_hostname; }
00151
00152
00153 std::string real_name(void) const { return M_real_name; }
00154
00155
00156 std::string network_name(void) const { return M_network_name; }
00157
00158
00159 void set_ident(std::string const& ident)
00160 { Dout(dc::debug, "Setting ident to \"" << ident << "\"."); M_ident = ident; }
00161
00162
00163 void set_real_hostname(std::string const& real_hostname)
00164 { Dout(dc::debug, "Setting real hostname to \"" << real_hostname << "\"."); M_real_hostname = real_hostname; }
00165
00166
00167 void set_hostname(std::string const& hostname)
00168 { Dout(dc::debug, "Setting hostname to \"" << hostname << "\"."); M_hostname = hostname; }
00169
00170
00171 void set_real_name(std::string const& real_name) { M_real_name = real_name; }
00172
00173
00174 void set_network_name(std::string const& network_name) { M_network_name = network_name; }
00175
00176
00177 bool has_authentication_password(void) const { return !M_authpass.empty(); }
00178
00179
00180 std::string const& authentication_password(void) const { ASSERT(has_authentication_password()); return M_authpass; }
00181
00182
00183 Network const& private_network(void) const { return M_private_network; }
00184
00185
00186 Network& private_network(void) { return M_private_network; }
00187
00188
00189 Network const& network(void) const { return server_connection().network(); }
00190
00191
00192 Network& network(void) { return server_connection().network(); }
00193
00194
00195 void set_client_session(ClientSession* client_session) { M_client_session = client_session; }
00196
00197
00198 void reset_client_session(ClientSession* client_session)
00199 { if (M_client_session == client_session) M_client_session = NULL; }
00200
00201
00202 bool has_client_session(void) const { return M_client_session; }
00203
00204
00205 bool has_server_session(void) const { return M_primary_server_connection->has_server_session(); }
00206
00207
00208 ClientSession const& client_session(void) const { ASSERT(M_client_session != NULL); return *M_client_session; }
00209
00210
00211 ClientSession& client_session(void) { ASSERT(M_client_session != NULL); return *M_client_session; }
00212
00213
00214 ServerSession const& server_session(void) const { return M_primary_server_connection->server_session(); }
00215
00216
00217 ServerSession& server_session(void) { return M_primary_server_connection->server_session(); }
00218
00219
00220
00221
00222
00223 boost::shared_ptr<Target> notice_target(question_nt number);
00224
00225
00226 ServerConnection const& server_connection(void) const { return *M_primary_server_connection; }
00227
00228
00229 ServerConnection& server_connection(void) { return *M_primary_server_connection; }
00230
00231
00232 void start_secondary_server_connection(boost::shared_ptr<Server> new_server)
00233 {
00234 ASSERT(!has_secondary_server_connection());
00235 #ifdef CWDEBUG
00236
00237 debug::SecondaryConnection dummy(true);
00238 #endif
00239 M_secondary_server_connection.reset(NEW(ServerConnection(this, new_server)));
00240 M_secondary_server_connection->connect(false);
00241 }
00242
00243
00244 void swap_primary_and_secondary_connection(void)
00245 {
00246 std::swap(M_primary_server_connection, M_secondary_server_connection);
00247 M_primary_server_connection->make_primary();
00248 M_secondary_server_connection->make_secondary();
00249 }
00250
00251
00252 void end_secondary_server_connection(void)
00253 {
00254 ASSERT(has_secondary_server_connection());
00255 #ifdef CWDEBUG
00256
00257 debug::SecondaryConnection dummy(true);
00258 #endif
00259 M_secondary_server_connection->disconnect();
00260
00261 }
00262
00263
00264
00265 void destroy_secondary_server_connection(void)
00266 {
00267 M_secondary_server_connection.reset();
00268 }
00269
00270
00271 bool has_secondary_server_connection(void) const { return M_secondary_server_connection.get(); }
00272
00273
00274 boost::shared_ptr<ServerConnection const> secondary_server_connection(void) const
00275 { return boost::const_pointer_cast<ServerConnection const>(M_secondary_server_connection); }
00276
00277
00278 boost::shared_ptr<ServerConnection> secondary_server_connection(void)
00279 { return M_secondary_server_connection; }
00280
00281
00282 Authentication const& authentication(void) const { return *M_authentication; }
00283
00284
00285 Authentication& authentication(void) { return *M_authentication; }
00286
00287 void do_quit(boost::shared_ptr<Nick> nick, std::string const& quit_message);
00288 void send_notice(std::string const& notice, question_nt number = no_related_question);
00289 void received_network_domain_answer(UserAnswerEventType const& event_type,
00290 std::pair<std::string, std::string> network_domain);
00291 void pass_as_is_to_server(ClientMessageIn const& msg);
00292 void pass_as_is_to_client(ServerMessageIn const& msg);
00293
00294 private:
00295 void pass_as_is_to(MessageIn const& msg, std::ostream& os);
00296
00297 public:
00298 friend std::ostream& operator<<(std::ostream& os, Identity const& identity);
00299 };
00300
00301 #endif // IDENTITY_H