summaryrefslogtreecommitdiffstats
blob: 12b4208d3271c2be7c91e05176bcd05c86901731 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/*
 * Copyright (c) 2014 Isode Limited.
 * All rights reserved.
 * See the COPYING file for more information.
 */

#include <boost/lexical_cast.hpp>
#include <boost/smart_ptr/make_shared.hpp>
#include <Swiften/Elements/IQ.h>
#include <Swiften/Elements/Message.h>
#include <Swiften/Elements/Presence.h>
#include <Swiften/Elements/Delay.h>
#include <Swiften/Serializer/XML/XMLElement.h>
#include <Swiften/Serializer/XML/XMLRawTextNode.h>
#include <Swiften/Serializer/GenericStanzaSerializer.h>
#include <Swiften/Serializer/IQSerializer.h>
#include <Swiften/Serializer/MessageSerializer.h>
#include <Swiften/Serializer/PayloadSerializerCollection.h>
#include <Swiften/Serializer/PayloadSerializers/DelaySerializer.h>
#include <Swiften/Serializer/PayloadSerializers/ForwardedSerializer.h>
#include <Swiften/Serializer/PresenceSerializer.h>


using namespace Swift;

ForwardedSerializer::ForwardedSerializer(PayloadSerializerCollection* serializers) : serializers_(serializers) {
}

ForwardedSerializer::~ForwardedSerializer() {
}

std::string ForwardedSerializer::serializePayload(boost::shared_ptr<Forwarded> payload) const {
	if (!payload) {
		return "";
	}

	XMLElement element("forwarded", "urn:xmpp:forward:0");

	if (payload->getDelay()) {
		element.addNode(boost::make_shared<XMLRawTextNode>(DelaySerializer().serialize(payload->getDelay())));
	}

	if (payload->getStanza()) { /* find out what type of stanza we are dealing with and branch into the correct serializer*/
		boost::shared_ptr<IQ> iq;
		boost::shared_ptr<Message> message;
		boost::shared_ptr<Presence> presence;
		const std::string ns = "jabber:client";
		if ((iq = boost::dynamic_pointer_cast<IQ>(payload->getStanza()))) {
			element.addNode(boost::make_shared<XMLRawTextNode>(safeByteArrayToString(IQSerializer(serializers_).serialize(iq, ns))));
		} else if ((message = boost::dynamic_pointer_cast<Message>(payload->getStanza()))) {
			element.addNode(boost::make_shared<XMLRawTextNode>(safeByteArrayToString(MessageSerializer(serializers_).serialize(message, ns))));
		} else if ((presence = boost::dynamic_pointer_cast<Presence>(payload->getStanza()))) {
			element.addNode(boost::make_shared<XMLRawTextNode>(safeByteArrayToString(PresenceSerializer(serializers_).serialize(presence, ns))));
		}
	}

	return element.serialize();
}