1 #include <xmpp/xmpp_parser.hpp>
2 #include <xmpp/xmpp_stanza.hpp>
3 
4 #include <logger/logger.hpp>
5 
6 /**
7  * Expat handlers. Called by the Expat library, never by ourself.
8  * They just forward the call to the XmppParser corresponding methods.
9  */
10 
start_element_handler(void * user_data,const XML_Char * name,const XML_Char ** atts)11 static void start_element_handler(void* user_data, const XML_Char* name, const XML_Char** atts)
12 {
13   static_cast<XmppParser*>(user_data)->start_element(name, atts);
14 }
15 
end_element_handler(void * user_data,const XML_Char * name)16 static void end_element_handler(void* user_data, const XML_Char* name)
17 {
18   static_cast<XmppParser*>(user_data)->end_element(name);
19 }
20 
character_data_handler(void * user_data,const XML_Char * s,int len)21 static void character_data_handler(void *user_data, const XML_Char *s, int len)
22 {
23   static_cast<XmppParser*>(user_data)->char_data(s, static_cast<std::size_t>(len));
24 }
25 
26 /**
27  * XmppParser class
28  */
29 
XmppParser()30 XmppParser::XmppParser():
31   level(0),
32   current_node(nullptr),
33   root(nullptr)
34 {
35   this->init_xml_parser();
36 }
37 
init_xml_parser()38 void XmppParser::init_xml_parser()
39 {
40   // Create the expat parser
41   this->parser = XML_ParserCreateNS("UTF-8", ':');
42   XML_SetUserData(this->parser, static_cast<void*>(this));
43 
44   // Install Expat handlers
45   XML_SetElementHandler(this->parser, &start_element_handler, &end_element_handler);
46   XML_SetCharacterDataHandler(this->parser, &character_data_handler);
47 }
48 
~XmppParser()49 XmppParser::~XmppParser()
50 {
51   XML_ParserFree(this->parser);
52 }
53 
feed(const char * data,const int len,const bool is_final)54 int XmppParser::feed(const char* data, const int len, const bool is_final)
55 {
56   int res = XML_Parse(this->parser, data, len, is_final);
57   if (res == XML_STATUS_ERROR &&
58       (XML_GetErrorCode(this->parser) != XML_ERROR_FINISHED))
59     log_error("Xml_Parse encountered an error: ",
60               XML_ErrorString(XML_GetErrorCode(this->parser)));
61   return res;
62 }
63 
parse(const int len,const bool is_final)64 int XmppParser::parse(const int len, const bool is_final)
65 {
66   int res = XML_ParseBuffer(this->parser, len, is_final);
67   if (res == XML_STATUS_ERROR)
68     log_error("Xml_Parsebuffer encountered an error: ",
69               XML_ErrorString(XML_GetErrorCode(this->parser)));
70   return res;
71 }
72 
reset()73 void XmppParser::reset()
74 {
75   XML_ParserFree(this->parser);
76   this->init_xml_parser();
77   this->current_node = nullptr;
78   this->root.reset(nullptr);
79   this->level = 0;
80 }
81 
get_buffer(const size_t size) const82 void* XmppParser::get_buffer(const size_t size) const
83 {
84   return XML_GetBuffer(this->parser, static_cast<int>(size));
85 }
86 
start_element(const XML_Char * name,const XML_Char ** attribute)87 void XmppParser::start_element(const XML_Char* name, const XML_Char** attribute)
88 {
89   this->level++;
90 
91   auto new_node = std::make_unique<XmlNode>(name, this->current_node);
92   auto new_node_ptr = new_node.get();
93   if (this->current_node)
94     this->current_node->add_child(std::move(new_node));
95   else
96     this->root = std::move(new_node);
97   this->current_node = new_node_ptr;
98   for (size_t i = 0; attribute[i]; i += 2)
99     this->current_node->set_attribute(attribute[i], attribute[i+1]);
100   if (this->level == 1)
101     this->stream_open_event(*this->current_node);
102 }
103 
end_element(const XML_Char *)104 void XmppParser::end_element(const XML_Char*)
105 {
106   this->level--;
107   if (this->level == 0)
108     { // End of the whole stream
109       this->stream_close_event(*this->current_node);
110       this->current_node = nullptr;
111       this->root.reset();
112     }
113   else
114     {
115       auto parent = this->current_node->get_parent();
116       if (this->level == 1)
117         { // End of a stanza
118           this->stanza_event(*this->current_node);
119           // Note: deleting all the children of our parent deletes ourself,
120           // so current_node is an invalid pointer after this line
121           parent->delete_all_children();
122         }
123       this->current_node = parent;
124     }
125 }
126 
char_data(const XML_Char * data,const size_t len)127 void XmppParser::char_data(const XML_Char* data, const size_t len)
128 {
129   if (this->current_node->has_children())
130     this->current_node->get_last_child()->add_to_tail({data, len});
131   else
132     this->current_node->add_to_inner({data, len});
133 }
134 
stanza_event(const Stanza & stanza) const135 void XmppParser::stanza_event(const Stanza& stanza) const
136 {
137   for (const auto& callback: this->stanza_callbacks)
138     {
139       try {
140         callback(stanza);
141       } catch (const std::exception& e) {
142         log_error("Unhandled exception: ", e.what());
143       }
144     }
145 }
146 
stream_open_event(const XmlNode & node) const147 void XmppParser::stream_open_event(const XmlNode& node) const
148 {
149   for (const auto& callback: this->stream_open_callbacks)
150     callback(node);
151 }
152 
stream_close_event(const XmlNode & node) const153 void XmppParser::stream_close_event(const XmlNode& node) const
154 {
155   for (const auto& callback: this->stream_close_callbacks)
156     callback(node);
157 }
158 
add_stanza_callback(std::function<void (const Stanza &)> && callback)159 void XmppParser::add_stanza_callback(std::function<void(const Stanza&)>&& callback)
160 {
161   this->stanza_callbacks.emplace_back(std::move(callback));
162 }
163 
add_stream_open_callback(std::function<void (const XmlNode &)> && callback)164 void XmppParser::add_stream_open_callback(std::function<void(const XmlNode&)>&& callback)
165 {
166   this->stream_open_callbacks.emplace_back(std::move(callback));
167 }
168 
add_stream_close_callback(std::function<void (const XmlNode &)> && callback)169 void XmppParser::add_stream_close_callback(std::function<void(const XmlNode&)>&& callback)
170 {
171   this->stream_close_callbacks.emplace_back(std::move(callback));
172 }
173