X-Git-Url: https://git.quassel-irc.org/?p=quassel.git;a=blobdiff_plain;f=src%2Fcommon%2Fmessage.cpp;h=34990c6442bca5a44b98db8ec41be8445dab9f22;hp=837dc35a593922b7ae2dc3d43e00bccea8dbcc52;hb=458c3278a4b9102bcc470d48816fac379492a588;hpb=2039f5e28eeb431e394f1c2468a26218bd926538 diff --git a/src/common/message.cpp b/src/common/message.cpp index 837dc35a..34990c64 100644 --- a/src/common/message.cpp +++ b/src/common/message.cpp @@ -1,11 +1,11 @@ /*************************************************************************** - * Copyright (C) 2005/06 by The Quassel Team * + * Copyright (C) 2005-2018 by the Quassel Project * * devel@quassel-irc.org * * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * - * (at your option) any later version. * + * (at your option) version 3. * * * * This program is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * @@ -15,30 +15,118 @@ * You should have received a copy of the GNU General Public License * * along with this program; if not, write to the * * Free Software Foundation, Inc., * - * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * ***************************************************************************/ #include "message.h" + +#include "util.h" +#include "peer.h" +#include "signalproxy.h" + #include -QDataStream &operator<<(QDataStream &out, const Message &msg) { - out << (quint32)msg.timeStamp.toTime_t() << (quint8)msg.type << (quint8)msg.flags - << msg.buffer << msg.sender.toUtf8() << msg.text.toUtf8(); - return out; +Message::Message(const BufferInfo &bufferInfo, Type type, const QString &contents, const QString &sender, const QString &senderPrefixes, Flags flags) + : _timestamp(QDateTime::currentDateTime().toUTC()), + _bufferInfo(bufferInfo), + _contents(contents), + _sender(sender), + _senderPrefixes(senderPrefixes), + _type(type), + _flags(flags) +{ } -QDataStream &operator>>(QDataStream &in, Message &msg) { - quint8 t, f; - quint32 ts; - QByteArray s, m; - BufferId buf; - in >> ts >> t >> f >> buf >> s >> m; - msg.type = (Message::Type)t; - msg.flags = (quint8)f; - msg.buffer = buf; - msg.timeStamp = QDateTime::fromTime_t(ts); - msg.sender = QString::fromUtf8(s); - msg.text = QString::fromUtf8(m); - return in; + +Message::Message(const QDateTime &ts, const BufferInfo &bufferInfo, Type type, const QString &contents, const QString &sender, const QString &senderPrefixes, Flags flags) + : _timestamp(ts), + _bufferInfo(bufferInfo), + _contents(contents), + _sender(sender), + _senderPrefixes(senderPrefixes), + _type(type), + _flags(flags) +{ } + +QDataStream &operator<<(QDataStream &out, const Message &msg) +{ + Q_ASSERT(SignalProxy::current()); + Q_ASSERT(SignalProxy::current()->targetPeer()); + + // We do not serialize the sender prefixes until we have a new protocol or client-features implemented + out << msg.msgId(); + + if (SignalProxy::current()->targetPeer()->hasFeature(Quassel::Feature::LongMessageTime)) { + out << (quint64) msg.timestamp().toMSecsSinceEpoch(); + } else { + out << (quint32) msg.timestamp().toTime_t(); + } + + out << (quint32) msg.type() + << (quint8) msg.flags() + << msg.bufferInfo() + << msg.sender().toUtf8(); + + if (SignalProxy::current()->targetPeer()->hasFeature(Quassel::Feature::SenderPrefixes)) + out << msg.senderPrefixes().toUtf8(); + + out << msg.contents().toUtf8(); + return out; +} + + +QDataStream &operator>>(QDataStream &in, Message &msg) +{ + Q_ASSERT(SignalProxy::current()); + Q_ASSERT(SignalProxy::current()->sourcePeer()); + + in >> msg._msgId; + + if (SignalProxy::current()->sourcePeer()->hasFeature(Quassel::Feature::LongMessageTime)) { + quint64 timeStamp; + in >> timeStamp; + msg._timestamp = QDateTime::fromMSecsSinceEpoch(timeStamp); + } else { + quint32 timeStamp; + in >> timeStamp; + msg._timestamp = QDateTime::fromTime_t(timeStamp); + } + + quint32 type; + in >> type; + msg._type = Message::Type(type); + + quint8 flags; + in >> flags; + msg._flags = Message::Flags(flags); + + in >> msg._bufferInfo; + + QByteArray sender; + in >> sender; + msg._sender = QString::fromUtf8(sender); + + QByteArray senderPrefixes; + if (SignalProxy::current()->sourcePeer()->hasFeature(Quassel::Feature::SenderPrefixes)) + in >> senderPrefixes; + msg._senderPrefixes = QString::fromUtf8(senderPrefixes); + + QByteArray contents; + in >> contents; + msg._contents = QString::fromUtf8(contents); + + return in; +} + + +QDebug operator<<(QDebug dbg, const Message &msg) +{ + dbg.nospace() << qPrintable(QString("Message(MsgId:")) << msg.msgId() + << qPrintable(QString(",")) << msg.timestamp() + << qPrintable(QString(", Type:")) << msg.type() + << qPrintable(QString(", Flags:")) << msg.flags() << qPrintable(QString(")")) + << msg.sender() << ":" << msg.contents(); + return dbg; +}