1 /***************************************************************************
2 * Copyright (C) 2005-2016 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 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
19 ***************************************************************************/
25 #include <QStringList>
27 #include <QNetworkProxy>
29 #include <QVariantMap>
36 #include "syncableobject.h"
38 #include "signalproxy.h"
40 #include "ircchannel.h"
45 // TODO: ConnectionInfo to propagate and sync the current state of NetworkConnection, encodings etcpp
47 class Network : public SyncableObject
51 Q_ENUMS(ConnectionState)
53 Q_PROPERTY(QString networkName READ networkName WRITE setNetworkName)
54 Q_PROPERTY(QString currentServer READ currentServer WRITE setCurrentServer)
55 Q_PROPERTY(QString myNick READ myNick WRITE setMyNick)
56 Q_PROPERTY(int latency READ latency WRITE setLatency)
57 Q_PROPERTY(QByteArray codecForServer READ codecForServer WRITE setCodecForServer)
58 Q_PROPERTY(QByteArray codecForEncoding READ codecForEncoding WRITE setCodecForEncoding)
59 Q_PROPERTY(QByteArray codecForDecoding READ codecForDecoding WRITE setCodecForDecoding)
60 Q_PROPERTY(IdentityId identityId READ identity WRITE setIdentity)
61 Q_PROPERTY(bool isConnected READ isConnected WRITE setConnected)
62 //Q_PROPERTY(Network::ConnectionState connectionState READ connectionState WRITE setConnectionState)
63 Q_PROPERTY(int connectionState READ connectionState WRITE setConnectionState)
64 Q_PROPERTY(bool useRandomServer READ useRandomServer WRITE setUseRandomServer)
65 Q_PROPERTY(QStringList perform READ perform WRITE setPerform)
66 Q_PROPERTY(bool useAutoIdentify READ useAutoIdentify WRITE setUseAutoIdentify)
67 Q_PROPERTY(QString autoIdentifyService READ autoIdentifyService WRITE setAutoIdentifyService)
68 Q_PROPERTY(QString autoIdentifyPassword READ autoIdentifyPassword WRITE setAutoIdentifyPassword)
69 Q_PROPERTY(bool useSasl READ useSasl WRITE setUseSasl)
70 Q_PROPERTY(QString saslAccount READ saslAccount WRITE setSaslAccount)
71 Q_PROPERTY(QString saslPassword READ saslPassword WRITE setSaslPassword)
72 Q_PROPERTY(bool useAutoReconnect READ useAutoReconnect WRITE setUseAutoReconnect)
73 Q_PROPERTY(quint32 autoReconnectInterval READ autoReconnectInterval WRITE setAutoReconnectInterval)
74 Q_PROPERTY(quint16 autoReconnectRetries READ autoReconnectRetries WRITE setAutoReconnectRetries)
75 Q_PROPERTY(bool unlimitedReconnectRetries READ unlimitedReconnectRetries WRITE setUnlimitedReconnectRetries)
76 Q_PROPERTY(bool rejoinChannels READ rejoinChannels WRITE setRejoinChannels)
79 enum ConnectionState {
89 // http://www.irc.org/tech_docs/005.html
90 // http://www.irc.org/tech_docs/draft-brocklesby-irc-isupport-03.txt
91 enum ChannelModeType {
92 NOT_A_CHANMODE = 0x00,
104 bool sslVerify; /// If true, validate SSL certificates
114 // sslVerify only applies when useSsl is true. sslVerify should be enabled by default,
115 // so enabling useSsl offers a more secure default.
116 Server() : port(6667), useSsl(false), sslVerify(true), sslVersion(0), useProxy(false),
117 proxyType(QNetworkProxy::Socks5Proxy), proxyHost("localhost"), proxyPort(8080) {}
119 Server(const QString &host, uint port, const QString &password, bool useSsl,
121 : host(host), port(port), password(password), useSsl(useSsl), sslVerify(sslVerify),
122 sslVersion(0), useProxy(false), proxyType(QNetworkProxy::Socks5Proxy),
123 proxyHost("localhost"), proxyPort(8080) {}
125 bool operator==(const Server &other) const;
126 bool operator!=(const Server &other) const;
128 typedef QList<Server> ServerList;
130 Network(const NetworkId &networkid, QObject *parent = 0);
133 inline NetworkId networkId() const { return _networkId; }
135 inline SignalProxy *proxy() const { return _proxy; }
136 inline void setProxy(SignalProxy *proxy) { _proxy = proxy; }
138 inline bool isMyNick(const QString &nick) const { return (myNick().toLower() == nick.toLower()); }
139 inline bool isMe(IrcUser *ircuser) const { return (ircuser->nick().toLower() == myNick().toLower()); }
141 bool isChannelName(const QString &channelname) const;
144 * Checks if the target counts as a STATUSMSG
146 * Status messages are prefixed with one or more characters from the server-provided STATUSMSG
147 * if available, otherwise "@" and "+" are assumed. Generally, status messages sent to a
148 * channel are only visible to those with the same or higher permissions, e.g. voiced.
150 * @param[in] target Name of destination, e.g. a channel or query
151 * @returns True if a STATUSMSG, otherwise false
153 bool isStatusMsg(const QString &target) const;
155 inline bool isConnected() const { return _connected; }
156 //Network::ConnectionState connectionState() const;
157 inline int connectionState() const { return _connectionState; }
159 QString prefixToMode(const QString &prefix) const;
160 inline QString prefixToMode(const QCharRef &prefix) const { return prefixToMode(QString(prefix)); }
161 QString modeToPrefix(const QString &mode) const;
162 inline QString modeToPrefix(const QCharRef &mode) const { return modeToPrefix(QString(mode)); }
164 ChannelModeType channelModeType(const QString &mode);
165 inline ChannelModeType channelModeType(const QCharRef &mode) { return channelModeType(QString(mode)); }
167 inline const QString &networkName() const { return _networkName; }
168 inline const QString ¤tServer() const { return _currentServer; }
169 inline const QString &myNick() const { return _myNick; }
170 inline int latency() const { return _latency; }
171 inline IrcUser *me() const { return ircUser(myNick()); }
172 inline IdentityId identity() const { return _identity; }
173 QStringList nicks() const;
174 inline QStringList channels() const { return _ircChannels.keys(); }
176 * Gets the list of available capabilities.
178 * @returns QStringList of available capabilities
180 inline const QStringList caps() const { return QStringList(_caps.keys()); }
182 * Gets the list of enabled (acknowledged) capabilities.
184 * @returns QStringList of enabled (acknowledged) capabilities
186 inline const QStringList capsEnabled() const { return _capsEnabled; }
187 inline const ServerList &serverList() const { return _serverList; }
188 inline bool useRandomServer() const { return _useRandomServer; }
189 inline const QStringList &perform() const { return _perform; }
190 inline bool useAutoIdentify() const { return _useAutoIdentify; }
191 inline const QString &autoIdentifyService() const { return _autoIdentifyService; }
192 inline const QString &autoIdentifyPassword() const { return _autoIdentifyPassword; }
193 inline bool useSasl() const { return _useSasl; }
194 inline const QString &saslAccount() const { return _saslAccount; }
195 inline const QString &saslPassword() const { return _saslPassword; }
196 inline bool useAutoReconnect() const { return _useAutoReconnect; }
197 inline quint32 autoReconnectInterval() const { return _autoReconnectInterval; }
198 inline quint16 autoReconnectRetries() const { return _autoReconnectRetries; }
199 inline bool unlimitedReconnectRetries() const { return _unlimitedReconnectRetries; }
200 inline bool rejoinChannels() const { return _rejoinChannels; }
202 NetworkInfo networkInfo() const;
203 void setNetworkInfo(const NetworkInfo &);
205 QString prefixes() const;
206 QString prefixModes() const;
207 void determinePrefixes() const;
209 bool supports(const QString ¶m) const { return _supports.contains(param); }
210 QString support(const QString ¶m) const;
213 * Checks if a given capability is acknowledged and active.
215 * @param[in] capability Name of capability
216 * @returns True if acknowledged (active), otherwise false
218 inline bool capEnabled(const QString &capability) const { return _capsEnabled.contains(capability.toLower()); }
219 // IRCv3 specs all use lowercase capability names
222 * Gets the value of an available capability, e.g. for SASL, "EXTERNAL,PLAIN".
224 * @param[in] capability Name of capability
225 * @returns Value of capability if one was specified, otherwise empty string
227 QString capValue(const QString &capability) const { return _caps.value(capability.toLower()); }
228 // IRCv3 specs all use lowercase capability names
229 // QHash returns the default constructed value if not found, in this case, empty string
230 // See: https://doc.qt.io/qt-4.8/qhash.html#value
232 IrcUser *newIrcUser(const QString &hostmask, const QVariantMap &initData = QVariantMap());
233 inline IrcUser *newIrcUser(const QByteArray &hostmask) { return newIrcUser(decodeServerString(hostmask)); }
234 IrcUser *ircUser(QString nickname) const;
235 inline IrcUser *ircUser(const QByteArray &nickname) const { return ircUser(decodeServerString(nickname)); }
236 inline QList<IrcUser *> ircUsers() const { return _ircUsers.values(); }
237 inline quint32 ircUserCount() const { return _ircUsers.count(); }
239 IrcChannel *newIrcChannel(const QString &channelname, const QVariantMap &initData = QVariantMap());
240 inline IrcChannel *newIrcChannel(const QByteArray &channelname) { return newIrcChannel(decodeServerString(channelname)); }
241 IrcChannel *ircChannel(QString channelname) const;
242 inline IrcChannel *ircChannel(const QByteArray &channelname) const { return ircChannel(decodeServerString(channelname)); }
243 inline QList<IrcChannel *> ircChannels() const { return _ircChannels.values(); }
244 inline quint32 ircChannelCount() const { return _ircChannels.count(); }
246 QByteArray codecForServer() const;
247 QByteArray codecForEncoding() const;
248 QByteArray codecForDecoding() const;
249 void setCodecForServer(QTextCodec *codec);
250 void setCodecForEncoding(QTextCodec *codec);
251 void setCodecForDecoding(QTextCodec *codec);
253 QString decodeString(const QByteArray &text) const;
254 QByteArray encodeString(const QString &string) const;
255 QString decodeServerString(const QByteArray &text) const;
256 QByteArray encodeServerString(const QString &string) const;
258 static QByteArray defaultCodecForServer();
259 static QByteArray defaultCodecForEncoding();
260 static QByteArray defaultCodecForDecoding();
261 static void setDefaultCodecForServer(const QByteArray &name);
262 static void setDefaultCodecForEncoding(const QByteArray &name);
263 static void setDefaultCodecForDecoding(const QByteArray &name);
265 inline bool autoAwayActive() const { return _autoAwayActive; }
266 inline void setAutoAwayActive(bool active) { _autoAwayActive = active; }
269 void setNetworkName(const QString &networkName);
270 void setCurrentServer(const QString ¤tServer);
271 void setConnected(bool isConnected);
272 void setConnectionState(int state);
273 virtual void setMyNick(const QString &mynick);
274 void setLatency(int latency);
275 void setIdentity(IdentityId);
277 void setServerList(const QVariantList &serverList);
278 void setUseRandomServer(bool);
279 void setPerform(const QStringList &);
280 void setUseAutoIdentify(bool);
281 void setAutoIdentifyService(const QString &);
282 void setAutoIdentifyPassword(const QString &);
283 void setUseSasl(bool);
284 void setSaslAccount(const QString &);
285 void setSaslPassword(const QString &);
286 virtual void setUseAutoReconnect(bool);
287 virtual void setAutoReconnectInterval(quint32);
288 virtual void setAutoReconnectRetries(quint16);
289 void setUnlimitedReconnectRetries(bool);
290 void setRejoinChannels(bool);
292 void setCodecForServer(const QByteArray &codecName);
293 void setCodecForEncoding(const QByteArray &codecName);
294 void setCodecForDecoding(const QByteArray &codecName);
296 void addSupport(const QString ¶m, const QString &value = QString());
297 void removeSupport(const QString ¶m);
299 // IRCv3 capability negotiation (can be connected to signals)
302 * Add an available capability, optionally providing a value.
304 * This may happen during first connect, or at any time later if a new capability becomes
305 * available (e.g. SASL service starting).
307 * @param[in] capability Name of the capability
310 * Optional value of the capability, e.g. sasl=plain.
313 void addCap(const QString &capability, const QString &value = QString());
316 * Marks a capability as acknowledged (enabled by the IRC server).
318 * @param[in] capability Name of the capability
320 void acknowledgeCap(const QString &capability);
323 * Removes a capability from the list of available capabilities.
325 * This may happen during first connect, or at any time later if an existing capability becomes
326 * unavailable (e.g. SASL service stopping). This also removes the capability from the list
327 * of acknowledged capabilities.
329 * @param[in] capability Name of the capability
331 void removeCap(const QString &capability);
334 * Clears all capabilities from the list of available capabilities.
336 * This also removes the capability from the list of acknowledged capabilities.
340 inline void addIrcUser(const QString &hostmask) { newIrcUser(hostmask); }
341 inline void addIrcChannel(const QString &channel) { newIrcChannel(channel); }
344 QVariantMap initSupports() const;
346 * Get the initial list of available capabilities.
348 * @return QVariantMap of <QString, QString> indicating available capabilities and values
350 QVariantMap initCaps() const;
352 * Get the initial list of enabled (acknowledged) capabilities.
354 * @return QVariantList of QString indicating enabled (acknowledged) capabilities and values
356 QVariantList initCapsEnabled() const { return toVariantList(capsEnabled()); }
357 inline QVariantList initServerList() const { return toVariantList(serverList()); }
358 virtual QVariantMap initIrcUsersAndChannels() const;
361 void initSetSupports(const QVariantMap &supports);
363 * Initialize the list of available capabilities.
365 * @param[in] caps QVariantMap of <QString, QString> indicating available capabilities and values
367 void initSetCaps(const QVariantMap &caps);
369 * Initialize the list of enabled (acknowledged) capabilities.
371 * @param[in] caps QVariantList of QString indicating enabled (acknowledged) capabilities and values
373 inline void initSetCapsEnabled(const QVariantList &capsEnabled) { _capsEnabled = fromVariantList<QString>(capsEnabled); }
374 inline void initSetServerList(const QVariantList &serverList) { _serverList = fromVariantList<Server>(serverList); }
375 virtual void initSetIrcUsersAndChannels(const QVariantMap &usersAndChannels);
378 * Update IrcUser hostmask and username from mask, creating an IrcUser if one does not exist.
380 * @param[in] mask Full nick!user@hostmask string
381 * @return IrcUser of the matching nick if exists, otherwise a new IrcUser
383 IrcUser *updateNickFromMask(const QString &mask);
385 // these slots are to keep the hashlists of all users and the
386 // channel lists up to date
387 void ircUserNickChanged(QString newnick);
389 virtual inline void requestConnect() const { REQUEST(NO_ARG) }
390 virtual inline void requestDisconnect() const { REQUEST(NO_ARG) }
391 virtual inline void requestSetNetworkInfo(const NetworkInfo &info) { REQUEST(ARG(info)) }
393 void emitConnectionError(const QString &);
396 virtual void removeIrcUser(IrcUser *ircuser);
397 virtual void removeIrcChannel(IrcChannel *ircChannel);
398 virtual void removeChansAndUsers();
401 void aboutToBeDestroyed();
402 void networkNameSet(const QString &networkName);
403 void currentServerSet(const QString ¤tServer);
404 void connectedSet(bool isConnected);
405 void connectionStateSet(Network::ConnectionState);
406 // void connectionStateSet(int);
407 void connectionError(const QString &errorMsg);
408 void myNickSet(const QString &mynick);
409 // void latencySet(int latency);
410 void identitySet(IdentityId);
412 void configChanged();
414 // void serverListSet(QVariantList serverList);
415 // void useRandomServerSet(bool);
416 // void performSet(const QStringList &);
417 // void useAutoIdentifySet(bool);
418 // void autoIdentifyServiceSet(const QString &);
419 // void autoIdentifyPasswordSet(const QString &);
420 // void useAutoReconnectSet(bool);
421 // void autoReconnectIntervalSet(quint32);
422 // void autoReconnectRetriesSet(quint16);
423 // void unlimitedReconnectRetriesSet(bool);
424 // void rejoinChannelsSet(bool);
426 // void codecForServerSet(const QByteArray &codecName);
427 // void codecForEncodingSet(const QByteArray &codecName);
428 // void codecForDecodingSet(const QByteArray &codecName);
430 // void supportAdded(const QString ¶m, const QString &value);
431 // void supportRemoved(const QString ¶m);
433 // IRCv3 capability negotiation (can drive other slots)
435 * Indicates a capability is now available, with optional value in Network::capValue().
437 * @see Network::addCap()
439 * @param[in] capability Name of the capability
441 void capAdded (const QString &capability);
444 * Indicates a capability was acknowledged (enabled by the IRC server).
446 * @see Network::acknowledgeCap()
448 * @param[in] capability Name of the capability
450 void capAcknowledged(const QString &capability);
453 * Indicates a capability was removed from the list of available capabilities.
455 * @see Network::removeCap()
457 * @param[in] capability Name of the capability
459 void capRemoved(const QString &capability);
461 // void ircUserAdded(const QString &hostmask);
462 void ircUserAdded(IrcUser *);
463 // void ircChannelAdded(const QString &channelname);
464 void ircChannelAdded(IrcChannel *);
466 // void connectRequested() const;
467 // void disconnectRequested() const;
468 // void setNetworkInfoRequested(const NetworkInfo &) const;
471 inline virtual IrcChannel *ircChannelFactory(const QString &channelname) { return new IrcChannel(channelname, this); }
472 inline virtual IrcUser *ircUserFactory(const QString &hostmask) { return new IrcUser(hostmask, this); }
475 QPointer<SignalProxy> _proxy;
477 NetworkId _networkId;
478 IdentityId _identity;
482 QString _networkName;
483 QString _currentServer;
485 ConnectionState _connectionState;
487 mutable QString _prefixes;
488 mutable QString _prefixModes;
490 QHash<QString, IrcUser *> _ircUsers; // stores all known nicks for the server
491 QHash<QString, IrcChannel *> _ircChannels; // stores all known channels
492 QHash<QString, QString> _supports; // stores results from RPL_ISUPPORT
494 QHash<QString, QString> _caps; /// Capabilities supported by the IRC server
495 // By synchronizing the supported capabilities, the client could suggest certain behaviors, e.g.
496 // in the Network settings dialog, recommending SASL instead of using NickServ, or warning if
497 // SASL EXTERNAL isn't available.
498 QStringList _capsEnabled; /// Enabled capabilities that received 'CAP ACK'
499 // _capsEnabled uses the same values from the <name>=<value> pairs stored in _caps
501 ServerList _serverList;
502 bool _useRandomServer;
503 QStringList _perform;
505 bool _useAutoIdentify;
506 QString _autoIdentifyService;
507 QString _autoIdentifyPassword;
510 QString _saslAccount;
511 QString _saslPassword;
513 bool _useAutoReconnect;
514 quint32 _autoReconnectInterval;
515 quint16 _autoReconnectRetries;
516 bool _unlimitedReconnectRetries;
517 bool _rejoinChannels;
519 QTextCodec *_codecForServer;
520 QTextCodec *_codecForEncoding;
521 QTextCodec *_codecForDecoding;
523 static QTextCodec *_defaultCodecForServer;
524 static QTextCodec *_defaultCodecForEncoding;
525 static QTextCodec *_defaultCodecForDecoding;
527 bool _autoAwayActive; // when this is active handle305 and handle306 don't trigger any output
529 friend class IrcUser;
530 friend class IrcChannel;
534 //! Stores all editable information about a network (as opposed to runtime state).
536 // set some default values, note that this does not initialize e.g. name and id
543 bool useCustomEncodings; // not used!
544 QByteArray codecForServer;
545 QByteArray codecForEncoding;
546 QByteArray codecForDecoding;
548 Network::ServerList serverList;
549 bool useRandomServer;
553 bool useAutoIdentify;
554 QString autoIdentifyService;
555 QString autoIdentifyPassword;
559 QString saslPassword;
561 bool useAutoReconnect;
562 quint32 autoReconnectInterval;
563 quint16 autoReconnectRetries;
564 bool unlimitedReconnectRetries;
567 bool operator==(const NetworkInfo &other) const;
568 bool operator!=(const NetworkInfo &other) const;
571 QDataStream &operator<<(QDataStream &out, const NetworkInfo &info);
572 QDataStream &operator>>(QDataStream &in, NetworkInfo &info);
573 QDebug operator<<(QDebug dbg, const NetworkInfo &i);
574 Q_DECLARE_METATYPE(NetworkInfo)
576 QDataStream &operator<<(QDataStream &out, const Network::Server &server);
577 QDataStream &operator>>(QDataStream &in, Network::Server &server);
578 QDebug operator<<(QDebug dbg, const Network::Server &server);
579 Q_DECLARE_METATYPE(Network::Server)