summaryrefslogtreecommitdiffstats
blob: 61395868276141a0f56df05bd1978f96c5aff7e7 (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
59
60
61
62
63
64
65
66
67
68
69
#include "Swiften/Serializer/XMPPSerializer.h"

#include <boost/bind.hpp>
#include <iostream>

#include "Swiften/Base/foreach.h"
#include "Swiften/Serializer/CompressRequestSerializer.h"
#include "Swiften/Serializer/CompressFailureSerializer.h"
#include "Swiften/Serializer/StreamFeaturesSerializer.h"
#include "Swiften/Serializer/AuthRequestSerializer.h"
#include "Swiften/Serializer/AuthFailureSerializer.h"
#include "Swiften/Serializer/AuthSuccessSerializer.h"
#include "Swiften/Serializer/StartTLSRequestSerializer.h"
#include "Swiften/Serializer/StartTLSFailureSerializer.h"
#include "Swiften/Serializer/TLSProceedSerializer.h"
#include "Swiften/Serializer/MessageSerializer.h"
#include "Swiften/Serializer/PresenceSerializer.h"
#include "Swiften/Serializer/IQSerializer.h"

namespace Swift {

XMPPSerializer::XMPPSerializer(PayloadSerializerCollection* payloadSerializers) {
	serializers_.push_back(boost::shared_ptr<ElementSerializer>(new PresenceSerializer(payloadSerializers)));
	serializers_.push_back(boost::shared_ptr<ElementSerializer>(new IQSerializer(payloadSerializers)));
	serializers_.push_back(boost::shared_ptr<ElementSerializer>(new MessageSerializer(payloadSerializers)));
	serializers_.push_back(boost::shared_ptr<ElementSerializer>(new CompressRequestSerializer()));
	serializers_.push_back(boost::shared_ptr<ElementSerializer>(new CompressFailureSerializer()));
	serializers_.push_back(boost::shared_ptr<ElementSerializer>(new AuthRequestSerializer()));
	serializers_.push_back(boost::shared_ptr<ElementSerializer>(new AuthFailureSerializer()));
	serializers_.push_back(boost::shared_ptr<ElementSerializer>(new AuthSuccessSerializer()));
	serializers_.push_back(boost::shared_ptr<ElementSerializer>(new StartTLSRequestSerializer()));
	serializers_.push_back(boost::shared_ptr<ElementSerializer>(new StartTLSFailureSerializer()));
	serializers_.push_back(boost::shared_ptr<ElementSerializer>(new TLSProceedSerializer()));
	serializers_.push_back(boost::shared_ptr<ElementSerializer>(new StreamFeaturesSerializer()));
}

String XMPPSerializer::serializeHeader(const String& from, const String& to, const String& id) const {
	String result = "<?xml version=\"1.0\"?><stream:stream xmlns=\"jabber:client\" xmlns:stream=\"http://etherx.jabber.org/streams\" version=\"1.0\"";
	if (!from.isEmpty()) {
		result += " from=\"" + from + "\"";
	}
	if (!to.isEmpty()) {
		result += " to=\"" + to + "\"";
	}
	if (!id.isEmpty()) {
		result += " id=\"" + id + "\"";
	}
	result += ">";
	return result;
}

String XMPPSerializer::serializeElement(boost::shared_ptr<Element> element) const {
	std::vector< boost::shared_ptr<ElementSerializer> >::const_iterator i = std::find_if(
			serializers_.begin(), serializers_.end(), 
			boost::bind(&ElementSerializer::canSerialize, _1, element));
	if (i != serializers_.end()) {
		return (*i)->serialize(element);
	}
	else {
		std::cerr << "Could not find serializer for " << typeid(*(element.get())).name() << std::endl;
		return "";
	}
}

String XMPPSerializer::serializeFooter() const {
	return "</stream:stream>";
}

}