diff --git a/src/XML/Elements.c b/src/XML/Elements.c new file mode 100644 index 0000000..fade5cb --- /dev/null +++ b/src/XML/Elements.c @@ -0,0 +1,112 @@ +#include + +#include +#include + +XMLElement * +XMLCreateTag(char *name) +{ + XMLElement *elem; + if (!name) + { + return NULL; + } + + elem = Malloc(sizeof(*elem)); + + elem->type = XML_ELEMENT_TAG; + elem->name = StrDuplicate(name); + elem->attrs = HashMapCreate(); + elem->children = ArrayCreate(); + + elem->data = NULL; + + return elem; +} +XMLElement * +XMLCreateText(char *data) +{ + XMLElement *elem; + if (!data) + { + return NULL; + } + + elem = Malloc(sizeof(*elem)); + + elem->type = XML_ELEMENT_DATA; + elem->name = NULL; + elem->attrs = NULL; + elem->children = NULL; + + elem->data = StrDuplicate(data); + + return elem; +} + +void +XMLAddAttr(XMLElement *element, char *key, char *val) +{ + if (!element || !key || !val) + { + return; + } + if (element->type != XML_ELEMENT_TAG) + { + return; + } + + HashMapSet(element->attrs, key, StrDuplicate(val)); +} +void +XMLAddChild(XMLElement *element, XMLElement *child) +{ + if (!element || !child) + { + return; + } + if (element->type != XML_ELEMENT_TAG) + { + return; + } + + ArrayAdd(element->children, child); +} + +void +XMLFreeElement(XMLElement *element) +{ + if (!element) + { + return; + } + + if (element->name) + { + Free(element->name); + } + if (element->data) + { + Free(element->data); + } + if (element->attrs) + { + char *key, *val; + while (HashMapIterate(element->attrs, &key, (void **) &val)) + { + Free(val); + } + HashMapFree(element->attrs); + } + if (element->children) + { + size_t i; + for (i = 0; i < ArraySize(element->children); i++) + { + XMLFreeElement(ArrayGet(element->children, i)); + } + ArrayFree(element->children); + } + + Free(element); +} diff --git a/src/XMPP/Component.c b/src/XMPP/Component.c new file mode 100644 index 0000000..248f6b9 --- /dev/null +++ b/src/XMPP/Component.c @@ -0,0 +1,37 @@ +#include + +#include + +/* TODO: Write the stream system once we have our XML implementation + * checked out. + * + * Did I mention I _hate_ writing XML SAX parsers? Oh, well, they're + * easier than making a DOM directly, so eeh. */ + +/* The default component port Prosody uses. */ +#define DEFAULT_PROSODY_PORT 5347 + +Stream * +XMPPInitialiseCompStream(char *host, int port) +{ + /* TODO */ + (void) host; + (void) port; + return NULL; +} + +bool +XMPPAuthenticateCompStream(Stream *stream, char *shared) +{ + /* TODO */ + (void) stream; + (void) shared; + return NULL; +} + +void +XMPPEndCompStream(Stream *stream) +{ + (void) stream; + return NULL; +} diff --git a/src/include/XML.h b/src/include/XML.h index 608daf3..1218768 100644 --- a/src/include/XML.h +++ b/src/include/XML.h @@ -3,6 +3,7 @@ #include #include +#include typedef struct XMLexer XMLexer; typedef struct XMLEvent { @@ -33,4 +34,39 @@ extern XMLexer * XMLCreateLexer(Stream *, bool); extern XMLEvent * XMLCrank(XMLexer *); extern void XMLFreeEvent(XMLEvent *); extern void XMLFreeLexer(XMLexer *); + +typedef struct XMLElement { + enum { + XML_ELEMENT_UNKNOWN = 0, + XML_ELEMENT_TAG, + XML_ELEMENT_DATA, + XML_ELEMENT_PI, + XML_ELEMENT_CDATA + } type; + + + /* Only set when the type is a TAG. */ + char *name; + HashMap *attrs; + Array *children; + + /* Only set when the type is DATA or + * CDATA */ + char *data; +} XMLElement; + +/* Decodes 1 element off a stream. */ +extern XMLElement * XMLDecode(Stream *stream, bool autofree); + +extern XMLElement * XMLCreateTag(char *name); +extern XMLElement * XMLCreateText(char *data); +extern void XMLAddAttr(XMLElement *element, char *key, char *val); +extern void XMLAddChild(XMLElement *element, XMLElement *child); + + +/* Frees an XML element properly. */ +extern void XMLFreeElement(XMLElement *element); + +/* Encodes an XML element to a stream */ +extern void XMLEncode(Stream *stream, XMLElement *element); #endif diff --git a/src/include/XMPP.h b/src/include/XMPP.h new file mode 100644 index 0000000..c5988bb --- /dev/null +++ b/src/include/XMPP.h @@ -0,0 +1,19 @@ +#ifndef PARSEE_XMPP_H +#define PARSEE_XMPP_H + +#include + +/* Initialises a raw component stream to host, with an optional port. + * If said port is 0, then it is set to the default Prosody port */ +extern Stream * XMPPInitialiseCompStream(char *host, int port); + +/* Authenticates a component stream with a given shared secret, + * with a stream ID from the server. This should be called right + * after XMPPInitialiseCompStream. */ +extern bool XMPPAuthenticateCompStream(Stream *stream, char *shared); + +/* TODO: XMPP stuff, I don't fucking know, I'm not a Jabbernerd. */ + +/* Closes a raw component stream. */ +extern void XMPPEndCompStream(Stream *stream); +#endif