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 CoreUserInputHandler::CoreUserInputHandler(CoreNetwork *parent)
34 : CoreBasicHandler(parent)
39 void CoreUserInputHandler::handleUserInput(const BufferInfo &bufferInfo, const QString &msg)
44 AliasManager::CommandList list = coreSession()->aliasManager().processInput(bufferInfo, msg);
46 for (int i = 0; i < list.count(); i++) {
47 QString cmd = list.at(i).second.section(' ', 0, 0).remove(0, 1).toUpper();
48 QString payload = list.at(i).second.section(' ', 1);
49 handle(cmd, Q_ARG(BufferInfo, list.at(i).first), Q_ARG(QString, payload));
54 // ====================
56 // ====================
57 void CoreUserInputHandler::handleAway(const BufferInfo &bufferInfo, const QString &msg)
60 if (msg.startsWith("-all")) {
61 if (msg.length() == 4) {
62 coreSession()->globalAway();
65 Q_ASSERT(msg.length() > 4);
67 coreSession()->globalAway(msg.mid(5));
75 void CoreUserInputHandler::issueAway(const QString &msg, bool autoCheck)
77 QString awayMsg = msg;
78 IrcUser *me = network()->me();
80 // if there is no message supplied we have to check if we are already away or not
81 if (autoCheck && msg.isEmpty()) {
82 if (me && !me->isAway()) {
83 Identity *identity = network()->identityPtr();
85 awayMsg = identity->awayReason();
87 if (awayMsg.isEmpty()) {
93 me->setAwayMessage(awayMsg);
95 putCmd("AWAY", serverEncode(awayMsg));
99 void CoreUserInputHandler::handleBan(const BufferInfo &bufferInfo, const QString &msg)
101 banOrUnban(bufferInfo, msg, true);
105 void CoreUserInputHandler::handleUnban(const BufferInfo &bufferInfo, const QString &msg)
107 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(Message::Error, BufferInfo::StatusBuffer, "", QString("Error: channel unknown in command: /BAN %1").arg(msg));
129 if (!params.isEmpty() && !params.contains("!") && network()->ircUser(params[0])) {
130 IrcUser *ircuser = network()->ircUser(params[0]);
131 // generalizedHost changes <nick> to *!ident@*.sld.tld.
132 QString generalizedHost = ircuser->host();
133 if (generalizedHost.isEmpty()) {
134 emit displayMsg(Message::Error, BufferInfo::StatusBuffer, "", QString("Error: host unknown in command: /BAN %1").arg(msg));
138 static QRegExp ipAddress("\\d+\\.\\d+\\.\\d+\\.\\d+");
139 if (ipAddress.exactMatch(generalizedHost)) {
140 int lastDotPos = generalizedHost.lastIndexOf('.') + 1;
141 generalizedHost.replace(lastDotPos, generalizedHost.length() - lastDotPos, '*');
143 else if (generalizedHost.lastIndexOf(".") != -1 && generalizedHost.lastIndexOf(".", generalizedHost.lastIndexOf(".")-1) != -1) {
144 int secondLastPeriodPosition = generalizedHost.lastIndexOf(".", generalizedHost.lastIndexOf(".")-1);
145 generalizedHost.replace(0, secondLastPeriodPosition, "*");
147 banUser = QString("*!%1@%2").arg(ircuser->user(), generalizedHost);
150 banUser = params.join(" ");
153 QString banMode = ban ? "+b" : "-b";
154 QString banMsg = QString("MODE %1 %2 %3").arg(banChannel, banMode, banUser);
155 emit putRawLine(serverEncode(banMsg));
159 void CoreUserInputHandler::handleCtcp(const BufferInfo &bufferInfo, const QString &msg)
163 QString nick = msg.section(' ', 0, 0);
164 QString ctcpTag = msg.section(' ', 1, 1).toUpper();
165 if (ctcpTag.isEmpty())
168 QString message = msg.section(' ', 2);
169 QString verboseMessage = tr("sending CTCP-%1 request to %2").arg(ctcpTag).arg(nick);
171 if (ctcpTag == "PING") {
172 message = QString::number(QDateTime::currentMSecsSinceEpoch());
175 // FIXME make this a proper event
176 coreNetwork()->coreSession()->ctcpParser()->query(coreNetwork(), nick, ctcpTag, message);
177 emit displayMsg(Message::Action, BufferInfo::StatusBuffer, "", verboseMessage, network()->myNick());
181 void CoreUserInputHandler::handleDelkey(const BufferInfo &bufferInfo, const QString &msg)
183 QString bufname = bufferInfo.bufferName().isNull() ? "" : bufferInfo.bufferName();
185 if (!bufferInfo.isValid())
188 if (!Cipher::neededFeaturesAvailable()) {
189 emit displayMsg(Message::Error, typeByTarget(bufname), bufname, tr("Error: QCA provider plugin not found. It is usually provided by the qca-ossl plugin."));
193 QStringList parms = msg.split(' ', QString::SkipEmptyParts);
195 if (parms.isEmpty() && !bufferInfo.bufferName().isEmpty() && bufferInfo.acceptsRegularMessages())
196 parms.prepend(bufferInfo.bufferName());
198 if (parms.isEmpty()) {
199 emit displayMsg(Message::Info, typeByTarget(bufname), bufname,
200 tr("[usage] /delkey <nick|channel> deletes the encryption key for nick or channel or just /delkey when in a channel or query."));
204 QString target = parms.at(0);
206 if (network()->cipherKey(target).isEmpty()) {
207 emit displayMsg(Message::Info, typeByTarget(bufname), bufname, tr("No key has been set for %1.").arg(target));
211 network()->setCipherKey(target, QByteArray());
212 emit displayMsg(Message::Info, typeByTarget(bufname), bufname, tr("The key for %1 has been deleted.").arg(target));
216 emit displayMsg(Message::Error, typeByTarget(bufname), bufname, tr("Error: Setting an encryption key requires Quassel to have been built "
217 "with support for the Qt Cryptographic Architecture (QCA2) library. "
218 "Contact your distributor about a Quassel package with QCA2 "
219 "support, or rebuild Quassel with QCA2 present."));
223 void CoreUserInputHandler::doMode(const BufferInfo &bufferInfo, const QChar& addOrRemove, const QChar& mode, const QString &nicks)
227 int maxModes = network()->support("MODES").toInt(&isNumber);
228 if (!isNumber || maxModes == 0) maxModes = 1;
230 QStringList nickList;
231 if (nicks == "*" && bufferInfo.type() == BufferInfo::ChannelBuffer) { // All users in channel
232 const QList<IrcUser*> users = network()->ircChannel(bufferInfo.bufferName())->ircUsers();
233 foreach(IrcUser *user, users) {
234 if ((addOrRemove == '+' && !network()->ircChannel(bufferInfo.bufferName())->userModes(user).contains(mode))
235 || (addOrRemove == '-' && network()->ircChannel(bufferInfo.bufferName())->userModes(user).contains(mode)))
236 nickList.append(user->nick());
239 nickList = nicks.split(' ', QString::SkipEmptyParts);
242 if (nickList.count() == 0) return;
244 while (!nickList.isEmpty()) {
245 int amount = qMin(nickList.count(), maxModes);
246 QString m = addOrRemove; for(int i = 0; i < amount; i++) m += mode;
248 params << bufferInfo.bufferName() << m;
249 for(int i = 0; i < amount; i++) params << nickList.takeFirst();
250 emit putCmd("MODE", serverEncode(params));
255 void CoreUserInputHandler::handleDeop(const BufferInfo &bufferInfo, const QString &nicks)
257 doMode(bufferInfo, '-', 'o', nicks);
261 void CoreUserInputHandler::handleDehalfop(const BufferInfo &bufferInfo, const QString &nicks)
263 doMode(bufferInfo, '-', 'h', nicks);
267 void CoreUserInputHandler::handleDevoice(const BufferInfo &bufferInfo, const QString &nicks)
269 doMode(bufferInfo, '-', 'v', nicks);
272 void CoreUserInputHandler::handleHalfop(const BufferInfo &bufferInfo, const QString &nicks)
274 doMode(bufferInfo, '+', 'h', nicks);
277 void CoreUserInputHandler::handleOp(const BufferInfo &bufferInfo, const QString &nicks) {
278 doMode(bufferInfo, '+', 'o', nicks);
282 void CoreUserInputHandler::handleInvite(const BufferInfo &bufferInfo, const QString &msg)
285 params << msg << bufferInfo.bufferName();
286 emit putCmd("INVITE", serverEncode(params));
290 void CoreUserInputHandler::handleJoin(const BufferInfo &bufferInfo, const QString &msg)
292 Q_UNUSED(bufferInfo);
294 // trim spaces before chans or keys
295 QString sane_msg = msg;
296 sane_msg.replace(QRegExp(", +"), ",");
297 QStringList params = sane_msg.trimmed().split(" ");
299 QStringList chans = params[0].split(",", QString::SkipEmptyParts);
301 if (params.count() > 1)
302 keys = params[1].split(",");
305 for (i = 0; i < chans.count(); i++) {
306 if (!network()->isChannelName(chans[i]))
307 chans[i].prepend('#');
309 if (i < keys.count()) {
310 network()->addChannelKey(chans[i], keys[i]);
313 network()->removeChannelKey(chans[i]);
317 static const char *cmd = "JOIN";
319 QStringList joinChans, joinKeys;
320 int slicesize = chans.count();
321 QList<QByteArray> encodedParams;
323 // go through all to-be-joined channels and (re)build the join list
324 while (i < chans.count()) {
325 joinChans.append(chans.at(i));
326 if (i < keys.count())
327 joinKeys.append(keys.at(i));
329 // if the channel list we built so far either contains all requested channels or exceeds
330 // the desired amount of channels in this slice, try to send what we have so far
331 if (++i == chans.count() || joinChans.count() >= slicesize) {
333 params.append(joinChans.join(","));
334 params.append(joinKeys.join(","));
335 encodedParams = serverEncode(params);
336 // check if it fits in one command
337 if (lastParamOverrun(cmd, encodedParams) == 0) {
338 emit putCmd(cmd, encodedParams);
340 else if (slicesize > 1) {
341 // back to start of slice, try again with half the amount of channels
352 void CoreUserInputHandler::handleKeyx(const BufferInfo &bufferInfo, const QString &msg)
354 QString bufname = bufferInfo.bufferName().isNull() ? "" : bufferInfo.bufferName();
356 if (!bufferInfo.isValid())
359 if (!Cipher::neededFeaturesAvailable()) {
360 emit displayMsg(Message::Error, typeByTarget(bufname), bufname, tr("Error: QCA provider plugin not found. It is usually provided by the qca-ossl plugin."));
364 QStringList parms = msg.split(' ', QString::SkipEmptyParts);
366 if (parms.count() == 0 && !bufferInfo.bufferName().isEmpty() && bufferInfo.acceptsRegularMessages())
367 parms.prepend(bufferInfo.bufferName());
368 else if (parms.count() != 1) {
369 emit displayMsg(Message::Info, typeByTarget(bufname), bufname,
370 tr("[usage] /keyx [<nick>] Initiates a DH1080 key exchange with the target."));
374 QString target = parms.at(0);
376 if (network()->isChannelName(target)) {
377 emit displayMsg(Message::Info, typeByTarget(bufname), bufname, tr("It is only possible to exchange keys in a query buffer."));
381 Cipher *cipher = network()->cipher(target);
382 if (!cipher) // happens when there is no CoreIrcChannel for the target
385 QByteArray pubKey = cipher->initKeyExchange();
386 if (pubKey.isEmpty())
387 emit displayMsg(Message::Error, typeByTarget(bufname), bufname, tr("Failed to initiate key exchange with %1.").arg(target));
389 QList<QByteArray> params;
390 params << serverEncode(target) << serverEncode("DH1080_INIT ") + pubKey;
391 emit putCmd("NOTICE", params);
392 emit displayMsg(Message::Info, typeByTarget(bufname), bufname, tr("Initiated key exchange with %1.").arg(target));
396 emit displayMsg(Message::Error, typeByTarget(bufname), bufname, tr("Error: Setting an encryption key requires Quassel to have been built "
397 "with support for the Qt Cryptographic Architecture (QCA) library. "
398 "Contact your distributor about a Quassel package with QCA "
399 "support, or rebuild Quassel with QCA present."));
404 void CoreUserInputHandler::handleKick(const BufferInfo &bufferInfo, const QString &msg)
406 QString nick = msg.section(' ', 0, 0, QString::SectionSkipEmpty);
407 QString reason = msg.section(' ', 1, -1, QString::SectionSkipEmpty).trimmed();
408 if (reason.isEmpty())
409 reason = network()->identityPtr()->kickReason();
411 QList<QByteArray> params;
412 params << serverEncode(bufferInfo.bufferName()) << serverEncode(nick) << channelEncode(bufferInfo.bufferName(), reason);
413 emit putCmd("KICK", params);
417 void CoreUserInputHandler::handleKill(const BufferInfo &bufferInfo, const QString &msg)
420 QString nick = msg.section(' ', 0, 0, QString::SectionSkipEmpty);
421 QString pass = msg.section(' ', 1, -1, QString::SectionSkipEmpty);
422 QList<QByteArray> params;
423 params << serverEncode(nick) << serverEncode(pass);
424 emit putCmd("KILL", params);
428 void CoreUserInputHandler::handleList(const BufferInfo &bufferInfo, const QString &msg)
431 emit putCmd("LIST", serverEncode(msg.split(' ', QString::SkipEmptyParts)));
435 void CoreUserInputHandler::handleMe(const BufferInfo &bufferInfo, const QString &msg)
437 if (bufferInfo.bufferName().isEmpty() || !bufferInfo.acceptsRegularMessages())
438 return; // server buffer
439 // FIXME make this a proper event
440 coreNetwork()->coreSession()->ctcpParser()->query(coreNetwork(), bufferInfo.bufferName(), "ACTION", msg);
441 emit displayMsg(Message::Action, bufferInfo.type(), bufferInfo.bufferName(), msg, network()->myNick(), Message::Self);
445 void CoreUserInputHandler::handleMode(const BufferInfo &bufferInfo, const QString &msg)
449 QStringList params = msg.split(' ', QString::SkipEmptyParts);
450 // 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
451 if (!params.isEmpty()) {
452 if (!network()->isChannelName(params[0]) && !network()->isMyNick(params[0]))
453 params.prepend(bufferInfo.bufferName());
454 if (network()->isMyNick(params[0]) && params.count() == 2)
455 network()->updateIssuedModes(params[1]);
456 if (params[0] == "-reset" && params.count() == 1) {
457 // FIXME: give feedback to the user (I don't want to add new strings right now)
458 network()->resetPersistentModes();
463 // TODO handle correct encoding for buffer modes (channelEncode())
464 emit putCmd("MODE", serverEncode(params));
468 // TODO: show privmsgs
469 void CoreUserInputHandler::handleMsg(const BufferInfo &bufferInfo, const QString &msg)
471 Q_UNUSED(bufferInfo);
472 if (!msg.contains(' '))
475 QString target = msg.section(' ', 0, 0);
476 QString msgSection = msg.section(' ', 1);
478 std::function<QByteArray(const QString &, const QString &)> encodeFunc = [this] (const QString &target, const QString &message) -> QByteArray {
479 return userEncode(target, message);
483 putPrivmsg(target, msgSection, encodeFunc, network()->cipher(target));
485 putPrivmsg(target, msgSection, encodeFunc);
490 void CoreUserInputHandler::handleNick(const BufferInfo &bufferInfo, const QString &msg)
493 QString nick = msg.section(' ', 0, 0);
494 emit putCmd("NICK", serverEncode(nick));
498 void CoreUserInputHandler::handleNotice(const BufferInfo &bufferInfo, const QString &msg)
500 QString bufferName = msg.section(' ', 0, 0);
501 QString payload = msg.section(' ', 1);
502 QList<QByteArray> params;
503 params << serverEncode(bufferName) << channelEncode(bufferInfo.bufferName(), payload);
504 emit putCmd("NOTICE", params);
505 emit displayMsg(Message::Notice, typeByTarget(bufferName), bufferName, payload, network()->myNick(), Message::Self);
510 void CoreUserInputHandler::handleOper(const BufferInfo &bufferInfo, const QString &msg)
513 emit putRawLine(serverEncode(QString("OPER %1").arg(msg)));
517 void CoreUserInputHandler::handlePart(const BufferInfo &bufferInfo, const QString &msg)
519 QList<QByteArray> params;
522 // msg might contain either a channel name and/or a reaon, so we have to check if the first word is a known channel
523 QString channelName = msg.section(' ', 0, 0);
524 if (channelName.isEmpty() || !network()->ircChannel(channelName)) {
525 channelName = bufferInfo.bufferName();
529 partReason = msg.mid(channelName.length() + 1);
532 if (partReason.isEmpty())
533 partReason = network()->identityPtr()->partReason();
535 params << serverEncode(channelName) << channelEncode(bufferInfo.bufferName(), partReason);
536 emit putCmd("PART", params);
540 void CoreUserInputHandler::handlePing(const BufferInfo &bufferInfo, const QString &msg)
546 param = QTime::currentTime().toString("hh:mm:ss.zzz");
548 putCmd("PING", serverEncode(param));
552 void CoreUserInputHandler::handlePrint(const BufferInfo &bufferInfo, const QString &msg)
554 if (bufferInfo.bufferName().isEmpty() || !bufferInfo.acceptsRegularMessages())
555 return; // server buffer
557 QByteArray encMsg = channelEncode(bufferInfo.bufferName(), msg);
558 emit displayMsg(Message::Info, bufferInfo.type(), bufferInfo.bufferName(), msg, network()->myNick(), Message::Self);
562 // TODO: implement queries
563 void CoreUserInputHandler::handleQuery(const BufferInfo &bufferInfo, const QString &msg)
566 QString target = msg.section(' ', 0, 0);
567 QString message = msg.section(' ', 1);
568 if (message.isEmpty())
569 emit displayMsg(Message::Server, BufferInfo::QueryBuffer, target, tr("Starting query with %1").arg(target), network()->myNick(), Message::Self);
571 emit displayMsg(Message::Plain, BufferInfo::QueryBuffer, target, message, network()->myNick(), Message::Self);
572 handleMsg(bufferInfo, msg);
576 void CoreUserInputHandler::handleQuit(const BufferInfo &bufferInfo, const QString &msg)
579 network()->disconnectFromIrc(true, msg);
583 void CoreUserInputHandler::issueQuit(const QString &reason)
585 emit putCmd("QUIT", serverEncode(reason));
589 void CoreUserInputHandler::handleQuote(const BufferInfo &bufferInfo, const QString &msg)
592 emit putRawLine(serverEncode(msg));
596 void CoreUserInputHandler::handleSay(const BufferInfo &bufferInfo, const QString &msg)
598 if (bufferInfo.bufferName().isEmpty() || !bufferInfo.acceptsRegularMessages())
599 return; // server buffer
601 std::function<QByteArray(const QString &, const QString &)> encodeFunc = [this] (const QString &target, const QString &message) -> QByteArray {
602 return channelEncode(target, message);
606 putPrivmsg(bufferInfo.bufferName(), msg, encodeFunc, network()->cipher(bufferInfo.bufferName()));
608 putPrivmsg(bufferInfo.bufferName(), msg, encodeFunc);
610 emit displayMsg(Message::Plain, bufferInfo.type(), bufferInfo.bufferName(), msg, network()->myNick(), Message::Self);
614 void CoreUserInputHandler::handleSetkey(const BufferInfo &bufferInfo, const QString &msg)
616 QString bufname = bufferInfo.bufferName().isNull() ? "" : bufferInfo.bufferName();
618 if (!bufferInfo.isValid())
621 if (!Cipher::neededFeaturesAvailable()) {
622 emit displayMsg(Message::Error, typeByTarget(bufname), bufname, tr("Error: QCA provider plugin not found. It is usually provided by the qca-ossl plugin."));
626 QStringList parms = msg.split(' ', QString::SkipEmptyParts);
628 if (parms.count() == 1 && !bufferInfo.bufferName().isEmpty() && bufferInfo.acceptsRegularMessages())
629 parms.prepend(bufferInfo.bufferName());
630 else if (parms.count() != 2) {
631 emit displayMsg(Message::Info, typeByTarget(bufname), bufname,
632 tr("[usage] /setkey <nick|channel> <key> sets the encryption key for nick or channel. "
633 "/setkey <key> when in a channel or query buffer sets the key for it."));
637 QString target = parms.at(0);
638 QByteArray key = parms.at(1).toLocal8Bit();
639 network()->setCipherKey(target, key);
641 emit displayMsg(Message::Info, typeByTarget(bufname), bufname, tr("The key for %1 has been set.").arg(target));
644 emit displayMsg(Message::Error, typeByTarget(bufname), bufname, tr("Error: Setting an encryption key requires Quassel to have been built "
645 "with support for the Qt Cryptographic Architecture (QCA) library. "
646 "Contact your distributor about a Quassel package with QCA "
647 "support, or rebuild Quassel with QCA present."));
652 void CoreUserInputHandler::handleShowkey(const BufferInfo &bufferInfo, const QString &msg)
654 QString bufname = bufferInfo.bufferName().isNull() ? "" : bufferInfo.bufferName();
656 if (!bufferInfo.isValid())
659 if (!Cipher::neededFeaturesAvailable()) {
660 emit displayMsg(Message::Error, typeByTarget(bufname), bufname, tr("Error: QCA provider plugin not found. It is usually provided by the qca-ossl plugin."));
664 QStringList parms = msg.split(' ', QString::SkipEmptyParts);
666 if (parms.isEmpty() && !bufferInfo.bufferName().isEmpty() && bufferInfo.acceptsRegularMessages())
667 parms.prepend(bufferInfo.bufferName());
669 if (parms.isEmpty()) {
670 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."));
674 QString target = parms.at(0);
675 QByteArray key = network()->cipherKey(target);
678 emit displayMsg(Message::Info, typeByTarget(bufname), bufname, tr("No key has been set for %1.").arg(target));
682 emit displayMsg(Message::Info, typeByTarget(bufname), bufname, tr("The key for %1 is %2:%3").arg(target, network()->cipherUsesCBC(target) ? "CBC" : "ECB", QString(key)));
686 emit displayMsg(Message::Error, typeByTarget(bufname), bufname, tr("Error: Setting an encryption key requires Quassel to have been built "
687 "with support for the Qt Cryptographic Architecture (QCA2) library. "
688 "Contact your distributor about a Quassel package with QCA2 "
689 "support, or rebuild Quassel with QCA2 present."));
694 void CoreUserInputHandler::handleTopic(const BufferInfo &bufferInfo, const QString &msg)
696 if (bufferInfo.bufferName().isEmpty() || !bufferInfo.acceptsRegularMessages())
699 QList<QByteArray> params;
700 params << serverEncode(bufferInfo.bufferName());
702 if (!msg.isEmpty()) {
704 params << encrypt(bufferInfo.bufferName(), channelEncode(bufferInfo.bufferName(), msg));
706 params << channelEncode(bufferInfo.bufferName(), msg);
710 emit putCmd("TOPIC", params);
714 void CoreUserInputHandler::handleVoice(const BufferInfo &bufferInfo, const QString &msg)
716 QStringList nicks = msg.split(' ', QString::SkipEmptyParts);
717 QString m = "+"; for (int i = 0; i < nicks.count(); i++) m += 'v';
719 params << bufferInfo.bufferName() << m << nicks;
720 emit putCmd("MODE", serverEncode(params));
724 void CoreUserInputHandler::handleWait(const BufferInfo &bufferInfo, const QString &msg)
726 int splitPos = msg.indexOf(';');
731 int delay = msg.left(splitPos).trimmed().toInt(&ok);
737 QString command = msg.mid(splitPos + 1).trimmed();
738 if (command.isEmpty())
741 _delayedCommands[startTimer(delay)] = Command(bufferInfo, command);
745 void CoreUserInputHandler::handleWho(const BufferInfo &bufferInfo, const QString &msg)
748 emit putCmd("WHO", serverEncode(msg.split(' ')));
752 void CoreUserInputHandler::handleWhois(const BufferInfo &bufferInfo, const QString &msg)
755 emit putCmd("WHOIS", serverEncode(msg.split(' ')));
759 void CoreUserInputHandler::handleWhowas(const BufferInfo &bufferInfo, const QString &msg)
762 emit putCmd("WHOWAS", serverEncode(msg.split(' ')));
766 void CoreUserInputHandler::defaultHandler(QString cmd, const BufferInfo &bufferInfo, const QString &msg)
768 Q_UNUSED(bufferInfo);
769 emit putCmd(serverEncode(cmd.toUpper()), serverEncode(msg.split(" ")));
773 void CoreUserInputHandler::putPrivmsg(const QString &target, const QString &message, std::function<QByteArray(const QString &, const QString &)> encodeFunc, Cipher *cipher)
776 QString cmd("PRIVMSG");
777 QByteArray targetEnc = serverEncode(target);
779 std::function<QList<QByteArray>(QString &)> cmdGenerator = [&] (QString &splitMsg) -> QList<QByteArray> {
780 QByteArray splitMsgEnc = encodeFunc(target, splitMsg);
783 if (cipher && !cipher->key().isEmpty() && !splitMsg.isEmpty()) {
784 cipher->encrypt(splitMsgEnc);
787 return QList<QByteArray>() << targetEnc << splitMsgEnc;
790 putCmd(cmd, network()->splitMessage(cmd, message, cmdGenerator));
794 // returns 0 if the message will not be chopped by the irc server or number of chopped bytes if message is too long
795 int CoreUserInputHandler::lastParamOverrun(const QString &cmd, const QList<QByteArray> ¶ms)
797 // the server will pass our message truncated to 512 bytes including CRLF with the following format:
798 // ":prefix COMMAND param0 param1 :lastparam"
799 // where prefix = "nickname!user@host"
800 // that means that the last message can be as long as:
801 // 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)
802 IrcUser *me = network()->me();
803 int maxLen = 480 - cmd.toLatin1().count(); // educated guess in case we don't know us (yet?)
806 maxLen = 512 - serverEncode(me->nick()).count() - serverEncode(me->user()).count() - serverEncode(me->host()).count() - cmd.toLatin1().count() - 6;
808 if (!params.isEmpty()) {
809 for (int i = 0; i < params.count() - 1; i++) {
810 maxLen -= (params[i].count() + 1);
812 maxLen -= 2; // " :" last param separator;
814 if (params.last().count() > maxLen) {
815 return params.last().count() - maxLen;
828 QByteArray CoreUserInputHandler::encrypt(const QString &target, const QByteArray &message_, bool *didEncrypt) const
833 if (message_.isEmpty())
836 if (!Cipher::neededFeaturesAvailable())
839 Cipher *cipher = network()->cipher(target);
840 if (!cipher || cipher->key().isEmpty())
843 QByteArray message = message_;
844 bool result = cipher->encrypt(message);
846 *didEncrypt = result;
854 void CoreUserInputHandler::timerEvent(QTimerEvent *event)
856 if (!_delayedCommands.contains(event->timerId())) {
857 QObject::timerEvent(event);
860 BufferInfo bufferInfo = _delayedCommands[event->timerId()].bufferInfo;
861 QString rawCommand = _delayedCommands[event->timerId()].command;
862 _delayedCommands.remove(event->timerId());
865 // the stored command might be the result of an alias expansion, so we need to split it up again
866 QStringList commands = rawCommand.split(QRegExp("; ?"));
867 foreach(QString command, commands) {
868 handleUserInput(bufferInfo, command);