873458f26bd6b460fb113ebc32634fccaf021d17
[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
29 Message::Message(const BufferInfo &bufferInfo, Type type, const QString &contents, const QString &sender, const QString &senderPrefixes, Flags flags)
30     : _timestamp(QDateTime::currentDateTime().toUTC()),
31     _bufferInfo(bufferInfo),
32     _contents(contents),
33     _sender(sender),
34     _senderPrefixes(senderPrefixes),
35     _type(type),
36     _flags(flags)
37 {
38 }
39
40
41 Message::Message(const QDateTime &ts, const BufferInfo &bufferInfo, Type type, const QString &contents, const QString &sender, const QString &senderPrefixes, Flags flags)
42     : _timestamp(ts),
43     _bufferInfo(bufferInfo),
44     _contents(contents),
45     _sender(sender),
46     _senderPrefixes(senderPrefixes),
47     _type(type),
48     _flags(flags)
49 {
50 }
51
52
53 QDataStream &operator<<(QDataStream &out, const Message &msg)
54 {
55     // We do not serialize the sender prefixes until we have a new protocol or client-features implemented
56     out << msg.msgId()
57         << (quint32) msg.timestamp().toTime_t()
58         << (quint32) msg.type()
59         << (quint8) msg.flags()
60         << msg.bufferInfo()
61         << msg.sender().toUtf8();
62
63     if (SignalProxy::current()->targetPeer()->hasFeature(Quassel::Feature::SenderPrefixes))
64         out << msg.senderPrefixes().toUtf8();
65
66     out << msg.contents().toUtf8();
67     return out;
68 }
69
70
71 QDataStream &operator>>(QDataStream &in, Message &msg)
72 {
73     in >> msg._msgId;
74
75     quint32 timeStamp;
76     in >> timeStamp;
77     msg._timestamp = QDateTime::fromTime_t(timeStamp);
78
79     quint32 type;
80     in >> type;
81     msg._type = Message::Type(type);
82
83     quint8 flags;
84     in >> flags;
85     msg._flags = Message::Flags(flags);
86
87     in >> msg._bufferInfo;
88
89     QByteArray sender;
90     in >> sender;
91     msg._sender = QString::fromUtf8(sender);
92
93     QByteArray senderPrefixes;
94     if (SignalProxy::current()->sourcePeer()->hasFeature(Quassel::Feature::SenderPrefixes))
95         in >> senderPrefixes;
96     msg._senderPrefixes = QString::fromUtf8(senderPrefixes);
97
98     QByteArray contents;
99     in >> contents;
100     msg._contents = QString::fromUtf8(contents);
101
102     return in;
103 }
104
105
106 QDebug operator<<(QDebug dbg, const Message &msg)
107 {
108     dbg.nospace() << qPrintable(QString("Message(MsgId:")) << msg.msgId()
109     << qPrintable(QString(",")) << msg.timestamp()
110     << qPrintable(QString(", Type:")) << msg.type()
111     << qPrintable(QString(", Flags:")) << msg.flags() << qPrintable(QString(")"))
112     << msg.sender() << ":" << msg.contents();
113     return dbg;
114 }