1 /***************************************************************************
2 * Copyright (C) 2005-2015 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"
33 #if QT_VERSION < 0x050000
34 // QChar::LineFeed is Qt 5
35 static const QChar QCharLF = QChar('\n');
37 static const QChar QCharLF = QChar::LineFeed;
40 CoreUserInputHandler::CoreUserInputHandler(CoreNetwork *parent)
41 : CoreBasicHandler(parent)
46 void CoreUserInputHandler::handleUserInput(const BufferInfo &bufferInfo, const QString &msg)
51 AliasManager::CommandList list = coreSession()->aliasManager().processInput(bufferInfo, msg);
53 for (int i = 0; i < list.count(); i++) {
54 QString cmd = list.at(i).second.section(' ', 0, 0).remove(0, 1).toUpper();
55 QString payload = list.at(i).second.section(' ', 1);
56 handle(cmd, Q_ARG(BufferInfo, list.at(i).first), Q_ARG(QString, payload));
61 // ====================
63 // ====================
64 void CoreUserInputHandler::handleAway(const BufferInfo &bufferInfo, const QString &msg)
67 if (msg.startsWith("-all")) {
68 if (msg.length() == 4) {
69 coreSession()->globalAway();
72 Q_ASSERT(msg.length() > 4);
74 coreSession()->globalAway(msg.mid(5));
82 void CoreUserInputHandler::issueAway(const QString &msg, bool autoCheck)
84 QString awayMsg = msg;
85 IrcUser *me = network()->me();
87 // if there is no message supplied we have to check if we are already away or not
88 if (autoCheck && msg.isEmpty()) {
89 if (me && !me->isAway()) {
90 Identity *identity = network()->identityPtr();
92 awayMsg = identity->awayReason();
94 if (awayMsg.isEmpty()) {
100 me->setAwayMessage(awayMsg);
102 putCmd("AWAY", serverEncode(awayMsg));
106 void CoreUserInputHandler::handleBan(const BufferInfo &bufferInfo, const QString &msg)
108 banOrUnban(bufferInfo, msg, true);
112 void CoreUserInputHandler::handleUnban(const BufferInfo &bufferInfo, const QString &msg)
114 banOrUnban(bufferInfo, msg, false);
118 void CoreUserInputHandler::banOrUnban(const BufferInfo &bufferInfo, const QString &msg, bool ban)
123 QStringList params = msg.split(" ");
125 if (!params.isEmpty() && isChannelName(params[0])) {
126 banChannel = params.takeFirst();
128 else if (bufferInfo.type() == BufferInfo::ChannelBuffer) {
129 banChannel = bufferInfo.bufferName();
132 emit displayMsg(Message::Error, BufferInfo::StatusBuffer, "", QString("Error: channel unknown in command: /BAN %1").arg(msg));
136 if (!params.isEmpty() && !params.contains("!") && network()->ircUser(params[0])) {
137 IrcUser *ircuser = network()->ircUser(params[0]);
138 // generalizedHost changes <nick> to *!ident@*.sld.tld.
139 QString generalizedHost = ircuser->host();
140 if (generalizedHost.isEmpty()) {
141 emit displayMsg(Message::Error, BufferInfo::StatusBuffer, "", QString("Error: host unknown in command: /BAN %1").arg(msg));
145 static QRegExp ipAddress("\\d+\\.\\d+\\.\\d+\\.\\d+");
146 if (ipAddress.exactMatch(generalizedHost)) {
147 int lastDotPos = generalizedHost.lastIndexOf('.') + 1;
148 generalizedHost.replace(lastDotPos, generalizedHost.length() - lastDotPos, '*');
150 else if (generalizedHost.lastIndexOf(".") != -1 && generalizedHost.lastIndexOf(".", generalizedHost.lastIndexOf(".")-1) != -1) {
151 int secondLastPeriodPosition = generalizedHost.lastIndexOf(".", generalizedHost.lastIndexOf(".")-1);
152 generalizedHost.replace(0, secondLastPeriodPosition, "*");
154 banUser = QString("*!%1@%2").arg(ircuser->user(), generalizedHost);
157 banUser = params.join(" ");
160 QString banMode = ban ? "+b" : "-b";
161 QString banMsg = QString("MODE %1 %2 %3").arg(banChannel, banMode, banUser);
162 emit putRawLine(serverEncode(banMsg));
166 void CoreUserInputHandler::handleCtcp(const BufferInfo &bufferInfo, const QString &msg)
170 QString nick = msg.section(' ', 0, 0);
171 QString ctcpTag = msg.section(' ', 1, 1).toUpper();
172 if (ctcpTag.isEmpty())
175 QString message = msg.section(' ', 2);
176 QString verboseMessage = tr("sending CTCP-%1 request to %2").arg(ctcpTag).arg(nick);
178 if (ctcpTag == "PING") {
179 message = QString::number(QDateTime::currentMSecsSinceEpoch());
182 // FIXME make this a proper event
183 coreNetwork()->coreSession()->ctcpParser()->query(coreNetwork(), nick, ctcpTag, message);
184 emit displayMsg(Message::Action, BufferInfo::StatusBuffer, "", verboseMessage, network()->myNick());
188 void CoreUserInputHandler::handleDelkey(const BufferInfo &bufferInfo, const QString &msg)
190 QString bufname = bufferInfo.bufferName().isNull() ? "" : bufferInfo.bufferName();
192 if (!bufferInfo.isValid())
195 if (!Cipher::neededFeaturesAvailable()) {
196 emit displayMsg(Message::Error, typeByTarget(bufname), bufname, tr("Error: QCA provider plugin not found. It is usually provided by the qca-ossl plugin."));
200 QStringList parms = msg.split(' ', QString::SkipEmptyParts);
202 if (parms.isEmpty() && !bufferInfo.bufferName().isEmpty() && bufferInfo.acceptsRegularMessages())
203 parms.prepend(bufferInfo.bufferName());
205 if (parms.isEmpty()) {
206 emit displayMsg(Message::Info, typeByTarget(bufname), bufname,
207 tr("[usage] /delkey <nick|channel> deletes the encryption key for nick or channel or just /delkey when in a channel or query."));
211 QString target = parms.at(0);
213 if (network()->cipherKey(target).isEmpty()) {
214 emit displayMsg(Message::Info, typeByTarget(bufname), bufname, tr("No key has been set for %1.").arg(target));
218 network()->setCipherKey(target, QByteArray());
219 emit displayMsg(Message::Info, typeByTarget(bufname), bufname, tr("The key for %1 has been deleted.").arg(target));
223 emit displayMsg(Message::Error, typeByTarget(bufname), bufname, tr("Error: Setting an encryption key requires Quassel to have been built "
224 "with support for the Qt Cryptographic Architecture (QCA2) library. "
225 "Contact your distributor about a Quassel package with QCA2 "
226 "support, or rebuild Quassel with QCA2 present."));
230 void CoreUserInputHandler::doMode(const BufferInfo &bufferInfo, const QChar& addOrRemove, const QChar& mode, const QString &nicks)
234 int maxModes = network()->support("MODES").toInt(&isNumber);
235 if (!isNumber || maxModes == 0) maxModes = 1;
237 QStringList nickList;
238 if (nicks == "*" && bufferInfo.type() == BufferInfo::ChannelBuffer) { // All users in channel
239 const QList<IrcUser*> users = network()->ircChannel(bufferInfo.bufferName())->ircUsers();
240 foreach(IrcUser *user, users) {
241 if ((addOrRemove == '+' && !network()->ircChannel(bufferInfo.bufferName())->userModes(user).contains(mode))
242 || (addOrRemove == '-' && network()->ircChannel(bufferInfo.bufferName())->userModes(user).contains(mode)))
243 nickList.append(user->nick());
246 nickList = nicks.split(' ', QString::SkipEmptyParts);
249 if (nickList.count() == 0) return;
251 while (!nickList.isEmpty()) {
252 int amount = qMin(nickList.count(), maxModes);
253 QString m = addOrRemove; for(int i = 0; i < amount; i++) m += mode;
255 params << bufferInfo.bufferName() << m;
256 for(int i = 0; i < amount; i++) params << nickList.takeFirst();
257 emit putCmd("MODE", serverEncode(params));
262 void CoreUserInputHandler::handleDeop(const BufferInfo &bufferInfo, const QString &nicks)
264 doMode(bufferInfo, '-', 'o', nicks);
268 void CoreUserInputHandler::handleDehalfop(const BufferInfo &bufferInfo, const QString &nicks)
270 doMode(bufferInfo, '-', 'h', nicks);
274 void CoreUserInputHandler::handleDevoice(const BufferInfo &bufferInfo, const QString &nicks)
276 doMode(bufferInfo, '-', 'v', nicks);
279 void CoreUserInputHandler::handleHalfop(const BufferInfo &bufferInfo, const QString &nicks)
281 doMode(bufferInfo, '+', 'h', nicks);
284 void CoreUserInputHandler::handleOp(const BufferInfo &bufferInfo, const QString &nicks) {
285 doMode(bufferInfo, '+', 'o', nicks);
289 void CoreUserInputHandler::handleInvite(const BufferInfo &bufferInfo, const QString &msg)
292 params << msg << bufferInfo.bufferName();
293 emit putCmd("INVITE", serverEncode(params));
297 void CoreUserInputHandler::handleJoin(const BufferInfo &bufferInfo, const QString &msg)
299 Q_UNUSED(bufferInfo);
301 // trim spaces before chans or keys
302 QString sane_msg = msg;
303 sane_msg.replace(QRegExp(", +"), ",");
304 QStringList params = sane_msg.trimmed().split(" ");
306 QStringList chans = params[0].split(",", QString::SkipEmptyParts);
308 if (params.count() > 1)
309 keys = params[1].split(",");
312 for (i = 0; i < chans.count(); i++) {
313 if (!network()->isChannelName(chans[i]))
314 chans[i].prepend('#');
316 if (i < keys.count()) {
317 network()->addChannelKey(chans[i], keys[i]);
320 network()->removeChannelKey(chans[i]);
324 static const char *cmd = "JOIN";
326 QStringList joinChans, joinKeys;
327 int slicesize = chans.count();
328 QList<QByteArray> encodedParams;
330 // go through all to-be-joined channels and (re)build the join list
331 while (i < chans.count()) {
332 joinChans.append(chans.at(i));
333 if (i < keys.count())
334 joinKeys.append(keys.at(i));
336 // if the channel list we built so far either contains all requested channels or exceeds
337 // the desired amount of channels in this slice, try to send what we have so far
338 if (++i == chans.count() || joinChans.count() >= slicesize) {
340 params.append(joinChans.join(","));
341 params.append(joinKeys.join(","));
342 encodedParams = serverEncode(params);
343 // check if it fits in one command
344 if (lastParamOverrun(cmd, encodedParams) == 0) {
345 emit putCmd(cmd, encodedParams);
347 else if (slicesize > 1) {
348 // back to start of slice, try again with half the amount of channels
359 void CoreUserInputHandler::handleKeyx(const BufferInfo &bufferInfo, const QString &msg)
361 QString bufname = bufferInfo.bufferName().isNull() ? "" : bufferInfo.bufferName();
363 if (!bufferInfo.isValid())
366 if (!Cipher::neededFeaturesAvailable()) {
367 emit displayMsg(Message::Error, typeByTarget(bufname), bufname, tr("Error: QCA provider plugin not found. It is usually provided by the qca-ossl plugin."));
371 QStringList parms = msg.split(' ', QString::SkipEmptyParts);
373 if (parms.count() == 0 && !bufferInfo.bufferName().isEmpty() && bufferInfo.acceptsRegularMessages())
374 parms.prepend(bufferInfo.bufferName());
375 else if (parms.count() != 1) {
376 emit displayMsg(Message::Info, typeByTarget(bufname), bufname,
377 tr("[usage] /keyx [<nick>] Initiates a DH1080 key exchange with the target."));
381 QString target = parms.at(0);
383 if (network()->isChannelName(target)) {
384 emit displayMsg(Message::Info, typeByTarget(bufname), bufname, tr("It is only possible to exchange keys in a query buffer."));
388 Cipher *cipher = network()->cipher(target);
389 if (!cipher) // happens when there is no CoreIrcChannel for the target
392 QByteArray pubKey = cipher->initKeyExchange();
393 if (pubKey.isEmpty())
394 emit displayMsg(Message::Error, typeByTarget(bufname), bufname, tr("Failed to initiate key exchange with %1.").arg(target));
396 QList<QByteArray> params;
397 params << serverEncode(target) << serverEncode("DH1080_INIT ") + pubKey;
398 emit putCmd("NOTICE", params);
399 emit displayMsg(Message::Info, typeByTarget(bufname), bufname, tr("Initiated key exchange with %1.").arg(target));
403 emit displayMsg(Message::Error, typeByTarget(bufname), bufname, tr("Error: Setting an encryption key requires Quassel to have been built "
404 "with support for the Qt Cryptographic Architecture (QCA) library. "
405 "Contact your distributor about a Quassel package with QCA "
406 "support, or rebuild Quassel with QCA present."));
411 void CoreUserInputHandler::handleKick(const BufferInfo &bufferInfo, const QString &msg)
413 QString nick = msg.section(' ', 0, 0, QString::SectionSkipEmpty);
414 QString reason = msg.section(' ', 1, -1, QString::SectionSkipEmpty).trimmed();
415 if (reason.isEmpty())
416 reason = network()->identityPtr()->kickReason();
418 QList<QByteArray> params;
419 params << serverEncode(bufferInfo.bufferName()) << serverEncode(nick) << channelEncode(bufferInfo.bufferName(), reason);
420 emit putCmd("KICK", params);
424 void CoreUserInputHandler::handleKill(const BufferInfo &bufferInfo, const QString &msg)
427 QString nick = msg.section(' ', 0, 0, QString::SectionSkipEmpty);
428 QString pass = msg.section(' ', 1, -1, QString::SectionSkipEmpty);
429 QList<QByteArray> params;
430 params << serverEncode(nick) << serverEncode(pass);
431 emit putCmd("KILL", params);
435 void CoreUserInputHandler::handleList(const BufferInfo &bufferInfo, const QString &msg)
438 emit putCmd("LIST", serverEncode(msg.split(' ', QString::SkipEmptyParts)));
442 void CoreUserInputHandler::handleMe(const BufferInfo &bufferInfo, const QString &msg)
444 if (bufferInfo.bufferName().isEmpty() || !bufferInfo.acceptsRegularMessages())
445 return; // server buffer
446 // FIXME make this a proper event
448 // Split apart messages at line feeds. The IRC protocol uses those to separate commands, so
449 // they need to be split into multiple messages.
450 QStringList messages = msg.split(QCharLF);
452 foreach (auto message, messages) {
453 // Handle each separated message independently
454 coreNetwork()->coreSession()->ctcpParser()->query(coreNetwork(), bufferInfo.bufferName(),
456 emit displayMsg(Message::Action, bufferInfo.type(), bufferInfo.bufferName(), message,
457 network()->myNick(), Message::Self);
462 void CoreUserInputHandler::handleMode(const BufferInfo &bufferInfo, const QString &msg)
466 QStringList params = msg.split(' ', QString::SkipEmptyParts);
467 // if the first argument is neither a channel nor us (user modes are only to oneself) the current buffer is assumed to be the target
468 if (!params.isEmpty()) {
469 if (!network()->isChannelName(params[0]) && !network()->isMyNick(params[0]))
470 params.prepend(bufferInfo.bufferName());
471 if (network()->isMyNick(params[0]) && params.count() == 2)
472 network()->updateIssuedModes(params[1]);
473 if (params[0] == "-reset" && params.count() == 1) {
474 // FIXME: give feedback to the user (I don't want to add new strings right now)
475 network()->resetPersistentModes();
480 // TODO handle correct encoding for buffer modes (channelEncode())
481 emit putCmd("MODE", serverEncode(params));
485 // TODO: show privmsgs
486 void CoreUserInputHandler::handleMsg(const BufferInfo &bufferInfo, const QString &msg)
488 Q_UNUSED(bufferInfo);
489 if (!msg.contains(' '))
492 QString target = msg.section(' ', 0, 0);
493 QString msgSection = msg.section(' ', 1);
495 std::function<QByteArray(const QString &, const QString &)> encodeFunc = [this] (const QString &target, const QString &message) -> QByteArray {
496 return userEncode(target, message);
500 putPrivmsg(target, msgSection, encodeFunc, network()->cipher(target));
502 putPrivmsg(target, msgSection, encodeFunc);
507 void CoreUserInputHandler::handleNick(const BufferInfo &bufferInfo, const QString &msg)
510 QString nick = msg.section(' ', 0, 0);
511 emit putCmd("NICK", serverEncode(nick));
515 void CoreUserInputHandler::handleNotice(const BufferInfo &bufferInfo, const QString &msg)
517 QString bufferName = msg.section(' ', 0, 0);
518 QList<QByteArray> params;
519 // Split apart messages at line feeds. The IRC protocol uses those to separate commands, so
520 // they need to be split into multiple messages.
521 QStringList messages = msg.section(' ', 1).split(QCharLF);
523 foreach (auto message, messages) {
524 // Handle each separated message independently
526 params << serverEncode(bufferName) << channelEncode(bufferInfo.bufferName(), message);
527 emit putCmd("NOTICE", params);
528 emit displayMsg(Message::Notice, typeByTarget(bufferName), bufferName, message,
529 network()->myNick(), Message::Self);
535 void CoreUserInputHandler::handleOper(const BufferInfo &bufferInfo, const QString &msg)
538 emit putRawLine(serverEncode(QString("OPER %1").arg(msg)));
542 void CoreUserInputHandler::handlePart(const BufferInfo &bufferInfo, const QString &msg)
544 QList<QByteArray> params;
547 // msg might contain either a channel name and/or a reaon, so we have to check if the first word is a known channel
548 QString channelName = msg.section(' ', 0, 0);
549 if (channelName.isEmpty() || !network()->ircChannel(channelName)) {
550 channelName = bufferInfo.bufferName();
554 partReason = msg.mid(channelName.length() + 1);
557 if (partReason.isEmpty())
558 partReason = network()->identityPtr()->partReason();
560 params << serverEncode(channelName) << channelEncode(bufferInfo.bufferName(), partReason);
561 emit putCmd("PART", params);
565 void CoreUserInputHandler::handlePing(const BufferInfo &bufferInfo, const QString &msg)
571 param = QTime::currentTime().toString("hh:mm:ss.zzz");
573 // Take priority so this won't get stuck behind other queued messages.
574 putCmd("PING", serverEncode(param), QByteArray(), true);
578 void CoreUserInputHandler::handlePrint(const BufferInfo &bufferInfo, const QString &msg)
580 if (bufferInfo.bufferName().isEmpty() || !bufferInfo.acceptsRegularMessages())
581 return; // server buffer
583 QByteArray encMsg = channelEncode(bufferInfo.bufferName(), msg);
584 emit displayMsg(Message::Info, bufferInfo.type(), bufferInfo.bufferName(), msg, network()->myNick(), Message::Self);
588 // TODO: implement queries
589 void CoreUserInputHandler::handleQuery(const BufferInfo &bufferInfo, const QString &msg)
592 QString target = msg.section(' ', 0, 0);
593 // Split apart messages at line feeds. The IRC protocol uses those to separate commands, so
594 // they need to be split into multiple messages.
595 QStringList messages = msg.section(' ', 1).split(QCharLF);
597 foreach (auto message, messages) {
598 // Handle each separated message independently
599 if (message.isEmpty()) {
600 emit displayMsg(Message::Server, BufferInfo::QueryBuffer, target,
601 tr("Starting query with %1").arg(target), network()->myNick(),
603 // handleMsg is a no-op if message is empty
605 emit displayMsg(Message::Plain, BufferInfo::QueryBuffer, target, message,
606 network()->myNick(), Message::Self);
607 // handleMsg needs the target specified at the beginning of the message
608 handleMsg(bufferInfo, target + " " + message);
614 void CoreUserInputHandler::handleQuit(const BufferInfo &bufferInfo, const QString &msg)
617 network()->disconnectFromIrc(true, msg);
621 void CoreUserInputHandler::issueQuit(const QString &reason, bool forceImmediate)
623 // If needing an immediate QUIT (e.g. core shutdown), prepend this to the queue
624 emit putCmd("QUIT", serverEncode(reason), QByteArray(), forceImmediate);
628 void CoreUserInputHandler::handleQuote(const BufferInfo &bufferInfo, const QString &msg)
631 emit putRawLine(serverEncode(msg));
635 void CoreUserInputHandler::handleSay(const BufferInfo &bufferInfo, const QString &msg)
637 if (bufferInfo.bufferName().isEmpty() || !bufferInfo.acceptsRegularMessages())
638 return; // server buffer
640 std::function<QByteArray(const QString &, const QString &)> encodeFunc = [this] (const QString &target, const QString &message) -> QByteArray {
641 return channelEncode(target, message);
644 // Split apart messages at line feeds. The IRC protocol uses those to separate commands, so
645 // they need to be split into multiple messages.
646 QStringList messages = msg.split(QCharLF, QString::SkipEmptyParts);
648 foreach (auto message, messages) {
649 // Handle each separated message independently
651 putPrivmsg(bufferInfo.bufferName(), message, encodeFunc,
652 network()->cipher(bufferInfo.bufferName()));
654 putPrivmsg(bufferInfo.bufferName(), message, encodeFunc);
656 emit displayMsg(Message::Plain, bufferInfo.type(), bufferInfo.bufferName(), message,
657 network()->myNick(), Message::Self);
662 void CoreUserInputHandler::handleSetkey(const BufferInfo &bufferInfo, const QString &msg)
664 QString bufname = bufferInfo.bufferName().isNull() ? "" : bufferInfo.bufferName();
666 if (!bufferInfo.isValid())
669 if (!Cipher::neededFeaturesAvailable()) {
670 emit displayMsg(Message::Error, typeByTarget(bufname), bufname, tr("Error: QCA provider plugin not found. It is usually provided by the qca-ossl plugin."));
674 QStringList parms = msg.split(' ', QString::SkipEmptyParts);
676 if (parms.count() == 1 && !bufferInfo.bufferName().isEmpty() && bufferInfo.acceptsRegularMessages())
677 parms.prepend(bufferInfo.bufferName());
678 else if (parms.count() != 2) {
679 emit displayMsg(Message::Info, typeByTarget(bufname), bufname,
680 tr("[usage] /setkey <nick|channel> <key> sets the encryption key for nick or channel. "
681 "/setkey <key> when in a channel or query buffer sets the key for it."));
685 QString target = parms.at(0);
686 QByteArray key = parms.at(1).toLocal8Bit();
687 network()->setCipherKey(target, key);
689 emit displayMsg(Message::Info, typeByTarget(bufname), bufname, tr("The key for %1 has been set.").arg(target));
692 emit displayMsg(Message::Error, typeByTarget(bufname), bufname, tr("Error: Setting an encryption key requires Quassel to have been built "
693 "with support for the Qt Cryptographic Architecture (QCA) library. "
694 "Contact your distributor about a Quassel package with QCA "
695 "support, or rebuild Quassel with QCA present."));
700 void CoreUserInputHandler::handleShowkey(const BufferInfo &bufferInfo, const QString &msg)
702 QString bufname = bufferInfo.bufferName().isNull() ? "" : bufferInfo.bufferName();
704 if (!bufferInfo.isValid())
707 if (!Cipher::neededFeaturesAvailable()) {
708 emit displayMsg(Message::Error, typeByTarget(bufname), bufname, tr("Error: QCA provider plugin not found. It is usually provided by the qca-ossl plugin."));
712 QStringList parms = msg.split(' ', QString::SkipEmptyParts);
714 if (parms.isEmpty() && !bufferInfo.bufferName().isEmpty() && bufferInfo.acceptsRegularMessages())
715 parms.prepend(bufferInfo.bufferName());
717 if (parms.isEmpty()) {
718 emit displayMsg(Message::Info, typeByTarget(bufname), bufname, tr("[usage] /showkey <nick|channel> shows the encryption key for nick or channel or just /showkey when in a channel or query."));
722 QString target = parms.at(0);
723 QByteArray key = network()->cipherKey(target);
726 emit displayMsg(Message::Info, typeByTarget(bufname), bufname, tr("No key has been set for %1.").arg(target));
730 emit displayMsg(Message::Info, typeByTarget(bufname), bufname, tr("The key for %1 is %2:%3").arg(target, network()->cipherUsesCBC(target) ? "CBC" : "ECB", QString(key)));
734 emit displayMsg(Message::Error, typeByTarget(bufname), bufname, tr("Error: Setting an encryption key requires Quassel to have been built "
735 "with support for the Qt Cryptographic Architecture (QCA2) library. "
736 "Contact your distributor about a Quassel package with QCA2 "
737 "support, or rebuild Quassel with QCA2 present."));
742 void CoreUserInputHandler::handleTopic(const BufferInfo &bufferInfo, const QString &msg)
744 if (bufferInfo.bufferName().isEmpty() || !bufferInfo.acceptsRegularMessages())
747 QList<QByteArray> params;
748 params << serverEncode(bufferInfo.bufferName());
750 if (!msg.isEmpty()) {
752 params << encrypt(bufferInfo.bufferName(), channelEncode(bufferInfo.bufferName(), msg));
754 params << channelEncode(bufferInfo.bufferName(), msg);
758 emit putCmd("TOPIC", params);
762 void CoreUserInputHandler::handleVoice(const BufferInfo &bufferInfo, const QString &msg)
764 QStringList nicks = msg.split(' ', QString::SkipEmptyParts);
765 QString m = "+"; for (int i = 0; i < nicks.count(); i++) m += 'v';
767 params << bufferInfo.bufferName() << m << nicks;
768 emit putCmd("MODE", serverEncode(params));
772 void CoreUserInputHandler::handleWait(const BufferInfo &bufferInfo, const QString &msg)
774 int splitPos = msg.indexOf(';');
779 int delay = msg.left(splitPos).trimmed().toInt(&ok);
785 QString command = msg.mid(splitPos + 1).trimmed();
786 if (command.isEmpty())
789 _delayedCommands[startTimer(delay)] = Command(bufferInfo, command);
793 void CoreUserInputHandler::handleWho(const BufferInfo &bufferInfo, const QString &msg)
796 emit putCmd("WHO", serverEncode(msg.split(' ')));
800 void CoreUserInputHandler::handleWhois(const BufferInfo &bufferInfo, const QString &msg)
803 emit putCmd("WHOIS", serverEncode(msg.split(' ')));
807 void CoreUserInputHandler::handleWhowas(const BufferInfo &bufferInfo, const QString &msg)
810 emit putCmd("WHOWAS", serverEncode(msg.split(' ')));
814 void CoreUserInputHandler::defaultHandler(QString cmd, const BufferInfo &bufferInfo, const QString &msg)
816 Q_UNUSED(bufferInfo);
817 emit putCmd(serverEncode(cmd.toUpper()), serverEncode(msg.split(" ")));
821 void CoreUserInputHandler::putPrivmsg(const QString &target, const QString &message, std::function<QByteArray(const QString &, const QString &)> encodeFunc, Cipher *cipher)
824 QString cmd("PRIVMSG");
825 QByteArray targetEnc = serverEncode(target);
827 std::function<QList<QByteArray>(QString &)> cmdGenerator = [&] (QString &splitMsg) -> QList<QByteArray> {
828 QByteArray splitMsgEnc = encodeFunc(target, splitMsg);
831 if (cipher && !cipher->key().isEmpty() && !splitMsg.isEmpty()) {
832 cipher->encrypt(splitMsgEnc);
835 return QList<QByteArray>() << targetEnc << splitMsgEnc;
838 putCmd(cmd, network()->splitMessage(cmd, message, cmdGenerator));
842 // returns 0 if the message will not be chopped by the irc server or number of chopped bytes if message is too long
843 int CoreUserInputHandler::lastParamOverrun(const QString &cmd, const QList<QByteArray> ¶ms)
845 // the server will pass our message truncated to 512 bytes including CRLF with the following format:
846 // ":prefix COMMAND param0 param1 :lastparam"
847 // where prefix = "nickname!user@host"
848 // that means that the last message can be as long as:
849 // 512 - nicklen - userlen - hostlen - commandlen - sum(param[0]..param[n-1])) - 2 (for CRLF) - 4 (":!@" + 1space between prefix and command) - max(paramcount - 1, 0) (space for simple params) - 2 (space and colon for last param)
850 IrcUser *me = network()->me();
851 int maxLen = 480 - cmd.toLatin1().count(); // educated guess in case we don't know us (yet?)
854 maxLen = 512 - serverEncode(me->nick()).count() - serverEncode(me->user()).count() - serverEncode(me->host()).count() - cmd.toLatin1().count() - 6;
856 if (!params.isEmpty()) {
857 for (int i = 0; i < params.count() - 1; i++) {
858 maxLen -= (params[i].count() + 1);
860 maxLen -= 2; // " :" last param separator;
862 if (params.last().count() > maxLen) {
863 return params.last().count() - maxLen;
876 QByteArray CoreUserInputHandler::encrypt(const QString &target, const QByteArray &message_, bool *didEncrypt) const
881 if (message_.isEmpty())
884 if (!Cipher::neededFeaturesAvailable())
887 Cipher *cipher = network()->cipher(target);
888 if (!cipher || cipher->key().isEmpty())
891 QByteArray message = message_;
892 bool result = cipher->encrypt(message);
894 *didEncrypt = result;
902 void CoreUserInputHandler::timerEvent(QTimerEvent *event)
904 if (!_delayedCommands.contains(event->timerId())) {
905 QObject::timerEvent(event);
908 BufferInfo bufferInfo = _delayedCommands[event->timerId()].bufferInfo;
909 QString rawCommand = _delayedCommands[event->timerId()].command;
910 _delayedCommands.remove(event->timerId());
913 // the stored command might be the result of an alias expansion, so we need to split it up again
914 QStringList commands = rawCommand.split(QRegExp("; ?"));
915 foreach(QString command, commands) {
916 handleUserInput(bufferInfo, command);