summaryrefslogtreecommitdiffstats
path: root/src/kvirc/kernel/kvi_ircconnection.h
blob: 9e70c3d544219ffff2dc8da65aec3949bbb890df (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
#ifndef _KVI_IRCCONNECTION_H_
#define _KVI_IRCCONNECTION_H_
//=============================================================================
//
//   File : kvi_ircconnection.h
//   Created on Mon 03 May 2004 01:45:42 by Szymon Stefanek
//
//   This file is part of the KVIrc IRC client distribution
//   Copyright (C) 2004 Szymon Stefanek <pragma at kvirc dot net>
//
//   This program is FREE software. You can redistribute it and/or
//   modify it under the terms of the GNU General Public License
//   as published by the Free Software Foundation; either version 2
//   of the License, or (at your opinion) any later version.
//
//   This program is distributed in the HOPE that it will be USEFUL,
//   but WITHOUT ANY WARRANTY; without even the implied warranty of
//   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
//   See the GNU General Public License for more details.
//
//   You should have received a copy of the GNU General Public License
//   along with this program. If not, write to the Free Software Foundation,
//   Inc. ,51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
//
//=============================================================================

#include "kvi_settings.h"
#include "kvi_qstring.h"
#include "kvi_qcstring.h"
#include "kvi_pointerlist.h"
#include "kvi_time.h"
#include <tqobject.h>

class KviConsole;
class KviIrcNetworkData;
class KviIrcServer;
class KviProxy;
class KviIrcSocket;
class KviIrcLink;
class KviDataBuffer;
class KviIrcContext;
class KviFrame;
class KviChannel;
class KviQuery;
class KviIrcConnectionTarget;
class KviIrcUserDataBase;
class KviIrcConnectionUserInfo;
class KviIrcConnectionServerInfo;
class KviIrcConnectionStateData;
class KviIrcConnectionAntiCtcpFloodData;
class KviIrcConnectionNetsplitDetectorData;
class KviIrcConnectionAsyncWhoisData;
class KviIrcConnectionStatistics;
class KviLagMeter;
class KviNotifyListManager;
class KviDns;
class KviUserIdentity;

class TQTimer;
class TQTextCodec;


class KVIRC_API KviIrcConnection : public TQObject
{
	friend class KviConsole;
	friend class KviIrcContext;
	friend class KviIrcLink;
	friend class KviServerParser;
	TQ_OBJECT
  
public:
	// only KviConsole can create this
	// pContext must NOT be null
	// pTarget must NOT be null and must be allocated with new! this class takes the ownership
	KviIrcConnection(KviIrcContext * pContext,KviIrcConnectionTarget * pTarget,KviUserIdentity * pIdentity);
	~KviIrcConnection();
public:
	enum State { Idle, Connecting, Connected };
private:
	KviFrame                             * m_pFrame;                // shallow, never null
	KviConsole                           * m_pConsole;              // shallow, never null
	KviIrcContext                        * m_pContext;              // shallow, never null
	
	State                                  m_eState;
	bool                                   m_bIdentdAttached;

	KviIrcConnectionTarget               * m_pTarget;               // owned, never null

	KviIrcLink                           * m_pLink;                 // owned, never null
	
	// The initial informations about the user we'll send out to the server
	// Note that the ACTUAL user informations are in m_pUserInfo instead
	KviUserIdentity                      * m_pUserIdentity;         // owned, never null
	
	// The ACTUAL user informations
	KviIrcConnectionUserInfo             * m_pUserInfo;             // owned, never null
	// The ACTUAL server informations
	KviIrcConnectionServerInfo           * m_pServerInfo;           // owned, never null

	KviIrcConnectionStateData            * m_pStateData;            // owned, never null

	KviPointerList<KviChannel>               * m_pChannelList;          // owned, never null, elements shallow
	KviPointerList<KviQuery>                 * m_pQueryList;            // owned, never null, elements shallow

	KviIrcUserDataBase                   * m_pUserDataBase;         // owned, never null
	
	KviNotifyListManager                 * m_pNotifyListManager;    // owned, see restartNotifyList()
	TQTimer                               * m_pNotifyListTimer;      // delayed startup timer for the notify lists

	KviLagMeter                          * m_pLagMeter;             // owned, may be null (when not running)

	KviIrcConnectionAntiCtcpFloodData    * m_pAntiCtcpFloodData;    // owned, never null
	KviIrcConnectionNetsplitDetectorData * m_pNetsplitDetectorData; // owned, never null
	KviIrcConnectionAsyncWhoisData       * m_pAsyncWhoisData;       // owned, never null
	
	KviIrcConnectionStatistics           * m_pStatistics;           // owned, never null

	KviDns                               * m_pLocalhostDns;         // FIXME: this should go to an aux structure
	
	TQTextCodec                           * m_pTextCodec;            // connection codec: never null
public:
	// returns a pointer to the owning console
	// the pointer is NEVER null
	KviConsole * console(){ return m_pConsole; };
	// never null
	KviFrame * frame(){ return m_pFrame; };
	// never null
	KviIrcContext * context(){ return m_pContext; };
	// forwarder from KviIrcConnectionTarget, never null
	KVI_DEPRECATED KviIrcServer * server();
	// forwarder from KviIrcConnectionTarget, may be null
	KVI_DEPRECATED KviProxy * proxy();
	// never null!
	KviIrcConnectionTarget * target(){ return m_pTarget; };
	// never null!
	KviIrcLink * link(){ return m_pLink; };
	// this should not be used...
	KviIrcSocket * socket() KVI_DEPRECATED;
	// the current state
	State state(){ return m_eState; };
	// forwarder from KviIrcConnectionTarget, always non-empty string
	const TQString & networkName();
	// never null!
	KviIrcUserDataBase * userDataBase(){ return m_pUserDataBase; };
	// never null. Use #include "kvi_ircconnectionuserinfo.h"
	KviIrcConnectionUserInfo * userInfo(){ return m_pUserInfo; };
	// never null. Use #include "kvi_ircconnectionserverinfo.h"
	KviIrcConnectionServerInfo * serverInfo(){ return m_pServerInfo; };
	// never null. Use #include "kvi_ircconnectionstatedata.h"
	KviIrcConnectionStateData * stateData(){ return m_pStateData; };
	// never null. Use #include "kvi_ircconnectionantictcpflooddata.h"
	KviIrcConnectionAntiCtcpFloodData * antiCtcpFloodData(){ return m_pAntiCtcpFloodData; };
	// never null. Use #include "kvi_ircconnectionnetsplitdetectordata.h" 
	KviIrcConnectionNetsplitDetectorData * netsplitDetectorData(){ return m_pNetsplitDetectorData; };
	// never null. Use #include "kvi_ircconnectionasyncwhoisdata.h"
	KviIrcConnectionAsyncWhoisData * asyncWhoisData(){ return m_pAsyncWhoisData; };
	// never null. Use #include "kvi_ircconnectionstatistics.h"
	KviIrcConnectionStatistics * statistics(){ return m_pStatistics; };
	// may be null
	KviNotifyListManager * notifyListManager(){ return m_pNotifyListManager; };
	// may be null (when not running)
	KviLagMeter * lagMeter(){ return m_pLagMeter; };
	// should be never null.. but if really everything goes wrong, it might be...
	TQTextCodec * textCodec(){ return m_pTextCodec; };
public:
	// helper (really common access to userInfo()->nickName())
	const TQString & currentNickName();
	// helper (really common access to userInfo()->userName())
	const TQString & currentUserName();
	// helper (really common access to serverInfo()->name())
	const TQString & currentServerName();

	// world interface: the following three functions use m_pLink->sendPacket()
	bool sendData(const char *buffer,int buflen = -1);
	bool sendFmtData(const char *fmt,...);
	//
	// channel management
	//
	// ----> KviChannelManager ?
	KviChannel * findChannel(const TQString &name);
	KviPointerList<KviChannel> * channelList(){ return m_pChannelList; };
	int getCommonChannels(const TQString &nick,TQString &szChansBuffer,bool bAddEscapeSequences = true);
	KviChannel * createChannel(const TQString &name);
	void registerChannel(KviChannel *c);
	void unregisterChannel(KviChannel *c);
	void keepChannelsOpenAfterDisconnect();
	void closeAllChannels();
	void setEncoding(const TQString &szEncoding);
public slots:
	void partAllChannels();
	void unhighlightAllChannels();
	void unhighlightAllQueries();
	void restartNotifyList();
	void closeAllQueries();
public:
	//
	// query management
	//
	// ----> KviQueryManager ?
	KviQuery * findQuery(const TQString &nick);
	KviPointerList<KviQuery> * queryList(){ return m_pQueryList; };
	KviQuery * createQuery(const TQString &nick);
	void registerQuery(KviQuery *q);
	void unregisterQuery(KviQuery *q);
	void keepQueriesOpenAfterDisconnect();
	void resurrectDeadQueries();
	void restartLagMeter();
protected:
	//
	// notify list management
	//
	void delayedStartNotifyList();
	//
	// login operations
	//
	void resolveLocalHost();
	void loginToIrcServer();
protected:
	//
	// KviServerParser interface
	//
	// called to start the notify lists when RPL_ENDOFMOTD is received
	void endOfMotdReceived();
	// called when RPL_SERVINFO (004) is received
	void serverInfoReceived(const TQString &szServerName,const TQString &szUserModes,const TQString &szChanModes);
	// called to update the away state
	void changeAwayState(bool bAway);
	// called on each JOIN (but meaningful almost only on the first one) (FIXME: call it also on other messages ?)
	void userInfoReceived(const TQString &szUserName,const TQString &szHostName);
	// called when NICK is received
	void nickChange(const TQString &szNewNick);
	// called when MODE is received for each mode character
	bool changeUserMode(char mode,bool bSet);
	// called on the first numerics received from server
	void loginComplete(const TQString &szNickName);
private slots:
	void hostNameLookupTerminated(KviDns * pDns);
protected:
	//
	// KviIrcContext interface
	//
	// this starts an asynchronous connection attempt
	// you must return control to the tqt core for the connection
	// operations to be initiated
	void start();
	// kills any running connection or attempt
	void abort();
	// called once in a while (every 5 secs at the moment)
	void heartbeat(kvi_time_t tNow);
protected:
	//
	// KviIrcLink interface (down)
	//
	// this signals that the connection attempt has failed
	void linkAttemptFailed(int iError);
	// called by KviIrcLink when the socket state changes to Connected
	void linkEstabilished();
	// called by KviIrcLink when the socket state changes to Ready
	void linkTerminated();
public:
	// this is called by KviIrcLink OR KviMexLinkFilter when a message is arriving from the server
	// needs to be public because subclasses of KviMexLinkFilter may call it.
	void incomingMessage(const char * message);
signals:
	void awayStateChanged();
	void userModeChanged();
	void nickNameChanged();
	void channelRegistered(KviChannel*);
	void channelUnregistered(KviChannel*);
	void chanListChanged();
public:
	TQString decodeText(const char * szText);
	KviTQCString encodeText(const TQString &szText);
private:
	void setupTextCodec();
};


// TODO: KviIdentity


#endif //!_KVI_IRCCONNECTION_H_