00001
00022 #ifndef CLIENT_H
00023 #define CLIENT_H
00024
00025 #include <iostream>
00026 #include <string>
00027
00028 #include <map>
00029
00030 #include <sigc++/signal_system.h>
00031
00032 #include <time.h>
00033
00034 #include <libicq2000/buffer.h>
00035 #include <libicq2000/socket.h>
00036 #include <libicq2000/events.h>
00037 #include <libicq2000/constants.h>
00038 #include <libicq2000/Contact.h>
00039 #include <libicq2000/ContactList.h>
00040 #include <libicq2000/custom_marshal.h>
00041 #include <libicq2000/Translator.h>
00042 #include <libicq2000/RequestIDCache.h>
00043 #include <libicq2000/ICBMCookieCache.h>
00044 #include <libicq2000/DirectClient.h>
00045 #include <libicq2000/DCCache.h>
00046 #include <libicq2000/userinfoconstants.h>
00047
00048 using std::string;
00049 using SigC::Signal1;
00050
00051 namespace ICQ2000 {
00052
00053
00054 class MessageSNAC;
00055 class MessageACKSNAC;
00056 class MessageOfflineUserSNAC;
00057 class SrvResponseSNAC;
00058 class UINResponseSNAC;
00059 class RateInfoChangeSNAC;
00060 class BuddyOnlineSNAC;
00061 class BuddyOfflineSNAC;
00062 class UserInfoSNAC;
00063 class OutSNAC;
00064
00065
00066 const unsigned short STATUS_FLAG_ONLINE = 0x0000;
00067 const unsigned short STATUS_FLAG_AWAY = 0x0001;
00068 const unsigned short STATUS_FLAG_DND = 0x0002;
00069 const unsigned short STATUS_FLAG_NA = 0x0004;
00070 const unsigned short STATUS_FLAG_OCCUPIED = 0x0010;
00071 const unsigned short STATUS_FLAG_FREEFORCHAT = 0x0020;
00072 const unsigned short STATUS_FLAG_INVISIBLE = 0x0100;
00073
00079 class Client : public SigC::Object {
00080 private:
00081 enum State { NOT_CONNECTED,
00082 AUTH_AWAITING_CONN_ACK,
00083 AUTH_AWAITING_AUTH_REPLY,
00084 BOS_AWAITING_CONN_ACK,
00085 BOS_AWAITING_LOGIN_REPLY,
00086 BOS_LOGGED_IN,
00087 UIN_AWAITING_CONN_ACK,
00088 UIN_AWAITING_UIN_REPLY
00089 } m_state;
00090
00091 unsigned int m_uin;
00092 string m_password;
00093
00094 string m_authorizerHostname;
00095 unsigned short m_authorizerPort;
00096
00097 string m_bosHostname;
00098 unsigned short m_bosPort;
00099 bool m_bosOverridePort;
00100
00101 bool m_in_dc, m_out_dc;
00102
00103 unsigned short m_client_seq_num;
00104 unsigned int m_requestid;
00105 Status m_status;
00106 bool m_invisible;
00107
00108 Translator m_translator;
00109
00110 ContactList m_contact_list;
00111
00112 unsigned char *m_cookie_data;
00113 unsigned short m_cookie_length;
00114
00115 unsigned int m_ext_ip;
00116 TCPSocket m_serverSocket;
00117 TCPServer m_listenServer;
00118
00119 DCCache m_dccache;
00120 map<unsigned int, DirectClient*> m_uinmap;
00121
00122 time_t m_last_server_ping;
00123
00124 RequestIDCache m_reqidcache;
00125 ICBMCookieCache m_cookiecache;
00126
00127 Buffer m_recv;
00128
00129 void Init();
00130 unsigned short NextSeqNum();
00131 unsigned int NextRequestID();
00132
00133 void ConnectAuthorizer(State state);
00134 void DisconnectAuthorizer();
00135 void ConnectBOS();
00136 void DisconnectBOS();
00137 void DisconnectInt();
00138
00139
00140 void PingServer();
00141
00142 DirectClient* ConnectDirect(Contact *c);
00143 void DisconnectDirectConns();
00144 void DisconnectDirectConn(int fd);
00145
00146
00147 void SignalConnect();
00148 void SignalDisconnect(DisconnectedEvent::Reason r);
00149 void SignalMessage(MessageSNAC *snac);
00150 void SignalMessageACK(MessageACKSNAC *snac);
00151 void SignalMessageOfflineUser(MessageOfflineUserSNAC *snac);
00152 void SignalSrvResponse(SrvResponseSNAC *snac);
00153 void SignalUINResponse(UINResponseSNAC *snac);
00154 void SignalUINRequestError();
00155 void SignalRateInfoChange(RateInfoChangeSNAC *snac);
00156 void SignalLog(LogEvent::LogType type, const string& msg);
00157 void SignalUserOnline(BuddyOnlineSNAC *snac);
00158 void SignalUserOffline(BuddyOfflineSNAC *snac);
00159 void SignalUserAdded(Contact *c);
00160 void SignalServerBasedContact(Contact *c);
00161 void SignalUserRemoved(Contact *c);
00162 void SignalAddSocket(int fd, SocketEvent::Mode m);
00163 void SignalRemoveSocket(int fd);
00164
00165
00166 void SendAuthReq();
00167 void SendNewUINReq();
00168 void SendCookie();
00169 void SendCapabilities();
00170 void SendRateInfoRequest();
00171 void SendRateInfoAck();
00172 void SendPersonalInfoRequest();
00173 void SendAddICBMParameter();
00174 void SendSetUserInfo();
00175 void SendLogin();
00176 void SendOfflineMessagesRequest();
00177 void SendOfflineMessagesACK();
00178
00179 void SendAdvancedACK(MessageSNAC *snac);
00180
00181 void Send(Buffer& b);
00182
00183 void HandleUserInfoSNAC(UserInfoSNAC *snac);
00184
00185 Buffer::marker FLAPHeader(Buffer& b, unsigned char channel);
00186 void FLAPFooter(Buffer& b, Buffer::marker& mk);
00187
00188 void FLAPwrapSNAC(Buffer& b, const OutSNAC& snac);
00189 void FLAPwrapSNACandSend(const OutSNAC& snac);
00190
00191
00192
00197 void RecvFromServer();
00198
00199 void Parse();
00200 void ParseCh1(Buffer& b, unsigned short seq_num);
00201 void ParseCh2(Buffer& b, unsigned short seq_num);
00202 void ParseCh3(Buffer& b, unsigned short seq_num);
00203 void ParseCh4(Buffer& b, unsigned short seq_num);
00204
00205
00206
00207 Contact* lookupICQ(unsigned int uin);
00208 Contact* lookupMobile(const string& m);
00209
00210 Contact* getUserInfoCacheContact(unsigned int reqid);
00211
00212
00213
00214
00215 unsigned short MapStatusToICQStatus(Status st, bool inv);
00216 Status MapICQStatusToStatus(unsigned short st);
00217 bool MapICQStatusToInvisible(unsigned short st);
00218
00219 void ICBMCookieCache_expired_cb(MessageEvent *ev);
00220 void dccache_expired_cb(DirectClient *dc);
00221 void reqidcache_expired_cb( RequestIDCacheValue *v );
00222 void dc_connected_cb(DirectClient *dc);
00223 void dc_log_cb(LogEvent *ev);
00224 void dc_socket_cb(SocketEvent *ev);
00225 void dc_messaged_cb(MessageEvent *ev);
00226 void dc_messageack_cb(MessageEvent *ev);
00227
00228 bool SendDirect(MessageEvent *ev);
00229
00230 void SendViaServer(MessageEvent *ev);
00231 void SendViaServerAdvanced(MessageEvent *ev);
00232 void SendViaServerNormal(MessageEvent *ev);
00233 UINICQSubType* EventToUINICQSubType(MessageEvent *ev);
00234
00235 void Connect();
00236 void Disconnect(DisconnectedEvent::Reason r = DisconnectedEvent::REQUESTED);
00237
00238 public:
00239 Client();
00240 Client(const unsigned int uin, const string& password);
00241 ~Client();
00242
00248 void setUIN(unsigned int uin) { m_uin = uin; }
00249
00254 unsigned int getUIN() const { return m_uin; }
00255
00260 void setPassword(const string& password) { m_password = password; }
00261
00266 string getPassword() const { return m_password; }
00267
00268 bool setTranslationMap(const string& szMapFileName);
00269 const string& getTranslationMapFileName() const;
00270 const string& getTranslationMapName() const;
00271 bool usingDefaultMap() const;
00272
00273
00279 Signal1<void,ConnectedEvent*> connected;
00280
00294 Signal1<void,DisconnectedEvent*> disconnected;
00295
00301 Signal1<bool,MessageEvent*,StopOnTrueMarshal> messaged;
00302
00311 Signal1<void,MessageEvent*> messageack;
00312
00317 Signal1<void,ContactListEvent*> contactlist;
00318
00324 Signal1<void,NewUINEvent*> newuin;
00325
00331 Signal1<void,RateInfoChangeEvent*> rate;
00332
00341 Signal1<void,LogEvent*> logger;
00342
00352 Signal1<void,SocketEvent*> socket;
00353
00364 Signal1<void,MyStatusChangeEvent*> statuschanged;
00365
00372 Signal1<void,AwayMessageEvent*> want_auto_resp;
00373
00380 Signal1<void,SearchResultEvent*> search_result;
00381
00382
00383
00384 void SignalUserInfoChange(Contact *c);
00385 void SignalMessageQueueChanged(Contact *c);
00386
00387
00388
00389 void SendEvent(MessageEvent *ev);
00390
00391
00392 void setStatus(const Status st, bool inv = false);
00393 Status getStatus() const;
00394 bool getInvisible() const;
00395
00396
00397 void addContact(Contact& c);
00398 void removeContact(const unsigned int uin);
00399 Contact* getContact(const unsigned int uin);
00400 void fetchSimpleContactInfo(Contact* c);
00401 void fetchDetailContactInfo(Contact* c);
00402 void fetchServerBasedContactList();
00403
00404
00405 SearchResultEvent* searchForContacts(const string& nickname, const string& firstname,
00406 const string& lastname);
00407
00408 SearchResultEvent* searchForContacts(const string& nickname, const string& firstname,
00409 const string& lastname, const string& email,
00410 unsigned short min_age, unsigned short max_age,
00411 Sex sex, unsigned char language, const string& city,
00412 const string& state, unsigned short country,
00413 const string& company_name, const string& department,
00414 const string& position, bool only_online);
00415
00416 SearchResultEvent* searchForContacts(unsigned int uin);
00417
00418
00419
00420
00421
00422
00423
00424
00425
00426
00427
00428
00429
00430 void setLoginServerHost(const string& host);
00431 string getLoginServerHost() const;
00432
00433 void setLoginServerPort(const unsigned short& port);
00434 unsigned short getLoginServerPort() const;
00435
00436 void setBOSServerOverridePort(const bool& b);
00437 bool getBOSServerOverridePort() const;
00438
00439 void setBOSServerPort(const unsigned short& port);
00440 unsigned short getBOSServerPort() const;
00441
00442 void setAcceptInDC(bool d);
00443 bool getAcceptInDC() const;
00444
00445 void setUseOutDC(bool d);
00446 bool getUseOutDC() const;
00447
00448 void Poll();
00449 void socket_cb(int fd, SocketEvent::Mode m);
00450
00451 void RegisterUIN();
00452
00453
00454
00455
00456
00457 bool isConnected() const;
00458
00459 };
00460 }
00461
00462 #endif