summaryrefslogtreecommitdiffstats
blob: fd3d019bcea593453a5f0e0db8d72cb28b0c2dcb (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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/*
 * Copyright (c) 2010 Remko Tronçon
 * Licensed under the GNU General Public License v3.
 * See Documentation/Licenses/GPLv3.txt for more information.
 */

#include <Swiften/Parser/PayloadParsers/StreamInitiationParser.h>

#include <boost/lexical_cast.hpp>
#include <boost/cast.hpp>

#include <Swiften/Parser/PayloadParsers/FormParserFactory.h>
#include <Swiften/Parser/PayloadParsers/FormParser.h>
#include <Swiften/Base/foreach.h>


#define FILE_TRANSFER_NS "http://jabber.org/protocol/si/profile/file-transfer"
#define FEATURE_NEG_NS "http://jabber.org/protocol/feature-neg"

namespace Swift {

StreamInitiationParser::StreamInitiationParser() : level(TopLevel), formParser(0), inFile(false), inFeature(false)  {
	formParserFactory = new FormParserFactory();
}

StreamInitiationParser::~StreamInitiationParser() {
	delete formParserFactory;
}

void StreamInitiationParser::handleStartElement(const std::string& element, const std::string& ns, const AttributeMap& attributes) {
	if (level == TopLevel) {
		getPayloadInternal()->setID(attributes.getAttribute("id"));
		if (!attributes.getAttribute("profile").empty()) {
			getPayloadInternal()->setIsFileTransfer(attributes.getAttribute("profile") == FILE_TRANSFER_NS);
		}
	}
	else if (level == PayloadLevel) {
		if (element == "file") {
			inFile = true;
			currentFile = StreamInitiationFileInfo();
			currentFile.setName(attributes.getAttribute("name"));
			try {
				currentFile.setSize(boost::lexical_cast<int>(attributes.getAttribute("size")));
			}
			catch (boost::bad_lexical_cast&) {
			}
		}
		else if (element == "feature" && ns == FEATURE_NEG_NS) {
			inFeature = true;
		}
	}
	else if (level == FileOrFeatureLevel) {
		if (inFile && element == "desc") {
			currentText.clear();
		}
		else if (inFeature && formParserFactory->canParse(element, ns, attributes)) {
			assert(!formParser);
			formParser = boost::polymorphic_downcast<FormParser*>(formParserFactory->createPayloadParser());
		}
	}

	if (formParser) {
		formParser->handleStartElement(element, ns, attributes);
	}
	++level;
}

void StreamInitiationParser::handleEndElement(const std::string& element, const std::string& ns) {
	--level;
	if (formParser) {
		formParser->handleEndElement(element, ns);
	}
	if (level == TopLevel) {
	}
	else if (level == PayloadLevel) {
		if (element == "file") {
			getPayloadInternal()->setFileInfo(currentFile);
			inFile = false;
		}
		else if (element == "feature" && ns == FEATURE_NEG_NS) {
			inFeature = false;
		}
	}
	else if (level == FileOrFeatureLevel) {
		if (inFile && element == "desc") {
			currentFile.setDescription(currentText);
		}
		else if (formParser) {
			Form::ref form = formParser->getPayloadInternal();
			if (form) {
				FormField::ref field = boost::dynamic_pointer_cast<FormField>(form->getField("stream-method"));
				if (field) {
					if (form->getType() == Form::FormType) {
						foreach (const FormField::Option& option, field->getOptions()) {
							getPayloadInternal()->addProvidedMethod(option.value);
						}
					}
					else if (form->getType() == Form::SubmitType) {
						if (field->getRawValues().size() > 0) {
							getPayloadInternal()->setRequestedMethod(field->getRawValues()[0]);
						}
					}
				}
			}
			delete formParser;
			formParser = NULL;
		}
	}
}

void StreamInitiationParser::handleCharacterData(const std::string& data) {
	if (formParser) {
		formParser->handleCharacterData(data);
	}
	else {
		currentText += data;
	}
}


}