#include "libews.h"
-FILE *calfile;
+icalcomponent *fetch_item(int xmlfd, GError **error);
int main(int argc, char **argv)
{
+ FILE *calfile;
GError *error = NULL;
icalcomponent *calcomp;
- xmlDocPtr xml_doc;
- xmlNode *xml_node;
int xmlfd = 0; /* stdin */
- char buf[1];
char *outbuf;
if (argc >= 2) {
fprintf(stderr, "Too many args. Want only xml file and ical file\n");
return -1;
}
+ calcomp = fetch_item(xmlfd, &error);
+
+ if (!calcomp) {
+ fprintf(stderr, "Failed to convert: %s\n", error->message);
+ g_clear_error(&error);
+ return 1;
+ }
+ outbuf =icalcomponent_as_ical_string_r(calcomp);
+ fprintf(calfile, "%s", outbuf);
+ free(outbuf);
+ return 0;
+}
+
+icalcomponent *fetch_item(int xmlfd, GError **error)
+{
+ xmlDocPtr xml_doc;
+ xmlNode *xml_node;
+ char buf[1];
read(xmlfd, buf, 1);
if (*buf == '<')
lseek(xmlfd, 0, SEEK_SET);
xml_doc = xmlReadFd(xmlfd, "noname.xml", "utf-8", XML_PARSE_RECOVER);
if (!xml_doc) {
fprintf(stderr, "Failed to parse XML\n");
- return -1;
+ exit(1);
}
xml_node = xmlDocGetRootElement(xml_doc);
if (!xml_node) {
fprintf(stderr, "Failed to parse XML\n");
- return -1;
+ exit(1);
}
if (xml_node->type != XML_ELEMENT_NODE ||
strcmp((char *)xml_node->name, "Envelope")) {
fprintf(stderr, "Root node not as expected: %s\n", xml_node->name);
- return -1;
+ exit(1);
}
for (xml_node = xml_node->children; xml_node; xml_node = xml_node->next) {
if (xml_node->type == XML_ELEMENT_NODE &&
}
if (!xml_node) {
fprintf(stderr, "SOAP Body node not found\n");
- return -1;
+ exit(1);
}
for (xml_node = xml_node->children; xml_node; xml_node = xml_node->next) {
}
if (!xml_node) {
fprintf(stderr, "GetItemResponse node not found\n");
- return -1;
+ exit(1);
}
for (xml_node = xml_node->children; xml_node; xml_node = xml_node->next) {
}
if (!xml_node) {
fprintf(stderr, "ResponseMessages node not found\n");
- return -1;
+ exit(1);
}
for (xml_node = xml_node->children; xml_node; xml_node = xml_node->next) {
}
if (!xml_node) {
fprintf(stderr, "GetItemResponseMessage node not found\n");
- return -1;
+ exit(1);
}
for (xml_node = xml_node->children; xml_node; xml_node = xml_node->next) {
}
if (!xml_node) {
fprintf(stderr, "Items node not found\n");
- return -1;
+ exit(1);
}
for (xml_node = xml_node->children; xml_node; xml_node = xml_node->next) {
}
if (!xml_node) {
fprintf(stderr, "CalendarItem node not found\n");
- return -1;
+ exit(1);
}
- calcomp = ews_calitem_to_ical(xml_node, NULL, NULL, NULL, NULL, &error);
-
- if (!calcomp) {
- fprintf(stderr, "Failed to convert: %s\n", error->message);
- g_clear_error(&error);
- return 1;
- }
- outbuf =icalcomponent_as_ical_string_r(calcomp);
- fprintf(calfile, "%s", outbuf);
- free(outbuf);
-
- return 0;
+ return ews_calitem_to_ical(xml_node, NULL, NULL, NULL, NULL, error);
}