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,
99 // Default port assignments according to what many IRC networks have settled on.
100 // Technically not a standard, but it's fairly widespread.
101 // See https://freenode.net/news/port-6697-irc-via-tlsssl
103 PORT_PLAINTEXT = 6667, /// Default port for unencrypted connections
104 PORT_SSL = 6697 /// Default port for encrypted connections
112 bool sslVerify; /// If true, validate SSL certificates
122 // sslVerify only applies when useSsl is true. sslVerify should be enabled by default,
123 // so enabling useSsl offers a more secure default.
124 Server() : port(6667), useSsl(false), sslVerify(true), sslVersion(0), useProxy(false),
125 proxyType(QNetworkProxy::Socks5Proxy), proxyHost("localhost"), proxyPort(8080) {}
127 Server(const QString &host, uint port, const QString &password, bool useSsl,
129 : host(host), port(port), password(password), useSsl(useSsl), sslVerify(sslVerify),
130 sslVersion(0), useProxy(false), proxyType(QNetworkProxy::Socks5Proxy),
131 proxyHost("localhost"), proxyPort(8080) {}
133 bool operator==(const Server &other) const;
134 bool operator!=(const Server &other) const;
136 typedef QList<Server> ServerList;
138 Network(const NetworkId &networkid, QObject *parent = 0);
141 inline NetworkId networkId() const { return _networkId; }
143 inline SignalProxy *proxy() const { return _proxy; }
144 inline void setProxy(SignalProxy *proxy) { _proxy = proxy; }
146 inline bool isMyNick(const QString &nick) const { return (myNick().toLower() == nick.toLower()); }
147 inline bool isMe(IrcUser *ircuser) const { return (ircuser->nick().toLower() == myNick().toLower()); }
149 bool isChannelName(const QString &channelname) const;
152 * Checks if the target counts as a STATUSMSG
154 * Status messages are prefixed with one or more characters from the server-provided STATUSMSG
155 * if available, otherwise "@" and "+" are assumed. Generally, status messages sent to a
156 * channel are only visible to those with the same or higher permissions, e.g. voiced.
158 * @param[in] target Name of destination, e.g. a channel or query
159 * @returns True if a STATUSMSG, otherwise false
161 bool isStatusMsg(const QString &target) const;
163 inline bool isConnected() const { return _connected; }
164 //Network::ConnectionState connectionState() const;
165 inline int connectionState() const { return _connectionState; }
167 QString prefixToMode(const QString &prefix) const;
168 inline QString prefixToMode(const QCharRef &prefix) const { return prefixToMode(QString(prefix)); }
169 QString modeToPrefix(const QString &mode) const;
170 inline QString modeToPrefix(const QCharRef &mode) const { return modeToPrefix(QString(mode)); }
172 ChannelModeType channelModeType(const QString &mode);
173 inline ChannelModeType channelModeType(const QCharRef &mode) { return channelModeType(QString(mode)); }
175 inline const QString &networkName() const { return _networkName; }
176 inline const QString ¤tServer() const { return _currentServer; }
177 inline const QString &myNick() const { return _myNick; }
178 inline int latency() const { return _latency; }
179 inline IrcUser *me() const { return ircUser(myNick()); }
180 inline IdentityId identity() const { return _identity; }
181 QStringList nicks() const;
182 inline QStringList channels() const { return _ircChannels.keys(); }
184 * Gets the list of available capabilities.
186 * @returns QStringList of available capabilities
188 inline const QStringList caps() const { return QStringList(_caps.keys()); }
190 * Gets the list of enabled (acknowledged) capabilities.
192 * @returns QStringList of enabled (acknowledged) capabilities
194 inline const QStringList capsEnabled() const { return _capsEnabled; }
195 inline const ServerList &serverList() const { return _serverList; }
196 inline bool useRandomServer() const { return _useRandomServer; }
197 inline const QStringList &perform() const { return _perform; }
198 inline bool useAutoIdentify() const { return _useAutoIdentify; }
199 inline const QString &autoIdentifyService() const { return _autoIdentifyService; }
200 inline const QString &autoIdentifyPassword() const { return _autoIdentifyPassword; }
201 inline bool useSasl() const { return _useSasl; }
202 inline const QString &saslAccount() const { return _saslAccount; }
203 inline const QString &saslPassword() const { return _saslPassword; }
204 inline bool useAutoReconnect() const { return _useAutoReconnect; }
205 inline quint32 autoReconnectInterval() const { return _autoReconnectInterval; }
206 inline quint16 autoReconnectRetries() const { return _autoReconnectRetries; }
207 inline bool unlimitedReconnectRetries() const { return _unlimitedReconnectRetries; }
208 inline bool rejoinChannels() const { return _rejoinChannels; }
210 NetworkInfo networkInfo() const;
211 void setNetworkInfo(const NetworkInfo &);
213 QString prefixes() const;
214 QString prefixModes() const;
215 void determinePrefixes() const;
217 bool supports(const QString ¶m) const { return _supports.contains(param); }
218 QString support(const QString ¶m) const;
221 * Checks if a given capability is acknowledged and active.
223 * @param[in] capability Name of capability
224 * @returns True if acknowledged (active), otherwise false
226 inline bool capEnabled(const QString &capability) const { return _capsEnabled.contains(capability.toLower()); }
227 // IRCv3 specs all use lowercase capability names
230 * Gets the value of an available capability, e.g. for SASL, "EXTERNAL,PLAIN".
232 * @param[in] capability Name of capability
233 * @returns Value of capability if one was specified, otherwise empty string
235 QString capValue(const QString &capability) const { return _caps.value(capability.toLower()); }
236 // IRCv3 specs all use lowercase capability names
237 // QHash returns the default constructed value if not found, in this case, empty string
238 // See: https://doc.qt.io/qt-4.8/qhash.html#value
240 IrcUser *newIrcUser(const QString &hostmask, const QVariantMap &initData = QVariantMap());
241 inline IrcUser *newIrcUser(const QByteArray &hostmask) { return newIrcUser(decodeServerString(hostmask)); }
242 IrcUser *ircUser(QString nickname) const;
243 inline IrcUser *ircUser(const QByteArray &nickname) const { return ircUser(decodeServerString(nickname)); }
244 inline QList<IrcUser *> ircUsers() const { return _ircUsers.values(); }
245 inline quint32 ircUserCount() const { return _ircUsers.count(); }
247 IrcChannel *newIrcChannel(const QString &channelname, const QVariantMap &initData = QVariantMap());
248 inline IrcChannel *newIrcChannel(const QByteArray &channelname) { return newIrcChannel(decodeServerString(channelname)); }
249 IrcChannel *ircChannel(QString channelname) const;
250 inline IrcChannel *ircChannel(const QByteArray &channelname) const { return ircChannel(decodeServerString(channelname)); }
251 inline QList<IrcChannel *> ircChannels() const { return _ircChannels.values(); }
252 inline quint32 ircChannelCount() const { return _ircChannels.count(); }
254 QByteArray codecForServer() const;
255 QByteArray codecForEncoding() const;
256 QByteArray codecForDecoding() const;
257 void setCodecForServer(QTextCodec *codec);
258 void setCodecForEncoding(QTextCodec *codec);
259 void setCodecForDecoding(QTextCodec *codec);
261 QString decodeString(const QByteArray &text) const;
262 QByteArray encodeString(const QString &string) const;
263 QString decodeServerString(const QByteArray &text) const;
264 QByteArray encodeServerString(const QString &string) const;
266 static QByteArray defaultCodecForServer();
267 static QByteArray defaultCodecForEncoding();
268 static QByteArray defaultCodecForDecoding();
269 static void setDefaultCodecForServer(const QByteArray &name);
270 static void setDefaultCodecForEncoding(const QByteArray &name);
271 static void setDefaultCodecForDecoding(const QByteArray &name);
273 inline bool autoAwayActive() const { return _autoAwayActive; }
274 inline void setAutoAwayActive(bool active) { _autoAwayActive = active; }
277 void setNetworkName(const QString &networkName);
278 void setCurrentServer(const QString ¤tServer);
279 void setConnected(bool isConnected);
280 void setConnectionState(int state);
281 virtual void setMyNick(const QString &mynick);
282 void setLatency(int latency);
283 void setIdentity(IdentityId);
285 void setServerList(const QVariantList &serverList);
286 void setUseRandomServer(bool);
287 void setPerform(const QStringList &);
288 void setUseAutoIdentify(bool);
289 void setAutoIdentifyService(const QString &);
290 void setAutoIdentifyPassword(const QString &);
291 void setUseSasl(bool);
292 void setSaslAccount(const QString &);
293 void setSaslPassword(const QString &);
294 virtual void setUseAutoReconnect(bool);
295 virtual void setAutoReconnectInterval(quint32);
296 virtual void setAutoReconnectRetries(quint16);
297 void setUnlimitedReconnectRetries(bool);
298 void setRejoinChannels(bool);
300 void setCodecForServer(const QByteArray &codecName);
301 void setCodecForEncoding(const QByteArray &codecName);
302 void setCodecForDecoding(const QByteArray &codecName);
304 void addSupport(const QString ¶m, const QString &value = QString());
305 void removeSupport(const QString ¶m);
307 // IRCv3 capability negotiation (can be connected to signals)
310 * Add an available capability, optionally providing a value.
312 * This may happen during first connect, or at any time later if a new capability becomes
313 * available (e.g. SASL service starting).
315 * @param[in] capability Name of the capability
318 * Optional value of the capability, e.g. sasl=plain.
321 void addCap(const QString &capability, const QString &value = QString());
324 * Marks a capability as acknowledged (enabled by the IRC server).
326 * @param[in] capability Name of the capability
328 void acknowledgeCap(const QString &capability);
331 * Removes a capability from the list of available capabilities.
333 * This may happen during first connect, or at any time later if an existing capability becomes
334 * unavailable (e.g. SASL service stopping). This also removes the capability from the list
335 * of acknowledged capabilities.
337 * @param[in] capability Name of the capability
339 void removeCap(const QString &capability);
342 * Clears all capabilities from the list of available capabilities.
344 * This also removes the capability from the list of acknowledged capabilities.
348 inline void addIrcUser(const QString &hostmask) { newIrcUser(hostmask); }
349 inline void addIrcChannel(const QString &channel) { newIrcChannel(channel); }
352 QVariantMap initSupports() const;
354 * Get the initial list of available capabilities.
356 * @return QVariantMap of <QString, QString> indicating available capabilities and values
358 QVariantMap initCaps() const;
360 * Get the initial list of enabled (acknowledged) capabilities.
362 * @return QVariantList of QString indicating enabled (acknowledged) capabilities and values
364 QVariantList initCapsEnabled() const { return toVariantList(capsEnabled()); }
365 inline QVariantList initServerList() const { return toVariantList(serverList()); }
366 virtual QVariantMap initIrcUsersAndChannels() const;
369 void initSetSupports(const QVariantMap &supports);
371 * Initialize the list of available capabilities.
373 * @param[in] caps QVariantMap of <QString, QString> indicating available capabilities and values
375 void initSetCaps(const QVariantMap &caps);
377 * Initialize the list of enabled (acknowledged) capabilities.
379 * @param[in] caps QVariantList of QString indicating enabled (acknowledged) capabilities and values
381 inline void initSetCapsEnabled(const QVariantList &capsEnabled) { _capsEnabled = fromVariantList<QString>(capsEnabled); }
382 inline void initSetServerList(const QVariantList &serverList) { _serverList = fromVariantList<Server>(serverList); }
383 virtual void initSetIrcUsersAndChannels(const QVariantMap &usersAndChannels);
386 * Update IrcUser hostmask and username from mask, creating an IrcUser if one does not exist.
388 * @param[in] mask Full nick!user@hostmask string
389 * @return IrcUser of the matching nick if exists, otherwise a new IrcUser
391 IrcUser *updateNickFromMask(const QString &mask);
393 // these slots are to keep the hashlists of all users and the
394 // channel lists up to date
395 void ircUserNickChanged(QString newnick);
397 virtual inline void requestConnect() const { REQUEST(NO_ARG) }
398 virtual inline void requestDisconnect() const { REQUEST(NO_ARG) }
399 virtual inline void requestSetNetworkInfo(const NetworkInfo &info) { REQUEST(ARG(info)) }
401 void emitConnectionError(const QString &);
404 virtual void removeIrcUser(IrcUser *ircuser);
405 virtual void removeIrcChannel(IrcChannel *ircChannel);
406 virtual void removeChansAndUsers();
409 void aboutToBeDestroyed();
410 void networkNameSet(const QString &networkName);
411 void currentServerSet(const QString ¤tServer);
412 void connectedSet(bool isConnected);
413 void connectionStateSet(Network::ConnectionState);
414 // void connectionStateSet(int);
415 void connectionError(const QString &errorMsg);
416 void myNickSet(const QString &mynick);
417 // void latencySet(int latency);
418 void identitySet(IdentityId);
420 void configChanged();
422 // void serverListSet(QVariantList serverList);
423 // void useRandomServerSet(bool);
424 // void performSet(const QStringList &);
425 // void useAutoIdentifySet(bool);
426 // void autoIdentifyServiceSet(const QString &);
427 // void autoIdentifyPasswordSet(const QString &);
428 // void useAutoReconnectSet(bool);
429 // void autoReconnectIntervalSet(quint32);
430 // void autoReconnectRetriesSet(quint16);
431 // void unlimitedReconnectRetriesSet(bool);
432 // void rejoinChannelsSet(bool);
434 // void codecForServerSet(const QByteArray &codecName);
435 // void codecForEncodingSet(const QByteArray &codecName);
436 // void codecForDecodingSet(const QByteArray &codecName);
438 // void supportAdded(const QString ¶m, const QString &value);
439 // void supportRemoved(const QString ¶m);
441 // IRCv3 capability negotiation (can drive other slots)
443 * Indicates a capability is now available, with optional value in Network::capValue().
445 * @see Network::addCap()
447 * @param[in] capability Name of the capability
449 void capAdded (const QString &capability);
452 * Indicates a capability was acknowledged (enabled by the IRC server).
454 * @see Network::acknowledgeCap()
456 * @param[in] capability Name of the capability
458 void capAcknowledged(const QString &capability);
461 * Indicates a capability was removed from the list of available capabilities.
463 * @see Network::removeCap()
465 * @param[in] capability Name of the capability
467 void capRemoved(const QString &capability);
469 // void ircUserAdded(const QString &hostmask);
470 void ircUserAdded(IrcUser *);
471 // void ircChannelAdded(const QString &channelname);
472 void ircChannelAdded(IrcChannel *);
474 // void connectRequested() const;
475 // void disconnectRequested() const;
476 // void setNetworkInfoRequested(const NetworkInfo &) const;
479 inline virtual IrcChannel *ircChannelFactory(const QString &channelname) { return new IrcChannel(channelname, this); }
480 inline virtual IrcUser *ircUserFactory(const QString &hostmask) { return new IrcUser(hostmask, this); }
483 QPointer<SignalProxy> _proxy;
485 NetworkId _networkId;
486 IdentityId _identity;
490 QString _networkName;
491 QString _currentServer;
493 ConnectionState _connectionState;
495 mutable QString _prefixes;
496 mutable QString _prefixModes;
498 QHash<QString, IrcUser *> _ircUsers; // stores all known nicks for the server
499 QHash<QString, IrcChannel *> _ircChannels; // stores all known channels
500 QHash<QString, QString> _supports; // stores results from RPL_ISUPPORT
502 QHash<QString, QString> _caps; /// Capabilities supported by the IRC server
503 // By synchronizing the supported capabilities, the client could suggest certain behaviors, e.g.
504 // in the Network settings dialog, recommending SASL instead of using NickServ, or warning if
505 // SASL EXTERNAL isn't available.
506 QStringList _capsEnabled; /// Enabled capabilities that received 'CAP ACK'
507 // _capsEnabled uses the same values from the <name>=<value> pairs stored in _caps
509 ServerList _serverList;
510 bool _useRandomServer;
511 QStringList _perform;
513 bool _useAutoIdentify;
514 QString _autoIdentifyService;
515 QString _autoIdentifyPassword;
518 QString _saslAccount;
519 QString _saslPassword;
521 bool _useAutoReconnect;
522 quint32 _autoReconnectInterval;
523 quint16 _autoReconnectRetries;
524 bool _unlimitedReconnectRetries;
525 bool _rejoinChannels;
527 QTextCodec *_codecForServer;
528 QTextCodec *_codecForEncoding;
529 QTextCodec *_codecForDecoding;
531 static QTextCodec *_defaultCodecForServer;
532 static QTextCodec *_defaultCodecForEncoding;
533 static QTextCodec *_defaultCodecForDecoding;
535 bool _autoAwayActive; // when this is active handle305 and handle306 don't trigger any output
537 friend class IrcUser;
538 friend class IrcChannel;
542 //! Stores all editable information about a network (as opposed to runtime state).
544 // set some default values, note that this does not initialize e.g. name and id
551 bool useCustomEncodings; // not used!
552 QByteArray codecForServer;
553 QByteArray codecForEncoding;
554 QByteArray codecForDecoding;
556 Network::ServerList serverList;
557 bool useRandomServer;
561 bool useAutoIdentify;
562 QString autoIdentifyService;
563 QString autoIdentifyPassword;
567 QString saslPassword;
569 bool useAutoReconnect;
570 quint32 autoReconnectInterval;
571 quint16 autoReconnectRetries;
572 bool unlimitedReconnectRetries;
575 bool operator==(const NetworkInfo &other) const;
576 bool operator!=(const NetworkInfo &other) const;
579 QDataStream &operator<<(QDataStream &out, const NetworkInfo &info);
580 QDataStream &operator>>(QDataStream &in, NetworkInfo &info);
581 QDebug operator<<(QDebug dbg, const NetworkInfo &i);
582 Q_DECLARE_METATYPE(NetworkInfo)
584 QDataStream &operator<<(QDataStream &out, const Network::Server &server);
585 QDataStream &operator>>(QDataStream &in, Network::Server &server);
586 QDebug operator<<(QDebug dbg, const Network::Server &server);
587 Q_DECLARE_METATYPE(Network::Server)