modernize: Replace most remaining old-style connects by PMF ones
[quassel.git] / src / common / message.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-2018 by the Quassel Project                        *
3  *   devel@quassel-irc.org                                                 *
4  *                                                                         *
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.                                           *
9  *                                                                         *
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.                          *
14  *                                                                         *
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  ***************************************************************************/
20
21 #include "message.h"
22
23 #include "util.h"
24 #include "peer.h"
25 #include "signalproxy.h"
26
27 #include <QDataStream>
28 #include <utility>
29
30 Message::Message(BufferInfo bufferInfo, Type type, QString contents, QString sender,
31                  QString senderPrefixes, QString realName, QString avatarUrl, Flags flags)
32     : _timestamp(QDateTime::currentDateTime().toUTC()),
33     _bufferInfo(std::move(bufferInfo)),
34     _contents(std::move(contents)),
35     _sender(std::move(sender)),
36     _senderPrefixes(std::move(senderPrefixes)),
37     _realName(std::move(realName)),
38     _avatarUrl(std::move(avatarUrl)),
39     _type(type),
40     _flags(flags)
41 {
42 }
43
44
45 Message::Message(QDateTime ts, BufferInfo bufferInfo, Type type, QString contents,
46                  QString sender, QString senderPrefixes, QString realName,
47                  QString avatarUrl, Flags flags)
48     : _timestamp(std::move(ts)),
49     _bufferInfo(std::move(bufferInfo)),
50     _contents(std::move(contents)),
51     _sender(std::move(sender)),
52     _senderPrefixes(std::move(senderPrefixes)),
53     _realName(std::move(realName)),
54     _avatarUrl(std::move(avatarUrl)),
55     _type(type),
56     _flags(flags)
57 {
58 }
59
60
61 QDataStream &operator<<(QDataStream &out, const Message &msg)
62 {
63     Q_ASSERT(SignalProxy::current());
64     Q_ASSERT(SignalProxy::current()->targetPeer());
65
66     // We do not serialize the sender prefixes until we have a new protocol or client-features implemented
67     out << msg.msgId();
68
69     if (SignalProxy::current()->targetPeer()->hasFeature(Quassel::Feature::LongTime)) {
70         // toMSecs returns a qint64, signed rather than unsigned
71         out << (qint64) msg.timestamp().toMSecsSinceEpoch();
72     } else {
73         out << (quint32) msg.timestamp().toTime_t();
74     }
75
76     out << (quint32) msg.type()
77         << (quint8) msg.flags()
78         << msg.bufferInfo()
79         << msg.sender().toUtf8();
80
81     if (SignalProxy::current()->targetPeer()->hasFeature(Quassel::Feature::SenderPrefixes))
82         out << msg.senderPrefixes().toUtf8();
83
84     if (SignalProxy::current()->targetPeer()->hasFeature(Quassel::Feature::RichMessages)) {
85         out << msg.realName().toUtf8();
86         out << msg.avatarUrl().toUtf8();
87     }
88
89     out << msg.contents().toUtf8();
90     return out;
91 }
92
93
94 QDataStream &operator>>(QDataStream &in, Message &msg)
95 {
96     Q_ASSERT(SignalProxy::current());
97     Q_ASSERT(SignalProxy::current()->sourcePeer());
98
99     in >> msg._msgId;
100
101     if (SignalProxy::current()->sourcePeer()->hasFeature(Quassel::Feature::LongTime)) {
102         // timestamp is a qint64, signed rather than unsigned
103         qint64 timeStamp;
104         in >> timeStamp;
105         msg._timestamp = QDateTime::fromMSecsSinceEpoch(timeStamp);
106     } else {
107         quint32 timeStamp;
108         in >> timeStamp;
109         msg._timestamp = QDateTime::fromTime_t(timeStamp);
110     }
111
112     quint32 type;
113     in >> type;
114     msg._type = Message::Type(type);
115
116     quint8 flags;
117     in >> flags;
118     msg._flags = Message::Flags(flags);
119
120     in >> msg._bufferInfo;
121
122     QByteArray sender;
123     in >> sender;
124     msg._sender = QString::fromUtf8(sender);
125
126     QByteArray senderPrefixes;
127     if (SignalProxy::current()->sourcePeer()->hasFeature(Quassel::Feature::SenderPrefixes))
128         in >> senderPrefixes;
129     msg._senderPrefixes = QString::fromUtf8(senderPrefixes);
130
131     QByteArray realName;
132     QByteArray avatarUrl;
133     if (SignalProxy::current()->sourcePeer()->hasFeature(Quassel::Feature::RichMessages)) {
134         in >> realName;
135         in >> avatarUrl;
136     }
137     msg._realName = QString::fromUtf8(realName);
138     msg._avatarUrl = QString::fromUtf8(avatarUrl);
139
140     QByteArray contents;
141     in >> contents;
142     msg._contents = QString::fromUtf8(contents);
143
144     return in;
145 }
146
147
148 QDebug operator<<(QDebug dbg, const Message &msg)
149 {
150     dbg.nospace() << qPrintable(QString("Message(MsgId:")) << msg.msgId()
151     << qPrintable(QString(",")) << msg.timestamp()
152     << qPrintable(QString(", Type:")) << msg.type()
153     << qPrintable(QString(", RealName:")) << msg.realName()
154     << qPrintable(QString(", AvatarURL:")) << msg.avatarUrl()
155     << qPrintable(QString(", Flags:")) << msg.flags() << qPrintable(QString(")"))
156     << msg.senderPrefixes() << msg.sender() << ":" << msg.contents();
157     return dbg;
158 }