1 /***************************************************************************
2 * Copyright (C) 2005-09 by the Quassel Project *
3 * devel@quassel-irc.org *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) version 3. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
25 #include <QStringList>
27 #include <QNetworkProxy>
29 #include <QVariantMap>
35 #include "syncableobject.h"
37 #include "signalproxy.h"
39 #include "ircchannel.h"
44 // TODO: ConnectionInfo to propagate and sync the current state of NetworkConnection, encodings etcpp
46 class Network : public SyncableObject {
49 Q_ENUMS(ConnectionState Network::ConnectionState)
51 Q_PROPERTY(QString networkName READ networkName WRITE setNetworkName STORED false)
52 Q_PROPERTY(QString currentServer READ currentServer WRITE setCurrentServer STORED false)
53 Q_PROPERTY(QString myNick READ myNick WRITE setMyNick STORED false)
54 Q_PROPERTY(int latency READ latency WRITE setLatency STORED false)
55 Q_PROPERTY(QByteArray codecForServer READ codecForServer WRITE setCodecForServer STORED false)
56 Q_PROPERTY(QByteArray codecForEncoding READ codecForEncoding WRITE setCodecForEncoding STORED false)
57 Q_PROPERTY(QByteArray codecForDecoding READ codecForDecoding WRITE setCodecForDecoding STORED false)
58 Q_PROPERTY(IdentityId identityId READ identity WRITE setIdentity STORED false)
59 Q_PROPERTY(bool isConnected READ isConnected WRITE setConnected STORED false)
60 //Q_PROPERTY(Network::ConnectionState connectionState READ connectionState WRITE setConnectionState STORED false)
61 Q_PROPERTY(int connectionState READ connectionState WRITE setConnectionState STORED false)
62 Q_PROPERTY(bool useRandomServer READ useRandomServer WRITE setUseRandomServer STORED false)
63 Q_PROPERTY(QStringList perform READ perform WRITE setPerform STORED false)
64 Q_PROPERTY(bool useAutoIdentify READ useAutoIdentify WRITE setUseAutoIdentify STORED false)
65 Q_PROPERTY(QString autoIdentifyService READ autoIdentifyService WRITE setAutoIdentifyService STORED false)
66 Q_PROPERTY(QString autoIdentifyPassword READ autoIdentifyPassword WRITE setAutoIdentifyPassword STORED false)
67 Q_PROPERTY(bool useAutoReconnect READ useAutoReconnect WRITE setUseAutoReconnect STORED false)
68 Q_PROPERTY(quint32 autoReconnectInterval READ autoReconnectInterval WRITE setAutoReconnectInterval STORED false)
69 Q_PROPERTY(quint16 autoReconnectRetries READ autoReconnectRetries WRITE setAutoReconnectRetries STORED false)
70 Q_PROPERTY(bool unlimitedReconnectRetries READ unlimitedReconnectRetries WRITE setUnlimitedReconnectRetries STORED false)
71 Q_PROPERTY(bool rejoinChannels READ rejoinChannels WRITE setRejoinChannels STORED false)
74 enum ConnectionState {
84 // http://www.irc.org/tech_docs/005.html
85 // http://www.irc.org/tech_docs/draft-brocklesby-irc-isupport-03.txt
86 enum ChannelModeType {
87 NOT_A_CHANMODE = 0x00,
108 Server() : port(6667), useSsl(false), sslVersion(0), useProxy(false), proxyType(QNetworkProxy::Socks5Proxy), proxyHost("localhost"), proxyPort(8080) {}
109 Server(const QString &host, uint port, const QString &password, bool useSsl)
110 : host(host), port(port), password(password), useSsl(useSsl), sslVersion(0),
111 useProxy(false), proxyType(QNetworkProxy::Socks5Proxy), proxyHost("localhost"), proxyPort(8080) {}
112 bool operator==(const Server &other) const;
113 bool operator!=(const Server &other) const;
115 typedef QList<Server> ServerList;
117 Network(const NetworkId &networkid, QObject *parent = 0);
120 inline NetworkId networkId() const { return _networkId; }
122 inline SignalProxy *proxy() const { return _proxy; }
123 inline void setProxy(SignalProxy *proxy) { _proxy = proxy; }
125 inline bool isMyNick(const QString &nick) const { return (myNick().toLower() == nick.toLower()); }
126 inline bool isMe(IrcUser *ircuser) const { return (ircuser->nick().toLower() == myNick().toLower()); }
128 bool isChannelName(const QString &channelname) const;
130 inline bool isConnected() const { return _connected; }
131 //Network::ConnectionState connectionState() const;
132 inline int connectionState() const { return _connectionState; }
134 QString prefixToMode(const QString &prefix);
135 inline QString prefixToMode(const QCharRef &prefix) { return prefixToMode(QString(prefix)); }
136 QString modeToPrefix(const QString &mode);
137 inline QString modeToPrefix(const QCharRef &mode) { return modeToPrefix(QString(mode)); }
139 ChannelModeType channelModeType(const QString &mode);
140 inline ChannelModeType channelModeType(const QCharRef &mode) { return channelModeType(QString(mode)); }
142 inline const QString &networkName() const { return _networkName; }
143 inline const QString ¤tServer() const { return _currentServer; }
144 inline const QString &myNick() const { return _myNick; }
145 inline int latency() const { return _latency; }
146 inline IrcUser *me() const { return ircUser(myNick()); }
147 inline IdentityId identity() const { return _identity; }
148 QStringList nicks() const;
149 inline QStringList channels() const { return _ircChannels.keys(); }
150 inline const ServerList &serverList() const { return _serverList; }
151 inline bool useRandomServer() const { return _useRandomServer; }
152 inline const QStringList &perform() const { return _perform; }
153 inline bool useAutoIdentify() const { return _useAutoIdentify; }
154 inline const QString &autoIdentifyService() const { return _autoIdentifyService; }
155 inline const QString &autoIdentifyPassword() const { return _autoIdentifyPassword; }
156 inline bool useAutoReconnect() const { return _useAutoReconnect; }
157 inline quint32 autoReconnectInterval() const { return _autoReconnectInterval; }
158 inline quint16 autoReconnectRetries() const { return _autoReconnectRetries; }
159 inline bool unlimitedReconnectRetries() const { return _unlimitedReconnectRetries; }
160 inline bool rejoinChannels() const { return _rejoinChannels; }
162 NetworkInfo networkInfo() const;
163 void setNetworkInfo(const NetworkInfo &);
166 QString prefixModes();
168 bool supports(const QString ¶m) const { return _supports.contains(param); }
169 QString support(const QString ¶m) const;
171 IrcUser *newIrcUser(const QString &hostmask, const QVariantMap &initData = QVariantMap());
172 inline IrcUser *newIrcUser(const QByteArray &hostmask) { return newIrcUser(decodeServerString(hostmask)); }
173 IrcUser *ircUser(QString nickname) const;
174 inline IrcUser *ircUser(const QByteArray &nickname) const { return ircUser(decodeServerString(nickname)); }
175 inline QList<IrcUser *> ircUsers() const { return _ircUsers.values(); }
176 inline quint32 ircUserCount() const { return _ircUsers.count(); }
178 IrcChannel *newIrcChannel(const QString &channelname, const QVariantMap &initData = QVariantMap());
179 inline IrcChannel *newIrcChannel(const QByteArray &channelname) { return newIrcChannel(decodeServerString(channelname)); }
180 IrcChannel *ircChannel(QString channelname) const;
181 inline IrcChannel *ircChannel(const QByteArray &channelname) const { return ircChannel(decodeServerString(channelname)); }
182 inline QList<IrcChannel *> ircChannels() const { return _ircChannels.values(); }
183 inline quint32 ircChannelCount() const { return _ircChannels.count(); }
185 QByteArray codecForServer() const;
186 QByteArray codecForEncoding() const;
187 QByteArray codecForDecoding() const;
188 void setCodecForServer(QTextCodec *codec);
189 void setCodecForEncoding(QTextCodec *codec);
190 void setCodecForDecoding(QTextCodec *codec);
192 QString decodeString(const QByteArray &text) const;
193 QByteArray encodeString(const QString &string) const;
194 QString decodeServerString(const QByteArray &text) const;
195 QByteArray encodeServerString(const QString &string) const;
197 static QByteArray defaultCodecForServer();
198 static QByteArray defaultCodecForEncoding();
199 static QByteArray defaultCodecForDecoding();
200 static void setDefaultCodecForServer(const QByteArray &name);
201 static void setDefaultCodecForEncoding(const QByteArray &name);
202 static void setDefaultCodecForDecoding(const QByteArray &name);
204 inline bool autoAwayActive() const { return _autoAwayActive; }
205 inline void setAutoAwayActive(bool active) { _autoAwayActive = active; }
207 static QStringList presetNetworks(bool onlyDefault = false);
208 static QStringList presetDefaultChannels(const QString &networkName);
209 static NetworkInfo networkInfoFromPreset(const QString &networkName);
212 void setNetworkName(const QString &networkName);
213 void setCurrentServer(const QString ¤tServer);
214 void setConnected(bool isConnected);
215 void setConnectionState(int state);
216 virtual void setMyNick(const QString &mynick);
217 void setLatency(int latency);
218 void setIdentity(IdentityId);
220 void setServerList(const QVariantList &serverList);
221 void setUseRandomServer(bool);
222 void setPerform(const QStringList &);
223 void setUseAutoIdentify(bool);
224 void setAutoIdentifyService(const QString &);
225 void setAutoIdentifyPassword(const QString &);
226 virtual void setUseAutoReconnect(bool);
227 virtual void setAutoReconnectInterval(quint32);
228 virtual void setAutoReconnectRetries(quint16);
229 void setUnlimitedReconnectRetries(bool);
230 void setRejoinChannels(bool);
232 void setCodecForServer(const QByteArray &codecName);
233 void setCodecForEncoding(const QByteArray &codecName);
234 void setCodecForDecoding(const QByteArray &codecName);
236 void addSupport(const QString ¶m, const QString &value = QString());
237 void removeSupport(const QString ¶m);
239 inline void addIrcUser(const QString &hostmask) { newIrcUser(hostmask); }
240 inline void addIrcChannel(const QString &channel) { newIrcChannel(channel); }
243 QVariantMap initSupports() const;
244 inline QVariantList initServerList() const { return toVariantList(serverList()); }
245 virtual QVariantMap initIrcUsersAndChannels() const;
248 void initSetSupports(const QVariantMap &supports);
249 inline void initSetServerList(const QVariantList &serverList) { _serverList = fromVariantList<Server>(serverList); }
250 virtual void initSetIrcUsersAndChannels(const QVariantMap &usersAndChannels);
252 IrcUser *updateNickFromMask(const QString &mask);
254 // these slots are to keep the hashlists of all users and the
255 // channel lists up to date
256 void ircUserNickChanged(QString newnick);
258 virtual inline void requestConnect() const { REQUEST(NO_ARG) }
259 virtual inline void requestDisconnect() const { REQUEST(NO_ARG) }
260 virtual inline void requestSetNetworkInfo(const NetworkInfo &info) { REQUEST(ARG(info)) }
262 void emitConnectionError(const QString &);
265 void removeIrcUser(IrcUser *ircuser);
266 void removeIrcChannel(IrcChannel *ircChannel);
267 void removeChansAndUsers();
270 void aboutToBeDestroyed();
271 void networkNameSet(const QString &networkName);
272 void currentServerSet(const QString ¤tServer);
273 void connectedSet(bool isConnected);
274 void connectionStateSet(Network::ConnectionState);
275 // void connectionStateSet(int);
276 void connectionError(const QString &errorMsg);
277 void myNickSet(const QString &mynick);
278 // void latencySet(int latency);
279 void identitySet(IdentityId);
281 void configChanged();
283 // void serverListSet(QVariantList serverList);
284 // void useRandomServerSet(bool);
285 // void performSet(const QStringList &);
286 // void useAutoIdentifySet(bool);
287 // void autoIdentifyServiceSet(const QString &);
288 // void autoIdentifyPasswordSet(const QString &);
289 // void useAutoReconnectSet(bool);
290 // void autoReconnectIntervalSet(quint32);
291 // void autoReconnectRetriesSet(quint16);
292 // void unlimitedReconnectRetriesSet(bool);
293 // void rejoinChannelsSet(bool);
295 // void codecForServerSet(const QByteArray &codecName);
296 // void codecForEncodingSet(const QByteArray &codecName);
297 // void codecForDecodingSet(const QByteArray &codecName);
299 // void supportAdded(const QString ¶m, const QString &value);
300 // void supportRemoved(const QString ¶m);
302 // void ircUserAdded(const QString &hostmask);
303 void ircUserAdded(IrcUser *);
304 // void ircChannelAdded(const QString &channelname);
305 void ircChannelAdded(IrcChannel *);
307 // void connectRequested() const;
308 // void disconnectRequested() const;
309 // void setNetworkInfoRequested(const NetworkInfo &) const;
312 inline virtual IrcChannel *ircChannelFactory(const QString &channelname) { return new IrcChannel(channelname, this); }
315 QPointer<SignalProxy> _proxy;
317 NetworkId _networkId;
318 IdentityId _identity;
322 QString _networkName;
323 QString _currentServer;
325 ConnectionState _connectionState;
328 QString _prefixModes;
330 QHash<QString, IrcUser *> _ircUsers; // stores all known nicks for the server
331 QHash<QString, IrcChannel *> _ircChannels; // stores all known channels
332 QHash<QString, QString> _supports; // stores results from RPL_ISUPPORT
334 ServerList _serverList;
335 bool _useRandomServer;
336 QStringList _perform;
338 bool _useAutoIdentify;
339 QString _autoIdentifyService;
340 QString _autoIdentifyPassword;
342 bool _useAutoReconnect;
343 quint32 _autoReconnectInterval;
344 quint16 _autoReconnectRetries;
345 bool _unlimitedReconnectRetries;
346 bool _rejoinChannels;
348 void determinePrefixes();
350 QTextCodec *_codecForServer;
351 QTextCodec *_codecForEncoding;
352 QTextCodec *_codecForDecoding;
354 static QTextCodec *_defaultCodecForServer;
355 static QTextCodec *_defaultCodecForEncoding;
356 static QTextCodec *_defaultCodecForDecoding;
358 bool _autoAwayActive; // when this is active handle305 and handle306 don't trigger any output
360 static QString _networksIniPath;
362 friend class IrcUser;
363 friend class IrcChannel;
366 //! Stores all editable information about a network (as opposed to runtime state).
368 // set some default values, note that this does not initialize e.g. name and id
375 bool useCustomEncodings; // not used!
376 QByteArray codecForServer;
377 QByteArray codecForEncoding;
378 QByteArray codecForDecoding;
380 Network::ServerList serverList;
381 bool useRandomServer;
385 bool useAutoIdentify;
386 QString autoIdentifyService;
387 QString autoIdentifyPassword;
389 bool useAutoReconnect;
390 quint32 autoReconnectInterval;
391 quint16 autoReconnectRetries;
392 bool unlimitedReconnectRetries;
395 bool operator==(const NetworkInfo &other) const;
396 bool operator!=(const NetworkInfo &other) const;
399 QDataStream &operator<<(QDataStream &out, const NetworkInfo &info);
400 QDataStream &operator>>(QDataStream &in, NetworkInfo &info);
401 QDebug operator<<(QDebug dbg, const NetworkInfo &i);
402 Q_DECLARE_METATYPE(NetworkInfo)
404 QDataStream &operator<<(QDataStream &out, const Network::Server &server);
405 QDataStream &operator>>(QDataStream &in, Network::Server &server);
406 QDebug operator<<(QDebug dbg, const Network::Server &server);
407 Q_DECLARE_METATYPE(Network::Server)