1 /***************************************************************************
2 * Copyright (C) 2005-2019 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 ***************************************************************************/
21 #include "coreuserinputhandler.h"
25 #include "ctcpparser.h"
32 CoreUserInputHandler::CoreUserInputHandler(CoreNetwork* parent)
33 : CoreBasicHandler(parent)
36 void CoreUserInputHandler::handleUserInput(const BufferInfo& bufferInfo, const QString& msg)
41 AliasManager::CommandList list = coreSession()->aliasManager().processInput(bufferInfo, msg);
43 for (int i = 0; i < list.count(); i++) {
44 QString cmd = list.at(i).second.section(' ', 0, 0).remove(0, 1).toUpper();
45 QString payload = list.at(i).second.section(' ', 1);
46 handle(cmd, Q_ARG(BufferInfo, list.at(i).first), Q_ARG(QString, payload));
50 // ====================
52 // ====================
53 void CoreUserInputHandler::handleAway(const BufferInfo& bufferInfo, const QString& msg, const bool skipFormatting)
56 if (msg.startsWith("-all")) {
57 if (msg.length() == 4) {
58 coreSession()->globalAway(QString(), skipFormatting);
61 Q_ASSERT(msg.length() > 4);
63 coreSession()->globalAway(msg.mid(5), skipFormatting);
67 issueAway(msg, true /* force away */, skipFormatting);
70 void CoreUserInputHandler::issueAway(const QString& msg, bool autoCheck, const bool skipFormatting)
72 QString awayMsg = msg;
73 IrcUser* me = network()->me();
75 // Only apply timestamp formatting when requested
76 // This avoids re-processing any existing away message when the core restarts, so chained escape
77 // percent signs won't get down-processed.
78 if (!skipFormatting) {
79 // Apply the timestamp formatting to the away message (if empty, nothing will happen)
80 awayMsg = formatCurrentDateTimeInString(awayMsg);
83 // if there is no message supplied we have to check if we are already away or not
84 if (autoCheck && msg.isEmpty()) {
85 if (me && !me->isAway()) {
86 Identity* identity = network()->identityPtr();
88 awayMsg = formatCurrentDateTimeInString(identity->awayReason());
90 if (awayMsg.isEmpty()) {
96 me->setAwayMessage(awayMsg);
98 putCmd("AWAY", serverEncode(awayMsg));
101 void CoreUserInputHandler::handleBan(const BufferInfo& bufferInfo, const QString& msg)
103 banOrUnban(bufferInfo, msg, true);
106 void CoreUserInputHandler::handleUnban(const BufferInfo& bufferInfo, const QString& msg)
108 banOrUnban(bufferInfo, msg, false);
111 void CoreUserInputHandler::banOrUnban(const BufferInfo& bufferInfo, const QString& msg, bool ban)
116 QStringList params = msg.split(" ");
118 if (!params.isEmpty() && isChannelName(params[0])) {
119 banChannel = params.takeFirst();
121 else if (bufferInfo.type() == BufferInfo::ChannelBuffer) {
122 banChannel = bufferInfo.bufferName();
125 emit displayMsg(NetworkInternalMessage(
127 BufferInfo::StatusBuffer,
129 QString("Error: channel unknown in command: /BAN %1").arg(msg)
134 if (!params.isEmpty() && !params.contains("!") && network()->ircUser(params[0])) {
135 IrcUser* ircuser = network()->ircUser(params[0]);
136 // generalizedHost changes <nick> to *!ident@*.sld.tld.
137 QString generalizedHost = ircuser->host();
138 if (generalizedHost.isEmpty()) {
139 emit displayMsg(NetworkInternalMessage(
141 BufferInfo::StatusBuffer,
143 QString("Error: host unknown in command: /BAN %1").arg(msg)
148 static QRegExp ipAddress(R"(\d+\.\d+\.\d+\.\d+)");
149 if (ipAddress.exactMatch(generalizedHost)) {
150 int lastDotPos = generalizedHost.lastIndexOf('.') + 1;
151 generalizedHost.replace(lastDotPos, generalizedHost.length() - lastDotPos, '*');
153 else if (generalizedHost.lastIndexOf(".") != -1 && generalizedHost.lastIndexOf(".", generalizedHost.lastIndexOf(".") - 1) != -1) {
154 int secondLastPeriodPosition = generalizedHost.lastIndexOf(".", generalizedHost.lastIndexOf(".") - 1);
155 generalizedHost.replace(0, secondLastPeriodPosition, "*");
157 banUser = QString("*!%1@%2").arg(ircuser->user(), generalizedHost);
160 banUser = params.join(" ");
163 QString banMode = ban ? "+b" : "-b";
164 QString banMsg = QString("MODE %1 %2 %3").arg(banChannel, banMode, banUser);
165 emit putRawLine(serverEncode(banMsg));
168 void CoreUserInputHandler::handleCtcp(const BufferInfo& bufferInfo, const QString& msg)
172 QString nick = msg.section(' ', 0, 0);
173 QString ctcpTag = msg.section(' ', 1, 1).toUpper();
174 if (ctcpTag.isEmpty())
177 QString message = msg.section(' ', 2);
178 QString verboseMessage = tr("sending CTCP-%1 request to %2").arg(ctcpTag).arg(nick);
180 if (ctcpTag == "PING") {
181 message = QString::number(QDateTime::currentMSecsSinceEpoch());
184 // FIXME make this a proper event
185 coreNetwork()->coreSession()->ctcpParser()->query(coreNetwork(), nick, ctcpTag, message);
186 emit displayMsg(NetworkInternalMessage(
188 BufferInfo::StatusBuffer,
196 void CoreUserInputHandler::handleDelkey(const BufferInfo& bufferInfo, const QString& msg)
198 QString bufname = bufferInfo.bufferName().isNull() ? "" : bufferInfo.bufferName();
200 if (!bufferInfo.isValid())
203 if (!Cipher::neededFeaturesAvailable()) {
204 emit displayMsg(NetworkInternalMessage(
206 typeByTarget(bufname),
208 tr("Error: QCA provider plugin not found. It is usually provided by the qca-ossl plugin.")
213 QStringList parms = msg.split(' ', QString::SkipEmptyParts);
215 if (parms.isEmpty() && !bufferInfo.bufferName().isEmpty() && bufferInfo.acceptsRegularMessages())
216 parms.prepend(bufferInfo.bufferName());
218 if (parms.isEmpty()) {
219 emit displayMsg(NetworkInternalMessage(
221 typeByTarget(bufname),
223 tr("[usage] /delkey <nick|channel> deletes the encryption key for nick or channel or just /delkey when in a "
229 QString target = parms.at(0);
231 if (network()->cipherKey(target).isEmpty()) {
232 emit displayMsg(NetworkInternalMessage(
234 typeByTarget(bufname),
236 tr("No key has been set for %1.").arg(target)
241 network()->setCipherKey(target, QByteArray());
242 emit displayMsg(NetworkInternalMessage(
244 typeByTarget(bufname),
246 tr("The key for %1 has been deleted.").arg(target)
251 emit displayMsg(NetworkInternalMessage(
253 typeByTarget(bufname),
255 tr("Error: Setting an encryption key requires Quassel to have been built "
256 "with support for the Qt Cryptographic Architecture (QCA2) library. "
257 "Contact your distributor about a Quassel package with QCA2 "
258 "support, or rebuild Quassel with QCA2 present.")
263 void CoreUserInputHandler::doMode(const BufferInfo& bufferInfo, const QChar& addOrRemove, const QChar& mode, const QString& nicks)
266 int maxModes = network()->support("MODES").toInt(&isNumber);
267 if (!isNumber || maxModes == 0)
270 QStringList nickList;
271 if (nicks == "*" && bufferInfo.type() == BufferInfo::ChannelBuffer) { // All users in channel
272 const QList<IrcUser*> users = network()->ircChannel(bufferInfo.bufferName())->ircUsers();
273 for (IrcUser* user : users) {
274 if ((addOrRemove == '+' && !network()->ircChannel(bufferInfo.bufferName())->userModes(user).contains(mode))
275 || (addOrRemove == '-' && network()->ircChannel(bufferInfo.bufferName())->userModes(user).contains(mode)))
276 nickList.append(user->nick());
280 nickList = nicks.split(' ', QString::SkipEmptyParts);
283 if (nickList.count() == 0)
286 while (!nickList.isEmpty()) {
287 int amount = qMin(nickList.count(), maxModes);
288 QString m = addOrRemove;
289 for (int i = 0; i < amount; i++)
292 params << bufferInfo.bufferName() << m;
293 for (int i = 0; i < amount; i++)
294 params << nickList.takeFirst();
295 emit putCmd("MODE", serverEncode(params));
299 void CoreUserInputHandler::handleDeop(const BufferInfo& bufferInfo, const QString& nicks)
301 doMode(bufferInfo, '-', 'o', nicks);
304 void CoreUserInputHandler::handleDehalfop(const BufferInfo& bufferInfo, const QString& nicks)
306 doMode(bufferInfo, '-', 'h', nicks);
309 void CoreUserInputHandler::handleDevoice(const BufferInfo& bufferInfo, const QString& nicks)
311 doMode(bufferInfo, '-', 'v', nicks);
314 void CoreUserInputHandler::handleHalfop(const BufferInfo& bufferInfo, const QString& nicks)
316 doMode(bufferInfo, '+', 'h', nicks);
319 void CoreUserInputHandler::handleOp(const BufferInfo& bufferInfo, const QString& nicks)
321 doMode(bufferInfo, '+', 'o', nicks);
324 void CoreUserInputHandler::handleInvite(const BufferInfo& bufferInfo, const QString& msg)
327 params << msg << bufferInfo.bufferName();
328 emit putCmd("INVITE", serverEncode(params));
331 void CoreUserInputHandler::handleJoin(const BufferInfo& bufferInfo, const QString& msg)
333 Q_UNUSED(bufferInfo);
335 // trim spaces before chans or keys
336 QString sane_msg = msg;
337 sane_msg.replace(QRegExp(", +"), ",");
338 QStringList params = sane_msg.trimmed().split(" ");
340 QStringList chans = params[0].split(",", QString::SkipEmptyParts);
342 if (params.count() > 1)
343 keys = params[1].split(",");
346 for (i = 0; i < chans.count(); i++) {
347 if (!network()->isChannelName(chans[i]))
348 chans[i].prepend('#');
350 if (i < keys.count()) {
351 network()->addChannelKey(chans[i], keys[i]);
354 network()->removeChannelKey(chans[i]);
358 static const char* cmd = "JOIN";
360 QStringList joinChans, joinKeys;
361 int slicesize = chans.count();
362 QList<QByteArray> encodedParams;
364 // go through all to-be-joined channels and (re)build the join list
365 while (i < chans.count()) {
366 joinChans.append(chans.at(i));
367 if (i < keys.count())
368 joinKeys.append(keys.at(i));
370 // if the channel list we built so far either contains all requested channels or exceeds
371 // the desired amount of channels in this slice, try to send what we have so far
372 if (++i == chans.count() || joinChans.count() >= slicesize) {
374 params.append(joinChans.join(","));
375 params.append(joinKeys.join(","));
376 encodedParams = serverEncode(params);
377 // check if it fits in one command
378 if (lastParamOverrun(cmd, encodedParams) == 0) {
379 emit putCmd(cmd, encodedParams);
381 else if (slicesize > 1) {
382 // back to start of slice, try again with half the amount of channels
392 void CoreUserInputHandler::handleKeyx(const BufferInfo& bufferInfo, const QString& msg)
394 QString bufname = bufferInfo.bufferName().isNull() ? "" : bufferInfo.bufferName();
396 if (!bufferInfo.isValid())
399 if (!Cipher::neededFeaturesAvailable()) {
400 emit displayMsg(NetworkInternalMessage(
402 typeByTarget(bufname),
404 tr("Error: QCA provider plugin not found. It is usually provided by the qca-ossl plugin.")
409 QStringList parms = msg.split(' ', QString::SkipEmptyParts);
411 if (parms.count() == 0 && !bufferInfo.bufferName().isEmpty() && bufferInfo.acceptsRegularMessages())
412 parms.prepend(bufferInfo.bufferName());
413 else if (parms.count() != 1) {
414 emit displayMsg(NetworkInternalMessage(
416 typeByTarget(bufname),
418 tr("[usage] /keyx [<nick>] Initiates a DH1080 key exchange with the target.")
423 QString target = parms.at(0);
425 if (network()->isChannelName(target)) {
426 emit displayMsg(NetworkInternalMessage(
428 typeByTarget(bufname),
430 tr("It is only possible to exchange keys in a query buffer.")
435 Cipher* cipher = network()->cipher(target);
436 if (!cipher) // happens when there is no CoreIrcChannel for the target
439 QByteArray pubKey = cipher->initKeyExchange();
440 if (pubKey.isEmpty())
441 emit displayMsg(NetworkInternalMessage(
443 typeByTarget(bufname),
445 tr("Failed to initiate key exchange with %1.").arg(target)
448 QList<QByteArray> params;
449 params << serverEncode(target) << serverEncode("DH1080_INIT ") + pubKey;
450 emit putCmd("NOTICE", params);
451 emit displayMsg(NetworkInternalMessage(
453 typeByTarget(bufname),
455 tr("Initiated key exchange with %1.").arg(target)
460 emit displayMsg(NetworkInternalMessage(
462 typeByTarget(bufname),
464 tr("Error: Setting an encryption key requires Quassel to have been built "
465 "with support for the Qt Cryptographic Architecture (QCA) library. "
466 "Contact your distributor about a Quassel package with QCA "
467 "support, or rebuild Quassel with QCA present.")
472 void CoreUserInputHandler::handleKick(const BufferInfo& bufferInfo, const QString& msg)
474 QString nick = msg.section(' ', 0, 0, QString::SectionSkipEmpty);
475 QString reason = msg.section(' ', 1, -1, QString::SectionSkipEmpty).trimmed();
476 if (reason.isEmpty())
477 reason = network()->identityPtr()->kickReason();
479 QList<QByteArray> params;
480 params << serverEncode(bufferInfo.bufferName()) << serverEncode(nick) << channelEncode(bufferInfo.bufferName(), reason);
481 emit putCmd("KICK", params);
484 void CoreUserInputHandler::handleKill(const BufferInfo& bufferInfo, const QString& msg)
487 QString nick = msg.section(' ', 0, 0, QString::SectionSkipEmpty);
488 QString pass = msg.section(' ', 1, -1, QString::SectionSkipEmpty);
489 QList<QByteArray> params;
490 params << serverEncode(nick) << serverEncode(pass);
491 emit putCmd("KILL", params);
494 void CoreUserInputHandler::handleList(const BufferInfo& bufferInfo, const QString& msg)
497 emit putCmd("LIST", serverEncode(msg.split(' ', QString::SkipEmptyParts)));
500 void CoreUserInputHandler::handleMe(const BufferInfo& bufferInfo, const QString& msg)
502 if (bufferInfo.bufferName().isEmpty() || !bufferInfo.acceptsRegularMessages())
503 return; // server buffer
504 // FIXME make this a proper event
506 // Split apart messages at line feeds. The IRC protocol uses those to separate commands, so
507 // they need to be split into multiple messages.
508 QStringList messages = msg.split(QChar::LineFeed);
510 for (const auto& message : messages) {
511 // Handle each separated message independently
512 coreNetwork()->coreSession()->ctcpParser()->query(coreNetwork(), bufferInfo.bufferName(), "ACTION", message);
513 emit displayMsg(NetworkInternalMessage(
516 bufferInfo.bufferName(),
524 void CoreUserInputHandler::handleMode(const BufferInfo& bufferInfo, const QString& msg)
528 QStringList params = msg.split(' ', QString::SkipEmptyParts);
529 if (!params.isEmpty()) {
530 if (params[0] == "-reset" && params.count() == 1) {
531 network()->resetPersistentModes();
532 emit displayMsg(NetworkInternalMessage(
534 BufferInfo::StatusBuffer,
536 tr("Your persistent modes have been reset.")
540 if (!network()->isChannelName(params[0]) && !network()->isMyNick(params[0]))
541 // If the first argument is neither a channel nor us (user modes are only to oneself)
542 // the current buffer is assumed to be the target.
543 // If the current buffer returns no name (e.g. status buffer), assume target is us.
544 params.prepend(!bufferInfo.bufferName().isEmpty() ? bufferInfo.bufferName() : network()->myNick());
545 if (network()->isMyNick(params[0]) && params.count() == 2)
546 network()->updateIssuedModes(params[1]);
549 // TODO handle correct encoding for buffer modes (channelEncode())
550 emit putCmd("MODE", serverEncode(params));
553 // TODO: show privmsgs
554 void CoreUserInputHandler::handleMsg(const BufferInfo& bufferInfo, const QString& msg)
556 Q_UNUSED(bufferInfo);
557 if (!msg.contains(' '))
560 QString target = msg.section(' ', 0, 0);
561 QString msgSection = msg.section(' ', 1);
563 std::function<QByteArray(const QString&, const QString&)> encodeFunc =
564 [this](const QString& target, const QString& message) -> QByteArray { return userEncode(target, message); };
567 putPrivmsg(target, msgSection, encodeFunc, network()->cipher(target));
569 putPrivmsg(target, msgSection, encodeFunc);
573 void CoreUserInputHandler::handleNick(const BufferInfo& bufferInfo, const QString& msg)
576 QString nick = msg.section(' ', 0, 0);
577 emit putCmd("NICK", serverEncode(nick));
580 void CoreUserInputHandler::handleNotice(const BufferInfo& bufferInfo, const QString& msg)
582 QString bufferName = msg.section(' ', 0, 0);
583 QList<QByteArray> params;
584 // Split apart messages at line feeds. The IRC protocol uses those to separate commands, so
585 // they need to be split into multiple messages.
586 QStringList messages = msg.section(' ', 1).split(QChar::LineFeed);
588 for (const auto& message : messages) {
589 // Handle each separated message independently
591 params << serverEncode(bufferName) << channelEncode(bufferInfo.bufferName(), message);
592 emit putCmd("NOTICE", params);
593 emit displayMsg(NetworkInternalMessage(
595 typeByTarget(bufferName),
604 void CoreUserInputHandler::handleOper(const BufferInfo& bufferInfo, const QString& msg)
607 emit putRawLine(serverEncode(QString("OPER %1").arg(msg)));
610 void CoreUserInputHandler::handlePart(const BufferInfo& bufferInfo, const QString& msg)
612 QList<QByteArray> params;
615 // msg might contain either a channel name and/or a reaon, so we have to check if the first word is a known channel
616 QString channelName = msg.section(' ', 0, 0);
617 if (channelName.isEmpty() || !network()->ircChannel(channelName)) {
618 channelName = bufferInfo.bufferName();
622 partReason = msg.mid(channelName.length() + 1);
625 if (partReason.isEmpty())
626 partReason = network()->identityPtr()->partReason();
628 params << serverEncode(channelName) << channelEncode(bufferInfo.bufferName(), partReason);
629 emit putCmd("PART", params);
632 void CoreUserInputHandler::handlePing(const BufferInfo& bufferInfo, const QString& msg)
638 param = QTime::currentTime().toString("hh:mm:ss.zzz");
640 // Take priority so this won't get stuck behind other queued messages.
641 putCmd("PING", serverEncode(param), {}, {}, true);
644 void CoreUserInputHandler::handlePrint(const BufferInfo& bufferInfo, const QString& msg)
646 if (bufferInfo.bufferName().isEmpty() || !bufferInfo.acceptsRegularMessages())
647 return; // server buffer
649 QByteArray encMsg = channelEncode(bufferInfo.bufferName(), msg);
650 emit displayMsg(NetworkInternalMessage(
653 bufferInfo.bufferName(),
660 // TODO: implement queries
661 void CoreUserInputHandler::handleQuery(const BufferInfo& bufferInfo, const QString& msg)
664 QString target = msg.section(' ', 0, 0);
665 // Split apart messages at line feeds. The IRC protocol uses those to separate commands, so
666 // they need to be split into multiple messages.
667 QStringList messages = msg.section(' ', 1).split(QChar::LineFeed);
669 for (const auto& message : messages) {
670 // Handle each separated message independently
671 if (message.isEmpty()) {
672 emit displayMsg(NetworkInternalMessage(
674 BufferInfo::QueryBuffer,
676 tr("Starting query with %1").arg(target),
680 // handleMsg is a no-op if message is empty
683 emit displayMsg(NetworkInternalMessage(
685 BufferInfo::QueryBuffer,
691 // handleMsg needs the target specified at the beginning of the message
692 handleMsg(bufferInfo, target + " " + message);
697 void CoreUserInputHandler::handleQuit(const BufferInfo& bufferInfo, const QString& msg)
700 network()->disconnectFromIrc(true, msg);
703 void CoreUserInputHandler::issueQuit(const QString& reason, bool forceImmediate)
705 // If needing an immediate QUIT (e.g. core shutdown), prepend this to the queue
706 emit putCmd("QUIT", serverEncode(reason), {}, {}, forceImmediate);
709 void CoreUserInputHandler::handleQuote(const BufferInfo& bufferInfo, const QString& msg)
712 emit putRawLine(serverEncode(msg));
715 void CoreUserInputHandler::handleSay(const BufferInfo& bufferInfo, const QString& msg)
717 if (bufferInfo.bufferName().isEmpty() || !bufferInfo.acceptsRegularMessages())
718 return; // server buffer
720 std::function<QByteArray(const QString&, const QString&)> encodeFunc =
721 [this](const QString& target, const QString& message) -> QByteArray { return channelEncode(target, message); };
723 // Split apart messages at line feeds. The IRC protocol uses those to separate commands, so
724 // they need to be split into multiple messages.
725 QStringList messages = msg.split(QChar::LineFeed, QString::SkipEmptyParts);
727 for (const auto& message : messages) {
728 // Handle each separated message independently
730 putPrivmsg(bufferInfo.bufferName(), message, encodeFunc, network()->cipher(bufferInfo.bufferName()));
732 putPrivmsg(bufferInfo.bufferName(), message, encodeFunc);
734 emit displayMsg(NetworkInternalMessage(
737 bufferInfo.bufferName(),
745 void CoreUserInputHandler::handleSetkey(const BufferInfo& bufferInfo, const QString& msg)
747 QString bufname = bufferInfo.bufferName().isNull() ? "" : bufferInfo.bufferName();
749 if (!bufferInfo.isValid())
752 if (!Cipher::neededFeaturesAvailable()) {
753 emit displayMsg(NetworkInternalMessage(
755 typeByTarget(bufname),
757 tr("Error: QCA provider plugin not found. It is usually provided by the qca-ossl plugin.")
762 QStringList parms = msg.split(' ', QString::SkipEmptyParts);
764 if (parms.count() == 1 && !bufferInfo.bufferName().isEmpty() && bufferInfo.acceptsRegularMessages())
765 parms.prepend(bufferInfo.bufferName());
766 else if (parms.count() != 2) {
767 emit displayMsg(NetworkInternalMessage(
769 typeByTarget(bufname),
771 tr("[usage] /setkey <nick|channel> <key> sets the encryption key for nick or channel. "
772 "/setkey <key> when in a channel or query buffer sets the key for it.")
777 QString target = parms.at(0);
778 QByteArray key = parms.at(1).toLocal8Bit();
779 network()->setCipherKey(target, key);
781 emit displayMsg(NetworkInternalMessage(
783 typeByTarget(bufname),
785 tr("The key for %1 has been set.").arg(target)
789 emit displayMsg(NetworkInternalMessage(
791 typeByTarget(bufname),
793 tr("Error: Setting an encryption key requires Quassel to have been built "
794 "with support for the Qt Cryptographic Architecture (QCA) library. "
795 "Contact your distributor about a Quassel package with QCA "
796 "support, or rebuild Quassel with QCA present.")
801 void CoreUserInputHandler::handleShowkey(const BufferInfo& bufferInfo, const QString& msg)
803 QString bufname = bufferInfo.bufferName().isNull() ? "" : bufferInfo.bufferName();
805 if (!bufferInfo.isValid())
808 if (!Cipher::neededFeaturesAvailable()) {
809 emit displayMsg(NetworkInternalMessage(
811 typeByTarget(bufname),
813 tr("Error: QCA provider plugin not found. It is usually provided by the qca-ossl plugin.")
818 QStringList parms = msg.split(' ', QString::SkipEmptyParts);
820 if (parms.isEmpty() && !bufferInfo.bufferName().isEmpty() && bufferInfo.acceptsRegularMessages())
821 parms.prepend(bufferInfo.bufferName());
823 if (parms.isEmpty()) {
824 emit displayMsg(NetworkInternalMessage(
826 typeByTarget(bufname),
828 tr("[usage] /showkey <nick|channel> shows the encryption key for nick or channel or just /showkey when in a "
834 QString target = parms.at(0);
835 QByteArray key = network()->cipherKey(target);
838 emit displayMsg(NetworkInternalMessage(
840 typeByTarget(bufname),
842 tr("No key has been set for %1.").arg(target)
847 emit displayMsg(NetworkInternalMessage(
849 typeByTarget(bufname),
851 tr("The key for %1 is %2:%3").arg(target, network()->cipherUsesCBC(target) ? "CBC" : "ECB", QString(key))
856 emit displayMsg(NetworkInternalMessage(
858 typeByTarget(bufname),
860 tr("Error: Setting an encryption key requires Quassel to have been built "
861 "with support for the Qt Cryptographic Architecture (QCA2) library. "
862 "Contact your distributor about a Quassel package with QCA2 "
863 "support, or rebuild Quassel with QCA2 present.")
868 void CoreUserInputHandler::handleTopic(const BufferInfo& bufferInfo, const QString& msg)
870 if (bufferInfo.bufferName().isEmpty() || !bufferInfo.acceptsRegularMessages())
873 QList<QByteArray> params;
874 params << serverEncode(bufferInfo.bufferName());
876 if (!msg.isEmpty()) {
878 params << encrypt(bufferInfo.bufferName(), channelEncode(bufferInfo.bufferName(), msg));
880 params << channelEncode(bufferInfo.bufferName(), msg);
884 emit putCmd("TOPIC", params);
887 void CoreUserInputHandler::handleVoice(const BufferInfo& bufferInfo, const QString& msg)
889 QStringList nicks = msg.split(' ', QString::SkipEmptyParts);
891 for (int i = 0; i < nicks.count(); i++)
894 params << bufferInfo.bufferName() << m << nicks;
895 emit putCmd("MODE", serverEncode(params));
898 void CoreUserInputHandler::handleWait(const BufferInfo& bufferInfo, const QString& msg)
900 int splitPos = msg.indexOf(';');
905 int delay = msg.left(splitPos).trimmed().toInt(&ok);
911 QString command = msg.mid(splitPos + 1).trimmed();
912 if (command.isEmpty())
915 _delayedCommands[startTimer(delay)] = Command(bufferInfo, command);
918 void CoreUserInputHandler::handleWho(const BufferInfo& bufferInfo, const QString& msg)
921 emit putCmd("WHO", serverEncode(msg.split(' ')));
924 void CoreUserInputHandler::handleWhois(const BufferInfo& bufferInfo, const QString& msg)
927 emit putCmd("WHOIS", serverEncode(msg.split(' ')));
930 void CoreUserInputHandler::handleWhowas(const BufferInfo& bufferInfo, const QString& msg)
933 emit putCmd("WHOWAS", serverEncode(msg.split(' ')));
936 void CoreUserInputHandler::defaultHandler(QString cmd, const BufferInfo& bufferInfo, const QString& msg)
938 Q_UNUSED(bufferInfo);
939 emit putCmd(serverEncode(cmd.toUpper()), serverEncode(msg.split(" ")));
942 void CoreUserInputHandler::putPrivmsg(const QString& target,
943 const QString& message,
944 std::function<QByteArray(const QString&, const QString&)> encodeFunc,
948 QString cmd("PRIVMSG");
949 QByteArray targetEnc = serverEncode(target);
951 std::function<QList<QByteArray>(QString&)> cmdGenerator = [&](QString& splitMsg) -> QList<QByteArray> {
952 QByteArray splitMsgEnc = encodeFunc(target, splitMsg);
955 if (cipher && !cipher->key().isEmpty() && !splitMsg.isEmpty()) {
956 cipher->encrypt(splitMsgEnc);
959 return QList<QByteArray>() << targetEnc << splitMsgEnc;
962 putCmd(cmd, network()->splitMessage(cmd, message, cmdGenerator));
965 // returns 0 if the message will not be chopped by the irc server or number of chopped bytes if message is too long
966 int CoreUserInputHandler::lastParamOverrun(const QString& cmd, const QList<QByteArray>& params)
968 // the server will pass our message truncated to 512 bytes including CRLF with the following format:
969 // ":prefix COMMAND param0 param1 :lastparam"
970 // where prefix = "nickname!user@host"
971 // that means that the last message can be as long as:
972 // 512 - nicklen - userlen - hostlen - commandlen - sum(param[0]..param[n-1])) - 2 (for CRLF) - 4 (":!@" + 1space between prefix and
973 // command) - max(paramcount - 1, 0) (space for simple params) - 2 (space and colon for last param)
974 IrcUser* me = network()->me();
975 int maxLen = 480 - cmd.toLatin1().count(); // educated guess in case we don't know us (yet?)
978 maxLen = 512 - serverEncode(me->nick()).count() - serverEncode(me->user()).count() - serverEncode(me->host()).count()
979 - cmd.toLatin1().count() - 6;
981 if (!params.isEmpty()) {
982 for (int i = 0; i < params.count() - 1; i++) {
983 maxLen -= (params[i].count() + 1);
985 maxLen -= 2; // " :" last param separator;
987 if (params.last().count() > maxLen) {
988 return params.last().count() - maxLen;
1000 QByteArray CoreUserInputHandler::encrypt(const QString& target, const QByteArray& message_, bool* didEncrypt) const
1003 *didEncrypt = false;
1005 if (message_.isEmpty())
1008 if (!Cipher::neededFeaturesAvailable())
1011 Cipher* cipher = network()->cipher(target);
1012 if (!cipher || cipher->key().isEmpty())
1015 QByteArray message = message_;
1016 bool result = cipher->encrypt(message);
1018 *didEncrypt = result;
1025 void CoreUserInputHandler::timerEvent(QTimerEvent* event)
1027 if (!_delayedCommands.contains(event->timerId())) {
1028 QObject::timerEvent(event);
1031 BufferInfo bufferInfo = _delayedCommands[event->timerId()].bufferInfo;
1032 QString rawCommand = _delayedCommands[event->timerId()].command;
1033 _delayedCommands.remove(event->timerId());
1036 // the stored command might be the result of an alias expansion, so we need to split it up again
1037 QStringList commands = rawCommand.split(QRegExp("; ?"));
1038 for (const QString& command : commands) {
1039 handleUserInput(bufferInfo, command);