/* ====================================================================
 * FTD4Linux Software License, Version 1.0
 *
 * Copyright (c) 2000-2004 The FTD4Linux Development Team.  All rights
 * reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 *
 * 3. The end-user documentation included with the redistribution,
 *    if any, must include the following acknowledgment:
 *       "This product includes software developed by the
 *        FTD4Linux Development Team (http://www.ftd4linux.2y.net/)."
 *    Alternately, this acknowledgment may appear in the software itself,
 *    if and wherever such third-party acknowledgments normally appear.
 *
 * 4. The names "FTD" and "FTD4Linux" must
 *    not be used to endorse or promote products derived from this
 *    software without prior written permission. For written
 *    permission, please contact alguno@ftd4linux.2y.net.
 *
 * 5. Products derived from this software may not be called "FTD4Linux",
 *    nor may "FTD4Linux" appear in their name, without prior written
 *    permission of the FTD4Linux Development Team.
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED.  IN NO EVENT SHALL THE FTD4LINUX DEVELOPMENT TEAM OR
 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#include "ftd4linux.h"
#include "ftd_mail.h"

/**
 * Leest een lijst mail-berichten uit een XML gecodeerde string.
 *
 * @arg     buffer          De XML gecodeerde string met de mailberichten.
 *
 * @return  Een nieuwe lijst met ::FtdMailBericht pointers naar de gelezen berichten.
 */
FtdList *
ftd_mail_parse(FtdString *buffer)
{
    xmlDocPtr        doc;
    xmlNodePtr       cur;
    FtdList         *list = NULL;

    g_assert(buffer);

    doc = xmlParseMemory(ftd_string_value(buffer), ftd_string_length(buffer));
    cur = xmlDocGetRootElement(doc);
    if (cur == NULL) {
        fprintf(stderr,"ftd_mail_parse(): empty document!\n");
        xmlFreeDoc(doc);
        return NULL;
    }

    if (strcasecmp(cur->name, "Mail")) {
        fprintf(stderr,"ftd_mail_parse(): document of the wrong type, root node != Events");
        xmlFreeDoc(doc);
        return NULL;
    }

    cur = cur->xmlChildrenNode;
    if (!cur) {
        g_warning("ftd_mail_parse(): no children of root-node!\n");
        xmlFreeDoc(doc);
        return NULL;
    }

    while (cur) {
        if (!strcasecmp(cur->name, "Event")) {
            xmlChar *code;

            code = xmlGetProp(cur, "Code");
            if (code == NULL) {
                cur = cur->next;
                continue;
            }

            if (!strcasecmp (code, "0")) {              /* <Event Code="0"> == status bericht */
                /* Niets te doen voor deze situatie */
            } else if (!strcasecmp (code, "1")) {       /* <Event Code="1"> == ontvangen bericht */
                FtdMailBericht  *bericht = ftd_mail_parse_bericht(doc, cur->xmlChildrenNode);
                if( bericht ) {
                    bericht->type = MAIL_TYPE_ONTVANGEN;
                    list = ftd_list_append(list, bericht);
                }
            } else if (!strcasecmp (code, "2")) {       /* <Event Code="2"> == verzonden bericht */
                FtdMailBericht  *bericht = ftd_mail_parse_bericht(doc, cur->xmlChildrenNode);
                if( bericht ) {
                    bericht->type = MAIL_TYPE_VERZONDEN;
                    list = ftd_list_append(list, bericht);
                }
            }

            xmlFree(code);
        }
        cur = cur->next;
    }
    xmlFreeDoc(doc);

    return list;
}

/**
 * Leest een mailbericht uit een XML boom
 *
 * @arg     doc         Het XML document waar het mailbericht in staat.
 * @arg     cur         Een pointer naar het element in het XML document waar het mailbericht staat.
 *
 * @return  Een nieuw mailbericht met de gelezen inhoud.
 */
FtdMailBericht *
ftd_mail_parse_bericht(xmlDocPtr doc, xmlNodePtr cur)
{
    FtdString       *from = NULL;
    FtdString       *subject = NULL;
    FtdString       *body = NULL;
    time_t           stamp = (time_t) 0;
    FtdMailBericht  *bericht;

    while(cur != NULL) {
        if (!strcasecmp(cur->name, "From")) {
            from = ftd_xml_parse_string(doc, cur);
        } else if (!strcasecmp(cur->name, "Subject")) {
            subject = ftd_xml_parse_string(doc, cur);
        } else if (!strcasecmp(cur->name, "Body")) {
            body = ftd_xml_parse_string(doc, cur);
        } else if (!strcasecmp(cur->name, "Stamp")) {
            stamp = ftd_xml_parse_int(doc, cur);
        }
        cur = cur->next;
    }

    bericht = ftd_mail_bericht_new(ftd_string_value(from), ftd_string_value(subject), ftd_string_value(body), stamp);
    ftd_string_free(from);
    ftd_string_free(subject);
    ftd_string_free(body);

    return bericht;
}
