-// returns 0 if the message will not be chopped by the irc server or number of chopped bytes if message is too long
-int CoreUserInputHandler::lastParamOverrun(const QString &cmd, const QList<QByteArray> ¶ms) {
- // the server will pass our message truncated to 512 bytes including CRLF with the following format:
- // ":prefix COMMAND param0 param1 :lastparam"
- // where prefix = "nickname!user@host"
- // that means that the last message can be as long as:
- // 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)
- IrcUser *me = network()->me();
- int maxLen = 480 - cmd.toAscii().count(); // educated guess in case we don't know us (yet?)
-
- if(me)
- maxLen = 512 - serverEncode(me->nick()).count() - serverEncode(me->user()).count() - serverEncode(me->host()).count() - cmd.toAscii().count() - 6;
-
- if(!params.isEmpty()) {
- for(int i = 0; i < params.count() - 1; i++) {
- maxLen -= (params[i].count() + 1);
+
+void CoreUserInputHandler::handleTopic(const BufferInfo &bufferInfo, const QString &msg)
+{
+ if (bufferInfo.bufferName().isEmpty())
+ return;
+
+ QList<QByteArray> params;
+ params << serverEncode(bufferInfo.bufferName());
+
+ if (!msg.isEmpty()) {
+# ifdef HAVE_QCA2
+ params << encrypt(bufferInfo.bufferName(), channelEncode(bufferInfo.bufferName(), msg));
+# else
+ params << channelEncode(bufferInfo.bufferName(), msg);
+# endif
+ }
+
+ emit putCmd("TOPIC", params);
+}
+
+
+void CoreUserInputHandler::handleVoice(const BufferInfo &bufferInfo, const QString &msg)
+{
+ QStringList nicks = msg.split(' ', QString::SkipEmptyParts);
+ QString m = "+"; for (int i = 0; i < nicks.count(); i++) m += 'v';
+ QStringList params;
+ params << bufferInfo.bufferName() << m << nicks;
+ emit putCmd("MODE", serverEncode(params));
+}
+
+
+void CoreUserInputHandler::handleWait(const BufferInfo &bufferInfo, const QString &msg)
+{
+ int splitPos = msg.indexOf(';');
+ if (splitPos <= 0)
+ return;
+
+ bool ok;
+ int delay = msg.left(splitPos).trimmed().toInt(&ok);
+ if (!ok)
+ return;
+
+ delay *= 1000;
+
+ QString command = msg.mid(splitPos + 1).trimmed();
+ if (command.isEmpty())
+ return;
+
+ _delayedCommands[startTimer(delay)] = Command(bufferInfo, command);
+}
+
+
+void CoreUserInputHandler::handleWho(const BufferInfo &bufferInfo, const QString &msg)
+{
+ Q_UNUSED(bufferInfo)
+ emit putCmd("WHO", serverEncode(msg.split(' ')));
+}
+
+
+void CoreUserInputHandler::handleWhois(const BufferInfo &bufferInfo, const QString &msg)
+{
+ Q_UNUSED(bufferInfo)
+ emit putCmd("WHOIS", serverEncode(msg.split(' ')));
+}
+
+
+void CoreUserInputHandler::handleWhowas(const BufferInfo &bufferInfo, const QString &msg)
+{
+ Q_UNUSED(bufferInfo)
+ emit putCmd("WHOWAS", serverEncode(msg.split(' ')));
+}
+
+
+void CoreUserInputHandler::defaultHandler(QString cmd, const BufferInfo &bufferInfo, const QString &msg)
+{
+ Q_UNUSED(bufferInfo);
+ emit putCmd(serverEncode(cmd.toUpper()), serverEncode(msg.split(" ")));
+}
+
+
+void CoreUserInputHandler::putPrivmsg(const QByteArray &target, const QByteArray &message, Cipher *cipher)
+{
+ // Encrypted messages need special care. There's no clear relation between cleartext and encrypted message length,
+ // so we can't just compute the maxSplitPos. Instead, we need to loop through the splitpoints until the crypted
+ // version is short enough...
+ // TODO: check out how the various possible encryption methods behave length-wise and make
+ // this clean by predicting the length of the crypted msg.
+ // For example, blowfish-ebc seems to create 8-char chunks.
+
+ static const char *cmd = "PRIVMSG";
+ static const char *splitter = " .,-";
+
+ int maxSplitPos = message.count();
+ int splitPos = maxSplitPos;
+ forever {
+ QByteArray crypted = message.left(splitPos);
+ bool isEncrypted = false;
+#ifdef HAVE_QCA2
+ if (cipher && !cipher->key().isEmpty() && !message.isEmpty()) {
+ isEncrypted = cipher->encrypt(crypted);
+ }
+#endif
+ int overrun = lastParamOverrun(cmd, QList<QByteArray>() << target << crypted);
+ if (overrun) {
+ // In case this is not an encrypted msg, we can just cut off at the end
+ if (!isEncrypted)
+ maxSplitPos = message.count() - overrun;
+
+ splitPos = -1;
+ for (const char *splitChar = splitter; *splitChar != 0; splitChar++) {
+ splitPos = qMax(splitPos, message.lastIndexOf(*splitChar, maxSplitPos) + 1); // keep split char on old line
+ }
+ if (splitPos <= 0 || splitPos > maxSplitPos)
+ splitPos = maxSplitPos;
+
+ maxSplitPos = splitPos - 1;
+ if (maxSplitPos <= 0) { // this should never happen, but who knows...
+ qWarning() << tr("[Error] Could not encrypt your message: %1").arg(message.data());
+ return;
+ }
+ continue; // we never come back here for !encrypted!
+ }
+
+ // now we have found a valid splitpos (or didn't need to split to begin with)
+ putCmd(cmd, QList<QByteArray>() << target << crypted);
+ if (splitPos < message.count())
+ putPrivmsg(target, message.mid(splitPos), cipher);
+
+ return;