+/******** IRCv3 Capability Negotiation ********/
+
+void CoreNetwork::serverCapAdded(const QString &capability)
+{
+ // Check if it's a known capability; if so, add it to the list
+ // Handle special cases first
+ if (capability == IrcCap::SASL) {
+ // Only request SASL if it's enabled
+ if (networkInfo().useSasl)
+ queueCap(capability);
+ } else if (IrcCap::knownCaps.contains(capability)) {
+ // Handling for general known capabilities
+ queueCap(capability);
+ }
+}
+
+void CoreNetwork::serverCapAcknowledged(const QString &capability)
+{
+ // This may be called multiple times in certain situations.
+
+ // Handle core-side configuration
+ if (capability == IrcCap::AWAY_NOTIFY) {
+ // away-notify enabled, stop the autoWho timers, handle manually
+ setAutoWhoEnabled(false);
+ }
+
+ // Handle capabilities that require further messages sent to the IRC server
+ // If you change this list, ALSO change the list in CoreNetwork::capsRequiringServerMessages
+ if (capability == IrcCap::SASL) {
+ // If SASL mechanisms specified, limit to what's accepted for authentication
+ // if the current identity has a cert set, use SASL EXTERNAL
+ // FIXME use event
+#ifdef HAVE_SSL
+ if (!identityPtr()->sslCert().isNull()) {
+ if (IrcCap::SaslMech::maybeSupported(capValue(IrcCap::SASL), IrcCap::SaslMech::EXTERNAL)) {
+ // EXTERNAL authentication supported, send request
+ putRawLine(serverEncode("AUTHENTICATE EXTERNAL"));
+ } else {
+ displayMsg(Message::Error, BufferInfo::StatusBuffer, "",
+ tr("SASL EXTERNAL authentication not supported"));
+ sendNextCap();
+ }
+ } else {
+#endif
+ if (IrcCap::SaslMech::maybeSupported(capValue(IrcCap::SASL), IrcCap::SaslMech::PLAIN)) {
+ // PLAIN authentication supported, send request
+ // Only working with PLAIN atm, blowfish later
+ putRawLine(serverEncode("AUTHENTICATE PLAIN"));
+ } else {
+ displayMsg(Message::Error, BufferInfo::StatusBuffer, "",
+ tr("SASL PLAIN authentication not supported"));
+ sendNextCap();
+ }
+#ifdef HAVE_SSL
+ }
+#endif
+ }
+}
+
+void CoreNetwork::serverCapRemoved(const QString &capability)
+{
+ // This may be called multiple times in certain situations.
+
+ // Handle special cases here
+ if (capability == IrcCap::AWAY_NOTIFY) {
+ // away-notify disabled, enable autoWho according to configuration
+ setAutoWhoEnabled(networkConfig()->autoWhoEnabled());
+ }
+}
+
+void CoreNetwork::queueCap(const QString &capability)
+{
+ // IRCv3 specs all use lowercase capability names
+ QString _capLowercase = capability.toLower();
+ if (!_capsQueued.contains(_capLowercase)) {
+ _capsQueued.append(_capLowercase);
+ }
+}
+
+QString CoreNetwork::takeQueuedCap()
+{
+ if (!_capsQueued.empty()) {
+ return _capsQueued.takeFirst();
+ } else {
+ return QString();
+ }
+}
+
+void CoreNetwork::beginCapNegotiation()
+{
+ // Don't begin negotiation if no capabilities are queued to request
+ if (!capNegotiationInProgress())
+ return;
+
+ _capNegotiationActive = true;
+ displayMsg(Message::Server, BufferInfo::StatusBuffer, "",
+ tr("Ready to negotiate (found: %1)").arg(caps().join(", ")));
+ displayMsg(Message::Server, BufferInfo::StatusBuffer, "",
+ tr("Negotiating capabilities (requesting: %1)...").arg(_capsQueued.join(", ")));
+ sendNextCap();
+}
+
+void CoreNetwork::sendNextCap()
+{
+ if (capNegotiationInProgress()) {
+ // Request the next capability and remove it from the list
+ // Handle one at a time so one capability failing won't NAK all of 'em
+ putRawLine(serverEncode(QString("CAP REQ :%1").arg(takeQueuedCap())));
+ } else {
+ // No pending desired capabilities, capability negotiation finished
+ // If SASL requested but not available, print a warning
+ if (networkInfo().useSasl && !capEnabled(IrcCap::SASL))
+ displayMsg(Message::Error, BufferInfo::StatusBuffer, "",
+ tr("SASL authentication currently not supported by server"));
+
+ if (_capNegotiationActive) {
+ displayMsg(Message::Server, BufferInfo::StatusBuffer, "",
+ tr("Capability negotiation finished (enabled: %1)").arg(capsEnabled().join(", ")));
+ _capNegotiationActive = false;
+ }
+
+ // If nick registration is already complete, CAP END is not required
+ if (!_capInitialNegotiationEnded) {
+ putRawLine(serverEncode(QString("CAP END")));
+ _capInitialNegotiationEnded = true;
+ }
+ }
+}